-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manager.js
232 lines (216 loc) · 6.16 KB
/
Manager.js
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
define([
"../_base/array", "../_base/declare", "../_base/lang", "../_base/window",
"../dom-class", "../Evented", "../has", "../keys", "../on", "../topic", "../touch",
"./common", "./autoscroll", "./Avatar"
], function(array, declare, lang, win, domClass, Evented, has, keys, on, topic, touch,
dnd, autoscroll, Avatar){
// module:
// dojo/dnd/Manager
var Manager = declare("dojo.dnd.Manager", [Evented], {
// summary:
// the manager of DnD operations (usually a singleton)
constructor: function(){
this.avatar = null;
this.source = null;
this.nodes = [];
this.copy = true;
this.target = null;
this.canDropFlag = false;
this.events = [];
},
// avatar's offset from the mouse
OFFSET_X: has("touch") ? 0 : 16,
OFFSET_Y: has("touch") ? -64 : 16,
// methods
overSource: function(source){
// summary:
// called when a source detected a mouse-over condition
// source: Object
// the reporter
if(this.avatar){
this.target = (source && source.targetState != "Disabled") ? source : null;
this.canDropFlag = Boolean(this.target);
this.avatar.update();
}
topic.publish("/dnd/source/over", source);
},
outSource: function(source){
// summary:
// called when a source detected a mouse-out condition
// source: Object
// the reporter
if(this.avatar){
if(this.target == source){
this.target = null;
this.canDropFlag = false;
this.avatar.update();
topic.publish("/dnd/source/over", null);
}
}else{
topic.publish("/dnd/source/over", null);
}
},
startDrag: function(source, nodes, copy){
// summary:
// called to initiate the DnD operation
// source: Object
// the source which provides items
// nodes: Array
// the list of transferred items
// copy: Boolean
// copy items, if true, move items otherwise
// Tell autoscroll that a drag is starting
autoscroll.autoScrollStart(win.doc);
this.source = source;
this.nodes = nodes;
this.copy = Boolean(copy); // normalizing to true boolean
this.avatar = this.makeAvatar();
win.body().appendChild(this.avatar.node);
topic.publish("/dnd/start", source, nodes, this.copy);
function stopEvent(e){
e.preventDefault();
e.stopPropagation();
}
this.events = [
on(win.doc, touch.move, lang.hitch(this, "onMouseMove")),
on(win.doc, touch.release, lang.hitch(this, "onMouseUp")),
on(win.doc, "keydown", lang.hitch(this, "onKeyDown")),
on(win.doc, "keyup", lang.hitch(this, "onKeyUp")),
// cancel text selection and text dragging
on(win.doc, "dragstart", stopEvent),
on(win.body(), "selectstart", stopEvent)
];
var c = "dojoDnd" + (copy ? "Copy" : "Move");
domClass.add(win.body(), c);
},
canDrop: function(flag){
// summary:
// called to notify if the current target can accept items
var canDropFlag = Boolean(this.target && flag);
if(this.canDropFlag != canDropFlag){
this.canDropFlag = canDropFlag;
this.avatar.update();
}
},
stopDrag: function(){
// summary:
// stop the DnD in progress
domClass.remove(win.body(), ["dojoDndCopy", "dojoDndMove"]);
array.forEach(this.events, function(handle){ handle.remove(); });
this.events = [];
this.avatar.destroy();
this.avatar = null;
this.source = this.target = null;
this.nodes = [];
},
makeAvatar: function(){
// summary:
// makes the avatar; it is separate to be overwritten dynamically, if needed
return new Avatar(this);
},
updateAvatar: function(){
// summary:
// updates the avatar; it is separate to be overwritten dynamically, if needed
this.avatar.update();
},
// mouse event processors
onMouseMove: function(e){
// summary:
// event processor for onmousemove
// e: Event
// mouse event
var a = this.avatar;
if(a){
autoscroll.autoScrollNodes(e);
//autoscroll.autoScroll(e);
var s = a.node.style;
s.left = (e.pageX + this.OFFSET_X) + "px";
s.top = (e.pageY + this.OFFSET_Y) + "px";
var copy = Boolean(this.source.copyState(dnd.getCopyKeyState(e)));
if(this.copy != copy){
this._setCopyStatus(copy);
}
}
if(has("touch")){
// Prevent page from scrolling so that user can drag instead.
e.preventDefault();
}
},
onMouseUp: function(e){
// summary:
// event processor for onmouseup
// e: Event
// mouse event
if(this.avatar){
if(this.target && this.canDropFlag){
var copy = Boolean(this.source.copyState(dnd.getCopyKeyState(e)));
topic.publish("/dnd/drop/before", this.source, this.nodes, copy, this.target, e);
topic.publish("/dnd/drop", this.source, this.nodes, copy, this.target, e);
}else{
topic.publish("/dnd/cancel");
}
this.stopDrag();
}
},
// keyboard event processors
onKeyDown: function(e){
// summary:
// event processor for onkeydown:
// watching for CTRL for copy/move status, watching for ESCAPE to cancel the drag
// e: Event
// keyboard event
if(this.avatar){
switch(e.keyCode){
case keys.CTRL:
var copy = Boolean(this.source.copyState(true));
if(this.copy != copy){
this._setCopyStatus(copy);
}
break;
case keys.ESCAPE:
topic.publish("/dnd/cancel");
this.stopDrag();
break;
}
}
},
onKeyUp: function(e){
// summary:
// event processor for onkeyup, watching for CTRL for copy/move status
// e: Event
// keyboard event
if(this.avatar && e.keyCode == keys.CTRL){
var copy = Boolean(this.source.copyState(false));
if(this.copy != copy){
this._setCopyStatus(copy);
}
}
},
// utilities
_setCopyStatus: function(copy){
// summary:
// changes the copy status
// copy: Boolean
// the copy status
this.copy = copy;
this.source._markDndStatus(this.copy);
this.updateAvatar();
domClass.replace(win.body(),
"dojoDnd" + (this.copy ? "Copy" : "Move"),
"dojoDnd" + (this.copy ? "Move" : "Copy"));
}
});
// dnd._manager:
// The manager singleton variable. Can be overwritten if needed.
dnd._manager = null;
Manager.manager = dnd.manager = function(){
// summary:
// Returns the current DnD manager. Creates one if it is not created yet.
if(!dnd._manager){
dnd._manager = new Manager();
}
return dnd._manager; // Object
};
// TODO: for 2.0, store _manager and manager in Manager only. Don't access dnd or dojo.dnd.
return Manager;
});