debian-mirror-gitlab/app/assets/javascripts/tree.js

65 lines
2 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
/* eslint-disable func-names, consistent-return, one-var, class-methods-use-this */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import { visitUrl } from './lib/utils/url_utility';
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
export default class TreeView {
constructor() {
this.initKeyNav();
// Code browser tree slider
// Make the entire tree-item row clickable, but not if clicking another link (like a commit message)
2021-03-08 18:12:59 +05:30
$('.tree-content-holder .tree-item').on('click', function (e) {
2019-12-26 22:10:19 +05:30
const $clickedEl = $(e.target);
const path = $('.tree-item-file-name a', this).attr('href');
2017-09-10 17:25:29 +05:30
if (!$clickedEl.is('a') && !$clickedEl.is('.str-truncated')) {
if (e.metaKey || e.which === 2) {
e.preventDefault();
return window.open(path, '_blank');
2016-09-13 17:45:13 +05:30
}
2020-05-24 23:13:21 +05:30
return visitUrl(path);
2017-09-10 17:25:29 +05:30
}
});
// Show the "Loading commit data" for only the first element
2021-03-08 18:12:59 +05:30
$('span.log_loading').first().removeClass('hide');
2017-09-10 17:25:29 +05:30
}
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
initKeyNav() {
2019-12-26 22:10:19 +05:30
const li = $('tr.tree-item');
let liSelected = null;
2021-03-08 18:12:59 +05:30
return $('body').keydown((e) => {
2019-12-26 22:10:19 +05:30
let next, path;
2018-12-13 13:39:08 +05:30
if ($('input:focus').length > 0 && (e.which === 38 || e.which === 40)) {
2017-09-10 17:25:29 +05:30
return false;
}
if (e.which === 40) {
if (liSelected) {
next = liSelected.next();
if (next.length > 0) {
2018-12-13 13:39:08 +05:30
liSelected.removeClass('selected');
liSelected = next.addClass('selected');
2016-09-13 17:45:13 +05:30
}
2017-09-10 17:25:29 +05:30
} else {
2018-12-13 13:39:08 +05:30
liSelected = li.eq(0).addClass('selected');
2017-09-10 17:25:29 +05:30
}
return $(liSelected).focus();
} else if (e.which === 38) {
if (liSelected) {
next = liSelected.prev();
if (next.length > 0) {
2018-12-13 13:39:08 +05:30
liSelected.removeClass('selected');
liSelected = next.addClass('selected');
2016-09-13 17:45:13 +05:30
}
2017-09-10 17:25:29 +05:30
} else {
2018-12-13 13:39:08 +05:30
liSelected = li.last().addClass('selected');
2016-09-13 17:45:13 +05:30
}
2017-09-10 17:25:29 +05:30
return $(liSelected).focus();
} else if (e.which === 13) {
path = $('.tree-item.selected .tree-item-file-name a').attr('href');
if (path) {
2018-03-17 18:26:18 +05:30
return visitUrl(path);
2017-09-10 17:25:29 +05:30
}
}
});
}
}