Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,19 @@ Lastly, even though binding hotkeys in your templates/html tends to be a bad ide
Example of how directive-based hotkeys works:

```html
<modal title="Modal Title" hotkey="{esc: close}">
<modal title="Modal Title" hotkey="{esc: close}" hotkey-description="close modal">
```

another example:

```html
<modal
title="Another Modal Title"
hotkey="{'ctrl+m': handleControlM, enter: handleEnter}"
hotkey-description="{'ctrl+m': 'control-m thing', enter: 'an enter'}"
hotkey-action="{'ctrl+m': 'keypress'}"
hotkey-allow-in="INPUT,TEXTAREA"
>
```

#### Cheatsheet
Expand Down
30 changes: 26 additions & 4 deletions build/hotkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,19 +606,41 @@
restrict: 'A',
link: function (scope, el, attrs) {
var keys = [],
allowIn;
allowIn,
hotkeyDescription,
hotkeyAction;

angular.forEach(scope.$eval(attrs.hotkey), function (func, hotkey) {
// split and trim the hotkeys string into array
allowIn = typeof attrs.hotkeyAllowIn === "string" ? attrs.hotkeyAllowIn.split(/[\s,]+/) : [];

if (typeof attrs.hotkeyDescription === "string") {
try {
hotkeyDescription = scope.$eval(attrs.hotkeyDescription)[hotkey];
} catch(e) {
hotkeyDescription = attrs.hotkeyDescription;
}
} else {
hotkeyDescription = attrs.hotkeyDescription;
}

if (typeof attrs.hotkeyAction === "string") {
try {
hotkeyAction = scope.$eval(attrs.hotkeyAction)[hotkey];
} catch(e) {
hotkeyAction = attrs.hotkeyAction;
}
} else {
hotkeyAction = attrs.hotkeyAction;
}

keys.push(hotkey);

hotkeys.add({
combo: hotkey,
description: attrs.hotkeyDescription,
description: hotkeyDescription,
callback: func,
action: attrs.hotkeyAction,
action: hotkeyAction,
allowIn: allowIn
});
});
Expand Down Expand Up @@ -1050,7 +1072,7 @@
}

function _belongsTo(element, ancestor) {
if (element === document) {
if (element === null || element === document) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion build/hotkeys.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"karma-html2js-preprocessor": "~0.1.0",
"karma-jasmine": "~0.1.3",
"karma-phantomjs-launcher": "^0.2.0",
"karma-script-launcher": "~0.1.0"
"karma-script-launcher": "~0.1.0",
"phantomjs": "1.9"
}
}
28 changes: 25 additions & 3 deletions src/hotkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,19 +600,41 @@
restrict: 'A',
link: function (scope, el, attrs) {
var keys = [],
allowIn;
allowIn,
hotkeyDescription,
hotkeyAction;

angular.forEach(scope.$eval(attrs.hotkey), function (func, hotkey) {
// split and trim the hotkeys string into array
allowIn = typeof attrs.hotkeyAllowIn === "string" ? attrs.hotkeyAllowIn.split(/[\s,]+/) : [];

if (typeof attrs.hotkeyDescription === "string") {
try {
hotkeyDescription = scope.$eval(attrs.hotkeyDescription)[hotkey];
} catch(e) {
hotkeyDescription = attrs.hotkeyDescription;
}
} else {
hotkeyDescription = attrs.hotkeyDescription;
}

if (typeof attrs.hotkeyAction === "string") {
try {
hotkeyAction = scope.$eval(attrs.hotkeyAction)[hotkey];
} catch(e) {
hotkeyAction = attrs.hotkeyAction;
}
} else {
hotkeyAction = attrs.hotkeyAction;
}

keys.push(hotkey);

hotkeys.add({
combo: hotkey,
description: attrs.hotkeyDescription,
description: hotkeyDescription,
callback: func,
action: attrs.hotkeyAction,
action: hotkeyAction,
allowIn: allowIn
});
});
Expand Down
17 changes: 15 additions & 2 deletions test/hotkeys.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ describe 'Angular Hotkeys', ->

describe 'hotkey directive', ->

elSimple = elAllowIn = elMultiple = scope = hotkeys = $compile = $document = executedSimple = executedAllowIn = null
elSimpleAction = elMultipleAction = elSimple = elAllowIn = elMultiple = scope = hotkeys = $compile = $document = executedSimple = executedAllowIn = null

beforeEach ->
module('cfp.hotkeys')
Expand All @@ -463,7 +463,9 @@ describe 'hotkey directive', ->
scope.callmeMultiple = () ->
elSimple = $compile('<div hotkey="{e: callmeSimple}" hotkey-description="testing simple case"></div>')(scope)
elAllowIn = $compile('<div hotkey="{w: callmeAllowIn}" hotkey-description="testing with allowIn" hotkey-allow-in="INPUT, TEXTAREA"></div>')(scope)
elMultiple = $compile('<div hotkey="{a: callmeMultiple, b: callmeMultiple}" hotkey-description="testing with multiple hotkeys"></div>')(scope)
elMultiple = $compile('<div hotkey="{a: callmeMultiple, b: callmeMultiple}" hotkey-description="{a: \'test for a\', b: \'test for b\'}"></div>')(scope)
elSimpleAction = $compile('<div hotkey="{q: callmeSimple}" hotkey-action="keypress"></div>')(scope)
elMultipleAction = $compile('<div hotkey="{u: callmeMultiple, v: callmeMultiple}" hotkey-action="{u: \'keypress\'}"></div>')(scope)
scope.$digest()

it 'should allow hotkey binding via directive', ->
Expand All @@ -489,9 +491,13 @@ describe 'hotkey directive', ->

it 'should unbind the hotkey when the directive is destroyed', ->
expect(hotkeys.get('e').combo).toEqual ['e']
expect(hotkeys.get('e').description).toEqual 'testing simple case'
expect(hotkeys.get('w').combo).toEqual ['w']
expect(hotkeys.get('w').description).toEqual 'testing with allowIn'
expect(hotkeys.get('a').combo).toEqual ['a']
expect(hotkeys.get('a').description).toEqual 'test for a'
expect(hotkeys.get('b').combo).toEqual ['b']
expect(hotkeys.get('b').description).toEqual 'test for b'
elSimple.remove()
elAllowIn.remove()
elMultiple.remove()
Expand All @@ -500,6 +506,13 @@ describe 'hotkey directive', ->
expect(hotkeys.get('a')).toBe no
expect(hotkeys.get('b')).toBe no

it 'should handle actions simply and multiply', ->
expect(hotkeys.get('q').combo).toEqual ['q']
expect(hotkeys.get('q').action).toEqual 'keypress'
expect(hotkeys.get('u').combo).toEqual ['u']
expect(hotkeys.get('u').action).toEqual 'keypress'
expect(hotkeys.get('v').combo).toEqual ['v']
expect(hotkeys.get('v').action).toBe undefined

describe 'Platform specific things', ->
beforeEach ->
Expand Down