Restructure the nature of click callbacks. PointClickCallback now
[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
RK
141
142DygraphOps.dispatchMouseDown = function(g, x, y, custom) {
143 DygraphOps.dispatchMouseDown_Point(
144 g,
145 g.toDomXCoord(x),
146 g.toDomYCoord(y),
147 custom);
148};
149
150DygraphOps.dispatchMouseMove = function(g, x, y, custom) {
151 DygraphOps.dispatchMouseMove_Point(
152 g,
153 g.toDomXCoord(x),
154 g.toDomYCoord(y),
155 custom);
156};
157
158DygraphOps.dispatchMouseUp = function(g, x, y, custom) {
159 DygraphOps.dispatchMouseUp_Point(
160 g,
161 g.toDomXCoord(x),
162 g.toDomYCoord(y),
163 custom);
164};
165