This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
target_handlers.js
335 lines (286 loc) · 11.2 KB
/
target_handlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
'use strict';
/* global KeyEvent */
(function(exports) {
var DefaultTargetHandler = function(target, app) {
this.target = target;
this.app = app;
this.ignoreCommitActions = false;
};
DefaultTargetHandler.prototype.activate = function() {
this.app.feedbackManager.triggerFeedback(this.target);
this.app.visualHighlightManager.show(this.target);
};
DefaultTargetHandler.prototype.longPress = function() {
// Does the key have an long press value?
if (!('longPressValue' in this.target.dataset)) {
return;
}
// Ignore any action when commit.
this.ignoreCommitActions = true;
var keyCode = parseInt(this.target.dataset.longPressKeyCode, 10);
this.app.inputMethodManager.currentIMEngine.click(keyCode);
this.app.visualHighlightManager.hide(this.target);
};
DefaultTargetHandler.prototype.moveOut = function() {
this.app.visualHighlightManager.hide(this.target);
};
DefaultTargetHandler.prototype.moveIn = function() {
this.app.visualHighlightManager.show(this.target);
};
DefaultTargetHandler.prototype.commit = function() {
if (this.ignoreCommitActions) {
return;
}
if (this.app.upperCaseStateManager.isUpperCasePressed) {
this.app.upperCaseStateManager.switchUpperCaseState({
isUpperCaseCombo: true
});
}
var keyCode = parseInt(this.target.dataset.keycode, 10);
var upperCaseKeyCode = parseInt(this.target.dataset.keycodeUpper, 10);
var engine = this.app.inputMethodManager.currentIMEngine;
/*
* XXX: A hack to send both keycode and uppercase keycode to latin IME,
* since latin IME would maintain a promise queue for each key, and
* send correct keycode based on the current capitalization state.
* See bug 1013570 and bug 987809 for details.
* This hack should be removed and the state/input queue should be
* maintained out of latin.js.
*/
if (this.app.layoutManager.currentModifiedLayout.imEngine === 'latin') {
engine.click(keyCode, upperCaseKeyCode);
} else {
engine.click(
this.app.upperCaseStateManager.isUpperCase ? upperCaseKeyCode : keyCode);
}
this.app.visualHighlightManager.hide(this.target);
};
DefaultTargetHandler.prototype.cancel = function() {
this.app.visualHighlightManager.hide(this.target);
};
DefaultTargetHandler.prototype.doubleTap = function() {
this.commit();
};
var NullTargetHandler = function(target, app) {
DefaultTargetHandler.apply(this, arguments);
};
NullTargetHandler.prototype = Object.create(DefaultTargetHandler.prototype);
NullTargetHandler.prototype.activate =
NullTargetHandler.prototype.moveIn =
NullTargetHandler.prototype.moveOut =
NullTargetHandler.prototype.commit =
NullTargetHandler.prototype.cancel =
NullTargetHandler.prototype.doubleTap = function() { };
var SpaceKeyTargetHandler = function(target, app) {
DefaultTargetHandler.apply(this, arguments);
};
SpaceKeyTargetHandler.prototype =
Object.create(DefaultTargetHandler.prototype);
SpaceKeyTargetHandler.prototype.longPress = function() {
this.app.targetHandlersManager.activeTargetsManager.clearAllTargets();
navigator.mozInputMethod.mgmt.hide();
this.app.visualHighlightManager.hide(this.target);
};
var CandidateSelectionTargetHandler = function(target, app) {
DefaultTargetHandler.apply(this, arguments);
};
CandidateSelectionTargetHandler.prototype =
Object.create(DefaultTargetHandler.prototype);
CandidateSelectionTargetHandler.prototype.commit = function() {
this.app.candidatePanelManager.hideFullPanel();
// We use dataset.data instead of target.textContent because the
// text actually displayed to the user might have an ellipsis in it
// to make it fit.
var engine = this.app.inputMethodManager.currentIMEngine;
if (typeof engine.select === 'function') {
engine.select(this.target.textContent, this.target.dataset.data);
}
this.app.visualHighlightManager.hide(this.target);
};
var BackspaceTargetHandler = function(target, app) {
DefaultTargetHandler.apply(this, arguments);
this.deleteTimeout = undefined;
this.deleteInterval = undefined;
this.ignoreCommitActions = false;
};
BackspaceTargetHandler.prototype =
Object.create(DefaultTargetHandler.prototype);
// Backspace repeat delay and repeat rate
BackspaceTargetHandler.prototype.REPEAT_RATE = 75;
BackspaceTargetHandler.prototype.REPEAT_TIMEOUT = 700;
// Sends a delete code to remove last character
// The argument specifies whether this is an auto repeat or not.
BackspaceTargetHandler.prototype._sendDelete = function(isRepeat) {
// Pass the isRepeat argument to the input method. It may not want
// to compute suggestions, for example, if this is just one in a series
// of repeating events.
var engine = this.app.inputMethodManager.currentIMEngine;
engine.click(KeyEvent.DOM_VK_BACK_SPACE, null, isRepeat);
};
BackspaceTargetHandler.prototype.activate = function() {
this.app.feedbackManager.triggerFeedback(this.target);
this.app.visualHighlightManager.show(this.target);
// First repetition, after a delay
this.deleteTimeout = setTimeout(function() {
this._sendDelete(true);
// Second, after shorter delay
this.deleteInterval = setInterval(function() {
this._sendDelete(true);
}.bind(this), this.REPEAT_RATE);
}.bind(this), this.REPEAT_TIMEOUT);
};
BackspaceTargetHandler.prototype.moveOut = function() {
this.app.visualHighlightManager.hide(this.target);
clearTimeout(this.deleteTimeout);
clearInterval(this.deleteInterval);
};
BackspaceTargetHandler.prototype.moveIn = function() {
// Do nothing and make sure commit does nothing.
this.ignoreCommitActions = true;
};
BackspaceTargetHandler.prototype.commit = function() {
if (this.ignoreCommitActions) {
return;
}
clearTimeout(this.deleteTimeout);
clearInterval(this.deleteInterval);
this._sendDelete(false);
this.app.visualHighlightManager.hide(this.target);
};
BackspaceTargetHandler.prototype.cancel = function() {
clearTimeout(this.deleteTimeout);
clearInterval(this.deleteInterval);
this.app.visualHighlightManager.hide(this.target);
};
var CompositeTargetHandler = function(target, app) {
DefaultTargetHandler.apply(this, arguments);
};
CompositeTargetHandler.prototype =
Object.create(DefaultTargetHandler.prototype);
CompositeTargetHandler.prototype.commit = function() {
// Keys with this attribute set send more than a single character
// Like ".com" or "2nd" or (in Catalan) "l·l".
var compositeString = this.target.dataset.compositeKey;
var engine = this.app.inputMethodManager.currentIMEngine;
for (var i = 0; i < compositeString.length; i++) {
engine.click(compositeString.charCodeAt(i));
}
this.app.visualHighlightManager.hide(this.target);
};
var PageSwitchingTargetHandler = function(target, app) {
DefaultTargetHandler.apply(this, arguments);
};
PageSwitchingTargetHandler.prototype =
Object.create(DefaultTargetHandler.prototype);
PageSwitchingTargetHandler.prototype.commit = function() {
var keyCode = parseInt(this.target.dataset.keycode, 10);
var page;
switch (keyCode) {
case this.app.layoutManager.KEYCODE_BASIC_LAYOUT:
// Return to default page
page = this.app.layoutManager.LAYOUT_PAGE_DEFAULT;
break;
case this.app.layoutManager.KEYCODE_ALTERNATE_LAYOUT:
// Switch to numbers+symbols page
page = this.app.layoutManager.LAYOUT_PAGE_SYMBOLS_I;
break;
case this.app.layoutManager.KEYCODE_SYMBOL_LAYOUT:
page = this.app.layoutManager.LAYOUT_PAGE_SYMBOLS_II;
break;
case KeyEvent.DOM_VK_ALT:
// alternate between pages 1 and 2 of SYMBOLS
if (this.app.layoutManager.currentLayoutPage ===
this.app.layoutManager.LAYOUT_PAGE_SYMBOLS_I) {
page = this.app.layoutManager.LAYOUT_PAGE_SYMBOLS_II;
} else {
page = this.app.layoutManager.LAYOUT_PAGE_SYMBOLS_I;
}
break;
}
this.app.setLayoutPage(page);
this.app.visualHighlightManager.hide(this.target);
// If needed, empty the candidate panel
var currentIMEngine = this.app.inputMethodManager.currentIMEngine;
if (typeof currentIMEngine.empty === 'function') {
currentIMEngine.empty();
}
};
var CapsLockTargetHandler = function(target, app) {
DefaultTargetHandler.apply(this, arguments);
};
CapsLockTargetHandler.prototype = Object.create(DefaultTargetHandler.prototype);
CapsLockTargetHandler.prototype.activate = function() {
this.app.upperCaseStateManager.switchUpperCaseState({
isUpperCasePressed: true
});
this.app.feedbackManager.triggerFeedback(this.target);
this.app.visualHighlightManager.show(this.target);
};
CapsLockTargetHandler.prototype.commit = function() {
this.app.upperCaseStateManager.switchUpperCaseState({
isUpperCase: !this.app.upperCaseStateManager.isUpperCaseCombo &&
!this.app.upperCaseStateManager.isUpperCase,
isUpperCaseLocked: false,
isUpperCasePressed: false,
isUpperCaseCombo: false
});
this.app.visualHighlightManager.hide(this.target);
};
CapsLockTargetHandler.prototype.doubleTap = function() {
this.app.upperCaseStateManager.switchUpperCaseState({
isUpperCaseLocked: true
});
this.app.visualHighlightManager.hide(this.target);
};
var SwitchKeyboardTargetHandler = function(target, app) {
DefaultTargetHandler.apply(this, arguments);
};
SwitchKeyboardTargetHandler.prototype =
Object.create(DefaultTargetHandler.prototype);
SwitchKeyboardTargetHandler.prototype.longPress = function() {
this.ignoreCommitActions = true;
this.app.targetHandlersManager.activeTargetsManager.clearAllTargets();
navigator.mozInputMethod.mgmt.showAll();
this.app.visualHighlightManager.hide(this.target);
};
SwitchKeyboardTargetHandler.prototype.commit = function() {
if (this.ignoreCommitActions) {
return;
}
this.app.targetHandlersManager.activeTargetsManager.clearAllTargets();
navigator.mozInputMethod.mgmt.next();
this.app.visualHighlightManager.hide(this.target);
};
var ToggleCandidatePanelTargetHandler = function(target, app) {
DefaultTargetHandler.apply(this, arguments);
};
ToggleCandidatePanelTargetHandler.prototype =
Object.create(DefaultTargetHandler.prototype);
ToggleCandidatePanelTargetHandler.prototype.commit = function() {
this.app.candidatePanelManager.toggleFullPanel();
this.app.visualHighlightManager.hide(this.target);
};
var DismissSuggestionsTargetHandler = function(target, app) {
DefaultTargetHandler.apply(this, arguments);
};
DismissSuggestionsTargetHandler.prototype =
Object.create(DefaultTargetHandler.prototype);
DismissSuggestionsTargetHandler.prototype.commit = function() {
var engine = this.app.inputMethodManager.currentIMEngine;
if (typeof engine.dismissSuggestions === 'function') {
engine.dismissSuggestions();
}
this.app.visualHighlightManager.hide(this.target);
};
exports.DefaultTargetHandler = DefaultTargetHandler;
exports.NullTargetHandler = NullTargetHandler;
exports.SpaceKeyTargetHandler = SpaceKeyTargetHandler;
exports.CandidateSelectionTargetHandler = CandidateSelectionTargetHandler;
exports.BackspaceTargetHandler = BackspaceTargetHandler;
exports.CompositeTargetHandler = CompositeTargetHandler;
exports.PageSwitchingTargetHandler = PageSwitchingTargetHandler;
exports.CapsLockTargetHandler = CapsLockTargetHandler;
exports.SwitchKeyboardTargetHandler = SwitchKeyboardTargetHandler;
exports.ToggleCandidatePanelTargetHandler = ToggleCandidatePanelTargetHandler;
exports.DismissSuggestionsTargetHandler = DismissSuggestionsTargetHandler;
})(window);