1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
1422 |
alexandre_ |
11 |
|
|
|
12 |
|
1318 |
alexandre_ |
13 |
dojo.provide("dojo.dnd.TreeDragAndDropV3");
|
|
|
14 |
dojo.require("dojo.dnd.HtmlDragAndDrop");
|
|
|
15 |
dojo.require("dojo.lang.func");
|
|
|
16 |
dojo.require("dojo.lang.array");
|
|
|
17 |
dojo.require("dojo.lang.extras");
|
|
|
18 |
dojo.require("dojo.Deferred");
|
|
|
19 |
dojo.require("dojo.html.layout");
|
|
|
20 |
dojo.dnd.TreeDragSourceV3 = function (node, syncController, type, treeNode) {
|
|
|
21 |
this.controller = syncController;
|
|
|
22 |
this.treeNode = treeNode;
|
|
|
23 |
dojo.dnd.HtmlDragSource.call(this, node, type);
|
|
|
24 |
};
|
|
|
25 |
dojo.inherits(dojo.dnd.TreeDragSourceV3, dojo.dnd.HtmlDragSource);
|
|
|
26 |
dojo.dnd.TreeDropTargetV3 = function (domNode, controller, type, treeNode) {
|
|
|
27 |
this.treeNode = treeNode;
|
|
|
28 |
this.controller = controller;
|
|
|
29 |
dojo.dnd.HtmlDropTarget.call(this, domNode, type);
|
|
|
30 |
};
|
|
|
31 |
dojo.inherits(dojo.dnd.TreeDropTargetV3, dojo.dnd.HtmlDropTarget);
|
|
|
32 |
dojo.lang.extend(dojo.dnd.TreeDropTargetV3, {autoExpandDelay:1500, autoExpandTimer:null, position:null, indicatorStyle:"2px black groove", showIndicator:function (position) {
|
|
|
33 |
if (this.position == position) {
|
|
|
34 |
return;
|
|
|
35 |
}
|
|
|
36 |
this.hideIndicator();
|
|
|
37 |
this.position = position;
|
|
|
38 |
var node = this.treeNode;
|
|
|
39 |
node.contentNode.style.width = dojo.html.getBorderBox(node.labelNode).width + "px";
|
|
|
40 |
if (position == "onto") {
|
|
|
41 |
node.contentNode.style.border = this.indicatorStyle;
|
|
|
42 |
} else {
|
|
|
43 |
if (position == "before") {
|
|
|
44 |
node.contentNode.style.borderTop = this.indicatorStyle;
|
|
|
45 |
} else {
|
|
|
46 |
if (position == "after") {
|
|
|
47 |
node.contentNode.style.borderBottom = this.indicatorStyle;
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
}, hideIndicator:function () {
|
|
|
52 |
this.treeNode.contentNode.style.borderBottom = "";
|
|
|
53 |
this.treeNode.contentNode.style.borderTop = "";
|
|
|
54 |
this.treeNode.contentNode.style.border = "";
|
|
|
55 |
this.treeNode.contentNode.style.width = "";
|
|
|
56 |
this.position = null;
|
|
|
57 |
}, onDragOver:function (e) {
|
|
|
58 |
var accepts = dojo.dnd.HtmlDropTarget.prototype.onDragOver.apply(this, arguments);
|
|
|
59 |
if (accepts && this.treeNode.isFolder && !this.treeNode.isExpanded) {
|
|
|
60 |
this.setAutoExpandTimer();
|
|
|
61 |
}
|
|
|
62 |
if (accepts) {
|
|
|
63 |
this.cacheNodeCoords();
|
|
|
64 |
}
|
|
|
65 |
return accepts;
|
|
|
66 |
}, accepts:function (dragObjects) {
|
|
|
67 |
var accepts = dojo.dnd.HtmlDropTarget.prototype.accepts.apply(this, arguments);
|
|
|
68 |
if (!accepts) {
|
|
|
69 |
return false;
|
|
|
70 |
}
|
|
|
71 |
for (var i = 0; i < dragObjects.length; i++) {
|
|
|
72 |
var sourceTreeNode = dragObjects[i].treeNode;
|
|
|
73 |
if (sourceTreeNode === this.treeNode) {
|
|
|
74 |
return false;
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
return true;
|
|
|
78 |
}, setAutoExpandTimer:function () {
|
|
|
79 |
var _this = this;
|
|
|
80 |
var autoExpand = function () {
|
|
|
81 |
if (dojo.dnd.dragManager.currentDropTarget === _this) {
|
|
|
82 |
_this.controller.expand(_this.treeNode);
|
|
|
83 |
dojo.dnd.dragManager.cacheTargetLocations();
|
|
|
84 |
}
|
|
|
85 |
};
|
|
|
86 |
this.autoExpandTimer = dojo.lang.setTimeout(autoExpand, _this.autoExpandDelay);
|
|
|
87 |
}, getAcceptPosition:function (e, dragObjects) {
|
|
|
88 |
var DndMode = this.treeNode.tree.DndMode;
|
|
|
89 |
if (DndMode & dojo.widget.TreeV3.prototype.DndModes.ONTO && this.treeNode.actionIsDisabledNow(this.treeNode.actions.ADDCHILD)) {
|
|
|
90 |
DndMode &= ~dojo.widget.TreeV3.prototype.DndModes.ONTO;
|
|
|
91 |
}
|
|
|
92 |
var position = this.getPosition(e, DndMode);
|
|
|
93 |
if (position == "onto") {
|
|
|
94 |
return position;
|
|
|
95 |
}
|
|
|
96 |
for (var i = 0; i < dragObjects.length; i++) {
|
|
|
97 |
var source = dragObjects[i].dragSource;
|
|
|
98 |
if (source.treeNode && this.isAdjacentNode(source.treeNode, position)) {
|
|
|
99 |
continue;
|
|
|
100 |
}
|
|
|
101 |
if (!this.controller.canMove(source.treeNode ? source.treeNode : source, this.treeNode.parent)) {
|
|
|
102 |
return false;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
return position;
|
|
|
106 |
}, onDropEnd:function (e) {
|
|
|
107 |
this.clearAutoExpandTimer();
|
|
|
108 |
this.hideIndicator();
|
|
|
109 |
}, onDragOut:function (e) {
|
|
|
110 |
this.clearAutoExpandTimer();
|
|
|
111 |
this.hideIndicator();
|
|
|
112 |
}, clearAutoExpandTimer:function () {
|
|
|
113 |
if (this.autoExpandTimer) {
|
|
|
114 |
clearTimeout(this.autoExpandTimer);
|
|
|
115 |
this.autoExpandTimer = null;
|
|
|
116 |
}
|
|
|
117 |
}, onDragMove:function (e, dragObjects) {
|
|
|
118 |
var position = this.getAcceptPosition(e, dragObjects);
|
|
|
119 |
if (position) {
|
|
|
120 |
this.showIndicator(position);
|
|
|
121 |
}
|
|
|
122 |
}, isAdjacentNode:function (sourceNode, position) {
|
|
|
123 |
if (sourceNode === this.treeNode) {
|
|
|
124 |
return true;
|
|
|
125 |
}
|
|
|
126 |
if (sourceNode.getNextSibling() === this.treeNode && position == "before") {
|
|
|
127 |
return true;
|
|
|
128 |
}
|
|
|
129 |
if (sourceNode.getPreviousSibling() === this.treeNode && position == "after") {
|
|
|
130 |
return true;
|
|
|
131 |
}
|
|
|
132 |
return false;
|
|
|
133 |
}, cacheNodeCoords:function () {
|
|
|
134 |
var node = this.treeNode.contentNode;
|
|
|
135 |
this.cachedNodeY = dojo.html.getAbsolutePosition(node).y;
|
|
|
136 |
this.cachedNodeHeight = dojo.html.getBorderBox(node).height;
|
|
|
137 |
}, getPosition:function (e, DndMode) {
|
|
|
138 |
var mousey = e.pageY || e.clientY + dojo.body().scrollTop;
|
|
|
139 |
var relY = mousey - this.cachedNodeY;
|
|
|
140 |
var p = relY / this.cachedNodeHeight;
|
|
|
141 |
var position = "";
|
|
|
142 |
if (DndMode & dojo.widget.TreeV3.prototype.DndModes.ONTO && DndMode & dojo.widget.TreeV3.prototype.DndModes.BETWEEN) {
|
|
|
143 |
if (p <= 0.33) {
|
|
|
144 |
position = "before";
|
|
|
145 |
} else {
|
|
|
146 |
if (p <= 0.66 || this.treeNode.isExpanded && this.treeNode.children.length && !this.treeNode.isLastChild()) {
|
|
|
147 |
position = "onto";
|
|
|
148 |
} else {
|
|
|
149 |
position = "after";
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
} else {
|
|
|
153 |
if (DndMode & dojo.widget.TreeV3.prototype.DndModes.BETWEEN) {
|
|
|
154 |
if (p <= 0.5 || this.treeNode.isExpanded && this.treeNode.children.length && !this.treeNode.isLastChild()) {
|
|
|
155 |
position = "before";
|
|
|
156 |
} else {
|
|
|
157 |
position = "after";
|
|
|
158 |
}
|
|
|
159 |
} else {
|
|
|
160 |
if (DndMode & dojo.widget.TreeV3.prototype.DndModes.ONTO) {
|
|
|
161 |
position = "onto";
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
return position;
|
|
|
166 |
}, getTargetParentIndex:function (source, position) {
|
|
|
167 |
var index = position == "before" ? this.treeNode.getParentIndex() : this.treeNode.getParentIndex() + 1;
|
|
|
168 |
if (source.treeNode && this.treeNode.parent === source.treeNode.parent && this.treeNode.getParentIndex() > source.treeNode.getParentIndex()) {
|
|
|
169 |
index--;
|
|
|
170 |
}
|
|
|
171 |
return index;
|
|
|
172 |
}, onDrop:function (e) {
|
|
|
173 |
var position = this.position;
|
|
|
174 |
var source = e.dragObject.dragSource;
|
|
|
175 |
var targetParent, targetIndex;
|
|
|
176 |
if (position == "onto") {
|
|
|
177 |
targetParent = this.treeNode;
|
|
|
178 |
targetIndex = 0;
|
|
|
179 |
} else {
|
|
|
180 |
targetIndex = this.getTargetParentIndex(source, position);
|
|
|
181 |
targetParent = this.treeNode.parent;
|
|
|
182 |
}
|
|
|
183 |
var r = this.getDropHandler(e, source, targetParent, targetIndex)();
|
|
|
184 |
return r;
|
|
|
185 |
}, getDropHandler:function (e, source, targetParent, targetIndex) {
|
|
|
186 |
var handler;
|
|
|
187 |
var _this = this;
|
|
|
188 |
handler = function () {
|
|
|
189 |
var result;
|
|
|
190 |
if (source.treeNode) {
|
|
|
191 |
result = _this.controller.move(source.treeNode, targetParent, targetIndex, true);
|
|
|
192 |
} else {
|
|
|
193 |
if (dojo.lang.isFunction(source.onDrop)) {
|
|
|
194 |
source.onDrop(targetParent, targetIndex);
|
|
|
195 |
}
|
|
|
196 |
var treeNode = source.getTreeNode();
|
|
|
197 |
if (treeNode) {
|
|
|
198 |
result = _this.controller.createChild(targetParent, targetIndex, treeNode, true);
|
|
|
199 |
} else {
|
|
|
200 |
result = true;
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
if (result instanceof dojo.Deferred) {
|
|
|
204 |
var isSuccess = result.fired == 0;
|
|
|
205 |
if (!isSuccess) {
|
|
|
206 |
_this.handleDropError(source, targetParent, targetIndex, result);
|
|
|
207 |
}
|
|
|
208 |
return isSuccess;
|
|
|
209 |
} else {
|
|
|
210 |
return result;
|
|
|
211 |
}
|
|
|
212 |
};
|
|
|
213 |
return handler;
|
|
|
214 |
}, handleDropError:function (source, parent, index, result) {
|
|
|
215 |
dojo.debug("TreeDropTargetV3.handleDropError: DND error occured");
|
|
|
216 |
dojo.debugShallow(result);
|
|
|
217 |
}});
|
|
|
218 |
|