1 /**
  2  * @fileOverview attribute management and event in one
  3  * @author  yiminghe@gmail.com,lifesinger@gmail.com
  4  */
  5 KISSY.add('base', function (S, Attribute, Event) {
  6 
  7     /**
  8      * @name Base
  9      * @extends Event.Target
 10      * @extends Attribute
 11      * @class <p>
 12      * A base class which objects requiring attributes and custom event support can
 13      * extend. attributes configured
 14      * through the static {@link Base.ATTRS} property for each class
 15      * in the hierarchy will be initialized by Base.
 16      * </p>
 17      */
 18     function Base(config) {
 19         var self = this,
 20             c = self.constructor;
 21         // define
 22         while (c) {
 23             addAttrs(self, c['ATTRS']);
 24             c = c.superclass ? c.superclass.constructor : null;
 25         }
 26         // initial
 27         initAttrs(self, config);
 28     }
 29 
 30 
 31     /**
 32      * The default set of attributes which will be available for instances of this class, and
 33      * their configuration
 34      *
 35      * By default if the value is an object literal or an array it will be "shallow" cloned, to
 36      * protect the default value.
 37      *
 38      * @name Base.ATTRS
 39      * @type Object
 40      */
 41 
 42 
 43     /**
 44      * see {@link Attribute#set}
 45      * @name set
 46      * @memberOf Base#
 47      * @function
 48      */
 49 
 50 
 51     function addAttrs(host, attrs) {
 52         if (attrs) {
 53             for (var attr in attrs) {
 54                 // 子类上的 ATTRS 配置优先
 55                 if (attrs.hasOwnProperty(attr)) {
 56                     // 父类后加,父类不覆盖子类的相同设置
 57                     // 属性对象会 merge   a: {y:{getter:fn}}, b:{y:{value:3}}, b extends a => b {y:{value:3}}
 58                     host.addAttr(attr, attrs[attr], false);
 59                 }
 60             }
 61         }
 62     }
 63 
 64     function initAttrs(host, config) {
 65         if (config) {
 66             for (var attr in config) {
 67                 if (config.hasOwnProperty(attr)) {
 68                     // 用户设置会调用 setter/validator 的,但不会触发属性变化事件
 69                     host.__set(attr, config[attr]);
 70                 }
 71 
 72             }
 73         }
 74     }
 75 
 76     S.augment(Base, Event.Target, Attribute);
 77 
 78     Base.Attribute = Attribute;
 79 
 80     S.Base = Base;
 81 
 82     return Base;
 83 }, {
 84     requires:["base/attribute", "event"]
 85 });
 86