Debian Codegraph @

dojox/mdnd/adapter/DndFromDojo.js

raw symbols SCIP
1 define(["dojo/_base/kernel",
2 "dojo/_base/declare",
3 "dojo/_base/connect",
4 "dojo/_base/array",
5 "dojo/dom-class",
6 "dojo/_base/window",
7 "dojox/mdnd/AreaManager",
8 "dojo/dnd/Manager"
9 ],function(dojo, declare, connect, array, domClass, win, AreaManager, Manager){
10 var dfd = declare(
11 "dojox.mdnd.adapter.DndFromDojo",
12 null,
13 {
14 // summary:
15 // Allow communication between Dojo dnd items and DojoX D&D areas
16
17 // dropIndicatorSize: Object
18 // size by default of dropIndicator (display only into a D&D Area)
19 dropIndicatorSize: {'w':0,'h':50},
20
21 // _areaManager: Object
22 // Reference to the current DojoX Dnd Manager
23 _areaManager: null,
24
25 // _dojoManager
26 // Reference to the current Dojo Manager
27 _dojoManager: null,
28
29 // _currentArea: Object
30 // The current Area on mouse over
31 _currentArea: null,
32
33 // _oldArea: Object
34 // The old area the mouse has passed over
35 _oldArea: null,
36
37 // _moveHandler: Object
38 // The handler of mouse connection
39 _moveHandler: null,
40
41 // _subscribeHandler: Array
42 // The list of dojo dnd topics
43 _subscribeHandler: null,
44
45 constructor: function(){
46 this._areaManager = AreaManager.areaManager();
47 this._dojoManager = Manager.manager();
48 this._currentArea = null;
49 this._moveHandler = null;
50 this.subscribeDnd();
51 },
52
53 subscribeDnd: function(){
54 // summary:
55 // Subscribe to somes topics of dojo drag and drop.
56
57 //console.log(("dojox.mdnd.adapter.DndFromDojo ::: subscribeDnd");
58 this._subscribeHandler = [
59 connect.subscribe("/dnd/start",this,"onDragStart"),
60 connect.subscribe("/dnd/drop/before", this, "onDrop"),
61 connect.subscribe("/dnd/cancel",this,"onDropCancel"),
62 connect.subscribe("/dnd/source/over",this,"onDndSource")
63 ]
64 },
65
66 unsubscribeDnd: function(){
67 // summary:
68 // Unsubscribe to some topics of dojo drag and drop.
69
70 //console.log(("dojox.mdnd.adapter.DndFromDojo ::: unsubscribeDnd");
71 array.forEach(this._subscribeHandler, connect.unsubscribe);
72 },
73
74 _getHoverArea: function(/*Object*/ coords){
75 // summary:
76 // Get a D&D dojoX area as a DOM node positioned under a specific point.
77 // coords:
78 // Object containing the coordinates x and y (mouse position)
79 // tags:
80 // protected
81
82 //console.log("dojox.mdnd.adapter.DndFromDojo ::: _getHoverArea");
83 var x = coords.x;
84 var y = coords.y;
85 this._oldArea = this._currentArea;
86 this._currentArea = null;
87 var areas = this._areaManager._areaList;
88 for(var i = 0; i < areas.length; i++){
89 var area = areas[i];
90 var startX = area.coords.x;
91 var endX = startX + area.node.offsetWidth;
92 var startY = area.coords.y;
93 var endY = startY + area.node.offsetHeight;
94 // check if the coordinates mouse is in a D&D Area
95 if(startX <= x && x <= endX && startY <= y && y <= endY){
96 this._areaManager._oldIndexArea = this._areaManager._currentIndexArea;
97 this._areaManager._currentIndexArea = i;
98 this._currentArea = area.node;
99 break;
100 }
101 }
102 if(this._currentArea != this._oldArea){
103 if(this._currentArea == null){
104 // case when the dragNode was in a D&D area but it's out now.
105 this.onDragExit();
106 }
107 else if(this._oldArea == null){
108 // case when the dragNode was out a D&D area but it's in now.
109 this.onDragEnter();
110 }
111 else{
112 // case when the dragNode was in a D&D area and enter in an other D&D area directly.
113 this.onDragExit();
114 this.onDragEnter();
115 }
116 }
117
118 //console.log("dojox.mdnd.adapter.DndFromDojo ::: _getHoverArea",this._dojoManager.avatar.node,this._currentArea,this._oldArea);
119 },
120
121 onDragStart: function(/*Object*/source, /*Array*/nodes, /*Boolean*/copy){
122 // summary:
123 // Occurs when the "/dnd/start" topic is published.
124 // source:
125 // the source which provides items
126 // nodes:
127 // the list of transferred items
128 // copy:
129 // copy items, if true, move items otherwise
130 // tags:
131 // callback
132
133 //console.log("dojox.mdnd.adapter.DndFromDojo ::: onDragStart");
134 // catch the dragNode to get the type when it's necessary.
135 this._dragNode = nodes[0];
136 this._copy = copy; this._source = source;
137 // Connect the onMouseMove :
138 // It's useful to activate the detection of a D&D area and the dropIndicator place only if
139 // the dragNode is out of a the source dojo. The classic behaviour of the dojo source is kept.
140 this._outSourceHandler = connect.connect(this._dojoManager, "outSource", this, function(){
141 //dojo.disconnect(this._outSourceHandler);
142 if(this._moveHandler == null){
143 this._moveHandler = connect.connect(dojo.doc, "mousemove", this, "onMouseMove");
144 }
145 });
146 },
147
148 onMouseMove: function(/*DOMEvent*/e){
149 // summary:
150 // Occurs when the user moves the mouse.
151 // e:
152 // the DOM event
153 // tags:
154 // callback
155
156 //console.log("dojox.mdnd.adapter.DndFromDojo ::: onMouseMove");
157 // calculate the coordonates of the mouse.
158 var coords = {
159 'x': e.pageX,
160 'y': e.pageY
161 };
162 this._getHoverArea(coords);
163 // if a D&D area has been found and if it's accepted to drop this type of dragged node
164 if(this._currentArea && this._areaManager._accept){
165 // specific case : a dropIndicator can be hidden (see onDndSource method)
166 if(this._areaManager._dropIndicator.node.style.visibility == "hidden"){
167 this._areaManager._dropIndicator.node.style.visibility = "";
168 domClass.add(this._dojoManager.avatar.node, "dojoDndAvatarCanDrop");
169 }
170 // place the dropIndicator in D&D Area with a default size.
171 this._areaManager.placeDropIndicator(coords, this.dropIndicatorSize);
172 }
173 },
174
175 onDragEnter: function(){
176 // summary:
177 // Occurs when the user drages an DOJO dnd item inside a D&D dojoX area.
178 // tags:
179 // callback
180
181 //console.log("dojox.mdnd.adapter.DndFromDojo ::: onDragEnter");
182 // Check if the type of dragged node is accepted in the selected D&D dojoX Area.
183 var _dndType = this._dragNode.getAttribute("dndType");
184 // need to have an array as type
185 var type = (_dndType) ? _dndType.split(/\s*,\s*/) : ["text"];
186 this._areaManager._isAccepted(type, this._areaManager._areaList[this._areaManager._currentIndexArea].accept);
187 // if the D&D dojoX Area accepts the drop, change the color of Avatar.
188 if(this._dojoManager.avatar){
189 if(this._areaManager._accept){
190 domClass.add(this._dojoManager.avatar.node, "dojoDndAvatarCanDrop");
191 }
192 else{
193 domClass.remove(this._dojoManager.avatar.node, "dojoDndAvatarCanDrop");
194 }
195 }
196 },
197
198 onDragExit: function(){
199 // summary:
200 // Occurs when the user leaves a D&D dojoX area after dragging an DOJO dnd item over it.
201
202 //console.log("dojox.mdnd.adapter.DndFromDojo ::: onDragExit");
203 // if the dragged node exits of a D&D dojoX Area :
204 this._areaManager._accept = false;
205 // change color of avatar
206 if(this._dojoManager.avatar){
207 domClass.remove(this._dojoManager.avatar.node, "dojoDndAvatarCanDrop");
208 }
209 // reset all variables and remove the dropIndicator.
210 if(this._currentArea == null){
211 this._areaManager._dropMode.refreshItems(this._areaManager._areaList[this._areaManager._oldIndexArea], this._areaManager._oldDropIndex, this.dropIndicatorSize, false);
212 this._areaManager._resetAfterDrop();
213 }
214 else{
215 this._areaManager._dropIndicator.remove();
216 }
217 },
218
219 isAccepted: function(/*Node*/node, /*Object*/accept){
220 // summary:
221 // Check if a dragNode is accepted into a dojo target.
222 // node:
223 // The dragged node.
224 // accept:
225 // Object containing the type accepted for a target dojo.
226 // returns:
227 // true if the dragged node is accepted in the target dojo.
228
229 //console.log("dojox.mdnd.adapter.DndFromDojo ::: isAccepted");
230 var type = (node.getAttribute("dndType")) ? node.getAttribute("dndType") : "text";
231 if(type && type in accept)
232 return true; // Boolean
233 else
234 return false; // Boolean
235 },
236
237 onDndSource: function(/*Object*/ source){
238 // summary:
239 // Called when the mouse enters or exits of a source dojo.
240 // source:
241 // the dojo source/target
242 // tags:
243 // callback
244
245 //console.log("dojox.mdnd.adapter.DndFromDojo ::: onDndSource",source);
246 // Only the case : "source dojo into a D&D dojoX Area" is treated.
247 if(this._currentArea == null){
248 return;
249 }
250 if(source){
251 // Enter in a source/target dojo.
252 // test if the type of draggedNode is accepted :
253 var accept = false;
254 if(this._dojoManager.target == source){
255 accept = true;
256 }
257 else{
258 accept = this.isAccepted(this._dragNode, source.accept);
259 }
260 if(accept){
261 // disconnect the onMouseMove to disabled the search of a drop zone in the D&D dojoX Area.
262 connect.disconnect(this._moveHandler);
263 this._currentArea = this._moveHandler = null;
264 // hidden the visibility of dojoX dropIndicator to prevent an offset when the dropIndicator disappears.
265 // test if drop indicator is visible before applaying hidden style.
266 var dropIndicator = this._areaManager._dropIndicator.node;
267 if(dropIndicator && dropIndicator.parentNode !== null && dropIndicator.parentNode.nodeType == 1)
268 dropIndicator.style.visibility = "hidden";
269 }
270 else{
271 // if the type of dragged node is not accepted in the target dojo, the color of avatar
272 // have to be the same that the color of D&D dojoX Area acceptance.
273 this._resetAvatar();
274 }
275 }
276 else{
277 // Exit of a source/target dojo.
278 // reconnect the onMouseMove to enabled the search of a drop zone in the D&D dojox Area.
279 if(!this._moveHandler)
280 this._moveHandler = connect.connect(dojo.doc, "mousemove", this, "onMouseMove");
281
282 this._resetAvatar();
283 }
284 },
285
286 _resetAvatar: function(){
287 // summary:
288 // Function executed in onDndSource function to set the avatar
289 // acceptance according to the dojox DnD AreaManager Acceptance.
290 // It is used when The mouse exit a source/target dojo or if the
291 // dragged node is not accepted in dojo source / target.
292 // tags:
293 // protected
294
295 //console.log("dojox.mdnd.adapter.DndFromDojo ::: _resetAvatar");
296 if(this._dojoManager.avatar){
297 if(this._areaManager._accept){
298 domClass.add(this._dojoManager.avatar.node, "dojoDndAvatarCanDrop");
299 }
300 else{
301 domClass.remove(this._dojoManager.avatar.node, "dojoDndAvatarCanDrop");
302 }
303 }
304 },
305
306 onDropCancel: function(){
307 // summary:
308 // Occurs when the "/dnd/cancel" topic is published.
309 // tags:
310 // callback
311
312 //console.log("dojox.mdnd.adapter.DndFromDojo ::: onDropCancel");
313 if(this._currentArea == null){
314 // the dragged node is not in the D&D dojox Area => Cancel
315 this._areaManager._resetAfterDrop();
316 connect.disconnect(this._moveHandler);
317 connect.disconnect(this._outSourceHandler);
318 this._currentArea = this._moveHandler = this._outSourceHandler = null;
319 }
320 else{
321 // the dragged node is in the D&D dojox Area
322 // (catch when dragged node exits of a source/target dojo and stays in the same D&D dojox Area)
323 // dojo cancel the drop but it's authorized in the D&D Area
324 if(this._areaManager._accept){
325 this.onDrop(this._source, [this._dragNode], this._copy, this._currentArea);
326 }
327 else{
328 this._currentArea = null;
329 connect.disconnect(this._outSourceHandler);
330 connect.disconnect(this._moveHandler);
331 this._moveHandler = this._outSourceHandler = null;
332 }
333 }
334 },
335
336 onDrop: function(/*Object*/source, /*Array*/nodes, /*Boolean*/copy){
337 // summary:
338 // Occurs when the user leaves a D&D dojox area after dragging an DOJO dnd item over it.
339 // source:
340 // the source which provides items
341 // nodes:
342 // the list of transferred items
343 // copy:
344 // copy items, if true, move items otherwise
345 // tags:
346 // callback
347
348 //console.log("dojox.mdnd.adapter.DndFromDojo ::: onDrop", this._currentArea);
349 connect.disconnect(this._moveHandler);
350 connect.disconnect(this._outSourceHandler);
351 this._moveHandler = this._outSourceHandler = null;
352 if(this._currentArea){
353 var dropIndex = this._areaManager._currentDropIndex;
354 connect.publish("/dnd/drop/after", [source, nodes, copy, this._currentArea, dropIndex]);
355 this._currentArea = null;
356 }
357 if(this._areaManager._dropIndicator.node.style.visibility == "hidden"){
358 this._areaManager._dropIndicator.node.style.visibility = "";
359 }
360 this._areaManager._resetAfterDrop();
361 }
362 });
363
364 dojox.mdnd.adapter._dndFromDojo = null;
365 dojox.mdnd.adapter._dndFromDojo = new dojox.mdnd.adapter.DndFromDojo();
366 return dfd;
367 });