Fix responsese to code review 194.
[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 };
39
40 unzoom.prototype.toString = function() {
41 return 'Unzoom Plugin';
42 };
43
44 unzoom.prototype.activate = function(g) {
45 return {
46 willDrawChart: this.willDrawChart
47 };
48 };
49
50 unzoom.prototype.willDrawChart = function(e) {
51 var g = e.dygraph;
52
53 if (this.button_ != null) {
54 return;
55 }
56
57 this.button_ = document.createElement('button');
58 this.button_.innerHTML = 'Unzoom';
59 this.button_.style.display = 'none';
60 this.button_.style.position = 'absolute';
61 this.button_.style.top = '2px';
62 this.button_.style.left = '59px';
63 this.button_.style.zIndex = 1000;
64 var parent = g.graphDiv;
65 parent.insertBefore(this.button_, parent.firstChild);
66
67 var self = this;
68 this.button_.onclick = function() {
69 // TODO(konigsberg): doUnzoom_ is private.
70 g.doUnzoom_();
71 }
72 g.addEvent(parent, 'mouseover', function() {
73 self.button_.style.display='block';
74 });
75
76 // TODO(konigsberg): Don't show unless the graph is zoomed.
77 g.addEvent(parent, 'mouseout', function() {
78 self.button_.style.display='none';
79 });
80 };
81
82 unzoom.prototype.destroy = function() {
83 this.button_.parentElement.removeChild(this.button_);
84 };
85
86 return unzoom;
87
88 })();