source: Dev/trunk/src/client/dojox/mdnd/adapter/DndFromDojo.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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