Commit | Line | Data |
---|---|---|
9c585af6 RK |
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 | ||
83b0c192 | 22 | /** |
9c585af6 RK |
23 | * @fileoverview Plug-in for providing unzoom-on-hover. |
24 | * | |
25 | * @author konigsberg@google.com (Robert Konigsberg) | |
26 | */ | |
27 | Dygraph.Plugins.Unzoom = (function() { | |
83b0c192 | 28 | |
9c585af6 | 29 | "use strict"; |
83b0c192 | 30 | |
9c585af6 | 31 | /** |
eced46cf | 32 | * Create a new instance. |
9c585af6 RK |
33 | * |
34 | * @constructor | |
35 | */ | |
36 | var unzoom = function() { | |
eced46cf | 37 | this.button_ = null; |
ea2169e7 RK |
38 | |
39 | // True when the mouse is over the canvas. Must be tracked | |
40 | // because the unzoom button state can change even when the | |
41 | // mouse-over state hasn't. | |
8f6c7ad7 | 42 | this.over_ = false; |
83b0c192 DV |
43 | }; |
44 | ||
9c585af6 | 45 | unzoom.prototype.toString = function() { |
83b0c192 | 46 | return 'Unzoom Plugin'; |
9c585af6 RK |
47 | }; |
48 | ||
49 | unzoom.prototype.activate = function(g) { | |
50 | return { | |
51 | willDrawChart: this.willDrawChart | |
52 | }; | |
53 | }; | |
54 | ||
55 | unzoom.prototype.willDrawChart = function(e) { | |
56 | var g = e.dygraph; | |
eced46cf | 57 | |
83b0c192 | 58 | if (this.button_ !== null) { |
ea2169e7 RK |
59 | // short-circuit: show the button only when we're moused over, and zoomed in. |
60 | var showButton = g.isZoomed() && this.over_; | |
61 | this.show(showButton); | |
9c585af6 RK |
62 | return; |
63 | } | |
64 | ||
eced46cf | 65 | this.button_ = document.createElement('button'); |
009e3ec3 | 66 | this.button_.innerHTML = 'Reset Zoom'; |
eced46cf RK |
67 | this.button_.style.display = 'none'; |
68 | this.button_.style.position = 'absolute'; | |
ea2169e7 RK |
69 | var area = g.plotter_.area; |
70 | this.button_.style.top = (area.y + 4) + 'px'; | |
71 | this.button_.style.left = (area.x + 4) + 'px'; | |
72 | this.button_.style.zIndex = 11; | |
9c585af6 | 73 | var parent = g.graphDiv; |
eced46cf RK |
74 | parent.insertBefore(this.button_, parent.firstChild); |
75 | ||
76 | var self = this; | |
77 | this.button_.onclick = function() { | |
ea2169e7 | 78 | g.resetZoom(); |
83b0c192 | 79 | }; |
8f6c7ad7 | 80 | |
eced46cf | 81 | g.addEvent(parent, 'mouseover', function() { |
8f6c7ad7 RK |
82 | if (g.isZoomed()) { |
83 | self.show(true); | |
84 | } | |
85 | self.over_ = true; | |
9c585af6 RK |
86 | }); |
87 | ||
eced46cf | 88 | g.addEvent(parent, 'mouseout', function() { |
8f6c7ad7 RK |
89 | self.show(false); |
90 | self.over_ = false; | |
9c585af6 RK |
91 | }); |
92 | }; | |
93 | ||
8f6c7ad7 | 94 | unzoom.prototype.show = function(enabled) { |
009e3ec3 | 95 | this.button_.style.display = enabled ? '' : 'none'; |
8f6c7ad7 RK |
96 | }; |
97 | ||
9c585af6 | 98 | unzoom.prototype.destroy = function() { |
eced46cf | 99 | this.button_.parentElement.removeChild(this.button_); |
9c585af6 RK |
100 | }; |
101 | ||
102 | return unzoom; | |
103 | ||
104 | })(); |