diff options
Diffstat (limited to 'node_modules/labs/rich-text-editor.reel/shortcut-manager.js')
-rw-r--r-- | node_modules/labs/rich-text-editor.reel/shortcut-manager.js | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/node_modules/labs/rich-text-editor.reel/shortcut-manager.js b/node_modules/labs/rich-text-editor.reel/shortcut-manager.js new file mode 100644 index 00000000..dac1b638 --- /dev/null +++ b/node_modules/labs/rich-text-editor.reel/shortcut-manager.js | |||
@@ -0,0 +1,237 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | /** | ||
7 | @module "montage/ui/rich-text-sanitizer.js" | ||
8 | @requires montage/core/core | ||
9 | */ | ||
10 | var Montage = require("montage").Montage, | ||
11 | Component = require("ui/component").Component | ||
12 | |||
13 | /** | ||
14 | @class module:"montage/ui/shortcut-manager.js".ShortcutManager | ||
15 | */ | ||
16 | exports.ShortcutManager = Montage.create(Montage,/** @lends module:"montage/ui/shortcut-manager.js".ShortcutManager# */ { | ||
17 | |||
18 | /** | ||
19 | Description TODO | ||
20 | @private | ||
21 | */ | ||
22 | _keydownListenerRegistered: { | ||
23 | enumerable: false, | ||
24 | value: false, | ||
25 | distinct: true | ||
26 | }, | ||
27 | |||
28 | /** | ||
29 | Description TODO | ||
30 | @private | ||
31 | */ | ||
32 | _modifiersMap: { | ||
33 | enumerable: false, | ||
34 | value: null | ||
35 | }, | ||
36 | |||
37 | /** | ||
38 | Description TODO | ||
39 | @private | ||
40 | */ | ||
41 | _keyNames: { | ||
42 | enumerable: false, | ||
43 | value: { | ||
44 | "BACKSPACE": 8, | ||
45 | "TAB": 9, | ||
46 | "ENTER": 13, | ||
47 | "ESCAPE": 27, | ||
48 | "PAGEUP": 33, | ||
49 | "PAGEDOWN": 34, | ||
50 | "END": 35, | ||
51 | "HOME": 36, | ||
52 | "LEFT": 37, | ||
53 | "UP": 38, | ||
54 | "RIGHT": 39, | ||
55 | "DOWN": 40, | ||
56 | "INSERT": 45, | ||
57 | "DELETE": 46 | ||
58 | // TODO: Complete list... | ||
59 | } | ||
60 | }, | ||
61 | |||
62 | /** | ||
63 | Description TODO | ||
64 | @private | ||
65 | */ | ||
66 | _shortcutMap: { | ||
67 | enumerable: false, | ||
68 | value: {}, | ||
69 | distinct: true | ||
70 | }, | ||
71 | |||
72 | /** | ||
73 | Description TODO | ||
74 | @private | ||
75 | */ | ||
76 | _target: { | ||
77 | enumerable: false, | ||
78 | value: null | ||
79 | }, | ||
80 | |||
81 | /** | ||
82 | Description TODO | ||
83 | Use the shortcuts array to pre-define shortcuts in template, each shortcut is an object with a keys and action property | ||
84 | @private | ||
85 | */ | ||
86 | target: { | ||
87 | enumerable: true, | ||
88 | set: function(target) { | ||
89 | this._target = target; | ||
90 | } | ||
91 | }, | ||
92 | |||
93 | /** | ||
94 | Description TODO | ||
95 | Use the shortcuts array to pre-define shortcuts in template, each shortcut is an object with a keys and action property | ||
96 | @private | ||
97 | */ | ||
98 | shortcuts: { | ||
99 | enumerable: true, | ||
100 | value: [], | ||
101 | distinct: true | ||
102 | }, | ||
103 | |||
104 | /** | ||
105 | Description TODO | ||
106 | @type {Function} | ||
107 | */ | ||
108 | deserializedFromTemplate: { | ||
109 | enumerable: false, | ||
110 | value : function() { | ||
111 | var shortcuts = this.shortcuts, | ||
112 | nbrShortcuts = shortcuts.length, | ||
113 | shortcut, | ||
114 | i; | ||
115 | |||
116 | for (i = 0; i < nbrShortcuts; i ++) { | ||
117 | shortcut = shortcuts[i]; | ||
118 | this.addShortcut(shortcut.keys, shortcut.action); | ||
119 | } | ||
120 | } | ||
121 | }, | ||
122 | |||
123 | /** | ||
124 | Description TODO | ||
125 | @type {Function} | ||
126 | */ | ||
127 | addShortcut: { | ||
128 | enumerable: true, | ||
129 | value: function(keys, action) { | ||
130 | var target = this._target, | ||
131 | modifiersMap = this._modifiersMap, | ||
132 | shortcutMap = this._shortcutMap, | ||
133 | key, | ||
134 | nbrKeys, | ||
135 | modifiers = 0, | ||
136 | i; | ||
137 | // Make sure we have a valid target | ||
138 | if (!target || (typeof target != "object" || !target.element) && target != "document" && target != document) { | ||
139 | console.log("SHORTCUT MANAGER: You need to set a valid target (must be a component with an element) before you can register shortcut"); | ||
140 | return; | ||
141 | } | ||
142 | |||
143 | // Initialize the modifiers map if needed | ||
144 | if (!modifiersMap) { | ||
145 | modifiersMap = {SHIFT: 1, CTRL: 2, ALT: 4, META: 8}; | ||
146 | modifiersMap.CMD = window.navigator.userAgent.match(/\bmacintosh\b/i) ? modifiersMap.META : modifiersMap.CTRL; | ||
147 | this._modifiersMap = modifiersMap; | ||
148 | } | ||
149 | |||
150 | // Register keydown listener | ||
151 | if (!this._keydownListenerRegistered) { | ||
152 | if (target == "document" || target == document) { | ||
153 | document.addEventListener("keydown", this); | ||
154 | } else { | ||
155 | target.element.addEventListener("keydown", this); | ||
156 | } | ||
157 | this._keydownListenerRegistered = true; | ||
158 | } | ||
159 | |||
160 | // Convert the keys into a modifiers mask | ||
161 | keys = keys.split("+"); | ||
162 | nbrKeys = keys.length; | ||
163 | for (i = 0; i < nbrKeys - 1; i ++) { | ||
164 | modifier = keys[i].toUpperCase(); | ||
165 | if (this._modifiersMap[modifier]) { | ||
166 | modifiers += this._modifiersMap[modifier]; | ||
167 | } | ||
168 | } | ||
169 | |||
170 | // Extra the final key | ||
171 | key = keys[nbrKeys - 1].toUpperCase(); | ||
172 | if (this._keyNames[key] !== undefined) { | ||
173 | key = this._keyNames[key]; | ||
174 | } else { | ||
175 | key = key.charCodeAt(0); | ||
176 | } | ||
177 | |||
178 | // Update the shortcutMap | ||
179 | if (shortcutMap[modifiers] === undefined) { | ||
180 | shortcutMap[modifiers] = {}; | ||
181 | } | ||
182 | if (shortcutMap[modifiers][key] === undefined) { | ||
183 | shortcutMap[modifiers][key] = [action]; | ||
184 | } else { | ||
185 | shortcutMap[modifiers][key].push(action); | ||
186 | } | ||
187 | } | ||
188 | }, | ||
189 | |||
190 | /** | ||
191 | Description TODO | ||
192 | @type {Function} | ||
193 | */ | ||
194 | removeShortcut: { | ||
195 | enumerable: true, | ||
196 | value : function() { | ||
197 | // TODO: Write Me | ||
198 | } | ||
199 | }, | ||
200 | |||
201 | /** | ||
202 | Description TODO | ||
203 | @function | ||
204 | */ | ||
205 | handleKeydown: { | ||
206 | enumerable: false, | ||
207 | value: function(event) { | ||
208 | var keyCode = event.keyCode, | ||
209 | shortcutMap = this._shortcutMap; | ||
210 | modifiers = event.shiftKey + (event.ctrlKey << 1) + (event.altKey << 2) + (event.metaKey << 3), | ||
211 | stopEvent = false; | ||
212 | |||
213 | // Check the shortcut map | ||
214 | if (this._shortcutMap[modifiers] && this._shortcutMap[modifiers][keyCode]) { | ||
215 | var handler = this.handler || this._target, | ||
216 | actions = this._shortcutMap[modifiers][keyCode], | ||
217 | nbrActions = actions.length, | ||
218 | action, | ||
219 | i; | ||