-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBootstrap.js
813 lines (686 loc) · 23.4 KB
/
Bootstrap.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2004-2008 1&1 Internet AG, Germany, http://www.1und1.de
License:
LGPL: http://www.gnu.org/licenses/lgpl.html
EPL: http://www.eclipse.org/org/documents/epl-v10.php
See the LICENSE file in the project's top-level directory for details.
Authors:
* Sebastian Werner (wpbasti)
* Andreas Ecker (ecker)
* Martin Wittemann (martinwittemann)
************************************************************************ */
/**
* Create namespace
*
* @ignore(qx.data)
* @ignore(qx.data.IListData)
* @ignore(qx.util.OOUtil)
*/
if (!window.qx) {
window.qx = {};
}
/**
* Bootstrap qx.Bootstrap to create myself later
* This is needed for the API browser etc. to let them detect me
*/
qx.Bootstrap = {
genericToString : function() {
return "[Class " + this.classname + "]";
},
createNamespace : function(name, object)
{
var splits = name.split(".");
var part = splits[0];
var parent = qx.$$namespaceRoot && qx.$$namespaceRoot[part] ? qx.$$namespaceRoot : window;
for (var i=0, len=splits.length-1; i<len; i++, part=splits[i])
{
if (!parent[part]) {
parent = parent[part] = {};
} else {
parent = parent[part];
}
}
// store object
parent[part] = object;
// return last part name (e.g. classname)
return part;
},
setDisplayName : function(fcn, classname, name)
{
fcn.displayName = classname + "." + name + "()";
},
setDisplayNames : function(functionMap, classname)
{
for (var name in functionMap)
{
var value = functionMap[name];
if (value instanceof Function) {
value.displayName = classname + "." + name + "()";
}
}
},
base : function(args, varargs)
{
if (qx.Bootstrap.DEBUG) {
if (!qx.Bootstrap.isFunction(args.callee.base)) {
throw new Error(
"Cannot call super class. Method is not derived: " +
args.callee.displayName
);
}
}
if (arguments.length === 1) {
return args.callee.base.call(this);
} else {
return args.callee.base.apply(this, Array.prototype.slice.call(arguments, 1));
}
},
define : function(name, config)
{
if (!config) {
config = { statics : {} };
}
var clazz;
var proto = null;
qx.Bootstrap.setDisplayNames(config.statics, name);
if (config.members || config.extend)
{
qx.Bootstrap.setDisplayNames(config.members, name + ".prototype");
clazz = config.construct || new Function;
if (config.extend) {
this.extendClass(clazz, clazz, config.extend, name, basename);
}
var statics = config.statics || {};
// use keys to include the shadowed in IE
for (var i=0, keys=qx.Bootstrap.keys(statics), l=keys.length; i<l; i++) {
var key = keys[i];
clazz[key] = statics[key];
}
proto = clazz.prototype;
// Enable basecalls within constructor
proto.base = qx.Bootstrap.base;
proto.name = proto.classname = name;
var members = config.members || {};
var key, member;
// use keys to include the shadowed in IE
for (var i=0, keys=qx.Bootstrap.keys(members), l=keys.length; i<l; i++) {
key = keys[i];
member = members[key];
// Enable basecalls for methods
// Hint: proto[key] is not yet overwritten here
if (member instanceof Function && proto[key]) {
member.base = proto[key];
}
proto[key] = member;
}
}
else
{
clazz = config.statics || {};
// Merge class into former class (needed for 'optimize: ["statics"]')
if (qx.Bootstrap.$$registry && qx.Bootstrap.$$registry[name]) {
var formerClass = qx.Bootstrap.$$registry[name];
// Add/overwrite properties and return early if necessary
if (this.keys(clazz).length !== 0) {
// Execute defer to prevent too early overrides
if (config.defer) {
config.defer(clazz, proto);
}
for (var curProp in clazz) {
formerClass[curProp] = clazz[curProp];
}
return formerClass;
}
}
}
// Store type info
clazz.$$type = "Class";
// Attach toString
if (!clazz.hasOwnProperty("toString")) {
clazz.toString = this.genericToString;
}
// Create namespace
var basename = name ? this.createNamespace(name, clazz) : "";
// Store names in constructor/object
clazz.name = clazz.classname = name;
clazz.basename = basename;
clazz.$$events = config.events;
// Execute defer section
if (config.defer) {
config.defer(clazz, proto);
}
// Store class reference in global class registry
if (name != null) {
qx.Bootstrap.$$registry[name] = clazz;
}
return clazz;
}
};
/**
* Internal class that is responsible for bootstrapping the qooxdoo
* framework at load time.
*/
qx.Bootstrap.define("qx.Bootstrap",
{
statics :
{
/** Timestamp of qooxdoo based application startup */
LOADSTART : qx.$$start || new Date(),
/**
* Mapping for early use of the qx.debug environment setting.
*/
DEBUG : (function() {
// make sure to reflect all changes here to the environment class!
var debug = true;
if (qx.$$environment && qx.$$environment["qx.debug"] === false) {
debug = false;
}
return debug;
})(),
/**
* Minimal accessor API for the environment settings given from the
* generator.
*
* WARNING: This method only should be used if the
* {@link qx.core.Environment} class is not loaded!
*
* @param key {String} The key to get the value from.
* @return {var} The value of the setting or <code>undefined</code>.
*/
getEnvironmentSetting : function(key) {
if (qx.$$environment) {
return qx.$$environment[key];
}
},
/**
* Minimal mutator for the environment settings given from the generator.
* It checks for the existance of the environment settings and sets the
* key if its not given from the generator. If a setting is available from
* the generator, the setting will be ignored.
*
* WARNING: This method only should be used if the
* {@link qx.core.Environment} class is not loaded!
*
* @param key {String} The key of the setting.
* @param value {var} The value for the setting.
*/
setEnvironmentSetting : function(key, value) {
if (!qx.$$environment) {
qx.$$environment = {};
}
if (qx.$$environment[key] === undefined) {
qx.$$environment[key] = value;
}
},
/**
* Creates a namespace and assigns the given object to it.
*
* @internal
* @signature function(name, object)
* @param name {String} The complete namespace to create. Typically, the last part is the class name itself
* @param object {Object} The object to attach to the namespace
* @return {String} last part of the namespace (which object is assigned to)
* @throws {Error} when the given object already exists.
*/
createNamespace : qx.Bootstrap.createNamespace,
/**
* Offers the ability to change the root for creating namespaces from window to
* whatever object is given.
*
* @param root {Object} The root to use.
* @internal
*/
setRoot : function(root) {
qx.$$namespaceRoot = root;
},
/**
* Call the same method of the super class.
*
* @signature function(args, varargs)
* @param args {arguments} the arguments variable of the calling method
* @param varargs {var} variable number of arguments passed to the overwritten function
* @return {var} the return value of the method of the base class.
*/
base : qx.Bootstrap.base,
/**
* Define a new class using the qooxdoo class system.
* Lightweight version of {@link qx.Class#define} with less features.
*
* @signature function(name, config)
* @param name {String?} Name of the class. If null, the class will not be
* attached to a namespace.
* @param config {Map ? null} Class definition structure. The configuration map has the following keys:
* <table>
* <tr><th>Name</th><th>Type</th><th>Description</th></tr>
* <tr><th>extend</th><td>Class</td><td>The super class the current class inherits from.</td></tr>
* <tr><th>construct</th><td>Function</td><td>The constructor of the class.</td></tr>
* <tr><th>statics</th><td>Map</td><td>Map of static values / functions of the class.</td></tr>
* <tr><th>members</th><td>Map</td><td>Map of instance members of the class.</td></tr>
* <tr><th>defer</th><td>Function</td><td>Function that is called at the end of
* processing the class declaration.</td></tr>
* </table>
* @return {Class} The defined class.
*/
define : qx.Bootstrap.define,
/**
* Sets the display name of the given function
*
* @signature function(fcn, classname, name)
* @param fcn {Function} the function to set the display name for
* @param classname {String} the name of the class the function is defined in
* @param name {String} the function name
*/
setDisplayName : qx.Bootstrap.setDisplayName,
/**
* Set the names of all functions defined in the given map
*
* @signature function(functionMap, classname)
* @param functionMap {Object} a map with functions as values
* @param classname {String} the name of the class, the functions are
* defined in
*/
setDisplayNames : qx.Bootstrap.setDisplayNames,
/**
* This method will be attached to all classes to return
* a nice identifier for them.
*
* @internal
* @signature function()
* @return {String} The class identifier
*/
genericToString : qx.Bootstrap.genericToString,
/**
* Inherit a clazz from a super class.
*
* This function differentiates between class and constructor because the
* constructor written by the user might be wrapped and the <code>base</code>
* property has to be attached to the constructor, while the <code>superclass</code>
* property has to be attached to the wrapped constructor.
*
* @param clazz {Function} The class's wrapped constructor
* @param construct {Function} The unwrapped constructor
* @param superClass {Function} The super class
* @param name {Function} fully qualified class name
* @param basename {Function} the base name
*/
extendClass : function(clazz, construct, superClass, name, basename)
{
var superproto = superClass.prototype;
// Use helper function/class to save the unnecessary constructor call while
// setting up inheritance.
var helper = new Function();
helper.prototype = superproto;
var proto = new helper();
// Apply prototype to new helper instance
clazz.prototype = proto;
// Store names in prototype
proto.name = proto.classname = name;
proto.basename = basename;
/*
- Store base constructor to constructor-
- Store reference to extend class
*/
construct.base = superClass;
clazz.superclass = superClass;
/*
- Store statics/constructor onto constructor/prototype
- Store correct constructor
- Store statics onto prototype
*/
construct.self = clazz.constructor = proto.constructor = clazz;
},
/**
* Find a class by its name
*
* @param name {String} class name to resolve
* @return {Class} the class
*/
getByName : function(name) {
return qx.Bootstrap.$$registry[name];
},
/** @type {Map} Stores all defined classes */
$$registry : {},
/*
---------------------------------------------------------------------------
OBJECT UTILITY FUNCTIONS
---------------------------------------------------------------------------
*/
/**
* Get the number of own properties in the object.
*
* @param map {Object} the map
* @return {Integer} number of objects in the map
* @lint ignoreUnused(key)
*/
objectGetLength : function(map) {
return qx.Bootstrap.keys(map).length;
},
/**
* Inserts all keys of the source object into the
* target objects. Attention: The target map gets modified.
*
* @param target {Object} target object
* @param source {Object} object to be merged
* @param overwrite {Boolean ? true} If enabled existing keys will be overwritten
* @return {Object} Target with merged values from the source object
*/
objectMergeWith : function(target, source, overwrite)
{
if (overwrite === undefined) {
overwrite = true;
}
for (var key in source)
{
if (overwrite || target[key] === undefined) {
target[key] = source[key];
}
}
return target;
},
/**
* IE does not return "shadowed" keys even if they are defined directly
* in the object.
*
* @internal
* @type {String[]}
*/
__shadowedKeys :
[
"isPrototypeOf",
"hasOwnProperty",
"toLocaleString",
"toString",
"valueOf",
"propertyIsEnumerable",
"constructor"
],
/**
* Get the keys of a map as array as returned by a "for ... in" statement.
*
* @signature function(map)
* @internal
* @param map {Object} the map
* @return {Array} array of the keys of the map
*/
keys :
({
"ES5" : Object.keys,
"BROKEN_IE" : function(map)
{
if (map === null || (typeof map != "object" && typeof map != "function")) {
throw new TypeError("Object.keys requires an object as argument.");
}
var arr = [];
var hasOwnProperty = Object.prototype.hasOwnProperty;
for (var key in map) {
if (hasOwnProperty.call(map, key)) {
arr.push(key);
}
}
// IE does not return "shadowed" keys even if they are defined directly
// in the object. This is incompatible with the ECMA standard!!
// This is why this checks are needed.
var shadowedKeys = qx.Bootstrap.__shadowedKeys;
for (var i=0, a=shadowedKeys, l=a.length; i<l; i++)
{
if (hasOwnProperty.call(map, a[i])) {
arr.push(a[i]);
}
}
return arr;
},
"default" : function(map)
{
if (map === null || (typeof map != "object" && typeof map != "function")) {
throw new TypeError("Object.keys requires an object as argument.");
}
var arr = [];
var hasOwnProperty = Object.prototype.hasOwnProperty;
for (var key in map) {
if (hasOwnProperty.call(map, key)) {
arr.push(key);
}
}
return arr;
}
})[
typeof(Object.keys) == "function" ? "ES5" :
(function() {for (var key in {toString : 1}) { return key }})() !== "toString" ? "BROKEN_IE" : "default"
],
/**
* Mapping from JavaScript string representation of objects to names
* @internal
* @type {Map}
*/
__classToTypeMap :
{
"[object String]": "String",
"[object Array]": "Array",
"[object Object]": "Object",
"[object RegExp]": "RegExp",
"[object Number]": "Number",
"[object Boolean]": "Boolean",
"[object Date]": "Date",
"[object Function]": "Function",
"[object Error]": "Error",
"[object Blob]": "Blob",
"[object ArrayBuffer]": "ArrayBuffer",
"[object FormData]": "FormData"
},
/*
---------------------------------------------------------------------------
FUNCTION UTILITY FUNCTIONS
---------------------------------------------------------------------------
*/
/**
* Returns a function whose "this" is altered.
*
* *Syntax*
*
* <pre class='javascript'>qx.Bootstrap.bind(myFunction, [self, [varargs...]]);</pre>
*
* *Example*
*
* <pre class='javascript'>
* function myFunction()
* {
* this.setStyle('color', 'red');
* // note that 'this' here refers to myFunction, not an element
* // we'll need to bind this function to the element we want to alter
* };
*
* var myBoundFunction = qx.Bootstrap.bind(myFunction, myElement);
* myBoundFunction(); // this will make the element myElement red.
* </pre>
*
* @param func {Function} Original function to wrap
* @param self {Object ? null} The object that the "this" of the function will refer to.
* @param varargs {arguments ? null} The arguments to pass to the function.
* @return {Function} The bound function.
*/
bind : function(func, self, varargs)
{
var fixedArgs = Array.prototype.slice.call(arguments, 2, arguments.length);
return function() {
var args = Array.prototype.slice.call(arguments, 0, arguments.length);
return func.apply(self, fixedArgs.concat(args));
};
},
/*
---------------------------------------------------------------------------
STRING UTILITY FUNCTIONS
---------------------------------------------------------------------------
*/
/**
* Convert the first character of the string to upper case.
*
* @param str {String} the string
* @return {String} the string with an upper case first character
*/
firstUp : function(str) {
return str.charAt(0).toUpperCase() + str.substr(1);
},
/**
* Convert the first character of the string to lower case.
*
* @param str {String} the string
* @return {String} the string with a lower case first character
*/
firstLow : function(str) {
return str.charAt(0).toLowerCase() + str.substr(1);
},
/*
---------------------------------------------------------------------------
TYPE UTILITY FUNCTIONS
---------------------------------------------------------------------------
*/
/**
* Get the internal class of the value. See
* http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
* for details.
*
* @param value {var} value to get the class for
* @return {String} the internal class of the value
*/
getClass : function(value)
{
// The typeof null and undefined is "object" under IE8
if(value === undefined) {
return "Undefined"
}else if(value === null) {
return "Null";
}
var classString = Object.prototype.toString.call(value);
return (
qx.Bootstrap.__classToTypeMap[classString] ||
classString.slice(8, -1)
);
},
/**
* Whether the value is a string.
*
* @param value {var} Value to check.
* @return {Boolean} Whether the value is a string.
*/
isString : function(value)
{
// Added "value !== null" because IE throws an exception "Object expected"
// by executing "value instanceof String" if value is a DOM element that
// doesn't exist. It seems that there is an internal difference between a
// JavaScript null and a null returned from calling DOM.
// e.q. by document.getElementById("ReturnedNull").
return (
value !== null && (
typeof value === "string" ||
qx.Bootstrap.getClass(value) == "String" ||
value instanceof String ||
(!!value && !!value.$$isString))
);
},
/**
* Whether the value is an array.
*
* @param value {var} Value to check.
* @return {Boolean} Whether the value is an array.
*/
isArray : function(value)
{
// Added "value !== null" because IE throws an exception "Object expected"
// by executing "value instanceof Array" if value is a DOM element that
// doesn't exist. It seems that there is an internal difference between a
// JavaScript null and a null returned from calling DOM.
// e.q. by document.getElementById("ReturnedNull").
return (
value !== null && (
value instanceof Array ||
(value && qx.data && qx.data.IListData && qx.util.OOUtil.hasInterface(value.constructor, qx.data.IListData) ) ||
qx.Bootstrap.getClass(value) == "Array" ||
(!!value && !!value.$$isArray))
);
},
/**
* Whether the value is an object. Note that built-in types like Window are
* not reported to be objects.
*
* @param value {var} Value to check.
* @return {Boolean} Whether the value is an object.
*/
isObject : function(value) {
return (
value !== undefined &&
value !== null &&
qx.Bootstrap.getClass(value) == "Object"
);
},
/**
* Whether the value is a function.
*
* @param value {var} Value to check.
* @return {Boolean} Whether the value is a function.
*/
isFunction : function(value) {
// @ITG:Wisej: Speed improvement.
// return qx.Bootstrap.getClass(value) == "Function";
return value instanceof Function;
},
/*
---------------------------------------------------------------------------
LOGGING UTILITY FUNCTIONS
---------------------------------------------------------------------------
*/
$$logs : [],
/**
* Sending a message at level "debug" to the logger.
*
* @param object {Object} Contextual object (either instance or static class)
* @param message {var} Any number of arguments supported. An argument may
* have any JavaScript data type. All data is serialized immediately and
* does not keep references to other objects.
*/
debug : function(object, message) {
qx.Bootstrap.$$logs.push(["debug", arguments]);
},
/**
* Sending a message at level "info" to the logger.
*
* @param object {Object} Contextual object (either instance or static class)
* @param message {var} Any number of arguments supported. An argument may
* have any JavaScript data type. All data is serialized immediately and
* does not keep references to other objects.
*/
info : function(object, message) {
qx.Bootstrap.$$logs.push(["info", arguments]);
},
/**
* Sending a message at level "warn" to the logger.
*
* @param object {Object} Contextual object (either instance or static class)
* @param message {var} Any number of arguments supported. An argument may
* have any JavaScript data type. All data is serialized immediately and
* does not keep references to other objects.
*/
warn : function(object, message) {
qx.Bootstrap.$$logs.push(["warn", arguments]);
},
/**
* Sending a message at level "error" to the logger.
*
* @param object {Object} Contextual object (either instance or static class)
* @param message {var} Any number of arguments supported. An argument may
* have any JavaScript data type. All data is serialized immediately and
* does not keep references to other objects.
*/
error : function(object, message) {
qx.Bootstrap.$$logs.push(["error", arguments]);
},
/**
* Prints the current stack trace at level "info"
*
* @param object {Object} Contextual object (either instance or static class)
*/
trace : function(object) {}
}
});