Document the value of `this`.
[dygraphs.git] / extras / unzoom.js
CommitLineData
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
0cd1ad15 21/*global Dygraph:false */
9c585af6 22
83b0c192 23/**
9c585af6
RK
24 * @fileoverview Plug-in for providing unzoom-on-hover.
25 *
26 * @author konigsberg@google.com (Robert Konigsberg)
27 */
28Dygraph.Plugins.Unzoom = (function() {
83b0c192 29
9c585af6 30 "use strict";
83b0c192 31
9c585af6 32 /**
eced46cf 33 * Create a new instance.
9c585af6
RK
34 *
35 * @constructor
36 */
37 var unzoom = function() {
eced46cf 38 this.button_ = null;
ea2169e7
RK
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.
8f6c7ad7 43 this.over_ = false;
83b0c192
DV
44 };
45
9c585af6 46 unzoom.prototype.toString = function() {
83b0c192 47 return 'Unzoom Plugin';
9c585af6
RK
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;
eced46cf 58
83b0c192 59 if (this.button_ !== null) {
ea2169e7
RK
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);
9c585af6
RK
63 return;
64 }
65
eced46cf 66 this.button_ = document.createElement('button');
009e3ec3 67 this.button_.innerHTML = 'Reset Zoom';
eced46cf
RK
68 this.button_.style.display = 'none';
69 this.button_.style.position = 'absolute';
ea2169e7
RK
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;
9c585af6 74 var parent = g.graphDiv;
eced46cf
RK
75 parent.insertBefore(this.button_, parent.firstChild);
76
77 var self = this;
78 this.button_.onclick = function() {
ea2169e7 79 g.resetZoom();
83b0c192 80 };
8f6c7ad7 81
aeca29ac 82 g.addAndTrackEvent(parent, 'mouseover', function() {
8f6c7ad7
RK
83 if (g.isZoomed()) {
84 self.show(true);
85 }
86 self.over_ = true;
9c585af6
RK
87 });
88
aeca29ac 89 g.addAndTrackEvent(parent, 'mouseout', function() {
8f6c7ad7
RK
90 self.show(false);
91 self.over_ = false;
9c585af6
RK
92 });
93 };
94
8f6c7ad7 95 unzoom.prototype.show = function(enabled) {
009e3ec3 96 this.button_.style.display = enabled ? '' : 'none';
8f6c7ad7
RK
97 };
98
9c585af6 99 unzoom.prototype.destroy = function() {
eced46cf 100 this.button_.parentElement.removeChild(this.button_);
9c585af6
RK
101 };
102
103 return unzoom;
104
105})();