BigW Consortium Gitlab

shortcuts.js 3.6 KB
Newer Older
1
import Cookies from 'js-cookie';
winh committed
2
import Mousetrap from 'mousetrap';
3
import { refreshCurrentPage, visitUrl } from './lib/utils/url_utility';
4
import findAndFollowLink from './shortcuts_dashboard_navigation';
5

6 7 8 9 10 11 12 13 14 15
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 {
16
  constructor() {
17 18
    this.onToggleHelp = this.onToggleHelp.bind(this);
    this.enabledHelp = [];
19

20 21 22 23
    Mousetrap.bind('?', this.onToggleHelp);
    Mousetrap.bind('s', Shortcuts.focusSearch);
    Mousetrap.bind('f', this.focusFilter.bind(this));
    Mousetrap.bind('p b', Shortcuts.onTogglePerfBar);
Fatih Acet committed
24

25 26 27 28 29 30 31 32 33 34 35 36
    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);
Fatih Acet committed
37

38 39
    if (typeof findFileURL !== 'undefined' && findFileURL !== null) {
      Mousetrap.bind('t', () => {
40
        visitUrl(findFileURL);
41 42 43 44 45 46
      });
    }

    $(document).on('click.more_help', '.js-more-help-button', function clickMoreHelp(e) {
      $(this).remove();
      $('.hidden-shortcut').show();
47
      e.preventDefault();
48 49 50 51
    });
  }

  onToggleHelp(e) {
52 53 54 55
    if (e.preventDefault) {
      e.preventDefault();
    }

56 57 58 59 60 61 62
    Shortcuts.toggleHelp(this.enabledHelp);
  }

  static onTogglePerfBar(e) {
    e.preventDefault();
    const performanceBarCookieName = 'perf_bar_enabled';
    if (Cookies.get(performanceBarCookieName) === 'true') {
63
      Cookies.set(performanceBarCookieName, 'false', { path: '/' });
64 65 66
    } else {
      Cookies.set(performanceBarCookieName, 'true', { path: '/' });
    }
67
    refreshCurrentPage();
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
  }

  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());
Fatih Acet committed
96
          }
97
          return results;
Fatih Acet committed
98
        }
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

        $('.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();
116 117 118 119

    if (e.preventDefault) {
      e.preventDefault();
    }
120 121
  }
}