source: Dev/branches/rest-dojo-ui/client/dojox/mdnd/dropMode/OverDropMode.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 8.0 KB
Line 
1define(["dojo/_base/kernel","dojo/_base/declare","dojo/_base/connect","dojo/_base/html",
2        "dojo/_base/array","dojox/mdnd/AreaManager"],function(dojo){
3        var odm = dojo.declare(
4                "dojox.mdnd.dropMode.OverDropMode",
5                null,
6        {
7                // summary:
8                //              Default class to find the nearest target only if the mouse is over an area.
9       
10                // _oldXPoint: Integer
11                //              used to save a X position
12                _oldXPoint: null,
13       
14                // _oldYPoint: Integer
15                //              used to save a Y position
16                _oldYPoint: null,
17       
18                // _oldBehaviour: Integer
19                //              see getDragpoint()
20                _oldBehaviour: "up",
21       
22                constructor: function(){
23                        //console.log("dojox.mdnd.dropMode.OverDropMode ::: constructor");
24                        this._dragHandler = [
25                                dojo.connect(dojox.mdnd.areaManager(), "onDragEnter", function(coords, size){
26                                        var m = dojox.mdnd.areaManager();
27                                        if(m._oldIndexArea == -1){
28                                                m._oldIndexArea = m._lastValidIndexArea;
29                                        }
30                                })
31                        ];
32       
33                },
34       
35                addArea: function(/*Array*/areas, /*Object*/object){
36                        // summary:
37                        //              Add a D&D Area into an array sorting by the x position.
38                        // areas:
39                        //              array of areas
40                        // object:
41                        //              data type of a DndArea
42                        // returns:
43                        //              a sorted area
44       
45                        //console.log("dojox.mdnd.dropMode.OverDropMode ::: addArea");
46                        var length = areas.length,
47                                position = dojo.position(object.node, true);
48                        object.coords = {'x':position.x, 'y':position.y};
49                        if(length == 0){
50                                areas.push(object);
51                        }
52                        else{
53                                var x = object.coords.x;
54                                for(var i = 0; i < length; i++){
55                                        if(x < areas[i].coords.x){
56                                                for(var j = length-1; j >= i; j--)
57                                                        areas[j + 1] = areas[j];
58                                                areas[i] = object;
59                                                break;
60                                        }
61                                }
62                                if(i == length){
63                                        areas.push(object);
64                                }
65                        }
66                        return areas;   // Array
67                },
68       
69                updateAreas: function(/*Array*/areaList){
70                        // summary:
71                        //              refresh areas position and size to determinate the nearest area to drop an item
72                        // description:
73                        //              the area position (and size) is equal to the postion of the domNode associated.
74                        // areaList:
75                        //              array of areas
76       
77                        //console.log("dojox.mdnd.dropMode.OverDropMode ::: updateAreas");
78                        var length = areaList.length;
79                        for(var i = 0; i < length; i++){
80                                this._updateArea(areaList[i]);
81                        }
82                },
83       
84                _updateArea : function(/*Object*/area){
85                        // summary:
86                        //              update the D&D area object (i.e. update coordinates of its DOM node)
87                        // area:
88                        //              the D&D area.
89                        // tags:
90                        //              protected
91       
92                        //console.log("dojox.mdnd.dropMode.OverDropMode ::: addArea");
93                        var position = dojo.position(area.node, true);
94                        area.coords.x = position.x;
95                        area.coords.x2 = position.x + position.w;
96                        area.coords.y = position.y;
97                },
98       
99                initItems: function(/*Object*/area){
100                        // summary:
101                        //              initialize the horizontal line in order to determinate the drop zone.
102                        // area:
103                        //              the D&D area.
104       
105                        //console.log("dojox.mdnd.dropMode.OverDropMode ::: initItems");
106                        dojo.forEach(area.items, function(obj){
107                                //get the vertical middle of the item
108                                var node = obj.item.node;
109                                var position = dojo.position(node, true);
110                                var y = position.y + position.h/2;
111                                obj.y = y;
112                        });
113                        area.initItems = true;
114                },
115       
116                refreshItems: function(/*Object*/area, /*Integer*/indexItem, /*Object*/size, /*Boolean*/added){
117                        // summary:
118                        //              take into account the drop indicator DOM element in order to compute horizontal lines
119                        // area:
120                        //              a D&D area object
121                        // indexItem:
122                        //              index of a draggable item
123                        // size:
124                        //              dropIndicator size
125                        // added:
126                        //              boolean to know if a dropIndicator has been added or deleted
127       
128                        //console.log("dojox.mdnd.dropMode.OverDropMode ::: refreshItems", area, indexItem, size, added);
129                        if(indexItem == -1){
130                                return;
131                        }
132                        else if(area && size && size.h){
133                                var height = size.h;
134                                if(area.margin){
135                                        height += area.margin.t;
136                                }
137                                var length = area.items.length;
138                                for(var i = indexItem; i < length; i++){
139                                        var item = area.items[i];
140                                        if(added){
141                                                item.y += height;
142                                        }
143                                        else{
144                                                item.y -= height;
145                                        }
146                                }
147                        }
148                },
149       
150                getDragPoint: function(/*Object*/coords, /*Object*/size, /*Object*/mousePosition){
151                        // summary:
152                        //              return coordinates of the draggable item.
153                        //              - For X point : the x position of mouse
154                        //              - For Y point : the y position of mouse
155                        // returns:
156                        //              an object of coordinates
157                        //              examples:{'x':10,'y':10}
158                        // coords:
159                        //              an object encapsulating X and Y position
160                        // size:
161                        //              an object encapsulating width and height values
162                        // mousePosition:
163                        //              coordinates of mouse
164       
165                        //console.log("dojox.mdnd.OverDropMode ::: getDragPoint");
166                        return {                        // Object
167                                'x': mousePosition.x,
168                                'y': mousePosition.y
169                                }
170                },
171       
172       
173                getTargetArea: function(/*Array*/areaList, /*Object*/ coords, /*integer*/currentIndexArea ){
174                        // summary:
175                        //              get the nearest D&D area.
176                        // areaList:
177                        //              a list of D&D areas objects
178                        // coords:
179                        //              coordinates [x,y] of the dragItem (see getDragPoint())
180                        // currentIndexArea:
181                        //              an index representing the active D&D area
182                        //returns:
183                        //              the index of the D&D area
184       
185                        //console.log("dojox.mdnd.dropMode.OverDropMode ::: getTargetArea");
186                        var index = 0;
187                        var x = coords.x;
188                        var y = coords.y;
189                        var end = areaList.length;
190                        var start = 0, direction = "right", compute = false;
191                        if(currentIndexArea == -1 || arguments.length < 3){
192                                // first time : Need to search the nearest area in all areas.
193                                compute = true;
194                        }
195                        else{
196                                // check if it's always the same area
197                                if(this._checkInterval(areaList, currentIndexArea, x, y)){
198                                        index = currentIndexArea;
199                                }
200                                else{
201                                        if(this._oldXPoint < x){
202                                                start = currentIndexArea + 1;
203                                        }
204                                        else{
205                                                start = currentIndexArea - 1;
206                                                end = 0;
207                                                direction = "left";
208                                        }
209                                        compute = true;
210                                }
211                        }
212                        if(compute){
213                                if(direction === "right"){
214                                        for(var i = start; i < end; i++){
215                                                if(this._checkInterval(areaList, i, x, y)){
216                                                        index = i;
217                                                        break;
218                                                }
219                                        }
220                                        if(i == end){
221                                                index = -1;
222                                        }
223                                }
224                                else{
225                                        for(var i = start; i >= end; i--){
226                                                if(this._checkInterval(areaList, i, x, y)){
227                                                        index = i;
228                                                        break;
229                                                }
230                                        }
231                                        if(i == end-1){
232                                                index = -1;
233                                        }
234                                }
235                        }
236                        this._oldXPoint = x;
237                        return index; // Integer
238                },
239       
240                _checkInterval: function(/*Array*/areaList, /*Integer*/index, /*Coord*/x, /*Coord*/y){
241                        // summary:
242                        //              check if the dragNode is in the interval.
243                        // returns:
244                        //              true if the dragNode is in intervall
245                        // areaList:
246                        //              a list of D&D areas objects
247                        // index:
248                        //              index of a D&D area (to get the interval)
249                        // x:
250                        //              coordinate x, of the dragNode (see getDragPoint())
251                        // tags:
252                        //              protected
253       
254                        //console.log("dojox.mdnd.dropMode.OverDropMode ::: _checkInterval");
255                        var area = areaList[index];
256                        var node = area.node;
257                        var coords = area.coords;
258                        var startX = coords.x;
259                        var endX = coords.x2;
260                        var startY = coords.y;
261                        var endY = startY + node.offsetHeight;
262                        if(startX <= x && x <= endX && startY <= y && y <= endY){
263                                return true;
264                        }
265                        return false; // Boolean
266                },
267       
268                getDropIndex: function(/*Object*/ targetArea, /*Object*/ coords){
269                        // summary:
270                        //              Return the index where the drop has to be placed.
271                        // targetArea:
272                        //              a D&D area object.
273                        // coords:
274                        //              coordinates [x,y] of the draggable item.
275                        // returns:
276                        //              a number or -1 if the area has no children or the drop index represents the last position in to the area
277       
278                        //console.log("dojox.mdnd.dropMode.OverDropMode ::: getDropIndex");
279                        var length = targetArea.items.length;
280                        var coordinates = targetArea.coords;
281                        var y = coords.y;
282                        if(length > 0){
283                                // course all children in the target area.
284                                for(var i = 0; i < length; i++){
285                                        // compare y value with y value of children
286                                        if(y < targetArea.items[i].y){
287                                                return i;       // integer
288                                        }
289                                        else{
290                                                if(i == length-1){
291                                                        return -1; // integer
292                                                }
293                                        }
294                                }
295                        }
296                        return -1;      //integer
297                },
298       
299                destroy: function(){
300                        dojo.forEach(this._dragHandler, dojo.disconnect);
301                }
302        });
303       
304        dojox.mdnd.areaManager()._dropMode = new dojox.mdnd.dropMode.OverDropMode();
305        return odm;
306});
Note: See TracBrowser for help on using the repository browser.