summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/shortcuts.js
blob: c6d9b007ad1348fe6d0559a188220faf25f4ef90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* 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 */
/* global findFileURL */

(function() {
  var bind = function(fn, me) { return function() { return fn.apply(me, arguments); }; };

  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() {
          return gl.utils.visitUrl(findFileURL);
        });
      }
    }

    Shortcuts.prototype.onToggleHelp = function(e) {
      e.preventDefault();
      return Shortcuts.toggleHelp(this.enabledHelp);
    };

    Shortcuts.prototype.toggleMarkdownPreview = function(e) {
      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();
          }
        }
      });
    };

    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(this);