Merge branch 'release-1.0.1'
[dygraphs.git] / auto_tests / tests / DygraphOps.js
1 // Copyright (c) 2011 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 Utility functions for Dygraphs.
24 *
25 * @author konigsberg@google.com (Robert Konigsberg)
26 */
27 var DygraphOps = {};
28
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
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 */
54 DygraphOps.createEvent = function(command, custom) {
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 }
64 }
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;
89 };
90
91 /**
92 * Dispatch an event onto the graph's canvas.
93 */
94 DygraphOps.dispatchCanvasEvent = function(g, event) {
95 g.canvas_.dispatchEvent(event);
96 };
97
98 DygraphOps.dispatchDoubleClick = function(g, custom) {
99 var opts = {
100 type : 'dblclick',
101 detail : 2
102 };
103 var event = DygraphOps.createEvent(opts, custom);
104 DygraphOps.dispatchCanvasEvent(g, event);
105 };
106
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) {
112 var pageX = Dygraph.findPosX(g.canvas_) + x;
113 var pageY = Dygraph.findPosY(g.canvas_) + y;
114
115 return {
116 type : type,
117 screenX : pageX,
118 screenY : pageY,
119 clientX : pageX,
120 clientY : pageY,
121 };
122 };
123
124 DygraphOps.dispatchMouseDown_Point = function(g, x, y, custom) {
125 var opts = DygraphOps.createOptsForPoint_(g, 'mousedown', x, y);
126 opts.detail = 1;
127 var event = DygraphOps.createEvent(opts, custom);
128 DygraphOps.dispatchCanvasEvent(g, event);
129 };
130
131 DygraphOps.dispatchMouseMove_Point = function(g, x, y, custom) {
132 var opts = DygraphOps.createOptsForPoint_(g, 'mousemove', x, y);
133 var event = DygraphOps.createEvent(opts, custom);
134 DygraphOps.dispatchCanvasEvent(g, event);
135 };
136
137 DygraphOps.dispatchMouseUp_Point = function(g, x, y, custom) {
138 var opts = DygraphOps.createOptsForPoint_(g, 'mouseup', x, y);
139 var event = DygraphOps.createEvent(opts, custom);
140 DygraphOps.dispatchCanvasEvent(g, event);
141 };
142
143 DygraphOps.dispatchMouseOver_Point = function(g, x, y, custom) {
144 var opts = DygraphOps.createOptsForPoint_(g, 'mouseover', x, y);
145 var event = DygraphOps.createEvent(opts, custom);
146 DygraphOps.dispatchCanvasEvent(g, event);
147 };
148
149 DygraphOps.dispatchMouseOut_Point = function(g, x, y, custom) {
150 var opts = DygraphOps.createOptsForPoint_(g, 'mouseout', x, y);
151 var event = DygraphOps.createEvent(opts, custom);
152 DygraphOps.dispatchCanvasEvent(g, event);
153 };
154
155 /**
156 * Dispatches a mouse down using the graph's data coordinate system.
157 * (The y value mapped to the first axis.)
158 */
159 DygraphOps.dispatchMouseDown = function(g, x, y, custom) {
160 DygraphOps.dispatchMouseDown_Point(
161 g,
162 g.toDomXCoord(x),
163 g.toDomYCoord(y),
164 custom);
165 };
166
167 /**
168 * Dispatches a mouse move using the graph's data coordinate system.
169 * (The y value mapped to the first axis.)
170 */
171 DygraphOps.dispatchMouseMove = function(g, x, y, custom) {
172 DygraphOps.dispatchMouseMove_Point(
173 g,
174 g.toDomXCoord(x),
175 g.toDomYCoord(y),
176 custom);
177 };
178
179 /**
180 * Dispatches a mouse up using the graph's data coordinate system.
181 * (The y value mapped to the first axis.)
182 */
183 DygraphOps.dispatchMouseUp = function(g, x, y, custom) {
184 DygraphOps.dispatchMouseUp_Point(
185 g,
186 g.toDomXCoord(x),
187 g.toDomYCoord(y),
188 custom);
189 };
190
191 /**
192 * Dispatches a mouse over using the graph's data coordinate system.
193 * (The y value mapped to the first axis.)
194 */
195 DygraphOps.dispatchMouseOver = function(g, x, y, custom) {
196 DygraphOps.dispatchMouseOver_Point(
197 g,
198 g.toDomXCoord(x),
199 g.toDomYCoord(y),
200 custom);
201 };
202
203 /**
204 * Dispatches a mouse out using the graph's data coordinate system.
205 * (The y value mapped to the first axis.)
206 */
207 DygraphOps.dispatchMouseOut = function(g, x, y, custom) {
208 DygraphOps.dispatchMouseOut_Point(
209 g,
210 g.toDomXCoord(x),
211 g.toDomYCoord(y),
212 custom);
213 };
214