1 /****************************************************************************
  2  Copyright (c) 2011-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 /**
 27  * The component container for Cocostudio, it has some components.
 28  * @class
 29  * @extends cc.Class
 30  */
 31 cc.ComponentContainer = cc.Class.extend(/** @lends cc.ComponentContainer# */{
 32     _components:null,
 33     _owner:null,
 34 
 35     /**
 36      * Construction of cc.ComponentContainer
 37      * @param node
 38      */
 39     ctor:function(node){
 40         this._components = null;
 41         this._owner = node;
 42     },
 43 
 44     /**
 45      * Gets component by name.
 46      * @param name
 47      * @returns {*}
 48      */
 49     getComponent:function(name){
 50         if(!name)
 51             throw "cc.ComponentContainer.getComponent(): name should be non-null";
 52         name = name.trim();
 53         return this._components[name];
 54     },
 55 
 56     /**
 57      * Adds a component to container
 58      * @param {cc.Component} component
 59      * @returns {boolean}
 60      */
 61     add:function(component){
 62         if(!component)
 63              throw "cc.ComponentContainer.add(): component should be non-null";
 64         if(component.getOwner()){
 65             cc.log("cc.ComponentContainer.add(): Component already added. It can't be added again");
 66             return false;
 67         }
 68 
 69         if(this._components == null){
 70             this._components = {};
 71             this._owner.scheduleUpdate();
 72         }
 73         var oldComponent = this._components[component.getName()];
 74         if(oldComponent){
 75             cc.log("cc.ComponentContainer.add(): Component already added. It can't be added again");
 76             return false;
 77         }
 78         component.setOwner(this._owner);
 79         this._components[component.getName()] = component;
 80         component.onEnter();
 81         return true;
 82     },
 83 
 84     /**
 85      * Removes component from container by name or component object.
 86      * @param {String|cc.Component} name component name or component object.
 87      * @returns {boolean}
 88      */
 89     remove:function(name){
 90         if(!name)
 91             throw "cc.ComponentContainer.remove(): name should be non-null";
 92         if(!this._components)
 93             return false;
 94         if(name instanceof cc.Component)
 95             return this._removeByComponent(name);
 96         else {
 97             name = name.trim();
 98             return this._removeByComponent(this._components[name]);
 99         }
100     },
101 
102     _removeByComponent:function(component){
103         if(component)
104             return false;
105         component.onExit();
106         component.setOwner(null);
107         delete this._components[component.getName()];
108         return true;
109     },
110 
111     /**
112      * Removes all components of container.
113      */
114     removeAll:function(){
115         if(!this._components)
116             return;
117         var locComponents = this._components;
118         for(var selKey in locComponents){
119             var selComponent = locComponents[selKey];
120             selComponent.onExit();
121             selComponent.setOwner(null);
122             delete locComponents[selKey];
123         }
124         this._owner.unscheduleUpdate();
125         this._components = null;
126     },
127 
128     _alloc:function(){
129         this._components = {};
130     },
131 
132     /**
133      * Visit callback by director. it calls every frame.
134      * @param {Number} delta
135      */
136     visit:function(delta){
137         if(!this._components)
138             return;
139 
140         var locComponents = this._components;
141         for(var selKey in locComponents)
142              locComponents[selKey].update(delta);
143     },
144 
145     /**
146      * Returns the container whether is empty.
147      * @returns {boolean}
148      */
149     isEmpty: function () {
150         if (!this._components)
151             return true;
152         return this._components.length == 0;
153     }
154 });
155 
156 
157