1 |
|
2 | (function() {
|
3 | var EventEmitter, ShortcutManager, _, helpers, invariant,
|
4 | extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
5 | hasProp = {}.hasOwnProperty;
|
6 |
|
7 | _ = require('lodash');
|
8 |
|
9 | invariant = require('invariant');
|
10 |
|
11 | EventEmitter = require('events').EventEmitter;
|
12 |
|
13 | helpers = require('./helpers');
|
14 |
|
15 | ShortcutManager = (function(superClass) {
|
16 | extend(ShortcutManager, superClass);
|
17 |
|
18 | ShortcutManager.CHANGE_EVENT = 'shortcuts:update';
|
19 |
|
20 | function ShortcutManager(keymap) {
|
21 | if (keymap == null) {
|
22 | keymap = {};
|
23 | }
|
24 | this._keymap = keymap;
|
25 | }
|
26 |
|
27 | ShortcutManager.prototype.addUpdateListener = function(callback) {
|
28 | invariant(callback, 'addUpdateListener: callback argument is not defined or falsy');
|
29 | return this.on(ShortcutManager.CHANGE_EVENT, callback);
|
30 | };
|
31 |
|
32 | ShortcutManager.prototype.removeUpdateListener = function(callback) {
|
33 | return this.removeListener(ShortcutManager.CHANGE_EVENT, callback);
|
34 | };
|
35 |
|
36 | ShortcutManager.prototype._platformName = helpers.getPlatformName();
|
37 |
|
38 | ShortcutManager.prototype._parseShortcutDescriptor = function(item) {
|
39 | if (_.isPlainObject(item)) {
|
40 | return _.get(item, this._platformName);
|
41 | } else {
|
42 | return item;
|
43 | }
|
44 | };
|
45 |
|
46 | ShortcutManager.prototype.setKeymap = function(keymap) {
|
47 | invariant(keymap, 'setKeymap: keymap argument is not defined or falsy.');
|
48 | this._keymap = keymap;
|
49 | return this.emit(ShortcutManager.CHANGE_EVENT);
|
50 | };
|
51 |
|
52 | ShortcutManager.prototype.getAllShortcuts = function() {
|
53 | return this._keymap;
|
54 | };
|
55 |
|
56 | ShortcutManager.prototype.getShortcuts = function(componentName) {
|
57 | var _parseShortcutDescriptor, cursor, shortcuts;
|
58 | invariant(componentName, 'getShortcuts: name argument is not defined or falsy.');
|
59 | cursor = this._keymap[componentName];
|
60 | invariant(cursor, "getShortcuts: There are no shortcuts with name " + componentName + ".");
|
61 | _parseShortcutDescriptor = this._parseShortcutDescriptor.bind(this);
|
62 | shortcuts = _(cursor).map(_parseShortcutDescriptor).flatten().value();
|
63 | return shortcuts;
|
64 | };
|
65 |
|
66 | ShortcutManager.prototype._parseShortcutKeyName = function(obj, keyName) {
|
67 | var result;
|
68 | result = _.findKey(obj, (function(_this) {
|
69 | return function(item) {
|
70 | var index;
|
71 | if (_.isPlainObject(item)) {
|
72 | item = _.get(item, _this._platformName);
|
73 | }
|
74 | if (_.isArray(item)) {
|
75 | index = item.indexOf(keyName);
|
76 | if (index >= 0) {
|
77 | item = item[index];
|
78 | }
|
79 | }
|
80 | return item === keyName;
|
81 | };
|
82 | })(this));
|
83 | return result;
|
84 | };
|
85 |
|
86 | ShortcutManager.prototype.findShortcutName = function(keyName, componentName) {
|
87 | var cursor, result;
|
88 | invariant(keyName, 'findShortcutName: keyName argument is not defined or falsy.');
|
89 | invariant(componentName, 'findShortcutName: componentName argument is not defined or falsy.');
|
90 | cursor = this._keymap[componentName];
|
91 | result = this._parseShortcutKeyName(cursor, keyName);
|
92 | return result;
|
93 | };
|
94 |
|
95 | return ShortcutManager;
|
96 |
|
97 | })(EventEmitter);
|
98 |
|
99 | module.exports = ShortcutManager;
|
100 |
|
101 | }).call(this);
|