Skip to content

Instantly share code, notes, and snippets.

@iamvery
Last active August 29, 2015 14:06
Show Gist options
  • Save iamvery/5bcaf58fd4ed479adf20 to your computer and use it in GitHub Desktop.
Save iamvery/5bcaf58fd4ed479adf20 to your computer and use it in GitHub Desktop.
# show keyboard shortcuts (app/views/shared/_keyboard_shortcuts_help.slim)
# display help
Shortcutter.Keyboard.bindShortcut '?', 'Show/hide this help dialog', ->
$('#keyboard-shortcuts-help').modal('toggle')
false
# focus on search
Shortcutter.Keyboard.bindShortcut '/', 'Focus on the "Search" form at the top', ->
$('#suggestion-search').focus()
false
# log activity
Shortcutter.Keyboard.bindShortcut 'l a', 'Focus on the "Log Activity" form at the top', ->
$("#activity-dropdown").addClass("open")
$('input#activity_status').focus()
false
# select project to log an activity for
Shortcutter.Keyboard.bindShortcut 'l p', 'Select a project to log an activity for', ->
$("#activity-dropdown").addClass("open")
$('#activity_project_id').trigger("chosen:activate")
false
# go to everyone's activities
Shortcutter.Keyboard.bindShortcut 'g e a', 'Go to everyone\'s activities', ->
window.location.href = $('a[rel=activities]').attr('href')
false
# go to my results
Shortcutter.Keyboard.bindShortcut 'g r', 'Go to my results', ->
window.location.href = $('a[rel=my-results]').attr('href')
false
# go to everyone's results
Shortcutter.Keyboard.bindShortcut 'g e r', 'Go to everyone\'s results', ->
window.location.href = $('a[rel=results]').attr('href')
false
# go home
Shortcutter.Keyboard.bindShortcut 'g h', 'Go to home', ->
window.location.href = $('div.navbar a.brand').attr('href')
false
# unfocus from the form
Shortcutter.Keyboard.bindShortcut 'escape', 'Unfocus from a form element', (e) ->
$('#keyboard-shortcuts-help').modal('hide')
$(e.srcElement).blur()
false
# be helpful
Shortcutter.Keyboard.bindShortcut 'h', 'Be Helpful', ->
clippy.load 'Clippy', (agent)->
agent.show()
false
# Update shortcut table with available shortcuts and descriptions
jQuery ($) ->
Shortcutter.Keyboard.insertShortcutsAsTableRows('table#keyboard_shortcuts tbody')
#= require mousetrap
#= require shortcutter/keyboard
#= require shortcutter/shortcut
# Allow escape to unfocus from a form element
orginalStopCallback = Mousetrap.stopCallback
newStopCallback = (e, element, combo) ->
if combo == 'escape'
return false
else
orginalStopCallback(e, element, combo)
Mousetrap.stopCallback = newStopCallback
@Shortcutter or= {}
class @Shortcutter.Keyboard
@shortcuts: []
@bindShortcut: (keys, description, callback) ->
Mousetrap.bind(keys, callback)
@shortcuts.push(new Shortcutter.Shortcut(keys, description))
@insertShortcutsAsTableRows: (selector) ->
tableBody = $(selector)
for shortcut in @shortcuts
shortcutRow = $(Mustache.to_html($('#keyboard_shortcut_template').html(), shortcut))
tableBody.append(shortcutRow)
@Shortcutter or= {}
class @Shortcutter.Shortcut
constructor: (keys, description) ->
@keys = keys
@description = description
keysInEnglish: ->
@keys.replace(/\s+/g, ' then ') # CoffeeScript thinks "/ /g" is math
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment