From: Robert Konigsberg Date: Wed, 18 Apr 2012 16:48:26 +0000 (-0400) Subject: Tweak the addEvent/removeEvent code. Restore the non-object methods, and add a second... X-Git-Tag: v1.0.0~282 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=1cc3540b8c9712df59ec46275e3bfc557a4e5fca;p=dygraphs.git Tweak the addEvent/removeEvent code. Restore the non-object methods, and add a second one just for managing the events associated with a graph. --- diff --git a/dygraph-utils.js b/dygraph-utils.js index e2af0b7..f5440c7 100644 --- a/dygraph-utils.js +++ b/dygraph-utils.js @@ -136,13 +136,27 @@ Dygraph.getContext = function(canvas) { * @param { Function } fn The function to call on the event. The function takes * one parameter: the event object. */ -Dygraph.prototype.addEvent = function addEvent(elem, type, fn) { +Dygraph.addEvent = function addEvent(elem, type, fn) { if (elem.addEventListener) { elem.addEventListener(type, fn, false); } else { elem[type+fn] = function(){fn(window.event);}; elem.attachEvent('on'+type, elem[type+fn]); } +}; + +/** + * @private + * Add an event handler. This event handler is kept until the graph is + * destroyed with a call to graph.destroy(). + * + * @param { DOM element } elem The element to add the event to. + * @param { String } type The type of the event, e.g. 'click' or 'mousemove'. + * @param { Function } fn The function to call on the event. The function takes + * one parameter: the event object. + */ +Dygraph.prototype.addEvent = function addEvent(elem, type, fn) { + Dygraph.addEvent(elem, type, fn); this.registeredEvents_.push({ elem : elem, type : type, fn : fn }); }; @@ -155,7 +169,7 @@ Dygraph.prototype.addEvent = function addEvent(elem, type, fn) { * @param { Function } fn The function to call on the event. The function takes * one parameter: the event object. */ -Dygraph.prototype.removeEvent = function addEvent(elem, type, fn) { +Dygraph.removeEvent = function addEvent(elem, type, fn) { if (elem.removeEventListener) { elem.removeEventListener(type, fn, false); } else { diff --git a/dygraph.js b/dygraph.js index 6c15698..06abf97 100644 --- a/dygraph.js +++ b/dygraph.js @@ -985,14 +985,14 @@ Dygraph.prototype.destroy = function() { for (var idx = 0; idx < this.registeredEvents_.length; idx++) { var reg = this.registeredEvents_[idx]; - this.removeEvent(reg.elem, reg.type, reg.fn); + Dygraph.removeEvent(reg.elem, reg.type, reg.fn); } this.registeredEvents_ = []; // remove mouse event handlers (This may not be necessary anymore) - this.removeEvent(this.mouseEventElement_, 'mouseout', this.mouseOutHandler); - this.removeEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler); - this.removeEvent(this.mouseEventElement_, 'mousemove', this.mouseUpHandler_); + Dygraph.removeEvent(this.mouseEventElement_, 'mouseout', this.mouseOutHandler); + Dygraph.removeEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler); + Dygraph.removeEvent(this.mouseEventElement_, 'mousemove', this.mouseUpHandler_); removeRecursive(this.maindiv_); var nullOut = function(obj) { @@ -1003,7 +1003,7 @@ Dygraph.prototype.destroy = function() { } }; // remove event handlers - this.removeEvent(window,'resize',this.resizeHandler); + Dygraph.removeEvent(window,'resize',this.resizeHandler); this.resizeHandler = null; // These may not all be necessary, but it can't hurt... nullOut(this.layout_);