/*global Arenite:true*/
/*jshint evil:true*/
/*global Arenite:true*/
/*jshint evil:true*/
Collection of utility functions to handle loading resources.
Arenite.Loader = function (arenite) {
var firefoxOs = navigator.userAgent.indexOf('Firefox') > -1 && navigator.userAgent.indexOf("Mobile") > -1;
var _createCORSRequest = function (method, url, success, failure) {
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.setRequestHeader(“X-Requested-With”, “XMLHttpRequest”); xhr.setRequestHeader(“Access-Control-Allow-Origin”, window.location.origin);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status % 100 < 4) {
success();
} else {
failure();
}
}
};
if (arenite.config.withCredentials) {
xhr.withCredentials = true;
}
return xhr;
};
var _loadResource = function (url, callback, error) {
var req = _createCORSRequest('GET', url, function () {
callback(req);
}, function () {
if (typeof error === 'function') {
error(req);
}
});
req.send();
};
var _loadStyleWithTag = function (url, callback) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('link');
script.rel = 'stylesheet';
script.type = 'text/css';
if (navigator.appVersion.indexOf("MSIE 9") === -1) { //IE 9 calls the callback twice
script.onreadystatechange = function () {
if (this.readyState === 'complete') {
callback();
}
};
}
script.onload = callback;
script.href = url;
head.appendChild(script);
};
var _loadScriptWithTag = function (url, callback) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.async = true;
script.type = 'text/javascript';
if (navigator.appVersion.indexOf("MSIE 9") === -1) { //IE 9 calls the callback twice
script.onreadystatechange = function () {
if (this.readyState === 'complete') {
callback();
}
};
}
script.onload = callback;
script.src = url;
head.appendChild(script);
};
var _loadScriptAsResource = function (url, callback) {
_loadResource(url, function (req) {
analyze the script for “annotation” type configurations
arenite.annotation.processAnnotations(req.responseText);
var file_tag = '\n//@ sourceURL=' + window.location.origin + '/' + url + '\n//# sourceURL=' + window.location.origin + '/' + url;
eval(req.responseText + file_tag);
callback();
});
};
var _sameOrigin = function (url) {
var loc = window.location;
var a = document.createElement('a');
a.href = url;
return a.hostname === loc.hostname &&
a.port === loc.port &&
a.protocol === loc.protocol;
};
var _loadScriptFrom = function (url, callback) {
var fileExt = url.match(/.*\.(\w+)\?*.*/)[1];
if (_sameOrigin(url) && !firefoxOs && fileExt === 'js') {
_loadScriptAsResource(url, callback);
} else {
if (fileExt === 'js') {
_loadScriptWithTag(url, callback);
} else if (fileExt === 'css') {
_loadStyleWithTag(url, callback);
} else {
throw 'Uknown extension "' + fileExt + '"';
}
}
};
var _loadScript = function (script, done) {
if (typeof script === 'string') {
_loadScriptFrom(script, done);
} else {
_loadScriptFrom(script.url, function () {
script.instances.forEach(function (instance, instanceName) {
var windowInstance = window[instance];
if(windowInstance){
arenite.context.add(instanceName, windowInstance);
}
delete window[instance];
});
if (typeof done === 'function') {
done();
}
});
}
};
return {
loader: {
Load a given resource using ajax.
loadResource(url, callback, error)
where url is the url to fetch from, callback is a function called with the ajax request after
it’s succesful completion and error is an optional callback to handle errors when fetching the resource
loadResource: _loadResource,
Load a script. This method will choose the best method to load the script (using tag or ajax) depending on the origin of the script. Additionally it can extract variables exposed in the window object and register them as instances in arenite.
loadScript(script, callback)
where script is either a url string or a structure defining a url and instances to be extracted from
the window object into arenite and callback is the callback for when the script is loaded.
loadScript: _loadScript
}
};
};