/
window.js
510 lines (409 loc) Β· 13.8 KB
/
window.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2011 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
var EventEmitter = require("events").EventEmitter,
assets = kroll.binding("assets"),
vm = require("vm"),
url = require("url"),
Script = kroll.binding('evals').Script;
var TAG = "Window";
exports.bootstrapWindow = function(Titanium) {
// flags to indicate if an activity should be created for the window
var newActivityRequiredKeys = ["fullscreen", "navBarHidden", "modal", "windowSoftInputMode"];
// Backward compatibility for lightweight windows
var UI = Titanium.UI;
var Window = Titanium.TiBaseWindow;
var ActivityWindow = UI.ActivityWindow;
var Proxy = Titanium.Proxy;
var TiWindow = Titanium.TiWindow;
Window.prototype.isActivity = false;
// set constants for representing states for the window
Window.prototype.state = {closed: 0, opening: 1, opened: 2, closing: 3};
// cached orientation modes for window that are not part of the normal properties map for the proxy
Window.prototype.cachedOrientationModes = null;
Window.prototype.cachedActivityProxy = null;
// this is mainly used when we need to perform an operation on an activity and our
// window does not have its own activity. IE: setting orientation on a window opened
// without having any of the newActivityRequiredKeys in it's construction arguments
Window.prototype.getActivityDecorView = function() {
var topActivity = Titanium.App.Android.getTopActivity();
if (topActivity) {
return topActivity.getDecorView();
}
if (kroll.DBG) {
kroll.log(TAG, "unable to find valid activity for decor view");
}
return null;
}
Window.prototype.setCachedProperty = function(name, value) {
if (!(this.propertyCache)) {
this.propertyCache = {};
}
this.propertyCache[name] = value;
}
Window.prototype.getCachedProperty = function(name) {
if (!(this.propertyCache)) {
this.propertyCache = {};
}
return this.propertyCache[name];
}
// set orientation access
var orientationGetter = function() {
if (this.window) {
return this.window.getOrientation();
}
return this.getActivityDecorView().getOrientation();
}
Window.prototype.getOrientation = orientationGetter;
Object.defineProperty(Window.prototype, "orientation", { get: orientationGetter});
// set orientationModes access
var orientationModesGetter = function() {
if (this.window) {
if (this.isActivity) {
return this.window.getOrientationModes();
} else {
return this.getActivityDecorView().getOrientationModes();
}
}
return this.cachedOrientationModes;
}
var orientationModesSetter = function(value) {
if (value != null) {
if (this.window) {
if (this.isActivity) {
this.window.setOrientationModes(value);
} else {
this.getActivityDecorView().setOrientationModes(value);
}
} else {
this.cachedOrientationModes = value;
}
} else if (kroll.DBG) {
kroll.log(TAG, "not allowed to set orientationModes to null");
}
}
Window.prototype.getOrientationModes = orientationModesGetter;
Window.prototype.setOrientationModes = orientationModesSetter;
Object.defineProperty(Window.prototype, "orientationModes", { get: orientationModesGetter, set: orientationModesSetter});
// activity getter (account for scenario when heavy weight window's activity is not created yet)
var activityProxyGetter = function () {
if (this.currentState == this.state.opened) {
return this.window._internalActivity;
}
if (this.cachedActivityProxy == null) {
this.cachedActivityProxy = {};
}
return this.cachedActivityProxy;
}
Window.prototype.getActivity = activityProxyGetter;
Object.defineProperty(Window.prototype, "activity", { get: activityProxyGetter });
// set windowPixelFormat access
var windowPixelFormatGetter = function() {
if (this.isActivity) {
if (this.window) {
return this.window.getWindowPixelFormat();
} else if (this.getCachedProperty("windowPixelFormat")) {
return this.getCachedProperty("windowPixelFormat");
}
return Titanium.UI.Android.PIXEL_FORMAT_UNKNOWN;
} else {
if (this.getCachedProperty("windowPixelFormat")) {
return this.getCachedProperty("windowPixelFormat");
}
return Titanium.UI.Android.PIXEL_FORMAT_UNKNOWN;
}
}
var windowPixelFormatSetter = function(value) {
if (this.isActivity) {
if (this.window) {
TiWindow.prototype.setWindowPixelFormat.call(this, value);
} else {
this.setCachedProperty("windowPixelFormat", value);
}
} else {
this.setCachedProperty("windowPixelFormat", value);
}
}
Window.prototype.getWindowPixelFormat = windowPixelFormatGetter;
Window.prototype.setWindowPixelFormat = windowPixelFormatSetter;
Object.defineProperty(Window.prototype, "windowPixelFormat", { get: windowPixelFormatGetter, set: windowPixelFormatSetter});
Window.prototype.open = function(options) {
// if the window is not closed, do not open
if (this.currentState != this.state.closed) {
if (kroll.DBG) {
kroll.log(TAG, "unable to open, window is not closed");
}
return;
}
this.currentState = this.state.opening;
if (!options) {
options = {};
} else if (!(options instanceof UI.Animation)) {
kroll.extend(this._properties, options);
}
// Determine if we should create a heavy or light weight window.
newActivityRequiredKeys.forEach(function(key) {
if (key in this._properties) {
this.isActivity = true;
}
}, this);
if (!this.isActivity && "tabOpen" in this._properties && options.tabOpen) {
this.isActivity = true;
}
// Set any cached properties on the properties given to the "true" view
if (this.propertyCache) {
kroll.extend(this._properties, this.propertyCache);
}
var needsOpen = false;
if (this.isActivity) {
this.window = new ActivityWindow(this._properties);
this.view = this.window;
needsOpen = true;
} else {
this.window = this.getActivityDecorView();
this.view = new UI.View(this._properties);
this.view.zIndex = Math.MAX_INT - 2;
this.window.add(this.view);
}
// handle orientation - don't put this in post open otherwise the orientation
// will visibly change after the window opens
if (this.cachedOrientationModes) {
this.setOrientationModes(this.cachedOrientationModes);
}
this.setWindowView(this.view);
this.addChildren();
if (needsOpen) {
var self = this;
this.window.on("windowCreated", function () {
self.postOpen();
});
this.window.open(options);
} else {
this.postOpen();
this.fireEvent("open");
this.fireEvent("focus");
}
}
Window.prototype.setWindow = function(existingWindow) {
this.currentState = this.state.opening;
// Set any cached properties on the properties given to the "true" view
if (this.propertyCache) {
kroll.extend(this._properties, this.propertyCache);
}
this.window = existingWindow;
this.view = this.window;
this.setWindowView(this.view);
this.addChildren();
var self = this;
this.window.on("open", function () {
self.postOpen();
});
}
Window.prototype.postOpen = function() {
// Set view and model listener after the window opens
this.setWindowView(this.view);
if ("url" in this._properties) {
this.loadUrl();
}
// Add event listeners and update the source of events after the window opens
for (var event in this._events) {
var listeners = this.listeners(event);
for (var i = 0; i < listeners.length; i++) {
this.addWrappedListener(event, listeners[i]);
}
}
var self = this;
this.addWrappedListener("closeFromActivity", function(e) {
self.window = null;
self.view = null;
self.currentState = self.state.closed;
});
if (this.cachedActivityProxy) {
this.window._internalActivity.extend(this.cachedActivityProxy);
}
this.currentState = this.state.opened;
// If there is any child added when the window state is opening, handle it here.
// For HW window, the correct activity is not available until this point.
// This resolves the issue caused by calling window.open() before calling window.add(view), eg TIMOB-6519.
this.addPostOpenChildren();
}
Window.prototype.runWindowUrl = function(scopeVars) {
var parent = this._module || kroll.Module.main;
var moduleId = this.url;
if (this.url.indexOf(".js") == this.url.length - 3) {
moduleId = this.url.substring(0, this.url.length - 3);
}
parent.require(moduleId, scopeVars, false);
}
Window.prototype.loadUrl = function() {
if (this.url == null) {
return;
}
if (kroll.DBG) {
kroll.log(TAG, "Loading window with URL: " + this.url);
}
// Reset creationUrl of the window
var currentUrl = url.resolve(this._sourceUrl, this.url);
this.window.setCreationUrl(currentUrl.href);
var scopeVars = {
currentWindow: this,
currentActivity: this.window._internalActivity,
currentTab: this.tab,
currentTabGroup: this.tabGroup
};
scopeVars = Titanium.initScopeVars(scopeVars, currentUrl);
this.runWindowUrl(scopeVars);
}
Window.prototype.close = function(options) {
// if the window is not opened, do not close
if (this.currentState != this.state.opened) {
if (kroll.DBG) {
kroll.log(TAG, "unable to close, window is not opened");
}
return;
}
this.currentState = this.state.closing;
if (this.isActivity) {
var self = this;
this.window.close(options);
this.currentState = this.state.closed;
} else {
if (this.view.parent != null) {
// make sure to remove the children otherwise when the window is opened a second time
// the children views wont be added again to the native view
this.removeChildren();
this.window.remove(this.view);
this.window = null;
}
this.currentState = this.state.closed;
this.fireEvent("close");
}
}
Window.prototype.add = function(view) {
if (this.view) {
// If the window is already opened, add the child to this.view directly
// and push the child to the array this._children
if (this.currentState == this.state.opened) {
this.view.add(view);
this._children.push(view);
}
// If the window state is opening, push the child to the array this._postOpenChildren.
// The children in this array will be added to this.view in postOpen().
else if (this.currentState == this.state.opening) {
this._postOpenChildren.push(view);
}
}
// If the window state is not opening or opened, push the child to the array this._children.
// By the time the window opens, the children in this array will be added to this.view.
else {
this._children.push(view);
}
}
Window.prototype.addChildren = function() {
if (this._children) {
var length = this._children.length;
for (var i = 0; i < length; i++) {
this.view.add(this._children[i]);
}
}
// don't delete the children once finished in case the window
// needs to be opened again
}
Window.prototype.addPostOpenChildren = function() {
if (this._postOpenChildren) {
var length = this._postOpenChildren.length;
// Add all the postOpenChildren to this.view and push them to the array this._children.
for (var i = 0; i < length; i++) {
var postOpenChild = this._postOpenChildren[i];
this.view.add(postOpenChild);
this._children.push(postOpenChild);
}
// Clear this._postOpenChildren because all the children in this array are now in this._children.
this._postOpenChildren.length = 0;
}
}
Window.prototype.remove = function(view) {
if (this.view) {
this.view.remove(view);
}
var children = this._children;
if (children) {
var viewIndex = children.indexOf(view);
if (viewIndex != -1) {
children.splice(viewIndex, 1);
}
}
}
Window.prototype.removeChildren = function() {
if (this._children) {
var length = this._children.length;
for (var i = 0; i < length; i++) {
this.view.remove(this._children[i]);
}
}
}
Window.prototype.animate = function(options, callback) {
if (this.view) {
if (callback) {
this.view.animate(options, callback);
} else {
this.view.animate(options);
}
} else if (kroll.DBG) {
kroll.log(TAG, "unable to call animate, view is undefined");
}
}
Window.prototype.addEventListener = function(event, listener) {
if (["open", "close"].indexOf(event) >= 0 || this.view == null) {
EventEmitter.prototype.addEventListener.call(this, event, listener);
} else {
this.addWrappedListener(event, listener);
}
}
// Add event listener to this.window and update the source of event to this.
Window.prototype.addWrappedListener = function(event, listener) {
var self = this;
self.view.addEventListener(event, function(e) {
if (e.source == self.view) {
e.source = self;
}
listener(e);
});
}
Window.prototype.removeEventListener = function(event, listener) {
if (["open", "close"].indexOf(event) >= 0 || this.window == null) {
EventEmitter.prototype.removeEventListener.call(this, event, listener);
} else {
this.view.removeEventListener(event, listener);
}
}
Window.prototype.fireEvent = function(event, data) {
if (["open", "close"].indexOf(event) >= 0 || this.window == null) {
EventEmitter.prototype.fireEvent.call(this, event, data);
} else {
this.view.fireEvent(event, data);
}
}
Window.prototype.setPropertyAndFire = function(property, value) {
if (!this.view) {
Proxy.prototype.setPropertyAndFire.call(this, property, value);
} else {
this.view.setPropertyAndFire(property, value);
}
}
Window.createWindow = function(scopeVars, options) {
var window = new Window(options);
// keeps track of the current window state
window.currentState = window.state.closed;
window._sourceUrl = scopeVars.sourceUrl;
window._currentActivity = scopeVars.currentActivity; // don't think we are using this, remove?
window._module = scopeVars.module;
window._children = [];
window._postOpenChildren = [];
return window;
}
return Window;
};