Moveable.js
5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
define([
"../_base/array", "../_base/declare", "../_base/lang",
"../dom", "../dom-class", "../Evented", "../on", "../topic", "../touch", "./common", "./Mover", "../_base/window"
], function(array, declare, lang, dom, domClass, Evented, on, topic, touch, dnd, Mover, win){
// module:
// dojo/dnd/Moveable
var Moveable = declare("dojo.dnd.Moveable", [Evented], {
// summary:
// an object, which makes a node movable
// object attributes (for markup)
handle: "",
delay: 0,
skip: false,
constructor: function(node, params){
// node: Node
// a node (or node's id) to be moved
// params: Moveable.__MoveableArgs?
// optional parameters
this.node = dom.byId(node);
if(!params){ params = {}; }
this.handle = params.handle ? dom.byId(params.handle) : null;
if(!this.handle){ this.handle = this.node; }
this.delay = params.delay > 0 ? params.delay : 0;
this.skip = params.skip;
this.mover = params.mover ? params.mover : Mover;
this.events = [
on(this.handle, touch.press, lang.hitch(this, "onMouseDown")),
// cancel text selection and text dragging
on(this.handle, "dragstart", lang.hitch(this, "onSelectStart")),
on(this.handle, "selectstart", lang.hitch(this, "onSelectStart"))
];
},
// markup methods
markupFactory: function(params, node, Ctor){
return new Ctor(node, params);
},
// methods
destroy: function(){
// summary:
// stops watching for possible move, deletes all references, so the object can be garbage-collected
array.forEach(this.events, function(handle){ handle.remove(); });
this.events = this.node = this.handle = null;
},
// mouse event processors
onMouseDown: function(e){
// summary:
// event processor for onmousedown/ontouchstart, creates a Mover for the node
// e: Event
// mouse/touch event
if(this.skip && dnd.isFormElement(e)){ return; }
if(this.delay){
this.events.push(
on(this.handle, touch.move, lang.hitch(this, "onMouseMove")),
on(this.handle, touch.release, lang.hitch(this, "onMouseUp"))
);
this._lastX = e.pageX;
this._lastY = e.pageY;
}else{
this.onDragDetected(e);
}
e.stopPropagation();
e.preventDefault();
},
onMouseMove: function(e){
// summary:
// event processor for onmousemove/ontouchmove, used only for delayed drags
// e: Event
// mouse/touch event
if(Math.abs(e.pageX - this._lastX) > this.delay || Math.abs(e.pageY - this._lastY) > this.delay){
this.onMouseUp(e);
this.onDragDetected(e);
}
e.stopPropagation();
e.preventDefault();
},
onMouseUp: function(e){
// summary:
// event processor for onmouseup, used only for delayed drags
// e: Event
// mouse event
for(var i = 0; i < 2; ++i){
this.events.pop().remove();
}
e.stopPropagation();
e.preventDefault();
},
onSelectStart: function(e){
// summary:
// event processor for onselectevent and ondragevent
// e: Event
// mouse event
if(!this.skip || !dnd.isFormElement(e)){
e.stopPropagation();
e.preventDefault();
}
},
// local events
onDragDetected: function(/*Event*/ e){
// summary:
// called when the drag is detected;
// responsible for creation of the mover
new this.mover(this.node, e, this);
},
onMoveStart: function(/*Mover*/ mover){
// summary:
// called before every move operation
topic.publish("/dnd/move/start", mover);
domClass.add(win.body(), "dojoMove");
domClass.add(this.node, "dojoMoveItem");
},
onMoveStop: function(/*Mover*/ mover){
// summary:
// called after every move operation
topic.publish("/dnd/move/stop", mover);
domClass.remove(win.body(), "dojoMove");
domClass.remove(this.node, "dojoMoveItem");
},
onFirstMove: function(/*===== mover, e =====*/){
// summary:
// called during the very first move notification;
// can be used to initialize coordinates, can be overwritten.
// mover: Mover
// e: Event
// default implementation does nothing
},
onMove: function(mover, leftTop /*=====, e =====*/){
// summary:
// called during every move notification;
// should actually move the node; can be overwritten.
// mover: Mover
// leftTop: Object
// e: Event
this.onMoving(mover, leftTop);
var s = mover.node.style;
s.left = leftTop.l + "px";
s.top = leftTop.t + "px";
this.onMoved(mover, leftTop);
},
onMoving: function(/*===== mover, leftTop =====*/){
// summary:
// called before every incremental move; can be overwritten.
// mover: Mover
// leftTop: Object
// default implementation does nothing
},
onMoved: function(/*===== mover, leftTop =====*/){
// summary:
// called after every incremental move; can be overwritten.
// mover: Mover
// leftTop: Object
// default implementation does nothing
}
});
/*=====
Moveable.__MoveableArgs = declare([], {
// handle: Node||String
// A node (or node's id), which is used as a mouse handle.
// If omitted, the node itself is used as a handle.
handle: null,
// delay: Number
// delay move by this number of pixels
delay: 0,
// skip: Boolean
// skip move of form elements
skip: false,
// mover: Object
// a constructor of custom Mover
mover: dnd.Mover
});
=====*/
return Moveable;
});