X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=plugins%2Funzoom.js;h=802c31b57cd88a81c93ed4d0e8037c95b5fd5b08;hb=0eb0ec0b7a3e1cac96af8a8861bd50b2f7bf0960;hp=8eefcf3fc5905a16e1688df6bb456fab8d1f377e;hpb=8f6c7ad7692372707cdd922b0e6b7d29dc043d9e;p=dygraphs.git diff --git a/plugins/unzoom.js b/plugins/unzoom.js index 8eefcf3..802c31b 100644 --- a/plugins/unzoom.js +++ b/plugins/unzoom.js @@ -18,16 +18,17 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. +/*global Dygraph:false */ -/** +/** * @fileoverview Plug-in for providing unzoom-on-hover. * * @author konigsberg@google.com (Robert Konigsberg) */ Dygraph.Plugins.Unzoom = (function() { - + "use strict"; - + /** * Create a new instance. * @@ -35,11 +36,15 @@ Dygraph.Plugins.Unzoom = (function() { */ var unzoom = function() { this.button_ = null; + + // True when the mouse is over the canvas. Must be tracked + // because the unzoom button state can change even when the + // mouse-over state hasn't. this.over_ = false; - }; - + }; + unzoom.prototype.toString = function() { - return 'Unzoom Plugin'; + return 'Unzoom Plugin'; }; unzoom.prototype.activate = function(g) { @@ -51,28 +56,28 @@ Dygraph.Plugins.Unzoom = (function() { unzoom.prototype.willDrawChart = function(e) { var g = e.dygraph; - if (this.button_ != null) { - if (g.isZoomed() && this.over_) { - this.show(true); - } + if (this.button_ !== null) { + // short-circuit: show the button only when we're moused over, and zoomed in. + var showButton = g.isZoomed() && this.over_; + this.show(showButton); return; } this.button_ = document.createElement('button'); - this.button_.innerHTML = 'Unzoom'; + this.button_.innerHTML = 'Reset Zoom'; this.button_.style.display = 'none'; this.button_.style.position = 'absolute'; - this.button_.style.top = '2px'; - this.button_.style.left = '59px'; - this.button_.style.zIndex = 1000; + var area = g.plotter_.area; + this.button_.style.top = (area.y + 4) + 'px'; + this.button_.style.left = (area.x + 4) + 'px'; + this.button_.style.zIndex = 11; var parent = g.graphDiv; parent.insertBefore(this.button_, parent.firstChild); var self = this; this.button_.onclick = function() { - // TODO(konigsberg): doUnzoom_ is private. - g.doUnzoom_(); - } + g.resetZoom(); + }; g.addEvent(parent, 'mouseover', function() { if (g.isZoomed()) { @@ -88,7 +93,7 @@ Dygraph.Plugins.Unzoom = (function() { }; unzoom.prototype.show = function(enabled) { - this.button_.style.display = enabled ? 'block' : 'none'; + this.button_.style.display = enabled ? '' : 'none'; }; unzoom.prototype.destroy = function() {