source: Dev/trunk/src/client/dojox/mdnd/dropMode/OverDropMode.js @ 529

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

Added Dojo 1.9.3 release.

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