tree.js 2.21 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, wrap-iife, max-len, quotes, consistent-return, no-var, one-var, one-var-declaration-per-line, no-else-return, prefer-arrow-callback, class-methods-use-this */
2 3

import $ from 'jquery';
4
import { visitUrl } from './lib/utils/url_utility';
Bryce Johnson's avatar
Bryce Johnson committed
5

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

28
  initKeyNav() {
29 30 31 32 33 34 35 36 37 38 39 40 41 42
    var li, liSelected;
    li = $("tr.tree-item");
    liSelected = null;
    return $('body').keydown(function(e) {
      var next, path;
      if ($("input:focus").length > 0 && (e.which === 38 || e.which === 40)) {
        return false;
      }
      if (e.which === 40) {
        if (liSelected) {
          next = liSelected.next();
          if (next.length > 0) {
            liSelected.removeClass("selected");
            liSelected = next.addClass("selected");
Fatih Acet's avatar
Fatih Acet committed
43
          }
44 45 46 47 48 49 50 51 52 53
        } else {
          liSelected = li.eq(0).addClass("selected");
        }
        return $(liSelected).focus();
      } else if (e.which === 38) {
        if (liSelected) {
          next = liSelected.prev();
          if (next.length > 0) {
            liSelected.removeClass("selected");
            liSelected = next.addClass("selected");
Fatih Acet's avatar
Fatih Acet committed
54
          }
55 56 57 58 59 60 61
        } else {
          liSelected = li.last().addClass("selected");
        }
        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);
Fatih Acet's avatar
Fatih Acet committed
63
        }
64 65
      }
    });
66 67
  }
}