2017-01-12 04:27:41 +00: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 */
|
2016-12-14 05:26:26 +00:00
|
|
|
/* global Mousetrap */
|
|
|
|
/* global findFileURL */
|
|
|
|
|
2016-07-24 20:45:11 +00:00
|
|
|
(function() {
|
2017-01-12 04:27:41 +00:00
|
|
|
var bind = function(fn, me) { return function() { return fn.apply(me, arguments); }; };
|
2016-07-24 20:45:11 +00:00
|
|
|
|
|
|
|
this.Shortcuts = (function() {
|
|
|
|
function Shortcuts(skipResetBindings) {
|
|
|
|
this.onToggleHelp = bind(this.onToggleHelp, this);
|
|
|
|
this.enabledHelp = [];
|
|
|
|
if (!skipResetBindings) {
|
|
|
|
Mousetrap.reset();
|
|
|
|
}
|
|
|
|
Mousetrap.bind('?', this.onToggleHelp);
|
|
|
|
Mousetrap.bind('s', Shortcuts.focusSearch);
|
|
|
|
Mousetrap.bind('f', (function(_this) {
|
|
|
|
return function(e) {
|
|
|
|
return _this.focusFilter(e);
|
|
|
|
};
|
|
|
|
})(this));
|
|
|
|
Mousetrap.bind(['ctrl+shift+p', 'command+shift+p'], this.toggleMarkdownPreview);
|
|
|
|
if (typeof findFileURL !== "undefined" && findFileURL !== null) {
|
|
|
|
Mousetrap.bind('t', function() {
|
2017-01-13 21:54:16 +00:00
|
|
|
return gl.utils.visitUrl(findFileURL);
|
2016-07-24 20:45:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Shortcuts.prototype.onToggleHelp = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
return Shortcuts.toggleHelp(this.enabledHelp);
|
|
|
|
};
|
|
|
|
|
|
|
|
Shortcuts.prototype.toggleMarkdownPreview = function(e) {
|
2017-03-23 03:03:39 +00:00
|
|
|
// Check if short-cut was triggered while in Write Mode
|
|
|
|
if ($(e.target).hasClass('js-note-text')) {
|
|
|
|
$('.js-md-preview-button').focus();
|
|
|
|
}
|
2016-07-24 20:45:11 +00: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 22:35:09 +00:00
|
|
|
for (i = 0, len = location.length; i < len; i += 1) {
|
2016-07-24 20:45:11 +00: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-27 03:32:10 +00:00
|
|
|
// allowed shortcuts if textarea, input, contenteditable are focused
|
2016-07-24 20:45:11 +00:00
|
|
|
if (['ctrl+shift+p', 'command+shift+p'].indexOf(combo) !== -1) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return defaultStopCallback.apply(this, arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
2017-02-10 06:50:50 +00:00
|
|
|
}).call(window);
|