-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore-debug.js
1682 lines (1314 loc) · 43.2 KB
/
core-debug.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
// Class
// -----------------
// Thanks to:
// - http://mootools.net/docs/core/Class/Class
// - http://ejohn.org/blog/simple-javascript-inheritance/
// - https://github.com/ded/klass
// - http://documentcloud.github.com/backbone/#Model-extend
// - https://github.com/joyent/node/blob/master/lib/util.js
// - https://github.com/kissyteam/kissy/blob/master/src/seed/src/kissy.js
var Hui = Hui || {};
(function() {
'use strict';
// The base Class implementation.
function Class(o) {
// Convert existed function to Class.
if (!(this instanceof Class) && isFunction(o)) {
return classify(o)
}
}
Hui.Class = Class;
// Create a new Class.
//
// var SuperPig = Class.create({
// Extends: Animal,
// Implements: Flyable,
// initialize: function() {
// SuperPig.superclass.initialize.apply(this, arguments)
// },
// Statics: {
// COLOR: 'red'
// }
// })
//
Class.create = function(parent, properties) {
if (!isFunction(parent)) {
properties = parent
parent = null
}
properties || (properties = {})
parent || (parent = properties.Extends || Class)
properties.Extends = parent
// The created class constructor
function SubClass() {
// Call the parent constructor.
parent.apply(this, arguments)
// Only call initialize in self constructor.
if (this.constructor === SubClass && this.initialize) {
this.initialize.apply(this, arguments)
}
}
// Inherit class (static) properties from parent.
if (parent !== Class) {
mix(SubClass, parent, parent.StaticsWhiteList)
}
// Add instance properties to the subclass.
implement.call(SubClass, properties)
// Make subclass extendable.
return classify(SubClass)
}
function implement(properties) {
var key, value
for (key in properties) {
value = properties[key]
if (Class.Mutators.hasOwnProperty(key)) {
Class.Mutators[key].call(this, value)
} else {
this.prototype[key] = value
}
}
}
// Create a sub Class based on `Class`.
Class.extend = function(properties) {
properties || (properties = {})
properties.Extends = this
return Class.create(properties)
}
function classify(cls) {
cls.extend = Class.extend
cls.implement = implement
return cls
}
// Mutators define special properties.
Class.Mutators = {
'Extends': function(parent) {
var existed = this.prototype
var proto = createProto(parent.prototype)
// Keep existed properties.
mix(proto, existed)
// Enforce the constructor to be what we expect.
proto.constructor = this
// Set the prototype chain to inherit from `parent`.
this.prototype = proto
// Set a convenience property in case the parent's prototype is
// needed later.
this.superclass = parent.prototype
},
'Implements': function(items) {
isArray(items) || (items = [items])
var proto = this.prototype, item
while (item = items.shift()) {
mix(proto, item.prototype || item)
}
},
'Statics': function(staticProperties) {
mix(this, staticProperties)
}
}
// Shared empty constructor function to aid in prototype-chain creation.
function Ctor() {
}
// See: http://jsperf.com/object-create-vs-new-ctor
var createProto = Object.__proto__ ?
function(proto) {
return { __proto__: proto }
} :
function(proto) {
Ctor.prototype = proto
return new Ctor()
}
// Helpers
// ------------
function mix(r, s, wl) {
// Copy "all" properties including inherited ones.
for (var p in s) {
if (s.hasOwnProperty(p)) {
if (wl && indexOf(wl, p) === -1) continue
// 在 iPhone 1 代等设备的 Safari 中,prototype 也会被枚举出来,需排除
if (p !== 'prototype') {
r[p] = s[p]
}
}
}
}
var toString = Object.prototype.toString
var isArray = Array.isArray || function(val) {
return toString.call(val) === '[object Array]'
}
var isFunction = function(val) {
return toString.call(val) === '[object Function]'
}
var indexOf = Array.prototype.indexOf ?
function(arr, item) {
return arr.indexOf(item)
} :
function(arr, item) {
for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i] === item) {
return i
}
}
return -1
}
})()
// Events
// -----------------
// Thanks to:
// - https://github.com/documentcloud/backbone/blob/master/backbone.js
// - https://github.com/joyent/node/blob/master/lib/events.js
var Hui = Hui || {};
(function() {
'use strict';
// Regular expression used to split event strings
var eventSplitter = /\s+/
// A module that can be mixed in to *any object* in order to provide it
// with custom events. You may bind with `on` or remove with `off` callback
// functions to an event; `trigger`-ing an event fires all callbacks in
// succession.
//
// var object = new Events();
// object.on('expand', function(){ alert('expanded'); });
// object.trigger('expand');
//
function Events() {
}
// Bind one or more space separated events, `events`, to a `callback`
// function. Passing `"all"` will bind the callback to all events fired.
Events.prototype.on = function(events, callback, context) {
var cache, event, list
if (!callback) return this
cache = this.__events || (this.__events = {})
events = events.split(eventSplitter)
while (event = events.shift()) {
list = cache[event] || (cache[event] = [])
list.push(callback, context)
}
return this
}
Events.prototype.once = function(events, callback, context) {
var that = this
var cb = function() {
that.off(events, cb)
callback.apply(context || that, arguments)
}
return this.on(events, cb, context)
}
// Remove one or many callbacks. If `context` is null, removes all callbacks
// with that function. If `callback` is null, removes all callbacks for the
// event. If `events` is null, removes all bound callbacks for all events.
Events.prototype.off = function(events, callback, context) {
var cache, event, list, i
// No events, or removing *all* events.
if (!(cache = this.__events)) return this
if (!(events || callback || context)) {
delete this.__events
return this
}
events = events ? events.split(eventSplitter) : keys(cache)
// Loop through the callback list, splicing where appropriate.
while (event = events.shift()) {
list = cache[event]
if (!list) continue
if (!(callback || context)) {
delete cache[event]
continue
}
for (i = list.length - 2; i >= 0; i -= 2) {
if (!(callback && list[i] !== callback ||
context && list[i + 1] !== context)) {
list.splice(i, 2)
}
}
}
return this
}
// Trigger one or many events, firing all bound callbacks. Callbacks are
// passed the same arguments as `trigger` is, apart from the event name
// (unless you're listening on `"all"`, which will cause your callback to
// receive the true name of the event as the first argument).
Events.prototype.trigger = function(events) {
var cache, event, all, list, i, len, rest = [], args, returned = true;
if (!(cache = this.__events)) return this
events = events.split(eventSplitter)
// Fill up `rest` with the callback arguments. Since we're only copying
// the tail of `arguments`, a loop is much faster than Array#slice.
for (i = 1, len = arguments.length; i < len; i++) {
rest[i - 1] = arguments[i]
}
// For each event, walk through the list of callbacks twice, first to
// trigger the event, then to trigger any `"all"` callbacks.
while (event = events.shift()) {
// Copy callback lists to prevent modification.
if (all = cache.all) all = all.slice()
if (list = cache[event]) list = list.slice()
// Execute event callbacks except one named "all"
if (event !== 'all') {
returned = triggerEvents(list, rest, this) && returned
}
// Execute "all" callbacks.
returned = triggerEvents(all, [event].concat(rest), this) && returned
}
return returned
}
Events.prototype.emit = Events.prototype.trigger
// Helpers
// -------
var keys = Object.keys
if (!keys) {
keys = function(o) {
var result = []
for (var name in o) {
if (o.hasOwnProperty(name)) {
result.push(name)
}
}
return result
}
}
// Mix `Events` to object instance or Class function.
Events.mixTo = function(receiver) {
receiver = isFunction(receiver) ? receiver.prototype : receiver
var proto = Events.prototype
var event = new Events
for (var key in proto) {
if (proto.hasOwnProperty(key)) {
copyProto(key)
}
}
function copyProto(key) {
receiver[key] = function() {
proto[key].apply(event, Array.prototype.slice.call(arguments))
return this
}
}
}
// Execute callbacks
function triggerEvents(list, args, context) {
var pass = true
if (list) {
var i = 0, l = list.length, a1 = args[0], a2 = args[1], a3 = args[2]
// call is faster than apply, optimize less than 3 argu
// http://blog.csdn.net/zhengyinhui100/article/details/7837127
switch (args.length) {
case 0: for (; i < l; i += 2) {pass = list[i].call(list[i + 1] || context) !== false && pass} break;
case 1: for (; i < l; i += 2) {pass = list[i].call(list[i + 1] || context, a1) !== false && pass} break;
case 2: for (; i < l; i += 2) {pass = list[i].call(list[i + 1] || context, a1, a2) !== false && pass} break;
case 3: for (; i < l; i += 2) {pass = list[i].call(list[i + 1] || context, a1, a2, a3) !== false && pass} break;
default: for (; i < l; i += 2) {pass = list[i].apply(list[i + 1] || context, args) !== false && pass} break;
}
}
// trigger will return false if one of the callbacks return false
return pass;
}
function isFunction(func) {
return Object.prototype.toString.call(func) === '[object Function]'
}
Hui.Events = Events;
})();
// Aspect
// ---------------------
// Thanks to:
// - http://yuilibrary.com/yui/docs/api/classes/Do.html
// - http://code.google.com/p/jquery-aop/
// - http://lazutkin.com/blog/2008/may/18/aop-aspect-javascript-dojo/
var Hui = Hui || {};
(function() {
'use strict';
Hui.Aspect = {};
// 在指定方法执行前,先执行 callback
Hui.Aspect.before = function(methodName, callback, context) {
return weave.call(this, 'before', methodName, callback, context);
};
// 在指定方法执行后,再执行 callback
Hui.Aspect.after = function(methodName, callback, context) {
return weave.call(this, 'after', methodName, callback, context);
};
// Helpers
// -------
var eventSplitter = /\s+/;
function weave(when, methodName, callback, context) {
var names = methodName.split(eventSplitter);
var name, method;
while (name = names.shift()) {
method = getMethod(this, name);
if (!method.__isAspected) {
wrap.call(this, name);
}
this.on(when + ':' + name, callback, context);
}
return this;
}
function getMethod(host, methodName) {
var method = host[methodName];
if (!method) {
throw new Error('Invalid method name: ' + methodName);
}
return method;
}
function wrap(methodName) {
var old = this[methodName];
this[methodName] = function() {
var args = Array.prototype.slice.call(arguments);
var beforeArgs = ['before:' + methodName].concat(args);
// prevent if trigger return false
if (this.trigger.apply(this, beforeArgs) === false) return;
var ret = old.apply(this, arguments);
var afterArgs = ['after:' + methodName, ret].concat(args);
this.trigger.apply(this, afterArgs);
return ret;
};
this[methodName].__isAspected = true;
}
})()
// Attribute
// -----------------
// Thanks to:
// - http://documentcloud.github.com/backbone/#Model
// - http://yuilibrary.com/yui/docs/api/classes/AttributeCore.html
// - https://github.com/berzniz/backbone.getters.setters
var Hui = Hui || {};
(function() {
'use strict';
Hui.Attribute = {};
// 负责 attributes 的初始化
// attributes 是与实例相关的状态信息,可读可写,发生变化时,会自动触发相关事件
Hui.Attribute.initAttrs = function(config) {
// initAttrs 是在初始化时调用的,默认情况下实例上肯定没有 attrs,不存在覆盖问题
var attrs = this.attrs = {};
// Get all inherited attributes.
var specialProps = this.propsInAttrs || [];
mergeInheritedAttrs(attrs, this, specialProps);
// Merge user-specific attributes from config.
if (config) {
mergeUserValue(attrs, config);
}
// 对于有 setter 的属性,要用初始值 set 一下,以保证关联属性也一同初始化
setSetterAttrs(this, attrs, config);
// Convert `on/before/afterXxx` config to event handler.
parseEventsFromAttrs(this, attrs);
// 将 this.attrs 上的 special properties 放回 this 上
copySpecialProps(specialProps, this, attrs, true);
};
// Get the value of an attribute.
Hui.Attribute.get = function(key) {
var attr = this.attrs[key] || {};
var val = attr.value;
return attr.getter ? attr.getter.call(this, val, key) : val;
};
// Set a hash of model attributes on the object, firing `"change"` unless
// you choose to silence it.
Hui.Attribute.set = function(key, val, options) {
var attrs = {};
// set("key", val, options)
if (isString(key)) {
attrs[key] = val;
}
// set({ "key": val, "key2": val2 }, options)
else {
attrs = key;
options = val;
}
options || (options = {});
var silent = options.silent;
var override = options.override;
var now = this.attrs;
var changed = this.__changedAttrs || (this.__changedAttrs = {});
for (key in attrs) {
if (!attrs.hasOwnProperty(key)) continue;
var attr = now[key] || (now[key] = {});
val = attrs[key];
if (attr.readOnly) {
throw new Error('This attribute is readOnly: ' + key);
}
// invoke setter
if (attr.setter) {
val = attr.setter.call(this, val, key);
}
// 获取设置前的 prev 值
var prev = this.get(key);
// 获取需要设置的 val 值
// 如果设置了 override 为 true,表示要强制覆盖,就不去 merge 了
// 都为对象时,做 merge 操作,以保留 prev 上没有覆盖的值
if (!override && isPlainObject(prev) && isPlainObject(val)) {
val = merge(merge({}, prev), val);
}
// set finally
now[key].value = val;
// invoke change event
// 初始化时对 set 的调用,不触发任何事件
if (!this.__initializingAttrs && !isEqual(prev, val)) {
if (silent) {
changed[key] = [val, prev];
}
else {
this.trigger('change:' + key, val, prev, key);
}
}
}
return this;
};
// Call this method to manually fire a `"change"` event for triggering
// a `"change:attribute"` event for each changed attribute.
Hui.Attribute.change = function() {
var changed = this.__changedAttrs;
if (changed) {
for (var key in changed) {
if (changed.hasOwnProperty(key)) {
var args = changed[key];
this.trigger('change:' + key, args[0], args[1], key);
}
}
delete this.__changedAttrs;
}
return this;
};
// for test
Hui.Attribute._isPlainObject = isPlainObject;
// Helpers
// -------
var toString = Object.prototype.toString;
var hasOwn = Object.prototype.hasOwnProperty;
/**
* Detect the JScript [[DontEnum]] bug:
* In IE < 9 an objects own properties, shadowing non-enumerable ones, are
* made non-enumerable as well.
* https://github.com/bestiejs/lodash/blob/7520066fc916e205ef84cb97fbfe630d7c154158/lodash.js#L134-L144
*/
/** Detect if own properties are iterated after inherited properties (IE < 9) */
var iteratesOwnLast;
(function() {
var props = [];
function Ctor() { this.x = 1; }
Ctor.prototype = { 'valueOf': 1, 'y': 1 };
for (var prop in new Ctor()) { props.push(prop); }
iteratesOwnLast = props[0] !== 'x';
}());
var isArray = Array.isArray || function(val) {
return toString.call(val) === '[object Array]';
};
function isString(val) {
return toString.call(val) === '[object String]';
}
function isFunction(val) {
return toString.call(val) === '[object Function]';
}
function isWindow(o) {
return o != null && o == o.window;
}
function isPlainObject(o) {
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor
// property. Make sure that DOM nodes and window objects don't
// pass through, as well
if (!o || toString.call(o) !== "[object Object]" ||
o.nodeType || isWindow(o)) {
return false;
}
try {
// Not own constructor property must be Object
if (o.constructor &&
!hasOwn.call(o, "constructor") &&
!hasOwn.call(o.constructor.prototype, "isPrototypeOf")) {
return false;
}
} catch (e) {
// IE8,9 Will throw exceptions on certain host objects #9897
return false;
}
var key;
// Support: IE<9
// Handle iteration over inherited properties before own properties.
// http://bugs.jquery.com/ticket/12199
if (iteratesOwnLast) {
for (key in o) {
return hasOwn.call(o, key);
}
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
for (key in o) {}
return key === undefined || hasOwn.call(o, key);
}
function isEmptyObject(o) {
if (!o || toString.call(o) !== "[object Object]" ||
o.nodeType || isWindow(o) || !o.hasOwnProperty) {
return false;
}
for (var p in o) {
if (o.hasOwnProperty(p)) return false;
}
return true;
}
function merge(receiver, supplier) {
var key, value;
for (key in supplier) {
if (supplier.hasOwnProperty(key)) {
receiver[key] = cloneValue(supplier[key], receiver[key]);
}
}
return receiver;
}
// 只 clone 数组和 plain object,其他的保持不变
function cloneValue(value, prev){
if (isArray(value)) {
value = value.slice();
}
else if (isPlainObject(value)) {
isPlainObject(prev) || (prev = {});
value = merge(prev, value);
}
return value;
}
var keys = Object.keys;
if (!keys) {
keys = function(o) {
var result = [];
for (var name in o) {
if (o.hasOwnProperty(name)) {
result.push(name);
}
}
return result;
};
}
function mergeInheritedAttrs(attrs, instance, specialProps) {
var inherited = [];
var proto = instance.constructor.prototype;
while (proto) {
// 不要拿到 prototype 上的
if (!proto.hasOwnProperty('attrs')) {
proto.attrs = {};
}
// 将 proto 上的特殊 properties 放到 proto.attrs 上,以便合并
copySpecialProps(specialProps, proto.attrs, proto);
// 为空时不添加
if (!isEmptyObject(proto.attrs)) {
inherited.unshift(proto.attrs);
}
// 向上回溯一级
proto = proto.constructor.superclass;
}
// Merge and clone default values to instance.
for (var i = 0, len = inherited.length; i < len; i++) {
mergeAttrs(attrs, normalize(inherited[i]));
}
}
function mergeUserValue(attrs, config) {
mergeAttrs(attrs, normalize(config, true), true);
}
function copySpecialProps(specialProps, receiver, supplier, isAttr2Prop) {
for (var i = 0, len = specialProps.length; i < len; i++) {
var key = specialProps[i];
if (supplier.hasOwnProperty(key)) {
receiver[key] = isAttr2Prop ? receiver.get(key) : supplier[key];
}
}
}
var EVENT_PATTERN = /^(on|before|after)([A-Z].*)$/;
var EVENT_NAME_PATTERN = /^(Change)?([A-Z])(.*)/;
function parseEventsFromAttrs(host, attrs) {
for (var key in attrs) {
if (attrs.hasOwnProperty(key)) {
var value = attrs[key].value, m;
if (isFunction(value) && (m = key.match(EVENT_PATTERN))) {
host[m[1]](getEventName(m[2]), value);
delete attrs[key];
}
}
}
}
// Converts `Show` to `show` and `ChangeTitle` to `change:title`
function getEventName(name) {
var m = name.match(EVENT_NAME_PATTERN);
var ret = m[1] ? 'change:' : '';
ret += m[2].toLowerCase() + m[3];
return ret;
}
function setSetterAttrs(host, attrs, config) {
var options = { silent: true };
host.__initializingAttrs = true;
for (var key in config) {
if (config.hasOwnProperty(key)) {
if (attrs[key].setter) {
host.set(key, config[key], options);
}
}
}
delete host.__initializingAttrs;
}
var ATTR_SPECIAL_KEYS = ['value', 'getter', 'setter', 'readOnly'];
// normalize `attrs` to
//
// {
// value: 'xx',
// getter: fn,
// setter: fn,
// readOnly: boolean
// }
//
function normalize(attrs, isUserValue) {
var newAttrs = {};
for (var key in attrs) {
var attr = attrs[key];
if (!isUserValue &&
isPlainObject(attr) &&
hasOwnProperties(attr, ATTR_SPECIAL_KEYS)) {
newAttrs[key] = attr;
continue;
}
newAttrs[key] = {
value: attr
};
}
return newAttrs;
}
var ATTR_OPTIONS = ['setter', 'getter', 'readOnly'];
// 专用于 attrs 的 merge 方法
function mergeAttrs(attrs, inheritedAttrs, isUserValue){
var key, value;
var attr;
for (key in inheritedAttrs) {
if (inheritedAttrs.hasOwnProperty(key)) {
value = inheritedAttrs[key];
attr = attrs[key];
if (!attr) {
attr = attrs[key] = {};
}
// 从严谨上来说,遍历 ATTR_SPECIAL_KEYS 更好
// 从性能来说,直接 人肉赋值 更快
// 这里还是选择 性能优先
// 只有 value 要复制原值,其他的直接覆盖即可
(value['value'] !== undefined) && (attr['value'] = cloneValue(value['value'], attr['value']));
// 如果是用户赋值,只要考虑value
if (isUserValue) continue;
for (var i in ATTR_OPTIONS) {
var option = ATTR_OPTIONS[i];
if (value[option] !== undefined) {
attr[option] = value[option];
}
}
}
}
return attrs;
}
function hasOwnProperties(object, properties) {
for (var i = 0, len = properties.length; i < len; i++) {
if (object.hasOwnProperty(properties[i])) {
return true;
}
}
return false;
}
// 对于 attrs 的 value 来说,以下值都认为是空值: null, undefined, '', [], {}
function isEmptyAttrValue(o) {
return o == null || // null, undefined
(isString(o) || isArray(o)) && o.length === 0 || // '', []
isEmptyObject(o); // {}
}
// 判断属性值 a 和 b 是否相等,注意仅适用于属性值的判断,非普适的 === 或 == 判断。
function isEqual(a, b) {
if (a === b) return true;
if (isEmptyAttrValue(a) && isEmptyAttrValue(b)) return true;
// Compare `[[Class]]` names.
var className = toString.call(a);
if (className != toString.call(b)) return false;
switch (className) {
// Strings, numbers, dates, and booleans are compared by value.
case '[object String]':
// Primitives and their corresponding object wrappers are
// equivalent; thus, `"5"` is equivalent to `new String("5")`.
return a == String(b);
case '[object Number]':
// `NaN`s are equivalent, but non-reflexive. An `equal`
// comparison is performed for other numeric values.
return a != +a ? b != +b : (a == 0 ? 1 / a == 1 / b : a == +b);
case '[object Date]':
case '[object Boolean]':
// Coerce dates and booleans to numeric primitive values.
// Dates are compared by their millisecond representations.
// Note that invalid dates with millisecond representations
// of `NaN` are not equivalent.
return +a == +b;
// RegExps are compared by their source patterns and flags.
case '[object RegExp]':
return a.source == b.source &&
a.global == b.global &&
a.multiline == b.multiline &&
a.ignoreCase == b.ignoreCase;
// 简单判断数组包含的 primitive 值是否相等
case '[object Array]':
var aString = a.toString();
var bString = b.toString();
// 只要包含非 primitive 值,为了稳妥起见,都返回 false
return aString.indexOf('[object') === -1 &&
bString.indexOf('[object') === -1 &&
aString === bString;
}
if (typeof a != 'object' || typeof b != 'object') return false;
// 简单判断两个对象是否相等,只判断第一层
if (isPlainObject(a) && isPlainObject(b)) {
// 键值不相等,立刻返回 false
if (!isEqual(keys(a), keys(b))) {
return false;
}
// 键相同,但有值不等,立刻返回 false
for (var p in a) {
if (a[p] !== b[p]) return false;
}
return true;
}
// 其他情况返回 false, 以避免误判导致 change 事件没发生
return false;
}
})();
// Base
// ---------
// Base 是一个基础类,提供 Class、Events、Attrs 和 Aspect 支持。
var Hui = Hui || {};
(function(){
'use strict';
var Class = Hui.Class;
var Events = Hui.Events;
var Aspect = Hui.Aspect;
var Attribute = Hui.Attribute;
Hui.Base = Class.create({
Implements: [Events, Aspect, Attribute],
initialize: function(config) {
this.initAttrs(config);
// Automatically register `this._onChangeAttr` method as
// a `change:attr` event handler.