BigW Consortium Gitlab

shortcuts.js.coffee 1.51 KB
Newer Older
1
class @Shortcuts
2
  constructor: ->
3 4 5 6
    @enabledHelp = []
    Mousetrap.reset()
    Mousetrap.bind('?', @selectiveHelp)
    Mousetrap.bind('s', Shortcuts.focusSearch)
7
    Mousetrap.bind(['ctrl+shift+p', 'command+shift+p'], @toggleMarkdownPreview)
8
    Mousetrap.bind('t', -> Turbolinks.visit(findFileURL)) if findFileURL?
9 10 11

  selectiveHelp: (e) =>
    Shortcuts.showHelp(e, @enabledHelp)
12

13 14 15
  toggleMarkdownPreview: (e) =>
    $(document).triggerHandler('markdown-preview:toggle', [e])

16
  @showHelp: (e, location) ->
17 18 19
    if $('#modal-shortcuts').length > 0
      $('#modal-shortcuts').modal('show')
    else
20 21
      url = '/help/shortcuts'
      url = gon.relative_url_root + url if gon.relative_url_root?
22
      $.ajax(
23
        url: url,
24 25 26
        dataType: 'script',
        success: (e) ->
          if location and location.length > 0
27
            $(l).show() for l in location
28 29 30
          else
            $('.hidden-shortcut').show()
            $('.js-more-help-button').remove()
31
      )
32
      e.preventDefault()
33

34 35 36
  @focusSearch: (e) ->
    $('#search').focus()
    e.preventDefault()
37 38 39 40 41

$(document).on 'click.more_help', '.js-more-help-button', (e) ->
  $(@).remove()
  $('.hidden-shortcut').show()
  e.preventDefault()
42 43 44 45 46 47 48 49 50 51 52

Mousetrap.stopCallback = (->
  defaultStopCallback = Mousetrap.stopCallback

  return (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(@, arguments)
)()