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