Commit | Line | Data |
---|---|---|
718ad8e2 | 1 | // Copyright (c) 2011 Google, Inc. |
72a74f04 RK |
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 Utility functions for Dygraphs. | |
24 | * | |
25 | * @author konigsberg@google.com (Robert Konigsberg) | |
26 | */ | |
27 | var DygraphOps = {}; | |
28 | ||
a12f271c RK |
29 | DygraphOps.defaultEvent_ = { |
30 | type : '', | |
31 | canBubble : true, | |
32 | cancelable : true, | |
33 | view : document.defaultView, | |
34 | detail : 0, | |
35 | screenX : 0, | |
36 | screenY : 0, | |
37 | clientX : 0, | |
38 | clientY : 0, | |
39 | ctrlKey : false, | |
40 | altKey : false, | |
41 | shiftKey : false, | |
42 | metaKey : false, | |
43 | button : 0, | |
44 | relatedTarget : null | |
45 | }; | |
46 | ||
21c079e5 RK |
47 | /** |
48 | * Create an event. Sets default event values except for special ones | |
49 | * overridden by the 'custom' parameter. | |
50 | * | |
51 | * @param command the command to create. | |
52 | * @param custom an associative array of event attributes and their new values. | |
53 | */ | |
17aad8df | 54 | DygraphOps.createEvent = function(command, custom) { |
a12f271c RK |
55 | |
56 | var copy = function(from, to) { | |
57 | if (from != null) { | |
58 | for (var prop in from) { | |
59 | if(from.hasOwnProperty(prop)) { | |
60 | to[prop] = from[prop]; | |
61 | } | |
62 | } | |
63 | } | |
72a74f04 | 64 | } |
a12f271c RK |
65 | |
66 | var e = {}; | |
67 | copy(DygraphOps.defaultEvent_, e); | |
68 | copy(command, e); | |
69 | copy(custom, e); | |
70 | ||
71 | var event = document.createEvent('MouseEvents'); | |
72 | event.initMouseEvent( | |
73 | e.type, | |
74 | e.canBubble, | |
75 | e.cancelable, | |
76 | e.view, | |
77 | e.detail, | |
78 | e.screenX, | |
79 | e.screenY, | |
80 | e.clientX, | |
81 | e.clientY, | |
82 | e.ctrlKey, | |
83 | e.altKey, | |
84 | e.shiftKey, | |
85 | e.metaKey, | |
86 | e.button, | |
87 | e.relatedTarget); | |
88 | return event; | |
45b74fb1 | 89 | }; |
a12f271c | 90 | |
21c079e5 RK |
91 | /** |
92 | * Dispatch an event onto the graph's canvas. | |
93 | */ | |
17aad8df | 94 | DygraphOps.dispatchCanvasEvent = function(g, event) { |
21c079e5 | 95 | g.canvas_.dispatchEvent(event); |
45b74fb1 | 96 | }; |
21c079e5 | 97 | |
a12f271c RK |
98 | DygraphOps.dispatchDoubleClick = function(g, custom) { |
99 | var opts = { | |
100 | type : 'dblclick', | |
101 | detail : 2 | |
102 | }; | |
914805d3 | 103 | var event = DygraphOps.createEvent(opts, custom); |
21c079e5 | 104 | DygraphOps.dispatchCanvasEvent(g, event); |
72a74f04 RK |
105 | }; |
106 | ||
45b74fb1 RK |
107 | /* |
108 | * Create an 'opts' argument which can be passed to createEvent that contains | |
109 | * type, screenX, screenY, clientX, clientY. | |
110 | */ | |
111 | DygraphOps.createOptsForPoint_ = function(g, type, x, y) { | |
464b5f50 DV |
112 | var pos = Dygraph.findPos(g.canvas_); |
113 | var pageX = pos.x + x; | |
114 | var pageY = pos.y + y; | |
72a74f04 | 115 | |
45b74fb1 RK |
116 | return { |
117 | type : type, | |
a12f271c RK |
118 | screenX : pageX, |
119 | screenY : pageY, | |
120 | clientX : pageX, | |
121 | clientY : pageY, | |
122 | }; | |
45b74fb1 | 123 | }; |
a12f271c | 124 | |
45b74fb1 RK |
125 | DygraphOps.dispatchMouseDown_Point = function(g, x, y, custom) { |
126 | var opts = DygraphOps.createOptsForPoint_(g, 'mousedown', x, y); | |
127 | opts.detail = 1; | |
914805d3 | 128 | var event = DygraphOps.createEvent(opts, custom); |
21c079e5 | 129 | DygraphOps.dispatchCanvasEvent(g, event); |
45b74fb1 | 130 | }; |
72a74f04 | 131 | |
9c831431 | 132 | DygraphOps.dispatchMouseMove_Point = function(g, x, y, custom) { |
45b74fb1 | 133 | var opts = DygraphOps.createOptsForPoint_(g, 'mousemove', x, y); |
914805d3 | 134 | var event = DygraphOps.createEvent(opts, custom); |
21c079e5 | 135 | DygraphOps.dispatchCanvasEvent(g, event); |
72a74f04 RK |
136 | }; |
137 | ||
9c831431 | 138 | DygraphOps.dispatchMouseUp_Point = function(g, x, y, custom) { |
45b74fb1 RK |
139 | var opts = DygraphOps.createOptsForPoint_(g, 'mouseup', x, y); |
140 | var event = DygraphOps.createEvent(opts, custom); | |
141 | DygraphOps.dispatchCanvasEvent(g, event); | |
142 | }; | |
72a74f04 | 143 | |
45b74fb1 RK |
144 | DygraphOps.dispatchMouseOver_Point = function(g, x, y, custom) { |
145 | var opts = DygraphOps.createOptsForPoint_(g, 'mouseover', x, y); | |
146 | var event = DygraphOps.createEvent(opts, custom); | |
147 | DygraphOps.dispatchCanvasEvent(g, event); | |
148 | }; | |
a12f271c | 149 | |
45b74fb1 RK |
150 | DygraphOps.dispatchMouseOut_Point = function(g, x, y, custom) { |
151 | var opts = DygraphOps.createOptsForPoint_(g, 'mouseout', x, y); | |
914805d3 | 152 | var event = DygraphOps.createEvent(opts, custom); |
21c079e5 | 153 | DygraphOps.dispatchCanvasEvent(g, event); |
72a74f04 | 154 | }; |
9c831431 | 155 | |
6ace73a8 RK |
156 | /** |
157 | * Dispatches a mouse down using the graph's data coordinate system. | |
158 | * (The y value mapped to the first axis.) | |
159 | */ | |
9c831431 RK |
160 | DygraphOps.dispatchMouseDown = function(g, x, y, custom) { |
161 | DygraphOps.dispatchMouseDown_Point( | |
162 | g, | |
163 | g.toDomXCoord(x), | |
164 | g.toDomYCoord(y), | |
165 | custom); | |
166 | }; | |
167 | ||
6ace73a8 RK |
168 | /** |
169 | * Dispatches a mouse move using the graph's data coordinate system. | |
170 | * (The y value mapped to the first axis.) | |
171 | */ | |
9c831431 RK |
172 | DygraphOps.dispatchMouseMove = function(g, x, y, custom) { |
173 | DygraphOps.dispatchMouseMove_Point( | |
174 | g, | |
175 | g.toDomXCoord(x), | |
176 | g.toDomYCoord(y), | |
177 | custom); | |
178 | }; | |
179 | ||
6ace73a8 RK |
180 | /** |
181 | * Dispatches a mouse up using the graph's data coordinate system. | |
182 | * (The y value mapped to the first axis.) | |
183 | */ | |
9c831431 RK |
184 | DygraphOps.dispatchMouseUp = function(g, x, y, custom) { |
185 | DygraphOps.dispatchMouseUp_Point( | |
186 | g, | |
187 | g.toDomXCoord(x), | |
188 | g.toDomYCoord(y), | |
189 | custom); | |
190 | }; | |
191 | ||
45b74fb1 RK |
192 | /** |
193 | * Dispatches a mouse over using the graph's data coordinate system. | |
194 | * (The y value mapped to the first axis.) | |
195 | */ | |
196 | DygraphOps.dispatchMouseOver = function(g, x, y, custom) { | |
197 | DygraphOps.dispatchMouseOver_Point( | |
198 | g, | |
199 | g.toDomXCoord(x), | |
200 | g.toDomYCoord(y), | |
201 | custom); | |
202 | }; | |
203 | ||
204 | /** | |
205 | * Dispatches a mouse out using the graph's data coordinate system. | |
206 | * (The y value mapped to the first axis.) | |
207 | */ | |
208 | DygraphOps.dispatchMouseOut = function(g, x, y, custom) { | |
209 | DygraphOps.dispatchMouseOut_Point( | |
210 | g, | |
211 | g.toDomXCoord(x), | |
212 | g.toDomYCoord(y), | |
213 | custom); | |
214 | }; | |
215 |