Changeset 7029
- Timestamp:
- 06/15/07 20:46:37 (1 year ago)
- Files:
-
- spinoffs/prototype/branches/event/src/event.js (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
spinoffs/prototype/branches/event/src/event.js
r7001 r7029 63 63 64 64 Object.extend(Observer.prototype, { 65 initialize: function(element, type, observer, useCapture) {65 initialize: function(element, type, callback, useCapture) { 66 66 this.element = $(element); 67 this.type = this._type = type; 68 this.callback = this._callback = callback; 69 this.useCapture = useCapture || false; 70 67 71 switch(type) { 68 72 case 'keypress': 69 if (B.IE || B.WebKit) t ype = 'keydown';73 if (B.IE || B.WebKit) this._type = 'keydown'; 70 74 break; 71 75 case 'mouseenter': 72 76 case 'mouseleave': 73 77 if (B.IE) break; 74 type = type == 'mouseenter' ? 'mouseover' : 'mouseout' 75 var originalObserver = this.originalObserver = observer; 76 observer = function(event) { 78 this._type = type == 'mouseenter' ? 'mouseover' : 'mouseout'; 79 this._callback = function(event) { 77 80 var rel = event.relatedTarget, cur = event.currentTarget; 78 81 rel = rel.nodeType == Node.TEXT_NODE ? rel.parentNode : rel; 79 82 if (rel && rel !== cur && !rel.descendantOf(cur)) 80 return originalObserver(event);83 return callback(event); 81 84 }; 82 85 } 83 this.type = type;84 this.observer = observer;85 this.useCapture = useCapture || false;86 86 }, 87 87 add: function() { 88 88 if (this.type == 'DOMContentLoaded' && this.element == document) { 89 Event.onReady(this. observer);89 Event.onReady(this.callback); 90 90 return; 91 91 } … … 97 97 var local = this.element._observers[this.type]; 98 98 if (!local) local = this.element._observers[this.type] = {}; 99 if (!this. observer.$$guid) this.observer.$$guid = Observer.guid++;100 local[this. observer.$$guid] = this;99 if (!this.callback.$$guid) this.callback.$$guid = Observer.guid++; 100 local[this.callback.$$guid] = this; 101 101 }, 102 102 remove: function() { 103 if (!this.observer) {104 if (this.element._observers) {105 var collection = this.element._observers;106 if (this.type) {107 collection = collection[this.type]108 if (!collection) return;109 }110 for (var i in collection) {111 if (i > 0) collection[i].remove();112 else for (var j in collection[i]) collection[i][j].remove();113 }114 }115 return;116 }117 if (!this.observer.$$guid) return;118 103 this._remove(); 119 delete this.element._observers[this.type][this. observer.$$guid];104 delete this.element._observers[this.type][this.callback.$$guid]; 120 105 } 121 106 }); … … 157 142 Object.extend(Observer.prototype, { 158 143 _add: function() { 159 this.element.addEventListener(this. type, this.observer, this.useCapture);144 this.element.addEventListener(this._type, this._callback, this.useCapture); 160 145 }, 161 146 _remove: function() { 162 this.element.removeEventListener(this. type, this.observer, this.useCapture);147 this.element.removeEventListener(this._type, this._callback, this.useCapture); 163 148 } 164 149 }); … … 171 156 _add: function() { 172 157 // create a wrapper for scope correction and event object normalization 173 var ob = this. observer, el = this.element, klass = this.constructor;158 var ob = this._callback, el = this.element, klass = this.constructor; 174 159 this.wrapper = function(e) { return ob.call(el, klass.extendEvent(e, el)) }; 175 this.element.attachEvent('on' + this. type, this.wrapper);160 this.element.attachEvent('on' + this._type, this.wrapper); 176 161 this.constructor.globalCache.push(this); 177 162 }, 178 163 _remove: function() { 179 if (!this.wrapper) this.wrapper = this.element._observers[this.type][this. observer.$$guid].wrapper;180 this.element.detachEvent('on' + this. type, this.wrapper);164 if (!this.wrapper) this.wrapper = this.element._observers[this.type][this.callback.$$guid].wrapper; 165 this.element.detachEvent('on' + this._type, this.wrapper); 181 166 } 182 167 }); … … 232 217 233 218 return { 234 observe: function(element, type, observer, useCapture) {219 observe: function(element, type, callback, useCapture) { 235 220 if (applyToCollection(arguments)) return; 236 new Observer(element, type, observer, useCapture).add(); 237 }, 238 stopObserving: function(element, type, observer, useCapture) { 221 new Observer(element, type, callback, useCapture).add(); 222 }, 223 224 stopObserving: function(element, type, callback, useCapture) { 225 useCapture = useCapture || false; 239 226 if (applyToCollection(arguments)) return; 240 if (!Prototype.Browser.IE && (type == 'mouseenter' || type == 'mouseleave')) { 241 type = type == 'mouseenter' ? 'mouseover' : 'mouseout'; 242 var cachedObserver = $H(element._observers[type]).find(function(obs) { 243 return obs.value.originalObserver === observer; 244 }); 245 if(cachedObserver) observer = cachedObserver.observer; 246 } 247 new Observer(element, type, observer, useCapture).remove(); 227 var collection = element._observers; 228 if (!collection) return; 229 230 if (!callback) { 231 if (type) { 232 collection = collection[type] 233 if (!collection) return; 234 } 235 for (var i in collection) { 236 if (i > 0) collection[i].remove(); 237 else for (var j in collection[i]) collection[i][j].remove(); 238 } 239 return; 240 } 241 collection = collection[type]; 242 if (!collection || !callback.$$guid) return; 243 var observer = collection[callback.$$guid]; 244 if(observer && observer.useCapture === useCapture) 245 observer.remove(); 248 246 }, 249 247