| 1 | // Copyright (c) 2013 Google, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | /*global Dygraph:false */ |
| 22 | |
| 23 | /** |
| 24 | * @fileoverview Plug-in for providing unzoom-on-hover. |
| 25 | * |
| 26 | * @author konigsberg@google.com (Robert Konigsberg) |
| 27 | */ |
| 28 | Dygraph.Plugins.Unzoom = (function() { |
| 29 | |
| 30 | "use strict"; |
| 31 | |
| 32 | /** |
| 33 | * Create a new instance. |
| 34 | * |
| 35 | * @constructor |
| 36 | */ |
| 37 | var unzoom = function() { |
| 38 | this.button_ = null; |
| 39 | |
| 40 | // True when the mouse is over the canvas. Must be tracked |
| 41 | // because the unzoom button state can change even when the |
| 42 | // mouse-over state hasn't. |
| 43 | this.over_ = false; |
| 44 | }; |
| 45 | |
| 46 | unzoom.prototype.toString = function() { |
| 47 | return 'Unzoom Plugin'; |
| 48 | }; |
| 49 | |
| 50 | unzoom.prototype.activate = function(g) { |
| 51 | return { |
| 52 | willDrawChart: this.willDrawChart |
| 53 | }; |
| 54 | }; |
| 55 | |
| 56 | unzoom.prototype.willDrawChart = function(e) { |
| 57 | var g = e.dygraph; |
| 58 | |
| 59 | if (this.button_ !== null) { |
| 60 | // short-circuit: show the button only when we're moused over, and zoomed in. |
| 61 | var showButton = g.isZoomed() && this.over_; |
| 62 | this.show(showButton); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | this.button_ = document.createElement('button'); |
| 67 | this.button_.innerHTML = 'Reset Zoom'; |
| 68 | this.button_.style.display = 'none'; |
| 69 | this.button_.style.position = 'absolute'; |
| 70 | var area = g.plotter_.area; |
| 71 | this.button_.style.top = (area.y + 4) + 'px'; |
| 72 | this.button_.style.left = (area.x + 4) + 'px'; |
| 73 | this.button_.style.zIndex = 11; |
| 74 | var parent = g.graphDiv; |
| 75 | parent.insertBefore(this.button_, parent.firstChild); |
| 76 | |
| 77 | var self = this; |
| 78 | this.button_.onclick = function() { |
| 79 | g.resetZoom(); |
| 80 | }; |
| 81 | |
| 82 | g.addAndTrackEvent(parent, 'mouseover', function() { |
| 83 | if (g.isZoomed()) { |
| 84 | self.show(true); |
| 85 | } |
| 86 | self.over_ = true; |
| 87 | }); |
| 88 | |
| 89 | g.addAndTrackEvent(parent, 'mouseout', function() { |
| 90 | self.show(false); |
| 91 | self.over_ = false; |
| 92 | }); |
| 93 | }; |
| 94 | |
| 95 | unzoom.prototype.show = function(enabled) { |
| 96 | this.button_.style.display = enabled ? '' : 'none'; |
| 97 | }; |
| 98 | |
| 99 | unzoom.prototype.destroy = function() { |
| 100 | this.button_.parentElement.removeChild(this.button_); |
| 101 | }; |
| 102 | |
| 103 | return unzoom; |
| 104 | |
| 105 | })(); |