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