forked from philippbosch/ng-shortcut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-shortcut.coffee
33 lines (26 loc) · 1.28 KB
/
ng-shortcut.coffee
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
"use strict"
angular
.module('ngShortcut', [])
.directive('shortcut', ['$document', ($document) ->
link: (scope, element, attrs, controller) ->
combos = attrs.shortcut.split(',')
for combo in combos
do (combo) ->
parts = combo.split('-')
keycode = parseInt(parts[parts.length-1], 10)
modifiers = parts.slice(0, parts.length-1)
handler = (e) ->
return if e.keyCode != keycode ||
attrs.shortcutActiveOn && e.currentTarget.activeElement.id != attrs.shortcutActiveOn
e.stopImmediatePropagation()
for meta in ['shift', 'ctrl', 'alt', 'meta']
return if !(modifiers.indexOf(meta) == -1) != e["#{meta}Key"]
eventName = attrs.shortcutEvent or 'click'
if 'shortcutTriggerHandler' of attrs
element.triggerHandler(eventName)
else
element.trigger(eventName)
$document.on 'keydown', handler
element.on '$destroy', ->
$document.off('keydown', handler)
])