]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/dnd/Source.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dojo / dnd / Source.js
1 if(!dojo._hasResource["dojo.dnd.Source"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.dnd.Source"] = true;
3 dojo.provide("dojo.dnd.Source");
4
5 dojo.require("dojo.dnd.Selector");
6 dojo.require("dojo.dnd.Manager");
7
8 /*
9         Container property:
10                 "Horizontal"- if this is the horizontal container
11         Source states:
12                 ""                      - normal state
13                 "Moved"         - this source is being moved
14                 "Copied"        - this source is being copied
15         Target states:
16                 ""                      - normal state
17                 "Disabled"      - the target cannot accept an avatar
18         Target anchor state:
19                 ""                      - item is not selected
20                 "Before"        - insert point is before the anchor
21                 "After"         - insert point is after the anchor
22 */
23
24 /*=====
25 dojo.dnd.__SourceArgs = function(){
26         //      summary:
27         //              a dict of parameters for DnD Source configuration. Note that any
28         //              property on Source elements may be configured, but this is the
29         //              short-list
30         //      isSource: Boolean?
31         //              can be used as a DnD source. Defaults to true.
32         //      accept: Array?
33         //              list of accepted types (text strings) for a target; defaults to
34         //              ["text"]
35         //      horizontal: Boolean?
36         //              a horizontal container, if true, vertical otherwise or when omitted
37         //      copyOnly: Boolean?
38         //              always copy items, if true, use a state of Ctrl key otherwise
39         //      withHandles: Boolean?
40         //              allows dragging only by handles
41         this.isSource = isSource;
42         this.accept = accept;
43         this.horizontal = horizontal;
44         this.copyOnly = copyOnly;
45         this.withHandles = withHandles;
46 }
47 =====*/
48
49 dojo.declare("dojo.dnd.Source", dojo.dnd.Selector, {
50         // summary: a Source object, which can be used as a DnD source, or a DnD target
51         
52         // object attributes (for markup)
53         isSource: true,
54         horizontal: false,
55         copyOnly: false,
56         skipForm: false,
57         withHandles: false,
58         accept: ["text"],
59         
60         constructor: function(/*DOMNode|String*/node, /*dojo.dnd.__SourceArgs?*/params){
61                 // summary: 
62                 //              a constructor of the Source
63                 // node:
64                 //              node or node's id to build the source on
65                 // params: 
66                 //              any property of this class may be configured via the params
67                 //              object which is mixed-in to the `dojo.dnd.Source` instance
68                 dojo.mixin(this, dojo.mixin({}, params));
69                 var type = this.accept;
70                 if(type.length){
71                         this.accept = {};
72                         for(var i = 0; i < type.length; ++i){
73                                 this.accept[type[i]] = 1;
74                         }
75                 }
76                 // class-specific variables
77                 this.isDragging = false;
78                 this.mouseDown = false;
79                 this.targetAnchor = null;
80                 this.targetBox = null;
81                 this.before = true;
82                 // states
83                 this.sourceState  = "";
84                 if(this.isSource){
85                         dojo.addClass(this.node, "dojoDndSource");
86                 }
87                 this.targetState  = "";
88                 if(this.accept){
89                         dojo.addClass(this.node, "dojoDndTarget");
90                 }
91                 if(this.horizontal){
92                         dojo.addClass(this.node, "dojoDndHorizontal");
93                 }
94                 // set up events
95                 this.topics = [
96                         dojo.subscribe("/dnd/source/over", this, "onDndSourceOver"),
97                         dojo.subscribe("/dnd/start",  this, "onDndStart"),
98                         dojo.subscribe("/dnd/drop",   this, "onDndDrop"),
99                         dojo.subscribe("/dnd/cancel", this, "onDndCancel")
100                 ];
101         },
102         
103         // methods
104         checkAcceptance: function(source, nodes){
105                 // summary: checks, if the target can accept nodes from this source
106                 // source: Object: the source which provides items
107                 // nodes: Array: the list of transferred items
108                 if(this == source){ return true; }
109                 for(var i = 0; i < nodes.length; ++i){
110                         var type = source.getItem(nodes[i].id).type;
111                         // type instanceof Array
112                         var flag = false;
113                         for(var j = 0; j < type.length; ++j){
114                                 if(type[j] in this.accept){
115                                         flag = true;
116                                         break;
117                                 }
118                         }
119                         if(!flag){
120                                 return false;   // Boolean
121                         }
122                 }
123                 return true;    // Boolean
124         },
125         copyState: function(keyPressed){
126                 // summary: Returns true, if we need to copy items, false to move.
127                 //              It is separated to be overwritten dynamically, if needed.
128                 // keyPressed: Boolean: the "copy" was pressed
129                 return this.copyOnly || keyPressed;     // Boolean
130         },
131         destroy: function(){
132                 // summary: prepares the object to be garbage-collected
133                 dojo.dnd.Source.superclass.destroy.call(this);
134                 dojo.forEach(this.topics, dojo.unsubscribe);
135                 this.targetAnchor = null;
136         },
137
138         // markup methods
139         markupFactory: function(params, node){
140                 params._skipStartup = true;
141                 return new dojo.dnd.Source(node, params);
142         },
143
144         // mouse event processors
145         onMouseMove: function(e){
146                 // summary: event processor for onmousemove
147                 // e: Event: mouse event
148                 if(this.isDragging && this.targetState == "Disabled"){ return; }
149                 dojo.dnd.Source.superclass.onMouseMove.call(this, e);
150                 var m = dojo.dnd.manager();
151                 if(this.isDragging){
152                         // calculate before/after
153                         var before = false;
154                         if(this.current){
155                                 if(!this.targetBox || this.targetAnchor != this.current){
156                                         this.targetBox = {
157                                                 xy: dojo.coords(this.current, true),
158                                                 w: this.current.offsetWidth,
159                                                 h: this.current.offsetHeight
160                                         };
161                                 }
162                                 if(this.horizontal){
163                                         before = (e.pageX - this.targetBox.xy.x) < (this.targetBox.w / 2);
164                                 }else{
165                                         before = (e.pageY - this.targetBox.xy.y) < (this.targetBox.h / 2);
166                                 }
167                         }
168                         if(this.current != this.targetAnchor || before != this.before){
169                                 this._markTargetAnchor(before);
170                                 m.canDrop(!this.current || m.source != this || !(this.current.id in this.selection));
171                         }
172                 }else{
173                         if(this.mouseDown && this.isSource){
174                                 var nodes = this.getSelectedNodes();
175                                 if(nodes.length){
176                                         m.startDrag(this, nodes, this.copyState(dojo.dnd.getCopyKeyState(e)));
177                                 }
178                         }
179                 }
180         },
181         onMouseDown: function(e){
182                 // summary: event processor for onmousedown
183                 // e: Event: mouse event
184                 if(this._legalMouseDown(e) && (!this.skipForm || !dojo.dnd.isFormElement(e))){
185                         this.mouseDown = true;
186                         this.mouseButton = e.button;
187                         dojo.dnd.Source.superclass.onMouseDown.call(this, e);
188                 }
189         },
190         onMouseUp: function(e){
191                 // summary: event processor for onmouseup
192                 // e: Event: mouse event
193                 if(this.mouseDown){
194                         this.mouseDown = false;
195                         dojo.dnd.Source.superclass.onMouseUp.call(this, e);
196                 }
197         },
198         
199         // topic event processors
200         onDndSourceOver: function(source){
201                 // summary: topic event processor for /dnd/source/over, called when detected a current source
202                 // source: Object: the source which has the mouse over it
203                 if(this != source){
204                         this.mouseDown = false;
205                         if(this.targetAnchor){
206                                 this._unmarkTargetAnchor();
207                         }
208                 }else if(this.isDragging){
209                         var m = dojo.dnd.manager();
210                         m.canDrop(this.targetState != "Disabled" && (!this.current || m.source != this || !(this.current.id in this.selection)));
211                 }
212         },
213         onDndStart: function(source, nodes, copy){
214                 // summary: topic event processor for /dnd/start, called to initiate the DnD operation
215                 // source: Object: the source which provides items
216                 // nodes: Array: the list of transferred items
217                 // copy: Boolean: copy items, if true, move items otherwise
218                 if(this.isSource){
219                         this._changeState("Source", this == source ? (copy ? "Copied" : "Moved") : "");
220                 }
221                 var accepted = this.accept && this.checkAcceptance(source, nodes);
222                 this._changeState("Target", accepted ? "" : "Disabled");
223                 if(accepted && this == source){
224                         dojo.dnd.manager().overSource(this);
225                 }
226                 this.isDragging = true;
227         },
228         onDndDrop: function(source, nodes, copy){
229                 // summary: topic event processor for /dnd/drop, called to finish the DnD operation
230                 // source: Object: the source which provides items
231                 // nodes: Array: the list of transferred items
232                 // copy: Boolean: copy items, if true, move items otherwise
233                 do{ //break box
234                         if(this.containerState != "Over"){ break; }
235                         var oldCreator = this._normalizedCreator;
236                         if(this != source){
237                                 // transferring nodes from the source to the target
238                                 if(this.creator){
239                                         // use defined creator
240                                         this._normalizedCreator = function(node, hint){
241                                                 return oldCreator.call(this, source.getItem(node.id).data, hint);
242                                         };
243                                 }else{
244                                         // we have no creator defined => move/clone nodes
245                                         if(copy){
246                                                 // clone nodes
247                                                 this._normalizedCreator = function(node, hint){
248                                                         var t = source.getItem(node.id);
249                                                         var n = node.cloneNode(true);
250                                                         n.id = dojo.dnd.getUniqueId();
251                                                         return {node: n, data: t.data, type: t.type};
252                                                 };
253                                         }else{
254                                                 // move nodes
255                                                 this._normalizedCreator = function(node, hint){
256                                                         var t = source.getItem(node.id);
257                                                         source.delItem(node.id);
258                                                         return {node: node, data: t.data, type: t.type};
259                                                 };
260                                         }
261                                 }
262                         }else{
263                                 // transferring nodes within the single source
264                                 if(this.current && this.current.id in this.selection){ break; }
265                                 if(this.creator){
266                                         // use defined creator
267                                         if(copy){
268                                                 // create new copies of data items
269                                                 this._normalizedCreator = function(node, hint){
270                                                         return oldCreator.call(this, source.getItem(node.id).data, hint);
271                                                 };
272                                         }else{
273                                                 // move nodes
274                                                 if(!this.current){ break; }
275                                                 this._normalizedCreator = function(node, hint){
276                                                         var t = source.getItem(node.id);
277                                                         return {node: node, data: t.data, type: t.type};
278                                                 };
279                                         }
280                                 }else{
281                                         // we have no creator defined => move/clone nodes
282                                         if(copy){
283                                                 // clone nodes
284                                                 this._normalizedCreator = function(node, hint){
285                                                         var t = source.getItem(node.id);
286                                                         var n = node.cloneNode(true);
287                                                         n.id = dojo.dnd.getUniqueId();
288                                                         return {node: n, data: t.data, type: t.type};
289                                                 };
290                                         }else{
291                                                 // move nodes
292                                                 if(!this.current){ break; }
293                                                 this._normalizedCreator = function(node, hint){
294                                                         var t = source.getItem(node.id);
295                                                         return {node: node, data: t.data, type: t.type};
296                                                 };
297                                         }
298                                 }
299                         }
300                         this._removeSelection();
301                         if(this != source){
302                                 this._removeAnchor();
303                         }
304                         if(this != source && !copy && !this.creator){
305                                 source.selectNone();
306                         }
307                         this.insertNodes(true, nodes, this.before, this.current);
308                         if(this != source && !copy && this.creator){
309                                 source.deleteSelectedNodes();
310                         }
311                         this._normalizedCreator = oldCreator;
312                 }while(false);
313                 this.onDndCancel();
314         },
315         onDndCancel: function(){
316                 // summary: topic event processor for /dnd/cancel, called to cancel the DnD operation
317                 if(this.targetAnchor){
318                         this._unmarkTargetAnchor();
319                         this.targetAnchor = null;
320                 }
321                 this.before = true;
322                 this.isDragging = false;
323                 this.mouseDown = false;
324                 delete this.mouseButton;
325                 this._changeState("Source", "");
326                 this._changeState("Target", "");
327         },
328         
329         // utilities
330         onOverEvent: function(){
331                 // summary: this function is called once, when mouse is over our container
332                 dojo.dnd.Source.superclass.onOverEvent.call(this);
333                 dojo.dnd.manager().overSource(this);
334         },
335         onOutEvent: function(){
336                 // summary: this function is called once, when mouse is out of our container
337                 dojo.dnd.Source.superclass.onOutEvent.call(this);
338                 dojo.dnd.manager().outSource(this);
339         },
340         _markTargetAnchor: function(before){
341                 // summary: assigns a class to the current target anchor based on "before" status
342                 // before: Boolean: insert before, if true, after otherwise
343                 if(this.current == this.targetAnchor && this.before == before){ return; }
344                 if(this.targetAnchor){
345                         this._removeItemClass(this.targetAnchor, this.before ? "Before" : "After");
346                 }
347                 this.targetAnchor = this.current;
348                 this.targetBox = null;
349                 this.before = before;
350                 if(this.targetAnchor){
351                         this._addItemClass(this.targetAnchor, this.before ? "Before" : "After");
352                 }
353         },
354         _unmarkTargetAnchor: function(){
355                 // summary: removes a class of the current target anchor based on "before" status
356                 if(!this.targetAnchor){ return; }
357                 this._removeItemClass(this.targetAnchor, this.before ? "Before" : "After");
358                 this.targetAnchor = null;
359                 this.targetBox = null;
360                 this.before = true;
361         },
362         _markDndStatus: function(copy){
363                 // summary: changes source's state based on "copy" status
364                 this._changeState("Source", copy ? "Copied" : "Moved");
365         },
366         _legalMouseDown: function(e){
367                 // summary: checks if user clicked on "approved" items
368                 // e: Event: mouse event
369                 if(!this.withHandles){ return true; }
370                 for(var node = e.target; node && !dojo.hasClass(node, "dojoDndItem"); node = node.parentNode){
371                         if(dojo.hasClass(node, "dojoDndHandle")){ return true; }
372                 }
373                 return false;   // Boolean
374         }
375 });
376
377 dojo.declare("dojo.dnd.Target", dojo.dnd.Source, {
378         // summary: a Target object, which can be used as a DnD target
379         
380         constructor: function(node, params){
381                 // summary: a constructor of the Target --- see the Source constructor for details
382                 this.isSource = false;
383                 dojo.removeClass(this.node, "dojoDndSource");
384         },
385
386         // markup methods
387         markupFactory: function(params, node){
388                 params._skipStartup = true;
389                 return new dojo.dnd.Target(node, params);
390         }
391 });
392
393 }