Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dijit / tree / dndSource.js @ 13

History | View | Annotate | Download (5.89 KB)

1 9 andrej.cim
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6
7
8
if(!dojo._hasResource["dijit.tree.dndSource"]){
9
dojo._hasResource["dijit.tree.dndSource"]=true;
10
dojo.provide("dijit.tree.dndSource");
11
dojo.require("dijit.tree._dndSelector");
12
dojo.require("dojo.dnd.Manager");
13
dojo.declare("dijit.tree.dndSource",dijit.tree._dndSelector,{isSource:true,accept:["text","treeNode"],copyOnly:false,dragThreshold:5,betweenThreshold:0,constructor:function(_1,_2){
14
if(!_2){
15
_2={};
16
}
17
dojo.mixin(this,_2);
18
this.isSource=typeof _2.isSource=="undefined"?true:_2.isSource;
19
var _3=_2.accept instanceof Array?_2.accept:["text","treeNode"];
20
this.accept=null;
21
if(_3.length){
22
this.accept={};
23
for(var i=0;i<_3.length;++i){
24
this.accept[_3[i]]=1;
25
}
26
}
27
this.isDragging=false;
28
this.mouseDown=false;
29
this.targetAnchor=null;
30
this.targetBox=null;
31
this.dropPosition="";
32
this._lastX=0;
33
this._lastY=0;
34
this.sourceState="";
35
if(this.isSource){
36
dojo.addClass(this.node,"dojoDndSource");
37
}
38
this.targetState="";
39
if(this.accept){
40
dojo.addClass(this.node,"dojoDndTarget");
41
}
42
this.topics=[dojo.subscribe("/dnd/source/over",this,"onDndSourceOver"),dojo.subscribe("/dnd/start",this,"onDndStart"),dojo.subscribe("/dnd/drop",this,"onDndDrop"),dojo.subscribe("/dnd/cancel",this,"onDndCancel")];
43
},checkAcceptance:function(_4,_5){
44
return true;
45
},copyState:function(_6){
46
return this.copyOnly||_6;
47
},destroy:function(){
48
this.inherited("destroy",arguments);
49
dojo.forEach(this.topics,dojo.unsubscribe);
50
this.targetAnchor=null;
51
},_onDragMouse:function(e){
52
var m=dojo.dnd.manager(),_7=this.targetAnchor,_8=this.current,_9=this.currentWidget,_a=this.dropPosition;
53
var _b="Over";
54
if(_8&&this.betweenThreshold>0){
55
if(!this.targetBox||_7!=_8){
56
this.targetBox=dojo.position(_8,true);
57
}
58
if((e.pageY-this.targetBox.y)<=this.betweenThreshold){
59
_b="Before";
60
}else{
61
if((e.pageY-this.targetBox.y)>=(this.targetBox.h-this.betweenThreshold)){
62
_b="After";
63
}
64
}
65
}
66
if(_8!=_7||_b!=_a){
67
if(_7){
68
this._removeItemClass(_7,_a);
69
}
70
if(_8){
71
this._addItemClass(_8,_b);
72
}
73
if(!_8){
74
m.canDrop(false);
75
}else{
76
if(_9==this.tree.rootNode&&_b!="Over"){
77
m.canDrop(false);
78
}else{
79
if(m.source==this&&(_8.id in this.selection)){
80
m.canDrop(false);
81
}else{
82
if(this.checkItemAcceptance(_8,m.source,_b.toLowerCase())&&!this._isParentChildDrop(m.source,_8)){
83
m.canDrop(true);
84
}else{
85
m.canDrop(false);
86
}
87
}
88
}
89
}
90
this.targetAnchor=_8;
91
this.dropPosition=_b;
92
}
93
},onMouseMove:function(e){
94
if(this.isDragging&&this.targetState=="Disabled"){
95
return;
96
}
97
this.inherited(arguments);
98
var m=dojo.dnd.manager();
99
if(this.isDragging){
100
this._onDragMouse(e);
101
}else{
102
if(this.mouseDown&&this.isSource&&(Math.abs(e.pageX-this._lastX)>=this.dragThreshold||Math.abs(e.pageY-this._lastY)>=this.dragThreshold)){
103
var n=this.getSelectedNodes();
104
var _c=[];
105
for(var i in n){
106
_c.push(n[i]);
107
}
108
if(_c.length){
109
m.startDrag(this,_c,this.copyState(dojo.isCopyKey(e)));
110
}
111
}
112
}
113
},onMouseDown:function(e){
114
this.mouseDown=true;
115
this.mouseButton=e.button;
116
this._lastX=e.pageX;
117
this._lastY=e.pageY;
118
this.inherited("onMouseDown",arguments);
119
},onMouseUp:function(e){
120
if(this.mouseDown){
121
this.mouseDown=false;
122
this.inherited("onMouseUp",arguments);
123
}
124
},onMouseOut:function(){
125
this.inherited(arguments);
126
this._unmarkTargetAnchor();
127
},checkItemAcceptance:function(_d,_e,_f){
128
return true;
129
},onDndSourceOver:function(_10){
130
if(this!=_10){
131
this.mouseDown=false;
132
this._unmarkTargetAnchor();
133
}else{
134
if(this.isDragging){
135
var m=dojo.dnd.manager();
136
m.canDrop(false);
137
}
138
}
139
},onDndStart:function(_11,_12,_13){
140
if(this.isSource){
141
this._changeState("Source",this==_11?(_13?"Copied":"Moved"):"");
142
}
143
var _14=this.checkAcceptance(_11,_12);
144
this._changeState("Target",_14?"":"Disabled");
145
if(this==_11){
146
dojo.dnd.manager().overSource(this);
147
}
148
this.isDragging=true;
149
},itemCreator:function(_15,_16,_17){
150
return dojo.map(_15,function(_18){
151
return {"id":_18.id,"name":_18.textContent||_18.innerText||""};
152
});
153
},onDndDrop:function(_19,_1a,_1b){
154
if(this.containerState=="Over"){
155
var _1c=this.tree,_1d=_1c.model,_1e=this.targetAnchor,_1f=false;
156
this.isDragging=false;
157
var _20=dijit.getEnclosingWidget(_1e);
158
var _21;
159
var _22;
160
_21=(_20&&_20.item)||_1c.item;
161
if(this.dropPosition=="Before"||this.dropPosition=="After"){
162
_21=(_20.getParent()&&_20.getParent().item)||_1c.item;
163
_22=_20.getIndexInParent();
164
if(this.dropPosition=="After"){
165
_22=_20.getIndexInParent()+1;
166
}
167
}else{
168
_21=(_20&&_20.item)||_1c.item;
169
}
170
var _23;
171
dojo.forEach(_1a,function(_24,idx){
172
var _25=_19.getItem(_24.id);
173
if(dojo.indexOf(_25.type,"treeNode")!=-1){
174
var _26=_25.data,_27=_26.item,_28=_26.getParent().item;
175
}
176
if(_19==this){
177
if(typeof _22=="number"){
178
if(_21==_28&&_26.getIndexInParent()<_22){
179
_22-=1;
180
}
181
}
182
_1d.pasteItem(_27,_28,_21,_1b,_22);
183
}else{
184
if(_1d.isItem(_27)){
185
_1d.pasteItem(_27,_28,_21,_1b,_22);
186
}else{
187
if(!_23){
188
_23=this.itemCreator(_1a,_1e,_19);
189
}
190
_1d.newItem(_23[idx],_21,_22);
191
}
192
}
193
},this);
194
this.tree._expandNode(_20);
195
}
196
this.onDndCancel();
197
},onDndCancel:function(){
198
this._unmarkTargetAnchor();
199
this.isDragging=false;
200
this.mouseDown=false;
201
delete this.mouseButton;
202
this._changeState("Source","");
203
this._changeState("Target","");
204
},onOverEvent:function(){
205
this.inherited(arguments);
206
dojo.dnd.manager().overSource(this);
207
},onOutEvent:function(){
208
this._unmarkTargetAnchor();
209
var m=dojo.dnd.manager();
210
if(this.isDragging){
211
m.canDrop(false);
212
}
213
m.outSource(this);
214
this.inherited(arguments);
215
},_isParentChildDrop:function(_29,_2a){
216
if(!_29.tree||_29.tree!=this.tree){
217
return false;
218
}
219
var _2b=_29.tree.domNode;
220
var ids={};
221
for(var x in _29.selection){
222
ids[_29.selection[x].parentNode.id]=true;
223
}
224
var _2c=_2a.parentNode;
225
while(_2c!=_2b&&(!_2c.id||!ids[_2c.id])){
226
_2c=_2c.parentNode;
227
}
228
return _2c.id&&ids[_2c.id];
229
},_unmarkTargetAnchor:function(){
230
if(!this.targetAnchor){
231
return;
232
}
233
this._removeItemClass(this.targetAnchor,this.dropPosition);
234
this.targetAnchor=null;
235
this.targetBox=null;
236
this.dropPosition=null;
237
},_markDndStatus:function(_2d){
238
this._changeState("Source",_2d?"Copied":"Moved");
239
}});
240
}