1 | /* |
---|
2 | * jQuery UI Sortable |
---|
3 | * |
---|
4 | * Copyright (c) 2008 Paul Bakaus |
---|
5 | * Dual licensed under the MIT (MIT-LICENSE.txt) |
---|
6 | * and GPL (GPL-LICENSE.txt) licenses. |
---|
7 | * |
---|
8 | * http://docs.jquery.com/UI/Sortables |
---|
9 | * |
---|
10 | * Depends: |
---|
11 | * ui.core.js |
---|
12 | */ |
---|
13 | (function($) { |
---|
14 | |
---|
15 | function contains(a, b) { |
---|
16 | var safari2 = $.browser.safari && $.browser.version < 522; |
---|
17 | if (a.contains && !safari2) { |
---|
18 | return a.contains(b); |
---|
19 | } |
---|
20 | if (a.compareDocumentPosition) |
---|
21 | return !!(a.compareDocumentPosition(b) & 16); |
---|
22 | while (b = b.parentNode) |
---|
23 | if (b == a) return true; |
---|
24 | return false; |
---|
25 | }; |
---|
26 | |
---|
27 | $.widget("ui.sortable", $.extend({}, $.ui.mouse, { |
---|
28 | init: function() { |
---|
29 | |
---|
30 | var o = this.options; |
---|
31 | this.containerCache = {}; |
---|
32 | this.element.addClass("ui-sortable"); |
---|
33 | |
---|
34 | //Get the items |
---|
35 | this.refresh(); |
---|
36 | |
---|
37 | //Let's determine if the items are floating |
---|
38 | this.floating = this.items.length ? (/left|right/).test(this.items[0].item.css('float')) : false; |
---|
39 | |
---|
40 | //Let's determine the parent's offset |
---|
41 | if(!(/(relative|absolute|fixed)/).test(this.element.css('position'))) this.element.css('position', 'relative'); |
---|
42 | this.offset = this.element.offset(); |
---|
43 | |
---|
44 | //Initialize mouse events for interaction |
---|
45 | this.mouseInit(); |
---|
46 | |
---|
47 | }, |
---|
48 | plugins: {}, |
---|
49 | ui: function(inst) { |
---|
50 | return { |
---|
51 | helper: (inst || this)["helper"], |
---|
52 | placeholder: (inst || this)["placeholder"] || $([]), |
---|
53 | position: (inst || this)["position"], |
---|
54 | absolutePosition: (inst || this)["positionAbs"], |
---|
55 | options: this.options, |
---|
56 | element: this.element, |
---|
57 | item: (inst || this)["currentItem"], |
---|
58 | sender: inst ? inst.element : null |
---|
59 | }; |
---|
60 | }, |
---|
61 | propagate: function(n,e,inst, noPropagation) { |
---|
62 | $.ui.plugin.call(this, n, [e, this.ui(inst)]); |
---|
63 | if(!noPropagation) this.element.triggerHandler(n == "sort" ? n : "sort"+n, [e, this.ui(inst)], this.options[n]); |
---|
64 | }, |
---|
65 | serialize: function(o) { |
---|
66 | |
---|
67 | var items = ($.isFunction(this.options.items) ? this.options.items.call(this.element) : $(this.options.items, this.element)).not('.ui-sortable-helper'); //Only the items of the sortable itself |
---|
68 | var str = []; o = o || {}; |
---|
69 | |
---|
70 | items.each(function() { |
---|
71 | var res = ($(this).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/)); |
---|
72 | if(res) str.push((o.key || res[1])+'[]='+(o.key && o.expression ? res[1] : res[2])); |
---|
73 | }); |
---|
74 | |
---|
75 | return str.join('&'); |
---|
76 | |
---|
77 | }, |
---|
78 | toArray: function(attr) { |
---|
79 | |
---|
80 | var items = ($.isFunction(this.options.items) ? this.options.items.call(this.element) : $(this.options.items, this.element)).not('.ui-sortable-helper'); //Only the items of the sortable itself |
---|
81 | var ret = []; |
---|
82 | |
---|
83 | items.each(function() { ret.push($(this).attr(attr || 'id')); }); |
---|
84 | return ret; |
---|
85 | |
---|
86 | }, |
---|
87 | /* Be careful with the following core functions */ |
---|
88 | intersectsWith: function(item) { |
---|
89 | |
---|
90 | var x1 = this.positionAbs.left, x2 = x1 + this.helperProportions.width, |
---|
91 | y1 = this.positionAbs.top, y2 = y1 + this.helperProportions.height; |
---|
92 | var l = item.left, r = l + item.width, |
---|
93 | t = item.top, b = t + item.height; |
---|
94 | |
---|
95 | if(this.options.tolerance == "pointer" || this.options.forcePointerForContainers || (this.options.tolerance == "guess" && this.helperProportions[this.floating ? 'width' : 'height'] > item[this.floating ? 'width' : 'height'])) { |
---|
96 | return (y1 + this.offset.click.top > t && y1 + this.offset.click.top < b && x1 + this.offset.click.left > l && x1 + this.offset.click.left < r); |
---|
97 | } else { |
---|
98 | |
---|
99 | return (l < x1 + (this.helperProportions.width / 2) // Right Half |
---|
100 | && x2 - (this.helperProportions.width / 2) < r // Left Half |
---|
101 | && t < y1 + (this.helperProportions.height / 2) // Bottom Half |
---|
102 | && y2 - (this.helperProportions.height / 2) < b ); // Top Half |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | }, |
---|
107 | intersectsWithEdge: function(item) { |
---|
108 | var x1 = this.positionAbs.left, x2 = x1 + this.helperProportions.width, |
---|
109 | y1 = this.positionAbs.top, y2 = y1 + this.helperProportions.height; |
---|
110 | var l = item.left, r = l + item.width, |
---|
111 | t = item.top, b = t + item.height; |
---|
112 | |
---|
113 | if(this.options.tolerance == "pointer" || (this.options.tolerance == "guess" && this.helperProportions[this.floating ? 'width' : 'height'] > item[this.floating ? 'width' : 'height'])) { |
---|
114 | |
---|
115 | if(!(y1 + this.offset.click.top > t && y1 + this.offset.click.top < b && x1 + this.offset.click.left > l && x1 + this.offset.click.left < r)) return false; |
---|
116 | |
---|
117 | if(this.floating) { |
---|
118 | if(x1 + this.offset.click.left > l && x1 + this.offset.click.left < l + item.width/2) return 2; |
---|
119 | if(x1 + this.offset.click.left > l+item.width/2 && x1 + this.offset.click.left < r) return 1; |
---|
120 | } else { |
---|
121 | if(y1 + this.offset.click.top > t && y1 + this.offset.click.top < t + item.height/2) return 2; |
---|
122 | if(y1 + this.offset.click.top > t+item.height/2 && y1 + this.offset.click.top < b) return 1; |
---|
123 | } |
---|
124 | |
---|
125 | } else { |
---|
126 | |
---|
127 | if (!(l < x1 + (this.helperProportions.width / 2) // Right Half |
---|
128 | && x2 - (this.helperProportions.width / 2) < r // Left Half |
---|
129 | && t < y1 + (this.helperProportions.height / 2) // Bottom Half |
---|
130 | && y2 - (this.helperProportions.height / 2) < b )) return false; // Top Half |
---|
131 | |
---|
132 | if(this.floating) { |
---|
133 | if(x2 > l && x1 < l) return 2; //Crosses left edge |
---|
134 | if(x1 < r && x2 > r) return 1; //Crosses right edge |
---|
135 | } else { |
---|
136 | if(y2 > t && y1 < t) return 1; //Crosses top edge |
---|
137 | if(y1 < b && y2 > b) return 2; //Crosses bottom edge |
---|
138 | } |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | return false; |
---|
143 | |
---|
144 | }, |
---|
145 | refresh: function() { |
---|
146 | this.refreshItems(); |
---|
147 | this.refreshPositions(); |
---|
148 | }, |
---|
149 | refreshItems: function() { |
---|
150 | |
---|
151 | this.items = []; |
---|
152 | this.containers = [this]; |
---|
153 | var items = this.items; |
---|
154 | var self = this; |
---|
155 | var queries = [[$.isFunction(this.options.items) ? this.options.items.call(this.element, null, { options: this.options, item: this.currentItem }) : $(this.options.items, this.element), this]]; |
---|
156 | |
---|
157 | if(this.options.connectWith) { |
---|
158 | for (var i = this.options.connectWith.length - 1; i >= 0; i--){ |
---|
159 | var cur = $(this.options.connectWith[i]); |
---|
160 | for (var j = cur.length - 1; j >= 0; j--){ |
---|
161 | var inst = $.data(cur[j], 'sortable'); |
---|
162 | if(inst && !inst.options.disabled) { |
---|
163 | queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element) : $(inst.options.items, inst.element), inst]); |
---|
164 | this.containers.push(inst); |
---|
165 | } |
---|
166 | }; |
---|
167 | }; |
---|
168 | } |
---|
169 | |
---|
170 | for (var i = queries.length - 1; i >= 0; i--){ |
---|
171 | queries[i][0].each(function() { |
---|
172 | $.data(this, 'sortable-item', queries[i][1]); // Data for target checking (mouse manager) |
---|
173 | items.push({ |
---|
174 | item: $(this), |
---|
175 | instance: queries[i][1], |
---|
176 | width: 0, height: 0, |
---|
177 | left: 0, top: 0 |
---|
178 | }); |
---|
179 | }); |
---|
180 | }; |
---|
181 | |
---|
182 | }, |
---|
183 | refreshPositions: function(fast) { |
---|
184 | |
---|
185 | //This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change |
---|
186 | if(this.offsetParent) { |
---|
187 | var po = this.offsetParent.offset(); |
---|
188 | this.offset.parent = { top: po.top + this.offsetParentBorders.top, left: po.left + this.offsetParentBorders.left }; |
---|
189 | } |
---|
190 | |
---|
191 | for (var i = this.items.length - 1; i >= 0; i--){ |
---|
192 | |
---|
193 | //We ignore calculating positions of all connected containers when we're not over them |
---|
194 | if(this.items[i].instance != this.currentContainer && this.currentContainer && this.items[i].item[0] != this.currentItem[0]) |
---|
195 | continue; |
---|
196 | |
---|
197 | var t = this.options.toleranceElement ? $(this.options.toleranceElement, this.items[i].item) : this.items[i].item; |
---|
198 | |
---|
199 | if(!fast) { |
---|
200 | this.items[i].width = t[0].offsetWidth; |
---|
201 | this.items[i].height = t[0].offsetHeight; |
---|
202 | } |
---|
203 | |
---|
204 | var p = t.offset(); |
---|
205 | this.items[i].left = p.left; |
---|
206 | this.items[i].top = p.top; |
---|
207 | |
---|
208 | }; |
---|
209 | |
---|
210 | if(this.options.custom && this.options.custom.refreshContainers) { |
---|
211 | this.options.custom.refreshContainers.call(this); |
---|
212 | } else { |
---|
213 | for (var i = this.containers.length - 1; i >= 0; i--){ |
---|
214 | var p =this.containers[i].element.offset(); |
---|
215 | this.containers[i].containerCache.left = p.left; |
---|
216 | this.containers[i].containerCache.top = p.top; |
---|
217 | this.containers[i].containerCache.width = this.containers[i].element.outerWidth(); |
---|
218 | this.containers[i].containerCache.height = this.containers[i].element.outerHeight(); |
---|
219 | }; |
---|
220 | } |
---|
221 | |
---|
222 | }, |
---|
223 | destroy: function() { |
---|
224 | this.element |
---|
225 | .removeClass("ui-sortable ui-sortable-disabled") |
---|
226 | .removeData("sortable") |
---|
227 | .unbind(".sortable"); |
---|
228 | this.mouseDestroy(); |
---|
229 | |
---|
230 | for ( var i = this.items.length - 1; i >= 0; i-- ) |
---|
231 | this.items[i].item.removeData("sortable-item"); |
---|
232 | }, |
---|
233 | createPlaceholder: function(that) { |
---|
234 | |
---|
235 | var self = that || this, o = self.options; |
---|
236 | |
---|
237 | if(o.placeholder.constructor == String) { |
---|
238 | var className = o.placeholder; |
---|
239 | o.placeholder = { |
---|
240 | element: function() { |
---|
241 | return $('<div></div>').addClass(className)[0]; |
---|
242 | }, |
---|
243 | update: function(i, p) { |
---|
244 | p.css(i.offset()).css({ width: i.outerWidth(), height: i.outerHeight() }); |
---|
245 | } |
---|
246 | }; |
---|
247 | } |
---|
248 | |
---|
249 | self.placeholder = $(o.placeholder.element.call(self.element, self.currentItem)).appendTo('body').css({ position: 'absolute' }); |
---|
250 | o.placeholder.update.call(self.element, self.currentItem, self.placeholder); |
---|
251 | }, |
---|
252 | contactContainers: function(e) { |
---|
253 | for (var i = this.containers.length - 1; i >= 0; i--){ |
---|
254 | |
---|
255 | if(this.intersectsWith(this.containers[i].containerCache)) { |
---|
256 | if(!this.containers[i].containerCache.over) { |
---|
257 | |
---|
258 | |
---|
259 | if(this.currentContainer != this.containers[i]) { |
---|
260 | |
---|
261 | //When entering a new container, we will find the item with the least distance and append our item near it |
---|
262 | var dist = 10000; var itemWithLeastDistance = null; var base = this.positionAbs[this.containers[i].floating ? 'left' : 'top']; |
---|
263 | for (var j = this.items.length - 1; j >= 0; j--) { |
---|
264 | if(!contains(this.containers[i].element[0], this.items[j].item[0])) continue; |
---|
265 | var cur = this.items[j][this.containers[i].floating ? 'left' : 'top']; |
---|
266 | if(Math.abs(cur - base) < dist) { |
---|
267 | dist = Math.abs(cur - base); itemWithLeastDistance = this.items[j]; |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | if(!itemWithLeastDistance && !this.options.dropOnEmpty) //Check if dropOnEmpty is enabled |
---|
272 | continue; |
---|
273 | |
---|
274 | //We also need to exchange the placeholder |
---|
275 | if(this.placeholder) this.placeholder.remove(); |
---|
276 | if(this.containers[i].options.placeholder) { |
---|
277 | this.containers[i].createPlaceholder(this); |
---|
278 | } else { |
---|
279 | this.placeholder = null;; |
---|
280 | } |
---|
281 | |
---|
282 | this.currentContainer = this.containers[i]; |
---|
283 | itemWithLeastDistance ? this.rearrange(e, itemWithLeastDistance, null, true) : this.rearrange(e, null, this.containers[i].element, true); |
---|
284 | this.propagate("change", e); //Call plugins and callbacks |
---|
285 | this.containers[i].propagate("change", e, this); //Call plugins and callbacks |
---|
286 | |
---|
287 | } |
---|
288 | |
---|
289 | this.containers[i].propagate("over", e, this); |
---|
290 | this.containers[i].containerCache.over = 1; |
---|
291 | } |
---|
292 | } else { |
---|
293 | if(this.containers[i].containerCache.over) { |
---|
294 | this.containers[i].propagate("out", e, this); |
---|
295 | this.containers[i].containerCache.over = 0; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | }; |
---|
300 | }, |
---|
301 | mouseCapture: function(e, overrideHandle) { |
---|
302 | |
---|
303 | if(this.options.disabled || this.options.type == 'static') return false; |
---|
304 | |
---|
305 | //We have to refresh the items data once first |
---|
306 | this.refreshItems(); |
---|
307 | |
---|
308 | //Find out if the clicked node (or one of its parents) is a actual item in this.items |
---|
309 | var currentItem = null, self = this, nodes = $(e.target).parents().each(function() { |
---|
310 | if($.data(this, 'sortable-item') == self) { |
---|
311 | currentItem = $(this); |
---|
312 | return false; |
---|
313 | } |
---|
314 | }); |
---|
315 | if($.data(e.target, 'sortable-item') == self) currentItem = $(e.target); |
---|
316 | |
---|
317 | if(!currentItem) return false; |
---|
318 | if(this.options.handle && !overrideHandle) { |
---|
319 | var validHandle = false; |
---|
320 | |
---|
321 | $(this.options.handle, currentItem).find("*").andSelf().each(function() { if(this == e.target) validHandle = true; }); |
---|
322 | if(!validHandle) return false; |
---|
323 | } |
---|
324 | |
---|
325 | this.currentItem = currentItem; |
---|
326 | return true; |
---|
327 | |
---|
328 | }, |
---|
329 | mouseStart: function(e, overrideHandle, noActivation) { |
---|
330 | |
---|
331 | var o = this.options; |
---|
332 | this.currentContainer = this; |
---|
333 | |
---|
334 | //We only need to call refreshPositions, because the refreshItems call has been moved to mouseCapture |
---|
335 | this.refreshPositions(); |
---|
336 | |
---|
337 | //Create and append the visible helper |
---|
338 | this.helper = typeof o.helper == 'function' ? $(o.helper.apply(this.element[0], [e, this.currentItem])) : this.currentItem.clone(); |
---|
339 | if (!this.helper.parents('body').length) $(o.appendTo != 'parent' ? o.appendTo : this.currentItem[0].parentNode)[0].appendChild(this.helper[0]); //Add the helper to the DOM if that didn't happen already |
---|
340 | this.helper.css({ position: 'absolute', clear: 'both' }).addClass('ui-sortable-helper'); //Position it absolutely and add a helper class |
---|
341 | |
---|
342 | /* |
---|
343 | * - Position generation - |
---|
344 | * This block generates everything position related - it's the core of draggables. |
---|
345 | */ |
---|
346 | |
---|
347 | this.margins = { //Cache the margins |
---|
348 | left: (parseInt(this.currentItem.css("marginLeft"),10) || 0), |
---|
349 | top: (parseInt(this.currentItem.css("marginTop"),10) || 0) |
---|
350 | }; |
---|
351 | |
---|
352 | this.offset = this.currentItem.offset(); //The element's absolute position on the page |
---|
353 | this.offset = { //Substract the margins from the element's absolute offset |
---|
354 | top: this.offset.top - this.margins.top, |
---|
355 | left: this.offset.left - this.margins.left |
---|
356 | }; |
---|
357 | |
---|
358 | this.offset.click = { //Where the click happened, relative to the element |
---|
359 | left: e.pageX - this.offset.left, |
---|
360 | top: e.pageY - this.offset.top |
---|
361 | }; |
---|
362 | |
---|
363 | this.offsetParent = this.helper.offsetParent(); //Get the offsetParent and cache its position |
---|
364 | var po = this.offsetParent.offset(); |
---|
365 | |
---|
366 | this.offsetParentBorders = { |
---|
367 | top: (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0), |
---|
368 | left: (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0) |
---|
369 | }; |
---|
370 | this.offset.parent = { //Store its position plus border |
---|
371 | top: po.top + this.offsetParentBorders.top, |
---|
372 | left: po.left + this.offsetParentBorders.left |
---|
373 | }; |
---|
374 | |
---|
375 | this.originalPosition = this.generatePosition(e); //Generate the original position |
---|
376 | this.domPosition = { prev: this.currentItem.prev()[0], parent: this.currentItem.parent()[0] }; //Cache the former DOM position |
---|
377 | |
---|
378 | //If o.placeholder is used, create a new element at the given position with the class |
---|
379 | this.helperProportions = { width: this.helper.outerWidth(), height: this.helper.outerHeight() };//Cache the helper size |
---|
380 | if(o.placeholder) this.createPlaceholder(); |
---|
381 | |
---|
382 | //Call plugins and callbacks |
---|
383 | this.propagate("start", e); |
---|
384 | this.helperProportions = { width: this.helper.outerWidth(), height: this.helper.outerHeight() };//Recache the helper size |
---|
385 | |
---|
386 | if(o.cursorAt) { |
---|
387 | if(o.cursorAt.left != undefined) this.offset.click.left = o.cursorAt.left; |
---|
388 | if(o.cursorAt.right != undefined) this.offset.click.left = this.helperProportions.width - o.cursorAt.right; |
---|
389 | if(o.cursorAt.top != undefined) this.offset.click.top = o.cursorAt.top; |
---|
390 | if(o.cursorAt.bottom != undefined) this.offset.click.top = this.helperProportions.height - o.cursorAt.bottom; |
---|
391 | } |
---|
392 | |
---|
393 | /* |
---|
394 | * - Position constraining - |
---|
395 | * Here we prepare position constraining like grid and containment. |
---|
396 | */ |
---|
397 | |
---|
398 | if(o.containment) { |
---|
399 | if(o.containment == 'parent') o.containment = this.helper[0].parentNode; |
---|
400 | if(o.containment == 'document' || o.containment == 'window') this.containment = [ |
---|
401 | 0 - this.offset.parent.left, |
---|
402 | 0 - this.offset.parent.top, |
---|
403 | $(o.containment == 'document' ? document : window).width() - this.offset.parent.left - this.helperProportions.width - this.margins.left - (parseInt(this.element.css("marginRight"),10) || 0), |
---|
404 | ($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.offset.parent.top - this.helperProportions.height - this.margins.top - (parseInt(this.element.css("marginBottom"),10) || 0) |
---|
405 | ]; |
---|
406 | |
---|
407 | if(!(/^(document|window|parent)$/).test(o.containment)) { |
---|
408 | var ce = $(o.containment)[0]; |
---|
409 | var co = $(o.containment).offset(); |
---|
410 | |
---|
411 | this.containment = [ |
---|
412 | co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) - this.offset.parent.left, |
---|
413 | co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) - this.offset.parent.top, |
---|
414 | co.left+Math.max(ce.scrollWidth,ce.offsetWidth) - (parseInt($(ce).css("borderLeftWidth"),10) || 0) - this.offset.parent.left - this.helperProportions.width - this.margins.left - (parseInt(this.currentItem.css("marginRight"),10) || 0), |
---|
415 | co.top+Math.max(ce.scrollHeight,ce.offsetHeight) - (parseInt($(ce).css("borderTopWidth"),10) || 0) - this.offset.parent.top - this.helperProportions.height - this.margins.top - (parseInt(this.currentItem.css("marginBottom"),10) || 0) |
---|
416 | ]; |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | //Set the original element visibility to hidden to still fill out the white space |
---|
421 | if(this.options.placeholder != 'clone') |
---|
422 | this.currentItem.css('visibility', 'hidden'); |
---|
423 | |
---|
424 | //Post 'activate' events to possible containers |
---|
425 | if(!noActivation) { |
---|
426 | for (var i = this.containers.length - 1; i >= 0; i--) { this.containers[i].propagate("activate", e, this); } |
---|
427 | } |
---|
428 | |
---|
429 | //Prepare possible droppables |
---|
430 | if($.ui.ddmanager) $.ui.ddmanager.current = this; |
---|
431 | if ($.ui.ddmanager && !o.dropBehaviour) $.ui.ddmanager.prepareOffsets(this, e); |
---|
432 | |
---|
433 | this.dragging = true; |
---|
434 | |
---|
435 | this.mouseDrag(e); //Execute the drag once - this causes the helper not to be visible before getting its correct position |
---|
436 | return true; |
---|
437 | |
---|
438 | |
---|
439 | }, |
---|
440 | convertPositionTo: function(d, pos) { |
---|
441 | if(!pos) pos = this.position; |
---|
442 | var mod = d == "absolute" ? 1 : -1; |
---|
443 | return { |
---|
444 | top: ( |
---|
445 | pos.top // the calculated relative position |
---|
446 | + this.offset.parent.top * mod // The offsetParent's offset without borders (offset + border) |
---|
447 | - (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollTop) * mod // The offsetParent's scroll position |
---|
448 | + this.margins.top * mod //Add the margin (you don't want the margin counting in intersection methods) |
---|
449 | ), |
---|
450 | left: ( |
---|
451 | pos.left // the calculated relative position |
---|
452 | + this.offset.parent.left * mod // The offsetParent's offset without borders (offset + border) |
---|
453 | - (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollLeft) * mod // The offsetParent's scroll position |
---|
454 | + this.margins.left * mod //Add the margin (you don't want the margin counting in intersection methods) |
---|
455 | ) |
---|
456 | }; |
---|
457 | }, |
---|
458 | generatePosition: function(e) { |
---|
459 | |
---|
460 | var o = this.options; |
---|
461 | var position = { |
---|
462 | top: ( |
---|
463 | e.pageY // The absolute mouse position |
---|
464 | - this.offset.click.top // Click offset (relative to the element) |
---|
465 | - this.offset.parent.top // The offsetParent's offset without borders (offset + border) |
---|
466 | + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollTop) // The offsetParent's scroll position, not if the element is fixed |
---|
467 | ), |
---|
468 | left: ( |
---|
469 | e.pageX // The absolute mouse position |
---|
470 | - this.offset.click.left // Click offset (relative to the element) |
---|
471 | - this.offset.parent.left // The offsetParent's offset without borders (offset + border) |
---|
472 | + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollLeft) // The offsetParent's scroll position, not if the element is fixed |
---|
473 | ) |
---|
474 | }; |
---|
475 | |
---|
476 | if(!this.originalPosition) return position; //If we are not dragging yet, we won't check for options |
---|
477 | |
---|
478 | /* |
---|
479 | * - Position constraining - |
---|
480 | * Constrain the position to a mix of grid, containment. |
---|
481 | */ |
---|
482 | if(this.containment) { |
---|
483 | if(position.left < this.containment[0]) position.left = this.containment[0]; |
---|
484 | if(position.top < this.containment[1]) position.top = this.containment[1]; |
---|
485 | if(position.left > this.containment[2]) position.left = this.containment[2]; |
---|
486 | if(position.top > this.containment[3]) position.top = this.containment[3]; |
---|
487 | } |
---|
488 | |
---|
489 | if(o.grid) { |
---|
490 | var top = this.originalPosition.top + Math.round((position.top - this.originalPosition.top) / o.grid[1]) * o.grid[1]; |
---|
491 | position.top = this.containment ? (!(top < this.containment[1] || top > this.containment[3]) ? top : (!(top < this.containment[1]) ? top - o.grid[1] : top + o.grid[1])) : top; |
---|
492 | |
---|
493 | var left = this.originalPosition.left + Math.round((position.left - this.originalPosition.left) / o.grid[0]) * o.grid[0]; |
---|
494 | position.left = this.containment ? (!(left < this.containment[0] || left > this.containment[2]) ? left : (!(left < this.containment[0]) ? left - o.grid[0] : left + o.grid[0])) : left; |
---|
495 | } |
---|
496 | |
---|
497 | return position; |
---|
498 | }, |
---|
499 | mouseDrag: function(e) { |
---|
500 | |
---|
501 | //Compute the helpers position |
---|
502 | this.position = this.generatePosition(e); |
---|
503 | this.positionAbs = this.convertPositionTo("absolute"); |
---|
504 | |
---|
505 | //Call the internal plugins |
---|
506 | $.ui.plugin.call(this, "sort", [e, this.ui()]); |
---|
507 | |
---|
508 | //Regenerate the absolute position used for position checks |
---|
509 | this.positionAbs = this.convertPositionTo("absolute"); |
---|
510 | |
---|
511 | //Set the helper's position |
---|
512 | this.helper[0].style.left = this.position.left+'px'; |
---|
513 | this.helper[0].style.top = this.position.top+'px'; |
---|
514 | |
---|
515 | //Rearrange |
---|
516 | for (var i = this.items.length - 1; i >= 0; i--) { |
---|
517 | var intersection = this.intersectsWithEdge(this.items[i]); |
---|
518 | if(!intersection) continue; |
---|
519 | |
---|
520 | if(this.items[i].item[0] != this.currentItem[0] //cannot intersect with itself |
---|
521 | && this.currentItem[intersection == 1 ? "next" : "prev"]()[0] != this.items[i].item[0] //no useless actions that have been done before |
---|
522 | && !contains(this.currentItem[0], this.items[i].item[0]) //no action if the item moved is the parent of the item checked |
---|
523 | && (this.options.type == 'semi-dynamic' ? !contains(this.element[0], this.items[i].item[0]) : true) |
---|
524 | ) { |
---|
525 | |
---|
526 | this.direction = intersection == 1 ? "down" : "up"; |
---|
527 | this.rearrange(e, this.items[i]); |
---|
528 | this.propagate("change", e); //Call plugins and callbacks |
---|
529 | break; |
---|
530 | } |
---|
531 | } |
---|
532 | |
---|
533 | //Post events to containers |
---|
534 | this.contactContainers(e); |
---|
535 | |
---|
536 | //Interconnect with droppables |
---|
537 | if($.ui.ddmanager) $.ui.ddmanager.drag(this, e); |
---|
538 | |
---|
539 | //Call callbacks |
---|
540 | this.element.triggerHandler("sort", [e, this.ui()], this.options["sort"]); |
---|
541 | |
---|
542 | return false; |
---|
543 | |
---|
544 | }, |
---|
545 | rearrange: function(e, i, a, hardRefresh) { |
---|
546 | a ? a[0].appendChild(this.currentItem[0]) : i.item[0].parentNode.insertBefore(this.currentItem[0], (this.direction == 'down' ? i.item[0] : i.item[0].nextSibling)); |
---|
547 | |
---|
548 | //Various things done here to improve the performance: |
---|
549 | // 1. we create a setTimeout, that calls refreshPositions |
---|
550 | // 2. on the instance, we have a counter variable, that get's higher after every append |
---|
551 | // 3. on the local scope, we copy the counter variable, and check in the timeout, if it's still the same |
---|
552 | // 4. this lets only the last addition to the timeout stack through |
---|
553 | this.counter = this.counter ? ++this.counter : 1; |
---|
554 | var self = this, counter = this.counter; |
---|
555 | |
---|
556 | window.setTimeout(function() { |
---|
557 | if(counter == self.counter) self.refreshPositions(!hardRefresh); //Precompute after each DOM insertion, NOT on mousemove |
---|
558 | },0); |
---|
559 | |
---|
560 | if(this.options.placeholder) |
---|
561 | this.options.placeholder.update.call(this.element, this.currentItem, this.placeholder); |
---|
562 | }, |
---|
563 | mouseStop: function(e, noPropagation) { |
---|
564 | |
---|
565 | //If we are using droppables, inform the manager about the drop |
---|
566 | if ($.ui.ddmanager && !this.options.dropBehaviour) |
---|
567 | $.ui.ddmanager.drop(this, e); |
---|
568 | |
---|
569 | if(this.options.revert) { |
---|
570 | var self = this; |
---|
571 | var cur = self.currentItem.offset(); |
---|
572 | |
---|
573 | //Also animate the placeholder if we have one |
---|
574 | if(self.placeholder) self.placeholder.animate({ opacity: 'hide' }, (parseInt(this.options.revert, 10) || 500)-50); |
---|
575 | |
---|
576 | $(this.helper).animate({ |
---|
577 | left: cur.left - this.offset.parent.left - self.margins.left + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollLeft), |
---|
578 | top: cur.top - this.offset.parent.top - self.margins.top + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollTop) |
---|
579 | }, parseInt(this.options.revert, 10) || 500, function() { |
---|
580 | self.clear(e); |
---|
581 | }); |
---|
582 | } else { |
---|
583 | this.clear(e, noPropagation); |
---|
584 | } |
---|
585 | |
---|
586 | return false; |
---|
587 | |
---|
588 | }, |
---|
589 | clear: function(e, noPropagation) { |
---|
590 | |
---|
591 | if(this.domPosition.prev != this.currentItem.prev().not(".ui-sortable-helper")[0] || this.domPosition.parent != this.currentItem.parent()[0]) this.propagate("update", e, null, noPropagation); //Trigger update callback if the DOM position has changed |
---|
592 | if(!contains(this.element[0], this.currentItem[0])) { //Node was moved out of the current element |
---|
593 | this.propagate("remove", e, null, noPropagation); |
---|
594 | for (var i = this.containers.length - 1; i >= 0; i--){ |
---|
595 | if(contains(this.containers[i].element[0], this.currentItem[0])) { |
---|
596 | this.containers[i].propagate("update", e, this, noPropagation); |
---|
597 | this.containers[i].propagate("receive", e, this, noPropagation); |
---|
598 | } |
---|
599 | }; |
---|
600 | }; |
---|
601 | |
---|
602 | //Post events to containers |
---|
603 | for (var i = this.containers.length - 1; i >= 0; i--){ |
---|
604 | this.containers[i].propagate("deactivate", e, this, noPropagation); |
---|
605 | if(this.containers[i].containerCache.over) { |
---|
606 | this.containers[i].propagate("out", e, this); |
---|
607 | this.containers[i].containerCache.over = 0; |
---|
608 | } |
---|
609 | } |
---|
610 | |
---|
611 | this.dragging = false; |
---|
612 | if(this.cancelHelperRemoval) { |
---|
613 | this.propagate("stop", e, null, noPropagation); |
---|
614 | return false; |
---|
615 | } |
---|
616 | |
---|
617 | $(this.currentItem).css('visibility', ''); |
---|
618 | if(this.placeholder) this.placeholder.remove(); |
---|
619 | this.helper.remove(); this.helper = null; |
---|
620 | this.propagate("stop", e, null, noPropagation); |
---|
621 | |
---|
622 | return true; |
---|
623 | |
---|
624 | } |
---|
625 | })); |
---|
626 | |
---|
627 | $.extend($.ui.sortable, { |
---|
628 | getter: "serialize toArray", |
---|
629 | defaults: { |
---|
630 | helper: "clone", |
---|
631 | tolerance: "guess", |
---|
632 | distance: 1, |
---|
633 | delay: 0, |
---|
634 | scroll: true, |
---|
635 | scrollSensitivity: 20, |
---|
636 | scrollSpeed: 20, |
---|
637 | cancel: ":input", |
---|
638 | items: '> *', |
---|
639 | zIndex: 1000, |
---|
640 | dropOnEmpty: true, |
---|
641 | appendTo: "parent" |
---|
642 | } |
---|
643 | }); |
---|
644 | |
---|
645 | /* |
---|
646 | * Sortable Extensions |
---|
647 | */ |
---|
648 | |
---|
649 | $.ui.plugin.add("sortable", "cursor", { |
---|
650 | start: function(e, ui) { |
---|
651 | var t = $('body'); |
---|
652 | if (t.css("cursor")) ui.options._cursor = t.css("cursor"); |
---|
653 | t.css("cursor", ui.options.cursor); |
---|
654 | }, |
---|
655 | stop: function(e, ui) { |
---|
656 | if (ui.options._cursor) $('body').css("cursor", ui.options._cursor); |
---|
657 | } |
---|
658 | }); |
---|
659 | |
---|
660 | $.ui.plugin.add("sortable", "zIndex", { |
---|
661 | start: function(e, ui) { |
---|
662 | var t = ui.helper; |
---|
663 | if(t.css("zIndex")) ui.options._zIndex = t.css("zIndex"); |
---|
664 | t.css('zIndex', ui.options.zIndex); |
---|
665 | }, |
---|
666 | stop: function(e, ui) { |
---|
667 | if(ui.options._zIndex) $(ui.helper).css('zIndex', ui.options._zIndex); |
---|
668 | } |
---|
669 | }); |
---|
670 | |
---|
671 | $.ui.plugin.add("sortable", "opacity", { |
---|
672 | start: function(e, ui) { |
---|
673 | var t = ui.helper; |
---|
674 | if(t.css("opacity")) ui.options._opacity = t.css("opacity"); |
---|
675 | t.css('opacity', ui.options.opacity); |
---|
676 | }, |
---|
677 | stop: function(e, ui) { |
---|
678 | if(ui.options._opacity) $(ui.helper).css('opacity', ui.options._opacity); |
---|
679 | } |
---|
680 | }); |
---|
681 | |
---|
682 | $.ui.plugin.add("sortable", "scroll", { |
---|
683 | start: function(e, ui) { |
---|
684 | var o = ui.options; |
---|
685 | var i = $(this).data("sortable"); |
---|
686 | |
---|
687 | i.overflowY = function(el) { |
---|
688 | do { if(/auto|scroll/.test(el.css('overflow')) || (/auto|scroll/).test(el.css('overflow-y'))) return el; el = el.parent(); } while (el[0].parentNode); |
---|
689 | return $(document); |
---|
690 | }(i.currentItem); |
---|
691 | i.overflowX = function(el) { |
---|
692 | do { if(/auto|scroll/.test(el.css('overflow')) || (/auto|scroll/).test(el.css('overflow-x'))) return el; el = el.parent(); } while (el[0].parentNode); |
---|
693 | return $(document); |
---|
694 | }(i.currentItem); |
---|
695 | |
---|
696 | if(i.overflowY[0] != document && i.overflowY[0].tagName != 'HTML') i.overflowYOffset = i.overflowY.offset(); |
---|
697 | if(i.overflowX[0] != document && i.overflowX[0].tagName != 'HTML') i.overflowXOffset = i.overflowX.offset(); |
---|
698 | |
---|
699 | }, |
---|
700 | sort: function(e, ui) { |
---|
701 | |
---|
702 | var o = ui.options; |
---|
703 | var i = $(this).data("sortable"); |
---|
704 | |
---|
705 | if(i.overflowY[0] != document && i.overflowY[0].tagName != 'HTML') { |
---|
706 | if((i.overflowYOffset.top + i.overflowY[0].offsetHeight) - e.pageY < o.scrollSensitivity) |
---|
707 | i.overflowY[0].scrollTop = i.overflowY[0].scrollTop + o.scrollSpeed; |
---|
708 | if(e.pageY - i.overflowYOffset.top < o.scrollSensitivity) |
---|
709 | i.overflowY[0].scrollTop = i.overflowY[0].scrollTop - o.scrollSpeed; |
---|
710 | } else { |
---|
711 | if(e.pageY - $(document).scrollTop() < o.scrollSensitivity) |
---|
712 | $(document).scrollTop($(document).scrollTop() - o.scrollSpeed); |
---|
713 | if($(window).height() - (e.pageY - $(document).scrollTop()) < o.scrollSensitivity) |
---|
714 | $(document).scrollTop($(document).scrollTop() + o.scrollSpeed); |
---|
715 | } |
---|
716 | |
---|
717 | if(i.overflowX[0] != document && i.overflowX[0].tagName != 'HTML') { |
---|
718 | if((i.overflowXOffset.left + i.overflowX[0].offsetWidth) - e.pageX < o.scrollSensitivity) |
---|
719 | i.overflowX[0].scrollLeft = i.overflowX[0].scrollLeft + o.scrollSpeed; |
---|
720 | if(e.pageX - i.overflowXOffset.left < o.scrollSensitivity) |
---|
721 | i.overflowX[0].scrollLeft = i.overflowX[0].scrollLeft - o.scrollSpeed; |
---|
722 | } else { |
---|
723 | if(e.pageX - $(document).scrollLeft() < o.scrollSensitivity) |
---|
724 | $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed); |
---|
725 | if($(window).width() - (e.pageX - $(document).scrollLeft()) < o.scrollSensitivity) |
---|
726 | $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed); |
---|
727 | } |
---|
728 | |
---|
729 | } |
---|
730 | }); |
---|
731 | |
---|
732 | $.ui.plugin.add("sortable", "axis", { |
---|
733 | sort: function(e, ui) { |
---|
734 | |
---|
735 | var i = $(this).data("sortable"); |
---|
736 | |
---|
737 | if(ui.options.axis == "y") i.position.left = i.originalPosition.left; |
---|
738 | if(ui.options.axis == "x") i.position.top = i.originalPosition.top; |
---|
739 | |
---|
740 | } |
---|
741 | }); |
---|
742 | |
---|
743 | })(jQuery); |
---|