X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2FDygraphOps.js;h=d792259839cdfe5747d2d838ee2168e369630775;hb=e26b71566419e1c051f3fbd1f4f8f64b063a04c9;hp=2e9d754a642fff6aa171cd5d3246b9410e564e2f;hpb=72a74f044b9df59ef09e420f575e7080c7349fad;p=dygraphs.git diff --git a/auto_tests/tests/DygraphOps.js b/auto_tests/tests/DygraphOps.js index 2e9d754..d792259 100644 --- a/auto_tests/tests/DygraphOps.js +++ b/auto_tests/tests/DygraphOps.js @@ -1,4 +1,4 @@ -// Copyright (c) 2011 Google, Inc. +// Copyright (c) 2011 Google, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -26,72 +26,166 @@ */ var DygraphOps = {}; -DygraphOps.dispatchDoubleClick = function(g, func) { - var evt = document.createEvent('MouseEvents'); - evt.initMouseEvent( - 'dblclick', - true, true, document.defaultView, - 2, 0, 0, 0, 0, - false, false, false, false, 0, null); - if (func) { - func(evt); - } - g.canvas_.dispatchEvent(evt); +DygraphOps.defaultEvent_ = { + type : '', + canBubble : true, + cancelable : true, + view : document.defaultView, + detail : 0, + screenX : 0, + screenY : 0, + clientX : 0, + clientY : 0, + ctrlKey : false, + altKey : false, + shiftKey : false, + metaKey : false, + button : 0, + relatedTarget : null }; -DygraphOps.dispatchMouseDown = function(g, x, y, func) { - var px = Dygraph.findPosX(g.canvas_); - var py = Dygraph.findPosY(g.canvas_); - - var pageX = px + g.toDomXCoord(x); - var pageY = py + g.toDomYCoord(y); - - var evt = document.createEvent('MouseEvents'); - evt.initMouseEvent( - 'mousedown', - true, true, document.defaultView, - 1, pageX, pageY, pageX, pageY, - false, false, false, false, 0, null); - if (func) { - func(evt); +/** + * Create an event. Sets default event values except for special ones + * overridden by the 'custom' parameter. + * + * @param command the command to create. + * @param custom an associative array of event attributes and their new values. + */ +DygraphOps.createEvent = function(command, custom) { + + var copy = function(from, to) { + if (from != null) { + for (var prop in from) { + if(from.hasOwnProperty(prop)) { + to[prop] = from[prop]; + } + } + } } - g.canvas_.dispatchEvent(evt); + + var e = {}; + copy(DygraphOps.defaultEvent_, e); + copy(command, e); + copy(custom, e); + + var event = document.createEvent('MouseEvents'); + event.initMouseEvent( + e.type, + e.canBubble, + e.cancelable, + e.view, + e.detail, + e.screenX, + e.screenY, + e.clientX, + e.clientY, + e.ctrlKey, + e.altKey, + e.shiftKey, + e.metaKey, + e.button, + e.relatedTarget); + return event; +} + +/** + * Dispatch an event onto the graph's canvas. + */ +DygraphOps.dispatchCanvasEvent = function(g, event) { + g.canvas_.dispatchEvent(event); +} + +DygraphOps.dispatchDoubleClick = function(g, custom) { + var opts = { + type : 'dblclick', + detail : 2 + }; + var event = DygraphOps.createEvent(opts, custom); + DygraphOps.dispatchCanvasEvent(g, event); }; -DygraphOps.dispatchMouseMove = function(g, x, y, func) { - var px = Dygraph.findPosX(g.canvas_); - var py = Dygraph.findPosY(g.canvas_); - - var pageX = px + g.toDomXCoord(x); - var pageY = py + g.toDomYCoord(y); - - var evt = document.createEvent('MouseEvents'); - evt.initMouseEvent( - 'mousemove', - true, true, document.defaultView, - 0, pageX, pageY, pageX, pageY, - false, false, false, false, 0, null); - if (func) { - func(evt); - } - g.canvas_.dispatchEvent(evt); +DygraphOps.dispatchMouseDown_Point = function(g, x, y, custom) { + var pageX = Dygraph.findPosX(g.canvas_) + x; + var pageY = Dygraph.findPosY(g.canvas_) + y; + + var opts = { + type : 'mousedown', + detail : 1, + screenX : pageX, + screenY : pageY, + clientX : pageX, + clientY : pageY, + }; + + var event = DygraphOps.createEvent(opts, custom); + DygraphOps.dispatchCanvasEvent(g, event); +} + +DygraphOps.dispatchMouseMove_Point = function(g, x, y, custom) { + var pageX = Dygraph.findPosX(g.canvas_) + x; + var pageY = Dygraph.findPosY(g.canvas_) + y; + + var opts = { + type : 'mousemove', + screenX : pageX, + screenY : pageY, + clientX : pageX, + clientY : pageY, + }; + + var event = DygraphOps.createEvent(opts, custom); + DygraphOps.dispatchCanvasEvent(g, event); }; -DygraphOps.dispatchMouseUp = function(g, x, y, func) { - var px = Dygraph.findPosX(g.canvas_); - var py = Dygraph.findPosY(g.canvas_); - - var pageX = px + g.toDomXCoord(x); - var pageY = py + g.toDomYCoord(y); - - var evt = document.createEvent('MouseEvents'); - evt.initMouseEvent( - 'mouseup', - true, true, document.defaultView, - 0, pageX, pageY, pageX, pageY, - false, false, false, false, 0, null); - if (func) { - func(evt); - } - g.canvas_.dispatchEvent(evt); +DygraphOps.dispatchMouseUp_Point = function(g, x, y, custom) { + var pageX = Dygraph.findPosX(g.canvas_) + x; + var pageY = Dygraph.findPosY(g.canvas_) + y; + + var opts = { + type : 'mouseup', + screenX : pageX, + screenY : pageY, + clientX : pageX, + clientY : pageY, + }; + + var event = DygraphOps.createEvent(opts, custom); + DygraphOps.dispatchCanvasEvent(g, event); +}; + +/** + * Dispatches a mouse down using the graph's data coordinate system. + * (The y value mapped to the first axis.) + */ +DygraphOps.dispatchMouseDown = function(g, x, y, custom) { + DygraphOps.dispatchMouseDown_Point( + g, + g.toDomXCoord(x), + g.toDomYCoord(y), + custom); }; + +/** + * Dispatches a mouse move using the graph's data coordinate system. + * (The y value mapped to the first axis.) + */ +DygraphOps.dispatchMouseMove = function(g, x, y, custom) { + DygraphOps.dispatchMouseMove_Point( + g, + g.toDomXCoord(x), + g.toDomYCoord(y), + custom); +}; + +/** + * Dispatches a mouse up using the graph's data coordinate system. + * (The y value mapped to the first axis.) + */ +DygraphOps.dispatchMouseUp = function(g, x, y, custom) { + DygraphOps.dispatchMouseUp_Point( + g, + g.toDomXCoord(x), + g.toDomYCoord(y), + custom); +}; +