-
Notifications
You must be signed in to change notification settings - Fork 1
/
declare.js
1224 lines (1158 loc) · 36.3 KB
/
declare.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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Module systems magic dance
(function (definition) {
// RequireJS
if (typeof define === "function") {
define(definition);
// YUI3
} else if (typeof YUI === "function") {
YUI.add("declare", definition);
// CommonJS and <script>
} else {
definition();
}
})( function( ){
// module:
// dojo/_base/declare
var global = this,
_extraNames = [ "hasOwnProperty", "valueOf", "isPrototypeOf", "propertyIsEnumerable", "toLocaleString", "toString", "constructor" ],
bug_for_in_skips_shadowed = (function( ){
// if true, the for-in iterator skips object properties that exist in Object's prototype (IE 6 - ?)
for(var i in {toString: 1}){
return false;
}
return true;
})( );
function _mix(dest, source, copyFunc){
// summary:
// Copies/adds all properties of source to dest; returns dest.
// dest: Object
// The object to which to copy/add all properties contained in source.
// source: Object
// The object from which to draw all properties to copy into dest.
// copyFunc: Function?
// The process used to copy/add a property in source; defaults to the Javascript assignment operator.
// returns:
// dest, as modified
// description:
// All properties, including functions (sometimes termed "methods"), excluding any non-standard extensions
// found in Object.prototype, are copied/added to dest. Copying/adding each particular property is
// delegated to copyFunc (if any); copyFunc defaults to the Javascript assignment operator if not provided.
// Notice that by default, _mixin executes a so-called "shallow copy" and aggregate types are copied/added by reference.
var name, s, i, empty = {};
for(name in source){
// the (!(name in empty) || empty[name] !== s) condition avoids copying properties in "source"
// inherited from Object.prototype. For example, if dest has a custom toString() method,
// don't overwrite it with the toString() method that source inherited from Object.prototype
s = source[name];
if(!(name in dest) || (dest[name] !== s && (!(name in empty) || empty[name] !== s))){
dest[name] = copyFunc ? copyFunc(s) : s;
}
}
if(bug_for_in_skips_shadowed){
if(source){
for(i = 0; i < _extraLen; ++i){
name = _extraNames[i];
s = source[name];
if(!(name in dest) || (dest[name] !== s && (!(name in empty) || empty[name] !== s))){
dest[name] = copyFunc ? copyFunc(s) : s;
}
}
}
}
return dest; // Object
}
function mix( destination, sources ){
// summary:
// Copies/adds all properties of one or more sources to dest; returns dest.
// dest: Object
// The object to which to copy/add all properties contained in source. If dest is falsy, then
// a new object is manufactured before copying/adding properties begins.
// sources: Object...
// One of more objects from which to draw all properties to copy into dest. sources are processed
// left-to-right and if more than one of these objects contain the same property name, the right-most
// value "wins".
// returns: Object
// dest, as modified
// description:
// All properties, including functions (sometimes termed "methods"), excluding any non-standard extensions
// found in Object.prototype, are copied/added from sources to dest. sources are processed left to right.
// The Javascript assignment operator is used to copy/add each property; therefore, by default, mixin
// executes a so-called "shallow copy" and aggregate types are copied/added by reference.
// example:
// make a shallow copy of an object
// | var copy = lang.mixin({}, source);
// example:
// many class constructors often take an object which specifies
// values to be configured on the object. In this case, it is
// often simplest to call `lang.mixin` on the `this` object:
// | declare("acme.Base", null, {
// | constructor: function(properties){
// | // property configuration:
// | lang.mixin(this, properties);
// |
// | console.log(this.quip);
// | // ...
// | },
// | quip: "I wasn't born yesterday, you know - I've seen movies.",
// | // ...
// | });
// |
// | // create an instance of the class and configure it
// | var b = new acme.Base({quip: "That's what it does!" });
// example:
// copy in properties from multiple objects
// | var flattened = lang.mixin(
// | {
// | name: "Frylock",
// | braces: true
// | },
// | {
// | name: "Carl Brutanananadilewski"
// | }
// | );
// |
// | // will print "Carl Brutanananadilewski"
// | console.log(flattened.name);
// | // will print "true"
// | console.log(flattened.braces);
if(!dest){ dest = {}; }
for(var i = 1, l = arguments.length; i < l; i++){
_mix(dest, arguments[i]);
}
return dest; // Object
}
function getProp(/*Array*/parts, /*Boolean*/create, /*Object*/context){
var p, i = 0;
if(!context){
if(!parts.length){
return global;
}else{
p = parts[i++];
context = context || (p in global ? global[p] : (create ? global[p] = {} : undefined));
}
}
while(context && (p = parts[i++])){
context = (p in context ? context[p] : (create ? context[p] = {} : undefined));
}
return context; // mixed
}
function setObject(name, value, context){
// summary:
// Set a property from a dot-separated string, such as "A.B.C"
// description:
// Useful for longer api chains where you have to test each object in
// the chain, or when you have an object reference in string format.
// Objects are created as needed along `path`. Returns the passed
// value if setting is successful or `undefined` if not.
// name: String
// Path to a property, in the form "A.B.C".
// value: anything
// value or object to place at location given by name
// context: Object?
// Optional. Object to use as root of path. Defaults to
// `dojo.global`.
// example:
// set the value of `foo.bar.baz`, regardless of whether
// intermediate objects already exist:
// | lang.setObject("foo.bar.baz", value);
// example:
// without `lang.setObject`, we often see code like this:
// | // ensure that intermediate objects are available
// | if(!obj["parent"]){ obj.parent = {}; }
// | if(!obj.parent["child"]){ obj.parent.child = {}; }
// | // now we can safely set the property
// | obj.parent.child.prop = "some value";
// whereas with `lang.setObject`, we can shorten that to:
// | lang.setObject("parent.child.prop", "some value", obj);
var parts = name.split("."), p = parts.pop(), obj = getProp(parts, true, context);
return obj && p ? (obj[p] = value) : undefined; // Object
}
var op = Object.prototype,
opts = op.toString,
xtor = new Function,
counter = 0,
cname = "constructor";
function err(msg, cls){ throw new Error("declare" + (cls ? " " + cls : "") + ": " + msg); }
// C3 Method Resolution Order (see http://www.python.org/download/releases/2.3/mro/)
function c3mro(bases, className){
var result = [], roots = [{cls: 0, refs: []}], nameMap = {}, clsCount = 1,
l = bases.length, i = 0, j, lin, base, top, proto, rec, name, refs;
// build a list of bases naming them if needed
for(; i < l; ++i){
base = bases[i];
if(!base){
err("mixin #" + i + " is unknown. Did you use dojo.require to pull it in?", className);
}else if(opts.call(base) != "[object Function]"){
err("mixin #" + i + " is not a callable constructor.", className);
}
lin = base._meta ? base._meta.bases : [base];
top = 0;
// add bases to the name map
for(j = lin.length - 1; j >= 0; --j){
proto = lin[j].prototype;
if(!proto.hasOwnProperty("declaredClass")){
proto.declaredClass = "uniqName_" + (counter++);
}
name = proto.declaredClass;
if(!nameMap.hasOwnProperty(name)){
nameMap[name] = {count: 0, refs: [], cls: lin[j]};
++clsCount;
}
rec = nameMap[name];
if(top && top !== rec){
rec.refs.push(top);
++top.count;
}
top = rec;
}
++top.count;
roots[0].refs.push(top);
}
// remove classes without external references recursively
while(roots.length){
top = roots.pop();
result.push(top.cls);
--clsCount;
// optimization: follow a single-linked chain
while(refs = top.refs, refs.length == 1){
top = refs[0];
if(!top || --top.count){
// branch or end of chain => do not end to roots
top = 0;
break;
}
result.push(top.cls);
--clsCount;
}
if(top){
// branch
for(i = 0, l = refs.length; i < l; ++i){
top = refs[i];
if(!--top.count){
roots.push(top);
}
}
}
}
if(clsCount){
err("can't build consistent linearization", className);
}
// calculate the superclass offset
base = bases[0];
result[0] = base ?
base._meta && base === result[result.length - base._meta.bases.length] ?
base._meta.bases.length : 1 : 0;
return result;
}
function inherited(args, a, f){
var name, chains, bases, caller, meta, base, proto, opf, pos,
cache = this._inherited = this._inherited || {};
// crack arguments
if(typeof args == "string"){
name = args;
args = a;
a = f;
}
f = 0;
caller = args.callee;
name = name || caller.nom;
if(!name){
err("can't deduce a name to call inherited()", this.declaredClass);
}
meta = this.constructor._meta;
bases = meta.bases;
pos = cache.p;
if(name != cname){
// method
if(cache.c !== caller){
// cache bust
pos = 0;
base = bases[0];
meta = base._meta;
if(meta.hidden[name] !== caller){
// error detection
chains = meta.chains;
if(chains && typeof chains[name] == "string"){
err("calling chained method with inherited: " + name, this.declaredClass);
}
// find caller
do{
meta = base._meta;
proto = base.prototype;
if(meta && (proto[name] === caller && proto.hasOwnProperty(name) || meta.hidden[name] === caller)){
break;
}
}while(base = bases[++pos]); // intentional assignment
pos = base ? pos : -1;
}
}
// find next
base = bases[++pos];
if(base){
proto = base.prototype;
if(base._meta && proto.hasOwnProperty(name)){
f = proto[name];
}else{
opf = op[name];
do{
proto = base.prototype;
f = proto[name];
if(f && (base._meta ? proto.hasOwnProperty(name) : f !== opf)){
break;
}
}while(base = bases[++pos]); // intentional assignment
}
}
f = base && f || op[name];
}else{
// constructor
if(cache.c !== caller){
// cache bust
pos = 0;
meta = bases[0]._meta;
if(meta && meta.ctor !== caller){
// error detection
chains = meta.chains;
if(!chains || chains.constructor !== "manual"){
err("calling chained constructor with inherited", this.declaredClass);
}
// find caller
while(base = bases[++pos]){ // intentional assignment
meta = base._meta;
if(meta && meta.ctor === caller){
break;
}
}
pos = base ? pos : -1;
}
}
// find next
while(base = bases[++pos]){ // intentional assignment
meta = base._meta;
f = meta ? meta.ctor : base;
if(f){
break;
}
}
f = base && f;
}
// cache the found super method
cache.c = f;
cache.p = pos;
// now we have the result
if(f){
return a === true ? f : f.apply(this, a || args);
}
// intentionally no return if a super method was not found
}
function getInherited(name, args){
if(typeof name == "string"){
return this.__inherited(name, args, true);
}
return this.__inherited(name, true);
}
function inherited__debug(args, a1, a2){
var f = this.getInherited(args, a1);
if(f){ return f.apply(this, a2 || a1 || args); }
// intentionally no return if a super method was not found
}
//var inheritedImpl = dojo.config.isDebug ? inherited__debug : inherited;
var inheritedImpl = inherited;
// emulation of "instanceof"
function isInstanceOf(cls){
var bases = this.constructor._meta.bases;
for(var i = 0, l = bases.length; i < l; ++i){
if(bases[i] === cls){
return true;
}
}
return this instanceof cls;
}
function mixOwn(target, source){
// add props adding metadata for incoming functions skipping a constructor
for(var name in source){
if(name != cname && source.hasOwnProperty(name)){
target[name] = source[name];
}
}
if(bug_for_in_skips_shadowed){
for(var extraNames= _extraNames, i= extraNames.length; i;){
name = extraNames[--i];
if(name != cname && source.hasOwnProperty(name)){
target[name] = source[name];
}
}
}
}
// implementation of safe mixin function
function safeMixin(target, source){
// summary:
// Mix in properties skipping a constructor and decorating functions
// like it is done by declare().
// target: Object
// Target object to accept new properties.
// source: Object
// Source object for new properties.
// description:
// This function is used to mix in properties like lang.mixin does,
// but it skips a constructor property and decorates functions like
// declare() does.
//
// It is meant to be used with classes and objects produced with
// declare. Functions mixed in with dojo.safeMixin can use
// this.inherited() like normal methods.
//
// This function is used to implement extend() method of a constructor
// produced with declare().
//
// example:
// | var A = declare(null, {
// | m1: function(){
// | console.log("A.m1");
// | },
// | m2: function(){
// | console.log("A.m2");
// | }
// | });
// | var B = declare(A, {
// | m1: function(){
// | this.inherited(arguments);
// | console.log("B.m1");
// | }
// | });
// | B.extend({
// | m2: function(){
// | this.inherited(arguments);
// | console.log("B.m2");
// | }
// | });
// | var x = new B();
// | dojo.safeMixin(x, {
// | m1: function(){
// | this.inherited(arguments);
// | console.log("X.m1");
// | },
// | m2: function(){
// | this.inherited(arguments);
// | console.log("X.m2");
// | }
// | });
// | x.m2();
// | // prints:
// | // A.m1
// | // B.m1
// | // X.m1
var name, t;
// add props adding metadata for incoming functions skipping a constructor
for(name in source){
t = source[name];
if((t !== op[name] || !(name in op)) && name != cname){
if(opts.call(t) == "[object Function]"){
// non-trivial function method => attach its name
t.nom = name;
}
target[name] = t;
}
}
if(has("bug-for-in-skips-shadowed")){
for(var extraNames= _extraNames, i= extraNames.length; i;){
name = extraNames[--i];
t = source[name];
if((t !== op[name] || !(name in op)) && name != cname){
if(opts.call(t) == "[object Function]"){
// non-trivial function method => attach its name
t.nom = name;
}
target[name] = t;
}
}
}
return target;
}
function extend(source){
declare.safeMixin(this.prototype, source);
return this;
}
function createSubclass(mixins){
return declare([this].concat(mixins));
}
// chained constructor compatible with the legacy declare()
function chainedConstructor(bases, ctorSpecial){
return function(){
var a = arguments, args = a, a0 = a[0], f, i, m,
l = bases.length, preArgs;
if(!(this instanceof a.callee)){
// not called via new, so force it
return applyNew(a);
}
//this._inherited = {};
// perform the shaman's rituals of the original declare()
// 1) call two types of the preamble
if(ctorSpecial && (a0 && a0.preamble || this.preamble)){
// full blown ritual
preArgs = new Array(bases.length);
// prepare parameters
preArgs[0] = a;
for(i = 0;;){
// process the preamble of the 1st argument
a0 = a[0];
if(a0){
f = a0.preamble;
if(f){
a = f.apply(this, a) || a;
}
}
// process the preamble of this class
f = bases[i].prototype;
f = f.hasOwnProperty("preamble") && f.preamble;
if(f){
a = f.apply(this, a) || a;
}
// one peculiarity of the preamble:
// it is called if it is not needed,
// e.g., there is no constructor to call
// let's watch for the last constructor
// (see ticket #9795)
if(++i == l){
break;
}
preArgs[i] = a;
}
}
// 2) call all non-trivial constructors using prepared arguments
for(i = l - 1; i >= 0; --i){
f = bases[i];
m = f._meta;
f = m ? m.ctor : f;
if(f){
f.apply(this, preArgs ? preArgs[i] : a);
}
}
// 3) continue the original ritual: call the postscript
f = this.postscript;
if(f){
f.apply(this, args);
}
};
}
// chained constructor compatible with the legacy declare()
function singleConstructor(ctor, ctorSpecial){
return function(){
var a = arguments, t = a, a0 = a[0], f;
if(!(this instanceof a.callee)){
// not called via new, so force it
return applyNew(a);
}
//this._inherited = {};
// perform the shaman's rituals of the original declare()
// 1) call two types of the preamble
if(ctorSpecial){
// full blown ritual
if(a0){
// process the preamble of the 1st argument
f = a0.preamble;
if(f){
t = f.apply(this, t) || t;
}
}
f = this.preamble;
if(f){
// process the preamble of this class
f.apply(this, t);
// one peculiarity of the preamble:
// it is called even if it is not needed,
// e.g., there is no constructor to call
// let's watch for the last constructor
// (see ticket #9795)
}
}
// 2) call a constructor
if(ctor){
ctor.apply(this, a);
}
// 3) continue the original ritual: call the postscript
f = this.postscript;
if(f){
f.apply(this, a);
}
};
}
// plain vanilla constructor (can use inherited() to call its base constructor)
function simpleConstructor(bases){
return function(){
var a = arguments, i = 0, f, m;
if(!(this instanceof a.callee)){
// not called via new, so force it
return applyNew(a);
}
//this._inherited = {};
// perform the shaman's rituals of the original declare()
// 1) do not call the preamble
// 2) call the top constructor (it can use this.inherited())
for(; f = bases[i]; ++i){ // intentional assignment
m = f._meta;
f = m ? m.ctor : f;
if(f){
f.apply(this, a);
break;
}
}
// 3) call the postscript
f = this.postscript;
if(f){
f.apply(this, a);
}
};
}
function chain(name, bases, reversed){
return function(){
var b, m, f, i = 0, step = 1;
if(reversed){
i = bases.length - 1;
step = -1;
}
for(; b = bases[i]; i += step){ // intentional assignment
m = b._meta;
f = (m ? m.hidden : b.prototype)[name];
if(f){
f.apply(this, arguments);
}
}
};
}
// forceNew(ctor)
// return a new object that inherits from ctor.prototype but
// without actually running ctor on the object.
function forceNew(ctor){
// create object with correct prototype using a do-nothing
// constructor
xtor.prototype = ctor.prototype;
var t = new xtor;
xtor.prototype = null; // clean up
return t;
}
// applyNew(args)
// just like 'new ctor()' except that the constructor and its arguments come
// from args, which must be an array or an arguments object
function applyNew(args){
// create an object with ctor's prototype but without
// calling ctor on it.
var ctor = args.callee, t = forceNew(ctor);
// execute the real constructor on the new object
ctor.apply(t, args);
return t;
}
function declare(className, superclass, props){
// summary:
// Create a feature-rich constructor from compact notation.
// className: String?
// The optional name of the constructor (loosely, a "class")
// stored in the "declaredClass" property in the created prototype.
// It will be used as a global name for a created constructor.
// superclass: Function|Function[]
// May be null, a Function, or an Array of Functions. This argument
// specifies a list of bases (the left-most one is the most deepest
// base).
// props: Object
// An object whose properties are copied to the created prototype.
// Add an instance-initialization function by making it a property
// named "constructor".
// returns: dojo/_base/declare.__DeclareCreatedObject
// New constructor function.
// description:
// Create a constructor using a compact notation for inheritance and
// prototype extension.
//
// Mixin ancestors provide a type of multiple inheritance.
// Prototypes of mixin ancestors are copied to the new class:
// changes to mixin prototypes will not affect classes to which
// they have been mixed in.
//
// Ancestors can be compound classes created by this version of
// declare(). In complex cases all base classes are going to be
// linearized according to C3 MRO algorithm
// (see http://www.python.org/download/releases/2.3/mro/ for more
// details).
//
// "className" is cached in "declaredClass" property of the new class,
// if it was supplied. The immediate super class will be cached in
// "superclass" property of the new class.
//
// Methods in "props" will be copied and modified: "nom" property
// (the declared name of the method) will be added to all copied
// functions to help identify them for the internal machinery. Be
// very careful, while reusing methods: if you use the same
// function under different names, it can produce errors in some
// cases.
//
// It is possible to use constructors created "manually" (without
// declare()) as bases. They will be called as usual during the
// creation of an instance, their methods will be chained, and even
// called by "this.inherited()".
//
// Special property "-chains-" governs how to chain methods. It is
// a dictionary, which uses method names as keys, and hint strings
// as values. If a hint string is "after", this method will be
// called after methods of its base classes. If a hint string is
// "before", this method will be called before methods of its base
// classes.
//
// If "constructor" is not mentioned in "-chains-" property, it will
// be chained using the legacy mode: using "after" chaining,
// calling preamble() method before each constructor, if available,
// and calling postscript() after all constructors were executed.
// If the hint is "after", it is chained as a regular method, but
// postscript() will be called after the chain of constructors.
// "constructor" cannot be chained "before", but it allows
// a special hint string: "manual", which means that constructors
// are not going to be chained in any way, and programmer will call
// them manually using this.inherited(). In the latter case
// postscript() will be called after the construction.
//
// All chaining hints are "inherited" from base classes and
// potentially can be overridden. Be very careful when overriding
// hints! Make sure that all chained methods can work in a proposed
// manner of chaining.
//
// Once a method was chained, it is impossible to unchain it. The
// only exception is "constructor". You don't need to define a
// method in order to supply a chaining hint.
//
// If a method is chained, it cannot use this.inherited() because
// all other methods in the hierarchy will be called automatically.
//
// Usually constructors and initializers of any kind are chained
// using "after" and destructors of any kind are chained as
// "before". Note that chaining assumes that chained methods do not
// return any value: any returned value will be discarded.
//
// example:
// | declare("my.classes.bar", my.classes.foo, {
// | // properties to be added to the class prototype
// | someValue: 2,
// | // initialization function
// | constructor: function(){
// | this.myComplicatedObject = new ReallyComplicatedObject();
// | },
// | // other functions
// | someMethod: function(){
// | doStuff();
// | }
// | });
//
// example:
// | var MyBase = declare(null, {
// | // constructor, properties, and methods go here
// | // ...
// | });
// | var MyClass1 = declare(MyBase, {
// | // constructor, properties, and methods go here
// | // ...
// | });
// | var MyClass2 = declare(MyBase, {
// | // constructor, properties, and methods go here
// | // ...
// | });
// | var MyDiamond = declare([MyClass1, MyClass2], {
// | // constructor, properties, and methods go here
// | // ...
// | });
//
// example:
// | var F = function(){ console.log("raw constructor"); };
// | F.prototype.method = function(){
// | console.log("raw method");
// | };
// | var A = declare(F, {
// | constructor: function(){
// | console.log("A.constructor");
// | },
// | method: function(){
// | console.log("before calling F.method...");
// | this.inherited(arguments);
// | console.log("...back in A");
// | }
// | });
// | new A().method();
// | // will print:
// | // raw constructor
// | // A.constructor
// | // before calling F.method...
// | // raw method
// | // ...back in A
//
// example:
// | var A = declare(null, {
// | "-chains-": {
// | destroy: "before"
// | }
// | });
// | var B = declare(A, {
// | constructor: function(){
// | console.log("B.constructor");
// | },
// | destroy: function(){
// | console.log("B.destroy");
// | }
// | });
// | var C = declare(B, {
// | constructor: function(){
// | console.log("C.constructor");
// | },
// | destroy: function(){
// | console.log("C.destroy");
// | }
// | });
// | new C().destroy();
// | // prints:
// | // B.constructor
// | // C.constructor
// | // C.destroy
// | // B.destroy
//
// example:
// | var A = declare(null, {
// | "-chains-": {
// | constructor: "manual"
// | }
// | });
// | var B = declare(A, {
// | constructor: function(){
// | // ...
// | // call the base constructor with new parameters
// | this.inherited(arguments, [1, 2, 3]);
// | // ...
// | }
// | });
//
// example:
// | var A = declare(null, {
// | "-chains-": {
// | m1: "before"
// | },
// | m1: function(){
// | console.log("A.m1");
// | },
// | m2: function(){
// | console.log("A.m2");
// | }
// | });
// | var B = declare(A, {
// | "-chains-": {
// | m2: "after"
// | },
// | m1: function(){
// | console.log("B.m1");
// | },
// | m2: function(){
// | console.log("B.m2");
// | }
// | });
// | var x = new B();
// | x.m1();
// | // prints:
// | // B.m1
// | // A.m1
// | x.m2();
// | // prints:
// | // A.m2
// | // B.m2
// crack parameters
if(typeof className != "string"){
props = superclass;
superclass = className;
className = "";
}
props = props || {};
var proto, i, t, ctor, name, bases, chains, mixins = 1, parents = superclass;
// build a prototype
if(opts.call(superclass) == "[object Array]"){
// C3 MRO
bases = c3mro(superclass, className);
t = bases[0];
mixins = bases.length - t;
superclass = bases[mixins];
}else{
bases = [0];
if(superclass){
if(opts.call(superclass) == "[object Function]"){
t = superclass._meta;
bases = bases.concat(t ? t.bases : superclass);
}else{
err("base class is not a callable constructor.", className);
}
}else if(superclass !== null){
err("unknown base class. Did you use dojo.require to pull it in?", className);
}
}
if(superclass){
for(i = mixins - 1;; --i){
proto = forceNew(superclass);
if(!i){
// stop if nothing to add (the last base)
break;
}
// mix in properties
t = bases[i];
(t._meta ? mixOwn : mix)(proto, t.prototype);
// chain in new constructor
ctor = new Function;
ctor.superclass = superclass;
ctor.prototype = proto;
superclass = proto.constructor = ctor;
}
}else{
proto = {};
}
// add all properties
declare.safeMixin(proto, props);
// add constructor
t = props.constructor;
if(t !== op.constructor){
t.nom = cname;
proto.constructor = t;
}
// collect chains and flags
for(i = mixins - 1; i; --i){ // intentional assignment
t = bases[i]._meta;
if(t && t.chains){
chains = mix(chains || {}, t.chains);
}
}
if(proto["-chains-"]){
chains = mix(chains || {}, proto["-chains-"]);
}
// build ctor
t = !chains || !chains.hasOwnProperty(cname);
bases[0] = ctor = (chains && chains.constructor === "manual") ? simpleConstructor(bases) :
(bases.length == 1 ? singleConstructor(props.constructor, t) : chainedConstructor(bases, t));
// add meta information to the constructor
ctor._meta = {bases: bases, hidden: props, chains: chains,
parents: parents, ctor: props.constructor};
ctor.superclass = superclass && superclass.prototype;
ctor.extend = extend;
ctor.createSubclass = createSubclass;
ctor.prototype = proto;
proto.constructor = ctor;
// add "standard" methods to the prototype
proto.getInherited = getInherited;
proto.isInstanceOf = isInstanceOf;
proto.inherited = inheritedImpl;
proto.__inherited = inherited;
// add name if specified