Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

798 строки
29 KiB

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