gitlab-org--gitlab-foss/app/assets/javascripts/shortcuts.js.coffee

53 lines
1.5 KiB
CoffeeScript
Raw Normal View History

2014-08-21 04:14:31 -04:00
class @Shortcuts
2013-06-30 15:10:52 -04:00
constructor: ->
2014-08-21 04:14:31 -04:00
@enabledHelp = []
Mousetrap.reset()
Mousetrap.bind('?', @selectiveHelp)
Mousetrap.bind('s', Shortcuts.focusSearch)
Mousetrap.bind(['ctrl+shift+p', 'command+shift+p'], @toggleMarkdownPreview)
Mousetrap.bind('t', -> Turbolinks.visit(findFileURL)) if findFileURL?
2014-08-21 04:14:31 -04:00
selectiveHelp: (e) =>
Shortcuts.showHelp(e, @enabledHelp)
toggleMarkdownPreview: (e) =>
$(document).triggerHandler('markdown-preview:toggle', [e])
2014-08-21 04:14:31 -04:00
@showHelp: (e, location) ->
2013-06-30 15:10:52 -04:00
if $('#modal-shortcuts').length > 0
$('#modal-shortcuts').modal('show')
else
url = '/help/shortcuts'
url = gon.relative_url_root + url if gon.relative_url_root?
2013-06-30 15:10:52 -04:00
$.ajax(
url: url,
2014-08-21 04:14:31 -04:00
dataType: 'script',
success: (e) ->
if location and location.length > 0
$(l).show() for l in location
2014-08-21 04:14:31 -04:00
else
$('.hidden-shortcut').show()
$('.js-more-help-button').remove()
2013-06-30 15:10:52 -04:00
)
2014-08-21 04:14:31 -04:00
e.preventDefault()
2013-06-30 15:10:52 -04:00
2014-08-21 04:14:31 -04:00
@focusSearch: (e) ->
$('#search').focus()
e.preventDefault()
$(document).on 'click.more_help', '.js-more-help-button', (e) ->
$(@).remove()
$('.hidden-shortcut').show()
e.preventDefault()
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)
)()