From: Dan Vanderkam Date: Tue, 29 Jan 2013 21:04:39 +0000 (-0800) Subject: Merge pull request #200 from kberg/resize-handler X-Git-Tag: v1.0.0~111 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=94efbcad5accd7f314b57fb5c84f0dd86b3cb58b;hp=bc4013391cbf04f12af0361438a3adf16e002190;p=dygraphs.git Merge pull request #200 from kberg/resize-handler Resize handler --- diff --git a/dygraph.js b/dygraph.js index 929cd10..43d3011 100644 --- a/dygraph.js +++ b/dygraph.js @@ -956,23 +956,27 @@ Dygraph.prototype.createInterface_ = function() { var dygraph = this; - this.mouseMoveHandler = function(e) { - dygraph.mouseMove_(e); - }; - this.addEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler); + // Don't recreate and register the handlers on subsequent calls. + // This happens when the graph is resized. + if (!this.mouseMoveHandler_) { + this.mouseMoveHandler_ = function(e) { + dygraph.mouseMove_(e); + }; + this.addEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler_); - this.mouseOutHandler = function(e) { - dygraph.mouseOut_(e); - }; - this.addEvent(this.mouseEventElement_, 'mouseout', this.mouseOutHandler); + this.mouseOutHandler_ = function(e) { + dygraph.mouseOut_(e); + }; + this.addEvent(this.mouseEventElement_, 'mouseout', this.mouseOutHandler_); - this.resizeHandler = function(e) { - dygraph.resize(); - }; + this.resizeHandler_ = function(e) { + dygraph.resize(); + }; - // Update when the window is resized. - // TODO(danvk): drop frames depending on complexity of the chart. - this.addEvent(window, 'resize', this.resizeHandler); + // Update when the window is resized. + // TODO(danvk): drop frames depending on complexity of the chart. + this.addEvent(window, 'resize', this.resizeHandler_); + } }; /** @@ -995,9 +999,14 @@ Dygraph.prototype.destroy = function() { this.registeredEvents_ = []; // remove mouse event handlers (This may not be necessary anymore) - Dygraph.removeEvent(this.mouseEventElement_, 'mouseout', this.mouseOutHandler); - Dygraph.removeEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler); + Dygraph.removeEvent(this.mouseEventElement_, 'mouseout', this.mouseOutHandler_); + Dygraph.removeEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler_); Dygraph.removeEvent(this.mouseEventElement_, 'mousemove', this.mouseUpHandler_); + + // remove window handlers + Dygraph.removeEvent(window,'resize',this.resizeHandler_); + this.resizeHandler_ = null; + removeRecursive(this.maindiv_); var nullOut = function(obj) { @@ -1007,9 +1016,6 @@ Dygraph.prototype.destroy = function() { } } }; - // remove event handlers - Dygraph.removeEvent(window,'resize',this.resizeHandler); - this.resizeHandler = null; // These may not all be necessary, but it can't hurt... nullOut(this.layout_); nullOut(this.plotter_);