sonata_jqueryui_js_jquery.ui.draggable_4.js 31 KB

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