| 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 | import * as utils from '../../src/dygraph-utils'; |
| 22 | |
| 23 | /** |
| 24 | * @fileoverview Utility functions for Dygraphs. |
| 25 | * |
| 26 | * @author konigsberg@google.com (Robert Konigsberg) |
| 27 | */ |
| 28 | var DygraphOps = {}; |
| 29 | |
| 30 | DygraphOps.defaultEvent_ = { |
| 31 | type : '', |
| 32 | canBubble : true, |
| 33 | cancelable : true, |
| 34 | view : document.defaultView, |
| 35 | detail : 0, |
| 36 | screenX : 0, |
| 37 | screenY : 0, |
| 38 | clientX : 0, |
| 39 | clientY : 0, |
| 40 | ctrlKey : false, |
| 41 | altKey : false, |
| 42 | shiftKey : false, |
| 43 | metaKey : false, |
| 44 | button : 0, |
| 45 | relatedTarget : null |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Create an event. Sets default event values except for special ones |
| 50 | * overridden by the 'custom' parameter. |
| 51 | * |
| 52 | * @param command the command to create. |
| 53 | * @param custom an associative array of event attributes and their new values. |
| 54 | */ |
| 55 | DygraphOps.createEvent = function(command, custom) { |
| 56 | |
| 57 | var copy = function(from, to) { |
| 58 | if (from != null) { |
| 59 | for (var prop in from) { |
| 60 | if(from.hasOwnProperty(prop)) { |
| 61 | to[prop] = from[prop]; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | var e = {}; |
| 68 | copy(DygraphOps.defaultEvent_, e); |
| 69 | copy(command, e); |
| 70 | copy(custom, e); |
| 71 | |
| 72 | var event = document.createEvent('MouseEvents'); |
| 73 | event.initMouseEvent( |
| 74 | e.type, |
| 75 | e.canBubble, |
| 76 | e.cancelable, |
| 77 | e.view, |
| 78 | e.detail, |
| 79 | e.screenX, |
| 80 | e.screenY, |
| 81 | e.clientX, |
| 82 | e.clientY, |
| 83 | e.ctrlKey, |
| 84 | e.altKey, |
| 85 | e.shiftKey, |
| 86 | e.metaKey, |
| 87 | e.button, |
| 88 | e.relatedTarget); |
| 89 | return event; |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * Dispatch an event onto the graph's canvas. |
| 94 | */ |
| 95 | DygraphOps.dispatchCanvasEvent = function(g, event) { |
| 96 | g.canvas_.dispatchEvent(event); |
| 97 | }; |
| 98 | |
| 99 | DygraphOps.dispatchDoubleClick = function(g, custom) { |
| 100 | var opts = { |
| 101 | type : 'dblclick', |
| 102 | detail : 2 |
| 103 | }; |
| 104 | var event = DygraphOps.createEvent(opts, custom); |
| 105 | DygraphOps.dispatchCanvasEvent(g, event); |
| 106 | }; |
| 107 | |
| 108 | /* |
| 109 | * Create an 'opts' argument which can be passed to createEvent that contains |
| 110 | * type, screenX, screenY, clientX, clientY. |
| 111 | */ |
| 112 | DygraphOps.createOptsForPoint_ = function(g, type, x, y) { |
| 113 | var pos = utils.findPos(g.canvas_); |
| 114 | var pageX = pos.x + x; |
| 115 | var pageY = pos.y + y; |
| 116 | |
| 117 | return { |
| 118 | type : type, |
| 119 | screenX : pageX, |
| 120 | screenY : pageY, |
| 121 | clientX : pageX, |
| 122 | clientY : pageY, |
| 123 | }; |
| 124 | }; |
| 125 | |
| 126 | DygraphOps.dispatchMouseDown_Point = function(g, x, y, custom) { |
| 127 | var opts = DygraphOps.createOptsForPoint_(g, 'mousedown', x, y); |
| 128 | opts.detail = 1; |
| 129 | var event = DygraphOps.createEvent(opts, custom); |
| 130 | DygraphOps.dispatchCanvasEvent(g, event); |
| 131 | }; |
| 132 | |
| 133 | DygraphOps.dispatchMouseMove_Point = function(g, x, y, custom) { |
| 134 | var opts = DygraphOps.createOptsForPoint_(g, 'mousemove', x, y); |
| 135 | var event = DygraphOps.createEvent(opts, custom); |
| 136 | DygraphOps.dispatchCanvasEvent(g, event); |
| 137 | }; |
| 138 | |
| 139 | DygraphOps.dispatchMouseUp_Point = function(g, x, y, custom) { |
| 140 | var opts = DygraphOps.createOptsForPoint_(g, 'mouseup', x, y); |
| 141 | var event = DygraphOps.createEvent(opts, custom); |
| 142 | DygraphOps.dispatchCanvasEvent(g, event); |
| 143 | }; |
| 144 | |
| 145 | DygraphOps.dispatchMouseOver_Point = function(g, x, y, custom) { |
| 146 | var opts = DygraphOps.createOptsForPoint_(g, 'mouseover', x, y); |
| 147 | var event = DygraphOps.createEvent(opts, custom); |
| 148 | DygraphOps.dispatchCanvasEvent(g, event); |
| 149 | }; |
| 150 | |
| 151 | DygraphOps.dispatchMouseOut_Point = function(g, x, y, custom) { |
| 152 | var opts = DygraphOps.createOptsForPoint_(g, 'mouseout', x, y); |
| 153 | var event = DygraphOps.createEvent(opts, custom); |
| 154 | DygraphOps.dispatchCanvasEvent(g, event); |
| 155 | }; |
| 156 | |
| 157 | /** |
| 158 | * Dispatches a mouse down using the graph's data coordinate system. |
| 159 | * (The y value mapped to the first axis.) |
| 160 | */ |
| 161 | DygraphOps.dispatchMouseDown = function(g, x, y, custom) { |
| 162 | DygraphOps.dispatchMouseDown_Point( |
| 163 | g, |
| 164 | g.toDomXCoord(x), |
| 165 | g.toDomYCoord(y), |
| 166 | custom); |
| 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * Dispatches a mouse move using the graph's data coordinate system. |
| 171 | * (The y value mapped to the first axis.) |
| 172 | */ |
| 173 | DygraphOps.dispatchMouseMove = function(g, x, y, custom) { |
| 174 | DygraphOps.dispatchMouseMove_Point( |
| 175 | g, |
| 176 | g.toDomXCoord(x), |
| 177 | g.toDomYCoord(y), |
| 178 | custom); |
| 179 | }; |
| 180 | |
| 181 | /** |
| 182 | * Dispatches a mouse up using the graph's data coordinate system. |
| 183 | * (The y value mapped to the first axis.) |
| 184 | */ |
| 185 | DygraphOps.dispatchMouseUp = function(g, x, y, custom) { |
| 186 | DygraphOps.dispatchMouseUp_Point( |
| 187 | g, |
| 188 | g.toDomXCoord(x), |
| 189 | g.toDomYCoord(y), |
| 190 | custom); |
| 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * Dispatches a mouse over using the graph's data coordinate system. |
| 195 | * (The y value mapped to the first axis.) |
| 196 | */ |
| 197 | DygraphOps.dispatchMouseOver = function(g, x, y, custom) { |
| 198 | DygraphOps.dispatchMouseOver_Point( |
| 199 | g, |
| 200 | g.toDomXCoord(x), |
| 201 | g.toDomYCoord(y), |
| 202 | custom); |
| 203 | }; |
| 204 | |
| 205 | /** |
| 206 | * Dispatches a mouse out using the graph's data coordinate system. |
| 207 | * (The y value mapped to the first axis.) |
| 208 | */ |
| 209 | DygraphOps.dispatchMouseOut = function(g, x, y, custom) { |
| 210 | DygraphOps.dispatchMouseOut_Point( |
| 211 | g, |
| 212 | g.toDomXCoord(x), |
| 213 | g.toDomYCoord(y), |
| 214 | custom); |
| 215 | }; |
| 216 | |
| 217 | |
| 218 | export default DygraphOps; |