1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00
Ghost-Admin/app/mixins/shortcuts-route.js
Kevin Ansfield 17b3ced8c9 Always call _super when using Ember hooks
no issue
- review use of Ember core hooks and add a call to `this._super` if missing
- fix a few occurrences of using the wrong component lifecycle hooks that could result in multiple/duplicate event handlers being attached

`_super` should always be called when overriding Ember's base hooks so that core functionality or app functionality added through extensions, mixins or addons is not lost. This is important as it guards against issues arising from later refactorings or core changes.

As example of lost functionality, there were a number of routes that extended from `AuthenticatedRoute` but then overrode the `beforeModel` hook without calling `_super` which meant that the route was no longer treated as authenticated.
2015-11-30 12:45:37 +00:00

90 lines
2.5 KiB
JavaScript

/* global key */
import Ember from 'ember';
const {Mixin, run, typeOf} = Ember;
// Configure KeyMaster to respond to all shortcuts,
// even inside of
// input, textarea, and select.
key.filter = function () {
return true;
};
key.setScope('default');
/**
* 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.
* 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)),
*
* ```javascript
* shortcuts: {
* 'ctrl+s, command+s': 'save',
* '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'}
* }
* ```
* 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
*/
export default Mixin.create({
registerShortcuts() {
let shortcuts = this.get('shortcuts');
Object.keys(shortcuts).forEach((shortcut) => {
let scope = shortcuts[shortcut].scope || 'default';
let action = shortcuts[shortcut];
let options;
if (typeOf(action) !== 'string') {
options = action.options;
action = action.action;
}
key(shortcut, scope, (event) => {
// stop things like ctrl+s from actually opening a save dialogue
event.preventDefault();
run(this, function () {
this.send(action, options);
});
});
});
},
removeShortcuts() {
let shortcuts = this.get('shortcuts');
Object.keys(shortcuts).forEach((shortcut) => {
let scope = shortcuts[shortcut].scope || 'default';
key.unbind(shortcut, scope);
});
},
activate() {
this._super(...arguments);
this.registerShortcuts();
},
deactivate() {
this._super(...arguments);
this.removeShortcuts();
}
});