Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/libs/Editor_files/class.js
4627 views
1
/**
2
* gamecore.js - Copyright 2012 Playcraft Labs, Inc. (see licence.txt)
3
* class.js
4
* Classes and objects
5
*/
6
7
/**
8
* @Class
9
* A modified version of class.js to cater to static inheritance and deep object cloning
10
* Based almost completely on class.js (Javascript MVC -- Justin Meyer, Brian Moschel, Michael Mayer and others)
11
* (http://javascriptmvc.com/contribute.html)
12
* Some portions adapted from Prototype JavaScript framework, version 1.6.0.1 (c) 2005-2007 Sam Stephenson
13
* <p>
14
* Class system for javascript
15
* <p>
16
* <code>
17
* var Fighter = gamecore.Base.extend('Fighter',
18
* {
19
* // static (this is inherited as well)
20
* firingSpeed: 1000
21
* },
22
* {
23
* // instance
24
*
25
* hp: 0,
26
* lastFireTime: 0,
27
*
28
* init: function(hp)
29
* {
30
* this.hp = hp;
31
* },
32
*
33
* fire: function()
34
* {
35
* this._super(); // super methods!
36
*
37
* // do firing!
38
* }
39
* });
40
*
41
* var gunship = new Fighter(100);
42
* </code>
43
*
44
* Introspection:
45
* <code>
46
* gamecore.Base.extend(‘Fighter.Gunship’);
47
* Fighter.Gunship.shortName; // ‘Gunship’
48
* Fighter.Gunship.fullName; // ‘Fighter.Gunship’
49
* Fighter.Gunship.namespace; // ‘Fighter’
50
* </code>
51
* <p>
52
* Setup method will be called prior to any init -- nice if you want to do things without needing the
53
* users to call _super in the init, as well as for normalizing parameters.
54
* <code>
55
* setup: function()
56
* {
57
* this.objectId = this.Class.totalObjects++;
58
* this.uniqueId = this.Class.fullName + ':' + this.objectId;
59
* }
60
* </code>
61
*/
62
63
// compatible with jquery classing
64
(function ($)
65
{
66
var regs = {
67
undHash: /_|-/,
68
colons: /::/,
69
words: /([A-Z]+)([A-Z][a-z])/g,
70
lowUp: /([a-z\d])([A-Z])/g,
71
dash: /([a-z\d])([A-Z])/g,
72
replacer: /\{([^\}]+)\}/g,
73
dot: /\./
74
},
75
getNext = function (current, nextPart, add)
76
{
77
return current[nextPart] || ( add && (current[nextPart] = {}) );
78
},
79
isContainer = function (current)
80
{
81
var type = typeof current;
82
return type && ( type == 'function' || type == 'object' );
83
},
84
getObject = function (objectName, roots, add)
85
{
86
var parts = objectName ? objectName.split(regs.dot) : [],
87
length = parts.length,
88
currents = $.isArray(roots) ? roots : [roots || window],
89
current,
90
ret,
91
i,
92
c = 0,
93
type;
94
95
if (length == 0)
96
{
97
return currents[0];
98
}
99
while (current = currents[c++])
100
{
101
for (i = 0; i < length - 1 && isContainer(current); i++)
102
{
103
current = getNext(current, parts[i], add);
104
}
105
if (isContainer(current))
106
{
107
108
ret = getNext(current, parts[i], add);
109
110
if (ret !== undefined)
111
{
112
113
if (add === false)
114
{
115
delete current[parts[i]];
116
}
117
return ret;
118
119
}
120
121
}
122
}
123
},
124
125
/**
126
* @class jQuery.String
127
*
128
* A collection of useful string helpers.
129
*
130
*/
131
str = $.String = $.extend($.String || {}, {
132
/**
133
* @function
134
* Gets an object from a string.
135
* @param {String} name the name of the object to look for
136
* @param {Array} [roots] an array of root objects to look for the name
137
* @param {Boolean} [add] true to add missing objects to
138
* the path. false to remove found properties. undefined to
139
* not modify the root object
140
*/
141
getObject: getObject,
142
/**
143
* Capitalizes a string
144
* @param {String} s the string.
145
* @return {String} a string with the first character capitalized.
146
*/
147
capitalize: function (s, cache)
148
{
149
return s.charAt(0).toUpperCase() + s.substr(1);
150
},
151
/**
152
* Capitalizes a string from something undercored. Examples:
153
* @codestart
154
* jQuery.String.camelize("one_two") //-> "oneTwo"
155
* "three-four".camelize() //-> threeFour
156
* @codeend
157
* @param {String} s
158
* @return {String} a the camelized string
159
*/
160
camelize: function (s)
161
{
162
s = str.classize(s);
163
return s.charAt(0).toLowerCase() + s.substr(1);
164
},
165
/**
166
* Like camelize, but the first part is also capitalized
167
* @param {String} s
168
* @return {String} the classized string
169
*/
170
classize: function (s, join)
171
{
172
var parts = s.split(regs.undHash),
173
i = 0;
174
for (; i < parts.length; i++)
175
{
176
parts[i] = str.capitalize(parts[i]);
177
}
178
179
return parts.join(join || '');
180
},
181
/**
182
* Like [jQuery.String.classize|classize], but a space separates each 'word'
183
* @codestart
184
* jQuery.String.niceName("one_two") //-> "One Two"
185
* @codeend
186
* @param {String} s
187
* @return {String} the niceName
188
*/
189
niceName: function (s)
190
{
191
return str.classize(s, ' ');
192
},
193
194
/**
195
* Underscores a string.
196
* @codestart
197
* jQuery.String.underscore("OneTwo") //-> "one_two"
198
* @codeend
199
* @param {String} s
200
* @return {String} the underscored string
201
*/
202
underscore: function (s)
203
{
204
return s.replace(regs.colons, '/').replace(regs.words, '$1_$2').replace(regs.lowUp, '$1_$2').replace(regs.dash, '_').toLowerCase();
205
},
206
/**
207
* Returns a string with {param} replaced values from data.
208
*
209
* $.String.sub("foo {bar}",{bar: "far"})
210
* //-> "foo far"
211
*
212
* @param {String} s The string to replace
213
* @param {Object} data The data to be used to look for properties. If it's an array, multiple
214
* objects can be used.
215
* @param {Boolean} [remove] if a match is found, remove the property from the object
216
*/
217
sub: function (s, data, remove)
218
{
219
var obs = [];
220
obs.push(s.replace(regs.replacer, function (whole, inside)
221
{
222
//convert inside to type
223
var ob = getObject(inside, data, typeof remove == 'boolean' ? !remove : remove),
224
type = typeof ob;
225
if ((type === 'object' || type === 'function') && type !== null)
226
{
227
obs.push(ob);
228
return "";
229
} else
230
{
231
return "" + ob;
232
}
233
}));
234
return obs.length <= 1 ? obs[0] : obs;
235
}
236
});
237
238
})(jQuery);
239
(function ($)
240
{
241
242
// if we are initializing a new class
243
var initializing = false,
244
makeArray = $.makeArray,
245
isFunction = $.isFunction,
246
isArray = $.isArray,
247
extend = $.extend,
248
249
/**
250
*
251
*/
252
cloneObject = function(object)
253
{
254
if (!object || typeof(object) != 'object')
255
return object;
256
257
// special case handling of array (deep copy them)
258
if (object instanceof Array)
259
{
260
var clone = [];
261
for (var c = 0; c < object.length; c++)
262
clone[c] = cloneObject(object[c]);
263
return clone;
264
}
265
else // otherwise, it's a normal object, clone it's properties
266
{
267
var cloneObj = {};
268
for (var prop in object)
269
cloneObj[prop] = cloneObject(object[prop]);
270
return cloneObj;
271
}
272
},
273
274
concatArgs = function (arr, args)
275
{
276
return arr.concat(makeArray(args));
277
},
278
// tests if we can get super in .toString()
279
fnTest = /xyz/.test(function ()
280
{
281
xyz;
282
}) ? /\b_super\b/ : /.*/,
283
// overwrites an object with methods, sets up _super
284
// newProps - new properties
285
// oldProps - where the old properties might be
286
// addTo - what we are adding to
287
inheritProps = function (newProps, oldProps, addTo)
288
{
289
addTo = addTo || newProps
290
for (var name in newProps)
291
{
292
// Check if we're overwriting an existing function
293
addTo[name] = isFunction(newProps[name]) &&
294
isFunction(oldProps[name]) &&
295
fnTest.test(newProps[name]) ? (function (name, fn)
296
{
297
return function ()
298
{
299
var tmp = this._super, ret;
300
301
// Add a new ._super() method that is the same method but on the super-class
302
this._super = oldProps[name];
303
304
// The method only need to be bound temporarily, so we remove it when we're done executing
305
ret = fn.apply(this, arguments);
306
this._super = tmp;
307
return ret;
308
};
309
})(name, newProps[name]) : newProps[name];
310
}
311
},
312
313
314
/**
315
* @class jQuery.Class
316
* @plugin jquery/class
317
* @tag core
318
* @download dist/jquery/jquery.class.js
319
* @test jquery/class/qunit.html
320
*
321
* Class provides simulated inheritance in JavaScript. Use clss to bridge the gap between
322
* jQuery's functional programming style and Object Oriented Programming. It
323
* is based off John Resig's [http://ejohn.org/blog/simple-javascript-inheritance/|Simple Class]
324
* Inheritance library. Besides prototypal inheritance, it includes a few important features:
325
*
326
* - Static inheritance
327
* - Introspection
328
* - Namespaces
329
* - Setup and initialization methods
330
* - Easy callback function creation
331
*
332
*
333
* ## Static v. Prototype
334
*
335
* Before learning about Class, it's important to
336
* understand the difference between
337
* a class's __static__ and __prototype__ properties.
338
*
339
* //STATIC
340
* MyClass.staticProperty //shared property
341
*
342
* //PROTOTYPE
343
* myclass = new MyClass()
344
* myclass.prototypeMethod() //instance method
345
*
346
* A static (or class) property is on the Class constructor
347
* function itself
348
* and can be thought of being shared by all instances of the
349
* Class. Prototype propertes are available only on instances of the Class.
350
*
351
* ## A Basic Class
352
*
353
* The following creates a Monster class with a
354
* name (for introspection), static, and prototype members.
355
* Every time a monster instance is created, the static
356
* count is incremented.
357
*
358
* @codestart
359
* $.Class.extend('Monster',
360
* /* @static *|
361
* {
362
* count: 0
363
* },
364
* /* @prototype *|
365
* {
366
* init: function( name ) {
367
*
368
* // saves name on the monster instance
369
* this.name = name;
370
*
371
* // sets the health
372
* this.health = 10;
373
*
374
* // increments count
375
* this.Class.count++;
376
* },
377
* eat: function( smallChildren ){
378
* this.health += smallChildren;
379
* },
380
* fight: function() {
381
* this.health -= 2;
382
* }
383
* });
384
*
385
* hydra = new Monster('hydra');
386
*
387
* dragon = new Monster('dragon');
388
*
389
* hydra.name // -> hydra
390
* Monster.count // -> 2
391
* Monster.shortName // -> 'Monster'
392
*
393
* hydra.eat(2); // health = 12
394
*
395
* dragon.fight(); // health = 8
396
*
397
* @codeend
398
*
399
*
400
* Notice that the prototype <b>init</b> function is called when a new instance of Monster is created.
401
*
402
*
403
* ## Inheritance
404
*
405
* When a class is extended, all static and prototype properties are available on the new class.
406
* If you overwrite a function, you can call the base class's function by calling
407
* <code>this._super</code>. Lets create a SeaMonster class. SeaMonsters are less
408
* efficient at eating small children, but more powerful fighters.
409
*
410
*
411
* Monster.extend("SeaMonster",{
412
* eat: function( smallChildren ) {
413
* this._super(smallChildren / 2);
414
* },
415
* fight: function() {
416
* this.health -= 1;
417
* }
418
* });
419
*
420
* lockNess = new SeaMonster('Lock Ness');
421
* lockNess.eat(4); //health = 12
422
* lockNess.fight(); //health = 11
423
*
424
* ### Static property inheritance
425
*
426
* You can also inherit static properties in the same way:
427
*
428
* $.Class.extend("First",
429
* {
430
* staticMethod: function() { return 1;}
431
* },{})
432
*
433
* First.extend("Second",{
434
* staticMethod: function() { return this._super()+1;}
435
* },{})
436
*
437
* Second.staticMethod() // -> 2
438
*
439
* ## Namespaces
440
*
441
* Namespaces are a good idea! We encourage you to namespace all of your code.
442
* It makes it possible to drop your code into another app without problems.
443
* Making a namespaced class is easy:
444
*
445
* @codestart
446
* $.Class.extend("MyNamespace.MyClass",{},{});
447
*
448
* new MyNamespace.MyClass()
449
* @codeend
450
* <h2 id='introspection'>Introspection</h2>
451
* Often, it's nice to create classes whose name helps determine functionality. Ruby on
452
* Rails's [http://api.rubyonrails.org/classes/ActiveRecord/Base.html|ActiveRecord] ORM class
453
* is a great example of this. Unfortunately, JavaScript doesn't have a way of determining
454
* an object's name, so the developer must provide a name. Class fixes this by taking a String name for the class.
455
* @codestart
456
* $.Class.extend("MyOrg.MyClass",{},{})
457
* MyOrg.MyClass.shortName //-> 'MyClass'
458
* MyOrg.MyClass.fullName //-> 'MyOrg.MyClass'
459
* @codeend
460
* The fullName (with namespaces) and the shortName (without namespaces) are added to the Class's
461
* static properties.
462
*
463
*
464
* <h2>Setup and initialization methods</h2>
465
* <p>
466
* Class provides static and prototype initialization functions.
467
* These come in two flavors - setup and init.
468
* Setup is called before init and
469
* can be used to 'normalize' init's arguments.
470
* </p>
471
* <div class='whisper'>PRO TIP: Typically, you don't need setup methods in your classes. Use Init instead.
472
* Reserve setup methods for when you need to do complex pre-processing of your class before init is called.
473
*
474
* </div>
475
* @codestart
476
* $.Class.extend("MyClass",
477
* {
478
* setup: function() {} //static setup
479
* init: function() {} //static constructor
480
* },
481
* {
482
* setup: function() {} //prototype setup
483
* init: function() {} //prototype constructor
484
* })
485
* @codeend
486
*
487
* <h3>Setup</h3>
488
* <p>Setup functions are called before init functions. Static setup functions are passed
489
* the base class followed by arguments passed to the extend function.
490
* Prototype static functions are passed the Class constructor function arguments.</p>
491
* <p>If a setup function returns an array, that array will be used as the arguments
492
* for the following init method. This provides setup functions the ability to normalize
493
* arguments passed to the init constructors. They are also excellent places
494
* to put setup code you want to almost always run.</p>
495
* <p>
496
* The following is similar to how [jQuery.Controller.prototype.setup]
497
* makes sure init is always called with a jQuery element and merged options
498
* even if it is passed a raw
499
* HTMLElement and no second parameter.
500
* </p>
501
* @codestart
502
* $.Class.extend("jQuery.Controller",{
503
* ...
504
* },{
505
* setup: function( el, options ) {
506
* ...
507
* return [$(el),
508
* $.extend(true,
509
* this.Class.defaults,
510
* options || {} ) ]
511
* }
512
* })
513
* @codeend
514
* Typically, you won't need to make or overwrite setup functions.
515
* <h3>Init</h3>
516
*
517
* <p>Init functions are called after setup functions.
518
* Typically, they receive the same arguments
519
* as their preceding setup function. The Foo class's <code>init</code> method
520
* gets called in the following example:
521
* </p>
522
* @codestart
523
* $.Class.Extend("Foo", {
524
* init: function( arg1, arg2, arg3 ) {
525
* this.sum = arg1+arg2+arg3;
526
* }
527
* })
528
* var foo = new Foo(1,2,3);
529
* foo.sum //-> 6
530
* @codeend
531
* <h2>Callbacks</h2>
532
* <p>Similar to jQuery's proxy method, Class provides a
533
* [jQuery.Class.static.callback callback]
534
* function that returns a callback to a method that will always
535
* have
536
* <code>this</code> set to the class or instance of the class.
537
* </p>
538
* The following example uses this.callback to make sure
539
* <code>this.name</code> is available in <code>show</code>.
540
* @codestart
541
* $.Class.extend("Todo",{
542
* init: function( name ) { this.name = name }
543
* get: function() {
544
* $.get("/stuff",this.callback('show'))
545
* },
546
* show: function( txt ) {
547
* alert(this.name+txt)
548
* }
549
* })
550
* new Todo("Trash").get()
551
* @codeend
552
* <p>Callback is available as a static and prototype method.</p>
553
*
554
* <h2>Typing<h2>
555
* Classes are automatically populating with three type related components:
556
*
557
* _types: a variable that contains an array of types of this class (extends history)
558
* _fullTypeName: a string representation of the extends hierarchy
559
* isA(string): a function you can call which will return true if the class is of a given type string.
560
* <p>
561
* Example:
562
* <p>
563
* Animal.extend('Tiger', {}, {});
564
* Tiger._types; // ['Animal', 'Tiger']
565
* Tiger._fullTypeName; // 'Animal | Tiger |"
566
* Tiger.isA('Animal'); // true
567
* </p>
568
* @constructor Creating a new instance of an object that has extended jQuery.Class
569
* calls the init prototype function and returns a new instance of the class.
570
*
571
*/
572
573
clss = $.Class = function ()
574
{
575
if (arguments.length)
576
{
577
return clss.extend.apply(clss, arguments);
578
}
579
};
580
581
/* @Static*/
582
extend(clss, {
583
/**
584
* @function callback
585
* Returns a callback function for a function on this Class.
586
* The callback function ensures that 'this' is set appropriately.
587
* @codestart
588
* $.Class.extend("MyClass",{
589
* getData: function() {
590
* this.showing = null;
591
* $.get("data.json",this.callback('gotData'),'json')
592
* },
593
* gotData: function( data ) {
594
* this.showing = data;
595
* }
596
* },{});
597
* MyClass.showData();
598
* @codeend
599
* <h2>Currying Arguments</h2>
600
* Additional arguments to callback will fill in arguments on the returning function.
601
* @codestart
602
* $.Class.extend("MyClass",{
603
* getData: function( <b>callback</b> ) {
604
* $.get("data.json",this.callback('process',<b>callback</b>),'json');
605
* },
606
* process: function( <b>callback</b>, jsonData ) { //callback is added as first argument
607
* jsonData.processed = true;
608
* callback(jsonData);
609
* }
610
* },{});
611
* MyClass.getData(showDataFunc)
612
* @codeend
613
* <h2>Nesting Functions</h2>
614
* Callback can take an array of functions to call as the first argument. When the returned callback function
615
* is called each function in the array is passed the return value of the prior function. This is often used
616
* to eliminate currying initial arguments.
617
* @codestart
618
* $.Class.extend("MyClass",{
619
* getData: function( callback ) {
620
* //calls process, then callback with value from process
621
* $.get("data.json",this.callback(['process2',callback]),'json')
622
* },
623
* process2: function( type,jsonData ) {
624
* jsonData.processed = true;
625
* return [jsonData];
626
* }
627
* },{});
628
* MyClass.getData(showDataFunc);
629
* @codeend
630
* @param {String|Array} fname If a string, it represents the function to be called.
631
* If it is an array, it will call each function in order and pass the return value of the prior function to the
632
* next function.
633
* @return {Function} the callback function.
634
*/
635
callback: function (funcs)
636
{
637
//args that should be curried
638
var args = makeArray(arguments),
639
self;
640
641
funcs = args.shift();
642
643
if (!isArray(funcs))
644
{
645
funcs = [funcs];
646
}
647
648
self = this;
649
650
return function class_cb()
651
{
652
var cur = concatArgs(args, arguments),
653
isString,
654
length = funcs.length,
655
f = 0,
656
func;
657
658
for (; f < length; f++)
659
{
660
func = funcs[f];
661
if (!func)
662
continue;
663
664
isString = typeof func == "string";
665
if (isString && self._set_called)
666
self.called = func;
667
668
cur = (isString ? self[func] : func).apply(self, cur || []);
669
if (f < length - 1)
670
cur = !isArray(cur) || cur._use_call ? [cur] : cur
671
}
672
return cur;
673
}
674
},
675
/**
676
* @function getObject
677
* Gets an object from a String.
678
* If the object or namespaces the string represent do not
679
* exist it will create them.
680
* @codestart
681
* Foo = {Bar: {Zar: {"Ted"}}}
682
* $.Class.getobject("Foo.Bar.Zar") //-> "Ted"
683
* @codeend
684
* @param {String} objectName the object you want to get
685
* @param {Object} [current=window] the object you want to look in.
686
* @return {Object} the object you are looking for.
687
*/
688
getObject: $.String.getObject,
689
/**
690
* @function newInstance
691
* Creates a new instance of the class. This method is useful for creating new instances
692
* with arbitrary parameters.
693
* <h3>Example</h3>
694
* @codestart
695
* $.Class.extend("MyClass",{},{})
696
* var mc = MyClass.newInstance.apply(null, new Array(parseInt(Math.random()*10,10))
697
* @codeend
698
* @return {class} instance of the class
699
*/
700
newInstance: function ()
701
{
702
var inst = this.rawInstance();
703
var args;
704
705
if (inst.setup)
706
args = inst.setup.apply(inst, arguments);
707
708
// Added by [email protected] -- fix for deep cloning of properties
709
for (var prop in inst.__proto__)
710
inst[prop] = cloneObject(inst[prop]);
711
712
if (inst.init)
713
inst.init.apply(inst, isArray(args) ? args : arguments);
714
715
return inst;
716
},
717
/**
718
* Setup gets called on the inherting class with the base class followed by the
719
* inheriting class's raw properties.
720
*
721
* Setup will deeply extend a static defaults property on the base class with
722
* properties on the base class. For example:
723
*
724
* $.Class("MyBase",{
725
* defaults : {
726
* foo: 'bar'
727
* }
728
* },{})
729
*
730
* MyBase("Inheriting",{
731
* defaults : {
732
* newProp : 'newVal'
733
* }
734
* },{}
735
*
736
* Inheriting.defaults -> {foo: 'bar', 'newProp': 'newVal'}
737
*
738
* @param {Object} baseClass the base class that is being inherited from
739
* @param {String} fullName the name of the new class
740
* @param {Object} staticProps the static properties of the new class
741
* @param {Object} protoProps the prototype properties of the new class
742
*/
743
setup: function (baseClass, fullName)
744
{
745
this.defaults = extend(true, {}, baseClass.defaults, this.defaults);
746
if (this._types == undefined) this._types = [];
747
this._types.push(this.fullName);
748
if (this._fullTypeName == undefined) this._fullTypeName = '|';
749
this._fullTypeName += this.fullName + '|';
750
return arguments;
751
},
752
rawInstance: function ()
753
{
754
initializing = true;
755
var inst = new this();
756
initializing = false;
757
return inst;
758
},
759
/**
760
* Extends a class with new static and prototype functions. There are a variety of ways
761
* to use extend:
762
* @codestart
763
* //with className, static and prototype functions
764
* $.Class.extend('Task',{ STATIC },{ PROTOTYPE })
765
* //with just classname and prototype functions
766
* $.Class.extend('Task',{ PROTOTYPE })
767
* //With just a className
768
* $.Class.extend('Task')
769
* @codeend
770
* @param {String} [fullName] the classes name (used for classes w/ introspection)
771
* @param {Object} [klass] the new classes static/class functions
772
* @param {Object} [proto] the new classes prototype functions
773
* @return {jQuery.Class} returns the new class
774
*/
775
extend: function (fullName, klass, proto)
776
{
777
// figure out what was passed
778
if (typeof fullName != 'string')
779
{
780
proto = klass;
781
klass = fullName;
782
fullName = null;
783
}
784
if (!proto)
785
{
786
proto = klass;
787
klass = null;
788
}
789
790
proto = proto || {};
791
var _super_class = this,
792
_super = this.prototype,
793
name, shortName, namespace, prototype;
794
795
// append the isA function
796
this.isA = function(typeName)
797
{
798
return this._fullTypeName.indexOf('|'+typeName+'|') != -1;
799
};
800
801
// Instantiate a base class (but only create the instance,
802
// don't run the init constructor)
803
initializing = true;
804
prototype = new this();
805
initializing = false;
806
// Copy the properties over onto the new prototype
807
inheritProps(proto, _super, prototype);
808
809
// The dummy class constructor
810
811
function Class()
812
{
813
// All construction is actually done in the init method
814
if (initializing) return;
815
816
if (this.constructor !== Class && arguments.length)
817
{ //we are being called w/o new
818
return arguments.callee.extend.apply(arguments.callee, arguments)
819
} else
820
{ //we are being called w/ new
821
// copy objects
822
823
return this.Class.newInstance.apply(this.Class, arguments)
824
}
825
}
826
827
// Copy old stuff onto class
828
for (name in this)
829
if (this.hasOwnProperty(name))
830
Class[name] = cloneObject(this[name]);
831
832
// copy new props on class
833
inheritProps(klass, this, Class);
834
835
// do namespace stuff
836
if (fullName)
837
{
838
var parts = fullName.split(/\./);
839
var shortName = parts.pop();
840
841
// Martin Wells (playcraft): bug fix. Don't add a namespace object if the class name
842
// has no namespace elements (i.e. it's just "MyClass", not "MyProject.MyClass")
843
if (parts.length > 0)
844
{
845
current = clss.getObject(parts.join('.'), window, true),
846
namespace = current;
847
}
848
849
current[shortName] = Class;
850
}
851
852
// set things that can't be overwritten
853
extend(Class, {
854
prototype: prototype,
855
namespace: namespace,
856
shortName: shortName,
857
constructor: Class,
858
fullName: fullName
859
});
860
861
//make sure our prototype looks nice
862
Class.prototype.Class = Class.prototype.constructor = Class;
863
864
865
/**
866
* @attribute fullName
867
* The full name of the class, including namespace, provided for introspection purposes.
868
* @codestart
869
* $.Class.extend("MyOrg.MyClass",{},{})
870
* MyOrg.MyClass.shortName //-> 'MyClass'
871
* MyOrg.MyClass.fullName //-> 'MyOrg.MyClass'
872
* @codeend
873
*/
874
875
var args = Class.setup.apply(Class, concatArgs([_super_class], arguments));
876
877
if (Class.init)
878
{
879
Class.init.apply(Class, args || []);
880
}
881
882
/* @Prototype*/
883
884
return Class;
885
/**
886
* @function setup
887
* If a setup method is provided, it is called when a new
888
* instances is created. It gets passed the same arguments that
889
* were given to the Class constructor function (<code> new Class( arguments ... )</code>).
890
*
891
* $.Class("MyClass",
892
* {
893
* setup: function( val ) {
894
* this.val = val;
895
* }
896
* })
897
* var mc = new MyClass("Check Check")
898
* mc.val //-> 'Check Check'
899
*
900
* Setup is called before [jQuery.Class.prototype.init init]. If setup
901
* return an array, those arguments will be used for init.
902
*
903
* $.Class("jQuery.Controller",{
904
* setup : function(htmlElement, rawOptions){
905
* return [$(htmlElement),
906
* $.extend({}, this.Class.defaults, rawOptions )]
907
* }
908
* })
909
*
910
* <div class='whisper'>PRO TIP:
911
* Setup functions are used to normalize constructor arguments and provide a place for
912
* setup code that extending classes don't have to remember to call _super to
913
* run.
914
* </div>
915
*
916
* Setup is not defined on $.Class itself, so calling super in inherting classes
917
* will break. Don't do the following:
918
*
919
* $.Class("Thing",{
920
* setup : function(){
921
* this._super(); // breaks!
922
* }
923
* })
924
*
925
* @return {Array|undefined} If an array is return, [jQuery.Class.prototype.init] is
926
* called with those arguments; otherwise, the original arguments are used.
927
*/
928
//break up
929
/**
930
* @function init
931
* If an <code>init</code> method is provided, it gets called when a new instance
932
* is created. Init gets called after [jQuery.Class.prototype.setup setup], typically with the
933
* same arguments passed to the Class
934
* constructor: (<code> new Class( arguments ... )</code>).
935
*
936
* $.Class("MyClass",
937
* {
938
* init: function( val ) {
939
* this.val = val;
940
* }
941
* })
942
* var mc = new MyClass(1)
943
* mc.val //-> 1
944
*
945
* [jQuery.Class.prototype.setup Setup] is able to modify the arguments passed to init. Read
946
* about it there.
947
*
948
*/
949
//Breaks up code
950
/**
951
* @attribute Class
952
* References the static properties of the instance's class.
953
* <h3>Quick Example</h3>
954
* @codestart
955
* // a class with a static classProperty property
956
* $.Class.extend("MyClass", {classProperty : true}, {});
957
*
958
* // a new instance of myClass
959
* var mc1 = new MyClass();
960
*
961
* //
962
* mc1.Class.classProperty = false;
963
*
964
* // creates a new MyClass
965
* var mc2 = new mc.Class();
966
* @codeend
967
* Getting static properties via the Class property, such as it's
968
* [jQuery.Class.static.fullName fullName] is very common.
969
*/
970
}
971
972
})
973
974
975
clss.prototype.
976
/**
977
* @function callback
978
* Returns a callback function. This does the same thing as and is described better in [jQuery.Class.static.callback].
979
* The only difference is this callback works
980
* on a instance instead of a class.
981
* @param {String|Array} fname If a string, it represents the function to be called.
982
* If it is an array, it will call each function in order and pass the return value of the prior function to the
983
* next function.
984
* @return {Function} the callback function
985
*/
986
callback = clss.callback;
987
988
989
})(jQuery);
990