1 // Copyright (c) 2011 Google, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
23 * @fileoverview Utility functions for Dygraphs.
25 * @author konigsberg@google.com (Robert Konigsberg)
29 DygraphOps
.defaultEvent_
= {
33 view
: document
.defaultView
,
48 * Create an event. Sets default event values except for special ones
49 * overridden by the 'custom' parameter.
51 * @param command the command to create.
52 * @param custom an associative array of event attributes and their new values.
54 DygraphOps
.createEvent
= function(command
, custom
) {
56 var copy
= function(from
, to
) {
58 for (var prop
in from
) {
59 if(from
.hasOwnProperty(prop
)) {
60 to
[prop
] = from
[prop
];
67 copy(DygraphOps
.defaultEvent_
, e
);
71 var event
= document
.createEvent('MouseEvents');
92 * Dispatch an event onto the graph's canvas.
94 DygraphOps
.dispatchCanvasEvent
= function(g
, event
) {
95 g
.canvas_
.dispatchEvent(event
);
98 DygraphOps
.dispatchDoubleClick
= function(g
, custom
) {
103 var event
= DygraphOps
.createEvent(opts
, custom
);
104 DygraphOps
.dispatchCanvasEvent(g
, event
);
108 * Create an 'opts' argument which can be passed to createEvent that contains
109 * type, screenX, screenY, clientX, clientY.
111 DygraphOps
.createOptsForPoint_
= function(g
, type
, x
, y
) {
112 var pageX
= Dygraph
.findPosX(g
.canvas_
) + x
;
113 var pageY
= Dygraph
.findPosY(g
.canvas_
) + y
;
124 DygraphOps
.dispatchMouseDown_Point
= function(g
, x
, y
, custom
) {
125 var opts
= DygraphOps
.createOptsForPoint_(g
, 'mousedown', x
, y
);
127 var event
= DygraphOps
.createEvent(opts
, custom
);
128 DygraphOps
.dispatchCanvasEvent(g
, event
);
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
);
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
);
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
);
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
);
156 * Dispatches a mouse down using the graph's data coordinate system.
157 * (The y value mapped to the first axis.)
159 DygraphOps
.dispatchMouseDown
= function(g
, x
, y
, custom
) {
160 DygraphOps
.dispatchMouseDown_Point(
168 * Dispatches a mouse move using the graph's data coordinate system.
169 * (The y value mapped to the first axis.)
171 DygraphOps
.dispatchMouseMove
= function(g
, x
, y
, custom
) {
172 DygraphOps
.dispatchMouseMove_Point(
180 * Dispatches a mouse up using the graph's data coordinate system.
181 * (The y value mapped to the first axis.)
183 DygraphOps
.dispatchMouseUp
= function(g
, x
, y
, custom
) {
184 DygraphOps
.dispatchMouseUp_Point(
192 * Dispatches a mouse over using the graph's data coordinate system.
193 * (The y value mapped to the first axis.)
195 DygraphOps
.dispatchMouseOver
= function(g
, x
, y
, custom
) {
196 DygraphOps
.dispatchMouseOver_Point(
204 * Dispatches a mouse out using the graph's data coordinate system.
205 * (The y value mapped to the first axis.)
207 DygraphOps
.dispatchMouseOut
= function(g
, x
, y
, custom
) {
208 DygraphOps
.dispatchMouseOut_Point(