forked from ariatemplates/ariatemplates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptLoader.js
110 lines (102 loc) · 4.37 KB
/
ScriptLoader.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
/*
* 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.
*/
/**
* Utility to load external scripts. Please do NOT use it for loading Aria Templates classes (the ones that use
* <code>Aria.classDefinition()</code>) and templates. Use <code>Aria.load()</code> instead, which is capable of
* caching, resolving dependencies and reading multipart files containing templates.
*/
Aria.classDefinition({
$classpath : "aria.utils.ScriptLoader",
$singleton : true,
$dependencies : ['aria.utils.Type', 'aria.core.Browser'],
$constructor : function () {
this._queueIndex = 0;
this._queueCount = {};
this._loadedScripts = [];
},
$destructor : function () {
this._queueCount = null;
this._loadedScripts = null;
},
$prototype : {
/**
* Load the scripts then call a callback function
* @param {Array} scripts - An array of scripts to load
* @param {Function} callback - A function to call once the whole set of scripts are loaded
*/
load : function (scripts, callback) {
var i, ii, url, scriptNode, scriptCount, loadedScripts = this._loadedScripts,
queueIndex = this._queueIndex, document = Aria.$frameworkWindow.document,
head = document.getElementsByTagName('head')[0], that = this,
onReadyStateChangeCallback = function (queueId, scriptNode) {
var key = "" + queueId;
that._queueCount[key]--;
if (that._queueCount[key] === 0) {
delete that._queueCount[key];
that.$callback(callback);
}
};
if (aria.utils.Type.isString(scripts)) {
scripts = [scripts];
}
scriptCount = 0;
for (i = 0, ii = scripts.length; i < ii; i++) {
url = scripts[i];
if (!loadedScripts[url]) {
scriptCount++;
loadedScripts[url] = true;
scriptNode = document.createElement('script');
scriptNode.setAttribute("type", "text/javascript");
if (callback) {
this._addScriptLoadedCallback(scriptNode, onReadyStateChangeCallback, [queueIndex, scriptNode]);
}
scriptNode.src = url;
head.appendChild(scriptNode);
}
}
if (scriptCount === 0) {
this.$callback(callback);
} else {
this._queueCount["" + queueIndex] = scriptCount;
}
this._queueIndex++;
},
/**
* Load the scripts then call a callback function
* @param {Node} scriptNode The script node to manage
* @param {Function} callback The callback to call once the script is loaded
* @param {Array} callbackArgs an array of arguments to be given to the callback
* @private
*/
_addScriptLoadedCallback : function (scriptNode, callback, callbackArgs) {
if (aria.core.Browser.isIE) {
this._addScriptLoadedCallback = function (scriptNode, callback, callbackArgs) {
scriptNode.onreadystatechange = function () {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
callback.apply(null, callbackArgs);
}
};
};
} else {
this._addScriptLoadedCallback = function (scriptNode, callback, callbackArgs) {
scriptNode.onload = function () {
callback.apply(null, callbackArgs);
};
};
}
this._addScriptLoadedCallback.call(this, scriptNode, callback, callbackArgs);
}
}
});