diff options
Diffstat (limited to 'lib/dojo/dnd/Mover.js')
-rw-r--r-- | lib/dojo/dnd/Mover.js | 160 |
1 files changed, 106 insertions, 54 deletions
diff --git a/lib/dojo/dnd/Mover.js b/lib/dojo/dnd/Mover.js index 6ea793efc..cdb995c08 100644 --- a/lib/dojo/dnd/Mover.js +++ b/lib/dojo/dnd/Mover.js @@ -5,61 +5,113 @@ */ -if(!dojo._hasResource["dojo.dnd.Mover"]){ -dojo._hasResource["dojo.dnd.Mover"]=true; +if(!dojo._hasResource["dojo.dnd.Mover"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojo.dnd.Mover"] = true; dojo.provide("dojo.dnd.Mover"); + dojo.require("dojo.dnd.common"); dojo.require("dojo.dnd.autoscroll"); -dojo.declare("dojo.dnd.Mover",null,{constructor:function(_1,e,_2){ -this.node=dojo.byId(_1); -this.marginBox={l:e.pageX,t:e.pageY}; -this.mouseButton=e.button; -var h=this.host=_2,d=_1.ownerDocument,_3=dojo.connect(d,"onmousemove",this,"onFirstMove"); -this.events=[dojo.connect(d,"onmousemove",this,"onMouseMove"),dojo.connect(d,"onmouseup",this,"onMouseUp"),dojo.connect(d,"ondragstart",dojo.stopEvent),dojo.connect(d.body,"onselectstart",dojo.stopEvent),_3]; -if(h&&h.onMoveStart){ -h.onMoveStart(this); -} -},onMouseMove:function(e){ -dojo.dnd.autoScroll(e); -var m=this.marginBox; -this.host.onMove(this,{l:m.l+e.pageX,t:m.t+e.pageY},e); -dojo.stopEvent(e); -},onMouseUp:function(e){ -if(dojo.isWebKit&&dojo.isMac&&this.mouseButton==2?e.button==0:this.mouseButton==e.button){ -this.destroy(); -} -dojo.stopEvent(e); -},onFirstMove:function(e){ -var s=this.node.style,l,t,h=this.host; -switch(s.position){ -case "relative": -case "absolute": -l=Math.round(parseFloat(s.left))||0; -t=Math.round(parseFloat(s.top))||0; -break; -default: -s.position="absolute"; -var m=dojo.marginBox(this.node); -var b=dojo.doc.body; -var bs=dojo.getComputedStyle(b); -var bm=dojo._getMarginBox(b,bs); -var bc=dojo._getContentBox(b,bs); -l=m.l-(bc.l-bm.l); -t=m.t-(bc.t-bm.t); -break; -} -this.marginBox.l=l-this.marginBox.l; -this.marginBox.t=t-this.marginBox.t; -if(h&&h.onFirstMove){ -h.onFirstMove(this,e); -} -dojo.disconnect(this.events.pop()); -},destroy:function(){ -dojo.forEach(this.events,dojo.disconnect); -var h=this.host; -if(h&&h.onMoveStop){ -h.onMoveStop(this); -} -this.events=this.node=this.host=null; -}}); + +dojo.declare("dojo.dnd.Mover", null, { + constructor: function(node, e, host){ + // summary: + // an object, which makes a node follow the mouse. + // Used as a default mover, and as a base class for custom movers. + // node: Node + // a node (or node's id) to be moved + // e: Event + // a mouse event, which started the move; + // only pageX and pageY properties are used + // host: Object? + // object which implements the functionality of the move, + // and defines proper events (onMoveStart and onMoveStop) + this.node = dojo.byId(node); + this.marginBox = {l: e.pageX, t: e.pageY}; + this.mouseButton = e.button; + var h = this.host = host, d = node.ownerDocument, + firstEvent = dojo.connect(d, "onmousemove", this, "onFirstMove"); + this.events = [ + dojo.connect(d, "onmousemove", this, "onMouseMove"), + dojo.connect(d, "onmouseup", this, "onMouseUp"), + // cancel text selection and text dragging + dojo.connect(d, "ondragstart", dojo.stopEvent), + dojo.connect(d.body, "onselectstart", dojo.stopEvent), + firstEvent + ]; + // notify that the move has started + if(h && h.onMoveStart){ + h.onMoveStart(this); + } + }, + // mouse event processors + onMouseMove: function(e){ + // summary: + // event processor for onmousemove + // e: Event + // mouse event + dojo.dnd.autoScroll(e); + var m = this.marginBox; + this.host.onMove(this, {l: m.l + e.pageX, t: m.t + e.pageY}, e); + dojo.stopEvent(e); + }, + onMouseUp: function(e){ + if(dojo.isWebKit && dojo.isMac && this.mouseButton == 2 ? + e.button == 0 : this.mouseButton == e.button){ + this.destroy(); + } + dojo.stopEvent(e); + }, + // utilities + onFirstMove: function(e){ + // summary: + // makes the node absolute; it is meant to be called only once. + // relative and absolutely positioned nodes are assumed to use pixel units + var s = this.node.style, l, t, h = this.host; + switch(s.position){ + case "relative": + case "absolute": + // assume that left and top values are in pixels already + l = Math.round(parseFloat(s.left)) || 0; + t = Math.round(parseFloat(s.top)) || 0; + break; + default: + s.position = "absolute"; // enforcing the absolute mode + var m = dojo.marginBox(this.node); + // event.pageX/pageY (which we used to generate the initial + // margin box) includes padding and margin set on the body. + // However, setting the node's position to absolute and then + // doing dojo.marginBox on it *doesn't* take that additional + // space into account - so we need to subtract the combined + // padding and margin. We use getComputedStyle and + // _getMarginBox/_getContentBox to avoid the extra lookup of + // the computed style. + var b = dojo.doc.body; + var bs = dojo.getComputedStyle(b); + var bm = dojo._getMarginBox(b, bs); + var bc = dojo._getContentBox(b, bs); + l = m.l - (bc.l - bm.l); + t = m.t - (bc.t - bm.t); + break; + } + this.marginBox.l = l - this.marginBox.l; + this.marginBox.t = t - this.marginBox.t; + if(h && h.onFirstMove){ + h.onFirstMove(this, e); + } + dojo.disconnect(this.events.pop()); + }, + destroy: function(){ + // summary: + // stops the move, deletes all references, so the object can be garbage-collected + dojo.forEach(this.events, dojo.disconnect); + // undo global settings + var h = this.host; + if(h && h.onMoveStop){ + h.onMoveStop(this); + } + // destroy objects + this.events = this.node = this.host = null; + } +}); + } |