tree.js 2.1 KB
Newer Older
1
/* eslint-disable func-names, consistent-return, one-var, no-else-return, class-methods-use-this */
2 3

import $ from 'jquery';
4
import { visitUrl } from './lib/utils/url_utility';
B
Bryce Johnson 已提交
5

6
export default class TreeView {
7
  constructor() {
8 9 10
    this.initKeyNav();
    // Code browser tree slider
    // Make the entire tree-item row clickable, but not if clicking another link (like a commit message)
11
    $('.tree-content-holder .tree-item').on('click', function(e) {
12 13
      const $clickedEl = $(e.target);
      const path = $('.tree-item-file-name a', this).attr('href');
14 15 16 17 18
      if (!$clickedEl.is('a') && !$clickedEl.is('.str-truncated')) {
        if (e.metaKey || e.which === 2) {
          e.preventDefault();
          return window.open(path, '_blank');
        } else {
19
          return visitUrl(path);
F
Fatih Acet 已提交
20
        }
21 22 23
      }
    });
    // Show the "Loading commit data" for only the first element
24 25 26
    $('span.log_loading')
      .first()
      .removeClass('hide');
27
  }
F
Fatih Acet 已提交
28

29
  initKeyNav() {
30 31
    const li = $('tr.tree-item');
    let liSelected = null;
32
    return $('body').keydown(e => {
33
      let next, path;
34
      if ($('input:focus').length > 0 && (e.which === 38 || e.which === 40)) {
35 36 37 38 39 40
        return false;
      }
      if (e.which === 40) {
        if (liSelected) {
          next = liSelected.next();
          if (next.length > 0) {
41 42
            liSelected.removeClass('selected');
            liSelected = next.addClass('selected');
F
Fatih Acet 已提交
43
          }
44
        } else {
45
          liSelected = li.eq(0).addClass('selected');
46 47 48 49 50 51
        }
        return $(liSelected).focus();
      } else if (e.which === 38) {
        if (liSelected) {
          next = liSelected.prev();
          if (next.length > 0) {
52 53
            liSelected.removeClass('selected');
            liSelected = next.addClass('selected');
F
Fatih Acet 已提交
54
          }
55
        } else {
56
          liSelected = li.last().addClass('selected');
57 58 59 60 61
        }
        return $(liSelected).focus();
      } else if (e.which === 13) {
        path = $('.tree-item.selected .tree-item-file-name a').attr('href');
        if (path) {
62
          return visitUrl(path);
F
Fatih Acet 已提交
63
        }
64 65
      }
    });
66 67
  }
}