forked from sproutit/sproutcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
636 lines (526 loc) · 18.2 KB
/
core.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
// ==========================================================================
// SproutCore
// Author: Charles Jolley
// copyright 2006-2008, Sprout Systems, Inc.
// ==========================================================================
// this is used by the JavascriptCompile class on the server side. You can
// use this to automatically determine the order javascript files need to be
// included in. On the client side, this is a NOP.
var require = require || function require() { } ;
require('license') ;
// ........................................
// GLOBAL CONSTANTS
//
// Most global constants should be defined inside of the SC namespace.
// However the following two are useful enough and generally benign enough
// to put into the global object.
var YES = true ;
var NO = false ;
// prevent a console.log from blowing things up if we are on a browser that
// does not support it
if (typeof console === 'undefined') {
var console = console || window.console || {} ;
console.log = console.info = console.warn = console.error = function(){};
}
// ........................................
// BOOTSTRAP
//
// The root namespace and some common utility methods are defined here. The
// rest of the methods go into the mixin defined below.
/**
@namespace
The SproutCore namespace. All SproutCore methods and functions are defined
inside of this namespace. You generally should not add new properties to
this namespace as it may be overwritten by future versions of SproutCore.
You can also use the shorthand "SC" instead of "SproutCore".
SproutCore-Base is a framework that provides core functions for SproutCore
including cross-platform functions, support for property observing and
objects. It's focus is on small size and performance. You can use this
in place of or along-side other cross-platform libraries such as jQuery or
Prototype.
The core Base framework is based on the jQuery API with a number of
performance optimizations.
*/
var SC = SC || {} ;
var SproutCore = SproutCore || SC ;
/**
Adds properties to a target object.
Takes the root object and adds the attributes for any additional
arguments passed. This can also perform a deep copy if the first param
is a bool that is YES. This is generally not very safe though and not
advised.
@param deep {Boolean} optional parameter. If true, triggers a deep copy.
@param target {Object} the target object to extend
@param properties {Object} one or more objects with properties to copy.
@returns {Object} the target object.
@static
*/
SC.mixin = function() {
// copy reference to target object
var target = arguments[0] || {};
var idx = 1;
var length = arguments.length ;
var deep = NO ;
var options ;
// Handle case where we have only one item...extend SC
if (length === 1) {
target = this || {};
idx=0;
// Handle a deep copy situation
} else if ((target===YES) || (target===NO)) {
deep = target;
target = arguments[1] || {};
idx = 2; // skip the boolean and the target
}
// Handle case when target is a string or something (possible in deep
// copy)
if ( typeof target != "object" && typeof target != "function" ) {
target = {};
}
// extend SC itself if only one argument is passed
if ( length === idx ) {
target = this;
idx = idx-1;
}
for ( ; idx < length; idx++ ) {
if (!(options = arguments[idx])) continue ;
for(var key in options) {
if (!options.hasOwnProperty(key)) continue ;
var src = target[key];
var copy = options[key] ;
if (target===copy) continue ; // prevent never-ending loop
// Recurse if we're merging object values
if ( deep && copy && (typeof copy === "object") && !copy.nodeType ) {
copy = SC.extend(deep,
src || (copy.length != null ? [ ] : { }), copy) ;
}
if (copy !== undefined) target[key] = copy ;
}
}
return target;
} ;
/**
Alternative to mixin. Provided for compatibility with jQuery.
@function
*/
SC.extend = SC.mixin ;
// Enough with the bootstrap code. Let's define some core functions
SC.mixin(/** @scope SC */ {
// ........................................
// GLOBAL CONSTANTS
//
T_ERROR: 'error',
T_OBJECT: 'object',
T_NULL: 'null',
T_CLASS: 'class',
T_HASH: 'hash',
T_FUNCTION: 'function',
T_UNDEFINED: 'undefined',
T_NUMBER: 'number',
T_BOOL: 'boolean',
T_ARRAY: 'array',
T_STRING: 'string',
// ........................................
// CORE HELPER METHODS
//
/**
Creates a clone of the passed object. This function can take just about
any type of object and create a clone of it, including primitive values
(which are not actually cloned because they are immutable).
If the passed object implements the clone() method, then this function
will simply call that method and return the result.
@param object {Object} the object to clone
@returns {Object} the cloned object
*/
clone: function(object) {
var ret = object ;
switch (SC.typeOf(object)) {
case T_ARRAY:
if (object.clone && SC.typeOf(object.clone) === SC.T_FUNCTION) {
ret = object.clone() ;
} else ret = object.slice() ;
break ;
case T_HASH:
case T_OBJECT:
if (object.clone && SC.typeOf(object.clone) === SC.T_FUNCTION) {
ret = object.clone() ;
} else {
ret = {} ;
for(var key in object) ret[key] = object[key] ;
}
}
return ret ;
},
/**
Call this method during setup of your app to queue up methods to be
called once the entire document has finished loading. If you call this
method once the document has already loaded, then the function will be
called immediately.
Any function you register with this method will be called just before
main.
@param target {Object} optional target object. Or just pass a method.
@param method {Function} the method to call.
@return {void}
*/
callOnLoad: function(target, method) {
// normalize parameters
if (method === undefined) { method = target; target = null; }
if (typeof(method) === 'string') {
if (target) {
method = target[method] ;
} else {
throw "You must pass a function to callOnLoad() (got: "+method+")";
}
}
// invoke the method if the queue is flushed.
if (SC._onloadQueueFlushed) method.apply(target || window.document) ;
var queue = SC._onloadQueue = (SC._onloadQueue || []) ;
queue.push([target, method]) ;
},
// To flush the callOnLoad queue, you need to set window.onload=SC.didLoad
didLoad: function() {
SC.app = SC.Application.create();
SC.app.run();
// set the current language
var b = $tag('body');
Element.addClassName(b, String.currentLanguage().toLowerCase()) ;
// call the onloadQueue.
var queue ;
SC.runLoop.beginRunLoop() ;
if (window.callOnLoad) {
if (window.callOnLoad instanceof Array) {
queue = window.callOnLoad ;
} else if (window.callOnLoad instanceof Function) {
queue = [window, window.callOnLoad] ;
}
} else queue = [] ;
queue = queue.concat(SC._onloadQueue) ;
var func = null ;
while(func = queue.shift()) {
if (SC.typeOf(func) === T_FUNCTION) {
func.call(document) ;
} else func[1].call(func[0] || document) ;
}
SC._onloadQueueFlushed = true ;
// start the app; call main.
if (window.main && (main instanceof Function)) main() ; // start app.
// finally handle any routes if any.
if (typeof Routes != 'undefined') {
Routes.doRoutes() ; // old style.
} else if (typeof SC.Routes != 'undefined') {
SC.Routes.ping() ; // handle routes, if modules is installed.
}
SC.runLoop.endRunLoop();
//remove possible IE7 leak
b = null;
queue = null;
func = null;
},
/**
Returns a consistant type for the passed item.
Use this instead of the built-in typeOf() to get the type of an item.
It will return the same result across all browsers and includes a bit
more detail. Here is what will be returned:
| Return Value Constant | Meaning |
| SC.T_STRING | String primitive |
| SC.T_NUMBER | Number primitive |
| SC.T_BOOLEAN | Boolean primitive |
| SC.T_NULL | Null value |
| SC.T_UNDEFINED | Undefined value |
| SC.T_FUNCTION | A function |
| SC.T_ARRAY | An instance of Array |
| SC.T_CLASS | A SproutCore class (created using SC.Object.extend()) |
| SC.T_OBJECT | A SproutCore object instance |
| SC.T_HASH | A JavaScript object not inheriting from SC.Object |
@param item {Object} the item to check
@returns {String} the type
*/
typeOf: function(item) {
if (item === undefined) return T_UNDEFINED ;
if (item === null) return T_NULL ;
var ret = typeof(item) ;
if (ret == "object") {
if (item instanceof Array) {
ret = T_ARRAY ;
} else if (item instanceof Function) {
ret = (item.isClass) ? T_CLASS : T_FUNCTION ;
// NB: typeOf() may be called before SC.Error has had a chance to load
// so this code checks for the presence of SC.Error first just to make
// sure. No error instance can exist before the class loads anyway so
// this is safe.
} else if (SC.Error && (item instanceof SC.Error)) {
ret = T_ERROR ;
} else if (item.isObject === true) {
ret = T_OBJECT ;
} else ret = T_HASH ;
} else if (ret === T_FUNCTION) ret = (item.isClass) ? T_CLASS : T_FUNCTION;
return ret ;
},
/**
Returns YES if the passed object is an array or array-like.
Unlike SC.$type this method returns true even if the passed object is
not formally array but appears to be array-like (i.e. has a length
property, responds to .objectAt, etc.)
@param obj {Object} the object to test
@returns {Boolean}
*/
isArray: function( obj )
{
return ($type(obj) === T_ARRAY) || (obj && ((obj.length!==undefined) || obj.objectAt));
},
/**
Converts the passed object to an Array. If the object appears to be
array-like, a new array will be cloned from it. Otherwise, a new array
will be created with the item itself as the only item in the array.
This is an alias for Array.from() as well.
@param object {Object} any enumerable or array-like object.
@returns {Array} Array of items
*/
$A: function(obj) {
// null or undefined
if (obj == null) return [] ;
// primitive
if (obj.slice instanceof Function) return obj.slice() ;
// enumerable
if (obj.toArray) return obj.toArray() ;
// not array-like
if (obj.length===undefined || $type(obj) === SC.T_FUNCTION) return [obj];
// when all else fails, do a manual convert...
var len = obj.length;
var ret = [] ;
for(var idx=0;idx<len;idx++) ret[idx] = obj[idx];
return ret ;
},
/**
Returns a unique GUID for the object. If the object does not yet have
a guid, one will be assigned to it. You can call this on any object,
SC.Object-based or not, but be aware that it will add a _guid property.
@param obj {Object} any object, string, number or primitive
@returns {String} the unique guid for this instance.
*/
guidFor: function(obj) {
if (obj === undefined) return "(undefined)" ;
if (obj === null) return '(null)' ;
if (obj._guid) return obj._guid ;
switch($type(obj)) {
case T_NUMBER:
return this._numberGuids[obj] = this._numberGuids[obj] || ("#" + obj);
break ;
case T_STRING:
return this._stringGuids[obj] = this._stringGuids[obj] || ("$" + obj);
break ;
case T_BOOL:
return (obj) ? "(true)" : "(false)" ;
break;
default:
return obj._guid = SC.generateGuid();
}
},
generateGuid: function() { return ("@" + (SC._nextGUID++)); },
_nextGUID: 0, _numberGuids: [], _stringGuids: {},
/**
Returns a unique hash code for the object. If the object implements
a hash() method, the value of that method will be returned. Otherwise,
this will return the same value as guidFor().
Unlike guidFor(), this method allows you to implement logic in your
code to cause two separate instances of the same object to be treated as
if they were equal for comparisons and other functions.
IMPORTANT: If you implement a hash() method, it MUST NOT return a number
or a string that contains only a number. Typically hash codes are strings
that begin with a "%".
@param obj {Object} the object
@returns {String} the hash code for this instance.
*/
hashFor: function(obj) {
return (obj && obj.hash && $type(obj.hash) === T_FUNCTION) ? obj.hash() : this.guidFor(obj) ;
},
/**
This will compare the two object values using their hash codes.
@param a {Object} first value to compare
@param b {Object} the second value to compare
@returns {Boolean} YES if the two have equal hash code values.
*/
isEqual: function(a,b) {
// shortcut a few places.
if (a === null) {
return b === null ;
} else if (a === undefined) {
return b === undefined ;
// finally, check their hash-codes
} else return SC.hashFor(a) === SC.hashFor(b) ;
},
/**
Convenience method to inspect an object by converting it to a hash.
*/
inspect: function(obj) {
return $H(obj).inspect() ;
},
/** Browser and Platform info. */
Platform: {
/** The current IE version number or 0 if not IE. */
IE: function() {
if (Prototype.Browser.IE) {
return (navigator.appVersion.match(/\bMSIE.*7\.\b/)) ? 7 : 6 ;
} else return 0 ;
}(),
/** The current Safari major version number of 0 if not Safari */
Safari: function() {
if (Prototype.Browser.WebKit) {
var vers = parseInt(navigator.appVersion.replace(/^.*?AppleWebKit\/(\d+).*?$/,'$1'),0) ;
return (vers > 420) ? 3 : 2 ;
} return 0 ;
}(),
/** The current Firefox major version number or 0 if not Firefox */
Firefox: function() {
var ret = 0;
if (Prototype.Browser.Gecko) {
if(navigator.userAgent.indexOf("Firefox") != -1)
{
ret = parseFloat((navigator.userAgent.match(/Firefox\/(.)/)[1]) || 0);
}
if (ret < 1) ret = 2; // default to version 2 if it is a Gecko browser.
}
return ret ;
}(),
isWindows: function() {
return !!(navigator.appVersion.match(/(Windows)/)) ;
}(),
isMac: function() {
if(Prototype.Browser.Gecko) {
return !!(navigator.appVersion.match(/(Macintosh)/));
} else {
return !!(navigator.appVersion.match(/(Mac OS X)/)) ;
}
}()
},
// DEPRECATED. here for compatibility only.
/** @private */
isIE: function() {
return SC.Platform.IE > 0 ;
},
/** @private */
isSafari: function() {
return SC.Platform.Safari > 0 ;
},
/** @private */
isSafari3: function() {
return SC.Platform.Safari >= 3 ;
},
/** @private */
isIE7: function() {
return SC.Platform.IE >= 7 ;
},
/** @private */
isIE6: function() {
return (SC.Platform.IE >= 6) && (SC.Platform.IE < 7) ;
},
/** @private */
isWindows: function() {
return SC.Platform.isWindows;
},
/** @private */
isMacOSX: function() {
return SC.Platform.isMac ;
},
/** @private */
isFireFox: function() {
return SC.Platform.Firefox > 0 ;
},
/** @private */
isFireFox2: function() {
return SC.Platform.Firefox >= 2 ;
}
});
/** Alias for SC.typeOf() */
SC.$type = SC.typeOf ;
/** @deprecated Use guidFor() instead. */
SC.getGUID = SC.guidFor ;
// Save the Platform.Browser name.
SC.Platform.Browser = function() {
if (SC.Platform.IE >0) {
return 'IE';
} else if (SC.Platform.Safari > 0) {
return 'Safari';
} else if (SC.Platform.Firefox >0) {
return 'Firefox';
}
}() ;
// Export the type variables into the global space.
var T_ERROR = SC.T_ERROR ;
var T_OBJECT = SC.T_OBJECT ;
var T_NULL = SC.T_NULL ;
var T_CLASS = SC.T_CLASS ;
var T_HASH = SC.T_HASH ;
var T_FUNCTION = SC.T_FUNCTION ;
var T_UNDEFINED = SC.T_UNDEFINED ;
var T_NUMBER = SC.T_NUMBER ;
var T_BOOL = SC.T_BOOL ;
var T_ARRAY = SC.T_ARRAY ;
var T_STRING = SC.T_STRING ;
// ........................................
// GLOBAL EXPORTS
//
// Global exports will be made optional in the future so you can avoid
// polluting the global namespace.
$type = SC.typeOf ;
$I = SC.inspect ;
// Legacy. Will retire.
SC.mixin(Object,{
// this will serialize a general JSON object into a URI.
serialize: function(obj) {
var ret = [] ;
for(var key in obj) {
var value = obj[key] ;
if (typeof value == 'number') { value = '' + value ; }
if (!(typeof value == 'string')) { value = value.join(','); }
ret.push(encodeURIComponent(key) + "=" + encodeURIComponent(value)) ;
}
return ret.join('&') ;
}
}) ;
// This will add or remove the class name based on the flag, allowing you to
// treat it like a bool setting. Simplifies the common case where you need
// to make a class name match a bool.
Element.setClassName = function(element,className,flag) {
if(SC.isIE())
{
if (flag) {
Element.addClassName(element,className);
} else {
Element.removeClassName(element,className) ;
}
}
else
{
if (flag) {
element.addClassName(className);
} else {
element.removeClassName(className) ;
}
}
} ;
// ........................................
// EVENT EXTENSIONS
//
Object.extend(Event,{
// get the character code for key pressed events.
getCharCode: function(e) {
return (e.keyCode) ? e.keyCode : ((e.which)?e.which:0) ;
},
// get the pressed char as a string.
getCharString: function(e) {
return String.fromCharCode(Event.getCharCode(e)) ;
},
pointerLocation: function(event) {
var ret = {
x: event.pageX || (event.clientX +
(document.documentElement.scrollLeft || document.body.scrollLeft)),
y: event.pageY || (event.clientY +
(document.documentElement.scrollTop || document.body.scrollTop))
};
return ret ;
},
ALT_KEY: '_ALT',
CTRL_KEY: '_CTRL',
SHIFT_KEY: '_SHIFT'
});