Debian Codegraph @

dojox/mdnd/AreaManager.js

raw symbols SCIP
1 define(["dojo/_base/kernel",
2 "dojo/_base/declare",
3 "dojo/_base/connect",
4 "dojo/_base/window",
5 "dojo/_base/array",
6 "dojo/_base/sniff",
7 "dojo/_base/lang",
8 "dojo/query",
9 "dojo/topic", // topic.publish()
10 "dojo/dom-class",
11 "dojo/dom-geometry",
12 "dojo/dom-construct",
13 "dijit/registry",
14 "dijit/_Widget",
15 "./Moveable",
16 "dojox/mdnd/AutoScroll"
17 ],function(dojo, declare, connect, win, array, sniff, lang, query, topic, domClass, geom, domConstruct, registry, _Widget, Moveable, AutoScroll){
18 var am = declare(
19 "dojox.mdnd.AreaManager",
20 null,
21 {
22 // summary:
23 // Drag And Drop manager
24
25 // autoRefresh: Boolean
26 // Enable the refresh of registered areas on drag start.
27 autoRefresh: true,
28
29
30 // areaClass: String
31 // CSS class enabled an area if areaClass is defined
32 areaClass: "dojoxDndArea",
33
34 // dragHandleClass: String
35 // CSS class enabled a drag handle.
36 dragHandleClass: "dojoxDragHandle",
37
38 constructor: function(){
39 // summary:
40 // Constructor of AreaManager class.
41 // Initialize arrays, connects and subscribes.
42
43 //console.log("dojox.mdnd.AreaManager ::: constructor");
44 this._areaList = [];
45 this.resizeHandler = connect.connect(dojo.global,"onresize", this, function(){
46 this._dropMode.updateAreas(this._areaList);
47 });
48
49 this._oldIndexArea = this._currentIndexArea = this._oldDropIndex = this._currentDropIndex = this._sourceIndexArea = this._sourceDropIndex = -1;
50 },
51
52 init: function(){
53 // summary:
54 // Initialize the manager by calling the registerByClass method
55
56 //console.log("dojox.mdnd.AreaManager ::: init");
57 this.registerByClass();
58 },
59
60 registerByNode: function(/*DOMNode*/area, /*Boolean*/notInitAreas){
61 // summary:
62 // To register Dnd Area : insert the DndArea using the specific sort of dropMode.
63 // area:
64 // a DOM node corresponding to the Dnd Area
65 // notInitAreas:
66 // if false or undefined, init the areas.
67
68 //console.log("dojox.mdnd.AreaManager ::: registerByNode", area);
69 var index = this._getIndexArea(area);
70 if(area && index == -1){
71 var acceptType = area.getAttribute("accept");
72 var accept = (acceptType) ? acceptType.split(/\s*,\s*/) : ["text"];
73 var obj = {
74 'node': area,
75 'items': [],
76 'coords': {},
77 'margin': null,
78 'accept': accept,
79 'initItems': false
80 };
81 array.forEach(this._getChildren(area), function(item){
82 this._setMarginArea(obj, item);
83 obj.items.push(this._addMoveableItem(item));
84 }, this);
85 this._areaList = this._dropMode.addArea(this._areaList, obj);
86 if(!notInitAreas){
87 this._dropMode.updateAreas(this._areaList);
88 }
89 connect.publish("/dojox/mdnd/manager/register",[area]);
90 }
91 },
92
93 registerByClass: function(){
94 // summary:
95 // Register all Dnd Areas identified by the attribute areaClass :
96 // insert Dnd Areas using the specific sort of dropMode.
97
98 //console.log("dojox.mdnd.AreaManager ::: registerByClass");
99 query('.'+this.areaClass).forEach(function(area){
100 this.registerByNode(area, true);
101 }, this);
102 this._dropMode.updateAreas(this._areaList);
103 },
104
105 unregister: function(/*DOMNode*/area){
106 // summary:
107 // Unregister a D&D Area and its children into the AreaManager.
108 // area:
109 // A node corresponding to the D&D Area.
110 // returns:
111 // True if the area is found and unregistered.
112
113 //console.log("dojox.mdnd.AreaManager ::: unregister");
114 var index = this._getIndexArea(area);
115 if(index != -1){
116 array.forEach(this._areaList[index].items, function(item){
117 this._deleteMoveableItem(item);
118 }, this);
119 this._areaList.splice(index,1);
120 // refresh target area
121 this._dropMode.updateAreas(this._areaList);
122 return true; // Boolean
123 }
124 return false; // Boolean
125 },
126
127 _addMoveableItem: function(/*DOMNode*/node){
128 // summary:
129 // Create a draggable item with a DOM node.
130 // node:
131 // A child of the D&D Area.
132 // returns:
133 // The draggable item.
134 // tags:
135 // protected
136
137 //console.log("dojox.mdnd.AreaManager ::: _addMoveableItem");
138 node.setAttribute("tabIndex", "0");
139 var handle = this._searchDragHandle(node);
140 var moveable = new Moveable({ 'handle': handle, 'skip': true }, node);
141 // add a css style :
142 domClass.add(handle || node, "dragHandle");
143 var type = node.getAttribute("dndType");
144 var item = {
145 'item': moveable,
146 'type': type ? type.split(/\s*,\s*/) : ["text"],
147 'handlers': [connect.connect(moveable, "onDragStart", this, "onDragStart")]
148 }
149 // connect to the uninitialize method of dijit._Widget to delete a moveable before a destruct
150 if(registry && registry.byNode){
151 var widget = registry.byNode(node);
152 if(widget){
153 item.type = widget.dndType ? widget.dndType.split(/\s*,\s*/) : ["text"];
154 item.handlers.push(
155 connect.connect(widget, "uninitialize", this, function(){
156 this.removeDragItem(node.parentNode, moveable.node);
157 })
158 );
159 }
160 }
161 return item; // Object
162 },
163
164 _deleteMoveableItem: function(/*Object*/ objItem){
165 // summary:
166 // Delete the Moveable object associated with a node.
167 // item:
168 // A moveable Object.
169 // tags:
170 // protected
171
172 //console.log("dojox.mdnd.AreaManager ::: _deleteMoveableItem", objItem);
173 // disconnect the handle
174 array.forEach(objItem.handlers, function(handler){
175 connect.disconnect(handler);
176 });
177 // delete css style :
178 var node = objItem.item.node,
179 handle = this._searchDragHandle(node);
180 domClass.remove(handle || node, "dragHandle");
181 // call destroy of Moveable class
182 objItem.item.destroy();
183 },
184
185 _getIndexArea: function(/*DOMNode*/area){
186 // summary:
187 // Get the index of an area.
188 // area:
189 // A moveable Object.
190 // returns:
191 // area index or -1
192 // tags:
193 // protected
194
195 //console.log("dojox.mdnd.AreaManager ::: _getIndexArea");
196 if(area){
197 for(var i = 0; i < this._areaList.length; i++){
198 if(this._areaList[i].node === area){
199 return i; // Integer
200 }
201 }
202 }
203 return -1; // Integer
204 },
205
206 _searchDragHandle: function(/*DOMNode*/node){
207 // summary:
208 // Return the node which contains the first specific CSS class handle.
209 // node:
210 // A child of the D&D Area.
211 // returns:
212 // The drag handle node.
213 // tags:
214 // protected
215
216 //console.log("dojox.mdnd.AreaManager ::: _searchDragHandle");
217 if(node){
218 var cssArray = this.dragHandleClass.split(' '),
219 length = cssArray.length,
220 queryCss = "";
221 array.forEach(cssArray, function(css, i){
222 queryCss += "." + css;
223 if(i != length - 1){
224 queryCss += ", ";
225 }
226 });
227 return query(queryCss, node)[0]; // DomNode
228 }
229 },
230
231 addDragItem: function(/*DOMNode*/area, /*DOMNode*/node, /*Integer*/index, /*Boolean*/notCheckParent){
232 // summary:
233 // To add an item programmatically.
234 // area:
235 // a node corresponding to the D&D Area
236 // node:
237 // the node which has to be treated.
238 // index:
239 // the place in the area
240 // noCheckParent:
241 // if true, doesn't check if node has a parent.
242 // returns:
243 // True if the node has been inserted else false.
244
245 //console.log("dojox.mdnd.AreaManager ::: addDragItem");
246 var add = true;
247 if(!notCheckParent){
248 add = area && node && (node.parentNode === null || (node.parentNode && node.parentNode.nodeType !== 1));
249 }
250 if(add){
251 var indexArea = this._getIndexArea(area);
252 if(indexArea !== -1){
253 var item = this._addMoveableItem(node),
254 items = this._areaList[indexArea].items;
255 if(0 <= index && index < items.length){
256 var firstListChild = items.slice(0, index),
257 lastListChild = items.slice(index, items.length);
258 firstListChild[firstListChild.length] = item;
259 this._areaList[indexArea].items = firstListChild.concat(lastListChild);
260 area.insertBefore(node, items[index].item.node);
261 }
262 else{
263 this._areaList[indexArea].items.push(item);
264 area.appendChild(node);
265 }
266 this._setMarginArea(this._areaList[indexArea], node);
267 this._areaList[indexArea].initItems = false;
268 return true; // Boolean
269 }
270 }
271 return false; // Boolean
272 },
273
274 removeDragItem: function(/*DOMNode*/area, /*DOMNode*/node){
275 // summary:
276 // Delete a moveable item programmatically. The node is removed from the area.
277 // area:
278 // A node corresponding to the DndArea.
279 // node:
280 // The node which has to be treated.
281 // returns:
282 // the removed node
283
284 //console.log("dojox.mdnd.AreaManager ::: removeDragItem");
285 var index = this._getIndexArea(area);
286 if(area && index !== -1){
287 var items = this._areaList[index].items;
288 for(var j = 0; j < items.length; j++){
289 if(items[j].item.node === node){
290 this._deleteMoveableItem(items[j]);
291 // delete item of the array
292 items.splice(j, 1);
293 return area.removeChild(node); // Object
294 }
295 }
296 }
297 return null;
298 },
299
300 _getChildren: function(/*DOMNode*/area){
301 // summary:
302 // Get the children of a D&D area.
303 // area:
304 // A DnD area.
305 // returns:
306 // The children of a DnD area
307 // tags:
308 // protected
309
310 //console.log("dojox.mdnd.AreaManager ::: _getChildren");
311 var children = [];
312 array.forEach(area.childNodes, function(child){
313 // delete \n
314 if(child.nodeType == 1){
315 if(registry && registry.byNode){
316 var widget = registry.byNode(child);
317 if(widget){
318 if(!widget.dragRestriction){
319 children.push(child);
320 }
321 }
322 else{
323 children.push(child);
324 }
325 }
326 else{
327 children.push(child);
328 }
329 }
330 });
331 return children; //Array
332 },
333
334 _setMarginArea: function(/*Object*/area,/*DOMNode*/node){
335 // summary:
336 // Set the value of margin in the data type of areaManager
337 // only when the margin has never been computed.
338 // area:
339 // The object of a D&D Area.
340 // node:
341 // The node which contains margins
342 // tags:
343 // protected
344
345 //console.log("dojox.mdnd.AreaManager ::: _setMarginArea");
346 if(area && area.margin === null && node){
347 area.margin = geom.getMarginExtents(node);
348 }
349 },
350
351 findCurrentIndexArea: function(/*Object*/coords, /*Object*/size){
352 // summary:
353 // find the nearest target area according to coordinates.
354 // Coordinates are representing by an object : for example, {'x':10,'y':10}
355 // coords:
356 // an object encapsulating X and Y position
357 // size:
358 // an object encapsulating the area size
359 // returns:
360 // an index of area
361
362 //console.log("dojox.mdnd.AreaManager ::: findCurrentIndexArea");
363 this._oldIndexArea = this._currentIndexArea;
364 this._currentIndexArea = this._dropMode.getTargetArea(this._areaList, coords, this._currentIndexArea);
365 if(this._currentIndexArea != this._oldIndexArea){
366 if(this._oldIndexArea != -1){
367 this.onDragExit(coords, size);
368 }
369 if(this._currentIndexArea != -1){
370 this.onDragEnter(coords, size);
371 }
372 }
373 return this._currentIndexArea; //Integer
374 },
375
376 _isAccepted: function(/*Array*/ type, /*Array*/ accept){
377 // summary:
378 // True if user can drop widget on this node.
379 // type:
380 // Array containing item type
381 // accept:
382 // Array containing types
383 this._accept = false;
384 for(var i = 0; i < accept.length; ++i){
385 for(var j = 0; j < type.length;++j){
386 if(type[j] == accept[i]){
387 this._accept = true;
388 break;
389 }
390 }
391 }
392 },
393
394 onDragStart: function(/*DOMNode*/node, /*Object*/coords, /*Object*/size){
395 // summary:
396 // Initialize the drag (see dojox.mdnd.Moveable.initOffsetDrag())
397 // node:
398 // The node which is about to be dragged
399 // coords:
400 // an object encapsulating X and Y position
401 // size:
402 // an object encapsulating width and height values
403 // tags:
404 // callback
405
406 //console.log("dojox.mdnd.AreaManager ::: onDragStart");
407 if(this.autoRefresh){
408 this._dropMode.updateAreas(this._areaList);
409 }
410
411 // Create the cover :
412 var _html = (sniff("webkit")) ? dojo.body() : dojo.body().parentNode;
413 if(!this._cover){
414 this._cover = domConstruct.create('div', {
415 'class': "dndCover"
416 });
417 this._cover2 = lang.clone(this._cover);
418 domClass.add(this._cover2, "dndCover2");
419 }
420 var h = _html.scrollHeight+"px";
421 this._cover.style.height = this._cover2.style.height = h;
422 dojo.body().appendChild(this._cover);
423 dojo.body().appendChild(this._cover2);
424
425 this._dragStartHandler = connect.connect(node.ownerDocument, "ondragstart", dojo, "stopEvent");
426 // to know the source
427 this._sourceIndexArea = this._lastValidIndexArea = this._currentIndexArea = this._getIndexArea(node.parentNode);
428 // delete the dragItem into the source area
429 var sourceArea = this._areaList[this._sourceIndexArea];
430 var children = sourceArea.items;
431 for(var i = 0; i < children.length; i++){
432 if(children[i].item.node == node){
433 this._dragItem = children[i];
434 this._dragItem.handlers.push(connect.connect(this._dragItem.item, "onDrag", this, "onDrag"));
435 this._dragItem.handlers.push(connect.connect(this._dragItem.item, "onDragEnd", this, "onDrop"));
436 children.splice(i,1);
437 this._currentDropIndex = this._sourceDropIndex = i;
438 break;
439 }
440 }
441 var nodeRef = null;
442 if(this._sourceDropIndex !== sourceArea.items.length){
443 nodeRef = sourceArea.items[this._sourceDropIndex].item.node;
444 }
445 // IE7 OPTIMIZATION
446 if(sniff("ie")> 7){
447 // connect these events on the cover
448 this._eventsIE7 = [
449 connect.connect(this._cover, "onmouseover", dojo, "stopEvent"),
450 connect.connect(this._cover, "onmouseout", dojo, "stopEvent"),
451 connect.connect(this._cover, "onmouseenter", dojo, "stopEvent"),
452 connect.connect(this._cover, "onmouseleave", dojo, "stopEvent")
453 ];
454 }
455
456 var s = node.style;
457 s.left = coords.x+"px";
458 s.top = coords.y+"px";
459 // attach the node to the cover
460 if(s.position == "relative" || s.position == ""){
461 s.position = "absolute"; // enforcing the absolute mode
462 }
463 this._cover.appendChild(node);
464
465 this._dropIndicator.place(sourceArea.node, nodeRef, size);
466 // add a style to place the _dragNode in foreground
467 domClass.add(node, "dragNode");
468 // A dragged node is always draggable in this source area.
469 this._accept = true;
470 connect.publish("/dojox/mdnd/drag/start",[node, sourceArea, this._sourceDropIndex]);
471 },
472
473 onDragEnter: function(/*Object*/coords, /*Object*/size){
474 // summary:
475 // Optionally called by the getTargetArea method of TargetFinder class.
476 // coords:
477 // coordinates of the dragged Node.
478 // size:
479 // size of the dragged Node.
480 // tags:
481 // callback
482
483 //console.log("dojox.mdnd.AreaManager ::: onDragEnter", coords, size);
484 if(this._currentIndexArea === this._sourceIndexArea){
485 this._accept = true;
486 }
487 else{
488 this._isAccepted(this._dragItem.type, this._areaList[this._currentIndexArea].accept);
489 }
490 },
491
492 onDragExit: function(/*Object*/coords, /*Object*/size){
493 // summary:
494 // Optionally called by the getTargetArea method of TargetFinder class.
495 // coords:
496 // coordinates of the dragged Node.
497 // size:
498 // size of the dragged Node.
499 // tags:
500 // callback
501
502 //console.log("dojox.mdnd.AreaManager ::: onDragExit");
503 this._accept = false;
504 },
505
506 onDrag: function(/*DOMNode*/node, /*Object*/coords, /*Object*/size, /*Object*/mousePosition){
507 // summary:
508 // Occurs when the dojo.dnd.Moveable.onDrag is fired.
509 // Search the nearest target area and called the placeDropIndicator
510 // node:
511 // The node which is dragged
512 // coords:
513 // an object encapsulating X and Y position
514 // size:
515 // an object encapsulating width and height values
516 // mousePosition:
517 // coordinates of mouse
518 // tags:
519 // callback
520
521 //console.log("dojox.mdnd.AreaManager ::: onDrag", node, ",", coords,size);
522 var coordinates = this._dropMode.getDragPoint(coords, size, mousePosition);
523 this.findCurrentIndexArea(coordinates, size);
524 if(this._currentIndexArea !== -1 && this._accept){
525 this.placeDropIndicator(coordinates, size);
526 }
527 },
528
529 placeDropIndicator: function(/*Object*/coords, /*Object*/size){
530 // summary:
531 // Search the right place to insert the dropIndicator and display the dropIndicator.
532 // coords:
533 // an object encapsulating X and Y position
534 // size:
535 // an object encapsulating width and height values
536 // returns:
537 // the current drop index
538
539 //console.log("dojox.mdnd.AreaManager ::: placeDropIndicator");
540 //keep old drop Index
541 this._oldDropIndex = this._currentDropIndex;
542 // calculate all children marker (see VerticalDropMode.initItems())
543 var area = this._areaList[this._currentIndexArea];
544 if(!area.initItems){
545 this._dropMode.initItems(area);
546 }
547 //get the index where the drop has to be placed.
548 this._currentDropIndex = this._dropMode.getDropIndex(area, coords);
549 if(!(this._currentIndexArea === this._oldIndexArea && this._oldDropIndex === this._currentDropIndex)){
550 this._placeDropIndicator(size);
551 }
552 return this._currentDropIndex; //Integer
553 },
554
555 _placeDropIndicator: function(/*Object*/size){
556 // summary:
557 // place the dropIndicator
558 // size:
559 // an object encapsulating width and height values
560 // tags:
561 // protected
562
563 var oldArea = this._areaList[this._lastValidIndexArea];
564 var currentArea = this._areaList[this._currentIndexArea];
565 //refresh the previous area after moving out the drop indicator
566 this._dropMode.refreshItems(oldArea, this._oldDropIndex, size, false);
567 // place dropIndicator
568 var node = null;
569 if(this._currentDropIndex != -1){
570 node = currentArea.items[this._currentDropIndex].item.node;
571 }
572 this._dropIndicator.place(currentArea.node, node);
573 this._lastValidIndexArea = this._currentIndexArea;
574 //refresh the current area after placing the drop indicator
575 this._dropMode.refreshItems(currentArea, this._currentDropIndex, size, true);
576 },
577
578 onDropCancel: function(){
579 // summary:
580 // Cancel the drop.
581 // The dragNode returns into the source.
582 // tags:
583 // callback
584
585 //console.log("dojox.mdnd.AreaManager ::: onDropCancel");
586 if(!this._accept){
587 var index = this._getIndexArea(this._dropIndicator.node.parentNode);
588 if(index != -1){
589 this._currentIndexArea = index;
590 }
591 else{
592 // case if the dropIndicator is in the area which has been unregistered during the drag.
593 // chose by default the first area.
594 this._currentIndexArea = 0;
595 }
596 }
597 },
598
599 onDrop: function(/*DOMNode*/node){
600 // summary:
601 // Drop the dragged item where the dropIndicator is displayed.
602 // node:
603 // The node which is about to be dropped
604 // tags:
605 // callback
606
607 //console.log("dojox.mdnd.AreaManager ::: onDrop");
608 //dropCancel
609 this.onDropCancel();
610 var targetArea = this._areaList[this._currentIndexArea];
611 domClass.remove(node, "dragNode");
612 var style = node.style;
613 style.position = "relative";
614 style.left = "0";
615 style.top = "0";
616 style.width = "auto";
617 if(targetArea.node == this._dropIndicator.node.parentNode){
618 targetArea.node.insertBefore(node, this._dropIndicator.node);
619 }
620 else{
621 // case if the dropIndicator is in the area which has been unregistered during the drag.
622 targetArea.node.appendChild(node);
623 this._currentDropIndex = targetArea.items.length;
624 }
625 // add child into the new target area.
626 var indexChild = this._currentDropIndex;
627 if(indexChild == -1){
628 indexChild = targetArea.items.length;
629 }
630 var children = targetArea.items;
631 var firstListArea = children.slice(0, indexChild);
632 var lastListArea = children.slice(indexChild, children.length);
633 firstListArea[firstListArea.length] = this._dragItem;
634 targetArea.items = firstListArea.concat(lastListArea);
635
636 this._setMarginArea(targetArea, node);
637 array.forEach(this._areaList, function(obj){
638 obj.initItems = false;
639 });
640 // disconnect onDrop handler
641 connect.disconnect(this._dragItem.handlers.pop());
642 connect.disconnect(this._dragItem.handlers.pop());
643 this._resetAfterDrop();
644 // remove the cover
645 if(this._cover){
646 dojo.body().removeChild(this._cover);
647 dojo.body().removeChild(this._cover2);
648 }
649 connect.publish("/dojox/mdnd/drop",[node, targetArea, indexChild]);
650 },
651
652 _resetAfterDrop: function(){
653 // summary:
654 // reset manager properties after dropping an item
655 // tags:
656 // protected
657
658 this._accept = false;
659 this._dragItem = null;
660 this._currentDropIndex = -1;
661 this._currentIndexArea = -1;
662 this._oldDropIndex = -1;
663 this._sourceIndexArea = -1;
664 this._sourceDropIndex = -1;
665 this._dropIndicator.remove();
666 if(this._dragStartHandler){
667 connect.disconnect(this._dragStartHandler);
668 }
669 if(sniff("ie") > 7){
670 array.forEach(this._eventsIE7, connect.disconnect);
671 }
672 },
673
674 destroy: function(){
675 // summary:
676 // Destroy the component.
677
678 //console.log("dojox.mdnd.AreaManager ::: destroy");
679 //see implementation of unregister()
680 while(this._areaList.length > 0){
681 if(!this.unregister(this._areaList[0].node)){
682 throw new Error("Error while destroying AreaManager");
683 }
684 }
685 connect.disconnect(this.resizeHandler);
686 this._dropIndicator.destroy();
687 this._dropMode.destroy();
688 if(AutoScroll.autoScroll){
689 AutoScroll.autoScroll.destroy();
690 }
691 if(this.refreshListener){
692 connect.unsubscribe(this.refreshListener);
693 }
694 // destroy the cover
695 if(this._cover){
696 domConstruct.destroy(this._cover);
697 domConstruct.destroy(this._cover2);
698 delete this._cover;
699 delete this._cover2;
700 }
701 }
702 });
703
704 if(_Widget){
705 // Add a new property to widget
706 lang.extend(_Widget, {
707 // dndType: String
708 // Defines a type of widget.
709 dndType : "text"
710 });
711 }
712
713 am._areaManager = null;
714 am.areaManager = function(){
715 // summary:
716 // Returns the current areaManager, creates one if it is not created yet.
717 if(!am._areaManager){
718 am._areaManager = new am();
719 }
720 return am._areaManager; // Object
721 };
722 return am;
723 });