Took a while to get conditional show to work; works in the case where you zoom in...
[dygraphs.git] / plugins / unzoom.js
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 /**
32 * Create a new instance.
33 *
34 * @constructor
35 */
36 var unzoom = function() {
37 this.button_ = null;
38 this.over_ = false;
39 };
40
41 unzoom.prototype.toString = function() {
42 return 'Unzoom Plugin';
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;
53
54 if (this.button_ != null) {
55 if (g.isZoomed() && this.over_) {
56 this.show(true);
57 }
58 return;
59 }
60
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;
68 var parent = g.graphDiv;
69 parent.insertBefore(this.button_, parent.firstChild);
70
71 var self = this;
72 this.button_.onclick = function() {
73 // TODO(konigsberg): doUnzoom_ is private.
74 g.doUnzoom_();
75 }
76
77 g.addEvent(parent, 'mouseover', function() {
78 if (g.isZoomed()) {
79 self.show(true);
80 }
81 self.over_ = true;
82 });
83
84 g.addEvent(parent, 'mouseout', function() {
85 self.show(false);
86 self.over_ = false;
87 });
88 };
89
90 unzoom.prototype.show = function(enabled) {
91 this.button_.style.display = enabled ? 'block' : 'none';
92 };
93
94 unzoom.prototype.destroy = function() {
95 this.button_.parentElement.removeChild(this.button_);
96 };
97
98 return unzoom;
99
100 })();