forked from ariatemplates/ariatemplates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bridge.js
351 lines (306 loc) · 12 KB
/
Bridge.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
/*
* Copyright 2012 Amadeus s.a.s.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var Aria = require("../Aria");
var ariaUtilsAriaWindow = require("./AriaWindow");
var ariaUtilsJson = require("./Json");
var ariaCoreDownloadMgr = require("../core/DownloadMgr");
/**
* Creates a subwindow and load a module inside
* @class aria.utils.Bridge
*/
module.exports = Aria.classDefinition({
$classpath : 'aria.utils.Bridge',
$events : {
"forwardEvent" : {
description : "Wrapper for an event to forward",
properties : {
event : "The forwarded event"
}
}
},
$constructor : function () {
/**
* Child window used to display analysers and controls
* @protected
* @type Window
*/
this._subWindow = null;
/**
* Is bridge open, and subwindow created ?
* @type Boolean
*/
this.isOpen = false;
/**
* Classpath of the module controller to load in subwindow and classpath of root display. Also title
*
* <pre>
* {
* moduleCtrlClasspath : ...
* displayClasspath : ...
* title : ...
* }
* </pre>
*
* @protected
* @type Object
*/
this._config = null;
/**
* Reference to the main module in subwindow
* @protected
* @type aria.templates.ModuleCtrl
*/
this._moduleCtrlRef = null;
/**
* Reference to the root template context in subwindow
* @protected
* @type aria.templates.TemplateCtxt
*/
this._rootTplCtxtRef = null;
/**
* setInterval id for attaching bridge into subwindow
* @type Number
*/
this._bridgeAttachedInterval = false;
},
$destructor : function () {
this.close();
this._subWindow = null;
},
$prototype : {
/**
* Create external display with given module controller classpath
* @param {Object} config moduleControllerClasspath & displayClasspath
* @param {String} options for opening the subwindow. Default is "width=1024, height=800"
*/
open : function (config, options) {
if (this._subWindow) {
// TODO: log error
return;
}
// default options
if (!options) {
options = "width=1024, height=800";
}
// create sub window. Use date to create a new one each time
this._subWindow = Aria.$frameworkWindow.open("", config.title + ("" + (new Date()).getTime()), options);
// The _subWindow can be null if the popup blocker is enabled in the browser
if (this._subWindow == null) {
return;
}
// this is for creating the same window (usefull for debugging)
// this._subWindow = window.open("", config.title, options);
// retrieve current AT version to load the same on the subwindow
var scripts = Aria.$frameworkWindow.document.getElementsByTagName("script"), script, src, urlMatch, atJsName, atSkinName;
for (var i = 0, l = scripts.length; i < l; i++) {
script = scripts[i];
if (script.attributes && script.attributes["src"]) {
src = script.attributes["src"].nodeValue;
urlMatch = /aria\/(aria-?templates-([^\/]+)\.js)/.exec(src);
if (urlMatch && urlMatch.length > 1) {
atJsName = script.src; // retrieves the full url to Aria Templates
atSkinName = ariaCoreDownloadMgr.resolveURL("aria/css/atskin-" + urlMatch[2] + ".js", true);
break;
}
if (/aria\/bootstrap.js/.test(src)) {
// not packaged
atJsName = script.src; // retrieves the full url to Aria Templates
atSkinName = ariaCoreDownloadMgr.resolveURL("aria/css/atskin.js", true);
break;
}
}
}
if (!atJsName) {
// FIXME log Error
return false;
}
// create subwindow content
var pullTimeout = 500; // ms to wait between each check of Aria.loadTemplate
var sourceCode = [
'<!DOCTYPE html>\n',
"<html><head><title>" + config.title + "</title>", // HEAD
"<script type='text/javascript'>Aria = { _xxDebug: true };</script>",
"<script language='JavaScript' src='", // AT script
atJsName, // AT script
"'></script>", // AT script
(aria.widgets && aria.widgets.AriaSkin) ? ["<script type='text/javascript'>",
"Aria['classDefinition']({$classpath : 'aria.widgets.AriaSkin',", "$singleton : true,",
"$prototype : window.aria.utils.Json.copy(",
ariaUtilsJson.convertToJsonString(aria.widgets.AriaSkin.classDefinition.$prototype), ")",
"});</script>"].join("") : ["<script language='JavaScript' src='", // AT Skin script
atSkinName, // AT Skin script
"'></script>" // AT Skin script
].join(""),
"</head>", // END HEAD
"<body onUnload='window.__atBridge&&__atBridge.close()' style='overflow:hidden;'>", // used to
// restore environment
"<div id='main'><h3 id='main_title' style='text-align:center;margin-top:200px;'>Starting.</h3></div>",
"<script type='text/javascript'>var appStart = function () {", // START ToolsModule
"if (window.Aria && window.Aria.loadTemplate && window.__atBridge) { window.__atBridge.moduleStart.call(window.__atBridge);}",
"else { document.getElementById('main_title').innerHTML += '.'; setTimeout(appStart, "
+ pullTimeout + ");}}; appStart();", "</script>", // START ToolsModule
"</body></html>"].join(""); // BODY
this._subWindow.document.write(sourceCode);
this._subWindow.document.close();
// double pulling mandatory for IE
var oSelf = this;
this._bridgeAttachedInterval = setInterval(function () {
// add bridge to subwindow
if (oSelf._subWindow) {
oSelf._subWindow.__atBridge = oSelf;
} else {
clearInterval(oSelf._bridgeAttachedInterval);
}
}, 2000);
this._config = config;
ariaUtilsAriaWindow.attachWindow();
ariaUtilsAriaWindow.$on({
"unloadWindow" : this._onMainWindowUnload,
scope : this
});
},
/**
* Called from the main window when it is being unloaded.
* @param {Object} e event object
*/
_onMainWindowUnload : function (e) {
// automatically close the associated window
this.close();
},
/**
* Function called from the sub window to start the module
*/
moduleStart : function () {
// start working in subwindow
var Aria = this._subWindow.Aria, aria = this._subWindow.aria;
clearInterval(this._bridgeAttachedInterval);
// link the url map and the root map in the sub-window
// to the corresponding maps in the main window:
Aria.rootFolderPath = this.getAria().rootFolderPath;
aria.core.DownloadMgr._urlMap = ariaCoreDownloadMgr._urlMap;
aria.core.DownloadMgr._rootMap = ariaCoreDownloadMgr._rootMap;
Aria.setRootDim({
width : {
min : 16
},
height : {
min : 16
}
});
Aria.load({
classes : ['aria.templates.ModuleCtrlFactory'],
oncomplete : {
fn : this._templatesReady,
scope : this
}
});
},
/**
* Called when templates package is ready. It will create associated module.
* @protected
*/
_templatesReady : function () {
// continue working in subwindow
// var Aria = this._subWindow.Aria;
var aria = this._subWindow.aria;
// creates module instance first to be able to dispose it when window close
aria.templates.ModuleCtrlFactory.createModuleCtrl({
classpath : this._config.moduleCtrlClasspath,
autoDispose : false,
initArgs : {
bridge : this
}
}, {
fn : this._moduleLoaded,
scope : this
}, false);
},
/**
* Callback when module source is loaded
* @param {Object} moduleCtrl and moduleCtrlPrivate
* @protected
*/
_moduleLoaded : function (moduleCtrlObject) {
// finish working in subwindow
var Aria = this._subWindow.Aria; // , aria = this._subWindow.aria;
var moduleCtrl = moduleCtrlObject.moduleCtrlPrivate;
Aria.loadTemplate({
classpath : this._config.displayClasspath,
div : 'main',
moduleCtrl : moduleCtrl,
width : {
min : 16
},
height : {
min : 16
}
}, {
fn : this._displayLoaded,
scope : this
});
this._moduleCtrlRef = moduleCtrl;
this.isOpen = true;
},
/**
* Callback when root display is loaded
* @protected
*/
_displayLoaded : function (status) {
this._rootTplCtxtRef = status.templateCtxt;
},
/**
* Close subwindow and restaure environment
*/
close : function () {
if (this.isOpen) {
if (this._moduleCtrlRef) {
this._moduleCtrlRef.$dispose();
this._moduleCtrlRef = null;
}
if (this._rootTplCtxtRef) {
this._rootTplCtxtRef.$dispose();
this._rootTplCtxtRef = null;
}
this._subWindow.close();
this._subWindow = null;
ariaUtilsAriaWindow.$unregisterListeners(this);
ariaUtilsAriaWindow.detachWindow();
// restaure hijacked function
this.isOpen = false;
}
},
/**
* Returns main window document
* @return {HTMLElement}
*/
getDocument : function () {
return Aria.$window.document;
},
/**
* Returns aria in the main window
* @return {Object}
*/
getAriaPackage : function () {
return aria;
},
/**
* Returns the Aria object in the main window
* @return {Object}
*/
getAria : function () {
return Aria;
}
}
});