Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7029

Show
Ignore:
Timestamp:
06/15/07 20:46:37 (1 year ago)
Author:
tobie
Message:

Refactoring the stopObserving method. Making it more OO.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spinoffs/prototype/branches/event/src/event.js

    r7001 r7029  
    6363   
    6464  Object.extend(Observer.prototype, { 
    65     initialize: function(element, type, observer, useCapture) { 
     65    initialize: function(element, type, callback, useCapture) { 
    6666      this.element = $(element); 
     67      this.type     = this._type     = type; 
     68      this.callback = this._callback = callback; 
     69      this.useCapture = useCapture || false; 
     70       
    6771      switch(type) { 
    6872        case 'keypress': 
    69           if (B.IE || B.WebKit) type = 'keydown'; 
     73          if (B.IE || B.WebKit) this._type = 'keydown'; 
    7074          break; 
    7175        case 'mouseenter': 
    7276        case 'mouseleave': 
    7377          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) { 
    7780            var rel = event.relatedTarget, cur = event.currentTarget; 
    7881            rel = rel.nodeType == Node.TEXT_NODE ? rel.parentNode : rel; 
    7982            if (rel && rel !== cur && !rel.descendantOf(cur)) 
    80               return originalObserver(event);  
     83              return callback(event);  
    8184          }; 
    8285      } 
    83       this.type = type; 
    84       this.observer = observer; 
    85       this.useCapture = useCapture || false; 
    8686    }, 
    8787    add: function() { 
    8888      if (this.type == 'DOMContentLoaded' && this.element == document) { 
    89         Event.onReady(this.observer); 
     89        Event.onReady(this.callback); 
    9090        return; 
    9191      } 
     
    9797      var local = this.element._observers[this.type]; 
    9898      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; 
    101101    }, 
    102102    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; 
    118103      this._remove(); 
    119       delete this.element._observers[this.type][this.observer.$$guid]; 
     104      delete this.element._observers[this.type][this.callback.$$guid]; 
    120105    } 
    121106  }); 
     
    157142    Object.extend(Observer.prototype, { 
    158143      _add: function() { 
    159         this.element.addEventListener(this.type, this.observer, this.useCapture); 
     144        this.element.addEventListener(this._type, this._callback, this.useCapture); 
    160145      }, 
    161146      _remove: function() { 
    162         this.element.removeEventListener(this.type, this.observer, this.useCapture); 
     147        this.element.removeEventListener(this._type, this._callback, this.useCapture); 
    163148      } 
    164149    }); 
     
    171156      _add: function() { 
    172157        // 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; 
    174159        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); 
    176161        this.constructor.globalCache.push(this); 
    177162      }, 
    178163      _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); 
    181166      } 
    182167    }); 
     
    232217   
    233218  return { 
    234     observe: function(element, type, observer, useCapture) { 
     219    observe: function(element, type, callback, useCapture) { 
    235220      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; 
    239226      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(); 
    248246    }, 
    249247