|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var Plotly = require('../../../lib/index'); |
| 4 | +var d3SelectAll = require('../../strict-d3').selectAll; |
| 5 | +var drag = require('../assets/drag'); |
| 6 | + |
| 7 | +module.exports.shapeColors = [ |
| 8 | + 'red', |
| 9 | + 'green', |
| 10 | + 'blue', |
| 11 | + 'yellow', |
| 12 | + 'orange', |
| 13 | +]; |
| 14 | + |
| 15 | +module.exports.shapeColorTab = { |
| 16 | + red: 'rgb(255, 0, 0)', |
| 17 | + green: 'rgb(0, 128, 0)', |
| 18 | + blue: 'rgb(0, 0, 255)', |
| 19 | + yellow: 'rgb(255, 255, 0)', |
| 20 | + orange: 'rgb(255, 165, 0)' |
| 21 | +}; |
| 22 | + |
| 23 | +function createDesc(xcats, ycats) { |
| 24 | + return 'x is specified as ' + xcats + ', y is specified as ' + ycats; |
| 25 | +} |
| 26 | + |
| 27 | +module.exports.shapeColorTestDesc = { |
| 28 | + red: createDesc('indices', 'numeric strings'), |
| 29 | + green: createDesc('indices', 'numeric string categories, one of which is missing'), |
| 30 | + blue: createDesc('string categories', 'indices'), |
| 31 | + yellow: createDesc('numeric string categories, one of which is missing', 'indices'), |
| 32 | + orange: createDesc('(path) indices', '(path) indices'), |
| 33 | +}; |
| 34 | + |
| 35 | +function keepMthOfNShapes(shapes, nshapes, m) { |
| 36 | + var oneShape = shapes[m]; |
| 37 | + var tailShapes = shapes.slice(nshapes); |
| 38 | + return [oneShape].concat(tailShapes); |
| 39 | +} |
| 40 | + |
| 41 | +module.exports.plotMthShape = function(gd, m) { |
| 42 | + var mock; |
| 43 | + mock = require('../../image/mocks/numeric-string-categories.json'); |
| 44 | + mock.config = {editable: true}; |
| 45 | + mock.layout.shapes = keepMthOfNShapes(mock.layout.shapes, mock.layout.shapes.length, m); |
| 46 | + var promise = Plotly.newPlot(gd, mock); |
| 47 | + return promise; |
| 48 | +}; |
| 49 | + |
| 50 | +module.exports.plotColoredShape = function(gd, color) { |
| 51 | + var mock = require('../../image/mocks/numeric-string-categories.json'); |
| 52 | + var m; |
| 53 | + mock.layout.shapes.forEach(function(e, i) { |
| 54 | + if(e && e.line && e.line.color === color) { m = i; } |
| 55 | + }); |
| 56 | + return module.exports.plotMthShape(gd, m); |
| 57 | +}; |
| 58 | + |
| 59 | +module.exports.getShapeByColor = function(color) { |
| 60 | + var rgbColor = module.exports.shapeColorTab[color]; |
| 61 | + var shapeNode = d3SelectAll('.shapelayer path[style*="stroke: ' + rgbColor + '"]').node(); |
| 62 | + return shapeNode; |
| 63 | +}; |
| 64 | + |
| 65 | +module.exports.dragShapeByColor = function(color, dx, dy) { |
| 66 | + var shapeNode = module.exports.getShapeByColor(color); |
| 67 | + return drag({node: shapeNode, dpos: [dx, dy]}); |
| 68 | +}; |
| 69 | + |
| 70 | +module.exports.bboxChange = function(fromBBox, toBBox) { |
| 71 | + var ret = { |
| 72 | + dx: toBBox.x - fromBBox.x, |
| 73 | + dy: toBBox.y - fromBBox.y, |
| 74 | + dwidth: toBBox.width - fromBBox.width, |
| 75 | + dheight: toBBox.height - fromBBox.height, |
| 76 | + }; |
| 77 | + return ret; |
| 78 | +}; |
| 79 | + |
| 80 | +// Returns a promise that resolves with an object that looks like: |
| 81 | +// { |
| 82 | +// dx: ... // change in horizontal shape position |
| 83 | +// dy: ... // change in vertical shape position |
| 84 | +// dwidth: ... // change in shape width |
| 85 | +// dheight: ... // change in shape height |
| 86 | +// } |
| 87 | +module.exports.testDragShapeByColor = function(gd, shapeColor, dx, dy) { |
| 88 | + var startBBox, endBBox; |
| 89 | + // Plot the shape with the specified color |
| 90 | + var promise = module.exports.plotColoredShape(gd, shapeColor).then(function() { |
| 91 | + // get the bounding box of the shape with the specified color |
| 92 | + var shapeNode = module.exports.getShapeByColor(shapeColor); |
| 93 | + startBBox = shapeNode.getBoundingClientRect(); |
| 94 | + // drag that shape |
| 95 | + return module.exports.dragShapeByColor(shapeColor, dx, dy); |
| 96 | + }).then(function() { |
| 97 | + // get the new bounding box of the shape |
| 98 | + var shapeNode = module.exports.getShapeByColor(shapeColor); |
| 99 | + endBBox = shapeNode.getBoundingClientRect(); |
| 100 | + return module.exports.bboxChange(startBBox, endBBox); |
| 101 | + }); |
| 102 | + return promise; |
| 103 | +}; |
| 104 | + |
| 105 | +module.exports.d3SelectAll = d3SelectAll; |
0 commit comments