Drag = (callback) => {
const reset = {
event: null,
xo: 0,
yo: 0,
x: 0,
y: 0,
dx: 0,
dy: 0,
};
const context = { node: undefined };
const behaviour = {
context,
on: node => {
context.node = node;
on(node, behaviour.start);
return behaviour;
},
off: node => {
context.node = undefined;
off(node, behaviour.start);
off(node, behaviour.drag);
return behaviour;
},
start: {
mousedown: event => {
Object.assign(context, reset);
off(context.node, behaviour["start"]);
context.xo = event.pageX;
context.yo = event.pageY;
callback && callback(event, context, "dragstart");
on(context.overlay||window.document.body, behaviour["drag"]);
}
},
drag: {
mousemove: event => {
context.x = event.pageX;
context.y = event.pageY;
context.dx = context.x - context.xo;
context.dy = context.y - context.yo;
callback && callback(event, context, "drag");
},
mouseup: event => {
callback && callback(event, context, "dragend");
off(context.overlay||window.document.body, behaviour["drag"]);
on(context.node, behaviour["start"]);
}
}
};
return behaviour;
}