This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
system_dialog_manager.js
323 lines (293 loc) · 10.1 KB
/
system_dialog_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
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
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* global Service */
'use strict';
(function(exports) {
var DEBUG = false;
/**
* A manager for each SystemDialog `show` and `hide`.
* Show a dialog - the dialog publish `show` event from SystemDialog.
* Hide a dialog - the dialog publish `hide` event from SystemDialog.
* - a new active dialog publish 'system-dialog-show' event,
* then hide the previous active dialog.
* - received 'home', 'holdhome' event from the home button.
* And pass the event type to be the argument
* for options `onHide` attribute.
*
* @class SystemDialogManager
* @requires module:Service
*/
var SystemDialogManager = function SystemDialogManager() {};
SystemDialogManager.prototype = {
/**
* System app is made of a top-level `<div ="screen"></div>` DOM element
* which contain all possible screens displayed by the app.
* Multiple screens can be displayed at a time.
* We store the list of currently
* visible screens into this DOM element class attribute.
*
* @memberof SystemDialogManager
* @prop {DOMElement} windows - the `#windows` element, which is the same
* element that the would AppWindowManager use.
* @prop {DOMElement} screen - the `#screen` element.
*/
elements: {
windows: null,
screen: null,
containerElement: document.getElementById('dialog-overlay')
},
/**
* @memberof SystemDialogManager#
*
*/
states: {
activeDialog: null,
runningDialogs: {}
},
/**
* @memberof SystemDialogManager#
*/
configs: {
listens: ['system-dialog-created',
'simlockcreated',
'actionmenucreated',
'system-dialog-show',
'system-dialog-hide',
'simlockshow',
'actionmenushow',
'simlockhide',
'actionmenuhide',
'system-dialog-requestfocus',
'simlockrequestfocus',
'home',
'holdhome',
'hierarchytopmostwindowchanged']
}
};
SystemDialogManager.prototype.isActive = function() {
return !!this.states.activeDialog;
};
SystemDialogManager.prototype.setHierarchy = function(active) {
if (this.states.activeDialog) {
this.states.activeDialog._setVisibleForScreenReader(active);
}
};
SystemDialogManager.prototype.setFocus = function(active) {
if (!this.states.activeDialog) {
return false;
}
if (active) {
this.states.activeDialog.focus();
}
return true;
};
SystemDialogManager.prototype.name = 'SystemDialogManager';
SystemDialogManager.prototype.EVENT_PREFIX = 'systemdialogmanager';
SystemDialogManager.prototype.publish = function(evtName) {
this.debug('publishing ' + evtName);
window.dispatchEvent(new CustomEvent(this.EVENT_PREFIX + evtName, {
detail: this
}));
};
SystemDialogManager.prototype['_handle_system-resize'] = function() {
if (this.states.activeDialog) {
this.states.activeDialog.resize();
return false;
}
return true;
};
SystemDialogManager.prototype._handle_mozChromeEvent =
function(evt) {
if (!this.states.activeDialog || !evt.detail ||
evt.detail.type !== 'inputmethod-contextchange') {
return true;
}
var typesToHandle = ['select-one', 'select-multiple', 'date', 'time',
'datetime', 'datetime-local', 'blur'];
if (typesToHandle.indexOf(evt.detail.inputType) < 0) {
return true;
}
this.states.activeDialog.broadcast('inputmethod-contextchange',
evt.detail);
return false;
};
SystemDialogManager.prototype._handle_home = function(evt) {
// Automatically hide the dialog on home button press
if (this.states.activeDialog) {
// Deactivate the dialog and pass the event type in the two cases
this.deactivateDialog(this.states.activeDialog, evt.type);
}
return true;
};
SystemDialogManager.prototype._handle_holdhome = function(evt) {
// Automatically hide the dialog on home button press
if (this.states.activeDialog) {
// Deactivate the dialog and pass the event type in the two cases
this.deactivateDialog(this.states.activeDialog, evt.type);
}
return true;
};
SystemDialogManager.prototype.respondToHierarchyEvent = function(evt) {
if (this['_handle_' + evt.type]) {
return this['_handle_' + evt.type](evt);
}
return true;
};
/**
* @listens system-dialog-created - when a system dialog got created,
* it would fire this event.
* @listens system-dialog-show - when a system dialog got show request,
* it would fire this event.
* @listens system-dialog-hide - when a system dialog got hide request,
* it would fire this event.
* @this {SystemDialogManager}
* @memberof SystemDialogManager
*/
SystemDialogManager.prototype.handleEvent = function sdm_handleEvent(evt) {
var dialog = null;
switch (evt.type) {
// We only care about appWindow's fullscreen state because
// we are on top of the appWindow.
case 'hierarchytopmostwindowchanged':
var appWindow = evt.detail.getTopMostWindow();
var isFullScreen = appWindow && appWindow.isFullScreen();
var container = this.elements.containerElement;
container.classList.toggle('fullscreen', isFullScreen);
if (this.states.activeDialog) {
this.states.activeDialog.resize();
}
break;
case 'system-dialog-requestfocus':
case 'simlockrequestfocus':
if (evt.detail !== this.states.activeDialog) {
return;
}
Service.request('focus', this);
break;
case 'simlockcreated':
case 'actionmenucreated':
case 'system-dialog-created':
dialog = evt.detail;
this.registerDialog(dialog);
break;
case 'simlockshow':
case 'system-dialog-show':
case 'actionmenushow':
dialog = evt.detail;
this.activateDialog(dialog);
break;
case 'simlockhide':
case 'actionmenuhide':
case 'system-dialog-hide':
dialog = evt.detail;
this.deactivateDialog(dialog);
break;
}
};
/**
* @private
* @this {SystemDialogManager}
* @memberof SystemDialogManager
*/
SystemDialogManager.prototype.initElements = function sdm_initElements() {
var selectors = { windows: 'windows', screen: 'screen',
containerElement: 'dialog-overlay'};
for (var name in selectors) {
var id = selectors[name];
this.elements[name] = document.getElementById(id);
}
};
/**
* Hook listeners of events this manager interested in.
*
* @private
* @this {SystemDialogManager}
* @memberof SystemDialogManager
*/
SystemDialogManager.prototype.start = function sdm_start() {
this.initElements();
this.configs.listens.forEach((function _initEvent(type) {
self.addEventListener(type, this);
}).bind(this));
Service.request('registerHierarchy', this);
};
/**
* @private
* @this {SystemDialogManager}
* @memberof SystemDialogManager
*/
SystemDialogManager.prototype.registerDialog =
function sdm_registerDialog(dialog) {
this.states.runningDialogs[dialog.instanceID] = dialog;
};
/**
* @private
* @this {SystemDialogManager}
* @memberof SystemDialogManager
*/
SystemDialogManager.prototype.unregisterDialog =
function sdm_unregisterDialog(dialog) {
delete this.states.runningDialogs[dialog.instanceID];
};
/**
* Set an dialog as the active dialog.
*
* @private
* @this {SystemDialogManager}
* @memberof SystemDialogManager
*/
SystemDialogManager.prototype.activateDialog =
function sdm_activateDialog(dialog) {
this.debug('activateDialog: dialog.instanceID = ' + dialog.instanceID);
// Hide the previous active dialog.
if (this.states.activeDialog &&
dialog.instanceID != this.states.activeDialog.instanceID) {
// Means other dialog interrupted.
// Then, have to hide the previous active dialog.
this.states.activeDialog.hide('interrupted', true);
}
// Record new active dialog.
this.states.activeDialog = dialog;
// Activate dialog on screen element.
if (!this.elements.screen.classList.contains('dialog')) {
this.elements.screen.classList.add('dialog');
this.publish('-activated');
}
};
/**
* Deactivate the current active dialog.
*
* @private
* @this {SystemDialogManager}
* @memberof SystemDialogManager
*/
SystemDialogManager.prototype.deactivateDialog =
function sdm_deactivateDialog(dialog, reason) {
this.debug('deactivateDialog: dialog.instanceID = ' + dialog.instanceID);
if (this.states.activeDialog &&
dialog.instanceID == this.states.activeDialog.instanceID) {
// Hide itself
if (reason) { // The request is coming from SystemDialogManager.
this.states.activeDialog.hide(reason, true);
} else { // The request is coming from dialog controller.
// Do nothing since the dialog is hidden already.
}
// Deactivate dialog on screen element
this.elements.screen.classList.remove('dialog');
// Clear activeDialog
this.states.activeDialog = null;
this.publish('-deactivated');
} else { // The dialog is not active.
// Just hide itself, no need to disturb other active dialog.
// Since the dialog is hidden already, do nothing here.
}
};
SystemDialogManager.prototype.debug = function sd_debug() {
if (DEBUG) {
console.log('[SystemDialogManager]' +
'[' + Service.currentTime() + ']' +
'[' + Array.slice(arguments).concat() + ']');
}
};
exports.SystemDialogManager = SystemDialogManager;
}(window));