1 | define(["dojo/_base/lang","dojo/_base/declare","dojo/_base/array","dojo/_base/event","dojo/_base/connect", |
---|
2 | "dojo/dom-class","dojo/_base/window","./Mover"], |
---|
3 | function(lang,declare,arr,event,connect,domClass,win,Mover){ |
---|
4 | return declare("dojox.gfx.Moveable", null, { |
---|
5 | constructor: function(shape, params){ |
---|
6 | // summary: an object, which makes a shape moveable |
---|
7 | // shape: dojox.gfx.Shape: a shape object to be moved |
---|
8 | // params: Object: an optional object with additional parameters; |
---|
9 | // following parameters are recognized: |
---|
10 | // delay: Number: delay move by this number of pixels |
---|
11 | // mover: Object: a constructor of custom Mover |
---|
12 | this.shape = shape; |
---|
13 | this.delay = (params && params.delay > 0) ? params.delay : 0; |
---|
14 | this.mover = (params && params.mover) ? params.mover : Mover; |
---|
15 | this.events = [ |
---|
16 | this.shape.connect("onmousedown", this, "onMouseDown") |
---|
17 | // cancel text selection and text dragging |
---|
18 | //, dojo.connect(this.handle, "ondragstart", dojo, "stopEvent") |
---|
19 | //, dojo.connect(this.handle, "onselectstart", dojo, "stopEvent") |
---|
20 | ]; |
---|
21 | }, |
---|
22 | |
---|
23 | // methods |
---|
24 | destroy: function(){ |
---|
25 | // summary: stops watching for possible move, deletes all references, so the object can be garbage-collected |
---|
26 | arr.forEach(this.events, this.shape.disconnect, this.shape); |
---|
27 | this.events = this.shape = null; |
---|
28 | }, |
---|
29 | |
---|
30 | // mouse event processors |
---|
31 | onMouseDown: function(e){ |
---|
32 | // summary: event processor for onmousedown, creates a Mover for the shape |
---|
33 | // e: Event: mouse event |
---|
34 | if(this.delay){ |
---|
35 | this.events.push( |
---|
36 | this.shape.connect("onmousemove", this, "onMouseMove"), |
---|
37 | this.shape.connect("onmouseup", this, "onMouseUp")); |
---|
38 | this._lastX = e.clientX; |
---|
39 | this._lastY = e.clientY; |
---|
40 | }else{ |
---|
41 | new this.mover(this.shape, e, this); |
---|
42 | } |
---|
43 | event.stop(e); |
---|
44 | }, |
---|
45 | onMouseMove: function(e){ |
---|
46 | // summary: event processor for onmousemove, used only for delayed drags |
---|
47 | // e: Event: mouse event |
---|
48 | if(Math.abs(e.clientX - this._lastX) > this.delay || Math.abs(e.clientY - this._lastY) > this.delay){ |
---|
49 | this.onMouseUp(e); |
---|
50 | new this.mover(this.shape, e, this); |
---|
51 | } |
---|
52 | event.stop(e); |
---|
53 | }, |
---|
54 | onMouseUp: function(e){ |
---|
55 | // summary: event processor for onmouseup, used only for delayed delayed drags |
---|
56 | // e: Event: mouse event |
---|
57 | this.shape.disconnect(this.events.pop()); |
---|
58 | this.shape.disconnect(this.events.pop()); |
---|
59 | }, |
---|
60 | |
---|
61 | // local events |
---|
62 | onMoveStart: function(/* dojox.gfx.Mover */ mover){ |
---|
63 | // summary: called before every move operation |
---|
64 | connect.publish("/gfx/move/start", [mover]); |
---|
65 | domClass.add(win.body(), "dojoMove"); |
---|
66 | }, |
---|
67 | onMoveStop: function(/* dojox.gfx.Mover */ mover){ |
---|
68 | // summary: called after every move operation |
---|
69 | connect.publish("/gfx/move/stop", [mover]); |
---|
70 | domClass.remove(win.body(), "dojoMove"); |
---|
71 | }, |
---|
72 | onFirstMove: function(/* dojox.gfx.Mover */ mover){ |
---|
73 | // summary: called during the very first move notification, |
---|
74 | // can be used to initialize coordinates, can be overwritten. |
---|
75 | |
---|
76 | // default implementation does nothing |
---|
77 | }, |
---|
78 | onMove: function(/* dojox.gfx.Mover */ mover, /* Object */ shift){ |
---|
79 | // summary: called during every move notification, |
---|
80 | // should actually move the node, can be overwritten. |
---|
81 | this.onMoving(mover, shift); |
---|
82 | this.shape.applyLeftTransform(shift); |
---|
83 | this.onMoved(mover, shift); |
---|
84 | }, |
---|
85 | onMoving: function(/* dojox.gfx.Mover */ mover, /* Object */ shift){ |
---|
86 | // summary: called before every incremental move, |
---|
87 | // can be overwritten. |
---|
88 | |
---|
89 | // default implementation does nothing |
---|
90 | }, |
---|
91 | onMoved: function(/* dojox.gfx.Mover */ mover, /* Object */ shift){ |
---|
92 | // summary: called after every incremental move, |
---|
93 | // can be overwritten. |
---|
94 | |
---|
95 | // default implementation does nothing |
---|
96 | } |
---|
97 | }); |
---|
98 | }); |
---|