Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / mdnd / AreaManager.js @ 12

History | View | Annotate | Download (10 KB)

1
/*
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["dojox.mdnd.AreaManager"]){
9
dojo._hasResource["dojox.mdnd.AreaManager"]=true;
10
dojo.provide("dojox.mdnd.AreaManager");
11
dojo.require("dojox.mdnd.Moveable");
12
dojo.declare("dojox.mdnd.AreaManager",null,{autoRefresh:true,areaClass:"dojoxDndArea",dragHandleClass:"dojoxDragHandle",constructor:function(){
13
this._areaList=[];
14
this.resizeHandler=dojo.connect(dojo.global,"onresize",this,function(){
15
this._dropMode.updateAreas(this._areaList);
16
});
17
this._oldIndexArea=this._currentIndexArea=this._oldDropIndex=this._currentDropIndex=this._sourceIndexArea=this._sourceDropIndex=-1;
18
},init:function(){
19
this.registerByClass();
20
},registerByNode:function(_1,_2){
21
var _3=this._getIndexArea(_1);
22
if(_1&&_3==-1){
23
var _4=_1.getAttribute("accept");
24
var _5=(_4)?_4.split(/\s*,\s*/):["text"];
25
var _6={"node":_1,"items":[],"coords":{},"margin":null,"accept":_5,"initItems":false};
26
dojo.forEach(this._getChildren(_1),function(_7){
27
this._setMarginArea(_6,_7);
28
_6.items.push(this._addMoveableItem(_7));
29
},this);
30
this._areaList=this._dropMode.addArea(this._areaList,_6);
31
if(!_2){
32
this._dropMode.updateAreas(this._areaList);
33
}
34
dojo.publish("/dojox/mdnd/manager/register",[_1]);
35
}
36
},registerByClass:function(){
37
dojo.query("."+this.areaClass).forEach(function(_8){
38
this.registerByNode(_8,true);
39
},this);
40
this._dropMode.updateAreas(this._areaList);
41
},unregister:function(_9){
42
var _a=this._getIndexArea(_9);
43
if(_a!=-1){
44
dojo.forEach(this._areaList[_a].items,function(_b){
45
this._deleteMoveableItem(_b);
46
},this);
47
this._areaList.splice(_a,1);
48
this._dropMode.updateAreas(this._areaList);
49
return true;
50
}
51
return false;
52
},_addMoveableItem:function(_c){
53
_c.setAttribute("tabIndex","0");
54
var _d=this._searchDragHandle(_c);
55
var _e=new dojox.mdnd.Moveable({"handle":_d,"skip":true},_c);
56
dojo.addClass(_d||_c,"dragHandle");
57
var _f=_c.getAttribute("dndType");
58
var _10={"item":_e,"type":_f?_f.split(/\s*,\s*/):["text"],"handlers":[dojo.connect(_e,"onDragStart",this,"onDragStart")]};
59
if(dijit&&dijit.byNode){
60
var _11=dijit.byNode(_c);
61
if(_11){
62
_10.type=_11.dndType?_11.dndType.split(/\s*,\s*/):["text"];
63
_10.handlers.push(dojo.connect(_11,"uninitialize",this,function(){
64
this.removeDragItem(_c.parentNode,_e.node);
65
}));
66
}
67
}
68
return _10;
69
},_deleteMoveableItem:function(_12){
70
dojo.forEach(_12.handlers,function(_13){
71
dojo.disconnect(_13);
72
});
73
var _14=_12.item.node,_15=this._searchDragHandle(_14);
74
dojo.removeClass(_15||_14,"dragHandle");
75
_12.item.destroy();
76
},_getIndexArea:function(_16){
77
if(_16){
78
for(var i=0;i<this._areaList.length;i++){
79
if(this._areaList[i].node===_16){
80
return i;
81
}
82
}
83
}
84
return -1;
85
},_searchDragHandle:function(_17){
86
if(_17){
87
var _18=this.dragHandleClass.split(" "),_19=_18.length,_1a="";
88
dojo.forEach(_18,function(css,i){
89
_1a+="."+css;
90
if(i!=_19-1){
91
_1a+=", ";
92
}
93
});
94
return dojo.query(_1a,_17)[0];
95
}
96
},addDragItem:function(_1b,_1c,_1d,_1e){
97
var add=true;
98
if(!_1e){
99
add=_1b&&_1c&&(_1c.parentNode===null||(_1c.parentNode&&_1c.parentNode.nodeType!==1));
100
}
101
if(add){
102
var _1f=this._getIndexArea(_1b);
103
if(_1f!==-1){
104
var _20=this._addMoveableItem(_1c),_21=this._areaList[_1f].items;
105
if(0<=_1d&&_1d<_21.length){
106
var _22=_21.slice(0,_1d),_23=_21.slice(_1d,_21.length);
107
_22[_22.length]=_20;
108
this._areaList[_1f].items=_22.concat(_23);
109
_1b.insertBefore(_1c,_21[_1d].item.node);
110
}else{
111
this._areaList[_1f].items.push(_20);
112
_1b.appendChild(_1c);
113
}
114
this._setMarginArea(this._areaList[_1f],_1c);
115
this._areaList[_1f].initItems=false;
116
return true;
117
}
118
}
119
return false;
120
},removeDragItem:function(_24,_25){
121
var _26=this._getIndexArea(_24);
122
if(_24&&_26!==-1){
123
var _27=this._areaList[_26].items;
124
for(var j=0;j<_27.length;j++){
125
if(_27[j].item.node===_25){
126
this._deleteMoveableItem(_27[j]);
127
_27.splice(j,1);
128
return _24.removeChild(_25);
129
}
130
}
131
}
132
return null;
133
},_getChildren:function(_28){
134
var _29=[];
135
dojo.forEach(_28.childNodes,function(_2a){
136
if(_2a.nodeType==1){
137
if(dijit&&dijit.byNode){
138
var _2b=dijit.byNode(_2a);
139
if(_2b){
140
if(!_2b.dragRestriction){
141
_29.push(_2a);
142
}
143
}else{
144
_29.push(_2a);
145
}
146
}else{
147
_29.push(_2a);
148
}
149
}
150
});
151
return _29;
152
},_setMarginArea:function(_2c,_2d){
153
if(_2c&&_2c.margin===null&&_2d){
154
_2c.margin=dojo._getMarginExtents(_2d);
155
}
156
},findCurrentIndexArea:function(_2e,_2f){
157
this._oldIndexArea=this._currentIndexArea;
158
this._currentIndexArea=this._dropMode.getTargetArea(this._areaList,_2e,this._currentIndexArea);
159
if(this._currentIndexArea!=this._oldIndexArea){
160
if(this._oldIndexArea!=-1){
161
this.onDragExit(_2e,_2f);
162
}
163
if(this._currentIndexArea!=-1){
164
this.onDragEnter(_2e,_2f);
165
}
166
}
167
return this._currentIndexArea;
168
},_isAccepted:function(_30,_31){
169
this._accept=false;
170
for(var i=0;i<_31.length;++i){
171
for(var j=0;j<_30.length;++j){
172
if(_30[j]==_31[i]){
173
this._accept=true;
174
break;
175
}
176
}
177
}
178
},onDragStart:function(_32,_33,_34){
179
if(this.autoRefresh){
180
this._dropMode.updateAreas(this._areaList);
181
}
182
var _35=(dojo.isWebKit)?dojo.body():dojo.body().parentNode;
183
if(!this._cover){
184
this._cover=dojo.create("div",{"class":"dndCover"});
185
this._cover2=dojo.clone(this._cover);
186
dojo.addClass(this._cover2,"dndCover2");
187
}
188
var h=_35.scrollHeight+"px";
189
this._cover.style.height=this._cover2.style.height=h;
190
dojo.body().appendChild(this._cover);
191
dojo.body().appendChild(this._cover2);
192
this._dragStartHandler=dojo.connect(_32.ownerDocument,"ondragstart",dojo,"stopEvent");
193
this._sourceIndexArea=this._lastValidIndexArea=this._currentIndexArea=this._getIndexArea(_32.parentNode);
194
var _36=this._areaList[this._sourceIndexArea];
195
var _37=_36.items;
196
for(var i=0;i<_37.length;i++){
197
if(_37[i].item.node==_32){
198
this._dragItem=_37[i];
199
this._dragItem.handlers.push(dojo.connect(this._dragItem.item,"onDrag",this,"onDrag"));
200
this._dragItem.handlers.push(dojo.connect(this._dragItem.item,"onDragEnd",this,"onDrop"));
201
_37.splice(i,1);
202
this._currentDropIndex=this._sourceDropIndex=i;
203
break;
204
}
205
}
206
var _38=null;
207
if(this._sourceDropIndex!==_36.items.length){
208
_38=_36.items[this._sourceDropIndex].item.node;
209
}
210
if(dojo.isIE>7){
211
this._eventsIE7=[dojo.connect(this._cover,"onmouseover",dojo,"stopEvent"),dojo.connect(this._cover,"onmouseout",dojo,"stopEvent"),dojo.connect(this._cover,"onmouseenter",dojo,"stopEvent"),dojo.connect(this._cover,"onmouseleave",dojo,"stopEvent")];
212
}
213
var s=_32.style;
214
s.left=_33.x+"px";
215
s.top=_33.y+"px";
216
if(s.position=="relative"||s.position==""){
217
s.position="absolute";
218
}
219
this._cover.appendChild(_32);
220
this._dropIndicator.place(_36.node,_38,_34);
221
dojo.addClass(_32,"dragNode");
222
this._accept=true;
223
dojo.publish("/dojox/mdnd/drag/start",[_32,_36,this._sourceDropIndex]);
224
},onDragEnter:function(_39,_3a){
225
if(this._currentIndexArea===this._sourceIndexArea){
226
this._accept=true;
227
}else{
228
this._isAccepted(this._dragItem.type,this._areaList[this._currentIndexArea].accept);
229
}
230
},onDragExit:function(_3b,_3c){
231
this._accept=false;
232
},onDrag:function(_3d,_3e,_3f,_40){
233
var _41=this._dropMode.getDragPoint(_3e,_3f,_40);
234
this.findCurrentIndexArea(_41,_3f);
235
if(this._currentIndexArea!==-1&&this._accept){
236
this.placeDropIndicator(_41,_3f);
237
}
238
},placeDropIndicator:function(_42,_43){
239
this._oldDropIndex=this._currentDropIndex;
240
var _44=this._areaList[this._currentIndexArea];
241
if(!_44.initItems){
242
this._dropMode.initItems(_44);
243
}
244
this._currentDropIndex=this._dropMode.getDropIndex(_44,_42);
245
if(!(this._currentIndexArea===this._oldIndexArea&&this._oldDropIndex===this._currentDropIndex)){
246
this._placeDropIndicator(_43);
247
}
248
return this._currentDropIndex;
249
},_placeDropIndicator:function(_45){
250
var _46=this._areaList[this._lastValidIndexArea];
251
var _47=this._areaList[this._currentIndexArea];
252
this._dropMode.refreshItems(_46,this._oldDropIndex,_45,false);
253
var _48=null;
254
if(this._currentDropIndex!=-1){
255
_48=_47.items[this._currentDropIndex].item.node;
256
}
257
this._dropIndicator.place(_47.node,_48);
258
this._lastValidIndexArea=this._currentIndexArea;
259
this._dropMode.refreshItems(_47,this._currentDropIndex,_45,true);
260
},onDropCancel:function(){
261
if(!this._accept){
262
var _49=this._getIndexArea(this._dropIndicator.node.parentNode);
263
if(_49!=-1){
264
this._currentIndexArea=_49;
265
}else{
266
this._currentIndexArea=0;
267
}
268
}
269
},onDrop:function(_4a){
270
this.onDropCancel();
271
var _4b=this._areaList[this._currentIndexArea];
272
dojo.removeClass(_4a,"dragNode");
273
var _4c=_4a.style;
274
_4c.position="relative";
275
_4c.left="0";
276
_4c.top="0";
277
_4c.width="auto";
278
if(_4b.node==this._dropIndicator.node.parentNode){
279
_4b.node.insertBefore(_4a,this._dropIndicator.node);
280
}else{
281
_4b.node.appendChild(_4a);
282
this._currentDropIndex=_4b.items.length;
283
}
284
var _4d=this._currentDropIndex;
285
if(_4d==-1){
286
_4d=_4b.items.length;
287
}
288
var _4e=_4b.items;
289
var _4f=_4e.slice(0,_4d);
290
var _50=_4e.slice(_4d,_4e.length);
291
_4f[_4f.length]=this._dragItem;
292
_4b.items=_4f.concat(_50);
293
this._setMarginArea(_4b,_4a);
294
dojo.forEach(this._areaList,function(obj){
295
obj.initItems=false;
296
});
297
dojo.disconnect(this._dragItem.handlers.pop());
298
dojo.disconnect(this._dragItem.handlers.pop());
299
this._resetAfterDrop();
300
if(this._cover){
301
dojo.body().removeChild(this._cover);
302
dojo.body().removeChild(this._cover2);
303
}
304
dojo.publish("/dojox/mdnd/drop",[_4a,_4b,_4d]);
305
},_resetAfterDrop:function(){
306
this._accept=false;
307
this._dragItem=null;
308
this._currentDropIndex=-1;
309
this._currentIndexArea=-1;
310
this._oldDropIndex=-1;
311
this._sourceIndexArea=-1;
312
this._sourceDropIndex=-1;
313
this._dropIndicator.remove();
314
if(this._dragStartHandler){
315
dojo.disconnect(this._dragStartHandler);
316
}
317
if(dojo.isIE>7){
318
dojo.forEach(this._eventsIE7,dojo.disconnect);
319
}
320
},destroy:function(){
321
while(this._areaList.length>0){
322
if(!this.unregister(this._areaList[0].node)){
323
throw new Error("Error while destroying AreaManager");
324
}
325
}
326
dojo.disconnect(this.resizeHandler);
327
this._dropIndicator.destroy();
328
this._dropMode.destroy();
329
if(dojox.mdnd.autoScroll){
330
dojox.mdnd.autoScroll.destroy();
331
}
332
if(this.refreshListener){
333
dojo.unsubscribe(this.refreshListener);
334
}
335
if(this._cover){
336
dojo._destroyElement(this._cover);
337
dojo._destroyElement(this._cover2);
338
delete this._cover;
339
delete this._cover2;
340
}
341
}});
342
if(dijit&&dijit._Widget){
343
dojo.extend(dijit._Widget,{dndType:"text"});
344
}
345
dojox.mdnd._areaManager=null;
346
dojox.mdnd.areaManager=function(){
347
if(!dojox.mdnd._areaManager){
348
dojox.mdnd._areaManager=new dojox.mdnd.AreaManager();
349
}
350
return dojox.mdnd._areaManager;
351
};
352
}