1 /** 2 * @fileOverview storage for component 3 * @author yiminghe@gmail.com 4 */ 5 KISSY.add("component/manager", function (S) { 6 var uis = { 7 // 不带前缀 prefixCls 8 /* 9 "menu" :{ 10 priority:0, 11 constructor:Menu 12 } 13 */ 14 }; 15 16 function getConstructorByXClass(cls) { 17 var cs = cls.split(/\s+/), p = -1, t, ui = null; 18 for (var i = 0; i < cs.length; i++) { 19 var uic = uis[cs[i]]; 20 if (uic && (t = uic.priority) > p) { 21 p = t; 22 ui = uic.constructor; 23 } 24 } 25 return ui; 26 } 27 28 function getXClassByConstructor(constructor) { 29 for (var u in uis) { 30 var ui = uis[u]; 31 if (ui.constructor == constructor) { 32 return u; 33 } 34 } 35 return 0; 36 } 37 38 function setConstructorByXClass(cls, uic) { 39 if (S.isFunction(uic)) { 40 uis[cls] = { 41 constructor:uic, 42 priority:0 43 }; 44 } else { 45 uic.priority = uic.priority || 0; 46 uis[cls] = uic; 47 } 48 } 49 50 51 function getCssClassWithPrefix(cls) { 52 var cs = S.trim(cls).split(/\s+/); 53 for (var i = 0; i < cs.length; i++) { 54 if (cs[i]) { 55 cs[i] = this.get("prefixCls") + cs[i]; 56 } 57 } 58 return cs.join(" "); 59 } 60 61 62 var componentInstances = {}; 63 64 /** 65 * @name Manager 66 * @memberOf Component 67 * @namespace 68 * Manage component metadata. 69 */ 70 var Manager = /** @lends Component.Manager */{ 71 72 __instances:componentInstances, 73 74 addComponent:function (id, component) { 75 componentInstances[id] = component; 76 }, 77 78 removeComponent:function (id) { 79 delete componentInstances[id]; 80 }, 81 82 getComponent:function (id) { 83 return componentInstances[id]; 84 }, 85 86 getCssClassWithPrefix:getCssClassWithPrefix, 87 /** 88 * Get css class name for this component constructor. 89 * @param {Function} constructor Component's constructor. 90 * @type {Function} 91 * @return {String} 92 * @function 93 */ 94 getXClassByConstructor:getXClassByConstructor, 95 /** 96 * Get component constructor by css class name. 97 * @param {String} classNames Class names separated by space. 98 * @type {Function} 99 * @return {Function} 100 * @function 101 */ 102 getConstructorByXClass:getConstructorByXClass, 103 /** 104 * Associate css class with component constructor. 105 * @type {Function} 106 * @param {String} className Component's class name. 107 * @param {Function} componentConstructor Component's constructor. 108 * @function 109 */ 110 setConstructorByXClass:setConstructorByXClass 111 }; 112 113 Manager.getCssClassWithPrefix = getCssClassWithPrefix; 114 115 return Manager; 116 });