diff options
Diffstat (limited to 'js/mediators/keyboard-mediator.js')
-rw-r--r-- | js/mediators/keyboard-mediator.js | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/js/mediators/keyboard-mediator.js b/js/mediators/keyboard-mediator.js new file mode 100644 index 00000000..144932c0 --- /dev/null +++ b/js/mediators/keyboard-mediator.js | |||
@@ -0,0 +1,240 @@ | |||
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 | // The following class is responsible for listening for keydown events. | ||
8 | |||
9 | var Montage = require("montage/core/core").Montage; | ||
10 | var Component = require("montage/ui/component").Component; | ||
11 | |||
12 | // Put the keyboard constants in the global scope. | ||
13 | var Keyboard = exports.Keyboard = { | ||
14 | BACKSPACE:8, | ||
15 | TAB:9, | ||
16 | ENTER:13, | ||
17 | SHIFT:16, | ||
18 | ALT:18, | ||
19 | DELETE:46, | ||
20 | LEFT:37, | ||
21 | UP:38, | ||
22 | RIGHT:39, | ||
23 | DOWN:40, | ||
24 | ESCAPE: 27, | ||
25 | SPACE: 32, | ||
26 | |||
27 | A:65, | ||
28 | B:66, | ||
29 | C:67, | ||
30 | D:68, | ||
31 | E:69, | ||
32 | F:70, | ||
33 | G:71, | ||
34 | H:72, | ||
35 | I:73, | ||
36 | J:74, | ||
37 | K:75, | ||
38 | L:76, | ||
39 | M:77, | ||
40 | N:78, | ||
41 | O:79, | ||
42 | P:80, | ||
43 | Q:81, | ||
44 | R:82, | ||
45 | S:83, | ||
46 | T:84, | ||
47 | U:85, | ||
48 | V:86, | ||
49 | W:87, | ||
50 | X:88, | ||
51 | Y:89, | ||
52 | Z:90, | ||
53 | PLUS:187, | ||
54 | MINUS:189 | ||
55 | }; | ||
56 | |||
57 | exports.KeyboardMediator = Montage.create(Component, { | ||
58 | |||
59 | deserializedFromTemplate: { | ||
60 | value: function() { | ||
61 | this.eventManager.addEventListener("appLoaded", this, false); | ||
62 | } | ||
63 | }, | ||
64 | |||
65 | handleAppLoaded: { | ||
66 | value: function() { | ||
67 | document.addEventListener("keydown", this, false); | ||
68 | document.addEventListener("keyup", this, false); | ||
69 | |||
70 | this.addEventListener("change@appModel.livePreview", this.handleLivePreview, false); | ||
71 | } | ||
72 | }, | ||
73 | |||
74 | handleLivePreview: { | ||
75 | value: function() { | ||
76 | if(this.appModel.livePreview) { | ||
77 | document.removeEventListener("keydown", this, false); | ||
78 | document.removeEventListener("keyup", this, false); | ||
79 | } else { | ||
80 | document.addEventListener("keydown", this, false); | ||
81 | document.addEventListener("keyup", this, false); | ||
82 | } | ||
83 | } | ||
84 | }, | ||
85 | |||
86 | handleKeydown: { | ||
87 | value: function(evt) { | ||
88 | if(document.activeElement.nodeName !== "BODY") { | ||
89 | // Don't do anything if an input or other control is focused | ||
90 | return; | ||
91 | } | ||
92 | |||
93 | // Disable defaults for the Arrow Keys | ||
94 | if((evt.keyCode == Keyboard.LEFT) || (evt.keyCode == Keyboard.RIGHT) || (evt.keyCode == Keyboard.UP) || (evt.keyCode == Keyboard.DOWN)) { | ||
95 | evt.preventDefault(); | ||
96 | } | ||
97 | |||
98 | // Check DELETE OR BACKSPACE | ||
99 | if((evt.keyCode == Keyboard.BACKSPACE) || (evt.keyCode == Keyboard.DELETE)) { | ||
100 | evt.stopImmediatePropagation(); | ||
101 | evt.preventDefault(); | ||
102 | NJevent("deleting"); | ||
103 | return; | ||
104 | } | ||
105 | |||
106 | // Check if cmd+z/ctrl+z for Undo (Windows/Mac) | ||
107 | if ((evt.keyCode == Keyboard.Z) && (evt.ctrlKey || evt.metaKey) && !evt.shiftKey) { | ||
108 | NJevent("executeUndo"); | ||
109 | //menuViewManagerModule.MenuViewManager.closeMenu("mainMenuBar"); | ||
110 | return; | ||
111 | } | ||
112 | |||
113 | // Check if cmd+shift+z for Redo (Mac) | ||
114 | if ((evt.keyCode == Keyboard.Z) && evt.metaKey && evt.shiftKey) { | ||
115 | NJevent("executeRedo"); | ||
116 | //menuViewManagerModule.MenuViewManager.closeMenu("mainMenuBar"); | ||
117 | return; | ||
118 | } | ||
119 | |||
120 | // Check if ctrl+y for Redo (Windows) | ||
121 | if ((evt.keyCode == Keyboard.Y) && evt.ctrlKey) { | ||
122 | NJevent("executeRedo"); | ||
123 | //menuViewManagerModule.MenuViewManager.closeMenu("mainMenuBar"); | ||
124 | return; | ||
125 | } | ||
126 | |||
127 | // Shortcut for Selection Tool is V | ||
128 | if(evt.keyCode === Keyboard.V) { | ||
129 | evt.preventDefault(); | ||
130 | this.application.ninja.handleSelectTool({"detail": this.application.ninja.toolsData.defaultToolsData[0]}); | ||
131 | return; | ||
132 | } | ||
133 | |||
134 | // Shortcut for Tag Tool is D | ||
135 | if(evt.keyCode === Keyboard.D){ | ||
136 | evt.preventDefault(); | ||
137 | this.application.ninja.handleSelectTool({"detail": this.application.ninja.toolsData.defaultToolsData[4]}); | ||
138 | return; | ||
139 | } | ||
140 | |||
141 | // Shortcut for Rotate Tool is W | ||
142 | if(evt.keyCode === Keyboard.W){ | ||
143 | evt.preventDefault(); | ||
144 | this.application.ninja.handleSelectTool({"detail": this.application.ninja.toolsData.defaultToolsData[2]}); | ||
145 | return; | ||
146 | } | ||
147 | |||
148 | // Shortcut for Translate Tool is G | ||
149 | if(evt.keyCode === Keyboard.G){ | ||
150 | evt.preventDefault(); | ||
151 | this.application.ninja.handleSelectTool({"detail": this.application.ninja.toolsData.defaultToolsData[3]}); | ||
152 | return; | ||
153 | } | ||
154 | |||
155 | // Shortcut for Rectangle Tool is R | ||
156 | // unless the user is pressing the command key. | ||
157 | // If the user is pressing the command key, they want to refresh the browser. | ||
158 | if((evt.keyCode === Keyboard.R) && !evt.metaKey) { | ||
159 | evt.preventDefault(); | ||
160 | this.application.ninja.handleSelectTool({"detail": this.application.ninja.toolsData.defaultToolsData[7]}); | ||
161 | this.application.ninja.handleSelectSubTool({"detail": this.application.ninja.toolsData.defaultToolsData[7].subtools[1]}); | ||
162 | return; | ||
163 | } | ||
164 | |||
165 | // Shortcut for Oval Tool is O | ||
166 | if(evt.keyCode === Keyboard.O) { | ||
167 | evt.preventDefault(); | ||
168 | this.application.ninja.handleSelectTool({"detail": this.application.ninja.toolsData.defaultToolsData[7]}); | ||
169 | this.application.ninja.handleSelectSubTool({"detail": this.application.ninja.toolsData.defaultToolsData[7].subtools[0]}); | ||
170 | return; | ||
171 | } | ||
172 | |||
173 | // Shortcut for Line Tool is L | ||
174 | if(evt.keyCode === Keyboard.L ) { | ||
175 | evt.preventDefault(); | ||
176 | this.application.ninja.handleSelectTool({"detail": this.application.ninja.toolsData.defaultToolsData[7]}); | ||
177 | this.application.ninja.handleSelectSubTool({"detail": this.application.ninja.toolsData.defaultToolsData[7].subtools[2]}); | ||
178 | return; | ||
179 | } | ||
180 | |||
181 | if(evt.keyCode === Keyboard.Z ) { | ||
182 | evt.preventDefault(); | ||
183 | this.application.ninja.handleSelectTool({"detail": this.application.ninja.toolsData.defaultToolsData[16]}); | ||
184 | return; | ||
185 | } | ||
186 | |||
187 | // Check if cmd+a/ctrl+a for Select All | ||
188 | if((evt.keyCode == Keyboard.A) && (evt.ctrlKey || evt.metaKey)) { | ||
189 | NJevent("selectAll"); | ||
190 | return; | ||
191 | } | ||
192 | |||
193 | if(evt.keyCode === Keyboard.ESCAPE){//ESC key | ||
194 | //console.log("ESC key pressed"); | ||
195 | if(this.application.ninja.toolsData) this.application.ninja.toolsData.selectedToolInstance.HandleEscape(evt); | ||
196 | //menuViewManagerModule.MenuViewManager.closeMenu("mainMenuBar"); | ||
197 | } | ||
198 | |||
199 | if(this.application.ninja.toolsData) this.application.ninja.toolsData.selectedToolInstance.HandleKeyPress(evt); | ||
200 | |||
201 | } | ||
202 | }, | ||
203 | |||
204 | handleKeyup: { | ||
205 | value: function(evt) { | ||
206 | if(document.activeElement.nodeName !== "BODY") { | ||
207 | // Don't do anything if an input or other control is focused | ||
208 | return; | ||
209 | } | ||
210 | |||
211 | if(this.application.ninja.toolsData) this.application.ninja.toolsData.selectedToolInstance.HandleKeyUp(evt); | ||
212 | } | ||
213 | }, | ||
214 | |||
215 | _handleKeydown: { | ||
216 | value: function(evt) { | ||
217 | |||
218 | // Check if cmd-shift-+/ctrl-shift-+ for toggling snapping | ||
219 | if(evt.shiftKey && (evt.ctrlKey || evt.metaKey) && (evt.keyCode === 187)) | ||
220 | { | ||
221 | MainMenuModule.MenuActionManager.toggleSnapping("snap", !DocumentManagerModule.DocumentManager.activeDocument.snapping); | ||
222 | evt.preventDefault(); | ||
223 | return; | ||
224 | } | ||
225 | |||
226 | if(evt.keyCode === Keyboard.PLUS && (evt.metaKey||evt.ctrlKey)) { | ||
227 | evt.preventDefault(); | ||
228 | this._toolsList.action("zoomIn", evt); | ||
229 | return; | ||
230 | } | ||
231 | |||
232 | if(evt.keyCode === Keyboard.MINUS && (evt.metaKey || evt.ctrlKey)) { | ||
233 | evt.preventDefault(); | ||
234 | this._toolsList.action("zoomOut", evt); | ||
235 | return; | ||
236 | } | ||
237 | |||