ares-ide
Version:
A browser-based code editor and UI designer for Enyo 2 projects
94 lines (91 loc) • 2.16 kB
JavaScript
//
// implement loader 'machine':
// the machine implements the interface required by 'loader'
//
// create analyzer namespace if it doesn't already exist
if (window.analyzer === undefined) {
window.analyzer = {};
}
analyzer.runtimeMachine = {
_head: function(inType, inAttrs, inText) {
this._inflight = true;
var elt = document.createElement(inType);
for (var n in inAttrs) {
elt.setAttribute(n, inAttrs[n]);
}
if (inText) {
elt.innerText = inText;
}
if (!this._headElt) {
this._headElt = document.getElementsByTagName("head")[0];
}
this._headElt.appendChild(elt);
return elt;
},
sheet: function(s) {
this._head("link", {
type: "text/css",
media: "screen",
rel: "stylesheet",
href: s
});
},
inject: function(inCode) {
this._head("script", {type: "text/javascript"}, inCode);
},
_scripts: [],
script: function(s) {
if (this._inflight) {
this._scripts.push(s);
} else {
this._script(s);
}
},
_require: function(inScript) {
},
_script: function(inPath) {
this._inflight = true;
var elt = this._head("script", {
type: "text/javascript",
src: inPath
});
var self = this;
if (enyo.platform.ie && enyo.platform.ie <= 8) {
elt.onreadystatechange = function() {
if (elt.readyState === 'complete' || elt.readyState === 'loaded') {
elt.onreadystatechange = "";
self._loaded(inPath);
}
};
}
else {
elt.onload = function() {
self._loaded(inPath);
};
elt.onerror = function() {
self._error(inPath);
};
}
},
_continue: function() {
this._inflight = false;
var script = this._scripts.pop();
if (script) {
this._script(script);
}/* else {
this._done();
}*/
},
_loaded: function(inPath) {
//console.log("loaded [" + inPath + "]");
this._continue();
},
_error: function(inPath) {
//console.log("error [" + inPath + "]");
// another hack: cannot bubble up errors from here. This
// object is isolated from the analyser by enyo.loader. Have
// to send a signal for the error to show up on user's screen
enyo.Signals.send("onAnalyserError",{ msg : "Failed to load " + inPath } );
this._continue();
}
};