DygraphOps now does the right thing with events, which have read-only attributes...
[dygraphs.git] / auto_tests / tests / DygraphOps.js
... / ...
CommitLineData
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 */
27var DygraphOps = {};
28
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 }
57 }
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);
91};
92
93DygraphOps.dispatchMouseDown = function(g, x, y, custom) {
94 var px = Dygraph.findPosX(g.canvas_);
95 var py = Dygraph.findPosY(g.canvas_);
96
97 var pageX = px + g.toDomXCoord(x);
98 var pageY = py + g.toDomYCoord(y);
99
100 var opts = {
101 type : 'mousedown',
102 detail : 1,
103 screenX : pageX,
104 screenY : pageY,
105 clientX : pageX,
106 clientY : pageY,
107 };
108
109 var event = DygraphOps.createEvent_(opts, custom);
110 g.canvas_.dispatchEvent(event);
111};
112
113DygraphOps.dispatchMouseMove = function(g, x, y, custom) {
114 var px = Dygraph.findPosX(g.canvas_);
115 var py = Dygraph.findPosY(g.canvas_);
116
117 var pageX = px + g.toDomXCoord(x);
118 var pageY = py + g.toDomYCoord(y);
119
120 var opts = {
121 type : 'mousemove',
122 screenX : pageX,
123 screenY : pageY,
124 clientX : pageX,
125 clientY : pageY,
126 };
127
128 var event = DygraphOps.createEvent_(opts, custom);
129 g.canvas_.dispatchEvent(event);
130};
131
132DygraphOps.dispatchMouseUp = function(g, x, y, custom) {
133 var px = Dygraph.findPosX(g.canvas_);
134 var py = Dygraph.findPosY(g.canvas_);
135
136 var pageX = px + g.toDomXCoord(x);
137 var pageY = py + g.toDomYCoord(y);
138
139 var opts = {
140 type : 'mouseup',
141 screenX : pageX,
142 screenY : pageY,
143 clientX : pageX,
144 clientY : pageY,
145 };
146
147 var event = DygraphOps.createEvent_(opts, custom);
148 g.canvas_.dispatchEvent(event);
149};