forked from mozilla-b2g/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_handlers_manager.js
163 lines (140 loc) · 6.25 KB
/
target_handlers_manager.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
'use strict';
/* global ActiveTargetsManager, KeyEvent,
DefaultTargetHandler, NullTargetHandler, SpaceKeyTargetHandler,
CandidateSelectionTargetHandler, CompositeTargetHandler,
PageSwitchingTargetHandler, CapsLockTargetHandler,
SwitchKeyboardTargetHandler, ToggleCandidatePanelTargetHandler,
DismissSuggestionsTargetHandler, BackspaceTargetHandler */
(function(exports) {
var TargetHandlersManager = function(app) {
this.handlers = undefined;
this.activeTargetsManager = null;
this.app = app;
};
TargetHandlersManager.prototype.start = function() {
this.app.console.log('TargetHandlersManager.start()');
this.handlers = new WeakMap();
var activeTargetsManager = this.activeTargetsManager =
new ActiveTargetsManager(this.app);
// Create partial functions and hook to the callback properties,
// see http://mdn.io/bind#Partial_Functions
activeTargetsManager.ontargetactivated =
this._callTargetAction.bind(this, 'activate', true, false);
activeTargetsManager.ontargetlongpressed =
this._callTargetAction.bind(this, 'longPress', false, false);
activeTargetsManager.ontargetmovedout =
this._callTargetAction.bind(this, 'moveOut', false, true);
activeTargetsManager.ontargetmovedin =
this._callTargetAction.bind(this, 'moveIn', true, false);
activeTargetsManager.ontargetcommitted =
this._callTargetAction.bind(this, 'commit', false, true);
activeTargetsManager.ontargetcancelled =
this._callTargetAction.bind(this, 'cancel', false, true);
activeTargetsManager.ontargetdoubletapped =
this._callTargetAction.bind(this, 'doubleTap', false, true);
activeTargetsManager.onnewtargetwillactivate =
this._callTargetAction.bind(this, 'newTargetActivate', false, false);
activeTargetsManager.start();
};
TargetHandlersManager.prototype.stop = function() {
this.app.console.log('TargetHandlersManager.stop()');
this.handlers = null;
this.activeTargetsManager.stop();
this.activeTargetsManager = null;
};
// This method is the scaffold of our partical functions:
// The first 3 arguments are instructions set with bind() on how the function
// should process the target (put/delete the handler instance in the handers
// map, and call the named action method), and the fourth argument is the actual
// active target to handle.
//
// An active target and it's handler enjoys a life cycle that beginning with
// "activate" or "moveIn", and end with "commit", "cancel", or "moveout".
// "longpress" is noticeably an optional step during the life cycle and does
// not start or end the handler/active target, so it was not mentioned in the
// above list.
// "newTargetActivate" is similar to "longpress", it is an optional step too,
// so it was not mentioned in the above list. When newTargetActivate is called,
// isUpperCase will be set to true, and hold the icon's background.
//
// Please note that since we are using target (an abstract key object associated
// with one DOM element) as the identifier of handlers, we do not assign new
// handler if there are two touches on the same element. Currently that cannot
// happen because of what done in bug 985855, however in the future that will
// change (and these handlers needs to) to adopt bug 985853 (Combo key).
TargetHandlersManager.prototype._callTargetAction = function(action,
setHandler,
deleteHandler,
target) {
this.app.console.log('TargetHandlersManager._callTargetAction()',
action, setHandler, deleteHandler, target);
var handler;
if (this.handlers.has(target)) {
handler = this.handlers.get(target);
if (setHandler) {
console.warn('TargetHandlersManager: ' +
'calling targetHandler.' + action + '() on existing handler.');
}
if (deleteHandler) {
this.handlers.delete(target);
}
} else {
handler = this._createHandlerForTarget(target);
if (!setHandler) {
console.warn('TargetHandlersManager: ' +
'calling targetHandler.' + action + '() on non-existing handler.');
}
if (!deleteHandler) {
this.handlers.set(target, handler);
}
}
handler[action]();
};
// This method decide which of the TargetHandler is the right one to
// handle the active target. It decide the TargetHandler to use and create
// and instance of it, and return the instance.
// |target| is an object, not a DOM element.
TargetHandlersManager.prototype._createHandlerForTarget = function(target) {
this.app.console.log('TargetHandlersManager._createHandlerForTarget()');
var handler;
// This is unfortunately very complex but this is essentially what's already
// specified in keyboard.js.
// We will need to normalize the identifier for each targets in the future.
if ('isDismissSuggestionsButton' in target) {
handler = new DismissSuggestionsTargetHandler(target, this.app);
} else if ('selection' in target) {
handler = new CandidateSelectionTargetHandler(target, this.app);
} else if ('compositeKey' in target) {
handler = new CompositeTargetHandler(target, this.app);
} else if ('keyCode' in target) {
switch (target.keyCode) {
// Delete is a special key, it reacts when pressed not released
case KeyEvent.DOM_VK_BACK_SPACE:
handler = new BackspaceTargetHandler(target, this.app);
break;
case KeyEvent.DOM_VK_SPACE:
handler = new SpaceKeyTargetHandler(target, this.app);
break;
case KeyEvent.DOM_VK_ALT:
handler = new PageSwitchingTargetHandler(target, this.app);
break;
case this.app.layoutManager.KEYCODE_SWITCH_KEYBOARD:
handler = new SwitchKeyboardTargetHandler(target, this.app);
break;
case this.app.layoutManager.KEYCODE_TOGGLE_CANDIDATE_PANEL:
handler = new ToggleCandidatePanelTargetHandler(target, this.app);
break;
case KeyEvent.DOM_VK_CAPS_LOCK:
handler = new CapsLockTargetHandler(target, this.app);
break;
default:
handler = new DefaultTargetHandler(target, this.app);
break;
}
} else {
handler = new NullTargetHandler(target, this.app);
}
return handler;
};
exports.TargetHandlersManager = TargetHandlersManager;
})(window);