2017-01-11 23:27:41 -05:00
|
|
|
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, quotes, prefer-arrow-callback, consistent-return, object-shorthand, no-unused-vars, one-var, one-var-declaration-per-line, no-else-return, comma-dangle, max-len */
|
2017-03-30 13:31:59 -04:00
|
|
|
/* global Mousetrap */
|
2017-05-17 07:20:55 -04:00
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
|
2017-03-30 13:31:59 -04:00
|
|
|
import findAndFollowLink from './shortcuts_dashboard_navigation';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
(function() {
|
|
|
|
this.Shortcuts = (function() {
|
|
|
|
function Shortcuts(skipResetBindings) {
|
2017-05-08 15:11:53 -04:00
|
|
|
this.onToggleHelp = this.onToggleHelp.bind(this);
|
2016-07-24 16:45:11 -04:00
|
|
|
this.enabledHelp = [];
|
|
|
|
if (!skipResetBindings) {
|
|
|
|
Mousetrap.reset();
|
|
|
|
}
|
|
|
|
Mousetrap.bind('?', this.onToggleHelp);
|
|
|
|
Mousetrap.bind('s', Shortcuts.focusSearch);
|
2017-03-30 13:31:59 -04:00
|
|
|
Mousetrap.bind('f', (e => this.focusFilter(e)));
|
2017-05-17 07:20:55 -04:00
|
|
|
Mousetrap.bind('p b', this.onTogglePerfBar);
|
2017-03-29 14:58:18 -04:00
|
|
|
|
2017-04-06 09:27:48 -04:00
|
|
|
const $globalDropdownMenu = $('.global-dropdown-menu');
|
|
|
|
const $globalDropdownToggle = $('.global-dropdown-toggle');
|
2017-07-13 10:23:55 -04:00
|
|
|
const findFileURL = document.body.dataset.findFile;
|
2017-03-26 10:24:33 -04:00
|
|
|
|
2017-03-30 13:31:59 -04:00
|
|
|
$('.global-dropdown').on('hide.bs.dropdown', () => {
|
2017-04-06 09:27:48 -04:00
|
|
|
$globalDropdownMenu.removeClass('shortcuts');
|
2017-03-26 10:24:33 -04:00
|
|
|
});
|
|
|
|
|
2017-03-30 13:31:59 -04:00
|
|
|
Mousetrap.bind('n', () => {
|
2017-04-06 09:27:48 -04:00
|
|
|
$globalDropdownMenu.toggleClass('shortcuts');
|
|
|
|
$globalDropdownToggle.trigger('click');
|
|
|
|
|
|
|
|
if (!$globalDropdownMenu.is(':visible')) {
|
|
|
|
$globalDropdownToggle.blur();
|
|
|
|
}
|
2017-03-26 10:24:33 -04:00
|
|
|
});
|
|
|
|
|
2017-03-30 13:31:59 -04:00
|
|
|
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'));
|
2017-03-30 11:14:56 -04:00
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
Mousetrap.bind(['ctrl+shift+p', 'command+shift+p'], this.toggleMarkdownPreview);
|
|
|
|
if (typeof findFileURL !== "undefined" && findFileURL !== null) {
|
|
|
|
Mousetrap.bind('t', function() {
|
2017-01-13 16:54:16 -05:00
|
|
|
return gl.utils.visitUrl(findFileURL);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Shortcuts.prototype.onToggleHelp = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
return Shortcuts.toggleHelp(this.enabledHelp);
|
|
|
|
};
|
|
|
|
|
2017-05-17 07:20:55 -04:00
|
|
|
Shortcuts.prototype.onTogglePerfBar = function(e) {
|
|
|
|
e.preventDefault();
|
2017-06-07 16:21:05 -04:00
|
|
|
const performanceBarCookieName = 'perf_bar_enabled';
|
|
|
|
if (Cookies.get(performanceBarCookieName) === 'true') {
|
|
|
|
Cookies.remove(performanceBarCookieName, { path: '/' });
|
2017-06-07 13:55:07 -04:00
|
|
|
} else {
|
2017-07-07 11:10:33 -04:00
|
|
|
Cookies.set(performanceBarCookieName, 'true', { path: '/' });
|
2017-05-17 07:20:55 -04:00
|
|
|
}
|
2017-06-07 13:55:07 -04:00
|
|
|
gl.utils.refreshCurrentPage();
|
2017-05-17 07:20:55 -04:00
|
|
|
};
|
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
Shortcuts.prototype.toggleMarkdownPreview = function(e) {
|
2017-03-22 23:03:39 -04:00
|
|
|
// Check if short-cut was triggered while in Write Mode
|
2017-04-21 09:19:08 -04:00
|
|
|
const $target = $(e.target);
|
|
|
|
const $form = $target.closest('form');
|
|
|
|
|
|
|
|
if ($target.hasClass('js-note-text')) {
|
|
|
|
$('.js-md-preview-button', $form).focus();
|
2017-03-22 23:03:39 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
return $(document).triggerHandler('markdown-preview:toggle', [e]);
|
|
|
|
};
|
|
|
|
|
|
|
|
Shortcuts.toggleHelp = function(location) {
|
|
|
|
var $modal;
|
|
|
|
$modal = $('#modal-shortcuts');
|
|
|
|
if ($modal.length) {
|
|
|
|
$modal.modal('toggle');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $.ajax({
|
|
|
|
url: gon.shortcuts_path,
|
|
|
|
dataType: 'script',
|
|
|
|
success: function(e) {
|
|
|
|
var i, l, len, results;
|
|
|
|
if (location && location.length > 0) {
|
|
|
|
results = [];
|
2017-01-10 17:35:09 -05:00
|
|
|
for (i = 0, len = location.length; i < len; i += 1) {
|
2016-07-24 16:45:11 -04:00
|
|
|
l = location[i];
|
|
|
|
results.push($(l).show());
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
} else {
|
|
|
|
$('.hidden-shortcut').show();
|
|
|
|
return $('.js-more-help-button').remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Shortcuts.prototype.focusFilter = function(e) {
|
|
|
|
if (this.filterInput == null) {
|
|
|
|
this.filterInput = $('input[type=search]', '.nav-controls');
|
|
|
|
}
|
|
|
|
this.filterInput.focus();
|
|
|
|
return e.preventDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
Shortcuts.focusSearch = function(e) {
|
|
|
|
$('#search').focus();
|
|
|
|
return e.preventDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
return Shortcuts;
|
|
|
|
})();
|
|
|
|
|
|
|
|
$(document).on('click.more_help', '.js-more-help-button', function(e) {
|
|
|
|
$(this).remove();
|
|
|
|
$('.hidden-shortcut').show();
|
|
|
|
return e.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
Mousetrap.stopCallback = (function() {
|
|
|
|
var defaultStopCallback;
|
|
|
|
defaultStopCallback = Mousetrap.stopCallback;
|
|
|
|
return function(e, element, combo) {
|
2016-07-26 23:32:10 -04:00
|
|
|
// allowed shortcuts if textarea, input, contenteditable are focused
|
2016-07-24 16:45:11 -04:00
|
|
|
if (['ctrl+shift+p', 'command+shift+p'].indexOf(combo) !== -1) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return defaultStopCallback.apply(this, arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
2017-02-10 01:50:50 -05:00
|
|
|
}).call(window);
|