Unzoom plug-in, rev 1.
[dygraphs.git] / plugins / 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
21
22/**
23 * @fileoverview Plug-in for providing unzoom-on-hover.
24 *
25 * @author konigsberg@google.com (Robert Konigsberg)
26 */
27Dygraph.Plugins.Unzoom = (function() {
28
29 "use strict";
30
31 /**
32 * Draws the unzoom box.
33 *
34 * @constructor
35 */
36 var unzoom = function() {
37 };
38
39 unzoom.prototype.toString = function() {
40 return "Unzoom Plugin";
41 };
42
43 unzoom.prototype.activate = function(g) {
44 return {
45 willDrawChart: this.willDrawChart
46 };
47 };
48
49 unzoom.prototype.willDrawChart = function(e) {
50 var g = e.dygraph;
51 // API note:
52 // Consider adding a context parameter to activate and willDrawChart
53 // that can be used for storage so I don't have to do things like
54 // use up g.unzoomButton_.
55 if (g.hasOwnProperty("unzoomButton_")) {
56 return;
57 }
58
59 var elem = document.createElement("button");
60 elem.innerHTML = "Unzoom";
61 elem.style.display="none";
62 elem.style.position="absolute";
63 elem.style.top = '2px';
64 elem.style.left = '59px';
65 elem.style.zIndex = 1000;
66 var parent = g.graphDiv;
67 parent.insertBefore(elem, parent.firstChild);
68 elem.onclick = function() {
69 // TODO(konigsberg): doUnzoom_ is private.
70 g.doUnzoom_();
71 }
72 g.unzoomButton_ = elem;
73 Dygraph.addEvent(parent, "mouseover", function() {
74 g.unzoomButton_.style.display="block";
75 });
76
77 // TODO(konigsberg): Don't show unless the graph is zoomed.
78 Dygraph.addEvent(parent, "mouseout", function() {
79 g.unzoomButton_.style.display="none";
80 });
81 };
82
83 unzoom.prototype.destroy = function() {
84 delete g.unzoomButton_;
85 // TODO(konigsberg): Remove events installed above.
86 };
87
88 return unzoom;
89
90})();