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

Changeset 7056

Show
Ignore:
Timestamp:
06/18/07 17:00:15 (1 year ago)
Author:
mislav
Message:

Prototype: mousewheel enhancements, updated TODO, test tweaks. It is no longer relevant whether you use window or document objects to attach the handler to - it works either way. (Mozilla/IE use document while Opera/Safari wants window). Both 'mousewheel' and 'DOMMouseScroll' are accepted as event types, but 'mousewheel' is preffered (for now).

Files:

Legend:

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

    r7045 r7056  
    6464  Object.extend(Observer.prototype, { 
    6565    initialize: function(element, type, callback, useCapture) { 
    66       this.element = $(element); 
     66      // underscored properties are for overriding 
     67      this.element  = this._element  = $(element); 
    6768      this.type     = this._type     = type; 
    6869      this.callback = this._callback = callback; 
     
    7374          if (B.IE || B.WebKit) this._type = 'keydown'; 
    7475          break; 
    75         case 'mouseenter': case 'mouseleave': 
     76        case 'mouseenter': 
     77        case 'mouseleave': 
    7678          if (B.IE) break; 
    7779          this._type = type == 'mouseenter' ? 'mouseover' : 'mouseout'; 
    7880          this._callback = function(event) { 
    7981            var rel = event.relatedTarget, cur = event.currentTarget; 
    80             rel = (rel.nodeType == Node.TEXT_NODE) ? rel.parentNode : rel
    81             if (rel && rel !== cur && !rel.descendantOf(cur)) 
     82            if (rel.nodeType == Node.TEXT_NODE) rel = rel.parentNode
     83            if (rel && rel != cur && !rel.descendantOf(cur)) 
    8284              return callback.call(this, event);  
    8385          }; 
    8486          break; 
    8587        case 'mousewheel': 
     88        case 'DOMMouseScroll': 
     89          if (B.IE || B.Opera) this._type = 'mousewheel'; 
     90          else this._type = 'DOMMouseScroll'; 
     91 
    8692          if (B.IE) { 
    87             // this.element == window ? document : this.element 
     93            if (this.element == window) this._element = document; 
    8894            this._callback = function(event) { 
    8995              callback.call(this, event, event.wheelDelta/120); 
    9096            } 
    9197          } else { 
    92             this._type = 'DOMMouseScroll'; 
     98            if (this.element == document && (B.Opera || B.Safari)) this._element = window; 
     99            // Opera handler is similar to IE's 
    93100            this._callback = B.Opera ? function(event) { 
    94101              callback.call(this, event, -event.wheelDelta/120); 
     
    102109    add: function() { 
    103110      if (this.type == 'DOMContentLoaded' && this.element == document) { 
    104         Event.onReady(this.callback); return; 
     111        Event.onReady(this.callback); 
     112        return; 
    105113      } 
    106114      this._add(); 
     
    155163    Object.extend(Observer.prototype, { 
    156164      _add: function() { 
    157         this.element.addEventListener(this._type, this._callback, this.useCapture); 
     165        if (this._type == 'mousewheel') 
     166          this._element['on' + this._type] = this._callback; 
     167        else 
     168          this._element.addEventListener(this._type, this._callback, this.useCapture); 
    158169      }, 
    159170      _remove: function() { 
    160         this.element.removeEventListener(this._type, this._callback, this.useCapture); 
     171        if (this._type == 'mousewheel') 
     172          this._element['on' + this._type] = null; 
     173        else 
     174          this._element.removeEventListener(this._type, this._callback, this.useCapture); 
    161175      } 
    162176    }); 
    163177     
     178    // extend Event.prototype with our neat instance methods 
    164179    Event.prototype = Event.prototype || document.createEvent("UIEvents").__proto__; 
    165180    Object.extend(Event.prototype, instanceMethods);     
     
    168183      _add: function() { 
    169184        // create a wrapper for scope correction and event object normalization 
    170         var ob = this._callback, el = this.element, klass = this.constructor; 
     185        var ob = this._callback, el = this._element, klass = this.constructor; 
    171186        this.wrapper = function(e) { return ob.call(el, klass.extendEvent(e, el)) }; 
    172187        // attach it 
    173         if (this._type == 'mousewheel') this.element['on' + this._type] = this.wrapper; 
    174         else this.element.attachEvent('on' + this._type, this.wrapper); 
     188        if (this._type == 'mousewheel') this._element['on' + this._type] = this.wrapper; 
     189        else this._element.attachEvent('on' + this._type, this.wrapper); 
    175190        this.constructor.globalCache.push(this); 
    176191      }, 
    177192      _remove: function() { 
    178         if (this._type == 'mousewheel') this.element['on' + this._type] = null; 
     193        if (this._type == 'mousewheel') this._element['on' + this._type] = null; 
    179194        else { 
    180195          if (!this.wrapper)  
    181             this.wrapper = this.element._observers[this.type][this.callback.$$guid].wrapper; 
    182           this.element.detachEvent('on' + this._type, this.wrapper); 
     196            this.wrapper = this._element._observers[this.type][this.callback.$$guid].wrapper; 
     197          this._element.detachEvent('on' + this._type, this.wrapper); 
    183198        } 
    184199      } 
  • spinoffs/prototype/branches/event/test/functional/event.html

    r7045 r7056  
    180180        msg.update('<i>DETACHED</i>').setStyle({ color:'gray' }); 
    181181      }, 
    182       scope: $('wheel') 
     182      scope: document 
    183183    } 
    184184  </script> 
     
    342342  </script> 
    343343   
    344   <p id="custom_events">Custom events test</p> 
     344  <p id="custom_events">Custom events test <b>(incomplete)</b></p> 
    345345   
    346346  <p id="custom_events_target">Lorem ipsum dolor sit amet.</p> 
  • spinoffs/prototype/branches/event/TODO

    r7045 r7056  
    1 * [done] Event handling (IE) fixes and enhancements, see r6194 
    2 * [done] Remove all event listeners on any element 
    3 * [done] event.stop() instead of Event.stop(event) 
    4 * [done] Node constants as per DOM/ECMAScript bindings (r6205) 
    5 * [done] isLeft/Middle/RightClick 
    6 * [done] support for "DOMContentLoaded" 
    7 * [done] Event.findElement should extend the result 
     1* try to prevent wrapper functions from messing with the scope: 
     2  - IE scope correction hack 
     3  - mouseenter/leave 
     4  - mousewheel 
    85* resolve special cases for mousewheel handling: 
    96  - window in IE 
    107  - document in Opera 
     8  - Opera & Safari won't fire the event on objects other than document 
    119* stopImmediatePropagation 
    1210  http://www.w3.org/TR/2003/NOTE-DOM-Level-3-Events-20031107/events.html#Events-Event-stopImmediatePropagation 
     
    1412            https://prototype.campfirenow.com/room/73745/transcript/message/23651995#message_23651995 
    1513* Custom events for adding/removing class names, updating elements, etc. 
     14 
     15 
     16Useful resources 
     17---------------- 
     18 
     19  http://www.quirksmode.org/dom/w3c_events.html 
     20  http://del.icio.us/mislav/events 
     21  http://dev.opera.com/articles/javascript/