/
exo.js
563 lines (486 loc) · 14.4 KB
/
exo.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
;(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function (root) {
var _exo;
var exo = require('../lib/exo');
exo.ns = function() {
var ctx, i, len, ns, j, sublen, part;
for (i = 0, len = arguments.length; i < len; i++) {
ctx = root;
ns = arguments[i].split('.');
for (j = 0, sublen = ns.length; j < sublen; j++) {
part = ns[j];
if (!(part in ctx)) {
ctx[part] = {};
}
ctx = ctx[part];
}
}
};
exo.resolve = function (name) {
if (!name) return root;
var ns = String.prototype.split.call(name, '.');
var ctx = root;
var i, len, part;
for (i = 0, len = ns.length; i < len; i ++) {
part = ns[i];
if (part in ctx) ctx = ctx[part];
else throw new Error('Can not resolve: ' + name);
}
return ctx;
};
if ("function" === typeof root.define && root.define.amd) {
define([], function () {
return exo;
});
} else {
_exo = root.exo;
root.exo = exo;
exo.noConflict = function () { // no conflict
root.exo = _exo;
return exo;
};
}
})(window);
},{"../lib/exo":2}],2:[function(require,module,exports){
"use strict";
var exo = module.exports = {};
var AUTO_ID = 1000,
objectPrototype = Object.prototype,
toString = objectPrototype.toString,
TemplateClass = function () {
};
var CONFIG_RESERVED_KEYS_ARRAY = [
"constructor", "extend", "singleton", "statics", "mixins", "inherits",
"$extend", "$extends", "$singleton", "$statics", "$mixins", "$inherits"
],
CONFIG_RESERVED_KEYS = {};
CONFIG_RESERVED_KEYS_ARRAY.forEach(function (key) {
CONFIG_RESERVED_KEYS[key] = 1;
});
function getAutoId(prefiex) {
return (prefiex ? prefiex.toString() : '') + (++AUTO_ID);
}
function isFunction(value) {
return value && typeof value === 'function';
}
//function isObject(value) {
// return value && typeof value === 'object';
//}
function isString(value) {
return typeof value === 'string';
}
var hasDefineProperty = (function () {
if (!isFunction(Object.defineProperty)) {
return false;
}
// Avoid IE8 bug
try {
Object.defineProperty({}, 'x', {});
} catch (e) {
return false;
}
return true;
})();
function Base() {
}
/**
* Copies all the properties of config to the specified object.
* Note that if recursive merging and cloning without referencing the original objects / arrays is needed, use
* {@link exo#merge} instead.
* @param {Object} object The receiver of the properties
* @param {Object} config The source of the properties
* @param {Object} [filter] The keys will be ignored
* @return {Object} returns obj
*/
exo.apply = apply;
function apply(object, config, filter) {
if (object && config && typeof config === 'object') {
var property;
for (property in config) {
if (!filter || !filter[property]) {
object[property] = config[property];
}
}
}
return object;
}
/**
* Copies all the properties of config to object if they don't already exist.
* @param {Object} object The receiver of the properties
* @param {Object} config The source of the properties
* @param {Object} [filter] The keys will be ignored
* @return {Object} returns obj
*/
exo.applyIf = applyIf;
function applyIf(object, config, filter) {
var property;
if (object) {
for (property in config) {
if (!filter || !filter[property]) {
if (object[property] === undefined) {
object[property] = config[property];
}
}
}
}
return object;
}
function createProxy(fn, context) {
return function () {
return fn.apply(context, arguments);
};
}
exo.clone = clone;
function clone(item) {
var type,
i,
_clone,
key;
if (item === null || item === undefined) {
return item;
}
type = toString.call(item);
// Date
if (type === '[object Date]') {
return new Date(item.getTime());
}
// Array
if (type === '[object Array]') {
i = item.length;
_clone = [];
while (i--) {
_clone[i] = clone(item[i]);
}
}
// Object
else if (type === '[object Object]' && item.constructor === Object) {
_clone = {};
for (key in item) {
_clone[key] = clone(item[key]);
}
}
return _clone || item;
}
exo.merge = merge;
function merge(destination) {
var i = 1,
ln = arguments.length,
mergeFn = merge,
cloneFn = clone,
object, key, value, sourceKey;
for (; i < ln; i++) {
object = arguments[i];
for (key in object) {
value = object[key];
if (value && value.constructor === Object) {
sourceKey = destination[key];
if (sourceKey && sourceKey.constructor === Object) {
mergeFn(sourceKey, value);
}
else {
destination[key] = cloneFn(value);
}
}
else {
destination[key] = value;
}
}
}
return destination;
}
exo.bind = bind;
function bind(fn, context) {
var curriedArgs = Array.prototype.slice.call(arguments, 2);
if (curriedArgs.length) {
return function () {
var allArgs = curriedArgs.slice(0);
for (var i = 0, n = arguments.length; i < n; ++i) {
allArgs.push(arguments[i]);
}
fn.apply(context, allArgs);
};
} else {
return createProxy(fn, context);
}
}
/**
* Forward `functions` from `from` to `to`.
*
* The `this` context of forwarded functions remains bound to the `to` object,
* ensuring that property polution does not occur.
*
* @param {Object} from
* @param {Object} to
* @param {Array} functions
* @api private
*/
exo.forward = forward;
function forward(from, to, functions) {
for (var i = 0, len = functions.length; i < len; i++) {
var method = functions[i];
from[method] = bind(to[method], to);
}
}
function wrap(fn, argsize) {
argsize = argsize ? Math.max(argsize, fn.length) : fn.length;
var wrapper;
switch (argsize) {
case 0:
wrapper = function () {
fn.call(this);
};
break;
case 1:
wrapper = function (a) {
fn.call(this, a);
};
break;
case 2:
wrapper = function (a1, a2) {
fn.call(this, a1, a2);
};
break;
case 3:
wrapper = function (a1, a2, a3) {
fn.call(this, a1, a2, a3);
};
break;
case 4:
wrapper = function (a1, a2, a3, a4) {
fn.call(this, a1, a2, a3, a4);
};
break;
default:
wrapper = function () {
fn.apply(this, arguments);
};
}
return wrapper;
}
function makeCtor(parent) {
if (parent.constructor === Object) {
return function () {
};
} else {
return wrap(parent.constructor);
}
}
exo.overload = overload;
function overload(fns) {
function overloaded() {
var fn = overloaded.__fns[arguments.length];
if (!fn) {
var i = Math.min(overloaded.__max, arguments.length + 1);
while (!(i in overloaded.__fns) && i < overloaded.__max) {
i++;
}
fn = overloaded.__fns[i];
}
/*jshint validthis:true */
return fn.apply(this, arguments);
}
overloaded.__fns = [];
overloaded.__max = 0;
var fn;
for (var i = 0; i < fns.length; i++) {
fn = fns[i];
overloaded.__fns[fn.length] = fn;
if (overloaded.__max < fn.length) {
overloaded.__max = fn.length;
}
}
return overloaded;
}
/**
* Sets the key of object with the specified value.
* The property is obfuscated, by not being enumerable, configurable and writable.
*
* @param {Object} obj The object
* @param {String} key The key
* @param {Mixed} value The value
* @param {Boolean} [isWritable] True to be writable, false otherwise (defaults to false)
* @param {Boolean} [isDeletable] True to be deletable, false otherwise (defaults to false)
*/
exo.obfuscateProperty = obfuscateProperty;
function obfuscateProperty(obj, key, value, isWritable, isDeletable) {
if (hasDefineProperty) {
Object.defineProperty(obj, key, {
value: value,
configurable: isDeletable || false,
writable: isWritable || false,
enumerable: false
});
} else {
obj[key] = value;
}
}
exo.chain = chain;
function chain(object) {
TemplateClass.prototype = object;
var result = new TemplateClass();
TemplateClass.prototype = null;
return result;
}
exo.define = define;
function define(className, data) {
var hasClassName = isString(className);
if (!data) {
data = (hasClassName ? {} : className) || {};
}
if (hasClassName) {
data.$classname = className;
}
var parentClass = data.$extends || data.$extend || data.extend;
if (isString(parentClass) && isFunction(exo.resolve)) {
parentClass = exo.resolve(parentClass);
}
if (!isFunction(parentClass)) {
parentClass = Base;
}
var cls = extend(parentClass, data);
if (hasClassName && isFunction(exo.ns) && isFunction(exo.resolve)) {
var ns, c;
var i = className.lastIndexOf('.');
ns = i >= 0 ? className.substring(0, i) : null;
c = i >= 0 ? className.substring(i + 1) : className;
if (ns) {
ns(ns);
}
exo.resolve(ns)[c] = cls;
}
return cls;
}
exo.extend = extend;
function extend(parentClass, data) {
if (!data) {
data = parentClass;
parentClass = Base;
}
var parent = parentClass.prototype,
prototype = chain(parent),
params = (isFunction(data) ? data.call(prototype, parent, parentClass) : data) || {},
Clazz;
if (isFunction(params)) {
Clazz = params;
} else if (params.constructor !== Object) {
Clazz = params.constructor;
} else {
Clazz = makeCtor(parent);
}
prototype.constructor = Clazz;
Clazz.prototype = prototype;
// the '$super' property of class refers to its super prototype
obfuscateProperty(Clazz, '$super', parent);
// the '$superclass' property of class refers to its super class
obfuscateProperty(Clazz, '$superclass', parentClass);
if (typeof params === 'object') {
params.$singleton = params.$singleton || params.singleton;
params.$statics = params.$statics || params.statics;
params.$mixins = params.$mixins || params.mixins;
params.$inherits = params.$inherits || params.inherits;
__extend(Clazz, params, prototype);
if (params.$singleton) {
Clazz = new Clazz();
}
}
return Clazz;
}
function __extend(targetClass, data, targetPrototype) {
var prototype = targetPrototype || targetClass.prototype,
_statics = data.$statics,
_mixins = data.$mixins,
_inherits = data.$inherits;
if (_statics) {
// copy static properties from statics to class
apply(targetClass, _statics);
}
if (_mixins) {
mixins(targetClass, _mixins, targetPrototype);
}
if (_inherits) {
inherits(targetClass, _inherits, targetPrototype);
}
apply(prototype, data, CONFIG_RESERVED_KEYS);
if (data.toString !== Object.prototype.toString) {
prototype.toString = data.toString;
}
}
/**
*
* @param {Object} targetClass the target class
* @param {Object|Array} inherits the classes inherits from
* @param targetPrototype
*/
exo.inherits = inherits;
function inherits(targetClass, inherits, targetPrototype) {
var i, ln;
if (!targetPrototype) {
targetPrototype = targetClass.prototype;
}
if (Array.isArray(inherits)) {
for (i = 0, ln = inherits.length; i < ln; i++) {
__mixin(targetClass, null, inherits[i], targetPrototype);
}
}
else {
__mixin(targetClass, null, inherits, targetPrototype);
}
}
/**
*
* @param targetClass
* @param mixins
* @param targetPrototype
*/
exo.mixins = mixins;
function mixins(targetClass, mixins, targetPrototype) {
var name, item, i, ln;
if (!targetPrototype) {
targetPrototype = targetClass.prototype;
}
if (Array.isArray(mixins)) {
for (i = 0, ln = mixins.length; i < ln; i++) {
item = mixins[i];
name = item.prototype.$mixinId || item.$mixinId;
if (!name) {
name = item.$mixinId = getAutoId('__mixin__');
}
__mixin(targetClass, name, item, targetPrototype);
}
}
else {
for (var mixinName in mixins) {
if (mixins.hasOwnProperty(mixinName)) {
__mixin(targetClass, mixinName, mixins[mixinName], targetPrototype);
}
}
}
}
function __mixin(target, name, mixinClass, targetPrototype) {
var mixin = mixinClass.prototype,
prototype = targetPrototype || target.prototype,
key;
if (name) {
if (!prototype.hasOwnProperty('$mixins')) {
if ('$mixins' in prototype) {
prototype.$mixins = chain(prototype.$mixins);
}
else {
prototype.$mixins = {};
}
}
}
for (key in mixin) {
if (name && (key === '$mixins')) {
merge(prototype.$mixins, mixin[key]);
}
else if (typeof prototype[key] === 'undefined' && key !== '$mixinId') {
prototype[key] = mixin[key];
}
}
if (name) {
prototype.$mixins[name] = mixin;
}
}
},{}]},{},[1])
;