diff options
author | Phil Hughes <me@iamphill.com> | 2017-10-10 12:10:11 +0300 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2017-10-13 13:38:09 +0300 |
commit | 299da4ca075160ea5778b10f2a93b87f65517a76 (patch) | |
tree | 18f2de9e0647f422480e2b31ce55b25bbbb314c2 /app/assets/javascripts/shortcuts.js | |
parent | 5843a43c16e007193f5e26522d1e7368a0bdb2d7 (diff) |
Remove shortcut JS classes from global namespace
Diffstat (limited to 'app/assets/javascripts/shortcuts.js')
-rw-r--r-- | app/assets/javascripts/shortcuts.js | 224 |
1 files changed, 106 insertions, 118 deletions
diff --git a/app/assets/javascripts/shortcuts.js b/app/assets/javascripts/shortcuts.js index f63b99ba1c5..ebe7a99ffae 100644 --- a/app/assets/javascripts/shortcuts.js +++ b/app/assets/javascripts/shortcuts.js @@ -1,128 +1,116 @@ -/* 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 */ -/* global Mousetrap */ import Cookies from 'js-cookie'; import Mousetrap from 'mousetrap'; - import findAndFollowLink from './shortcuts_dashboard_navigation'; -(function() { - this.Shortcuts = (function() { - function Shortcuts(skipResetBindings) { - this.onToggleHelp = this.onToggleHelp.bind(this); - this.enabledHelp = []; - if (!skipResetBindings) { - Mousetrap.reset(); - } - Mousetrap.bind('?', this.onToggleHelp); - Mousetrap.bind('s', Shortcuts.focusSearch); - Mousetrap.bind('f', (e => this.focusFilter(e))); - Mousetrap.bind('p b', this.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'], this.toggleMarkdownPreview); - if (typeof findFileURL !== "undefined" && findFileURL !== null) { - Mousetrap.bind('t', function() { - return gl.utils.visitUrl(findFileURL); - }); - } +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(skipResetBindings) { + this.onToggleHelp = this.onToggleHelp.bind(this); + this.enabledHelp = []; + if (!skipResetBindings) { + Mousetrap.reset(); } + Mousetrap.bind('?', this.onToggleHelp); + Mousetrap.bind('s', Shortcuts.focusSearch); + Mousetrap.bind('f', this.focusFilter.bind(this)); + Mousetrap.bind('p b', Shortcuts.onTogglePerfBar); - Shortcuts.prototype.onToggleHelp = function(e) { - e.preventDefault(); - return Shortcuts.toggleHelp(this.enabledHelp); - }; + 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); - Shortcuts.prototype.onTogglePerfBar = function(e) { + if (typeof findFileURL !== 'undefined' && findFileURL !== null) { + Mousetrap.bind('t', () => { + gl.utils.visitUrl(findFileURL); + }); + } + + $(document).on('click.more_help', '.js-more-help-button', function clickMoreHelp(e) { + $(this).remove(); + $('.hidden-shortcut').show(); e.preventDefault(); - const performanceBarCookieName = 'perf_bar_enabled'; - if (Cookies.get(performanceBarCookieName) === 'true') { - Cookies.remove(performanceBarCookieName, { path: '/' }); - } else { - Cookies.set(performanceBarCookieName, 'true', { path: '/' }); - } - gl.utils.refreshCurrentPage(); - }; - - Shortcuts.prototype.toggleMarkdownPreview = function(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(); - } - 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 = []; - for (i = 0, len = location.length; i < len; i += 1) { - l = location[i]; - results.push($(l).show()); - } - return results; - } else { - $('.hidden-shortcut').show(); - return $('.js-more-help-button').remove(); + }); + } + + onToggleHelp(e) { + e.preventDefault(); + Shortcuts.toggleHelp(this.enabledHelp); + } + + static onTogglePerfBar(e) { + e.preventDefault(); + const performanceBarCookieName = 'perf_bar_enabled'; + if (Cookies.get(performanceBarCookieName) === 'true') { + Cookies.remove(performanceBarCookieName, { path: '/' }); + } else { + Cookies.set(performanceBarCookieName, 'true', { path: '/' }); + } + gl.utils.refreshCurrentPage(); + } + + 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'); + } + + $.ajax({ + url: gon.shortcuts_path, + dataType: 'script', + success() { + if (location && location.length > 0) { + const results = []; + for (let i = 0, len = location.length; i < len; i += 1) { + results.push($(location[i]).show()); } + return results; } - }); - }; - - 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) { - // allowed shortcuts if textarea, input, contenteditable are focused - if (['ctrl+shift+p', 'command+shift+p'].indexOf(combo) !== -1) { - return false; - } else { - return defaultStopCallback.apply(this, arguments); - } - }; - })(); -}).call(window); + + $('.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(); + e.preventDefault(); + } +} |