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

124 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2017-09-10 17:25:29 +05:30
import Cookies from 'js-cookie';
2018-03-17 18:26:18 +05:30
import Mousetrap from 'mousetrap';
import axios from './lib/utils/axios_utils';
import { refreshCurrentPage, visitUrl } from './lib/utils/url_utility';
2017-08-17 22:00:37 +05:30
import findAndFollowLink from './shortcuts_dashboard_navigation';
2018-03-17 18:26:18 +05:30
const defaultStopCallback = Mousetrap.stopCallback;
Mousetrap.stopCallback = (e, element, combo) => {
if (['ctrl+shift+p', 'command+shift+p'].indexOf(combo) !== -1) {
return false;
}
return defaultStopCallback(e, element, combo);
};
export default class Shortcuts {
constructor() {
this.onToggleHelp = this.onToggleHelp.bind(this);
this.enabledHelp = [];
Mousetrap.bind('?', this.onToggleHelp);
Mousetrap.bind('s', Shortcuts.focusSearch);
Mousetrap.bind('f', this.focusFilter.bind(this));
Mousetrap.bind('p b', Shortcuts.onTogglePerfBar);
const findFileURL = document.body.dataset.findFile;
Mousetrap.bind('shift+t', () => findAndFollowLink('.shortcuts-todos'));
Mousetrap.bind('shift+a', () => findAndFollowLink('.dashboard-shortcuts-activity'));
Mousetrap.bind('shift+i', () => findAndFollowLink('.dashboard-shortcuts-issues'));
Mousetrap.bind('shift+m', () => findAndFollowLink('.dashboard-shortcuts-merge_requests'));
Mousetrap.bind('shift+p', () => findAndFollowLink('.dashboard-shortcuts-projects'));
Mousetrap.bind('shift+g', () => findAndFollowLink('.dashboard-shortcuts-groups'));
Mousetrap.bind('shift+l', () => findAndFollowLink('.dashboard-shortcuts-milestones'));
Mousetrap.bind('shift+s', () => findAndFollowLink('.dashboard-shortcuts-snippets'));
Mousetrap.bind(['ctrl+shift+p', 'command+shift+p'], Shortcuts.toggleMarkdownPreview);
if (typeof findFileURL !== 'undefined' && findFileURL !== null) {
Mousetrap.bind('t', () => {
visitUrl(findFileURL);
});
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
$(document).on('click.more_help', '.js-more-help-button', function clickMoreHelp(e) {
$(this).remove();
$('.hidden-shortcut').show();
e.preventDefault();
});
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
onToggleHelp(e) {
if (e.preventDefault) {
e.preventDefault();
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
Shortcuts.toggleHelp(this.enabledHelp);
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
static onTogglePerfBar(e) {
e.preventDefault();
const performanceBarCookieName = 'perf_bar_enabled';
if (Cookies.get(performanceBarCookieName) === 'true') {
Cookies.set(performanceBarCookieName, 'false', { path: '/' });
} else {
Cookies.set(performanceBarCookieName, 'true', { path: '/' });
}
refreshCurrentPage();
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
static toggleMarkdownPreview(e) {
// Check if short-cut was triggered while in Write Mode
const $target = $(e.target);
const $form = $target.closest('form');
if ($target.hasClass('js-note-text')) {
$('.js-md-preview-button', $form).focus();
2016-09-13 17:45:13 +05:30
}
2018-03-17 18:26:18 +05:30
$(document).triggerHandler('markdown-preview:toggle', [e]);
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
static toggleHelp(location) {
const $modal = $('#modal-shortcuts');
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
if ($modal.length) {
$modal.modal('toggle');
}
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
return axios.get(gon.shortcuts_path, {
responseType: 'text',
}).then(({ data }) => {
$.globalEval(data);
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
if (location && location.length > 0) {
const results = [];
for (let i = 0, len = location.length; i < len; i += 1) {
results.push($(location[i]).show());
2016-09-13 17:45:13 +05:30
}
2018-03-17 18:26:18 +05:30
return results;
2016-09-13 17:45:13 +05:30
}
2018-03-17 18:26:18 +05:30
$('.hidden-shortcut').show();
return $('.js-more-help-button').remove();
});
}
focusFilter(e) {
if (!this.filterInput) {
this.filterInput = $('input[type=search]', '.nav-controls');
}
this.filterInput.focus();
e.preventDefault();
}
static focusSearch(e) {
$('#search').focus();
if (e.preventDefault) {
e.preventDefault();
}
}
}