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 | ||
22 | /** | |
23 | * @fileoverview Plug-in for providing unzoom-on-hover. | |
24 | * | |
25 | * @author konigsberg@google.com (Robert Konigsberg) | |
26 | */ | |
27 | Dygraph.Plugins.Unzoom = (function() { | |
28 | ||
29 | "use strict"; | |
30 | ||
31 | /** | |
eced46cf | 32 | * Create a new instance. |
9c585af6 RK |
33 | * |
34 | * @constructor | |
35 | */ | |
36 | var unzoom = function() { | |
eced46cf | 37 | this.button_ = null; |
8f6c7ad7 | 38 | this.over_ = false; |
9c585af6 RK |
39 | }; |
40 | ||
41 | unzoom.prototype.toString = function() { | |
eced46cf | 42 | return 'Unzoom Plugin'; |
9c585af6 RK |
43 | }; |
44 | ||
45 | unzoom.prototype.activate = function(g) { | |
46 | return { | |
47 | willDrawChart: this.willDrawChart | |
48 | }; | |
49 | }; | |
50 | ||
51 | unzoom.prototype.willDrawChart = function(e) { | |
52 | var g = e.dygraph; | |
eced46cf RK |
53 | |
54 | if (this.button_ != null) { | |
8f6c7ad7 RK |
55 | if (g.isZoomed() && this.over_) { |
56 | this.show(true); | |
57 | } | |
9c585af6 RK |
58 | return; |
59 | } | |
60 | ||
eced46cf RK |
61 | this.button_ = document.createElement('button'); |
62 | this.button_.innerHTML = 'Unzoom'; | |
63 | this.button_.style.display = 'none'; | |
64 | this.button_.style.position = 'absolute'; | |
65 | this.button_.style.top = '2px'; | |
66 | this.button_.style.left = '59px'; | |
67 | this.button_.style.zIndex = 1000; | |
9c585af6 | 68 | var parent = g.graphDiv; |
eced46cf RK |
69 | parent.insertBefore(this.button_, parent.firstChild); |
70 | ||
71 | var self = this; | |
72 | this.button_.onclick = function() { | |
9c585af6 RK |
73 | // TODO(konigsberg): doUnzoom_ is private. |
74 | g.doUnzoom_(); | |
75 | } | |
8f6c7ad7 | 76 | |
eced46cf | 77 | g.addEvent(parent, 'mouseover', function() { |
8f6c7ad7 RK |
78 | if (g.isZoomed()) { |
79 | self.show(true); | |
80 | } | |
81 | self.over_ = true; | |
9c585af6 RK |
82 | }); |
83 | ||
eced46cf | 84 | g.addEvent(parent, 'mouseout', function() { |
8f6c7ad7 RK |
85 | self.show(false); |
86 | self.over_ = false; | |
9c585af6 RK |
87 | }); |
88 | }; | |
89 | ||
8f6c7ad7 RK |
90 | unzoom.prototype.show = function(enabled) { |
91 | this.button_.style.display = enabled ? 'block' : 'none'; | |
92 | }; | |
93 | ||
9c585af6 | 94 | unzoom.prototype.destroy = function() { |
eced46cf | 95 | this.button_.parentElement.removeChild(this.button_); |
9c585af6 RK |
96 | }; |
97 | ||
98 | return unzoom; | |
99 | ||
100 | })(); |