This is a specific sub-module of the Dojo DnD system that makes a DOM node moveable using the mouse.
Constructor takes the node to make moveable and an optional object of options which can have the following fields:
| handle | DOMNode or ID of a node that will be used as a handle for this Moveable |
| delay | If > 0, the move won't be initiated unless mouse drags more than delay pixels |
| skip | If true, dragging on form elements won't trigger the move. |
| mover | Constructor for a custom dojo.dnd.Mover. Default mover directly moves the node but you might want to clone it instead of moving the original |
You can connect to the following events of a Moveable:
require(["dojo/dnd/Moveable", "dojo/dom", "dojo/on", "dojo/domReady!"],
function(Moveable, dom, on){
on(dom.byId("doIt"), "click", function(){
var dnd = new Moveable(dom.byId("dndOne"));
});
});
<div id="dndArea">
<div id="dndOne">Hi, I am moveable when you want to.</div>
</div>
<p><button id="doIt" type="button">Make moveable</button></p>
#dndOne {
width: 100px;
height: 100px;
padding: 10px;
border: 1px solid #000;
background: red;
}
#dndArea {
height: 200px;
padding: 10px;
border: 1px solid #000;
}
Here is an example of a custom mover allowing to move an object with 5 pixels steps when Ctrl key is pressed while dragging:
require(["dojo/_base/declare", "dojo/_base/event", "dojo/dnd/Mover", "dojo/dnd/autoScroll"],
function(declare, event, Mover, autoScroll, dom))
var StepMover = declare([Mover], {
onMouseMove: function(e){
autoScroll(e);
var m = this.marginBox;
if(e.ctrlKey){
this.host.onMove(this, {l: parseInt((m.l + e.pageX) / 5) * 5, t: parseInt((m.t + e.pageY) / 5) * 5});
}else{
this.host.onMove(this, {l: m.l + e.pageX, t: m.t + e.pageY});
}
event.stop(e);
}
});
//Create your "Moveable" as
var myMoveable = new Moveable("aNode", {
mover: StepMover
});
});