Skip to content

Commit e9f8ac4

Browse files
committed
Shapes: add startDrag, moveShape and endDrag
1 parent 9905594 commit e9f8ac4

File tree

1 file changed

+57
-50
lines changed

1 file changed

+57
-50
lines changed

src/components/shapes/index.js

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -358,65 +358,72 @@ function setupDragElement(gd, shapePath, shapeOptions, index) {
358358

359359
var dragOptions = {
360360
element: shapePath.node(),
361-
prepFn: function() {
362-
setCursor(shapePath, 'move');
361+
prepFn: startDrag,
362+
doneFn: endDrag
363+
};
363364

364-
xa = Axes.getFromId(gd, shapeOptions.xref);
365-
ya = Axes.getFromId(gd, shapeOptions.yref);
365+
dragElement.init(dragOptions);
366366

367-
x2p = getDataToPixel(gd, xa);
368-
y2p = getDataToPixel(gd, ya, true);
369-
p2x = getPixelToData(gd, xa);
370-
p2y = getPixelToData(gd, ya, true);
367+
function startDrag() {
368+
setCursor(shapePath, 'move');
371369

372-
var astr = 'shapes[' + index + ']';
373-
if(shapeOptions.type === 'path') {
374-
pathIn = shapeOptions.path;
375-
astrPath = astr + '.path';
376-
}
377-
else {
378-
x0 = x2p(shapeOptions.x0);
379-
y0 = y2p(shapeOptions.y0);
380-
x1 = x2p(shapeOptions.x1);
381-
y1 = y2p(shapeOptions.y1);
382-
383-
astrX0 = astr + '.x0';
384-
astrY0 = astr + '.y0';
385-
astrX1 = astr + '.x1';
386-
astrY1 = astr + '.y1';
387-
}
370+
xa = Axes.getFromId(gd, shapeOptions.xref);
371+
ya = Axes.getFromId(gd, shapeOptions.yref);
388372

389-
update = {};
390-
},
391-
moveFn: function(dx, dy) {
392-
if(shapeOptions.type === 'path') {
393-
var moveX = function moveX(x) { return p2x(x2p(x) + dx); };
394-
if(xa && xa.type === 'date') moveX = encodeDate(moveX);
373+
x2p = getDataToPixel(gd, xa);
374+
y2p = getDataToPixel(gd, ya, true);
375+
p2x = getPixelToData(gd, xa);
376+
p2y = getPixelToData(gd, ya, true);
395377

396-
var moveY = function moveY(y) { return p2y(y2p(y) + dy); };
397-
if(ya && ya.type === 'date') moveY = encodeDate(moveY);
378+
var astr = 'shapes[' + index + ']';
379+
if(shapeOptions.type === 'path') {
380+
pathIn = shapeOptions.path;
381+
astrPath = astr + '.path';
382+
}
383+
else {
384+
x0 = x2p(shapeOptions.x0);
385+
y0 = y2p(shapeOptions.y0);
386+
x1 = x2p(shapeOptions.x1);
387+
y1 = y2p(shapeOptions.y1);
388+
389+
astrX0 = astr + '.x0';
390+
astrY0 = astr + '.y0';
391+
astrX1 = astr + '.x1';
392+
astrY1 = astr + '.y1';
393+
}
398394

399-
shapeOptions.path = movePath(pathIn, moveX, moveY);
400-
update[astrPath] = shapeOptions.path;
401-
}
402-
else {
403-
update[astrX0] = shapeOptions.x0 = p2x(x0 + dx);
404-
update[astrY0] = shapeOptions.y0 = p2y(y0 + dy);
405-
update[astrX1] = shapeOptions.x1 = p2x(x1 + dx);
406-
update[astrY1] = shapeOptions.y1 = p2y(y1 + dy);
407-
}
395+
update = {};
408396

409-
shapePath.attr('d', getPathString(gd, shapeOptions));
410-
},
411-
doneFn: function(dragged) {
412-
setCursor(shapePath);
413-
if(dragged) {
414-
Plotly.relayout(gd, update);
415-
}
397+
dragOptions.moveFn = moveShape;
398+
}
399+
400+
function endDrag(dragged) {
401+
setCursor(shapePath);
402+
if(dragged) {
403+
Plotly.relayout(gd, update);
416404
}
417-
};
405+
}
418406

419-
dragElement.init(dragOptions);
407+
function moveShape(dx, dy) {
408+
if(shapeOptions.type === 'path') {
409+
var moveX = function moveX(x) { return p2x(x2p(x) + dx); };
410+
if(xa && xa.type === 'date') moveX = encodeDate(moveX);
411+
412+
var moveY = function moveY(y) { return p2y(y2p(y) + dy); };
413+
if(ya && ya.type === 'date') moveY = encodeDate(moveY);
414+
415+
shapeOptions.path = movePath(pathIn, moveX, moveY);
416+
update[astrPath] = shapeOptions.path;
417+
}
418+
else {
419+
update[astrX0] = shapeOptions.x0 = p2x(x0 + dx);
420+
update[astrY0] = shapeOptions.y0 = p2y(y0 + dy);
421+
update[astrX1] = shapeOptions.x1 = p2x(x1 + dx);
422+
update[astrY1] = shapeOptions.y1 = p2y(y1 + dy);
423+
}
424+
425+
shapePath.attr('d', getPathString(gd, shapeOptions));
426+
}
420427
}
421428

422429
function getShapeLayer(gd, index) {

0 commit comments

Comments
 (0)