1 | /*
|
---|
2 | * jQuery UI Draggable 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/Draggables
|
---|
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.draggable", $.ui.mouse, {
|
---|
18 | options: {
|
---|
19 | addClasses: true,
|
---|
20 | appendTo: "parent",
|
---|
21 | axis: false,
|
---|
22 | connectToSortable: false,
|
---|
23 | containment: false,
|
---|
24 | cursor: "auto",
|
---|
25 | cursorAt: false,
|
---|
26 | grid: false,
|
---|
27 | handle: false,
|
---|
28 | helper: "original",
|
---|
29 | iframeFix: false,
|
---|
30 | opacity: false,
|
---|
31 | refreshPositions: false,
|
---|
32 | revert: false,
|
---|
33 | revertDuration: 500,
|
---|
34 | scope: "default",
|
---|
35 | scroll: true,
|
---|
36 | scrollSensitivity: 20,
|
---|
37 | scrollSpeed: 20,
|
---|
38 | snap: false,
|
---|
39 | snapMode: "both",
|
---|
40 | snapTolerance: 20,
|
---|
41 | stack: false,
|
---|
42 | zIndex: false
|
---|
43 | },
|
---|
44 | _create: function() {
|
---|
45 |
|
---|
46 | if (this.options.helper == 'original' && !(/^(?:r|a|f)/).test(this.element.css("position")))
|
---|
47 | this.element[0].style.position = 'relative';
|
---|
48 |
|
---|
49 | (this.options.addClasses && this.element.addClass("ui-draggable"));
|
---|
50 | (this.options.disabled && this.element.addClass("ui-draggable-disabled"));
|
---|
51 |
|
---|
52 | this._mouseInit();
|
---|
53 |
|
---|
54 | },
|
---|
55 |
|
---|
56 | destroy: function() {
|
---|
57 | if(!this.element.data('draggable')) return;
|
---|
58 | this.element
|
---|
59 | .removeData("draggable")
|
---|
60 | .unbind(".draggable")
|
---|
61 | .removeClass("ui-draggable"
|
---|
62 | + " ui-draggable-dragging"
|
---|
63 | + " ui-draggable-disabled");
|
---|
64 | this._mouseDestroy();
|
---|
65 |
|
---|
66 | return this;
|
---|
67 | },
|
---|
68 |
|
---|
69 | _mouseCapture: function(event) {
|
---|
70 |
|
---|
71 | var o = this.options;
|
---|
72 |
|
---|
73 | // among others, prevent a drag on a resizable-handle
|
---|
74 | if (this.helper || o.disabled || $(event.target).is('.ui-resizable-handle'))
|
---|
75 | return false;
|
---|
76 |
|
---|
77 | //Quit if we're not on a valid handle
|
---|
78 | this.handle = this._getHandle(event);
|
---|
79 | if (!this.handle)
|
---|
80 | return false;
|
---|
81 |
|
---|
82 | return true;
|
---|
83 |
|
---|
84 | },
|
---|
85 |
|
---|
86 | _mouseStart: function(event) {
|
---|
87 |
|
---|
88 | var o = this.options;
|
---|
89 |
|
---|
90 | //Create and append the visible helper
|
---|
91 | this.helper = this._createHelper(event);
|
---|
92 |
|
---|
93 | //Cache the helper size
|
---|
94 | this._cacheHelperProportions();
|
---|
95 |
|
---|
96 | //If ddmanager is used for droppables, set the global draggable
|
---|
97 | if($.ui.ddmanager)
|
---|
98 | $.ui.ddmanager.current = this;
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * - Position generation -
|
---|
102 | * This block generates everything position related - it's the core of draggables.
|
---|
103 | */
|
---|
104 |
|
---|
105 | //Cache the margins of the original element
|
---|
106 | this._cacheMargins();
|
---|
107 |
|
---|
108 | //Store the helper's css position
|
---|
109 | this.cssPosition = this.helper.css("position");
|
---|
110 | this.scrollParent = this.helper.scrollParent();
|
---|
111 |
|
---|
112 | //The element's absolute position on the page minus margins
|
---|
113 | this.offset = this.positionAbs = this.element.offset();
|
---|
114 | this.offset = {
|
---|
115 | top: this.offset.top - this.margins.top,
|
---|
116 | left: this.offset.left - this.margins.left
|
---|
117 | };
|
---|
118 |
|
---|
119 | $.extend(this.offset, {
|
---|
120 | click: { //Where the click happened, relative to the element
|
---|
121 | left: event.pageX - this.offset.left,
|
---|
122 | top: event.pageY - this.offset.top
|
---|
123 | },
|
---|
124 | parent: this._getParentOffset(),
|
---|
125 | relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
|
---|
126 | });
|
---|
127 |
|
---|
128 | //Generate the original position
|
---|
129 | this.originalPosition = this.position = this._generatePosition(event);
|
---|
130 | this.originalPageX = event.pageX;
|
---|
131 | this.originalPageY = event.pageY;
|
---|
132 |
|
---|
133 | //Adjust the mouse offset relative to the helper if 'cursorAt' is supplied
|
---|
134 | (o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
|
---|
135 |
|
---|
136 | //Set a containment if given in the options
|
---|
137 | if(o.containment)
|
---|
138 | this._setContainment();
|
---|
139 |
|
---|
140 | //Call plugins and callbacks
|
---|
141 | this._trigger("start", event);
|
---|
142 |
|
---|
143 | //Recache the helper size
|
---|
144 | this._cacheHelperProportions();
|
---|
145 |
|
---|
146 | //Prepare the droppable offsets
|
---|
147 | if ($.ui.ddmanager && !o.dropBehaviour)
|
---|
148 | $.ui.ddmanager.prepareOffsets(this, event);
|
---|
149 |
|
---|
150 | this.helper.addClass("ui-draggable-dragging");
|
---|
151 | this._mouseDrag(event, true); //Execute the drag once - this causes the helper not to be visible before getting its correct position
|
---|
152 | return true;
|
---|
153 | },
|
---|
154 |
|
---|
155 | _mouseDrag: function(event, noPropagation) {
|
---|
156 |
|
---|
157 | //Compute the helpers position
|
---|
158 | this.position = this._generatePosition(event);
|
---|
159 | this.positionAbs = this._convertPositionTo("absolute");
|
---|
160 |
|
---|
161 | //Call plugins and callbacks and use the resulting position if something is returned
|
---|
162 | if (!noPropagation) {
|
---|
163 | var ui = this._uiHash();
|
---|
164 | this._trigger('drag', event, ui);
|
---|
165 | this.position = ui.position;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px';
|
---|
169 | if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px';
|
---|
170 | if($.ui.ddmanager) $.ui.ddmanager.drag(this, event);
|
---|
171 |
|
---|
172 | return false;
|
---|
173 | },
|
---|
174 |
|
---|
175 | _mouseStop: function(event) {
|
---|
176 |
|
---|
177 | //If we are using droppables, inform the manager about the drop
|
---|
178 | var dropped = false;
|
---|
179 | if ($.ui.ddmanager && !this.options.dropBehaviour)
|
---|
180 | dropped = $.ui.ddmanager.drop(this, event);
|
---|
181 |
|
---|
182 | //if a drop comes from outside (a sortable)
|
---|
183 | if(this.dropped) {
|
---|
184 | dropped = this.dropped;
|
---|
185 | this.dropped = false;
|
---|
186 | }
|
---|
187 |
|
---|
188 | //if the original element is removed, don't bother to continue
|
---|
189 | if(!this.element[0] || !this.element[0].parentNode)
|
---|
190 | return false;
|
---|
191 |
|
---|
192 | if((this.options.revert == "invalid" && !dropped) || (this.options.revert == "valid" && dropped) || this.options.revert === true || ($.isFunction(this.options.revert) && this.options.revert.call(this.element, dropped))) {
|
---|
193 | var self = this;
|
---|
194 | $(this.helper).animate(this.originalPosition, parseInt(this.options.revertDuration, 10), function() {
|
---|
195 | self._trigger("stop", event);
|
---|
196 | self._clear();
|
---|
197 | });
|
---|
198 | } else {
|
---|
199 | this._trigger("stop", event);
|
---|
200 | this._clear();
|
---|
201 | }
|
---|
202 |
|
---|
203 | return false;
|
---|
204 | },
|
---|
205 |
|
---|
206 | _getHandle: function(event) {
|
---|
207 |
|
---|
208 | var handle = !this.options.handle || !$(this.options.handle, this.element).length ? true : false;
|
---|
209 | $(this.options.handle, this.element)
|
---|
210 | .find("*")
|
---|
211 | .andSelf()
|
---|
212 | .each(function() {
|
---|
213 | if(this == event.target) handle = true;
|
---|
214 | });
|
---|
215 |
|
---|
216 | return handle;
|
---|
217 |
|
---|
218 | },
|
---|
219 |
|
---|
220 | _createHelper: function(event) {
|
---|
221 |
|
---|
222 | var o = this.options;
|
---|
223 | var helper = $.isFunction(o.helper) ? $(o.helper.apply(this.element[0], [event])) : (o.helper == 'clone' ? this.element.clone() : this.element);
|
---|
224 |
|
---|
225 | if(!helper.parents('body').length)
|
---|
226 | helper.appendTo((o.appendTo == 'parent' ? this.element[0].parentNode : o.appendTo));
|
---|
227 |
|
---|
228 | if(helper[0] != this.element[0] && !(/(fixed|absolute)/).test(helper.css("position")))
|
---|
229 | helper.css("position", "absolute");
|
---|
230 |
|
---|
231 | return helper;
|
---|
232 |
|
---|
233 | },
|
---|
234 |
|
---|
235 | _adjustOffsetFromHelper: function(obj) {
|
---|
236 | if (typeof obj == 'string') {
|
---|
237 | obj = obj.split(' ');
|
---|
238 | }
|
---|
239 | if ($.isArray(obj)) {
|
---|
240 | obj = {left: +obj[0], top: +obj[1] || 0};
|
---|
241 | }
|
---|
242 | if ('left' in obj) {
|
---|
243 | this.offset.click.left = obj.left + this.margins.left;
|
---|
244 | }
|
---|
245 | if ('right' in obj) {
|
---|
246 | this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left;
|
---|
247 | }
|
---|
248 | if ('top' in obj) {
|
---|
249 | this.offset.click.top = obj.top + this.margins.top;
|
---|
250 | }
|
---|
251 | if ('bottom' in obj) {
|
---|
252 | this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top;
|
---|
253 | }
|
---|
254 | },
|
---|
255 |
|
---|
256 | _getParentOffset: function() {
|
---|
257 |
|
---|
258 | //Get the offsetParent and cache its position
|
---|
259 | this.offsetParent = this.helper.offsetParent();
|
---|
260 | var po = this.offsetParent.offset();
|
---|
261 |
|
---|
262 | // This is a special case where we need to modify a offset calculated on start, since the following happened:
|
---|
263 | // 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
|
---|
264 | // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
|
---|
265 | // the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
|
---|
266 | if(this.cssPosition == 'absolute' && this.scrollParent[0] != document && $.ui.contains(this.scrollParent[0], this.offsetParent[0])) {
|
---|
267 | po.left += this.scrollParent.scrollLeft();
|
---|
268 | po.top += this.scrollParent.scrollTop();
|
---|
269 | }
|
---|
270 |
|
---|
271 | if((this.offsetParent[0] == document.body) //This needs to be actually done for all browsers, since pageX/pageY includes this information
|
---|
272 | || (this.offsetParent[0].tagName && this.offsetParent[0].tagName.toLowerCase() == 'html' && $.browser.msie)) //Ugly IE fix
|
---|
273 | po = { top: 0, left: 0 };
|
---|
274 |
|
---|
275 | return {
|
---|
276 | top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0),
|
---|
277 | left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0)
|
---|
278 | };
|
---|
279 |
|
---|
280 | },
|
---|
281 |
|
---|
282 | _getRelativeOffset: function() {
|
---|
283 |
|
---|
284 | if(this.cssPosition == "relative") {
|
---|
285 | var p = this.element.position();
|
---|
286 | return {
|
---|
287 | top: p.top - (parseInt(this.helper.css("top"),10) || 0) + this.scrollParent.scrollTop(),
|
---|
288 | left: p.left - (parseInt(this.helper.css("left"),10) || 0) + this.scrollParent.scrollLeft()
|
---|
289 | };
|
---|
290 | } else {
|
---|
291 | return { top: 0, left: 0 };
|
---|
292 | }
|
---|
293 |
|
---|
294 | },
|
---|
295 |
|
---|
296 | _cacheMargins: function() {
|
---|
297 | this.margins = {
|
---|
298 | left: (parseInt(this.element.css("marginLeft"),10) || 0),
|
---|
299 | top: (parseInt(this.element.css("marginTop"),10) || 0)
|
---|
300 | };
|
---|
301 | },
|
---|
302 |
|
---|
303 | _cacheHelperProportions: function() {
|
---|
304 | this.helperProportions = {
|
---|
305 | width: this.helper.outerWidth(),
|
---|
306 | height: this.helper.outerHeight()
|
---|
307 | };
|
---|
308 | },
|
---|
309 |
|
---|
310 | _setContainment: function() {
|
---|
311 |
|
---|
312 | var o = this.options;
|
---|
313 | if(o.containment == 'parent') o.containment = this.helper[0].parentNode;
|
---|
314 | if(o.containment == 'document' || o.containment == 'window') this.containment = [
|
---|
315 | 0 - this.offset.relative.left - this.offset.parent.left,
|
---|
316 | 0 - this.offset.relative.top - this.offset.parent.top,
|
---|
317 | $(o.containment == 'document' ? document : window).width() - this.helperProportions.width - this.margins.left,
|
---|
318 | ($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top
|
---|
319 | ];
|
---|
320 |
|
---|
321 | if(!(/^(document|window|parent)$/).test(o.containment) && o.containment.constructor != Array) {
|
---|
322 | var ce = $(o.containment)[0]; if(!ce) return;
|
---|
323 | var co = $(o.containment).offset();
|
---|
324 | var over = ($(ce).css("overflow") != 'hidden');
|
---|
325 |
|
---|
326 | this.containment = [
|
---|
327 | co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0) - this.margins.left,
|
---|
328 | co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0) - this.margins.top,
|
---|
329 | 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,
|
---|
330 | 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
|
---|
331 | ];
|
---|
332 | } else if(o.containment.constructor == Array) {
|
---|
333 | this.containment = o.containment;
|
---|
334 | }
|
---|
335 |
|
---|
336 | },
|
---|
337 |
|
---|
338 | _convertPositionTo: function(d, pos) {
|
---|
339 |
|
---|
340 | if(!pos) pos = this.position;
|
---|
341 | var mod = d == "absolute" ? 1 : -1;
|
---|
342 | 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);
|
---|
343 |
|
---|
344 | return {
|
---|
345 | top: (
|
---|
346 | pos.top // The absolute mouse position
|
---|
347 | + this.offset.relative.top * mod // Only for relative positioned nodes: Relative offset from element to offset parent
|
---|
348 | + this.offset.parent.top * mod // The offsetParent's offset without borders (offset + border)
|
---|
349 | - ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod)
|
---|
350 | ),
|
---|
351 | left: (
|
---|
352 | pos.left // The absolute mouse position
|
---|
353 | + this.offset.relative.left * mod // Only for relative positioned nodes: Relative offset from element to offset parent
|
---|
354 | + this.offset.parent.left * mod // The offsetParent's offset without borders (offset + border)
|
---|
355 | - ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) * mod)
|
---|
356 | )
|
---|
357 | };
|
---|
358 |
|
---|
359 | },
|
---|
360 |
|
---|
361 | _generatePosition: function(event) {
|
---|
362 |
|
---|
363 | 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);
|
---|
364 |
|
---|
365 | // This is another very weird special case that only happens for relative elements:
|
---|
366 | // 1. If the css position is relative
|
---|
367 | // 2. and the scroll parent is the document or similar to the offset parent
|
---|
368 | // we have to refresh the relative offset during the scroll so there are no jumps
|
---|
369 | if(this.cssPosition == 'relative' && !(this.scrollParent[0] != document && this.scrollParent[0] != this.offsetParent[0])) {
|
---|
370 | this.offset.relative = this._getRelativeOffset();
|
---|
371 | }
|
---|
372 |
|
---|
373 | var pageX = event.pageX;
|
---|
374 | var pageY = event.pageY;
|
---|
375 |
|
---|
376 | /*
|
---|
377 | * - Position constraining -
|
---|
378 | * Constrain the position to a mix of grid, containment.
|
---|
379 | */
|
---|
380 |
|
---|
381 | if(this.originalPosition) { //If we are not dragging yet, we won't check for options
|
---|
382 |
|
---|
383 | if(this.containment) {
|
---|
384 | if(event.pageX - this.offset.click.left < this.containment[0]) pageX = this.containment[0] + this.offset.click.left;
|
---|
385 | if(event.pageY - this.offset.click.top < this.containment[1]) pageY = this.containment[1] + this.offset.click.top;
|
---|
386 | if(event.pageX - this.offset.click.left > this.containment[2]) pageX = this.containment[2] + this.offset.click.left;
|
---|
387 | if(event.pageY - this.offset.click.top > this.containment[3]) pageY = this.containment[3] + this.offset.click.top;
|
---|
388 | }
|
---|
389 |
|
---|
390 | if(o.grid) {
|
---|
391 | var top = this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1];
|
---|
392 | 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;
|
---|
393 |
|
---|
394 | var left = this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0];
|
---|
395 | 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;
|
---|
396 | }
|
---|
397 |
|
---|
398 | }
|
---|
399 |
|
---|
400 | return {
|
---|
401 | top: (
|
---|
402 | pageY // The absolute mouse position
|
---|
403 | - this.offset.click.top // Click offset (relative to the element)
|
---|
404 | - this.offset.relative.top // Only for relative positioned nodes: Relative offset from element to offset parent
|
---|
405 | - this.offset.parent.top // The offsetParent's offset without borders (offset + border)
|
---|
406 | + ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ))
|
---|
407 | ),
|
---|
408 | left: (
|
---|
409 | pageX // The absolute mouse position
|
---|
410 | - this.offset.click.left // Click offset (relative to the element)
|
---|
411 | - this.offset.relative.left // Only for relative positioned nodes: Relative offset from element to offset parent
|
---|
412 | - this.offset.parent.left // The offsetParent's offset without borders (offset + border)
|
---|
413 | + ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ))
|
---|
414 | )
|
---|
415 | };
|
---|
416 |
|
---|
417 | },
|
---|
418 |
|
---|
419 | _clear: function() {
|
---|
420 | this.helper.removeClass("ui-draggable-dragging");
|
---|
421 | if(this.helper[0] != this.element[0] && !this.cancelHelperRemoval) this.helper.remove();
|
---|
422 | //if($.ui.ddmanager) $.ui.ddmanager.current = null;
|
---|
423 | this.helper = null;
|
---|
424 | this.cancelHelperRemoval = false;
|
---|
425 | },
|
---|
426 |
|
---|
427 | // From now on bulk stuff - mainly helpers
|
---|
428 |
|
---|
429 | _trigger: function(type, event, ui) {
|
---|
430 | ui = ui || this._uiHash();
|
---|
431 | $.ui.plugin.call(this, type, [event, ui]);
|
---|
432 | if(type == "drag") this.positionAbs = this._convertPositionTo("absolute"); //The absolute position has to be recalculated after plugins
|
---|
433 | return $.Widget.prototype._trigger.call(this, type, event, ui);
|
---|
434 | },
|
---|
435 |
|
---|
436 | plugins: {},
|
---|
437 |
|
---|
438 | _uiHash: function(event) {
|
---|
439 | return {
|
---|
440 | helper: this.helper,
|
---|
441 | position: this.position,
|
---|
442 | originalPosition: this.originalPosition,
|
---|
443 | offset: this.positionAbs
|
---|
444 | };
|
---|
445 | }
|
---|
446 |
|
---|
447 | });
|
---|
448 |
|
---|
449 | $.extend($.ui.draggable, {
|
---|
450 | version: "1.8rc1",
|
---|
451 | eventPrefix: "drag"
|
---|
452 | });
|
---|
453 |
|
---|
454 | $.ui.plugin.add("draggable", "connectToSortable", {
|
---|
455 | start: function(event, ui) {
|
---|
456 |
|
---|
457 | var inst = $(this).data("draggable"), o = inst.options,
|
---|
458 | uiSortable = $.extend({}, ui, { item: inst.element });
|
---|
459 | inst.sortables = [];
|
---|
460 | $(o.connectToSortable).each(function() {
|
---|
461 | var sortable = $.data(this, 'sortable');
|
---|
462 | if (sortable && !sortable.options.disabled) {
|
---|
463 | inst.sortables.push({
|
---|
464 | instance: sortable,
|
---|
465 | shouldRevert: sortable.options.revert
|
---|
466 | });
|
---|
467 | sortable._refreshItems(); //Do a one-time refresh at start to refresh the containerCache
|
---|
468 | sortable._trigger("activate", event, uiSortable);
|
---|
469 | }
|
---|
470 | });
|
---|
471 |
|
---|
472 | },
|
---|
473 | stop: function(event, ui) {
|
---|
474 |
|
---|
475 | //If we are still over the sortable, we fake the stop event of the sortable, but also remove helper
|
---|
476 | var inst = $(this).data("draggable"),
|
---|
477 | uiSortable = $.extend({}, ui, { item: inst.element });
|
---|
478 |
|
---|
479 | $.each(inst.sortables, function() {
|
---|
480 | if(this.instance.isOver) {
|
---|
481 |
|
---|
482 | this.instance.isOver = 0;
|
---|
483 |
|
---|
484 | inst.cancelHelperRemoval = true; //Don't remove the helper in the draggable instance
|
---|
485 | this.instance.cancelHelperRemoval = false; //Remove it in the sortable instance (so sortable plugins like revert still work)
|
---|
486 |
|
---|
487 | //The sortable revert is supported, and we have to set a temporary dropped variable on the draggable to support revert: 'valid/invalid'
|
---|
488 | if(this.shouldRevert) this.instance.options.revert = true;
|
---|
489 |
|
---|
490 | //Trigger the stop of the sortable
|
---|
491 | this.instance._mouseStop(event);
|
---|
492 |
|
---|
493 | this.instance.options.helper = this.instance.options._helper;
|
---|
494 |
|
---|
495 | //If the helper has been the original item, restore properties in the sortable
|
---|
496 | if(inst.options.helper == 'original')
|
---|
497 | this.instance.currentItem.css({ top: 'auto', left: 'auto' });
|
---|
498 |
|
---|
499 | } else {
|
---|
500 | this.instance.cancelHelperRemoval = false; //Remove the helper in the sortable instance
|
---|
501 | this.instance._trigger("deactivate", event, uiSortable);
|
---|
502 | }
|
---|
503 |
|
---|
504 | });
|
---|
505 |
|
---|
506 | },
|
---|
507 | drag: function(event, ui) {
|
---|
508 |
|
---|
509 | var inst = $(this).data("draggable"), self = this;
|
---|
510 |
|
---|
511 | var checkPos = function(o) {
|
---|
512 | var dyClick = this.offset.click.top, dxClick = this.offset.click.left;
|
---|
513 | var helperTop = this.positionAbs.top, helperLeft = this.positionAbs.left;
|
---|
514 | var itemHeight = o.height, itemWidth = o.width;
|
---|
515 | var itemTop = o.top, itemLeft = o.left;
|
---|
516 |
|
---|
517 | return $.ui.isOver(helperTop + dyClick, helperLeft + dxClick, itemTop, itemLeft, itemHeight, itemWidth);
|
---|
518 | };
|
---|
519 |
|
---|
520 | $.each(inst.sortables, function(i) {
|
---|
521 |
|
---|
522 | //Copy over some variables to allow calling the sortable's native _intersectsWith
|
---|
523 | this.instance.positionAbs = inst.positionAbs;
|
---|
524 | this.instance.helperProportions = inst.helperProportions;
|
---|
525 | this.instance.offset.click = inst.offset.click;
|
---|
526 |
|
---|
527 | if(this.instance._intersectsWith(this.instance.containerCache)) {
|
---|
528 |
|
---|
529 | //If it intersects, we use a little isOver variable and set it once, so our move-in stuff gets fired only once
|
---|
530 | if(!this.instance.isOver) {
|
---|
531 |
|
---|
532 | this.instance.isOver = 1;
|
---|
533 | //Now we fake the start of dragging for the sortable instance,
|
---|
534 | //by cloning the list group item, appending it to the sortable and using it as inst.currentItem
|
---|
535 | //We can then fire the start event of the sortable with our passed browser event, and our own helper (so it doesn't create a new one)
|
---|
536 | this.instance.currentItem = $(self).clone().appendTo(this.instance.element).data("sortable-item", true);
|
---|
537 | this.instance.options._helper = this.instance.options.helper; //Store helper option to later restore it
|
---|
538 | this.instance.options.helper = function() { return ui.helper[0]; };
|
---|
539 |
|
---|
540 | event.target = this.instance.currentItem[0];
|
---|
541 | this.instance._mouseCapture(event, true);
|
---|
542 | this.instance._mouseStart(event, true, true);
|
---|
543 |
|
---|
544 | //Because the browser event is way off the new appended portlet, we modify a couple of variables to reflect the changes
|
---|
545 | this.instance.offset.click.top = inst.offset.click.top;
|
---|
546 | this.instance.offset.click.left = inst.offset.click.left;
|
---|
547 | this.instance.offset.parent.left -= inst.offset.parent.left - this.instance.offset.parent.left;
|
---|
548 | this.instance.offset.parent.top -= inst.offset.parent.top - this.instance.offset.parent.top;
|
---|
549 |
|
---|
550 | inst._trigger("toSortable", event);
|
---|
551 | inst.dropped = this.instance.element; //draggable revert needs that
|
---|
552 | //hack so receive/update callbacks work (mostly)
|
---|
553 | inst.currentItem = inst.element;
|
---|
554 | this.instance.fromOutside = inst;
|
---|
555 |
|
---|
556 | }
|
---|
557 |
|
---|
558 | //Provided we did all the previous steps, we can fire the drag event of the sortable on every draggable drag, when it intersects with the sortable
|
---|
559 | if(this.instance.currentItem) this.instance._mouseDrag(event);
|
---|
560 |
|
---|
561 | } else {
|
---|
562 |
|
---|
563 | //If it doesn't intersect with the sortable, and it intersected before,
|
---|
564 | //we fake the drag stop of the sortable, but make sure it doesn't remove the helper by using cancelHelperRemoval
|
---|
565 | if(this.instance.isOver) {
|
---|
566 |
|
---|
567 | this.instance.isOver = 0;
|
---|
568 | this.instance.cancelHelperRemoval = true;
|
---|
569 |
|
---|
570 | //Prevent reverting on this forced stop
|
---|
571 | this.instance.options.revert = false;
|
---|
572 |
|
---|
573 | // The out event needs to be triggered independently
|
---|
574 | this.instance._trigger('out', event, this.instance._uiHash(this.instance));
|
---|
575 |
|
---|
576 | this.instance._mouseStop(event, true);
|
---|
577 | this.instance.options.helper = this.instance.options._helper;
|
---|
578 |
|
---|
579 | //Now we remove our currentItem, the list group clone again, and the placeholder, and animate the helper back to it's original size
|
---|
580 | this.instance.currentItem.remove();
|
---|
581 | if(this.instance.placeholder) this.instance.placeholder.remove();
|
---|
582 |
|
---|
583 | inst._trigger("fromSortable", event);
|
---|
584 | inst.dropped = false; //draggable revert needs that
|
---|
585 | }
|
---|
586 |
|
---|
587 | };
|
---|
588 |
|
---|
589 | });
|
---|
590 |
|
---|
591 | }
|
---|
592 | });
|
---|
593 |
|
---|
594 | $.ui.plugin.add("draggable", "cursor", {
|
---|
595 | start: function(event, ui) {
|
---|
596 | var t = $('body'), o = $(this).data('draggable').options;
|
---|
597 | if (t.css("cursor")) o._cursor = t.css("cursor");
|
---|
598 | t.css("cursor", o.cursor);
|
---|
599 | },
|
---|
600 | stop: function(event, ui) {
|
---|
601 | var o = $(this).data('draggable').options;
|
---|
602 | if (o._cursor) $('body').css("cursor", o._cursor);
|
---|
603 | }
|
---|
604 | });
|
---|
605 |
|
---|
606 | $.ui.plugin.add("draggable", "iframeFix", {
|
---|
607 | start: function(event, ui) {
|
---|
608 | var o = $(this).data('draggable').options;
|
---|
609 | $(o.iframeFix === true ? "iframe" : o.iframeFix).each(function() {
|
---|
610 | $('<div class="ui-draggable-iframeFix" style="background: #fff;"></div>')
|
---|
611 | .css({
|
---|
612 | width: this.offsetWidth+"px", height: this.offsetHeight+"px",
|
---|
613 | position: "absolute", opacity: "0.001", zIndex: 1000
|
---|
614 | })
|
---|
615 | .css($(this).offset())
|
---|
616 | .appendTo("body");
|
---|
617 | });
|
---|
618 | },
|
---|
619 | stop: function(event, ui) {
|
---|
620 | $("div.ui-draggable-iframeFix").each(function() { this.parentNode.removeChild(this); }); //Remove frame helpers
|
---|
621 | }
|
---|
622 | });
|
---|
623 |
|
---|
624 | $.ui.plugin.add("draggable", "opacity", {
|
---|
625 | start: function(event, ui) {
|
---|
626 | var t = $(ui.helper), o = $(this).data('draggable').options;
|
---|
627 | if(t.css("opacity")) o._opacity = t.css("opacity");
|
---|
628 | t.css('opacity', o.opacity);
|
---|
629 | },
|
---|
630 | stop: function(event, ui) {
|
---|
631 | var o = $(this).data('draggable').options;
|
---|
632 | if(o._opacity) $(ui.helper).css('opacity', o._opacity);
|
---|
633 | }
|
---|
634 | });
|
---|
635 |
|
---|
636 | $.ui.plugin.add("draggable", "scroll", {
|
---|
637 | start: function(event, ui) {
|
---|
638 | var i = $(this).data("draggable");
|
---|
639 | if(i.scrollParent[0] != document && i.scrollParent[0].tagName != 'HTML') i.overflowOffset = i.scrollParent.offset();
|
---|
640 | },
|
---|
641 | drag: function(event, ui) {
|
---|
642 |
|
---|
643 | var i = $(this).data("draggable"), o = i.options, scrolled = false;
|
---|
644 |
|
---|
645 | if(i.scrollParent[0] != document && i.scrollParent[0].tagName != 'HTML') {
|
---|
646 |
|
---|
647 | if(!o.axis || o.axis != 'x') {
|
---|
648 | if((i.overflowOffset.top + i.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity)
|
---|
649 | i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop + o.scrollSpeed;
|
---|
650 | else if(event.pageY - i.overflowOffset.top < o.scrollSensitivity)
|
---|
651 | i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop - o.scrollSpeed;
|
---|
652 | }
|
---|
653 |
|
---|
654 | if(!o.axis || o.axis != 'y') {
|
---|
655 | if((i.overflowOffset.left + i.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity)
|
---|
656 | i.scrollParent[0].scrollLeft = scrolled = i.scrollParent[0].scrollLeft + o.scrollSpeed;
|
---|
657 | else if(event.pageX - i.overflowOffset.left < o.scrollSensitivity)
|
---|
658 | i.scrollParent[0].scrollLeft = scrolled = i.scrollParent[0].scrollLeft - o.scrollSpeed;
|
---|
659 | }
|
---|
660 |
|
---|
661 | } else {
|
---|
662 |
|
---|
663 | if(!o.axis || o.axis != 'x') {
|
---|
664 | if(event.pageY - $(document).scrollTop() < o.scrollSensitivity)
|
---|
665 | scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
|
---|
666 | else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity)
|
---|
667 | scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);
|
---|
668 | }
|
---|
669 |
|
---|
670 | if(!o.axis || o.axis != 'y') {
|
---|
671 | if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity)
|
---|
672 | scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
|
---|
673 | else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity)
|
---|
674 | scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);
|
---|
675 | }
|
---|
676 |
|
---|
677 | }
|
---|
678 |
|
---|
679 | if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour)
|
---|
680 | $.ui.ddmanager.prepareOffsets(i, event);
|
---|
681 |
|
---|
682 | }
|
---|
683 | });
|
---|
684 |
|
---|
685 | $.ui.plugin.add("draggable", "snap", {
|
---|
686 | start: function(event, ui) {
|
---|
687 |
|
---|
688 | var i = $(this).data("draggable"), o = i.options;
|
---|
689 | i.snapElements = [];
|
---|
690 |
|
---|
691 | $(o.snap.constructor != String ? ( o.snap.items || ':data(draggable)' ) : o.snap).each(function() {
|
---|
692 | var $t = $(this); var $o = $t.offset();
|
---|
693 | if(this != i.element[0]) i.snapElements.push({
|
---|
694 | item: this,
|
---|
695 | width: $t.outerWidth(), height: $t.outerHeight(),
|
---|
696 | top: $o.top, left: $o.left
|
---|
697 | });
|
---|
698 | });
|
---|
699 |
|
---|
700 | },
|
---|
701 | drag: function(event, ui) {
|
---|
702 |
|
---|
703 | var inst = $(this).data("draggable"), o = inst.options;
|
---|
704 | var d = o.snapTolerance;
|
---|
705 |
|
---|
706 | var x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width,
|
---|
707 | y1 = ui.offset.top, y2 = y1 + inst.helperProportions.height;
|
---|
708 |
|
---|
709 | for (var i = inst.snapElements.length - 1; i >= 0; i--){
|
---|
710 |
|
---|
711 | var l = inst.snapElements[i].left, r = l + inst.snapElements[i].width,
|
---|
712 | t = inst.snapElements[i].top, b = t + inst.snapElements[i].height;
|
---|
713 |
|
---|
714 | //Yes, I know, this is insane ;)
|
---|
715 | if(!((l-d < x1 && x1 < r+d && t-d < y1 && y1 < b+d) || (l-d < x1 && x1 < r+d && t-d < y2 && y2 < b+d) || (l-d < x2 && x2 < r+d && t-d < y1 && y1 < b+d) || (l-d < x2 && x2 < r+d && t-d < y2 && y2 < b+d))) {
|
---|
716 | if(inst.snapElements[i].snapping) (inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
|
---|
717 | inst.snapElements[i].snapping = false;
|
---|
718 | continue;
|
---|
719 | }
|
---|
720 |
|
---|
721 | if(o.snapMode != 'inner') {
|
---|
722 | var ts = Math.abs(t - y2) <= d;
|
---|
723 | var bs = Math.abs(b - y1) <= d;
|
---|
724 | var ls = Math.abs(l - x2) <= d;
|
---|
725 | var rs = Math.abs(r - x1) <= d;
|
---|
726 | if(ts) ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
|
---|
727 | if(bs) ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top - inst.margins.top;
|
---|
728 | if(ls) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left - inst.margins.left;
|
---|
729 | if(rs) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left - inst.margins.left;
|
---|
730 | }
|
---|
731 |
|
---|
732 | var first = (ts || bs || ls || rs);
|
---|
733 |
|
---|
734 | if(o.snapMode != 'outer') {
|
---|
735 | var ts = Math.abs(t - y1) <= d;
|
---|
736 | var bs = Math.abs(b - y2) <= d;
|
---|
737 | var ls = Math.abs(l - x1) <= d;
|
---|
738 | var rs = Math.abs(r - x2) <= d;
|
---|
739 | if(ts) ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top - inst.margins.top;
|
---|
740 | if(bs) ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
|
---|
741 | if(ls) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left - inst.margins.left;
|
---|
742 | if(rs) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left - inst.margins.left;
|
---|
743 | }
|
---|
744 |
|
---|
745 | if(!inst.snapElements[i].snapping && (ts || bs || ls || rs || first))
|
---|
746 | (inst.options.snap.snap && inst.options.snap.snap.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
|
---|
747 | inst.snapElements[i].snapping = (ts || bs || ls || rs || first);
|
---|
748 |
|
---|
749 | };
|
---|
750 |
|
---|
751 | }
|
---|
752 | });
|
---|
753 |
|
---|
754 | $.ui.plugin.add("draggable", "stack", {
|
---|
755 | start: function(event, ui) {
|
---|
756 |
|
---|
757 | var o = $(this).data("draggable").options;
|
---|
758 |
|
---|
759 | var group = $.makeArray($(o.stack)).sort(function(a,b) {
|
---|
760 | return (parseInt($(a).css("zIndex"),10) || 0) - (parseInt($(b).css("zIndex"),10) || 0);
|
---|
761 | });
|
---|
762 |
|
---|
763 | var min = parseInt(group[0].style.zIndex) || 0;
|
---|
764 | $(group).each(function(i) {
|
---|
765 | this.style.zIndex = min + i;
|
---|
766 | });
|
---|
767 |
|
---|
768 | this[0].style.zIndex = min + group.length;
|
---|
769 |
|
---|
770 | }
|
---|
771 | });
|
---|
772 |
|
---|
773 | $.ui.plugin.add("draggable", "zIndex", {
|
---|
774 | start: function(event, ui) {
|
---|
775 | var t = $(ui.helper), o = $(this).data("draggable").options;
|
---|
776 | if(t.css("zIndex")) o._zIndex = t.css("zIndex");
|
---|
777 | t.css('zIndex', o.zIndex);
|
---|
778 | },
|
---|
779 | stop: function(event, ui) {
|
---|
780 | var o = $(this).data("draggable").options;
|
---|
781 | if(o._zIndex) $(ui.helper).css('zIndex', o._zIndex);
|
---|
782 | }
|
---|
783 | });
|
---|
784 |
|
---|
785 | })(jQuery);
|
---|