Gregory Hill
2006-Jun-15 20:26 UTC
RE: Yahoo!-like Eventobjectemulation/abstractioninPrototype?
This is obviously rough draft and not thoroughly tested, but it seems to work. If you''d like, give it a try (load it after you load prototype.js): Function.prototype.bindAsEventListener = function(object) { var __method = this; return function(event) { return __method.call(object, new SuperEvent(event || window.event)); } } var SuperEvent = Class.create(); Object.extend(SuperEvent.prototype, { initialize: function (event) { for (attr in event) { this[attr] = event[attr]; } this.target = Event.element(event); this.srcElement = Event.element(event); this.which = event.which || event.button; this.button = event.button || event.which; this.pageX = Event.pointerX(event); this.pageY = Event.pointerY(event); this.clientX = this.pageX - (document.documentElement.scrollLeft || document.body.scrollLeft); this.clientY = this.pageY - (document.documentElement.scrollTop || document.body.scrollTop); this.preventDefault = Event.stop.bind(Event, event); this.stopPropagation = Event.stop.bind(Event, event); } } ); There are probably more things that would need to be addressed; I just based this off of what functionality was in prototype ''cause I believe most of the remaining attributes/functions work cross-browser already (and should be inherited in the for attr in event loop). This is obviously less efficient since it gathers data for every bound event, despite whether you use it or not. But, it will let you call IE-specific event functions in Firefox, so long as you still bound the events using the bindAsEventListener function. Greg
Andrew Tetlaw
2006-Jun-16 02:05 UTC
Re: Yahoo!-like Eventobjectemulation/abstractioninPrototype?
A comprehensive blast from the past that I used to use a lot: http://web.archive.org/web/20031002180514/dithered.com/javascript/dom2_events/dom2_events.js On 16/06/06, Gregory Hill <Gregory_Hill-l9nu40+TWxQ@public.gmane.org> wrote:> This is obviously rough draft and not thoroughly tested, but it seems to > work. If you''d like, give it a try (load it after you load > prototype.js): > > > Function.prototype.bindAsEventListener = function(object) { > var __method = this; > return function(event) { > return __method.call(object, new SuperEvent(event || window.event)); > } > } > > var SuperEvent = Class.create(); > Object.extend(SuperEvent.prototype, { > initialize: function (event) { > for (attr in event) { > this[attr] = event[attr]; > } > this.target = Event.element(event); > this.srcElement = Event.element(event); > this.which = event.which || event.button; > this.button = event.button || event.which; > this.pageX = Event.pointerX(event); > this.pageY = Event.pointerY(event); > this.clientX = this.pageX - (document.documentElement.scrollLeft > || document.body.scrollLeft); > this.clientY = this.pageY - (document.documentElement.scrollTop > || document.body.scrollTop); > this.preventDefault = Event.stop.bind(Event, event); > this.stopPropagation = Event.stop.bind(Event, event); > } > } ); > > There are probably more things that would need to be addressed; I just > based this off of what functionality was in prototype ''cause I believe > most of the remaining attributes/functions work cross-browser already > (and should be inherited in the for attr in event loop). > > This is obviously less efficient since it gathers data for every bound > event, despite whether you use it or not. But, it will let you call > IE-specific event functions in Firefox, so long as you still bound the > events using the bindAsEventListener function. > > Greg > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >-- Andrew Tetlaw htp://tetlaw.id.au
Ryan Gahl
2006-Jun-16 02:30 UTC
Re: Yahoo!-like Eventobjectemulation/abstractioninPrototype?
Greg, that''s a good start... but yea, I personally don''t really see the overall utility of such a thing compared to just using the proto abstract statics. On 6/15/06, Gregory Hill <Gregory_Hill-l9nu40+TWxQ@public.gmane.org> wrote:> > This is obviously rough draft and not thoroughly tested, but it seems to > work. If you''d like, give it a try (load it after you load > prototype.js): > > > Function.prototype.bindAsEventListener = function(object) { > var __method = this; > return function(event) { > return __method.call(object, new SuperEvent(event || window.event)); > } > } > > var SuperEvent = Class.create(); > Object.extend(SuperEvent.prototype, { > initialize: function (event) { > for (attr in event) { > this[attr] = event[attr]; > } > this.target = Event.element(event); > this.srcElement = Event.element(event); > this.which = event.which || event.button; > this.button = event.button || event.which; > this.pageX = Event.pointerX(event); > this.pageY = Event.pointerY(event); > this.clientX = this.pageX - (document.documentElement.scrollLeft > || document.body.scrollLeft); > this.clientY = this.pageY - (document.documentElement.scrollTop > || document.body.scrollTop); > this.preventDefault = Event.stop.bind(Event, event); > this.stopPropagation = Event.stop.bind(Event, event); > } > } ); > > There are probably more things that would need to be addressed; I just > based this off of what functionality was in prototype ''cause I believe > most of the remaining attributes/functions work cross-browser already > (and should be inherited in the for attr in event loop). > > This is obviously less efficient since it gathers data for every bound > event, despite whether you use it or not. But, it will let you call > IE-specific event functions in Firefox, so long as you still bound the > events using the bindAsEventListener function. > > Greg > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Sam Rowe
2006-Jun-16 14:47 UTC
Re: Yahoo!-like Eventobjectemulation/abstractioninPrototype?
On Thu, Jun 15, 2006 at 02:26:37PM -0600, Gregory Hill wrote: # This is obviously rough draft and not thoroughly tested, but it seems to # work. If you''d like, give it a try (load it after you load # prototype.js): # # # Function.prototype.bindAsEventListener = function(object) { # var __method = this; # return function(event) { # return __method.call(object, new SuperEvent(event || window.event)); # } # } # # var SuperEvent = Class.create(); # Object.extend(SuperEvent.prototype, { # initialize: function (event) { # for (attr in event) { # this[attr] = event[attr]; # } # this.target = Event.element(event); # this.srcElement = Event.element(event); # this.which = event.which || event.button; # this.button = event.button || event.which; # this.pageX = Event.pointerX(event); # this.pageY = Event.pointerY(event); # this.clientX = this.pageX - (document.documentElement.scrollLeft # || document.body.scrollLeft); # this.clientY = this.pageY - (document.documentElement.scrollTop # || document.body.scrollTop); # this.preventDefault = Event.stop.bind(Event, event); # this.stopPropagation = Event.stop.bind(Event, event); # } # } ); # # There are probably more things that would need to be addressed; I just # based this off of what functionality was in prototype ''cause I believe # most of the remaining attributes/functions work cross-browser already # (and should be inherited in the for attr in event loop). # # This is obviously less efficient since it gathers data for every bound # event, despite whether you use it or not. But, it will let you call # IE-specific event functions in Firefox, so long as you still bound the # events using the bindAsEventListener function. Seems to work great! Can I use that code in my project? My eventual hope is to release the project under the GPLv2. Thanks! -Sam