1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 cc.s_sharedNotificationCenter = null;
 28 
 29 cc.NotificationCenter = cc.Class.extend({
 30     ctor:function() {
 31         this._observers = [];
 32     },
 33 
 34     /**
 35      * @param {cc.Class} target
 36      * @param {String} selector
 37      * @param {String} name
 38      * @param {cc.Class} obj
 39      */
 40     addObserver:function(target, selector, name, obj) {
 41         if (this._observerExisted(target, name))
 42             return;
 43 
 44         var observer = new cc.NotificationObserver(target, selector, name, obj);
 45         if (!observer)
 46             return;
 47 
 48         this._observers.push(observer);
 49     },
 50 
 51     /**
 52      * @param {cc.Class} target
 53      * @param {String} name
 54      */
 55     removeObserver:function(target, name) {
 56         for (var i = 0; i < this._observers.length; i++) {
 57             var observer = this._observers[i];
 58             if (!observer)
 59                 continue;
 60             if (observer.getName() == name && observer.getTarget() == target) {
 61                 this._observers.splice(i, 1);
 62                 return;
 63             }
 64         }
 65     },
 66 
 67     /**
 68      * @param {String} name
 69      * @param {cc.Class} object
 70      */
 71     postNotification:function(name, object) {
 72         for (var i = 0; i < this._observers.length; i++) {
 73             var observer = this._observers[i];
 74             if (!observer)
 75                 continue;
 76             if (observer.getName() == name)
 77                 observer.performSelector(object);
 78         }
 79     },
 80 
 81     /**
 82      * @param {cc.Class} target
 83      * @param {String} name
 84      * @return {Boolean}
 85      * @private
 86      */
 87     _observerExisted:function(target, name) {
 88         var obj = null;
 89         for (var i = 0; i < this._observers.length; i++)
 90         {
 91             var observer = this._observers[i];
 92             if (!observer)
 93                 continue;
 94             if (observer.getName() == name && observer.getTarget() == target)
 95                 return true;
 96         }
 97         return false;
 98     },
 99     _observers:null
100 });
101 
102 /**
103  * @return {cc.NotificationCenter}
104  */
105 cc.NotificationCenter.getInstance = function() {
106     if (!cc.s_sharedNotificationCenter) {
107         cc.s_sharedNotificationCenter = new cc.NotificationCenter();
108     }
109     return cc.s_sharedNotificationCenter;
110 };
111 
112 cc.NotificationObserver = cc.Class.extend({
113 
114     /**
115      * @param {cc.Class} target
116      * @param {String} selector
117      * @param {String} name
118      * @param {cc.Class} obj
119      */
120     ctor:function (target, selector, name, obj) {
121         this._target = target;
122         this._selector = selector;
123         this._name = name;
124         this._obj = obj;
125     },
126 
127     /**
128      * @param {cc.Class} obj
129      */
130     performSelector:function (obj) {
131         if (this._target) {
132             this._target[this._selector](obj);
133         }
134     },
135 
136     _target:null,
137     _selector:null,
138     _name:null,
139     _object:null,
140 
141     /**
142      * @return {cc.Class}
143      */
144     getTarget:function () {
145         return this._target;
146     },
147 
148     /**
149      * @return {String}
150      */
151     getSelector:function () {
152         return this._selector;
153     },
154 
155     /**
156      * @return {String}
157      */
158     getName:function () {
159         return this._name;
160     },
161 
162     /**
163      * @return {cc.Class}
164      */
165     getObject:function () {
166         return this._object;
167     }
168 });