3bd3c4417ea5a1a86e80f49c429da6e41950af85
[dygraphs.git] / auto_tests / tests / DygraphOps.js
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 */
27 var DygraphOps = {};
28
29 DygraphOps.dispatchDoubleClick = function(g, func) {
30 var evt = document.createEvent('MouseEvents');
31 evt.initMouseEvent(
32 'dblclick',
33 true, true, document.defaultView,
34 2, 0, 0, 0, 0,
35 false, false, false, false, 0, null);
36 if (func) {
37 func(evt);
38 }
39 g.canvas_.dispatchEvent(evt);
40 };
41
42 DygraphOps.dispatchMouseDown = function(g, x, y, func) {
43 var px = Dygraph.findPosX(g.canvas_);
44 var py = Dygraph.findPosY(g.canvas_);
45
46 var pageX = px + g.toDomXCoord(x);
47 var pageY = py + g.toDomYCoord(y);
48
49 var evt = document.createEvent('MouseEvents');
50 evt.initMouseEvent(
51 'mousedown',
52 true, true, document.defaultView,
53 1, pageX, pageY, pageX, pageY,
54 false, false, false, false, 0, null);
55 if (func) {
56 func(evt);
57 }
58 g.canvas_.dispatchEvent(evt);
59 };
60
61 DygraphOps.dispatchMouseMove = function(g, x, y, func) {
62 var px = Dygraph.findPosX(g.canvas_);
63 var py = Dygraph.findPosY(g.canvas_);
64
65 var pageX = px + g.toDomXCoord(x);
66 var pageY = py + g.toDomYCoord(y);
67
68 var evt = document.createEvent('MouseEvents');
69 evt.initMouseEvent(
70 'mousemove',
71 true, true, document.defaultView,
72 0, pageX, pageY, pageX, pageY,
73 false, false, false, false, 0, null);
74 if (func) {
75 func(evt);
76 }
77 g.canvas_.dispatchEvent(evt);
78 };
79
80 DygraphOps.dispatchMouseUp = function(g, x, y, func) {
81 var px = Dygraph.findPosX(g.canvas_);
82 var py = Dygraph.findPosY(g.canvas_);
83
84 var pageX = px + g.toDomXCoord(x);
85 var pageY = py + g.toDomYCoord(y);
86
87 var evt = document.createEvent('MouseEvents');
88 evt.initMouseEvent(
89 'mouseup',
90 true, true, document.defaultView,
91 0, pageX, pageY, pageX, pageY,
92 false, false, false, false, 0, null);
93 if (func) {
94 func(evt);
95 }
96 g.canvas_.dispatchEvent(evt);
97 };