Skip to content

Commit 9905594

Browse files
committed
Shapes: add function setupDragElement(node)
1 parent d027674 commit 9905594

File tree

1 file changed

+70
-65
lines changed

1 file changed

+70
-65
lines changed

src/components/shapes/index.js

Lines changed: 70 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -346,74 +346,79 @@ function updateShape(gd, index, opt, value) {
346346
'clip' + gd._fullLayout._uid + clipAxes);
347347
}
348348

349-
if(!gd._context.editable) return;
350-
351-
var update;
352-
var x0, y0, x1, y1, astrX0, astrY0, astrX1, astrY1;
353-
var pathIn, astrPath;
354-
var xa, ya, x2p, y2p, p2x, p2y;
355-
dragElement.init({
356-
element: path.node(),
357-
prepFn: function() {
358-
setCursor(path, 'move');
359-
360-
xa = Axes.getFromId(gd, options.xref);
361-
ya = Axes.getFromId(gd, options.yref);
362-
363-
x2p = getDataToPixel(gd, xa);
364-
y2p = getDataToPixel(gd, ya, true);
365-
p2x = getPixelToData(gd, xa);
366-
p2y = getPixelToData(gd, ya, true);
367-
368-
var astr = 'shapes[' + index + ']';
369-
if(options.type === 'path') {
370-
pathIn = options.path;
371-
astrPath = astr + '.path';
372-
}
373-
else {
374-
x0 = x2p(options.x0);
375-
y0 = y2p(options.y0);
376-
x1 = x2p(options.x1);
377-
y1 = y2p(options.y1);
378-
379-
astrX0 = astr + '.x0';
380-
astrY0 = astr + '.y0';
381-
astrX1 = astr + '.x1';
382-
astrY1 = astr + '.y1';
383-
}
384-
385-
update = {};
386-
},
387-
moveFn: function(dx, dy) {
388-
if(options.type === 'path') {
389-
var moveX = function moveX(x) { return p2x(x2p(x) + dx); };
390-
if(xa && xa.type === 'date') moveX = encodeDate(moveX);
391-
392-
var moveY = function moveY(y) { return p2y(y2p(y) + dy); };
393-
if(ya && ya.type === 'date') moveY = encodeDate(moveY);
394-
395-
options.path = movePath(pathIn, moveX, moveY);
396-
update[astrPath] = options.path;
397-
}
398-
else {
399-
update[astrX0] = options.x0 = p2x(x0 + dx);
400-
update[astrY0] = options.y0 = p2y(y0 + dy);
401-
update[astrX1] = options.x1 = p2x(x1 + dx);
402-
update[astrY1] = options.y1 = p2y(y1 + dy);
403-
}
404-
405-
path.attr('d', getPathString(gd, options));
406-
},
407-
doneFn: function(dragged) {
408-
setCursor(path);
409-
if(dragged) {
410-
Plotly.relayout(gd, update);
411-
}
412-
}
413-
});
349+
if(gd._context.editable) setupDragElement(gd, path, options, index);
414350
}
415351
}
416352

353+
function setupDragElement(gd, shapePath, shapeOptions, index) {
354+
var update;
355+
var x0, y0, x1, y1, astrX0, astrY0, astrX1, astrY1;
356+
var pathIn, astrPath;
357+
var xa, ya, x2p, y2p, p2x, p2y;
358+
359+
var dragOptions = {
360+
element: shapePath.node(),
361+
prepFn: function() {
362+
setCursor(shapePath, 'move');
363+
364+
xa = Axes.getFromId(gd, shapeOptions.xref);
365+
ya = Axes.getFromId(gd, shapeOptions.yref);
366+
367+
x2p = getDataToPixel(gd, xa);
368+
y2p = getDataToPixel(gd, ya, true);
369+
p2x = getPixelToData(gd, xa);
370+
p2y = getPixelToData(gd, ya, true);
371+
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+
}
388+
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);
395+
396+
var moveY = function moveY(y) { return p2y(y2p(y) + dy); };
397+
if(ya && ya.type === 'date') moveY = encodeDate(moveY);
398+
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+
}
408+
409+
shapePath.attr('d', getPathString(gd, shapeOptions));
410+
},
411+
doneFn: function(dragged) {
412+
setCursor(shapePath);
413+
if(dragged) {
414+
Plotly.relayout(gd, update);
415+
}
416+
}
417+
};
418+
419+
dragElement.init(dragOptions);
420+
}
421+
417422
function getShapeLayer(gd, index) {
418423
var shape = gd._fullLayout.shapes[index],
419424
shapeLayer = gd._fullLayout._shapeUpperLayer;

0 commit comments

Comments
 (0)