UNPKG

3.53 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.9.3
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.addKeymap = function(keymap) {
47 invariant(keymap, 'addKeymap: keymap argument is not defined or falsy.');
48 return this._keymap = keymap;
49 };
50
51 ShortcutManager.prototype.setKeymap = function(keymap) {
52 invariant(keymap, 'setKeymap: keymap argument is not defined or falsy.');
53 this._keymap = keymap;
54 return this.emit(ShortcutManager.CHANGE_EVENT);
55 };
56
57 ShortcutManager.prototype.getAllShortcuts = function() {
58 return this._keymap;
59 };
60
61 ShortcutManager.prototype.getShortcuts = function(componentName) {
62 var _parseShortcutDescriptor, cursor, shortcuts;
63 invariant(componentName, 'getShortcuts: name argument is not defined or falsy.');
64 cursor = this._keymap[componentName];
65 invariant(cursor, "getShortcuts: There are no shortcuts with name " + componentName + ".");
66 _parseShortcutDescriptor = this._parseShortcutDescriptor.bind(this);
67 shortcuts = _(cursor).map(_parseShortcutDescriptor).flatten().value();
68 return shortcuts;
69 };
70
71 ShortcutManager.prototype.findShortcutName = function(keyName, componentName) {
72 var cursor, result;
73 invariant(keyName, 'findShortcutName: keyName argument is not defined or falsy.');
74 invariant(componentName, 'findShortcutName: componentName argument is not defined or falsy.');
75 cursor = this._keymap[componentName];
76 result = _.findKey(cursor, (function(_this) {
77 return function(item) {
78 var index;
79 if (_.isArray(item)) {
80 index = item.indexOf(keyName);
81 if (index >= 0) {
82 return item[index] === keyName;
83 }
84 } else if (_.isPlainObject(item)) {
85 return _.get(item, _this._platformName) === keyName;
86 } else {
87 return item === keyName;
88 }
89 };
90 })(this));
91 return result;
92 };
93
94 return ShortcutManager;
95
96 })(EventEmitter);
97
98 module.exports = ShortcutManager;
99
100}).call(this);