source: main/trunk/greenstone3/web/interfaces/oran/js/jquery-ui-1.8rc1/ui/jquery.ui.sortable.js@ 24245

Last change on this file since 24245 was 24245, checked in by sjb48, 13 years ago

Oran code for supporting format changes to document.

  • Property svn:executable set to *
File size: 39.1 KB
Line 
1/*
2 * jQuery UI Sortable 1.8rc1
3 *
4 * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
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 * jquery.ui.core.js
12 * jquery.ui.mouse.js
13 * jquery.ui.widget.js
14 */
15(function($) {
16
17$.widget("ui.sortable", $.ui.mouse, {
18 options: {
19 appendTo: "parent",
20 axis: false,
21 connectWith: false,
22 containment: false,
23 cursor: 'auto',
24 cursorAt: false,
25 dropOnEmpty: true,
26 forcePlaceholderSize: false,
27 forceHelperSize: false,
28 grid: false,
29 handle: false,
30 helper: "original",
31 items: '> *',
32 opacity: false,
33 placeholder: false,
34 revert: false,
35 scroll: true,
36 scrollSensitivity: 20,
37 scrollSpeed: 20,
38 scope: "default",
39 tolerance: "intersect",
40 zIndex: 1000
41 },
42 _create: function() {
43
44 var o = this.options;
45 this.containerCache = {};
46 this.element.addClass("ui-sortable");
47
48 //Get the items
49 this.refresh();
50
51 //Let's determine if the items are floating
52 this.floating = this.items.length ? (/left|right/).test(this.items[0].item.css('float')) : false;
53
54 //Let's determine the parent's offset
55 this.offset = this.element.offset();
56
57 //Initialize mouse events for interaction
58 this._mouseInit();
59
60 },
61
62 destroy: function() {
63 this.element
64 .removeClass("ui-sortable ui-sortable-disabled")
65 .removeData("sortable")
66 .unbind(".sortable");
67 this._mouseDestroy();
68
69 for ( var i = this.items.length - 1; i >= 0; i-- )
70 this.items[i].item.removeData("sortable-item");
71
72 return this;
73 },
74
75 _mouseCapture: function(event, overrideHandle) {
76
77 if (this.reverting) {
78 return false;
79 }
80
81 if(this.options.disabled || this.options.type == 'static') return false;
82
83 //We have to refresh the items data once first
84 this._refreshItems(event);
85
86 //Find out if the clicked node (or one of its parents) is a actual item in this.items
87 var currentItem = null, self = this, nodes = $(event.target).parents().each(function() {
88 if($.data(this, 'sortable-item') == self) {
89 currentItem = $(this);
90 return false;
91 }
92 });
93 if($.data(event.target, 'sortable-item') == self) currentItem = $(event.target);
94
95 if(!currentItem) return false;
96 if(this.options.handle && !overrideHandle) {
97 var validHandle = false;
98
99 $(this.options.handle, currentItem).find("*").andSelf().each(function() { if(this == event.target) validHandle = true; });
100 if(!validHandle) return false;
101 }
102
103 this.currentItem = currentItem;
104 this._removeCurrentsFromItems();
105 return true;
106
107 },
108
109 _mouseStart: function(event, overrideHandle, noActivation) {
110
111 var o = this.options, self = this;
112 this.currentContainer = this;
113
114 //We only need to call refreshPositions, because the refreshItems call has been moved to mouseCapture
115 this.refreshPositions();
116
117 //Create and append the visible helper
118 this.helper = this._createHelper(event);
119
120 //Cache the helper size
121 this._cacheHelperProportions();
122
123 /*
124 * - Position generation -
125 * This block generates everything position related - it's the core of draggables.
126 */
127
128 //Cache the margins of the original element
129 this._cacheMargins();
130
131 //Get the next scrolling parent
132 this.scrollParent = this.helper.scrollParent();
133
134 //The element's absolute position on the page minus margins
135 this.offset = this.currentItem.offset();
136 this.offset = {
137 top: this.offset.top - this.margins.top,
138 left: this.offset.left - this.margins.left
139 };
140
141 // Only after we got the offset, we can change the helper's position to absolute
142 // TODO: Still need to figure out a way to make relative sorting possible
143 this.helper.css("position", "absolute");
144 this.cssPosition = this.helper.css("position");
145
146 $.extend(this.offset, {
147 click: { //Where the click happened, relative to the element
148 left: event.pageX - this.offset.left,
149 top: event.pageY - this.offset.top
150 },
151 parent: this._getParentOffset(),
152 relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
153 });
154
155 //Generate the original position
156 this.originalPosition = this._generatePosition(event);
157 this.originalPageX = event.pageX;
158 this.originalPageY = event.pageY;
159
160 //Adjust the mouse offset relative to the helper if 'cursorAt' is supplied
161 (o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
162
163 //Cache the former DOM position
164 this.domPosition = { prev: this.currentItem.prev()[0], parent: this.currentItem.parent()[0] };
165
166 //If the helper is not the original, hide the original so it's not playing any role during the drag, won't cause anything bad this way
167 if(this.helper[0] != this.currentItem[0]) {
168 this.currentItem.hide();
169 }
170
171 //Create the placeholder
172 this._createPlaceholder();
173
174 //Set a containment if given in the options
175 if(o.containment)
176 this._setContainment();
177
178 if(o.cursor) { // cursor option
179 if ($('body').css("cursor")) this._storedCursor = $('body').css("cursor");
180 $('body').css("cursor", o.cursor);
181 }
182
183 if(o.opacity) { // opacity option
184 if (this.helper.css("opacity")) this._storedOpacity = this.helper.css("opacity");
185 this.helper.css("opacity", o.opacity);
186 }
187
188 if(o.zIndex) { // zIndex option
189 if (this.helper.css("zIndex")) this._storedZIndex = this.helper.css("zIndex");
190 this.helper.css("zIndex", o.zIndex);
191 }
192
193 //Prepare scrolling
194 if(this.scrollParent[0] != document && this.scrollParent[0].tagName != 'HTML')
195 this.overflowOffset = this.scrollParent.offset();
196
197 //Call callbacks
198 this._trigger("start", event, this._uiHash());
199
200 //Recache the helper size
201 if(!this._preserveHelperProportions)
202 this._cacheHelperProportions();
203
204
205 //Post 'activate' events to possible containers
206 if(!noActivation) {
207 for (var i = this.containers.length - 1; i >= 0; i--) { this.containers[i]._trigger("activate", event, self._uiHash(this)); }
208 }
209
210 //Prepare possible droppables
211 if($.ui.ddmanager)
212 $.ui.ddmanager.current = this;
213
214 if ($.ui.ddmanager && !o.dropBehaviour)
215 $.ui.ddmanager.prepareOffsets(this, event);
216
217 this.dragging = true;
218
219 this.helper.addClass("ui-sortable-helper");
220 this._mouseDrag(event); //Execute the drag once - this causes the helper not to be visible before getting its correct position
221 return true;
222
223 },
224
225 _mouseDrag: function(event) {
226
227 //Compute the helpers position
228 this.position = this._generatePosition(event);
229 this.positionAbs = this._convertPositionTo("absolute");
230
231 if (!this.lastPositionAbs) {
232 this.lastPositionAbs = this.positionAbs;
233 }
234
235 //Do scrolling
236 if(this.options.scroll) {
237 var o = this.options, scrolled = false;
238 if(this.scrollParent[0] != document && this.scrollParent[0].tagName != 'HTML') {
239
240 if((this.overflowOffset.top + this.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity)
241 this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop + o.scrollSpeed;
242 else if(event.pageY - this.overflowOffset.top < o.scrollSensitivity)
243 this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop - o.scrollSpeed;
244
245 if((this.overflowOffset.left + this.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity)
246 this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft + o.scrollSpeed;
247 else if(event.pageX - this.overflowOffset.left < o.scrollSensitivity)
248 this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft - o.scrollSpeed;
249
250 } else {
251
252 if(event.pageY - $(document).scrollTop() < o.scrollSensitivity)
253 scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
254 else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity)
255 scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);
256
257 if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity)
258 scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
259 else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity)
260 scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);
261
262 }
263
264 if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour)
265 $.ui.ddmanager.prepareOffsets(this, event);
266 }
267
268 //Regenerate the absolute position used for position checks
269 this.positionAbs = this._convertPositionTo("absolute");
270
271 //Set the helper position
272 if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px';
273 if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px';
274
275 //Rearrange
276 for (var i = this.items.length - 1; i >= 0; i--) {
277
278 //Cache variables and intersection, continue if no intersection
279 var item = this.items[i], itemElement = item.item[0], intersection = this._intersectsWithPointer(item);
280 if (!intersection) continue;
281
282 if(itemElement != this.currentItem[0] //cannot intersect with itself
283 && this.placeholder[intersection == 1 ? "next" : "prev"]()[0] != itemElement //no useless actions that have been done before
284 && !$.ui.contains(this.placeholder[0], itemElement) //no action if the item moved is the parent of the item checked
285 && (this.options.type == 'semi-dynamic' ? !$.ui.contains(this.element[0], itemElement) : true)
286 //&& itemElement.parentNode == this.placeholder[0].parentNode // only rearrange items within the same container
287 ) {
288
289 this.direction = intersection == 1 ? "down" : "up";
290
291 if (this.options.tolerance == "pointer" || this._intersectsWithSides(item)) {
292 this._rearrange(event, item);
293 } else {
294 break;
295 }
296
297 this._trigger("change", event, this._uiHash());
298 break;
299 }
300 }
301
302 //Post events to containers
303 this._contactContainers(event);
304
305 //Interconnect with droppables
306 if($.ui.ddmanager) $.ui.ddmanager.drag(this, event);
307
308 //Call callbacks
309 this._trigger('sort', event, this._uiHash());
310
311 this.lastPositionAbs = this.positionAbs;
312 return false;
313
314 },
315
316 _mouseStop: function(event, noPropagation) {
317
318 if(!event) return;
319
320 //If we are using droppables, inform the manager about the drop
321 if ($.ui.ddmanager && !this.options.dropBehaviour)
322 $.ui.ddmanager.drop(this, event);
323
324 if(this.options.revert) {
325 var self = this;
326 var cur = self.placeholder.offset();
327
328 self.reverting = true;
329
330 $(this.helper).animate({
331 left: cur.left - this.offset.parent.left - self.margins.left + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollLeft),
332 top: cur.top - this.offset.parent.top - self.margins.top + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollTop)
333 }, parseInt(this.options.revert, 10) || 500, function() {
334 self._clear(event);
335 });
336 } else {
337 this._clear(event, noPropagation);
338 }
339
340 return false;
341
342 },
343
344 cancel: function() {
345
346 var self = this;
347
348 if(this.dragging) {
349
350 this._mouseUp();
351
352 if(this.options.helper == "original")
353 this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper");
354 else
355 this.currentItem.show();
356
357 //Post deactivating events to containers
358 for (var i = this.containers.length - 1; i >= 0; i--){
359 this.containers[i]._trigger("deactivate", null, self._uiHash(this));
360 if(this.containers[i].containerCache.over) {
361 this.containers[i]._trigger("out", null, self._uiHash(this));
362 this.containers[i].containerCache.over = 0;
363 }
364 }
365
366 }
367
368 //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node!
369 if(this.placeholder[0].parentNode) this.placeholder[0].parentNode.removeChild(this.placeholder[0]);
370 if(this.options.helper != "original" && this.helper && this.helper[0].parentNode) this.helper.remove();
371
372 $.extend(this, {
373 helper: null,
374 dragging: false,
375 reverting: false,
376 _noFinalSort: null
377 });
378
379 if(this.domPosition.prev) {
380 $(this.domPosition.prev).after(this.currentItem);
381 } else {
382 $(this.domPosition.parent).prepend(this.currentItem);
383 }
384
385 return this;
386
387 },
388
389 serialize: function(o) {
390
391 var items = this._getItemsAsjQuery(o && o.connected);
392 var str = []; o = o || {};
393
394 $(items).each(function() {
395 var res = ($(o.item || this).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/));
396 if(res) str.push((o.key || res[1]+'[]')+'='+(o.key && o.expression ? res[1] : res[2]));
397 });
398
399 return str.join('&');
400
401 },
402
403 toArray: function(o) {
404
405 var items = this._getItemsAsjQuery(o && o.connected);
406 var ret = []; o = o || {};
407
408 items.each(function() { ret.push($(o.item || this).attr(o.attribute || 'id') || ''); });
409 return ret;
410
411 },
412
413 /* Be careful with the following core functions */
414 _intersectsWith: function(item) {
415
416 var x1 = this.positionAbs.left,
417 x2 = x1 + this.helperProportions.width,
418 y1 = this.positionAbs.top,
419 y2 = y1 + this.helperProportions.height;
420
421 var l = item.left,
422 r = l + item.width,
423 t = item.top,
424 b = t + item.height;
425
426 var dyClick = this.offset.click.top,
427 dxClick = this.offset.click.left;
428
429 var isOverElement = (y1 + dyClick) > t && (y1 + dyClick) < b && (x1 + dxClick) > l && (x1 + dxClick) < r;
430
431 if( this.options.tolerance == "pointer"
432 || this.options.forcePointerForContainers
433 || (this.options.tolerance != "pointer" && this.helperProportions[this.floating ? 'width' : 'height'] > item[this.floating ? 'width' : 'height'])
434 ) {
435 return isOverElement;
436 } else {
437
438 return (l < x1 + (this.helperProportions.width / 2) // Right Half
439 && x2 - (this.helperProportions.width / 2) < r // Left Half
440 && t < y1 + (this.helperProportions.height / 2) // Bottom Half
441 && y2 - (this.helperProportions.height / 2) < b ); // Top Half
442
443 }
444 },
445
446 _intersectsWithPointer: function(item) {
447
448 var isOverElementHeight = $.ui.isOverAxis(this.positionAbs.top + this.offset.click.top, item.top, item.height),
449 isOverElementWidth = $.ui.isOverAxis(this.positionAbs.left + this.offset.click.left, item.left, item.width),
450 isOverElement = isOverElementHeight && isOverElementWidth,
451 verticalDirection = this._getDragVerticalDirection(),
452 horizontalDirection = this._getDragHorizontalDirection();
453
454 if (!isOverElement)
455 return false;
456
457 return this.floating ?
458 ( ((horizontalDirection && horizontalDirection == "right") || verticalDirection == "down") ? 2 : 1 )
459 : ( verticalDirection && (verticalDirection == "down" ? 2 : 1) );
460
461 },
462
463 _intersectsWithSides: function(item) {
464
465 var isOverBottomHalf = $.ui.isOverAxis(this.positionAbs.top + this.offset.click.top, item.top + (item.height/2), item.height),
466 isOverRightHalf = $.ui.isOverAxis(this.positionAbs.left + this.offset.click.left, item.left + (item.width/2), item.width),
467 verticalDirection = this._getDragVerticalDirection(),
468 horizontalDirection = this._getDragHorizontalDirection();
469
470 if (this.floating && horizontalDirection) {
471 return ((horizontalDirection == "right" && isOverRightHalf) || (horizontalDirection == "left" && !isOverRightHalf));
472 } else {
473 return verticalDirection && ((verticalDirection == "down" && isOverBottomHalf) || (verticalDirection == "up" && !isOverBottomHalf));
474 }
475
476 },
477
478 _getDragVerticalDirection: function() {
479 var delta = this.positionAbs.top - this.lastPositionAbs.top;
480 return delta != 0 && (delta > 0 ? "down" : "up");
481 },
482
483 _getDragHorizontalDirection: function() {
484 var delta = this.positionAbs.left - this.lastPositionAbs.left;
485 return delta != 0 && (delta > 0 ? "right" : "left");
486 },
487
488 refresh: function(event) {
489 this._refreshItems(event);
490 this.refreshPositions();
491 return this;
492 },
493
494 _connectWith: function() {
495 var options = this.options;
496 return options.connectWith.constructor == String
497 ? [options.connectWith]
498 : options.connectWith;
499 },
500
501 _getItemsAsjQuery: function(connected) {
502
503 var self = this;
504 var items = [];
505 var queries = [];
506 var connectWith = this._connectWith();
507
508 if(connectWith && connected) {
509 for (var i = connectWith.length - 1; i >= 0; i--){
510 var cur = $(connectWith[i]);
511 for (var j = cur.length - 1; j >= 0; j--){
512 var inst = $.data(cur[j], 'sortable');
513 if(inst && inst != this && !inst.options.disabled) {
514 queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element) : $(inst.options.items, inst.element).not(".ui-sortable-helper").not('.ui-sortable-placeholder'), inst]);
515 }
516 };
517 };
518 }
519
520 queries.push([$.isFunction(this.options.items) ? this.options.items.call(this.element, null, { options: this.options, item: this.currentItem }) : $(this.options.items, this.element).not(".ui-sortable-helper").not('.ui-sortable-placeholder'), this]);
521
522 for (var i = queries.length - 1; i >= 0; i--){
523 queries[i][0].each(function() {
524 items.push(this);
525 });
526 };
527
528 return $(items);
529
530 },
531
532 _removeCurrentsFromItems: function() {
533
534 var list = this.currentItem.find(":data(sortable-item)");
535
536 for (var i=0; i < this.items.length; i++) {
537
538 for (var j=0; j < list.length; j++) {
539 if(list[j] == this.items[i].item[0])
540 this.items.splice(i,1);
541 };
542
543 };
544
545 },
546
547 _refreshItems: function(event) {
548
549 this.items = [];
550 this.containers = [this];
551 var items = this.items;
552 var self = this;
553 var queries = [[$.isFunction(this.options.items) ? this.options.items.call(this.element[0], event, { item: this.currentItem }) : $(this.options.items, this.element), this]];
554 var connectWith = this._connectWith();
555
556 if(connectWith) {
557 for (var i = connectWith.length - 1; i >= 0; i--){
558 var cur = $(connectWith[i]);
559 for (var j = cur.length - 1; j >= 0; j--){
560 var inst = $.data(cur[j], 'sortable');
561 if(inst && inst != this && !inst.options.disabled) {
562 queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element[0], event, { item: this.currentItem }) : $(inst.options.items, inst.element), inst]);
563 this.containers.push(inst);
564 }
565 };
566 };
567 }
568
569 for (var i = queries.length - 1; i >= 0; i--) {
570 var targetData = queries[i][1];
571 var _queries = queries[i][0];
572
573 for (var j=0, queriesLength = _queries.length; j < queriesLength; j++) {
574 var item = $(_queries[j]);
575
576 item.data('sortable-item', targetData); // Data for target checking (mouse manager)
577
578 items.push({
579 item: item,
580 instance: targetData,
581 width: 0, height: 0,
582 left: 0, top: 0
583 });
584 };
585 };
586
587 },
588
589 refreshPositions: function(fast) {
590
591 //This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change
592 if(this.offsetParent && this.helper) {
593 this.offset.parent = this._getParentOffset();
594 }
595
596 for (var i = this.items.length - 1; i >= 0; i--){
597 var item = this.items[i];
598
599 var t = this.options.toleranceElement ? $(this.options.toleranceElement, item.item) : item.item;
600
601 if (!fast) {
602 item.width = t.outerWidth();
603 item.height = t.outerHeight();
604 }
605
606 var p = t.offset();
607 item.left = p.left;
608 item.top = p.top;
609 };
610
611 if(this.options.custom && this.options.custom.refreshContainers) {
612 this.options.custom.refreshContainers.call(this);
613 } else {
614 for (var i = this.containers.length - 1; i >= 0; i--){
615 var p = this.containers[i].element.offset();
616 this.containers[i].containerCache.left = p.left;
617 this.containers[i].containerCache.top = p.top;
618 this.containers[i].containerCache.width = this.containers[i].element.outerWidth();
619 this.containers[i].containerCache.height = this.containers[i].element.outerHeight();
620 };
621 }
622
623 return this;
624 },
625
626 _createPlaceholder: function(that) {
627
628 var self = that || this, o = self.options;
629
630 if(!o.placeholder || o.placeholder.constructor == String) {
631 var className = o.placeholder;
632 o.placeholder = {
633 element: function() {
634
635 var el = $(document.createElement(self.currentItem[0].nodeName))
636 .addClass(className || self.currentItem[0].className+" ui-sortable-placeholder")
637 .removeClass("ui-sortable-helper")[0];
638
639 if(!className)
640 el.style.visibility = "hidden";
641
642 return el;
643 },
644 update: function(container, p) {
645
646 // 1. If a className is set as 'placeholder option, we don't force sizes - the class is responsible for that
647 // 2. The option 'forcePlaceholderSize can be enabled to force it even if a class name is specified
648 if(className && !o.forcePlaceholderSize) return;
649
650 //If the element doesn't have a actual height by itself (without styles coming from a stylesheet), it receives the inline height from the dragged item
651 if(!p.height()) { p.height(self.currentItem.innerHeight() - parseInt(self.currentItem.css('paddingTop')||0, 10) - parseInt(self.currentItem.css('paddingBottom')||0, 10)); };
652 if(!p.width()) { p.width(self.currentItem.innerWidth() - parseInt(self.currentItem.css('paddingLeft')||0, 10) - parseInt(self.currentItem.css('paddingRight')||0, 10)); };
653 }
654 };
655 }
656
657 //Create the placeholder
658 self.placeholder = $(o.placeholder.element.call(self.element, self.currentItem));
659
660 //Append it after the actual current item
661 self.currentItem.after(self.placeholder);
662
663 //Update the size of the placeholder (TODO: Logic to fuzzy, see line 316/317)
664 o.placeholder.update(self, self.placeholder);
665
666 },
667
668 _contactContainers: function(event) {
669
670 // get innermost container that intersects with item
671 var innermostContainer = null, innermostIndex = null;
672
673
674 for (var i = this.containers.length - 1; i >= 0; i--){
675
676 // never consider a container that's located within the item itself
677 if($.ui.contains(this.currentItem[0], this.containers[i].element[0]))
678 continue;
679
680 if(this._intersectsWith(this.containers[i].containerCache)) {
681
682 // if we've already found a container and it's more "inner" than this, then continue
683 if(innermostContainer && $.ui.contains(this.containers[i].element[0], innermostContainer.element[0]))
684 continue;
685
686 innermostContainer = this.containers[i];
687 innermostIndex = i;
688
689 } else {
690 // container doesn't intersect. trigger "out" event if necessary
691 if(this.containers[i].containerCache.over) {
692 this.containers[i]._trigger("out", event, this._uiHash(this));
693 this.containers[i].containerCache.over = 0;
694 }
695 }
696
697 }
698
699 // if no intersecting containers found, return
700 if(!innermostContainer) return;
701
702 // move the item into the container if it's not there already
703 if(this.currentContainer != this.containers[innermostIndex]) {
704
705 //When entering a new container, we will find the item with the least distance and append our item near it
706 var dist = 10000; var itemWithLeastDistance = null; var base = this.positionAbs[this.containers[innermostIndex].floating ? 'left' : 'top'];
707 for (var j = this.items.length - 1; j >= 0; j--) {
708 if(!$.ui.contains(this.containers[innermostIndex].element[0], this.items[j].item[0])) continue;
709 var cur = this.items[j][this.containers[innermostIndex].floating ? 'left' : 'top'];
710 if(Math.abs(cur - base) < dist) {
711 dist = Math.abs(cur - base); itemWithLeastDistance = this.items[j];
712 }
713 }
714
715 if(!itemWithLeastDistance && !this.options.dropOnEmpty) //Check if dropOnEmpty is enabled
716 return;
717
718 this.currentContainer = this.containers[innermostIndex];
719 itemWithLeastDistance ? this._rearrange(event, itemWithLeastDistance, null, true) : this._rearrange(event, null, this.containers[innermostIndex].element, true);
720 this._trigger("change", event, this._uiHash());
721 this.containers[innermostIndex]._trigger("change", event, this._uiHash(this));
722
723 //Update the placeholder
724 this.options.placeholder.update(this.currentContainer, this.placeholder);
725
726 }
727
728 this.containers[innermostIndex]._trigger("over", event, this._uiHash(this));
729 this.containers[innermostIndex].containerCache.over = 1;
730
731 },
732
733 _createHelper: function(event) {
734
735 var o = this.options;
736 var helper = $.isFunction(o.helper) ? $(o.helper.apply(this.element[0], [event, this.currentItem])) : (o.helper == 'clone' ? this.currentItem.clone() : this.currentItem);
737
738 if(!helper.parents('body').length) //Add the helper to the DOM if that didn't happen already
739 $(o.appendTo != 'parent' ? o.appendTo : this.currentItem[0].parentNode)[0].appendChild(helper[0]);
740
741 if(helper[0] == this.currentItem[0])
742 this._storedCSS = { width: this.currentItem[0].style.width, height: this.currentItem[0].style.height, position: this.currentItem.css("position"), top: this.currentItem.css("top"), left: this.currentItem.css("left") };
743
744 if(helper[0].style.width == '' || o.forceHelperSize) helper.width(this.currentItem.width());
745 if(helper[0].style.height == '' || o.forceHelperSize) helper.height(this.currentItem.height());
746
747 return helper;
748
749 },
750
751 _adjustOffsetFromHelper: function(obj) {
752 if (typeof obj == 'string') {
753 obj = obj.split(' ');
754 }
755 if ($.isArray(obj)) {
756 obj = {left: +obj[0], top: +obj[1] || 0};
757 }
758 if ('left' in obj) {
759 this.offset.click.left = obj.left + this.margins.left;
760 }
761 if ('right' in obj) {
762 this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left;
763 }
764 if ('top' in obj) {
765 this.offset.click.top = obj.top + this.margins.top;
766 }
767 if ('bottom' in obj) {
768 this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top;
769 }
770 },
771
772 _getParentOffset: function() {
773
774
775 //Get the offsetParent and cache its position
776 this.offsetParent = this.helper.offsetParent();
777 var po = this.offsetParent.offset();
778
779 // This is a special case where we need to modify a offset calculated on start, since the following happened:
780 // 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
781 // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
782 // the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
783 if(this.cssPosition == 'absolute' && this.scrollParent[0] != document && $.ui.contains(this.scrollParent[0], this.offsetParent[0])) {
784 po.left += this.scrollParent.scrollLeft();
785 po.top += this.scrollParent.scrollTop();
786 }
787
788 if((this.offsetParent[0] == document.body) //This needs to be actually done for all browsers, since pageX/pageY includes this information
789 || (this.offsetParent[0].tagName && this.offsetParent[0].tagName.toLowerCase() == 'html' && $.browser.msie)) //Ugly IE fix
790 po = { top: 0, left: 0 };
791
792 return {
793 top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0),
794 left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0)
795 };
796
797 },
798
799 _getRelativeOffset: function() {
800
801 if(this.cssPosition == "relative") {
802 var p = this.currentItem.position();
803 return {
804 top: p.top - (parseInt(this.helper.css("top"),10) || 0) + this.scrollParent.scrollTop(),
805 left: p.left - (parseInt(this.helper.css("left"),10) || 0) + this.scrollParent.scrollLeft()
806 };
807 } else {
808 return { top: 0, left: 0 };
809 }
810
811 },
812
813 _cacheMargins: function() {
814 this.margins = {
815 left: (parseInt(this.currentItem.css("marginLeft"),10) || 0),
816 top: (parseInt(this.currentItem.css("marginTop"),10) || 0)
817 };
818 },
819
820 _cacheHelperProportions: function() {
821 this.helperProportions = {
822 width: this.helper.outerWidth(),
823 height: this.helper.outerHeight()
824 };
825 },
826
827 _setContainment: function() {
828
829 var o = this.options;
830 if(o.containment == 'parent') o.containment = this.helper[0].parentNode;
831 if(o.containment == 'document' || o.containment == 'window') this.containment = [
832 0 - this.offset.relative.left - this.offset.parent.left,
833 0 - this.offset.relative.top - this.offset.parent.top,
834 $(o.containment == 'document' ? document : window).width() - this.helperProportions.width - this.margins.left,
835 ($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top
836 ];
837
838 if(!(/^(document|window|parent)$/).test(o.containment)) {
839 var ce = $(o.containment)[0];
840 var co = $(o.containment).offset();
841 var over = ($(ce).css("overflow") != 'hidden');
842
843 this.containment = [
844 co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0) - this.margins.left,
845 co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0) - this.margins.top,
846 co.left+(over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderLeftWidth"),10) || 0) - (parseInt($(ce).css("paddingRight"),10) || 0) - this.helperProportions.width - this.margins.left,
847 co.top+(over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderTopWidth"),10) || 0) - (parseInt($(ce).css("paddingBottom"),10) || 0) - this.helperProportions.height - this.margins.top
848 ];
849 }
850
851 },
852
853 _convertPositionTo: function(d, pos) {
854
855 if(!pos) pos = this.position;
856 var mod = d == "absolute" ? 1 : -1;
857 var o = this.options, scroll = this.cssPosition == 'absolute' && !(this.scrollParent[0] != document && $.ui.contains(this.scrollParent[0], this.offsetParent[0])) ? this.offsetParent : this.scrollParent, scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName);
858
859 return {
860 top: (
861 pos.top // The absolute mouse position
862 + this.offset.relative.top * mod // Only for relative positioned nodes: Relative offset from element to offset parent
863 + this.offset.parent.top * mod // The offsetParent's offset without borders (offset + border)
864 - ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod)
865 ),
866 left: (
867 pos.left // The absolute mouse position
868 + this.offset.relative.left * mod // Only for relative positioned nodes: Relative offset from element to offset parent
869 + this.offset.parent.left * mod // The offsetParent's offset without borders (offset + border)
870 - ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) * mod)
871 )
872 };
873
874 },
875
876 _generatePosition: function(event) {
877
878 var o = this.options, scroll = this.cssPosition == 'absolute' && !(this.scrollParent[0] != document && $.ui.contains(this.scrollParent[0], this.offsetParent[0])) ? this.offsetParent : this.scrollParent, scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName);
879
880 // This is another very weird special case that only happens for relative elements:
881 // 1. If the css position is relative
882 // 2. and the scroll parent is the document or similar to the offset parent
883 // we have to refresh the relative offset during the scroll so there are no jumps
884 if(this.cssPosition == 'relative' && !(this.scrollParent[0] != document && this.scrollParent[0] != this.offsetParent[0])) {
885 this.offset.relative = this._getRelativeOffset();
886 }
887
888 var pageX = event.pageX;
889 var pageY = event.pageY;
890
891 /*
892 * - Position constraining -
893 * Constrain the position to a mix of grid, containment.
894 */
895
896 if(this.originalPosition) { //If we are not dragging yet, we won't check for options
897
898 if(this.containment) {
899 if(event.pageX - this.offset.click.left < this.containment[0]) pageX = this.containment[0] + this.offset.click.left;
900 if(event.pageY - this.offset.click.top < this.containment[1]) pageY = this.containment[1] + this.offset.click.top;
901 if(event.pageX - this.offset.click.left > this.containment[2]) pageX = this.containment[2] + this.offset.click.left;
902 if(event.pageY - this.offset.click.top > this.containment[3]) pageY = this.containment[3] + this.offset.click.top;
903 }
904
905 if(o.grid) {
906 var top = this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1];
907 pageY = this.containment ? (!(top - this.offset.click.top < this.containment[1] || top - this.offset.click.top > this.containment[3]) ? top : (!(top - this.offset.click.top < this.containment[1]) ? top - o.grid[1] : top + o.grid[1])) : top;
908
909 var left = this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0];
910 pageX = this.containment ? (!(left - this.offset.click.left < this.containment[0] || left - this.offset.click.left > this.containment[2]) ? left : (!(left - this.offset.click.left < this.containment[0]) ? left - o.grid[0] : left + o.grid[0])) : left;
911 }
912
913 }
914
915 return {
916 top: (
917 pageY // The absolute mouse position
918 - this.offset.click.top // Click offset (relative to the element)
919 - this.offset.relative.top // Only for relative positioned nodes: Relative offset from element to offset parent
920 - this.offset.parent.top // The offsetParent's offset without borders (offset + border)
921 + ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ))
922 ),
923 left: (
924 pageX // The absolute mouse position
925 - this.offset.click.left // Click offset (relative to the element)
926 - this.offset.relative.left // Only for relative positioned nodes: Relative offset from element to offset parent
927 - this.offset.parent.left // The offsetParent's offset without borders (offset + border)
928 + ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ))
929 )
930 };
931
932 },
933
934 _rearrange: function(event, i, a, hardRefresh) {
935
936 a ? a[0].appendChild(this.placeholder[0]) : i.item[0].parentNode.insertBefore(this.placeholder[0], (this.direction == 'down' ? i.item[0] : i.item[0].nextSibling));
937
938 //Various things done here to improve the performance:
939 // 1. we create a setTimeout, that calls refreshPositions
940 // 2. on the instance, we have a counter variable, that get's higher after every append
941 // 3. on the local scope, we copy the counter variable, and check in the timeout, if it's still the same
942 // 4. this lets only the last addition to the timeout stack through
943 this.counter = this.counter ? ++this.counter : 1;
944 var self = this, counter = this.counter;
945
946 window.setTimeout(function() {
947 if(counter == self.counter) self.refreshPositions(!hardRefresh); //Precompute after each DOM insertion, NOT on mousemove
948 },0);
949
950 },
951
952 _clear: function(event, noPropagation) {
953
954 this.reverting = false;
955 // We delay all events that have to be triggered to after the point where the placeholder has been removed and
956 // everything else normalized again
957 var delayedTriggers = [], self = this;
958
959 // We first have to update the dom position of the actual currentItem
960 // Note: don't do it if the current item is already removed (by a user), or it gets reappended (see #4088)
961 if(!this._noFinalSort && this.currentItem[0].parentNode) this.placeholder.before(this.currentItem);
962 this._noFinalSort = null;
963
964 if(this.helper[0] == this.currentItem[0]) {
965 for(var i in this._storedCSS) {
966 if(this._storedCSS[i] == 'auto' || this._storedCSS[i] == 'static') this._storedCSS[i] = '';
967 }
968 this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper");
969 } else {
970 this.currentItem.show();
971 }
972
973 if(this.fromOutside && !noPropagation) delayedTriggers.push(function(event) { this._trigger("receive", event, this._uiHash(this.fromOutside)); });
974 if((this.fromOutside || this.domPosition.prev != this.currentItem.prev().not(".ui-sortable-helper")[0] || this.domPosition.parent != this.currentItem.parent()[0]) && !noPropagation) delayedTriggers.push(function(event) { this._trigger("update", event, this._uiHash()); }); //Trigger update callback if the DOM position has changed
975 if(!$.ui.contains(this.element[0], this.currentItem[0])) { //Node was moved out of the current element
976 if(!noPropagation) delayedTriggers.push(function(event) { this._trigger("remove", event, this._uiHash()); });
977 for (var i = this.containers.length - 1; i >= 0; i--){
978 if($.ui.contains(this.containers[i].element[0], this.currentItem[0]) && !noPropagation) {
979 delayedTriggers.push((function(c) { return function(event) { c._trigger("receive", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
980 delayedTriggers.push((function(c) { return function(event) { c._trigger("update", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
981 }
982 };
983 };
984
985 //Post events to containers
986 for (var i = this.containers.length - 1; i >= 0; i--){
987 if(!noPropagation) delayedTriggers.push((function(c) { return function(event) { c._trigger("deactivate", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
988 if(this.containers[i].containerCache.over) {
989 delayedTriggers.push((function(c) { return function(event) { c._trigger("out", event, this._uiHash(this)); }; }).call(this, this.containers[i]));
990 this.containers[i].containerCache.over = 0;
991 }
992 }
993
994 //Do what was originally in plugins
995 if(this._storedCursor) $('body').css("cursor", this._storedCursor); //Reset cursor
996 if(this._storedOpacity) this.helper.css("opacity", this._storedOpacity); //Reset opacity
997 if(this._storedZIndex) this.helper.css("zIndex", this._storedZIndex == 'auto' ? '' : this._storedZIndex); //Reset z-index
998
999 this.dragging = false;
1000 if(this.cancelHelperRemoval) {
1001 if(!noPropagation) {
1002 this._trigger("beforeStop", event, this._uiHash());
1003 for (var i=0; i < delayedTriggers.length; i++) { delayedTriggers[i].call(this, event); }; //Trigger all delayed events
1004 this._trigger("stop", event, this._uiHash());
1005 }
1006 return false;
1007 }
1008
1009 if(!noPropagation) this._trigger("beforeStop", event, this._uiHash());
1010
1011 //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node!
1012 this.placeholder[0].parentNode.removeChild(this.placeholder[0]);
1013
1014 if(this.helper[0] != this.currentItem[0]) this.helper.remove(); this.helper = null;
1015
1016 if(!noPropagation) {
1017 for (var i=0; i < delayedTriggers.length; i++) { delayedTriggers[i].call(this, event); }; //Trigger all delayed events
1018 this._trigger("stop", event, this._uiHash());
1019 }
1020
1021 this.fromOutside = false;
1022 return true;
1023
1024 },
1025
1026 _trigger: function() {
1027 if ($.Widget.prototype._trigger.apply(this, arguments) === false) {
1028 this.cancel();
1029 }
1030 },
1031
1032 _uiHash: function(inst) {
1033 var self = inst || this;
1034 return {
1035 helper: self.helper,
1036 placeholder: self.placeholder || $([]),
1037 position: self.position,
1038 originalPosition: self.originalPosition,
1039 offset: self.positionAbs,
1040 item: self.currentItem,
1041 sender: inst ? inst.element : null
1042 };
1043 }
1044
1045});
1046
1047$.extend($.ui.sortable, {
1048 version: "1.8rc1",
1049 eventPrefix: "sort"
1050});
1051
1052})(jQuery);
Note: See TracBrowser for help on using the repository browser.