2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-05-17 07:20:55 -04:00
|
|
|
import Cookies from 'js-cookie';
|
2017-08-30 08:30:20 -04:00
|
|
|
import Mousetrap from 'mousetrap';
|
2018-02-02 06:23:42 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-12-07 07:30:53 -05:00
|
|
|
import { refreshCurrentPage, visitUrl } from './lib/utils/url_utility';
|
2017-03-30 13:31:59 -04:00
|
|
|
import findAndFollowLink from './shortcuts_dashboard_navigation';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2017-10-10 05:10:11 -04:00
|
|
|
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 {
|
2018-01-12 08:18:53 -05:00
|
|
|
constructor() {
|
2017-10-10 05:10:11 -04:00
|
|
|
this.onToggleHelp = this.onToggleHelp.bind(this);
|
|
|
|
this.enabledHelp = [];
|
2018-01-12 08:18:53 -05:00
|
|
|
|
2017-10-10 05:10:11 -04:00
|
|
|
Mousetrap.bind('?', this.onToggleHelp);
|
|
|
|
Mousetrap.bind('s', Shortcuts.focusSearch);
|
|
|
|
Mousetrap.bind('f', this.focusFilter.bind(this));
|
|
|
|
Mousetrap.bind('p b', Shortcuts.onTogglePerfBar);
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-10-10 05:10:11 -04:00
|
|
|
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);
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-10-10 05:10:11 -04:00
|
|
|
if (typeof findFileURL !== 'undefined' && findFileURL !== null) {
|
|
|
|
Mousetrap.bind('t', () => {
|
2017-12-07 07:30:53 -05:00
|
|
|
visitUrl(findFileURL);
|
2017-10-10 05:10:11 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document).on('click.more_help', '.js-more-help-button', function clickMoreHelp(e) {
|
|
|
|
$(this).remove();
|
|
|
|
$('.hidden-shortcut').show();
|
2017-05-17 07:20:55 -04:00
|
|
|
e.preventDefault();
|
2017-10-10 05:10:11 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onToggleHelp(e) {
|
2017-12-19 11:18:48 -05:00
|
|
|
if (e.preventDefault) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
2017-10-10 05:10:11 -04:00
|
|
|
Shortcuts.toggleHelp(this.enabledHelp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static onTogglePerfBar(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const performanceBarCookieName = 'perf_bar_enabled';
|
|
|
|
if (Cookies.get(performanceBarCookieName) === 'true') {
|
2018-01-16 13:13:31 -05:00
|
|
|
Cookies.set(performanceBarCookieName, 'false', { path: '/' });
|
2017-10-10 05:10:11 -04:00
|
|
|
} else {
|
|
|
|
Cookies.set(performanceBarCookieName, 'true', { path: '/' });
|
|
|
|
}
|
2017-12-07 07:30:53 -05:00
|
|
|
refreshCurrentPage();
|
2017-10-10 05:10:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
$(document).triggerHandler('markdown-preview:toggle', [e]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static toggleHelp(location) {
|
|
|
|
const $modal = $('#modal-shortcuts');
|
|
|
|
|
|
|
|
if ($modal.length) {
|
|
|
|
$modal.modal('toggle');
|
2018-05-07 15:31:29 -04:00
|
|
|
return null;
|
2017-10-10 05:10:11 -04:00
|
|
|
}
|
|
|
|
|
2018-02-02 06:23:42 -05:00
|
|
|
return axios.get(gon.shortcuts_path, {
|
|
|
|
responseType: 'text',
|
|
|
|
}).then(({ data }) => {
|
|
|
|
$.globalEval(data);
|
|
|
|
|
|
|
|
if (location && location.length > 0) {
|
|
|
|
const results = [];
|
|
|
|
for (let i = 0, len = location.length; i < len; i += 1) {
|
|
|
|
results.push($(location[i]).show());
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2018-02-02 06:23:42 -05:00
|
|
|
return results;
|
|
|
|
}
|
2017-10-10 05:10:11 -04:00
|
|
|
|
2018-02-02 06:23:42 -05:00
|
|
|
$('.hidden-shortcut').show();
|
|
|
|
return $('.js-more-help-button').remove();
|
2017-10-10 05:10:11 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
focusFilter(e) {
|
|
|
|
if (!this.filterInput) {
|
|
|
|
this.filterInput = $('input[type=search]', '.nav-controls');
|
|
|
|
}
|
|
|
|
this.filterInput.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
static focusSearch(e) {
|
|
|
|
$('#search').focus();
|
2017-12-19 11:18:48 -05:00
|
|
|
|
|
|
|
if (e.preventDefault) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2017-10-10 05:10:11 -04:00
|
|
|
}
|
|
|
|
}
|