[483] | 1 | define(["dojo/_base/lang","dojo/_base/declare","dojo/_base/array","dojo/_base/event","dojo/topic","dojo/touch", |
---|
| 2 | "dojo/dom-class","dojo/_base/window","./Mover"], |
---|
| 3 | function(lang,declare,arr,event,topic,touch,domClass,win,Mover){ |
---|
| 4 | |
---|
| 5 | /*===== |
---|
| 6 | var __MoveableCtorArgs = declare("dojox.gfx.__MoveableCtorArgs", null, { |
---|
| 7 | // summary: |
---|
| 8 | // The arguments used for dojox/gfx/Moveable constructor. |
---|
| 9 | |
---|
| 10 | // delay: Number |
---|
| 11 | // delay move by this number of pixels |
---|
| 12 | delay:0, |
---|
| 13 | |
---|
| 14 | // mover: Object |
---|
| 15 | // a constructor of custom Mover |
---|
| 16 | mover:Mover |
---|
| 17 | }); |
---|
| 18 | =====*/ |
---|
| 19 | |
---|
| 20 | return declare("dojox.gfx.Moveable", null, { |
---|
| 21 | constructor: function(shape, params){ |
---|
| 22 | // summary: |
---|
| 23 | // an object, which makes a shape moveable |
---|
| 24 | // shape: dojox/gfx.Shape |
---|
| 25 | // a shape object to be moved. |
---|
| 26 | // params: __MoveableCtorArgs |
---|
| 27 | // an optional configuration object. |
---|
| 28 | |
---|
| 29 | this.shape = shape; |
---|
| 30 | this.delay = (params && params.delay > 0) ? params.delay : 0; |
---|
| 31 | this.mover = (params && params.mover) ? params.mover : Mover; |
---|
| 32 | this.events = [ |
---|
| 33 | this.shape.on(touch.press, lang.hitch(this, "onMouseDown")) |
---|
| 34 | // cancel text selection and text dragging |
---|
| 35 | //, dojo.connect(this.handle, "ondragstart", dojo, "stopEvent") |
---|
| 36 | //, dojo.connect(this.handle, "onselectstart", dojo, "stopEvent") |
---|
| 37 | ]; |
---|
| 38 | }, |
---|
| 39 | |
---|
| 40 | // methods |
---|
| 41 | destroy: function(){ |
---|
| 42 | // summary: |
---|
| 43 | // stops watching for possible move, deletes all references, so the object can be garbage-collected |
---|
| 44 | arr.forEach(this.events, function(handle){ |
---|
| 45 | handle.remove(); |
---|
| 46 | }); |
---|
| 47 | this.events = this.shape = null; |
---|
| 48 | }, |
---|
| 49 | |
---|
| 50 | // mouse event processors |
---|
| 51 | onMouseDown: function(e){ |
---|
| 52 | // summary: |
---|
| 53 | // event processor for onmousedown, creates a Mover for the shape |
---|
| 54 | // e: Event |
---|
| 55 | // mouse event |
---|
| 56 | if(this.delay){ |
---|
| 57 | this.events.push( |
---|
| 58 | this.shape.on(touch.move, lang.hitch(this, "onMouseMove")), |
---|
| 59 | this.shape.on(touch.release, lang.hitch(this, "onMouseUp"))); |
---|
| 60 | this._lastX = e.clientX; |
---|
| 61 | this._lastY = e.clientY; |
---|
| 62 | }else{ |
---|
| 63 | new this.mover(this.shape, e, this); |
---|
| 64 | } |
---|
| 65 | event.stop(e); |
---|
| 66 | }, |
---|
| 67 | onMouseMove: function(e){ |
---|
| 68 | // summary: |
---|
| 69 | // event processor for onmousemove, used only for delayed drags |
---|
| 70 | // e: Event |
---|
| 71 | // mouse event |
---|
| 72 | var clientX = e.clientX, |
---|
| 73 | clientY = e.clientY; |
---|
| 74 | |
---|
| 75 | if(Math.abs(clientX - this._lastX) > this.delay || Math.abs(clientY - this._lastY) > this.delay){ |
---|
| 76 | this.onMouseUp(e); |
---|
| 77 | new this.mover(this.shape, e, this); |
---|
| 78 | } |
---|
| 79 | event.stop(e); |
---|
| 80 | }, |
---|
| 81 | onMouseUp: function(e){ |
---|
| 82 | // summary: |
---|
| 83 | // event processor for onmouseup, used only for delayed delayed drags |
---|
| 84 | // e: Event |
---|
| 85 | // mouse event |
---|
| 86 | this.events.pop().remove(); |
---|
| 87 | }, |
---|
| 88 | |
---|
| 89 | // local events |
---|
| 90 | onMoveStart: function(/* dojox/gfx/Mover */ mover){ |
---|
| 91 | // summary: |
---|
| 92 | // called before every move operation |
---|
| 93 | // mover: |
---|
| 94 | // A Mover instance that fired the event. |
---|
| 95 | topic.publish("/gfx/move/start", mover); |
---|
| 96 | domClass.add(win.body(), "dojoMove"); |
---|
| 97 | }, |
---|
| 98 | onMoveStop: function(/* dojox/gfx/Mover */ mover){ |
---|
| 99 | // summary: |
---|
| 100 | // called after every move operation |
---|
| 101 | // mover: |
---|
| 102 | // A Mover instance that fired the event. |
---|
| 103 | topic.publish("/gfx/move/stop", mover); |
---|
| 104 | domClass.remove(win.body(), "dojoMove"); |
---|
| 105 | }, |
---|
| 106 | onFirstMove: function(/* dojox/gfx/Mover */ mover){ |
---|
| 107 | // summary: |
---|
| 108 | // called during the very first move notification, |
---|
| 109 | // can be used to initialize coordinates, can be overwritten. |
---|
| 110 | // mover: |
---|
| 111 | // A Mover instance that fired the event. |
---|
| 112 | |
---|
| 113 | // default implementation does nothing |
---|
| 114 | }, |
---|
| 115 | onMove: function(/* dojox/gfx/Mover */ mover, /* Object */ shift){ |
---|
| 116 | // summary: |
---|
| 117 | // called during every move notification, |
---|
| 118 | // should actually move the node, can be overwritten. |
---|
| 119 | // mover: |
---|
| 120 | // A Mover instance that fired the event. |
---|
| 121 | // shift: |
---|
| 122 | // An object as {dx,dy} that represents the shift. |
---|
| 123 | this.onMoving(mover, shift); |
---|
| 124 | this.shape.applyLeftTransform(shift); |
---|
| 125 | this.onMoved(mover, shift); |
---|
| 126 | }, |
---|
| 127 | onMoving: function(/* dojox/gfx/Mover */ mover, /* Object */ shift){ |
---|
| 128 | // summary: |
---|
| 129 | // called before every incremental move, |
---|
| 130 | // can be overwritten. |
---|
| 131 | // mover: |
---|
| 132 | // A Mover instance that fired the event. |
---|
| 133 | // shift: |
---|
| 134 | // An object as {dx,dy} that represents the shift. |
---|
| 135 | |
---|
| 136 | // default implementation does nothing |
---|
| 137 | }, |
---|
| 138 | onMoved: function(/* dojox/gfx/Mover */ mover, /* Object */ shift){ |
---|
| 139 | // summary: |
---|
| 140 | // called after every incremental move, |
---|
| 141 | // can be overwritten. |
---|
| 142 | // mover: |
---|
| 143 | // A Mover instance that fired the event. |
---|
| 144 | // shift: |
---|
| 145 | // An object as {dx,dy} that represents the shift. |
---|
| 146 | |
---|
| 147 | // default implementation does nothing |
---|
| 148 | } |
---|
| 149 | }); |
---|
| 150 | }); |
---|