2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2014-10-17 16:13:47 +04:00
|
|
|
/* global key */
|
2014-06-19 23:44:44 +04:00
|
|
|
|
2014-10-25 01:09:50 +04:00
|
|
|
// Configure KeyMaster to respond to all shortcuts,
|
|
|
|
// even inside of
|
|
|
|
// input, textarea, and select.
|
2014-06-19 23:44:44 +04:00
|
|
|
key.filter = function () {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2014-10-10 13:54:02 +04:00
|
|
|
key.setScope('default');
|
2014-06-19 23:44:44 +04:00
|
|
|
/**
|
|
|
|
* Only routes can implement shortcuts.
|
|
|
|
* If you need to trigger actions on the controller,
|
|
|
|
* simply call them with `this.get('controller').send('action')`.
|
|
|
|
*
|
|
|
|
* To implement shortcuts, add this mixin to your `extend()`,
|
|
|
|
* and implement a `shortcuts` hash.
|
2014-06-21 22:58:06 +04:00
|
|
|
* In this hash, keys are shortcut combinations and values are route action names.
|
|
|
|
* (see [keymaster docs](https://github.com/madrobby/keymaster/blob/master/README.markdown)),
|
2014-10-17 16:13:47 +04:00
|
|
|
*
|
2014-06-19 23:44:44 +04:00
|
|
|
* ```javascript
|
|
|
|
* shortcuts: {
|
|
|
|
* 'ctrl+s, command+s': 'save',
|
2014-06-21 22:58:06 +04:00
|
|
|
* 'ctrl+alt+z': 'toggleZenMode'
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
* For more complex actions, shortcuts can instead have their value
|
|
|
|
* be an object like {action, options}
|
|
|
|
* ```javascript
|
|
|
|
* shortcuts: {
|
|
|
|
* 'ctrl+k': {action: 'markdownShortcut', options: 'createLink'}
|
2014-06-19 23:44:44 +04:00
|
|
|
* }
|
|
|
|
* ```
|
2014-10-10 13:54:02 +04:00
|
|
|
* You can set the scope of your shortcut by passing a scope property.
|
|
|
|
* ```javascript
|
|
|
|
* shortcuts : {
|
|
|
|
* 'enter': {action : 'confirmModal', scope: 'modal'}
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
* If you don't specify a scope, we use a default scope called "default".
|
|
|
|
* To have all your shortcut work in all scopes, give it the scope "all".
|
|
|
|
* Find out more at the keymaster docs
|
2014-06-19 23:44:44 +04:00
|
|
|
*/
|
2015-08-19 14:55:40 +03:00
|
|
|
export default Ember.Mixin.create({
|
2014-06-19 23:44:44 +04:00
|
|
|
registerShortcuts: function () {
|
|
|
|
var self = this,
|
|
|
|
shortcuts = this.get('shortcuts');
|
|
|
|
|
2015-09-03 14:06:50 +03:00
|
|
|
Object.keys(shortcuts).forEach(function (shortcut) {
|
2014-10-10 13:54:02 +04:00
|
|
|
var scope = shortcuts[shortcut].scope || 'default',
|
|
|
|
action = shortcuts[shortcut],
|
|
|
|
options;
|
|
|
|
|
|
|
|
if (Ember.typeOf(action) !== 'string') {
|
|
|
|
options = action.options;
|
|
|
|
action = action.action;
|
|
|
|
}
|
|
|
|
|
|
|
|
key(shortcut, scope, function (event) {
|
2014-10-25 01:09:50 +04:00
|
|
|
// stop things like ctrl+s from actually opening a save dialogue
|
2014-06-19 23:44:44 +04:00
|
|
|
event.preventDefault();
|
2014-06-21 22:58:06 +04:00
|
|
|
self.send(action, options);
|
2014-06-19 23:44:44 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-06-19 23:44:44 +04:00
|
|
|
removeShortcuts: function () {
|
|
|
|
var shortcuts = this.get('shortcuts');
|
|
|
|
|
2015-09-03 14:06:50 +03:00
|
|
|
Object.keys(shortcuts).forEach(function (shortcut) {
|
2015-09-11 11:54:55 +03:00
|
|
|
var scope = shortcuts[shortcut].scope || 'default';
|
|
|
|
key.unbind(shortcut, scope);
|
2014-06-19 23:44:44 +04:00
|
|
|
});
|
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-06-19 23:44:44 +04:00
|
|
|
activate: function () {
|
|
|
|
this._super();
|
|
|
|
this.registerShortcuts();
|
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-06-19 23:44:44 +04:00
|
|
|
deactivate: function () {
|
|
|
|
this._super();
|
|
|
|
this.removeShortcuts();
|
|
|
|
}
|
|
|
|
});
|