Repository navigation
Expand file tree
/
Copy pathJumpStart.js
More file actions
176 lines (152 loc) · 4.48 KB
/
Copy pathJumpStart.js
File metadata and controls
176 lines (152 loc) · 4.48 KB
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
(function (nameSpace) {
"use strict";
var n = nameSpace || {},
j = [],
k = {},
vars = {},
pathCache = {};
vars.rootPath = function () {
if (!!document.currentScript && !!document.currentScript.src) {
return document.currentScript.src;
} else {
// Calculate root path by reading the last rendered script block from the page.
var ss = document.getElementsByTagName('script');
if (!!ss[ss.length-1].src) {
return ss[ss.length-1].src;
} else if (!!ss[ss.length-1].dataset.path) {
return ss[ss.length-1].dataset.path;
} else {
return '/';
}
}
}().replace(/(?:\/minified)?\/[a-zA-Z]+\.js$/, '');
// Class() - syntactic sugar function.
n.Class = function (className) {
// Create a variable
var a;
var thisClassName = className || 'jClass';
a = function jClass () {
if (arguments.length > 0) {
return this.init.apply(this, arguments) || this;
} else {
return this.init() || this;
}
};
try {
Object.defineProperty(a, 'name', { 'value': thisClassName });
} catch (e) {
a = new Function('return ' + a.toString().replace('jClass', thisClassName) + ';' )();
}
a.prototype = {
'init': function () {},
'toString': function () { return this.name; }
};
//Create empty constructor.
a.extend = function (o) {
if (!Object.create) {
var Temporary = function () {};
//Assign target prototype by reference.
Temporary.prototype = o.prototype;
//Inherit prototype via the temporary constructor.
this.prototype = new Temporary();
} else {
this.prototype = Object.create(o.prototype);
}
this.prototype.constructor = a;
//Save the parent prototype's init method.
this.prototype.parent = o.prototype;
return this;
};
return a;
};
n.Main = function () {};
n.jumpStart = function (f) {
if (!!f && typeof f === "function") {
j.push(f);
} else {
throw('Invalid Javascript object, cannot invoke module');
}
};
n.set = function (varName, varValue) {
vars[varName] = varValue;
};
n.get = function (varName) {
if (typeof vars[varName] !== 'undefined') {
return vars[varName];
}
return null;
};
// Note to self: REQUIRE = async false, execute the scripts in the order you request them ASAP but not before parsing completes.
// Note to self: INCLUDE = async true, execute the scripts as and when they arrive don't worry about order.
// Node to self: Calling either REQUIRE or INCLUDE after DOMContentLoaded event have identical results.
function addScript (path, _async) {
var script = document.createElement('script');
script.src = path;
script.async = !!_async;
return document.head.appendChild(script);
}
function resolve (jsFileName, async) {
if (jsFileName.indexOf('/') === -1) {
var path = J.get('rootPath') + '/' + jsFileName;
var selector = "[src$='/" + jsFileName + "']";
} else {
var path = jsFileName;
var selector = "[src='" + jsFileName + "']";
}
if(!document.querySelector(selector)) {
n.log("Importing " + path );
pathCache[path] = new Promise( function(done) {
addScript(path, async).addEventListener('load', done, false);
} );
} else {
n.log("File " + path + " has already been imported.", "warning");
}
return pathCache[path];
}
n.require = function(jsFileName){
return resolve(jsFileName, false);
};
n.include = function(jsFileName){
return resolve(jsFileName, true);
};
n.export = function(moduleName, moduleObject){
if (!!moduleObject && (typeof moduleObject === "function" || typeof moduleObject === "object") ) {
k[moduleName] = moduleObject;
} else {
n.log('Cannot export ' + moduleName + ' as it is not a valid function or object');
}
};
n.import = function(moduleName){
if (!!moduleName && !!k[moduleName] ) {
return k[moduleName];
}
};
n.log = function(obj, severity){
var severe = severity || 'info';
if(!!console && !!console.log) {
var consoleOptions = {
'info': console.log,
'warning': !!console.warn ? console.warn : console.log,
'error': !!console.error ? console.error : console.log
};
consoleOptions[severe].bind(console, obj).call();
}
};
function s(e) {
var i = 0, l = j.length;
for (i; i < l; i += 1) {
if (!!j[i].name){
k[j[i].name] = new j[i](n,k);
} else {
new j[i](n,k);
}
}
n.Main(n,k);
}
if (!!window.addEventListener) {
window.addEventListener('load', function (e) { s(e); }, false);
} else {
window.attachEvent('onload', function (e) { var E = e || window.event; s(E); });
}
return n;
}(window.J = window.J || {}));