merge in kberg changes
[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
21
22/**
23 * @fileoverview Utility functions for Dygraphs.
24 *
25 * @author konigsberg@google.com (Robert Konigsberg)
26 */
27var DygraphOps = {};
28
a12f271c
RK
29DygraphOps.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
47DygraphOps.createEvent_ = function(command, custom) {
48
49 var copy = function(from, to) {
50 if (from != null) {
51 for (var prop in from) {
52 if(from.hasOwnProperty(prop)) {
53 to[prop] = from[prop];
54 }
55 }
56 }
72a74f04 57 }
a12f271c
RK
58
59 var e = {};
60 copy(DygraphOps.defaultEvent_, e);
61 copy(command, e);
62 copy(custom, e);
63
64 var event = document.createEvent('MouseEvents');
65 event.initMouseEvent(
66 e.type,
67 e.canBubble,
68 e.cancelable,
69 e.view,
70 e.detail,
71 e.screenX,
72 e.screenY,
73 e.clientX,
74 e.clientY,
75 e.ctrlKey,
76 e.altKey,
77 e.shiftKey,
78 e.metaKey,
79 e.button,
80 e.relatedTarget);
81 return event;
82}
83
84DygraphOps.dispatchDoubleClick = function(g, custom) {
85 var opts = {
86 type : 'dblclick',
87 detail : 2
88 };
89 var event = DygraphOps.createEvent_(opts, custom);
90 g.canvas_.dispatchEvent(event);
72a74f04
RK
91};
92
9c831431
RK
93DygraphOps.dispatchMouseDown_Point = function(g, x, y, custom) {
94 var pageX = Dygraph.findPosX(g.canvas_) + x;
95 var pageY = Dygraph.findPosY(g.canvas_) + y;
72a74f04 96
a12f271c
RK
97 var opts = {
98 type : 'mousedown',
99 detail : 1,
100 screenX : pageX,
101 screenY : pageY,
102 clientX : pageX,
103 clientY : pageY,
104 };
105
106 var event = DygraphOps.createEvent_(opts, custom);
107 g.canvas_.dispatchEvent(event);
9c831431 108}
72a74f04 109
9c831431
RK
110DygraphOps.dispatchMouseMove_Point = function(g, x, y, custom) {
111 var pageX = Dygraph.findPosX(g.canvas_) + x;
112 var pageY = Dygraph.findPosY(g.canvas_) + y;
72a74f04 113
a12f271c
RK
114 var opts = {
115 type : 'mousemove',
116 screenX : pageX,
117 screenY : pageY,
118 clientX : pageX,
119 clientY : pageY,
120 };
121
122 var event = DygraphOps.createEvent_(opts, custom);
123 g.canvas_.dispatchEvent(event);
72a74f04
RK
124};
125
9c831431
RK
126DygraphOps.dispatchMouseUp_Point = function(g, x, y, custom) {
127 var pageX = Dygraph.findPosX(g.canvas_) + x;
128 var pageY = Dygraph.findPosY(g.canvas_) + y;
72a74f04 129
a12f271c
RK
130 var opts = {
131 type : 'mouseup',
132 screenX : pageX,
133 screenY : pageY,
134 clientX : pageX,
135 clientY : pageY,
136 };
137
138 var event = DygraphOps.createEvent_(opts, custom);
139 g.canvas_.dispatchEvent(event);
72a74f04 140};
9c831431 141
6ace73a8
RK
142/**
143 * Dispatches a mouse down using the graph's data coordinate system.
144 * (The y value mapped to the first axis.)
145 */
9c831431
RK
146DygraphOps.dispatchMouseDown = function(g, x, y, custom) {
147 DygraphOps.dispatchMouseDown_Point(
148 g,
149 g.toDomXCoord(x),
150 g.toDomYCoord(y),
151 custom);
152};
153
6ace73a8
RK
154/**
155 * Dispatches a mouse move using the graph's data coordinate system.
156 * (The y value mapped to the first axis.)
157 */
9c831431
RK
158DygraphOps.dispatchMouseMove = function(g, x, y, custom) {
159 DygraphOps.dispatchMouseMove_Point(
160 g,
161 g.toDomXCoord(x),
162 g.toDomYCoord(y),
163 custom);
164};
165
6ace73a8
RK
166/**
167 * Dispatches a mouse up using the graph's data coordinate system.
168 * (The y value mapped to the first axis.)
169 */
9c831431
RK
170DygraphOps.dispatchMouseUp = function(g, x, y, custom) {
171 DygraphOps.dispatchMouseUp_Point(
172 g,
173 g.toDomXCoord(x),
174 g.toDomYCoord(y),
175 custom);
176};
177