Skip to content

Instantly share code, notes, and snippets.

@cardil
Created September 29, 2012 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cardil/3804578 to your computer and use it in GitHub Desktop.
Save cardil/3804578 to your computer and use it in GitHub Desktop.
window.wave = window.wave || {};
//noinspection ThisExpressionReferencesGlobalObjectJS
wave.env = this;
/* COMPRESSOR START CUT */
wave.Loader = (function (window) {
"use strict";
var out, instance;
out = function(window) {
"use strict";
var basePathC, main;
main = window;
/**
* Finds base path of script
*
* @return {String}
* @private
*/
function findBasePath() {
var findFunc, basePath, thisScriptName;
findFunc = function (name) {
var script, scripts, i, src, l, length;
script = name + '.js';
scripts = document.getElementsByTagName('script');
for (i = scripts.length - 1; i >= 0; i = i - 1) {
src = scripts[i].src;
l = src.length;
length = script.length;
if (src.substr(l - length) === script) {
// set a global property here
return src.substr(0, l - length);
}
}
return null;
};
thisScriptName = 'TreeGraphEditor';
basePath = findFunc(thisScriptName);
if (basePath !== null) {
return basePath;
}
basePath = findFunc(thisScriptName + '-' + self.version);
return basePath;
}
function getRecursive(root, name) {
var splited, obj, i, subName;
splited = name.split(/\./g);
obj = root;
for (i in splited) {
if (splited.hasOwnProperty(i)) {
subName = splited[i];
if (typeof obj[subName] !== 'undefined') {
obj = obj[subName];
} else {
return null;
}
}
}
return obj;
}
function setRecursive(root, name, value) {
var splited, obj, i, subName;
splited = name.split(/\./g);
obj = root;
for (i in splited) {
if (splited.hasOwnProperty(i)) {
i = parseInt(i);
subName = splited[i];
if (typeof obj[subName] === 'undefined') {
if (i+1 === splited.length) {
obj[subName] = value;
break;
} else {
obj[subName] = {};
}
}
obj = obj[subName];
}
}
}
/**
*
* @param {String} libName
* @return {Object}
*/
this.require = function(libName) {
var thisBase, fullPath, lib, ret, fileName, tag1, tag2, head, tNode;
ret = getRecursive(main, libName);
if (ret !== null) {
return ret;
}
thisBase = basePathC || (basePathC = findBasePath());
fileName = libName.replace(/\./g, '/') + '.js';
fullPath = thisBase + fileName;
lib = window.jQuery.ajax({
url: fullPath,
dataType: "text",
async: false,
cache: true
});
head = document.getElementsByTagName('head')[0];
tag1 = document.createElement("script");
tag1.setAttribute('type', 'text/javascript');
tNode = document.createTextNode(lib.responseText);
tag1.appendChild(tNode);
tag2 = document.createElement("script");
tag2.setAttribute('type', 'text/javascript');
tag2.setAttribute('src', fullPath);
head.appendChild(tag1);
ret = getRecursive(main, libName);
return ret;
};
/**
* @return {String}
*/
this.getBasePath = function() {
if (!basePathC) {
basePathC = findBasePath();
}
return basePathC;
};
/**
* Defines module
* @param {String} name
* @param {Object} module
*/
this.define = function(name, module) {
setRecursive(main, name, module);
};
};
/**
* Singleton
*
* @return {wave.Loader}
*/
out.getInstance = function () {
return instance || (instance = new wave.Loader(window));
};
return out;
})(wave.env);
require = wave.env.require || wave.Loader.getInstance().require;
define = wave.env.define || wave.Loader.getInstance().define;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment