1 | ToolMan._coordinatesFactory.inside = function(item,container) { |
---|
2 | var iTL = this.topLeftOffset(item); |
---|
3 | var iBR = this.bottomRightOffset(item); |
---|
4 | var cTL = this.topLeftOffset(container); |
---|
5 | var cBR = this.bottomRightOffset(container); |
---|
6 | |
---|
7 | return (iTL.x >= cTL.x && iTL.x <= cBR.x && iTL.y >= cTL.y && iTL.y <= cBR.y) |
---|
8 | || (iBR.x >= cTL.x && iBR.x <= cBR.x && iBR.y >= cTL.y && iBR.y <= cBR.y); |
---|
9 | } |
---|
10 | |
---|
11 | ToolMan._dragdropFactory = { |
---|
12 | firstContainer: null, |
---|
13 | lastContainer: null, |
---|
14 | |
---|
15 | makeDragable: function(item) { |
---|
16 | var group = ToolMan.drag().createSimpleGroup(item); |
---|
17 | group.register('dragstart',this._onDragStart); |
---|
18 | group.register('dragmove', this._onDragMove); |
---|
19 | group.register('dragend', this._onDragEnd); |
---|
20 | |
---|
21 | item.isOutside = false; |
---|
22 | item.started = false; |
---|
23 | |
---|
24 | return group; |
---|
25 | }, |
---|
26 | |
---|
27 | makeListContainer: function(list,name) { |
---|
28 | // each container becomes a linked list node |
---|
29 | if (this.firstContainer == null) { |
---|
30 | this.firstContainer = this.lastContainer = list; |
---|
31 | list.previousContainer = null; |
---|
32 | list.nextContainer = null; |
---|
33 | } else { |
---|
34 | list.previousContainer = this.lastContainer; |
---|
35 | list.nextContainer = null; |
---|
36 | this.lastContainer.nextContainer = list; |
---|
37 | this.lastContainer = list; |
---|
38 | } |
---|
39 | |
---|
40 | var helpers = ToolMan.helpers(); |
---|
41 | var coordinates = ToolMan.coordinates(); |
---|
42 | |
---|
43 | //var children = list.childNodes; |
---|
44 | var items = new Array(); |
---|
45 | for (var i=0; i<list.childNodes.length; i++) { |
---|
46 | if (list.childNodes[i].nodeType == 1 && |
---|
47 | list.childNodes[i].nodeName.toLowerCase() == name.toLowerCase()) { |
---|
48 | items.push(list.childNodes[i]); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | // these functions are called when an item is draged over |
---|
53 | // a container or out of a container bounds. onDragOut |
---|
54 | // is also called when the drag ends with an item having |
---|
55 | // been added to the container |
---|
56 | list.onDragOver = new Function(); |
---|
57 | list.onDragOut = new Function(); |
---|
58 | list.onDragEnd = new Function(); |
---|
59 | |
---|
60 | if (list.factory == undefined) { |
---|
61 | list.factory = false; |
---|
62 | } |
---|
63 | |
---|
64 | var This = this; |
---|
65 | helpers.map(items, function(item) { |
---|
66 | var dragGroup = This.makeDragable(item); |
---|
67 | dragGroup.setThreshold(4); |
---|
68 | }); |
---|
69 | for (var i = 2, n = arguments.length; i < n; i++) { |
---|
70 | helpers.map(items, arguments[i]); |
---|
71 | } |
---|
72 | }, |
---|
73 | |
---|
74 | _onDragStart: function(dragEvent) { |
---|
75 | var container = ToolMan._dragdropFactory.firstContainer; |
---|
76 | var item = dragEvent.group.element; |
---|
77 | var coordinates = ToolMan.coordinates(); |
---|
78 | |
---|
79 | // Factory is a sort of assembly pieces, we want to keep them |
---|
80 | if (item.parentNode.factory) { |
---|
81 | var origin = item.cloneNode(true); |
---|
82 | item.parentNode.insertBefore(origin,item.nextSibling); |
---|
83 | ToolMan._dragdropFactory.makeDragable(origin); |
---|
84 | } |
---|
85 | |
---|
86 | while (container != null) { |
---|
87 | container.topLeft = coordinates.topLeftOffset(container); |
---|
88 | container.bottomRight = coordinates.bottomRightOffset(container); |
---|
89 | container = container.nextContainer; |
---|
90 | } |
---|
91 | |
---|
92 | // item starts out over current parent |
---|
93 | item.started = true; |
---|
94 | item.parentNode.onDragOver(); |
---|
95 | }, |
---|
96 | |
---|
97 | _onDragMove: function(dragEvent) { |
---|
98 | var helpers = ToolMan.helpers(); |
---|
99 | var coordinates = ToolMan.coordinates(); |
---|
100 | |
---|
101 | var item = dragEvent.group.element; |
---|
102 | var xmouse = dragEvent.transformedMouseOffset; |
---|
103 | var moveTo = null; |
---|
104 | |
---|
105 | // Check if we are nowhere |
---|
106 | if (item.isOutside) { |
---|
107 | var container = ToolMan._dragdropFactory.firstContainer; |
---|
108 | while (container != null) { |
---|
109 | if (coordinates.inside(item,container) && !container.factory) { |
---|
110 | container.onDragOver(); |
---|
111 | item.isOutside = false; |
---|
112 | |
---|
113 | // since isOutside was true, the current parent is a |
---|
114 | // temporary clone of some previous container node and |
---|
115 | // it needs to be removed from the document |
---|
116 | var tempParent = item.parentNode; |
---|
117 | tempParent.removeChild(item); |
---|
118 | container.appendChild(item); |
---|
119 | //tempParent.parentNode.removeChild(tempParent); |
---|
120 | |
---|
121 | break; |
---|
122 | } |
---|
123 | container = container.nextContainer; |
---|
124 | } |
---|
125 | |
---|
126 | if (this.isOutside) { |
---|
127 | return; |
---|
128 | } |
---|
129 | } |
---|
130 | // Outside the parent node |
---|
131 | else if (!coordinates.inside(item,item.parentNode)) { |
---|
132 | item.parentNode.onDragOut(); |
---|
133 | item.isOutside = true; |
---|
134 | var container = ToolMan._dragdropFactory.firstContainer; |
---|
135 | while (container != null) { |
---|
136 | if (coordinates.inside(item,container) && !container.factory) { |
---|
137 | container.onDragOver(); |
---|
138 | item.isOutside = false; |
---|
139 | container.appendChild(item); |
---|
140 | break; |
---|
141 | } |
---|
142 | container = container.nextContainer; |
---|
143 | } |
---|
144 | // if we're not in any container now, make a temporary clone of |
---|
145 | // the previous container node and add it to the document |
---|
146 | if (this.isOutside) { |
---|
147 | var tempParent = item.parentNode.cloneNode(false); |
---|
148 | item.parentNode.removeChild(item); |
---|
149 | tempParent.appendChild(item); |
---|
150 | document.body.appendChild(tempParent); |
---|
151 | return; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | // if we get here, we're inside some container bounds, so we do |
---|
156 | // everything the original dragsort script did to swap us into the |
---|
157 | // correct position |
---|
158 | |
---|
159 | if (item.parentNode.factory) { |
---|
160 | return; |
---|
161 | } |
---|
162 | |
---|
163 | var moveTo = null |
---|
164 | |
---|
165 | var previous = helpers.previousItem(item, item.nodeName) |
---|
166 | while (previous != null) { |
---|
167 | var bottomRight = coordinates.bottomRightOffset(previous) |
---|
168 | if (xmouse.y <= bottomRight.y && xmouse.x <= bottomRight.x) { |
---|
169 | moveTo = previous |
---|
170 | } |
---|
171 | previous = helpers.previousItem(previous, item.nodeName) |
---|
172 | } |
---|
173 | if (moveTo != null) { |
---|
174 | helpers.moveBefore(item, moveTo) |
---|
175 | return |
---|
176 | } |
---|
177 | |
---|
178 | var next = helpers.nextItem(item, item.nodeName) |
---|
179 | while (next != null) { |
---|
180 | var topLeft = coordinates.topLeftOffset(next) |
---|
181 | if (topLeft.y <= xmouse.y && topLeft.x <= xmouse.x) { |
---|
182 | moveTo = next |
---|
183 | } |
---|
184 | next = helpers.nextItem(next, item.nodeName) |
---|
185 | } |
---|
186 | if (moveTo != null) { |
---|
187 | helpers.moveBefore(item, helpers.nextItem(moveTo, item.nodeName)) |
---|
188 | return |
---|
189 | } |
---|
190 | }, |
---|
191 | |
---|
192 | _onDragEnd: function(dragEvent) { |
---|
193 | var item = dragEvent.group.element; |
---|
194 | |
---|
195 | if (!item.started) { |
---|
196 | return; |
---|
197 | } |
---|
198 | |
---|
199 | if (item.isOutside || item.parentNode.factory) { |
---|
200 | item.parentNode.removeChild(item); |
---|
201 | return; |
---|
202 | } |
---|
203 | |
---|
204 | item.parentNode.onDragEnd.call(item); |
---|
205 | ToolMan.coordinates().create(0, 0).reposition(dragEvent.group.element); |
---|
206 | } |
---|
207 | }; |
---|
208 | |
---|
209 | ToolMan.dragdrop = function() { |
---|
210 | return ToolMan._dragdropFactory; |
---|
211 | }; |
---|