-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmix.js
440 lines (378 loc) · 15.5 KB
/
mix.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
(function (global){
global.Mix = {
//------------config----------
nocache: false,
path: {},
synchronous: false,
//----------private members---------
_countModules: 0,
_loadingCountModules: 0, //загруженные модули с зависимостями
_loadingCountScripts: 0, //загруженные скрипты на клиента
_modules: {},
_loadingModules: {}, //загружаемые модули
//----------public functions--------
/**
* загрузка модуля из аттрибута data-main
*/
init: function (){
var scripts = Mix.$('script'),
mainModule = null;
for (var i = 0, l = scripts.length; i < l; ++i) {
var dataMain = scripts[i].getAttribute('data-main');
if (dataMain != null) {
mainModule = dataMain === '' ? 'main' : dataMain;
break;
}
}
if (mainModule)
Mix.config({synchronous: true}).loadScript(mainModule, '');
},
namespace: function (namespaces){
var ns = namespaces.split('.'),
current = namespaces.length > 0 ? window[ns[0]] : window;
if (current === undefined) {
current = window[ns[0]] = {};
}
for (var i = 1; i < ns.length; ++i) {
current = current[ns[i]] = current[ns[i]] || {};
}
return current;
},
getClassName: function (classPath){
var path = classPath.split('.');
return path[path.length - 1];
},
$: function (selector){
return selector.charAt(0) == '#' ? document.getElementById(selector.substr(1)) : document.getElementsByTagName(selector);
},
apply: function (o, c, defaults){
// no "this" reference for friendly out of scope calls
if (defaults) {
Mix.apply(o, defaults);
}
if (o && c && typeof c == 'object') {
for (var p in c) {
o[p] = c[p];
}
}
return o;
},
config: function (config){
return this.apply(this, config);
},
define: function (){
var classPath = '',
requires = [],
config = {};
//разбор аргументов. Если их 3 - classPath, requires, config
//если 2 - classPath, config
switch (arguments.length) {
case 2:
classPath = arguments[0];
config = arguments[1];
break;
case 3:
classPath = arguments[0];
requires = arguments[1] || [];
config = arguments[2];
break;
default:
throw('Incorrect arguments');
}
var path = classPath.split('.');
var className = path[path.length - 1];
var classNamespace = this.namespace(path.slice(0, path.length - 1).join('.'));
if (config.extend != undefined) {
requires.push(config.extend);
var pathParent = config.extend.split('.');
var parentClassName = pathParent[pathParent.length - 1];
var parentClassNamespace = this.namespace(pathParent.slice(0, pathParent.length - 1).join('.'));
}
Mix.module({
name: classPath,
requires: requires,
body: function (){
var parent = parentClassNamespace && parentClassNamespace[parentClassName] || Mix.Class,
newClass = parent.create(config);
//добавляю статические функции
for (var f in config) {
if (!config.hasOwnProperty(f)) continue;
if (/static_/.test(f)) {
var funcName = f.substring(7);
newClass[funcName] = config[f];
}
}
classNamespace[className] = newClass;
console.log('class:', className);
}
});
},
module: function (config){
config.loaded = false;
if (!this._modules[config.name]) {
this._countModules++;
}
this.apply(this._modules[config.name], config);
if (!this._loadingModules[config.name]) {
this._loadingCountModules++;
}
this.apply(this._loadingModules[config.name], config);
//TODO: подписаться на событие onReady
this.process();
},
getModuleConfig: function (moduleData){
//TODO: отрефакторить!
var config;
function parseName(name){
var parts = name.split(':');
if (parts.length == 1) {
return {
name: name
};
} else if (parts.length == 2) {
return {
pathName: parts[0],
name: parts[1]
}
} else {
throw 'Module name is not correct!';
}
}
if (typeof moduleData == "string") {
config = this._modules[parseName(moduleData).name];
if (!config) {
config = {
name: moduleData
};
}
} else if (typeof moduleData == "object") {
config = this._modules[moduleData.name];
if (!config) {
config = moduleData;
}
} else {
throw ("Module data is not correct!");
}
this.apply(config, parseName(config.name));
return config;
},
checkCircular: function (requiredFrom, moduleName, chain){
chain = chain || [moduleName];
if (requiredFrom) {
chain.push(requiredFrom.name);
if (requiredFrom.name == moduleName) {
throw ('Unresolved (circular?) dependencies: ' + chain.join('->'));
} else {
this.checkCircular(requiredFrom.requiredFrom, moduleName, chain);
}
}
},
loadModule: function (module, requiredFrom){
if (module.loaded == true) return true;
var load = true,
requires = module.requires || [];
for (var i = 0, l = requires.length; i < l; ++i) {
var requireModule = this.getModuleConfig(requires[i]);
if (!requireModule.requiredFrom)
requireModule.requiredFrom = module;
this.checkCircular(module, requireModule.name);
if (!requireModule.loaded) {
load = false;
this.loadModule(requireModule, module);
}
}
if (load && !this._modules[module.name]) {
load = false;
this.loadScript(module.name, requiredFrom, module.pathName);
}
return (load && module.scriptLoaded);
},
process: function (){
var moduleLoaded = false;
for (var k in this._loadingModules) {
if (!this._loadingModules.hasOwnProperty(k)) continue;
var module = this._loadingModules[k];
if (module.loaded) continue;
module.loaded = this.loadModule(module, module.requiredFrom);
if (module.loaded) {
moduleLoaded = true;
if (module.body) module.body();
this._loadingCountModules++;
this._loadingModules[module.name] = null;
delete this._loadingModules[module.name];
this.onProgress(this._countModules, this._loadingCountModules);
}
}
//если загружен хоть один модуль - запускаю проверку повторно
if (moduleLoaded) this.process();
},
onProgress: function (count, val){
// if (count <= 0) return;
// var p = val * 100 / count;
// console.log('progress: ' + Math.round(p) + '%');//debug
},
loadScript: function (name, requiredFrom, pathName){
var me = this,
prefix = '';
if (pathName) {
if (!(prefix = this.path[pathName])) throw('Undefined path: ' + pathName);
prefix += prefix.indexOf(prefix.length - 1) == '/' ? '' : '/';
}
var url = prefix + name.replace(/\./g, '/') + '.js' + (this.nocache ? '?nocache=' + new Date().getTime() : '');
var config = {
name: name,
requiredFrom: requiredFrom,
pathName: pathName,
requires: [],
loaded: false,
scriptLoaded: false,
body: null
};
if (!this._modules[name]) {
this._countModules++;
this._modules[name] = config;
}
if (!this._loadingModules[name]) {
this._loadingModules[name] = config;
}
function onLoad(){
me._modules[name].scriptLoaded = true;
me._loadingModules[name].scriptLoaded = true;
me._loadingCountScripts--;
me.process();
}
this._loadingCountScripts++;
if (this.synchronous) {
this.injectScript(url, onLoad, function (){
throw ('Failed to load module/class ' + name + ' at ' + url + ' ' + 'required from ' + requiredFrom.name);
}, this)
} else {
this.loadXHRScript(url, onLoad, function (){
throw ('Failed to load module/class ' + name + ' at ' + url + ' ' + 'required from ' + requiredFrom.name);
}, this)
}
},
loadXHRScript: function (url, onLoad, onError, scope){
var isCrossOriginRestricted = false,
fileName = url.split('/').pop(),
xhr, status;
if (typeof XMLHttpRequest !== 'undefined') {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
try {
xhr.open('GET', url, false);
xhr.send(null);
} catch (e) {
isCrossOriginRestricted = true;
}
status = (xhr.status === 1223) ? 204 : xhr.status;
if (!isCrossOriginRestricted) {
isCrossOriginRestricted = (status === 0);
}
if (isCrossOriginRestricted) {
onError.call(this, "Failed loading synchronously via XHR: '" + url + "'; It's likely that the file is either " +
"being loaded from a different domain or from the local file system whereby cross origin " +
"requests are not allowed due to security reasons. Use asynchronous loading with " +
"Ext.require instead.", this.synchronous);
}
else if (status >= 200 && status < 300) {
// Firebug friendly, file names are still shown even though they're eval'ed code
new Function(xhr.responseText + "\n//@ sourceURL=" + fileName)();
onLoad.call(scope);
}
else {
onError.call(this, "Failed loading synchronously via XHR: '" + url + "'; please " +
"verify that the file exists. " +
"XHR status code: " + status, this.synchronous);
}
// Prevent potential IE memory leak
xhr = null;
},
injectScript: function (url, onLoad, onError, scope){
var script = document.createElement('script'),
me = this,
onLoadFn = function (){
me.cleanupScriptElement(script);
onLoad.call(scope);
},
onErrorFn = function (){
me.cleanupScriptElement(script);
onError.call(scope);
};
script.type = 'text/javascript';
script.src = url;
script.onload = onLoadFn;
script.onerror = onErrorFn;
script.onreadystatechange = function (){
if (this.readyState === 'loaded' || this.readyState === 'complete') {
onLoadFn();
}
};
this.$('head')[0].appendChild(script);
return script;
},
cleanupScriptElement: function (script){
script.onload = null;
script.onreadystatechange = null;
script.onerror = null;
return this;
}
};
var initializing = false, fnTest = /xyz/.test(function (){
xyz;
}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
Mix.Class = function (){
};
// Create a new Class that inherits from this class
Mix.Class.create = function (prop){
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function (name, fn){
return function (){
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
// The dummy class constructor
function Class(){
// All construction is actually done in the init method
if (!initializing && this.init)
this.init.apply(this, arguments);
}
// Populate our constructed prototype object
Class.prototype = prototype;
// Enforce the constructor to be what we expect
Class.prototype.constructor = Class;
// And make this class extendable
Class.create = arguments.callee;
return Class;
};
Function.prototype.bind = function (scope){
var _function = this;
return function (){
return _function.apply(scope, arguments);
}
};
Mix.init();
})(this);