]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dijit/_tree/dndSource.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dijit / _tree / dndSource.js
1 if(!dojo._hasResource["dijit._tree.dndSource"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit._tree.dndSource"] = true;
3 dojo.provide("dijit._tree.dndSource");
4
5 dojo.require("dijit._tree.dndSelector");
6 dojo.require("dojo.dnd.Manager");
7
8 dojo.declare("dijit._tree.dndSource", dijit._tree.dndSelector, {
9         // summary: a Source object, which can be used as a DnD source, or a DnD target
10         
11         // object attributes (for markup)
12         isSource: true,
13         copyOnly: false,
14         skipForm: false,
15         accept: ["text"],
16         
17         constructor: function(tree, params){
18                 // summary: a constructor of the Source
19                 // tree: dijit.Tree: the tree widget to build the source on
20                 // params: Object: a dict of parameters, recognized parameters are:
21                 //      isSource: Boolean: can be used as a DnD source, if true; assumed to be "true" if omitted
22                 //      accept: Array: list of accepted types (text strings) for a target; assumed to be ["text"] if omitted
23                 //      horizontal: Boolean: a horizontal container, if true, vertical otherwise or when omitted
24                 //      copyOnly: Boolean: always copy items, if true, use a state of Ctrl key otherwise
25                 //      skipForm: Boolean: don't start the drag operation, if clicked on form elements
26                 //      the rest of parameters are passed to the selector
27                 if(!params){ params = {}; }
28                 dojo.mixin(this, params);
29                 this.isSource = typeof params.isSource == "undefined" ? true : params.isSource;
30                 var type = params.accept instanceof Array ? params.accept : ["text"];
31                 this.accept = null;
32                 if(type.length){
33                         this.accept = {};
34                         for(var i = 0; i < type.length; ++i){
35                                 this.accept[type[i]] = 1;
36                         }
37                 }
38
39                 // class-specific variables
40                 this.isDragging = false;
41                 this.mouseDown = false;
42                 this.targetAnchor = null;
43                 this.targetBox = null;
44                 this.before = true;
45
46                 // states
47                 this.sourceState  = "";
48                 if(this.isSource){
49                         dojo.addClass(this.node, "dojoDndSource");
50                 }
51                 this.targetState  = "";
52                 if(this.accept){
53                         dojo.addClass(this.node, "dojoDndTarget");
54                 }
55                 if(this.horizontal){
56                         dojo.addClass(this.node, "dojoDndHorizontal");
57                 }
58                 // set up events
59                 this.topics = [
60                         dojo.subscribe("/dnd/source/over", this, "onDndSourceOver"),
61                         dojo.subscribe("/dnd/start",  this, "onDndStart"),
62                         dojo.subscribe("/dnd/drop",   this, "onDndDrop"),
63                         dojo.subscribe("/dnd/cancel", this, "onDndCancel")
64                 ];
65         },
66
67         startup: function(){
68         },
69         
70         // methods
71         checkAcceptance: function(source, nodes){
72                 // summary: checks, if the target can accept nodes from this source
73                 // source: Object: the source which provides items
74                 // nodes: Array: the list of transferred items
75                 return true;    // Boolean
76         },
77         copyState: function(keyPressed){
78                 // summary: Returns true, if we need to copy items, false to move.
79                 //              It is separated to be overwritten dynamically, if needed.
80                 // keyPressed: Boolean: the "copy" was pressed
81                 return this.copyOnly || keyPressed;     // Boolean
82         },
83         destroy: function(){
84                 // summary: prepares the object to be garbage-collected
85                 this.inherited("destroy",arguments);
86                 dojo.forEach(this.topics, dojo.unsubscribe);
87                 this.targetAnchor = null;
88         },
89
90         // markup methods
91         markupFactory: function(params, node){
92                 params._skipStartup = true;
93                 return new dijit._tree.dndSource(node, params);
94         },
95
96         // mouse event processors
97         onMouseMove: function(e){
98                 // summary: event processor for onmousemove
99                 // e: Event: mouse event
100                 if(this.isDragging && this.targetState == "Disabled"){ return; }
101                 this.inherited("onMouseMove", arguments);
102                 var m = dojo.dnd.manager();
103                 if(this.isDragging){
104                         // calculate before/after
105
106                         if (this.allowBetween){ // not implemented yet for tree since it has no concept of order
107                                 var before = false;
108                                 if(this.current){
109                                         if(!this.targetBox || this.targetAnchor != this.current){
110                                                 this.targetBox = {
111                                                         xy: dojo.coords(this.current, true),
112                                                         w: this.current.offsetWidth,
113                                                         h: this.current.offsetHeight
114                                                 };
115                                         }
116                                         if(this.horizontal){
117                                                 before = (e.pageX - this.targetBox.xy.x) < (this.targetBox.w / 2);
118                                         }else{
119                                                 before = (e.pageY - this.targetBox.xy.y) < (this.targetBox.h / 2);
120                                         }
121                                 }
122                                 if(this.current != this.targetAnchor || before != this.before){
123                                         this._markTargetAnchor(before);
124                                         m.canDrop(!this.current || m.source != this || !(this.current.id in this.selection));
125                                 }
126                         }
127                 }else{
128                         if(this.mouseDown && this.isSource){
129                                 var n = this.getSelectedNodes();
130                                 var nodes=[];
131                                 for (var i in n){
132                                         nodes.push(n[i]);
133                                 }
134                                 if(nodes.length){
135                                         m.startDrag(this, nodes, this.copyState(dojo.dnd.getCopyKeyState(e)));
136                                 }
137                         }
138                 }
139         },
140
141         onMouseDown: function(e){
142                 // summary: event processor for onmousedown
143                 // e: Event: mouse event
144                 this.mouseDown = true;
145                 this.mouseButton = e.button;
146                 this.inherited("onMouseDown",arguments);
147         },
148
149         onMouseUp: function(e){
150                 // summary: event processor for onmouseup
151                 // e: Event: mouse event
152                 if(this.mouseDown){
153                         this.mouseDown = false;
154                         this.inherited("onMouseUp",arguments);
155                 }
156         },
157
158         onMouseOver: function(e){
159                 // summary: event processor for onmouseover
160                 // e: Event: mouse event
161
162                 // handle when mouse has just moved over the Tree itself (not a TreeNode, but the Tree)
163                 var rt = e.relatedTarget;       // the previous location
164                 while(rt){
165                         if(rt == this.node){ break; }
166                         try{
167                                 rt = rt.parentNode;
168                         }catch(x){
169                                 rt = null;
170                         }
171                 }
172                 if(!rt){
173                         this._changeState("Container", "Over");
174                         this.onOverEvent();
175                 }
176
177                 // code below is for handling depending on which TreeNode we are over
178                 var n = this._getChildByEvent(e);       // the TreeNode
179                 if(this.current == n){ return; }
180                 if(this.current){ this._removeItemClass(this.current, "Over"); }
181                 var m = dojo.dnd.manager();
182                 if(n){ 
183                         this._addItemClass(n, "Over"); 
184                         if(this.isDragging){
185                                 if(this.checkItemAcceptance(n,m.source)){
186                                         m.canDrop(this.targetState != "Disabled" && (!this.current || m.source != this || !(n in this.selection)));
187                                 }else{
188                                         m.canDrop(false);
189                                 }
190                         }
191                 }else{
192                         if(this.isDragging){
193                                 if (m.source && this.checkAcceptance(m.source,m.source.getSelectedNodes())){
194                                         m.canDrop(this.targetState != "Disabled" && (!this.current || m.source != this || !(this.current.id in this.selection)));
195                                 }else{
196                                         m.canDrop(false);
197                                 }
198                         }
199                 }
200                 this.current = n;
201         },
202
203         checkItemAcceptance: function(node, source){
204                 // summary: stub funciton to be overridden if one wants to check for the ability to drop at the node/item level 
205                 return true;    
206         },
207         
208         // topic event processors
209         onDndSourceOver: function(source){
210                 // summary: topic event processor for /dnd/source/over, called when detected a current source
211                 // source: Object: the source which has the mouse over it
212                 if(this != source){
213                         this.mouseDown = false;
214                         if(this.targetAnchor){
215                                 this._unmarkTargetAnchor();
216                         }
217                 }else if(this.isDragging){
218                         var m = dojo.dnd.manager();
219                         m.canDrop(this.targetState != "Disabled" && (!this.current || m.source != this || !(this.current.id in this.selection)));
220                 }
221         },
222         onDndStart: function(source, nodes, copy){
223                 // summary: topic event processor for /dnd/start, called to initiate the DnD operation
224                 // source: Object: the source which provides items
225                 // nodes: Array: the list of transferred items
226                 // copy: Boolean: copy items, if true, move items otherwise
227
228                 if(this.isSource){
229                         this._changeState("Source", this == source ? (copy ? "Copied" : "Moved") : "");
230                 }
231                 var accepted = this.checkAcceptance(source, nodes);
232
233                 this._changeState("Target", accepted ? "" : "Disabled");
234
235                 if(accepted){
236                         dojo.dnd.manager().overSource(this);
237                 }
238
239                 this.isDragging = true;
240         },
241
242         itemCreator: function(nodes){
243                 return dojo.map(nodes, function(node){
244                         return {
245                                 "id": node.id,
246                                 "name": node.textContent || node.innerText || ""
247                         };
248                 });
249         },
250
251         onDndDrop: function(source, nodes, copy){
252                 // summary:
253                 //              Topic event processor for /dnd/drop, called to finish the DnD operation..
254                 //              Updates data store items according to where node was dragged from and dropped
255                 //              to.   The tree will then respond to those data store updates and redraw itself.
256                 // source: Object: the source which provides items
257                 // nodes: Array: the list of transferred items
258                 // copy: Boolean: copy items, if true, move items otherwise
259
260                 if(this.containerState == "Over"){
261                         var tree = this.tree,
262                                 model = tree.model,
263                                 target = this.current,
264                                 requeryRoot = false;    // set to true iff top level items change
265
266                         this.isDragging = false;
267
268                         // Compute the new parent item
269                         var targetWidget = dijit.getEnclosingWidget(target),
270                                 newParentItem = (targetWidget && targetWidget.item) || tree.item;
271
272                         // If we are dragging from another source (or at least, another source
273                         // that points to a different data store), then we need to make new data
274                         // store items for each element in nodes[].  This call get the parameters
275                         // to pass to store.newItem()
276                         var newItemsParams;
277                         if(source != this){
278                                 newItemsParams = this.itemCreator(nodes, target);
279                         }
280
281                         dojo.forEach(nodes, function(node, idx){
282                                 if(source == this){
283                                         // This is a node from my own tree, and we are moving it, not copying.
284                                         // Remove item from old parent's children attribute.
285                                         // TODO: dijit._tree.dndSelector should implement deleteSelectedNodes()
286                                         // and this code should go there.
287                                         var childTreeNode = dijit.getEnclosingWidget(node),
288                                                 childItem = childTreeNode.item,
289                                                 oldParentItem = childTreeNode.getParent().item;
290
291                                         model.pasteItem(childItem, oldParentItem, newParentItem, copy);
292                                 }else{
293                                         model.newItem(newItemsParams[idx], newParentItem);
294                                 }
295                         }, this);
296
297                         // Expand the target node (if it's currently collapsed) so the user can see
298                         // where their node was dropped.   In particular since that node is still selected.
299                         this.tree._expandNode(targetWidget);
300                 }
301                 this.onDndCancel();
302         },
303         onDndCancel: function(){
304                 // summary: topic event processor for /dnd/cancel, called to cancel the DnD operation
305                 if(this.targetAnchor){
306                         this._unmarkTargetAnchor();
307                         this.targetAnchor = null;
308                 }
309                 this.before = true;
310                 this.isDragging = false;
311                 this.mouseDown = false;
312                 delete this.mouseButton;
313                 this._changeState("Source", "");
314                 this._changeState("Target", "");
315         },
316         
317         // utilities
318
319         onOverEvent: function(){
320                 // summary: this function is called once, when mouse is over our container
321                 this.inherited("onOverEvent",arguments);
322                 dojo.dnd.manager().overSource(this);
323         },
324         onOutEvent: function(){
325                 // summary: this function is called once, when mouse is out of our container
326                 this.inherited("onOutEvent",arguments); 
327                 dojo.dnd.manager().outSource(this);
328         },
329         _markTargetAnchor: function(before){
330                 // summary: assigns a class to the current target anchor based on "before" status
331                 // before: Boolean: insert before, if true, after otherwise
332                 if(this.current == this.targetAnchor && this.before == before){ return; }
333                 if(this.targetAnchor){
334                         this._removeItemClass(this.targetAnchor, this.before ? "Before" : "After");
335                 }
336                 this.targetAnchor = this.current;
337                 this.targetBox = null;
338                 this.before = before;
339                 if(this.targetAnchor){
340                         this._addItemClass(this.targetAnchor, this.before ? "Before" : "After");
341                 }
342         },
343         _unmarkTargetAnchor: function(){
344                 // summary: removes a class of the current target anchor based on "before" status
345                 if(!this.targetAnchor){ return; }
346                 this._removeItemClass(this.targetAnchor, this.before ? "Before" : "After");
347                 this.targetAnchor = null;
348                 this.targetBox = null;
349                 this.before = true;
350         },
351         _markDndStatus: function(copy){
352                 // summary: changes source's state based on "copy" status
353                 this._changeState("Source", copy ? "Copied" : "Moved");
354         }
355 });
356
357 dojo.declare("dijit._tree.dndTarget", dijit._tree.dndSource, {
358         // summary: a Target object, which can be used as a DnD target
359         
360         constructor: function(node, params){
361                 // summary: a constructor of the Target --- see the Source constructor for details
362                 this.isSource = false;
363                 dojo.removeClass(this.node, "dojoDndSource");
364         },
365
366         // markup methods
367         markupFactory: function(params, node){
368                 params._skipStartup = true;
369                 return new dijit._tree.dndTarget(node, params);
370         }
371 });
372
373 }