sortable.esm-673fe959.js 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. 'use strict';
  7. /**!
  8. * Sortable 1.15.0
  9. * @author RubaXa <trash@rubaxa.org>
  10. * @author owenm <owen23355@gmail.com>
  11. * @license MIT
  12. */
  13. function ownKeys(object, enumerableOnly) {
  14. var keys = Object.keys(object);
  15. if (Object.getOwnPropertySymbols) {
  16. var symbols = Object.getOwnPropertySymbols(object);
  17. if (enumerableOnly) {
  18. symbols = symbols.filter(function (sym) {
  19. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  20. });
  21. }
  22. keys.push.apply(keys, symbols);
  23. }
  24. return keys;
  25. }
  26. function _objectSpread2(target) {
  27. for (var i = 1; i < arguments.length; i++) {
  28. var source = arguments[i] != null ? arguments[i] : {};
  29. if (i % 2) {
  30. ownKeys(Object(source), true).forEach(function (key) {
  31. _defineProperty(target, key, source[key]);
  32. });
  33. } else if (Object.getOwnPropertyDescriptors) {
  34. Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
  35. } else {
  36. ownKeys(Object(source)).forEach(function (key) {
  37. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  38. });
  39. }
  40. }
  41. return target;
  42. }
  43. function _typeof(obj) {
  44. "@babel/helpers - typeof";
  45. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  46. _typeof = function (obj) {
  47. return typeof obj;
  48. };
  49. } else {
  50. _typeof = function (obj) {
  51. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  52. };
  53. }
  54. return _typeof(obj);
  55. }
  56. function _defineProperty(obj, key, value) {
  57. if (key in obj) {
  58. Object.defineProperty(obj, key, {
  59. value: value,
  60. enumerable: true,
  61. configurable: true,
  62. writable: true
  63. });
  64. } else {
  65. obj[key] = value;
  66. }
  67. return obj;
  68. }
  69. function _extends() {
  70. _extends = Object.assign || function (target) {
  71. for (var i = 1; i < arguments.length; i++) {
  72. var source = arguments[i];
  73. for (var key in source) {
  74. if (Object.prototype.hasOwnProperty.call(source, key)) {
  75. target[key] = source[key];
  76. }
  77. }
  78. }
  79. return target;
  80. };
  81. return _extends.apply(this, arguments);
  82. }
  83. function _objectWithoutPropertiesLoose(source, excluded) {
  84. if (source == null) return {};
  85. var target = {};
  86. var sourceKeys = Object.keys(source);
  87. var key, i;
  88. for (i = 0; i < sourceKeys.length; i++) {
  89. key = sourceKeys[i];
  90. if (excluded.indexOf(key) >= 0) continue;
  91. target[key] = source[key];
  92. }
  93. return target;
  94. }
  95. function _objectWithoutProperties(source, excluded) {
  96. if (source == null) return {};
  97. var target = _objectWithoutPropertiesLoose(source, excluded);
  98. var key, i;
  99. if (Object.getOwnPropertySymbols) {
  100. var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
  101. for (i = 0; i < sourceSymbolKeys.length; i++) {
  102. key = sourceSymbolKeys[i];
  103. if (excluded.indexOf(key) >= 0) continue;
  104. if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
  105. target[key] = source[key];
  106. }
  107. }
  108. return target;
  109. }
  110. var version = "1.15.0";
  111. function userAgent(pattern) {
  112. if (typeof window !== 'undefined' && window.navigator) {
  113. return !! /*@__PURE__*/navigator.userAgent.match(pattern);
  114. }
  115. }
  116. var IE11OrLess = userAgent(/(?:Trident.*rv[ :]?11\.|msie|iemobile|Windows Phone)/i);
  117. var Edge = userAgent(/Edge/i);
  118. var FireFox = userAgent(/firefox/i);
  119. var Safari = userAgent(/safari/i) && !userAgent(/chrome/i) && !userAgent(/android/i);
  120. var IOS = userAgent(/iP(ad|od|hone)/i);
  121. var ChromeForAndroid = userAgent(/chrome/i) && userAgent(/android/i);
  122. var captureMode = {
  123. capture: false,
  124. passive: false
  125. };
  126. function on(el, event, fn) {
  127. el.addEventListener(event, fn, !IE11OrLess && captureMode);
  128. }
  129. function off(el, event, fn) {
  130. el.removeEventListener(event, fn, !IE11OrLess && captureMode);
  131. }
  132. function matches(
  133. /**HTMLElement*/
  134. el,
  135. /**String*/
  136. selector) {
  137. if (!selector) return;
  138. selector[0] === '>' && (selector = selector.substring(1));
  139. if (el) {
  140. try {
  141. if (el.matches) {
  142. return el.matches(selector);
  143. } else if (el.msMatchesSelector) {
  144. return el.msMatchesSelector(selector);
  145. } else if (el.webkitMatchesSelector) {
  146. return el.webkitMatchesSelector(selector);
  147. }
  148. } catch (_) {
  149. return false;
  150. }
  151. }
  152. return false;
  153. }
  154. function getParentOrHost(el) {
  155. return el.host && el !== document && el.host.nodeType ? el.host : el.parentNode;
  156. }
  157. function closest(
  158. /**HTMLElement*/
  159. el,
  160. /**String*/
  161. selector,
  162. /**HTMLElement*/
  163. ctx, includeCTX) {
  164. if (el) {
  165. ctx = ctx || document;
  166. do {
  167. if (selector != null && (selector[0] === '>' ? el.parentNode === ctx && matches(el, selector) : matches(el, selector)) || includeCTX && el === ctx) {
  168. return el;
  169. }
  170. if (el === ctx) break;
  171. /* jshint boss:true */
  172. } while (el = getParentOrHost(el));
  173. }
  174. return null;
  175. }
  176. var R_SPACE = /\s+/g;
  177. function toggleClass(el, name, state) {
  178. if (el && name) {
  179. if (el.classList) {
  180. el.classList[state ? 'add' : 'remove'](name);
  181. } else {
  182. var className = (' ' + el.className + ' ').replace(R_SPACE, ' ').replace(' ' + name + ' ', ' ');
  183. el.className = (className + (state ? ' ' + name : '')).replace(R_SPACE, ' ');
  184. }
  185. }
  186. }
  187. function css(el, prop, val) {
  188. var style = el && el.style;
  189. if (style) {
  190. if (val === void 0) {
  191. if (document.defaultView && document.defaultView.getComputedStyle) {
  192. val = document.defaultView.getComputedStyle(el, '');
  193. } else if (el.currentStyle) {
  194. val = el.currentStyle;
  195. }
  196. return prop === void 0 ? val : val[prop];
  197. } else {
  198. if (!(prop in style) && prop.indexOf('webkit') === -1) {
  199. prop = '-webkit-' + prop;
  200. }
  201. style[prop] = val + (typeof val === 'string' ? '' : 'px');
  202. }
  203. }
  204. }
  205. function matrix(el, selfOnly) {
  206. var appliedTransforms = '';
  207. if (typeof el === 'string') {
  208. appliedTransforms = el;
  209. } else {
  210. do {
  211. var transform = css(el, 'transform');
  212. if (transform && transform !== 'none') {
  213. appliedTransforms = transform + ' ' + appliedTransforms;
  214. }
  215. /* jshint boss:true */
  216. } while (!selfOnly && (el = el.parentNode));
  217. }
  218. var matrixFn = window.DOMMatrix || window.WebKitCSSMatrix || window.CSSMatrix || window.MSCSSMatrix;
  219. /*jshint -W056 */
  220. return matrixFn && new matrixFn(appliedTransforms);
  221. }
  222. function find(ctx, tagName, iterator) {
  223. if (ctx) {
  224. var list = ctx.getElementsByTagName(tagName),
  225. i = 0,
  226. n = list.length;
  227. if (iterator) {
  228. for (; i < n; i++) {
  229. iterator(list[i], i);
  230. }
  231. }
  232. return list;
  233. }
  234. return [];
  235. }
  236. function getWindowScrollingElement() {
  237. var scrollingElement = document.scrollingElement;
  238. if (scrollingElement) {
  239. return scrollingElement;
  240. } else {
  241. return document.documentElement;
  242. }
  243. }
  244. /**
  245. * Returns the "bounding client rect" of given element
  246. * @param {HTMLElement} el The element whose boundingClientRect is wanted
  247. * @param {[Boolean]} relativeToContainingBlock Whether the rect should be relative to the containing block of (including) the container
  248. * @param {[Boolean]} relativeToNonStaticParent Whether the rect should be relative to the relative parent of (including) the contaienr
  249. * @param {[Boolean]} undoScale Whether the container's scale() should be undone
  250. * @param {[HTMLElement]} container The parent the element will be placed in
  251. * @return {Object} The boundingClientRect of el, with specified adjustments
  252. */
  253. function getRect(el, relativeToContainingBlock, relativeToNonStaticParent, undoScale, container) {
  254. if (!el.getBoundingClientRect && el !== window) return;
  255. var elRect, top, left, bottom, right, height, width;
  256. if (el !== window && el.parentNode && el !== getWindowScrollingElement()) {
  257. elRect = el.getBoundingClientRect();
  258. top = elRect.top;
  259. left = elRect.left;
  260. bottom = elRect.bottom;
  261. right = elRect.right;
  262. height = elRect.height;
  263. width = elRect.width;
  264. } else {
  265. top = 0;
  266. left = 0;
  267. bottom = window.innerHeight;
  268. right = window.innerWidth;
  269. height = window.innerHeight;
  270. width = window.innerWidth;
  271. }
  272. if ((relativeToContainingBlock || relativeToNonStaticParent) && el !== window) {
  273. // Adjust for translate()
  274. container = container || el.parentNode; // solves #1123 (see: https://stackoverflow.com/a/37953806/6088312)
  275. // Not needed on <= IE11
  276. if (!IE11OrLess) {
  277. do {
  278. if (container && container.getBoundingClientRect && (css(container, 'transform') !== 'none' || relativeToNonStaticParent && css(container, 'position') !== 'static')) {
  279. var containerRect = container.getBoundingClientRect(); // Set relative to edges of padding box of container
  280. top -= containerRect.top + parseInt(css(container, 'border-top-width'));
  281. left -= containerRect.left + parseInt(css(container, 'border-left-width'));
  282. bottom = top + elRect.height;
  283. right = left + elRect.width;
  284. break;
  285. }
  286. /* jshint boss:true */
  287. } while (container = container.parentNode);
  288. }
  289. }
  290. if (undoScale && el !== window) {
  291. // Adjust for scale()
  292. var elMatrix = matrix(container || el),
  293. scaleX = elMatrix && elMatrix.a,
  294. scaleY = elMatrix && elMatrix.d;
  295. if (elMatrix) {
  296. top /= scaleY;
  297. left /= scaleX;
  298. width /= scaleX;
  299. height /= scaleY;
  300. bottom = top + height;
  301. right = left + width;
  302. }
  303. }
  304. return {
  305. top: top,
  306. left: left,
  307. bottom: bottom,
  308. right: right,
  309. width: width,
  310. height: height
  311. };
  312. }
  313. /**
  314. * Checks if a side of an element is scrolled past a side of its parents
  315. * @param {HTMLElement} el The element who's side being scrolled out of view is in question
  316. * @param {String} elSide Side of the element in question ('top', 'left', 'right', 'bottom')
  317. * @param {String} parentSide Side of the parent in question ('top', 'left', 'right', 'bottom')
  318. * @return {HTMLElement} The parent scroll element that the el's side is scrolled past, or null if there is no such element
  319. */
  320. function isScrolledPast(el, elSide, parentSide) {
  321. var parent = getParentAutoScrollElement(el, true),
  322. elSideVal = getRect(el)[elSide];
  323. /* jshint boss:true */
  324. while (parent) {
  325. var parentSideVal = getRect(parent)[parentSide],
  326. visible = void 0;
  327. if (parentSide === 'top' || parentSide === 'left') {
  328. visible = elSideVal >= parentSideVal;
  329. } else {
  330. visible = elSideVal <= parentSideVal;
  331. }
  332. if (!visible) return parent;
  333. if (parent === getWindowScrollingElement()) break;
  334. parent = getParentAutoScrollElement(parent, false);
  335. }
  336. return false;
  337. }
  338. /**
  339. * Gets nth child of el, ignoring hidden children, sortable's elements (does not ignore clone if it's visible)
  340. * and non-draggable elements
  341. * @param {HTMLElement} el The parent element
  342. * @param {Number} childNum The index of the child
  343. * @param {Object} options Parent Sortable's options
  344. * @return {HTMLElement} The child at index childNum, or null if not found
  345. */
  346. function getChild(el, childNum, options, includeDragEl) {
  347. var currentChild = 0,
  348. i = 0,
  349. children = el.children;
  350. while (i < children.length) {
  351. if (children[i].style.display !== 'none' && children[i] !== Sortable.ghost && (includeDragEl || children[i] !== Sortable.dragged) && closest(children[i], options.draggable, el, false)) {
  352. if (currentChild === childNum) {
  353. return children[i];
  354. }
  355. currentChild++;
  356. }
  357. i++;
  358. }
  359. return null;
  360. }
  361. /**
  362. * Gets the last child in the el, ignoring ghostEl or invisible elements (clones)
  363. * @param {HTMLElement} el Parent element
  364. * @param {selector} selector Any other elements that should be ignored
  365. * @return {HTMLElement} The last child, ignoring ghostEl
  366. */
  367. function lastChild(el, selector) {
  368. var last = el.lastElementChild;
  369. while (last && (last === Sortable.ghost || css(last, 'display') === 'none' || selector && !matches(last, selector))) {
  370. last = last.previousElementSibling;
  371. }
  372. return last || null;
  373. }
  374. /**
  375. * Returns the index of an element within its parent for a selected set of
  376. * elements
  377. * @param {HTMLElement} el
  378. * @param {selector} selector
  379. * @return {number}
  380. */
  381. function index(el, selector) {
  382. var index = 0;
  383. if (!el || !el.parentNode) {
  384. return -1;
  385. }
  386. /* jshint boss:true */
  387. while (el = el.previousElementSibling) {
  388. if (el.nodeName.toUpperCase() !== 'TEMPLATE' && el !== Sortable.clone && (!selector || matches(el, selector))) {
  389. index++;
  390. }
  391. }
  392. return index;
  393. }
  394. /**
  395. * Returns the scroll offset of the given element, added with all the scroll offsets of parent elements.
  396. * The value is returned in real pixels.
  397. * @param {HTMLElement} el
  398. * @return {Array} Offsets in the format of [left, top]
  399. */
  400. function getRelativeScrollOffset(el) {
  401. var offsetLeft = 0,
  402. offsetTop = 0,
  403. winScroller = getWindowScrollingElement();
  404. if (el) {
  405. do {
  406. var elMatrix = matrix(el),
  407. scaleX = elMatrix.a,
  408. scaleY = elMatrix.d;
  409. offsetLeft += el.scrollLeft * scaleX;
  410. offsetTop += el.scrollTop * scaleY;
  411. } while (el !== winScroller && (el = el.parentNode));
  412. }
  413. return [offsetLeft, offsetTop];
  414. }
  415. /**
  416. * Returns the index of the object within the given array
  417. * @param {Array} arr Array that may or may not hold the object
  418. * @param {Object} obj An object that has a key-value pair unique to and identical to a key-value pair in the object you want to find
  419. * @return {Number} The index of the object in the array, or -1
  420. */
  421. function indexOfObject(arr, obj) {
  422. for (var i in arr) {
  423. if (!arr.hasOwnProperty(i)) continue;
  424. for (var key in obj) {
  425. if (obj.hasOwnProperty(key) && obj[key] === arr[i][key]) return Number(i);
  426. }
  427. }
  428. return -1;
  429. }
  430. function getParentAutoScrollElement(el, includeSelf) {
  431. // skip to window
  432. if (!el || !el.getBoundingClientRect) return getWindowScrollingElement();
  433. var elem = el;
  434. var gotSelf = false;
  435. do {
  436. // we don't need to get elem css if it isn't even overflowing in the first place (performance)
  437. if (elem.clientWidth < elem.scrollWidth || elem.clientHeight < elem.scrollHeight) {
  438. var elemCSS = css(elem);
  439. if (elem.clientWidth < elem.scrollWidth && (elemCSS.overflowX == 'auto' || elemCSS.overflowX == 'scroll') || elem.clientHeight < elem.scrollHeight && (elemCSS.overflowY == 'auto' || elemCSS.overflowY == 'scroll')) {
  440. if (!elem.getBoundingClientRect || elem === document.body) return getWindowScrollingElement();
  441. if (gotSelf || includeSelf) return elem;
  442. gotSelf = true;
  443. }
  444. }
  445. /* jshint boss:true */
  446. } while (elem = elem.parentNode);
  447. return getWindowScrollingElement();
  448. }
  449. function extend(dst, src) {
  450. if (dst && src) {
  451. for (var key in src) {
  452. if (src.hasOwnProperty(key)) {
  453. dst[key] = src[key];
  454. }
  455. }
  456. }
  457. return dst;
  458. }
  459. function isRectEqual(rect1, rect2) {
  460. return Math.round(rect1.top) === Math.round(rect2.top) && Math.round(rect1.left) === Math.round(rect2.left) && Math.round(rect1.height) === Math.round(rect2.height) && Math.round(rect1.width) === Math.round(rect2.width);
  461. }
  462. var _throttleTimeout;
  463. function throttle(callback, ms) {
  464. return function () {
  465. if (!_throttleTimeout) {
  466. var args = arguments,
  467. _this = this;
  468. if (args.length === 1) {
  469. callback.call(_this, args[0]);
  470. } else {
  471. callback.apply(_this, args);
  472. }
  473. _throttleTimeout = setTimeout(function () {
  474. _throttleTimeout = void 0;
  475. }, ms);
  476. }
  477. };
  478. }
  479. function cancelThrottle() {
  480. clearTimeout(_throttleTimeout);
  481. _throttleTimeout = void 0;
  482. }
  483. function scrollBy(el, x, y) {
  484. el.scrollLeft += x;
  485. el.scrollTop += y;
  486. }
  487. function clone(el) {
  488. var Polymer = window.Polymer;
  489. var $ = window.jQuery || window.Zepto;
  490. if (Polymer && Polymer.dom) {
  491. return Polymer.dom(el).cloneNode(true);
  492. } else if ($) {
  493. return $(el).clone(true)[0];
  494. } else {
  495. return el.cloneNode(true);
  496. }
  497. }
  498. var expando = 'Sortable' + new Date().getTime();
  499. function AnimationStateManager() {
  500. var animationStates = [],
  501. animationCallbackId;
  502. return {
  503. captureAnimationState: function captureAnimationState() {
  504. animationStates = [];
  505. if (!this.options.animation) return;
  506. var children = [].slice.call(this.el.children);
  507. children.forEach(function (child) {
  508. if (css(child, 'display') === 'none' || child === Sortable.ghost) return;
  509. animationStates.push({
  510. target: child,
  511. rect: getRect(child)
  512. });
  513. var fromRect = _objectSpread2({}, animationStates[animationStates.length - 1].rect); // If animating: compensate for current animation
  514. if (child.thisAnimationDuration) {
  515. var childMatrix = matrix(child, true);
  516. if (childMatrix) {
  517. fromRect.top -= childMatrix.f;
  518. fromRect.left -= childMatrix.e;
  519. }
  520. }
  521. child.fromRect = fromRect;
  522. });
  523. },
  524. addAnimationState: function addAnimationState(state) {
  525. animationStates.push(state);
  526. },
  527. removeAnimationState: function removeAnimationState(target) {
  528. animationStates.splice(indexOfObject(animationStates, {
  529. target: target
  530. }), 1);
  531. },
  532. animateAll: function animateAll(callback) {
  533. var _this = this;
  534. if (!this.options.animation) {
  535. clearTimeout(animationCallbackId);
  536. if (typeof callback === 'function') callback();
  537. return;
  538. }
  539. var animating = false,
  540. animationTime = 0;
  541. animationStates.forEach(function (state) {
  542. var time = 0,
  543. target = state.target,
  544. fromRect = target.fromRect,
  545. toRect = getRect(target),
  546. prevFromRect = target.prevFromRect,
  547. prevToRect = target.prevToRect,
  548. animatingRect = state.rect,
  549. targetMatrix = matrix(target, true);
  550. if (targetMatrix) {
  551. // Compensate for current animation
  552. toRect.top -= targetMatrix.f;
  553. toRect.left -= targetMatrix.e;
  554. }
  555. target.toRect = toRect;
  556. if (target.thisAnimationDuration) {
  557. // Could also check if animatingRect is between fromRect and toRect
  558. if (isRectEqual(prevFromRect, toRect) && !isRectEqual(fromRect, toRect) && // Make sure animatingRect is on line between toRect & fromRect
  559. (animatingRect.top - toRect.top) / (animatingRect.left - toRect.left) === (fromRect.top - toRect.top) / (fromRect.left - toRect.left)) {
  560. // If returning to same place as started from animation and on same axis
  561. time = calculateRealTime(animatingRect, prevFromRect, prevToRect, _this.options);
  562. }
  563. } // if fromRect != toRect: animate
  564. if (!isRectEqual(toRect, fromRect)) {
  565. target.prevFromRect = fromRect;
  566. target.prevToRect = toRect;
  567. if (!time) {
  568. time = _this.options.animation;
  569. }
  570. _this.animate(target, animatingRect, toRect, time);
  571. }
  572. if (time) {
  573. animating = true;
  574. animationTime = Math.max(animationTime, time);
  575. clearTimeout(target.animationResetTimer);
  576. target.animationResetTimer = setTimeout(function () {
  577. target.animationTime = 0;
  578. target.prevFromRect = null;
  579. target.fromRect = null;
  580. target.prevToRect = null;
  581. target.thisAnimationDuration = null;
  582. }, time);
  583. target.thisAnimationDuration = time;
  584. }
  585. });
  586. clearTimeout(animationCallbackId);
  587. if (!animating) {
  588. if (typeof callback === 'function') callback();
  589. } else {
  590. animationCallbackId = setTimeout(function () {
  591. if (typeof callback === 'function') callback();
  592. }, animationTime);
  593. }
  594. animationStates = [];
  595. },
  596. animate: function animate(target, currentRect, toRect, duration) {
  597. if (duration) {
  598. css(target, 'transition', '');
  599. css(target, 'transform', '');
  600. var elMatrix = matrix(this.el),
  601. scaleX = elMatrix && elMatrix.a,
  602. scaleY = elMatrix && elMatrix.d,
  603. translateX = (currentRect.left - toRect.left) / (scaleX || 1),
  604. translateY = (currentRect.top - toRect.top) / (scaleY || 1);
  605. target.animatingX = !!translateX;
  606. target.animatingY = !!translateY;
  607. css(target, 'transform', 'translate3d(' + translateX + 'px,' + translateY + 'px,0)');
  608. this.forRepaintDummy = repaint(target); // repaint
  609. css(target, 'transition', 'transform ' + duration + 'ms' + (this.options.easing ? ' ' + this.options.easing : ''));
  610. css(target, 'transform', 'translate3d(0,0,0)');
  611. typeof target.animated === 'number' && clearTimeout(target.animated);
  612. target.animated = setTimeout(function () {
  613. css(target, 'transition', '');
  614. css(target, 'transform', '');
  615. target.animated = false;
  616. target.animatingX = false;
  617. target.animatingY = false;
  618. }, duration);
  619. }
  620. }
  621. };
  622. }
  623. function repaint(target) {
  624. return target.offsetWidth;
  625. }
  626. function calculateRealTime(animatingRect, fromRect, toRect, options) {
  627. return Math.sqrt(Math.pow(fromRect.top - animatingRect.top, 2) + Math.pow(fromRect.left - animatingRect.left, 2)) / Math.sqrt(Math.pow(fromRect.top - toRect.top, 2) + Math.pow(fromRect.left - toRect.left, 2)) * options.animation;
  628. }
  629. var plugins = [];
  630. var defaults = {
  631. initializeByDefault: true
  632. };
  633. var PluginManager = {
  634. mount: function mount(plugin) {
  635. // Set default static properties
  636. for (var option in defaults) {
  637. if (defaults.hasOwnProperty(option) && !(option in plugin)) {
  638. plugin[option] = defaults[option];
  639. }
  640. }
  641. plugins.forEach(function (p) {
  642. if (p.pluginName === plugin.pluginName) {
  643. throw "Sortable: Cannot mount plugin ".concat(plugin.pluginName, " more than once");
  644. }
  645. });
  646. plugins.push(plugin);
  647. },
  648. pluginEvent: function pluginEvent(eventName, sortable, evt) {
  649. var _this = this;
  650. this.eventCanceled = false;
  651. evt.cancel = function () {
  652. _this.eventCanceled = true;
  653. };
  654. var eventNameGlobal = eventName + 'Global';
  655. plugins.forEach(function (plugin) {
  656. if (!sortable[plugin.pluginName]) return; // Fire global events if it exists in this sortable
  657. if (sortable[plugin.pluginName][eventNameGlobal]) {
  658. sortable[plugin.pluginName][eventNameGlobal](_objectSpread2({
  659. sortable: sortable
  660. }, evt));
  661. } // Only fire plugin event if plugin is enabled in this sortable,
  662. // and plugin has event defined
  663. if (sortable.options[plugin.pluginName] && sortable[plugin.pluginName][eventName]) {
  664. sortable[plugin.pluginName][eventName](_objectSpread2({
  665. sortable: sortable
  666. }, evt));
  667. }
  668. });
  669. },
  670. initializePlugins: function initializePlugins(sortable, el, defaults, options) {
  671. plugins.forEach(function (plugin) {
  672. var pluginName = plugin.pluginName;
  673. if (!sortable.options[pluginName] && !plugin.initializeByDefault) return;
  674. var initialized = new plugin(sortable, el, sortable.options);
  675. initialized.sortable = sortable;
  676. initialized.options = sortable.options;
  677. sortable[pluginName] = initialized; // Add default options from plugin
  678. _extends(defaults, initialized.defaults);
  679. });
  680. for (var option in sortable.options) {
  681. if (!sortable.options.hasOwnProperty(option)) continue;
  682. var modified = this.modifyOption(sortable, option, sortable.options[option]);
  683. if (typeof modified !== 'undefined') {
  684. sortable.options[option] = modified;
  685. }
  686. }
  687. },
  688. getEventProperties: function getEventProperties(name, sortable) {
  689. var eventProperties = {};
  690. plugins.forEach(function (plugin) {
  691. if (typeof plugin.eventProperties !== 'function') return;
  692. _extends(eventProperties, plugin.eventProperties.call(sortable[plugin.pluginName], name));
  693. });
  694. return eventProperties;
  695. },
  696. modifyOption: function modifyOption(sortable, name, value) {
  697. var modifiedValue;
  698. plugins.forEach(function (plugin) {
  699. // Plugin must exist on the Sortable
  700. if (!sortable[plugin.pluginName]) return; // If static option listener exists for this option, call in the context of the Sortable's instance of this plugin
  701. if (plugin.optionListeners && typeof plugin.optionListeners[name] === 'function') {
  702. modifiedValue = plugin.optionListeners[name].call(sortable[plugin.pluginName], value);
  703. }
  704. });
  705. return modifiedValue;
  706. }
  707. };
  708. function dispatchEvent(_ref) {
  709. var sortable = _ref.sortable,
  710. rootEl = _ref.rootEl,
  711. name = _ref.name,
  712. targetEl = _ref.targetEl,
  713. cloneEl = _ref.cloneEl,
  714. toEl = _ref.toEl,
  715. fromEl = _ref.fromEl,
  716. oldIndex = _ref.oldIndex,
  717. newIndex = _ref.newIndex,
  718. oldDraggableIndex = _ref.oldDraggableIndex,
  719. newDraggableIndex = _ref.newDraggableIndex,
  720. originalEvent = _ref.originalEvent,
  721. putSortable = _ref.putSortable,
  722. extraEventProperties = _ref.extraEventProperties;
  723. sortable = sortable || rootEl && rootEl[expando];
  724. if (!sortable) return;
  725. var evt,
  726. options = sortable.options,
  727. onName = 'on' + name.charAt(0).toUpperCase() + name.substr(1); // Support for new CustomEvent feature
  728. if (window.CustomEvent && !IE11OrLess && !Edge) {
  729. evt = new CustomEvent(name, {
  730. bubbles: true,
  731. cancelable: true
  732. });
  733. } else {
  734. evt = document.createEvent('Event');
  735. evt.initEvent(name, true, true);
  736. }
  737. evt.to = toEl || rootEl;
  738. evt.from = fromEl || rootEl;
  739. evt.item = targetEl || rootEl;
  740. evt.clone = cloneEl;
  741. evt.oldIndex = oldIndex;
  742. evt.newIndex = newIndex;
  743. evt.oldDraggableIndex = oldDraggableIndex;
  744. evt.newDraggableIndex = newDraggableIndex;
  745. evt.originalEvent = originalEvent;
  746. evt.pullMode = putSortable ? putSortable.lastPutMode : undefined;
  747. var allEventProperties = _objectSpread2(_objectSpread2({}, extraEventProperties), PluginManager.getEventProperties(name, sortable));
  748. for (var option in allEventProperties) {
  749. evt[option] = allEventProperties[option];
  750. }
  751. if (rootEl) {
  752. rootEl.dispatchEvent(evt);
  753. }
  754. if (options[onName]) {
  755. options[onName].call(sortable, evt);
  756. }
  757. }
  758. var _excluded = ["evt"];
  759. var pluginEvent = function pluginEvent(eventName, sortable) {
  760. var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
  761. originalEvent = _ref.evt,
  762. data = _objectWithoutProperties(_ref, _excluded);
  763. PluginManager.pluginEvent.bind(Sortable)(eventName, sortable, _objectSpread2({
  764. dragEl: dragEl,
  765. parentEl: parentEl,
  766. ghostEl: ghostEl,
  767. rootEl: rootEl,
  768. nextEl: nextEl,
  769. lastDownEl: lastDownEl,
  770. cloneEl: cloneEl,
  771. cloneHidden: cloneHidden,
  772. dragStarted: moved,
  773. putSortable: putSortable,
  774. activeSortable: Sortable.active,
  775. originalEvent: originalEvent,
  776. oldIndex: oldIndex,
  777. oldDraggableIndex: oldDraggableIndex,
  778. newIndex: newIndex,
  779. newDraggableIndex: newDraggableIndex,
  780. hideGhostForTarget: _hideGhostForTarget,
  781. unhideGhostForTarget: _unhideGhostForTarget,
  782. cloneNowHidden: function cloneNowHidden() {
  783. cloneHidden = true;
  784. },
  785. cloneNowShown: function cloneNowShown() {
  786. cloneHidden = false;
  787. },
  788. dispatchSortableEvent: function dispatchSortableEvent(name) {
  789. _dispatchEvent({
  790. sortable: sortable,
  791. name: name,
  792. originalEvent: originalEvent
  793. });
  794. }
  795. }, data));
  796. };
  797. function _dispatchEvent(info) {
  798. dispatchEvent(_objectSpread2({
  799. putSortable: putSortable,
  800. cloneEl: cloneEl,
  801. targetEl: dragEl,
  802. rootEl: rootEl,
  803. oldIndex: oldIndex,
  804. oldDraggableIndex: oldDraggableIndex,
  805. newIndex: newIndex,
  806. newDraggableIndex: newDraggableIndex
  807. }, info));
  808. }
  809. var dragEl,
  810. parentEl,
  811. ghostEl,
  812. rootEl,
  813. nextEl,
  814. lastDownEl,
  815. cloneEl,
  816. cloneHidden,
  817. oldIndex,
  818. newIndex,
  819. oldDraggableIndex,
  820. newDraggableIndex,
  821. activeGroup,
  822. putSortable,
  823. awaitingDragStarted = false,
  824. ignoreNextClick = false,
  825. sortables = [],
  826. tapEvt,
  827. touchEvt,
  828. lastDx,
  829. lastDy,
  830. tapDistanceLeft,
  831. tapDistanceTop,
  832. moved,
  833. lastTarget,
  834. lastDirection,
  835. pastFirstInvertThresh = false,
  836. isCircumstantialInvert = false,
  837. targetMoveDistance,
  838. // For positioning ghost absolutely
  839. ghostRelativeParent,
  840. ghostRelativeParentInitialScroll = [],
  841. // (left, top)
  842. _silent = false,
  843. savedInputChecked = [];
  844. /** @const */
  845. var documentExists = typeof document !== 'undefined',
  846. PositionGhostAbsolutely = IOS,
  847. CSSFloatProperty = Edge || IE11OrLess ? 'cssFloat' : 'float',
  848. // This will not pass for IE9, because IE9 DnD only works on anchors
  849. supportDraggable = documentExists && !ChromeForAndroid && !IOS && 'draggable' in document.createElement('div'),
  850. supportCssPointerEvents = function () {
  851. if (!documentExists) return; // false when <= IE11
  852. if (IE11OrLess) {
  853. return false;
  854. }
  855. var el = document.createElement('x');
  856. el.style.cssText = 'pointer-events:auto';
  857. return el.style.pointerEvents === 'auto';
  858. }(),
  859. _detectDirection = function _detectDirection(el, options) {
  860. var elCSS = css(el),
  861. elWidth = parseInt(elCSS.width) - parseInt(elCSS.paddingLeft) - parseInt(elCSS.paddingRight) - parseInt(elCSS.borderLeftWidth) - parseInt(elCSS.borderRightWidth),
  862. child1 = getChild(el, 0, options),
  863. child2 = getChild(el, 1, options),
  864. firstChildCSS = child1 && css(child1),
  865. secondChildCSS = child2 && css(child2),
  866. firstChildWidth = firstChildCSS && parseInt(firstChildCSS.marginLeft) + parseInt(firstChildCSS.marginRight) + getRect(child1).width,
  867. secondChildWidth = secondChildCSS && parseInt(secondChildCSS.marginLeft) + parseInt(secondChildCSS.marginRight) + getRect(child2).width;
  868. if (elCSS.display === 'flex') {
  869. return elCSS.flexDirection === 'column' || elCSS.flexDirection === 'column-reverse' ? 'vertical' : 'horizontal';
  870. }
  871. if (elCSS.display === 'grid') {
  872. return elCSS.gridTemplateColumns.split(' ').length <= 1 ? 'vertical' : 'horizontal';
  873. }
  874. if (child1 && firstChildCSS["float"] && firstChildCSS["float"] !== 'none') {
  875. var touchingSideChild2 = firstChildCSS["float"] === 'left' ? 'left' : 'right';
  876. return child2 && (secondChildCSS.clear === 'both' || secondChildCSS.clear === touchingSideChild2) ? 'vertical' : 'horizontal';
  877. }
  878. return child1 && (firstChildCSS.display === 'block' || firstChildCSS.display === 'flex' || firstChildCSS.display === 'table' || firstChildCSS.display === 'grid' || firstChildWidth >= elWidth && elCSS[CSSFloatProperty] === 'none' || child2 && elCSS[CSSFloatProperty] === 'none' && firstChildWidth + secondChildWidth > elWidth) ? 'vertical' : 'horizontal';
  879. },
  880. _dragElInRowColumn = function _dragElInRowColumn(dragRect, targetRect, vertical) {
  881. var dragElS1Opp = vertical ? dragRect.left : dragRect.top,
  882. dragElS2Opp = vertical ? dragRect.right : dragRect.bottom,
  883. dragElOppLength = vertical ? dragRect.width : dragRect.height,
  884. targetS1Opp = vertical ? targetRect.left : targetRect.top,
  885. targetS2Opp = vertical ? targetRect.right : targetRect.bottom,
  886. targetOppLength = vertical ? targetRect.width : targetRect.height;
  887. return dragElS1Opp === targetS1Opp || dragElS2Opp === targetS2Opp || dragElS1Opp + dragElOppLength / 2 === targetS1Opp + targetOppLength / 2;
  888. },
  889. /**
  890. * Detects first nearest empty sortable to X and Y position using emptyInsertThreshold.
  891. * @param {Number} x X position
  892. * @param {Number} y Y position
  893. * @return {HTMLElement} Element of the first found nearest Sortable
  894. */
  895. _detectNearestEmptySortable = function _detectNearestEmptySortable(x, y) {
  896. var ret;
  897. sortables.some(function (sortable) {
  898. var threshold = sortable[expando].options.emptyInsertThreshold;
  899. if (!threshold || lastChild(sortable)) return;
  900. var rect = getRect(sortable),
  901. insideHorizontally = x >= rect.left - threshold && x <= rect.right + threshold,
  902. insideVertically = y >= rect.top - threshold && y <= rect.bottom + threshold;
  903. if (insideHorizontally && insideVertically) {
  904. return ret = sortable;
  905. }
  906. });
  907. return ret;
  908. },
  909. _prepareGroup = function _prepareGroup(options) {
  910. function toFn(value, pull) {
  911. return function (to, from, dragEl, evt) {
  912. var sameGroup = to.options.group.name && from.options.group.name && to.options.group.name === from.options.group.name;
  913. if (value == null && (pull || sameGroup)) {
  914. // Default pull value
  915. // Default pull and put value if same group
  916. return true;
  917. } else if (value == null || value === false) {
  918. return false;
  919. } else if (pull && value === 'clone') {
  920. return value;
  921. } else if (typeof value === 'function') {
  922. return toFn(value(to, from, dragEl, evt), pull)(to, from, dragEl, evt);
  923. } else {
  924. var otherGroup = (pull ? to : from).options.group.name;
  925. return value === true || typeof value === 'string' && value === otherGroup || value.join && value.indexOf(otherGroup) > -1;
  926. }
  927. };
  928. }
  929. var group = {};
  930. var originalGroup = options.group;
  931. if (!originalGroup || _typeof(originalGroup) != 'object') {
  932. originalGroup = {
  933. name: originalGroup
  934. };
  935. }
  936. group.name = originalGroup.name;
  937. group.checkPull = toFn(originalGroup.pull, true);
  938. group.checkPut = toFn(originalGroup.put);
  939. group.revertClone = originalGroup.revertClone;
  940. options.group = group;
  941. },
  942. _hideGhostForTarget = function _hideGhostForTarget() {
  943. if (!supportCssPointerEvents && ghostEl) {
  944. css(ghostEl, 'display', 'none');
  945. }
  946. },
  947. _unhideGhostForTarget = function _unhideGhostForTarget() {
  948. if (!supportCssPointerEvents && ghostEl) {
  949. css(ghostEl, 'display', '');
  950. }
  951. }; // #1184 fix - Prevent click event on fallback if dragged but item not changed position
  952. if (documentExists && !ChromeForAndroid) {
  953. document.addEventListener('click', function (evt) {
  954. if (ignoreNextClick) {
  955. evt.preventDefault();
  956. evt.stopPropagation && evt.stopPropagation();
  957. evt.stopImmediatePropagation && evt.stopImmediatePropagation();
  958. ignoreNextClick = false;
  959. return false;
  960. }
  961. }, true);
  962. }
  963. var nearestEmptyInsertDetectEvent = function nearestEmptyInsertDetectEvent(evt) {
  964. if (dragEl) {
  965. evt = evt.touches ? evt.touches[0] : evt;
  966. var nearest = _detectNearestEmptySortable(evt.clientX, evt.clientY);
  967. if (nearest) {
  968. // Create imitation event
  969. var event = {};
  970. for (var i in evt) {
  971. if (evt.hasOwnProperty(i)) {
  972. event[i] = evt[i];
  973. }
  974. }
  975. event.target = event.rootEl = nearest;
  976. event.preventDefault = void 0;
  977. event.stopPropagation = void 0;
  978. nearest[expando]._onDragOver(event);
  979. }
  980. }
  981. };
  982. var _checkOutsideTargetEl = function _checkOutsideTargetEl(evt) {
  983. if (dragEl) {
  984. dragEl.parentNode[expando]._isOutsideThisEl(evt.target);
  985. }
  986. };
  987. /**
  988. * @class Sortable
  989. * @param {HTMLElement} el
  990. * @param {Object} [options]
  991. */
  992. function Sortable(el, options) {
  993. if (!(el && el.nodeType && el.nodeType === 1)) {
  994. throw "Sortable: `el` must be an HTMLElement, not ".concat({}.toString.call(el));
  995. }
  996. this.el = el; // root element
  997. this.options = options = _extends({}, options); // Export instance
  998. el[expando] = this;
  999. var defaults = {
  1000. group: null,
  1001. sort: true,
  1002. disabled: false,
  1003. store: null,
  1004. handle: null,
  1005. draggable: /^[uo]l$/i.test(el.nodeName) ? '>li' : '>*',
  1006. swapThreshold: 1,
  1007. // percentage; 0 <= x <= 1
  1008. invertSwap: false,
  1009. // invert always
  1010. invertedSwapThreshold: null,
  1011. // will be set to same as swapThreshold if default
  1012. removeCloneOnHide: true,
  1013. direction: function direction() {
  1014. return _detectDirection(el, this.options);
  1015. },
  1016. ghostClass: 'sortable-ghost',
  1017. chosenClass: 'sortable-chosen',
  1018. dragClass: 'sortable-drag',
  1019. ignore: 'a, img',
  1020. filter: null,
  1021. preventOnFilter: true,
  1022. animation: 0,
  1023. easing: null,
  1024. setData: function setData(dataTransfer, dragEl) {
  1025. dataTransfer.setData('Text', dragEl.textContent);
  1026. },
  1027. dropBubble: false,
  1028. dragoverBubble: false,
  1029. dataIdAttr: 'data-id',
  1030. delay: 0,
  1031. delayOnTouchOnly: false,
  1032. touchStartThreshold: (Number.parseInt ? Number : window).parseInt(window.devicePixelRatio, 10) || 1,
  1033. forceFallback: false,
  1034. fallbackClass: 'sortable-fallback',
  1035. fallbackOnBody: false,
  1036. fallbackTolerance: 0,
  1037. fallbackOffset: {
  1038. x: 0,
  1039. y: 0
  1040. },
  1041. supportPointer: Sortable.supportPointer !== false && 'PointerEvent' in window && !Safari,
  1042. emptyInsertThreshold: 5
  1043. };
  1044. PluginManager.initializePlugins(this, el, defaults); // Set default options
  1045. for (var name in defaults) {
  1046. !(name in options) && (options[name] = defaults[name]);
  1047. }
  1048. _prepareGroup(options); // Bind all private methods
  1049. for (var fn in this) {
  1050. if (fn.charAt(0) === '_' && typeof this[fn] === 'function') {
  1051. this[fn] = this[fn].bind(this);
  1052. }
  1053. } // Setup drag mode
  1054. this.nativeDraggable = options.forceFallback ? false : supportDraggable;
  1055. if (this.nativeDraggable) {
  1056. // Touch start threshold cannot be greater than the native dragstart threshold
  1057. this.options.touchStartThreshold = 1;
  1058. } // Bind events
  1059. if (options.supportPointer) {
  1060. on(el, 'pointerdown', this._onTapStart);
  1061. } else {
  1062. on(el, 'mousedown', this._onTapStart);
  1063. on(el, 'touchstart', this._onTapStart);
  1064. }
  1065. if (this.nativeDraggable) {
  1066. on(el, 'dragover', this);
  1067. on(el, 'dragenter', this);
  1068. }
  1069. sortables.push(this.el); // Restore sorting
  1070. options.store && options.store.get && this.sort(options.store.get(this) || []); // Add animation state manager
  1071. _extends(this, AnimationStateManager());
  1072. }
  1073. Sortable.prototype =
  1074. /** @lends Sortable.prototype */
  1075. {
  1076. constructor: Sortable,
  1077. _isOutsideThisEl: function _isOutsideThisEl(target) {
  1078. if (!this.el.contains(target) && target !== this.el) {
  1079. lastTarget = null;
  1080. }
  1081. },
  1082. _getDirection: function _getDirection(evt, target) {
  1083. return typeof this.options.direction === 'function' ? this.options.direction.call(this, evt, target, dragEl) : this.options.direction;
  1084. },
  1085. _onTapStart: function _onTapStart(
  1086. /** Event|TouchEvent */
  1087. evt) {
  1088. if (!evt.cancelable) return;
  1089. var _this = this,
  1090. el = this.el,
  1091. options = this.options,
  1092. preventOnFilter = options.preventOnFilter,
  1093. type = evt.type,
  1094. touch = evt.touches && evt.touches[0] || evt.pointerType && evt.pointerType === 'touch' && evt,
  1095. target = (touch || evt).target,
  1096. originalTarget = evt.target.shadowRoot && (evt.path && evt.path[0] || evt.composedPath && evt.composedPath()[0]) || target,
  1097. filter = options.filter;
  1098. _saveInputCheckedState(el); // Don't trigger start event when an element is been dragged, otherwise the evt.oldindex always wrong when set option.group.
  1099. if (dragEl) {
  1100. return;
  1101. }
  1102. if (/mousedown|pointerdown/.test(type) && evt.button !== 0 || options.disabled) {
  1103. return; // only left button and enabled
  1104. } // cancel dnd if original target is content editable
  1105. if (originalTarget.isContentEditable) {
  1106. return;
  1107. } // Safari ignores further event handling after mousedown
  1108. if (!this.nativeDraggable && Safari && target && target.tagName.toUpperCase() === 'SELECT') {
  1109. return;
  1110. }
  1111. target = closest(target, options.draggable, el, false);
  1112. if (target && target.animated) {
  1113. return;
  1114. }
  1115. if (lastDownEl === target) {
  1116. // Ignoring duplicate `down`
  1117. return;
  1118. } // Get the index of the dragged element within its parent
  1119. oldIndex = index(target);
  1120. oldDraggableIndex = index(target, options.draggable); // Check filter
  1121. if (typeof filter === 'function') {
  1122. if (filter.call(this, evt, target, this)) {
  1123. _dispatchEvent({
  1124. sortable: _this,
  1125. rootEl: originalTarget,
  1126. name: 'filter',
  1127. targetEl: target,
  1128. toEl: el,
  1129. fromEl: el
  1130. });
  1131. pluginEvent('filter', _this, {
  1132. evt: evt
  1133. });
  1134. preventOnFilter && evt.cancelable && evt.preventDefault();
  1135. return; // cancel dnd
  1136. }
  1137. } else if (filter) {
  1138. filter = filter.split(',').some(function (criteria) {
  1139. criteria = closest(originalTarget, criteria.trim(), el, false);
  1140. if (criteria) {
  1141. _dispatchEvent({
  1142. sortable: _this,
  1143. rootEl: criteria,
  1144. name: 'filter',
  1145. targetEl: target,
  1146. fromEl: el,
  1147. toEl: el
  1148. });
  1149. pluginEvent('filter', _this, {
  1150. evt: evt
  1151. });
  1152. return true;
  1153. }
  1154. });
  1155. if (filter) {
  1156. preventOnFilter && evt.cancelable && evt.preventDefault();
  1157. return; // cancel dnd
  1158. }
  1159. }
  1160. if (options.handle && !closest(originalTarget, options.handle, el, false)) {
  1161. return;
  1162. } // Prepare `dragstart`
  1163. this._prepareDragStart(evt, touch, target);
  1164. },
  1165. _prepareDragStart: function _prepareDragStart(
  1166. /** Event */
  1167. evt,
  1168. /** Touch */
  1169. touch,
  1170. /** HTMLElement */
  1171. target) {
  1172. var _this = this,
  1173. el = _this.el,
  1174. options = _this.options,
  1175. ownerDocument = el.ownerDocument,
  1176. dragStartFn;
  1177. if (target && !dragEl && target.parentNode === el) {
  1178. var dragRect = getRect(target);
  1179. rootEl = el;
  1180. dragEl = target;
  1181. parentEl = dragEl.parentNode;
  1182. nextEl = dragEl.nextSibling;
  1183. lastDownEl = target;
  1184. activeGroup = options.group;
  1185. Sortable.dragged = dragEl;
  1186. tapEvt = {
  1187. target: dragEl,
  1188. clientX: (touch || evt).clientX,
  1189. clientY: (touch || evt).clientY
  1190. };
  1191. tapDistanceLeft = tapEvt.clientX - dragRect.left;
  1192. tapDistanceTop = tapEvt.clientY - dragRect.top;
  1193. this._lastX = (touch || evt).clientX;
  1194. this._lastY = (touch || evt).clientY;
  1195. dragEl.style['will-change'] = 'all';
  1196. dragStartFn = function dragStartFn() {
  1197. pluginEvent('delayEnded', _this, {
  1198. evt: evt
  1199. });
  1200. if (Sortable.eventCanceled) {
  1201. _this._onDrop();
  1202. return;
  1203. } // Delayed drag has been triggered
  1204. // we can re-enable the events: touchmove/mousemove
  1205. _this._disableDelayedDragEvents();
  1206. if (!FireFox && _this.nativeDraggable) {
  1207. dragEl.draggable = true;
  1208. } // Bind the events: dragstart/dragend
  1209. _this._triggerDragStart(evt, touch); // Drag start event
  1210. _dispatchEvent({
  1211. sortable: _this,
  1212. name: 'choose',
  1213. originalEvent: evt
  1214. }); // Chosen item
  1215. toggleClass(dragEl, options.chosenClass, true);
  1216. }; // Disable "draggable"
  1217. options.ignore.split(',').forEach(function (criteria) {
  1218. find(dragEl, criteria.trim(), _disableDraggable);
  1219. });
  1220. on(ownerDocument, 'dragover', nearestEmptyInsertDetectEvent);
  1221. on(ownerDocument, 'mousemove', nearestEmptyInsertDetectEvent);
  1222. on(ownerDocument, 'touchmove', nearestEmptyInsertDetectEvent);
  1223. on(ownerDocument, 'mouseup', _this._onDrop);
  1224. on(ownerDocument, 'touchend', _this._onDrop);
  1225. on(ownerDocument, 'touchcancel', _this._onDrop); // Make dragEl draggable (must be before delay for FireFox)
  1226. if (FireFox && this.nativeDraggable) {
  1227. this.options.touchStartThreshold = 4;
  1228. dragEl.draggable = true;
  1229. }
  1230. pluginEvent('delayStart', this, {
  1231. evt: evt
  1232. }); // Delay is impossible for native DnD in Edge or IE
  1233. if (options.delay && (!options.delayOnTouchOnly || touch) && (!this.nativeDraggable || !(Edge || IE11OrLess))) {
  1234. if (Sortable.eventCanceled) {
  1235. this._onDrop();
  1236. return;
  1237. } // If the user moves the pointer or let go the click or touch
  1238. // before the delay has been reached:
  1239. // disable the delayed drag
  1240. on(ownerDocument, 'mouseup', _this._disableDelayedDrag);
  1241. on(ownerDocument, 'touchend', _this._disableDelayedDrag);
  1242. on(ownerDocument, 'touchcancel', _this._disableDelayedDrag);
  1243. on(ownerDocument, 'mousemove', _this._delayedDragTouchMoveHandler);
  1244. on(ownerDocument, 'touchmove', _this._delayedDragTouchMoveHandler);
  1245. options.supportPointer && on(ownerDocument, 'pointermove', _this._delayedDragTouchMoveHandler);
  1246. _this._dragStartTimer = setTimeout(dragStartFn, options.delay);
  1247. } else {
  1248. dragStartFn();
  1249. }
  1250. }
  1251. },
  1252. _delayedDragTouchMoveHandler: function _delayedDragTouchMoveHandler(
  1253. /** TouchEvent|PointerEvent **/
  1254. e) {
  1255. var touch = e.touches ? e.touches[0] : e;
  1256. if (Math.max(Math.abs(touch.clientX - this._lastX), Math.abs(touch.clientY - this._lastY)) >= Math.floor(this.options.touchStartThreshold / (this.nativeDraggable && window.devicePixelRatio || 1))) {
  1257. this._disableDelayedDrag();
  1258. }
  1259. },
  1260. _disableDelayedDrag: function _disableDelayedDrag() {
  1261. dragEl && _disableDraggable(dragEl);
  1262. clearTimeout(this._dragStartTimer);
  1263. this._disableDelayedDragEvents();
  1264. },
  1265. _disableDelayedDragEvents: function _disableDelayedDragEvents() {
  1266. var ownerDocument = this.el.ownerDocument;
  1267. off(ownerDocument, 'mouseup', this._disableDelayedDrag);
  1268. off(ownerDocument, 'touchend', this._disableDelayedDrag);
  1269. off(ownerDocument, 'touchcancel', this._disableDelayedDrag);
  1270. off(ownerDocument, 'mousemove', this._delayedDragTouchMoveHandler);
  1271. off(ownerDocument, 'touchmove', this._delayedDragTouchMoveHandler);
  1272. off(ownerDocument, 'pointermove', this._delayedDragTouchMoveHandler);
  1273. },
  1274. _triggerDragStart: function _triggerDragStart(
  1275. /** Event */
  1276. evt,
  1277. /** Touch */
  1278. touch) {
  1279. touch = touch || evt.pointerType == 'touch' && evt;
  1280. if (!this.nativeDraggable || touch) {
  1281. if (this.options.supportPointer) {
  1282. on(document, 'pointermove', this._onTouchMove);
  1283. } else if (touch) {
  1284. on(document, 'touchmove', this._onTouchMove);
  1285. } else {
  1286. on(document, 'mousemove', this._onTouchMove);
  1287. }
  1288. } else {
  1289. on(dragEl, 'dragend', this);
  1290. on(rootEl, 'dragstart', this._onDragStart);
  1291. }
  1292. try {
  1293. if (document.selection) {
  1294. // Timeout neccessary for IE9
  1295. _nextTick(function () {
  1296. document.selection.empty();
  1297. });
  1298. } else {
  1299. window.getSelection().removeAllRanges();
  1300. }
  1301. } catch (err) {}
  1302. },
  1303. _dragStarted: function _dragStarted(fallback, evt) {
  1304. awaitingDragStarted = false;
  1305. if (rootEl && dragEl) {
  1306. pluginEvent('dragStarted', this, {
  1307. evt: evt
  1308. });
  1309. if (this.nativeDraggable) {
  1310. on(document, 'dragover', _checkOutsideTargetEl);
  1311. }
  1312. var options = this.options; // Apply effect
  1313. !fallback && toggleClass(dragEl, options.dragClass, false);
  1314. toggleClass(dragEl, options.ghostClass, true);
  1315. Sortable.active = this;
  1316. fallback && this._appendGhost(); // Drag start event
  1317. _dispatchEvent({
  1318. sortable: this,
  1319. name: 'start',
  1320. originalEvent: evt
  1321. });
  1322. } else {
  1323. this._nulling();
  1324. }
  1325. },
  1326. _emulateDragOver: function _emulateDragOver() {
  1327. if (touchEvt) {
  1328. this._lastX = touchEvt.clientX;
  1329. this._lastY = touchEvt.clientY;
  1330. _hideGhostForTarget();
  1331. var target = document.elementFromPoint(touchEvt.clientX, touchEvt.clientY);
  1332. var parent = target;
  1333. while (target && target.shadowRoot) {
  1334. target = target.shadowRoot.elementFromPoint(touchEvt.clientX, touchEvt.clientY);
  1335. if (target === parent) break;
  1336. parent = target;
  1337. }
  1338. dragEl.parentNode[expando]._isOutsideThisEl(target);
  1339. if (parent) {
  1340. do {
  1341. if (parent[expando]) {
  1342. var inserted = void 0;
  1343. inserted = parent[expando]._onDragOver({
  1344. clientX: touchEvt.clientX,
  1345. clientY: touchEvt.clientY,
  1346. target: target,
  1347. rootEl: parent
  1348. });
  1349. if (inserted && !this.options.dragoverBubble) {
  1350. break;
  1351. }
  1352. }
  1353. target = parent; // store last element
  1354. }
  1355. /* jshint boss:true */
  1356. while (parent = parent.parentNode);
  1357. }
  1358. _unhideGhostForTarget();
  1359. }
  1360. },
  1361. _onTouchMove: function _onTouchMove(
  1362. /**TouchEvent*/
  1363. evt) {
  1364. if (tapEvt) {
  1365. var options = this.options,
  1366. fallbackTolerance = options.fallbackTolerance,
  1367. fallbackOffset = options.fallbackOffset,
  1368. touch = evt.touches ? evt.touches[0] : evt,
  1369. ghostMatrix = ghostEl && matrix(ghostEl, true),
  1370. scaleX = ghostEl && ghostMatrix && ghostMatrix.a,
  1371. scaleY = ghostEl && ghostMatrix && ghostMatrix.d,
  1372. relativeScrollOffset = PositionGhostAbsolutely && ghostRelativeParent && getRelativeScrollOffset(ghostRelativeParent),
  1373. dx = (touch.clientX - tapEvt.clientX + fallbackOffset.x) / (scaleX || 1) + (relativeScrollOffset ? relativeScrollOffset[0] - ghostRelativeParentInitialScroll[0] : 0) / (scaleX || 1),
  1374. dy = (touch.clientY - tapEvt.clientY + fallbackOffset.y) / (scaleY || 1) + (relativeScrollOffset ? relativeScrollOffset[1] - ghostRelativeParentInitialScroll[1] : 0) / (scaleY || 1); // only set the status to dragging, when we are actually dragging
  1375. if (!Sortable.active && !awaitingDragStarted) {
  1376. if (fallbackTolerance && Math.max(Math.abs(touch.clientX - this._lastX), Math.abs(touch.clientY - this._lastY)) < fallbackTolerance) {
  1377. return;
  1378. }
  1379. this._onDragStart(evt, true);
  1380. }
  1381. if (ghostEl) {
  1382. if (ghostMatrix) {
  1383. ghostMatrix.e += dx - (lastDx || 0);
  1384. ghostMatrix.f += dy - (lastDy || 0);
  1385. } else {
  1386. ghostMatrix = {
  1387. a: 1,
  1388. b: 0,
  1389. c: 0,
  1390. d: 1,
  1391. e: dx,
  1392. f: dy
  1393. };
  1394. }
  1395. var cssMatrix = "matrix(".concat(ghostMatrix.a, ",").concat(ghostMatrix.b, ",").concat(ghostMatrix.c, ",").concat(ghostMatrix.d, ",").concat(ghostMatrix.e, ",").concat(ghostMatrix.f, ")");
  1396. css(ghostEl, 'webkitTransform', cssMatrix);
  1397. css(ghostEl, 'mozTransform', cssMatrix);
  1398. css(ghostEl, 'msTransform', cssMatrix);
  1399. css(ghostEl, 'transform', cssMatrix);
  1400. lastDx = dx;
  1401. lastDy = dy;
  1402. touchEvt = touch;
  1403. }
  1404. evt.cancelable && evt.preventDefault();
  1405. }
  1406. },
  1407. _appendGhost: function _appendGhost() {
  1408. // Bug if using scale(): https://stackoverflow.com/questions/2637058
  1409. // Not being adjusted for
  1410. if (!ghostEl) {
  1411. var container = this.options.fallbackOnBody ? document.body : rootEl,
  1412. rect = getRect(dragEl, true, PositionGhostAbsolutely, true, container),
  1413. options = this.options; // Position absolutely
  1414. if (PositionGhostAbsolutely) {
  1415. // Get relatively positioned parent
  1416. ghostRelativeParent = container;
  1417. while (css(ghostRelativeParent, 'position') === 'static' && css(ghostRelativeParent, 'transform') === 'none' && ghostRelativeParent !== document) {
  1418. ghostRelativeParent = ghostRelativeParent.parentNode;
  1419. }
  1420. if (ghostRelativeParent !== document.body && ghostRelativeParent !== document.documentElement) {
  1421. if (ghostRelativeParent === document) ghostRelativeParent = getWindowScrollingElement();
  1422. rect.top += ghostRelativeParent.scrollTop;
  1423. rect.left += ghostRelativeParent.scrollLeft;
  1424. } else {
  1425. ghostRelativeParent = getWindowScrollingElement();
  1426. }
  1427. ghostRelativeParentInitialScroll = getRelativeScrollOffset(ghostRelativeParent);
  1428. }
  1429. ghostEl = dragEl.cloneNode(true);
  1430. toggleClass(ghostEl, options.ghostClass, false);
  1431. toggleClass(ghostEl, options.fallbackClass, true);
  1432. toggleClass(ghostEl, options.dragClass, true);
  1433. css(ghostEl, 'transition', '');
  1434. css(ghostEl, 'transform', '');
  1435. css(ghostEl, 'box-sizing', 'border-box');
  1436. css(ghostEl, 'margin', 0);
  1437. css(ghostEl, 'top', rect.top);
  1438. css(ghostEl, 'left', rect.left);
  1439. css(ghostEl, 'width', rect.width);
  1440. css(ghostEl, 'height', rect.height);
  1441. css(ghostEl, 'opacity', '0.8');
  1442. css(ghostEl, 'position', PositionGhostAbsolutely ? 'absolute' : 'fixed');
  1443. css(ghostEl, 'zIndex', '100000');
  1444. css(ghostEl, 'pointerEvents', 'none');
  1445. Sortable.ghost = ghostEl;
  1446. container.appendChild(ghostEl); // Set transform-origin
  1447. css(ghostEl, 'transform-origin', tapDistanceLeft / parseInt(ghostEl.style.width) * 100 + '% ' + tapDistanceTop / parseInt(ghostEl.style.height) * 100 + '%');
  1448. }
  1449. },
  1450. _onDragStart: function _onDragStart(
  1451. /**Event*/
  1452. evt,
  1453. /**boolean*/
  1454. fallback) {
  1455. var _this = this;
  1456. var dataTransfer = evt.dataTransfer;
  1457. var options = _this.options;
  1458. pluginEvent('dragStart', this, {
  1459. evt: evt
  1460. });
  1461. if (Sortable.eventCanceled) {
  1462. this._onDrop();
  1463. return;
  1464. }
  1465. pluginEvent('setupClone', this);
  1466. if (!Sortable.eventCanceled) {
  1467. cloneEl = clone(dragEl);
  1468. cloneEl.removeAttribute("id");
  1469. cloneEl.draggable = false;
  1470. cloneEl.style['will-change'] = '';
  1471. this._hideClone();
  1472. toggleClass(cloneEl, this.options.chosenClass, false);
  1473. Sortable.clone = cloneEl;
  1474. } // #1143: IFrame support workaround
  1475. _this.cloneId = _nextTick(function () {
  1476. pluginEvent('clone', _this);
  1477. if (Sortable.eventCanceled) return;
  1478. if (!_this.options.removeCloneOnHide) {
  1479. rootEl.insertBefore(cloneEl, dragEl);
  1480. }
  1481. _this._hideClone();
  1482. _dispatchEvent({
  1483. sortable: _this,
  1484. name: 'clone'
  1485. });
  1486. });
  1487. !fallback && toggleClass(dragEl, options.dragClass, true); // Set proper drop events
  1488. if (fallback) {
  1489. ignoreNextClick = true;
  1490. _this._loopId = setInterval(_this._emulateDragOver, 50);
  1491. } else {
  1492. // Undo what was set in _prepareDragStart before drag started
  1493. off(document, 'mouseup', _this._onDrop);
  1494. off(document, 'touchend', _this._onDrop);
  1495. off(document, 'touchcancel', _this._onDrop);
  1496. if (dataTransfer) {
  1497. dataTransfer.effectAllowed = 'move';
  1498. options.setData && options.setData.call(_this, dataTransfer, dragEl);
  1499. }
  1500. on(document, 'drop', _this); // #1276 fix:
  1501. css(dragEl, 'transform', 'translateZ(0)');
  1502. }
  1503. awaitingDragStarted = true;
  1504. _this._dragStartId = _nextTick(_this._dragStarted.bind(_this, fallback, evt));
  1505. on(document, 'selectstart', _this);
  1506. moved = true;
  1507. if (Safari) {
  1508. css(document.body, 'user-select', 'none');
  1509. }
  1510. },
  1511. // Returns true - if no further action is needed (either inserted or another condition)
  1512. _onDragOver: function _onDragOver(
  1513. /**Event*/
  1514. evt) {
  1515. var el = this.el,
  1516. target = evt.target,
  1517. dragRect,
  1518. targetRect,
  1519. revert,
  1520. options = this.options,
  1521. group = options.group,
  1522. activeSortable = Sortable.active,
  1523. isOwner = activeGroup === group,
  1524. canSort = options.sort,
  1525. fromSortable = putSortable || activeSortable,
  1526. vertical,
  1527. _this = this,
  1528. completedFired = false;
  1529. if (_silent) return;
  1530. function dragOverEvent(name, extra) {
  1531. pluginEvent(name, _this, _objectSpread2({
  1532. evt: evt,
  1533. isOwner: isOwner,
  1534. axis: vertical ? 'vertical' : 'horizontal',
  1535. revert: revert,
  1536. dragRect: dragRect,
  1537. targetRect: targetRect,
  1538. canSort: canSort,
  1539. fromSortable: fromSortable,
  1540. target: target,
  1541. completed: completed,
  1542. onMove: function onMove(target, after) {
  1543. return _onMove(rootEl, el, dragEl, dragRect, target, getRect(target), evt, after);
  1544. },
  1545. changed: changed
  1546. }, extra));
  1547. } // Capture animation state
  1548. function capture() {
  1549. dragOverEvent('dragOverAnimationCapture');
  1550. _this.captureAnimationState();
  1551. if (_this !== fromSortable) {
  1552. fromSortable.captureAnimationState();
  1553. }
  1554. } // Return invocation when dragEl is inserted (or completed)
  1555. function completed(insertion) {
  1556. dragOverEvent('dragOverCompleted', {
  1557. insertion: insertion
  1558. });
  1559. if (insertion) {
  1560. // Clones must be hidden before folding animation to capture dragRectAbsolute properly
  1561. if (isOwner) {
  1562. activeSortable._hideClone();
  1563. } else {
  1564. activeSortable._showClone(_this);
  1565. }
  1566. if (_this !== fromSortable) {
  1567. // Set ghost class to new sortable's ghost class
  1568. toggleClass(dragEl, putSortable ? putSortable.options.ghostClass : activeSortable.options.ghostClass, false);
  1569. toggleClass(dragEl, options.ghostClass, true);
  1570. }
  1571. if (putSortable !== _this && _this !== Sortable.active) {
  1572. putSortable = _this;
  1573. } else if (_this === Sortable.active && putSortable) {
  1574. putSortable = null;
  1575. } // Animation
  1576. if (fromSortable === _this) {
  1577. _this._ignoreWhileAnimating = target;
  1578. }
  1579. _this.animateAll(function () {
  1580. dragOverEvent('dragOverAnimationComplete');
  1581. _this._ignoreWhileAnimating = null;
  1582. });
  1583. if (_this !== fromSortable) {
  1584. fromSortable.animateAll();
  1585. fromSortable._ignoreWhileAnimating = null;
  1586. }
  1587. } // Null lastTarget if it is not inside a previously swapped element
  1588. if (target === dragEl && !dragEl.animated || target === el && !target.animated) {
  1589. lastTarget = null;
  1590. } // no bubbling and not fallback
  1591. if (!options.dragoverBubble && !evt.rootEl && target !== document) {
  1592. dragEl.parentNode[expando]._isOutsideThisEl(evt.target); // Do not detect for empty insert if already inserted
  1593. !insertion && nearestEmptyInsertDetectEvent(evt);
  1594. }
  1595. !options.dragoverBubble && evt.stopPropagation && evt.stopPropagation();
  1596. return completedFired = true;
  1597. } // Call when dragEl has been inserted
  1598. function changed() {
  1599. newIndex = index(dragEl);
  1600. newDraggableIndex = index(dragEl, options.draggable);
  1601. _dispatchEvent({
  1602. sortable: _this,
  1603. name: 'change',
  1604. toEl: el,
  1605. newIndex: newIndex,
  1606. newDraggableIndex: newDraggableIndex,
  1607. originalEvent: evt
  1608. });
  1609. }
  1610. if (evt.preventDefault !== void 0) {
  1611. evt.cancelable && evt.preventDefault();
  1612. }
  1613. target = closest(target, options.draggable, el, true);
  1614. dragOverEvent('dragOver');
  1615. if (Sortable.eventCanceled) return completedFired;
  1616. if (dragEl.contains(evt.target) || target.animated && target.animatingX && target.animatingY || _this._ignoreWhileAnimating === target) {
  1617. return completed(false);
  1618. }
  1619. ignoreNextClick = false;
  1620. if (activeSortable && !options.disabled && (isOwner ? canSort || (revert = parentEl !== rootEl) // Reverting item into the original list
  1621. : putSortable === this || (this.lastPutMode = activeGroup.checkPull(this, activeSortable, dragEl, evt)) && group.checkPut(this, activeSortable, dragEl, evt))) {
  1622. vertical = this._getDirection(evt, target) === 'vertical';
  1623. dragRect = getRect(dragEl);
  1624. dragOverEvent('dragOverValid');
  1625. if (Sortable.eventCanceled) return completedFired;
  1626. if (revert) {
  1627. parentEl = rootEl; // actualization
  1628. capture();
  1629. this._hideClone();
  1630. dragOverEvent('revert');
  1631. if (!Sortable.eventCanceled) {
  1632. if (nextEl) {
  1633. rootEl.insertBefore(dragEl, nextEl);
  1634. } else {
  1635. rootEl.appendChild(dragEl);
  1636. }
  1637. }
  1638. return completed(true);
  1639. }
  1640. var elLastChild = lastChild(el, options.draggable);
  1641. if (!elLastChild || _ghostIsLast(evt, vertical, this) && !elLastChild.animated) {
  1642. // Insert to end of list
  1643. // If already at end of list: Do not insert
  1644. if (elLastChild === dragEl) {
  1645. return completed(false);
  1646. } // if there is a last element, it is the target
  1647. if (elLastChild && el === evt.target) {
  1648. target = elLastChild;
  1649. }
  1650. if (target) {
  1651. targetRect = getRect(target);
  1652. }
  1653. if (_onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt, !!target) !== false) {
  1654. capture();
  1655. if (elLastChild && elLastChild.nextSibling) {
  1656. // the last draggable element is not the last node
  1657. el.insertBefore(dragEl, elLastChild.nextSibling);
  1658. } else {
  1659. el.appendChild(dragEl);
  1660. }
  1661. parentEl = el; // actualization
  1662. changed();
  1663. return completed(true);
  1664. }
  1665. } else if (elLastChild && _ghostIsFirst(evt, vertical, this)) {
  1666. // Insert to start of list
  1667. var firstChild = getChild(el, 0, options, true);
  1668. if (firstChild === dragEl) {
  1669. return completed(false);
  1670. }
  1671. target = firstChild;
  1672. targetRect = getRect(target);
  1673. if (_onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt, false) !== false) {
  1674. capture();
  1675. el.insertBefore(dragEl, firstChild);
  1676. parentEl = el; // actualization
  1677. changed();
  1678. return completed(true);
  1679. }
  1680. } else if (target.parentNode === el) {
  1681. targetRect = getRect(target);
  1682. var direction = 0,
  1683. targetBeforeFirstSwap,
  1684. differentLevel = dragEl.parentNode !== el,
  1685. differentRowCol = !_dragElInRowColumn(dragEl.animated && dragEl.toRect || dragRect, target.animated && target.toRect || targetRect, vertical),
  1686. side1 = vertical ? 'top' : 'left',
  1687. scrolledPastTop = isScrolledPast(target, 'top', 'top') || isScrolledPast(dragEl, 'top', 'top'),
  1688. scrollBefore = scrolledPastTop ? scrolledPastTop.scrollTop : void 0;
  1689. if (lastTarget !== target) {
  1690. targetBeforeFirstSwap = targetRect[side1];
  1691. pastFirstInvertThresh = false;
  1692. isCircumstantialInvert = !differentRowCol && options.invertSwap || differentLevel;
  1693. }
  1694. direction = _getSwapDirection(evt, target, targetRect, vertical, differentRowCol ? 1 : options.swapThreshold, options.invertedSwapThreshold == null ? options.swapThreshold : options.invertedSwapThreshold, isCircumstantialInvert, lastTarget === target);
  1695. var sibling;
  1696. if (direction !== 0) {
  1697. // Check if target is beside dragEl in respective direction (ignoring hidden elements)
  1698. var dragIndex = index(dragEl);
  1699. do {
  1700. dragIndex -= direction;
  1701. sibling = parentEl.children[dragIndex];
  1702. } while (sibling && (css(sibling, 'display') === 'none' || sibling === ghostEl));
  1703. } // If dragEl is already beside target: Do not insert
  1704. if (direction === 0 || sibling === target) {
  1705. return completed(false);
  1706. }
  1707. lastTarget = target;
  1708. lastDirection = direction;
  1709. var nextSibling = target.nextElementSibling,
  1710. after = false;
  1711. after = direction === 1;
  1712. var moveVector = _onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt, after);
  1713. if (moveVector !== false) {
  1714. if (moveVector === 1 || moveVector === -1) {
  1715. after = moveVector === 1;
  1716. }
  1717. _silent = true;
  1718. setTimeout(_unsilent, 30);
  1719. capture();
  1720. if (after && !nextSibling) {
  1721. el.appendChild(dragEl);
  1722. } else {
  1723. target.parentNode.insertBefore(dragEl, after ? nextSibling : target);
  1724. } // Undo chrome's scroll adjustment (has no effect on other browsers)
  1725. if (scrolledPastTop) {
  1726. scrollBy(scrolledPastTop, 0, scrollBefore - scrolledPastTop.scrollTop);
  1727. }
  1728. parentEl = dragEl.parentNode; // actualization
  1729. // must be done before animation
  1730. if (targetBeforeFirstSwap !== undefined && !isCircumstantialInvert) {
  1731. targetMoveDistance = Math.abs(targetBeforeFirstSwap - getRect(target)[side1]);
  1732. }
  1733. changed();
  1734. return completed(true);
  1735. }
  1736. }
  1737. if (el.contains(dragEl)) {
  1738. return completed(false);
  1739. }
  1740. }
  1741. return false;
  1742. },
  1743. _ignoreWhileAnimating: null,
  1744. _offMoveEvents: function _offMoveEvents() {
  1745. off(document, 'mousemove', this._onTouchMove);
  1746. off(document, 'touchmove', this._onTouchMove);
  1747. off(document, 'pointermove', this._onTouchMove);
  1748. off(document, 'dragover', nearestEmptyInsertDetectEvent);
  1749. off(document, 'mousemove', nearestEmptyInsertDetectEvent);
  1750. off(document, 'touchmove', nearestEmptyInsertDetectEvent);
  1751. },
  1752. _offUpEvents: function _offUpEvents() {
  1753. var ownerDocument = this.el.ownerDocument;
  1754. off(ownerDocument, 'mouseup', this._onDrop);
  1755. off(ownerDocument, 'touchend', this._onDrop);
  1756. off(ownerDocument, 'pointerup', this._onDrop);
  1757. off(ownerDocument, 'touchcancel', this._onDrop);
  1758. off(document, 'selectstart', this);
  1759. },
  1760. _onDrop: function _onDrop(
  1761. /**Event*/
  1762. evt) {
  1763. var el = this.el,
  1764. options = this.options; // Get the index of the dragged element within its parent
  1765. newIndex = index(dragEl);
  1766. newDraggableIndex = index(dragEl, options.draggable);
  1767. pluginEvent('drop', this, {
  1768. evt: evt
  1769. });
  1770. parentEl = dragEl && dragEl.parentNode; // Get again after plugin event
  1771. newIndex = index(dragEl);
  1772. newDraggableIndex = index(dragEl, options.draggable);
  1773. if (Sortable.eventCanceled) {
  1774. this._nulling();
  1775. return;
  1776. }
  1777. awaitingDragStarted = false;
  1778. isCircumstantialInvert = false;
  1779. pastFirstInvertThresh = false;
  1780. clearInterval(this._loopId);
  1781. clearTimeout(this._dragStartTimer);
  1782. _cancelNextTick(this.cloneId);
  1783. _cancelNextTick(this._dragStartId); // Unbind events
  1784. if (this.nativeDraggable) {
  1785. off(document, 'drop', this);
  1786. off(el, 'dragstart', this._onDragStart);
  1787. }
  1788. this._offMoveEvents();
  1789. this._offUpEvents();
  1790. if (Safari) {
  1791. css(document.body, 'user-select', '');
  1792. }
  1793. css(dragEl, 'transform', '');
  1794. if (evt) {
  1795. if (moved) {
  1796. evt.cancelable && evt.preventDefault();
  1797. !options.dropBubble && evt.stopPropagation();
  1798. }
  1799. ghostEl && ghostEl.parentNode && ghostEl.parentNode.removeChild(ghostEl);
  1800. if (rootEl === parentEl || putSortable && putSortable.lastPutMode !== 'clone') {
  1801. // Remove clone(s)
  1802. cloneEl && cloneEl.parentNode && cloneEl.parentNode.removeChild(cloneEl);
  1803. }
  1804. if (dragEl) {
  1805. if (this.nativeDraggable) {
  1806. off(dragEl, 'dragend', this);
  1807. }
  1808. _disableDraggable(dragEl);
  1809. dragEl.style['will-change'] = ''; // Remove classes
  1810. // ghostClass is added in dragStarted
  1811. if (moved && !awaitingDragStarted) {
  1812. toggleClass(dragEl, putSortable ? putSortable.options.ghostClass : this.options.ghostClass, false);
  1813. }
  1814. toggleClass(dragEl, this.options.chosenClass, false); // Drag stop event
  1815. _dispatchEvent({
  1816. sortable: this,
  1817. name: 'unchoose',
  1818. toEl: parentEl,
  1819. newIndex: null,
  1820. newDraggableIndex: null,
  1821. originalEvent: evt
  1822. });
  1823. if (rootEl !== parentEl) {
  1824. if (newIndex >= 0) {
  1825. // Add event
  1826. _dispatchEvent({
  1827. rootEl: parentEl,
  1828. name: 'add',
  1829. toEl: parentEl,
  1830. fromEl: rootEl,
  1831. originalEvent: evt
  1832. }); // Remove event
  1833. _dispatchEvent({
  1834. sortable: this,
  1835. name: 'remove',
  1836. toEl: parentEl,
  1837. originalEvent: evt
  1838. }); // drag from one list and drop into another
  1839. _dispatchEvent({
  1840. rootEl: parentEl,
  1841. name: 'sort',
  1842. toEl: parentEl,
  1843. fromEl: rootEl,
  1844. originalEvent: evt
  1845. });
  1846. _dispatchEvent({
  1847. sortable: this,
  1848. name: 'sort',
  1849. toEl: parentEl,
  1850. originalEvent: evt
  1851. });
  1852. }
  1853. putSortable && putSortable.save();
  1854. } else {
  1855. if (newIndex !== oldIndex) {
  1856. if (newIndex >= 0) {
  1857. // drag & drop within the same list
  1858. _dispatchEvent({
  1859. sortable: this,
  1860. name: 'update',
  1861. toEl: parentEl,
  1862. originalEvent: evt
  1863. });
  1864. _dispatchEvent({
  1865. sortable: this,
  1866. name: 'sort',
  1867. toEl: parentEl,
  1868. originalEvent: evt
  1869. });
  1870. }
  1871. }
  1872. }
  1873. if (Sortable.active) {
  1874. /* jshint eqnull:true */
  1875. if (newIndex == null || newIndex === -1) {
  1876. newIndex = oldIndex;
  1877. newDraggableIndex = oldDraggableIndex;
  1878. }
  1879. _dispatchEvent({
  1880. sortable: this,
  1881. name: 'end',
  1882. toEl: parentEl,
  1883. originalEvent: evt
  1884. }); // Save sorting
  1885. this.save();
  1886. }
  1887. }
  1888. }
  1889. this._nulling();
  1890. },
  1891. _nulling: function _nulling() {
  1892. pluginEvent('nulling', this);
  1893. rootEl = dragEl = parentEl = ghostEl = nextEl = cloneEl = lastDownEl = cloneHidden = tapEvt = touchEvt = moved = newIndex = newDraggableIndex = oldIndex = oldDraggableIndex = lastTarget = lastDirection = putSortable = activeGroup = Sortable.dragged = Sortable.ghost = Sortable.clone = Sortable.active = null;
  1894. savedInputChecked.forEach(function (el) {
  1895. el.checked = true;
  1896. });
  1897. savedInputChecked.length = lastDx = lastDy = 0;
  1898. },
  1899. handleEvent: function handleEvent(
  1900. /**Event*/
  1901. evt) {
  1902. switch (evt.type) {
  1903. case 'drop':
  1904. case 'dragend':
  1905. this._onDrop(evt);
  1906. break;
  1907. case 'dragenter':
  1908. case 'dragover':
  1909. if (dragEl) {
  1910. this._onDragOver(evt);
  1911. _globalDragOver(evt);
  1912. }
  1913. break;
  1914. case 'selectstart':
  1915. evt.preventDefault();
  1916. break;
  1917. }
  1918. },
  1919. /**
  1920. * Serializes the item into an array of string.
  1921. * @returns {String[]}
  1922. */
  1923. toArray: function toArray() {
  1924. var order = [],
  1925. el,
  1926. children = this.el.children,
  1927. i = 0,
  1928. n = children.length,
  1929. options = this.options;
  1930. for (; i < n; i++) {
  1931. el = children[i];
  1932. if (closest(el, options.draggable, this.el, false)) {
  1933. order.push(el.getAttribute(options.dataIdAttr) || _generateId(el));
  1934. }
  1935. }
  1936. return order;
  1937. },
  1938. /**
  1939. * Sorts the elements according to the array.
  1940. * @param {String[]} order order of the items
  1941. */
  1942. sort: function sort(order, useAnimation) {
  1943. var items = {},
  1944. rootEl = this.el;
  1945. this.toArray().forEach(function (id, i) {
  1946. var el = rootEl.children[i];
  1947. if (closest(el, this.options.draggable, rootEl, false)) {
  1948. items[id] = el;
  1949. }
  1950. }, this);
  1951. useAnimation && this.captureAnimationState();
  1952. order.forEach(function (id) {
  1953. if (items[id]) {
  1954. rootEl.removeChild(items[id]);
  1955. rootEl.appendChild(items[id]);
  1956. }
  1957. });
  1958. useAnimation && this.animateAll();
  1959. },
  1960. /**
  1961. * Save the current sorting
  1962. */
  1963. save: function save() {
  1964. var store = this.options.store;
  1965. store && store.set && store.set(this);
  1966. },
  1967. /**
  1968. * For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.
  1969. * @param {HTMLElement} el
  1970. * @param {String} [selector] default: `options.draggable`
  1971. * @returns {HTMLElement|null}
  1972. */
  1973. closest: function closest$1(el, selector) {
  1974. return closest(el, selector || this.options.draggable, this.el, false);
  1975. },
  1976. /**
  1977. * Set/get option
  1978. * @param {string} name
  1979. * @param {*} [value]
  1980. * @returns {*}
  1981. */
  1982. option: function option(name, value) {
  1983. var options = this.options;
  1984. if (value === void 0) {
  1985. return options[name];
  1986. } else {
  1987. var modifiedValue = PluginManager.modifyOption(this, name, value);
  1988. if (typeof modifiedValue !== 'undefined') {
  1989. options[name] = modifiedValue;
  1990. } else {
  1991. options[name] = value;
  1992. }
  1993. if (name === 'group') {
  1994. _prepareGroup(options);
  1995. }
  1996. }
  1997. },
  1998. /**
  1999. * Destroy
  2000. */
  2001. destroy: function destroy() {
  2002. pluginEvent('destroy', this);
  2003. var el = this.el;
  2004. el[expando] = null;
  2005. off(el, 'mousedown', this._onTapStart);
  2006. off(el, 'touchstart', this._onTapStart);
  2007. off(el, 'pointerdown', this._onTapStart);
  2008. if (this.nativeDraggable) {
  2009. off(el, 'dragover', this);
  2010. off(el, 'dragenter', this);
  2011. } // Remove draggable attributes
  2012. Array.prototype.forEach.call(el.querySelectorAll('[draggable]'), function (el) {
  2013. el.removeAttribute('draggable');
  2014. });
  2015. this._onDrop();
  2016. this._disableDelayedDragEvents();
  2017. sortables.splice(sortables.indexOf(this.el), 1);
  2018. this.el = el = null;
  2019. },
  2020. _hideClone: function _hideClone() {
  2021. if (!cloneHidden) {
  2022. pluginEvent('hideClone', this);
  2023. if (Sortable.eventCanceled) return;
  2024. css(cloneEl, 'display', 'none');
  2025. if (this.options.removeCloneOnHide && cloneEl.parentNode) {
  2026. cloneEl.parentNode.removeChild(cloneEl);
  2027. }
  2028. cloneHidden = true;
  2029. }
  2030. },
  2031. _showClone: function _showClone(putSortable) {
  2032. if (putSortable.lastPutMode !== 'clone') {
  2033. this._hideClone();
  2034. return;
  2035. }
  2036. if (cloneHidden) {
  2037. pluginEvent('showClone', this);
  2038. if (Sortable.eventCanceled) return; // show clone at dragEl or original position
  2039. if (dragEl.parentNode == rootEl && !this.options.group.revertClone) {
  2040. rootEl.insertBefore(cloneEl, dragEl);
  2041. } else if (nextEl) {
  2042. rootEl.insertBefore(cloneEl, nextEl);
  2043. } else {
  2044. rootEl.appendChild(cloneEl);
  2045. }
  2046. if (this.options.group.revertClone) {
  2047. this.animate(dragEl, cloneEl);
  2048. }
  2049. css(cloneEl, 'display', '');
  2050. cloneHidden = false;
  2051. }
  2052. }
  2053. };
  2054. function _globalDragOver(
  2055. /**Event*/
  2056. evt) {
  2057. if (evt.dataTransfer) {
  2058. evt.dataTransfer.dropEffect = 'move';
  2059. }
  2060. evt.cancelable && evt.preventDefault();
  2061. }
  2062. function _onMove(fromEl, toEl, dragEl, dragRect, targetEl, targetRect, originalEvent, willInsertAfter) {
  2063. var evt,
  2064. sortable = fromEl[expando],
  2065. onMoveFn = sortable.options.onMove,
  2066. retVal; // Support for new CustomEvent feature
  2067. if (window.CustomEvent && !IE11OrLess && !Edge) {
  2068. evt = new CustomEvent('move', {
  2069. bubbles: true,
  2070. cancelable: true
  2071. });
  2072. } else {
  2073. evt = document.createEvent('Event');
  2074. evt.initEvent('move', true, true);
  2075. }
  2076. evt.to = toEl;
  2077. evt.from = fromEl;
  2078. evt.dragged = dragEl;
  2079. evt.draggedRect = dragRect;
  2080. evt.related = targetEl || toEl;
  2081. evt.relatedRect = targetRect || getRect(toEl);
  2082. evt.willInsertAfter = willInsertAfter;
  2083. evt.originalEvent = originalEvent;
  2084. fromEl.dispatchEvent(evt);
  2085. if (onMoveFn) {
  2086. retVal = onMoveFn.call(sortable, evt, originalEvent);
  2087. }
  2088. return retVal;
  2089. }
  2090. function _disableDraggable(el) {
  2091. el.draggable = false;
  2092. }
  2093. function _unsilent() {
  2094. _silent = false;
  2095. }
  2096. function _ghostIsFirst(evt, vertical, sortable) {
  2097. var rect = getRect(getChild(sortable.el, 0, sortable.options, true));
  2098. var spacer = 10;
  2099. return vertical ? evt.clientX < rect.left - spacer || evt.clientY < rect.top && evt.clientX < rect.right : evt.clientY < rect.top - spacer || evt.clientY < rect.bottom && evt.clientX < rect.left;
  2100. }
  2101. function _ghostIsLast(evt, vertical, sortable) {
  2102. var rect = getRect(lastChild(sortable.el, sortable.options.draggable));
  2103. var spacer = 10;
  2104. return vertical ? evt.clientX > rect.right + spacer || evt.clientX <= rect.right && evt.clientY > rect.bottom && evt.clientX >= rect.left : evt.clientX > rect.right && evt.clientY > rect.top || evt.clientX <= rect.right && evt.clientY > rect.bottom + spacer;
  2105. }
  2106. function _getSwapDirection(evt, target, targetRect, vertical, swapThreshold, invertedSwapThreshold, invertSwap, isLastTarget) {
  2107. var mouseOnAxis = vertical ? evt.clientY : evt.clientX,
  2108. targetLength = vertical ? targetRect.height : targetRect.width,
  2109. targetS1 = vertical ? targetRect.top : targetRect.left,
  2110. targetS2 = vertical ? targetRect.bottom : targetRect.right,
  2111. invert = false;
  2112. if (!invertSwap) {
  2113. // Never invert or create dragEl shadow when target movemenet causes mouse to move past the end of regular swapThreshold
  2114. if (isLastTarget && targetMoveDistance < targetLength * swapThreshold) {
  2115. // multiplied only by swapThreshold because mouse will already be inside target by (1 - threshold) * targetLength / 2
  2116. // check if past first invert threshold on side opposite of lastDirection
  2117. if (!pastFirstInvertThresh && (lastDirection === 1 ? mouseOnAxis > targetS1 + targetLength * invertedSwapThreshold / 2 : mouseOnAxis < targetS2 - targetLength * invertedSwapThreshold / 2)) {
  2118. // past first invert threshold, do not restrict inverted threshold to dragEl shadow
  2119. pastFirstInvertThresh = true;
  2120. }
  2121. if (!pastFirstInvertThresh) {
  2122. // dragEl shadow (target move distance shadow)
  2123. if (lastDirection === 1 ? mouseOnAxis < targetS1 + targetMoveDistance // over dragEl shadow
  2124. : mouseOnAxis > targetS2 - targetMoveDistance) {
  2125. return -lastDirection;
  2126. }
  2127. } else {
  2128. invert = true;
  2129. }
  2130. } else {
  2131. // Regular
  2132. if (mouseOnAxis > targetS1 + targetLength * (1 - swapThreshold) / 2 && mouseOnAxis < targetS2 - targetLength * (1 - swapThreshold) / 2) {
  2133. return _getInsertDirection(target);
  2134. }
  2135. }
  2136. }
  2137. invert = invert || invertSwap;
  2138. if (invert) {
  2139. // Invert of regular
  2140. if (mouseOnAxis < targetS1 + targetLength * invertedSwapThreshold / 2 || mouseOnAxis > targetS2 - targetLength * invertedSwapThreshold / 2) {
  2141. return mouseOnAxis > targetS1 + targetLength / 2 ? 1 : -1;
  2142. }
  2143. }
  2144. return 0;
  2145. }
  2146. /**
  2147. * Gets the direction dragEl must be swapped relative to target in order to make it
  2148. * seem that dragEl has been "inserted" into that element's position
  2149. * @param {HTMLElement} target The target whose position dragEl is being inserted at
  2150. * @return {Number} Direction dragEl must be swapped
  2151. */
  2152. function _getInsertDirection(target) {
  2153. if (index(dragEl) < index(target)) {
  2154. return 1;
  2155. } else {
  2156. return -1;
  2157. }
  2158. }
  2159. /**
  2160. * Generate id
  2161. * @param {HTMLElement} el
  2162. * @returns {String}
  2163. * @private
  2164. */
  2165. function _generateId(el) {
  2166. var str = el.tagName + el.className + el.src + el.href + el.textContent,
  2167. i = str.length,
  2168. sum = 0;
  2169. while (i--) {
  2170. sum += str.charCodeAt(i);
  2171. }
  2172. return sum.toString(36);
  2173. }
  2174. function _saveInputCheckedState(root) {
  2175. savedInputChecked.length = 0;
  2176. var inputs = root.getElementsByTagName('input');
  2177. var idx = inputs.length;
  2178. while (idx--) {
  2179. var el = inputs[idx];
  2180. el.checked && savedInputChecked.push(el);
  2181. }
  2182. }
  2183. function _nextTick(fn) {
  2184. return setTimeout(fn, 0);
  2185. }
  2186. function _cancelNextTick(id) {
  2187. return clearTimeout(id);
  2188. } // Fixed #973:
  2189. if (documentExists) {
  2190. on(document, 'touchmove', function (evt) {
  2191. if ((Sortable.active || awaitingDragStarted) && evt.cancelable) {
  2192. evt.preventDefault();
  2193. }
  2194. });
  2195. } // Export utils
  2196. Sortable.utils = {
  2197. on: on,
  2198. off: off,
  2199. css: css,
  2200. find: find,
  2201. is: function is(el, selector) {
  2202. return !!closest(el, selector, el, false);
  2203. },
  2204. extend: extend,
  2205. throttle: throttle,
  2206. closest: closest,
  2207. toggleClass: toggleClass,
  2208. clone: clone,
  2209. index: index,
  2210. nextTick: _nextTick,
  2211. cancelNextTick: _cancelNextTick,
  2212. detectDirection: _detectDirection,
  2213. getChild: getChild
  2214. };
  2215. /**
  2216. * Get the Sortable instance of an element
  2217. * @param {HTMLElement} element The element
  2218. * @return {Sortable|undefined} The instance of Sortable
  2219. */
  2220. Sortable.get = function (element) {
  2221. return element[expando];
  2222. };
  2223. /**
  2224. * Mount a plugin to Sortable
  2225. * @param {...SortablePlugin|SortablePlugin[]} plugins Plugins being mounted
  2226. */
  2227. Sortable.mount = function () {
  2228. for (var _len = arguments.length, plugins = new Array(_len), _key = 0; _key < _len; _key++) {
  2229. plugins[_key] = arguments[_key];
  2230. }
  2231. if (plugins[0].constructor === Array) plugins = plugins[0];
  2232. plugins.forEach(function (plugin) {
  2233. if (!plugin.prototype || !plugin.prototype.constructor) {
  2234. throw "Sortable: Mounted plugin must be a constructor function, not ".concat({}.toString.call(plugin));
  2235. }
  2236. if (plugin.utils) Sortable.utils = _objectSpread2(_objectSpread2({}, Sortable.utils), plugin.utils);
  2237. PluginManager.mount(plugin);
  2238. });
  2239. };
  2240. /**
  2241. * Create sortable instance
  2242. * @param {HTMLElement} el
  2243. * @param {Object} [options]
  2244. */
  2245. Sortable.create = function (el, options) {
  2246. return new Sortable(el, options);
  2247. }; // Export
  2248. Sortable.version = version;
  2249. var autoScrolls = [],
  2250. scrollEl,
  2251. scrollRootEl,
  2252. scrolling = false,
  2253. lastAutoScrollX,
  2254. lastAutoScrollY,
  2255. touchEvt$1,
  2256. pointerElemChangedInterval;
  2257. function AutoScrollPlugin() {
  2258. function AutoScroll() {
  2259. this.defaults = {
  2260. scroll: true,
  2261. forceAutoScrollFallback: false,
  2262. scrollSensitivity: 30,
  2263. scrollSpeed: 10,
  2264. bubbleScroll: true
  2265. }; // Bind all private methods
  2266. for (var fn in this) {
  2267. if (fn.charAt(0) === '_' && typeof this[fn] === 'function') {
  2268. this[fn] = this[fn].bind(this);
  2269. }
  2270. }
  2271. }
  2272. AutoScroll.prototype = {
  2273. dragStarted: function dragStarted(_ref) {
  2274. var originalEvent = _ref.originalEvent;
  2275. if (this.sortable.nativeDraggable) {
  2276. on(document, 'dragover', this._handleAutoScroll);
  2277. } else {
  2278. if (this.options.supportPointer) {
  2279. on(document, 'pointermove', this._handleFallbackAutoScroll);
  2280. } else if (originalEvent.touches) {
  2281. on(document, 'touchmove', this._handleFallbackAutoScroll);
  2282. } else {
  2283. on(document, 'mousemove', this._handleFallbackAutoScroll);
  2284. }
  2285. }
  2286. },
  2287. dragOverCompleted: function dragOverCompleted(_ref2) {
  2288. var originalEvent = _ref2.originalEvent;
  2289. // For when bubbling is canceled and using fallback (fallback 'touchmove' always reached)
  2290. if (!this.options.dragOverBubble && !originalEvent.rootEl) {
  2291. this._handleAutoScroll(originalEvent);
  2292. }
  2293. },
  2294. drop: function drop() {
  2295. if (this.sortable.nativeDraggable) {
  2296. off(document, 'dragover', this._handleAutoScroll);
  2297. } else {
  2298. off(document, 'pointermove', this._handleFallbackAutoScroll);
  2299. off(document, 'touchmove', this._handleFallbackAutoScroll);
  2300. off(document, 'mousemove', this._handleFallbackAutoScroll);
  2301. }
  2302. clearPointerElemChangedInterval();
  2303. clearAutoScrolls();
  2304. cancelThrottle();
  2305. },
  2306. nulling: function nulling() {
  2307. touchEvt$1 = scrollRootEl = scrollEl = scrolling = pointerElemChangedInterval = lastAutoScrollX = lastAutoScrollY = null;
  2308. autoScrolls.length = 0;
  2309. },
  2310. _handleFallbackAutoScroll: function _handleFallbackAutoScroll(evt) {
  2311. this._handleAutoScroll(evt, true);
  2312. },
  2313. _handleAutoScroll: function _handleAutoScroll(evt, fallback) {
  2314. var _this = this;
  2315. var x = (evt.touches ? evt.touches[0] : evt).clientX,
  2316. y = (evt.touches ? evt.touches[0] : evt).clientY,
  2317. elem = document.elementFromPoint(x, y);
  2318. touchEvt$1 = evt; // IE does not seem to have native autoscroll,
  2319. // Edge's autoscroll seems too conditional,
  2320. // MACOS Safari does not have autoscroll,
  2321. // Firefox and Chrome are good
  2322. if (fallback || this.options.forceAutoScrollFallback || Edge || IE11OrLess || Safari) {
  2323. autoScroll(evt, this.options, elem, fallback); // Listener for pointer element change
  2324. var ogElemScroller = getParentAutoScrollElement(elem, true);
  2325. if (scrolling && (!pointerElemChangedInterval || x !== lastAutoScrollX || y !== lastAutoScrollY)) {
  2326. pointerElemChangedInterval && clearPointerElemChangedInterval(); // Detect for pointer elem change, emulating native DnD behaviour
  2327. pointerElemChangedInterval = setInterval(function () {
  2328. var newElem = getParentAutoScrollElement(document.elementFromPoint(x, y), true);
  2329. if (newElem !== ogElemScroller) {
  2330. ogElemScroller = newElem;
  2331. clearAutoScrolls();
  2332. }
  2333. autoScroll(evt, _this.options, newElem, fallback);
  2334. }, 10);
  2335. lastAutoScrollX = x;
  2336. lastAutoScrollY = y;
  2337. }
  2338. } else {
  2339. // if DnD is enabled (and browser has good autoscrolling), first autoscroll will already scroll, so get parent autoscroll of first autoscroll
  2340. if (!this.options.bubbleScroll || getParentAutoScrollElement(elem, true) === getWindowScrollingElement()) {
  2341. clearAutoScrolls();
  2342. return;
  2343. }
  2344. autoScroll(evt, this.options, getParentAutoScrollElement(elem, false), false);
  2345. }
  2346. }
  2347. };
  2348. return _extends(AutoScroll, {
  2349. pluginName: 'scroll',
  2350. initializeByDefault: true
  2351. });
  2352. }
  2353. function clearAutoScrolls() {
  2354. autoScrolls.forEach(function (autoScroll) {
  2355. clearInterval(autoScroll.pid);
  2356. });
  2357. autoScrolls = [];
  2358. }
  2359. function clearPointerElemChangedInterval() {
  2360. clearInterval(pointerElemChangedInterval);
  2361. }
  2362. var autoScroll = throttle(function (evt, options, rootEl, isFallback) {
  2363. // Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=505521
  2364. if (!options.scroll) return;
  2365. var x = (evt.touches ? evt.touches[0] : evt).clientX,
  2366. y = (evt.touches ? evt.touches[0] : evt).clientY,
  2367. sens = options.scrollSensitivity,
  2368. speed = options.scrollSpeed,
  2369. winScroller = getWindowScrollingElement();
  2370. var scrollThisInstance = false,
  2371. scrollCustomFn; // New scroll root, set scrollEl
  2372. if (scrollRootEl !== rootEl) {
  2373. scrollRootEl = rootEl;
  2374. clearAutoScrolls();
  2375. scrollEl = options.scroll;
  2376. scrollCustomFn = options.scrollFn;
  2377. if (scrollEl === true) {
  2378. scrollEl = getParentAutoScrollElement(rootEl, true);
  2379. }
  2380. }
  2381. var layersOut = 0;
  2382. var currentParent = scrollEl;
  2383. do {
  2384. var el = currentParent,
  2385. rect = getRect(el),
  2386. top = rect.top,
  2387. bottom = rect.bottom,
  2388. left = rect.left,
  2389. right = rect.right,
  2390. width = rect.width,
  2391. height = rect.height,
  2392. canScrollX = void 0,
  2393. canScrollY = void 0,
  2394. scrollWidth = el.scrollWidth,
  2395. scrollHeight = el.scrollHeight,
  2396. elCSS = css(el),
  2397. scrollPosX = el.scrollLeft,
  2398. scrollPosY = el.scrollTop;
  2399. if (el === winScroller) {
  2400. canScrollX = width < scrollWidth && (elCSS.overflowX === 'auto' || elCSS.overflowX === 'scroll' || elCSS.overflowX === 'visible');
  2401. canScrollY = height < scrollHeight && (elCSS.overflowY === 'auto' || elCSS.overflowY === 'scroll' || elCSS.overflowY === 'visible');
  2402. } else {
  2403. canScrollX = width < scrollWidth && (elCSS.overflowX === 'auto' || elCSS.overflowX === 'scroll');
  2404. canScrollY = height < scrollHeight && (elCSS.overflowY === 'auto' || elCSS.overflowY === 'scroll');
  2405. }
  2406. var vx = canScrollX && (Math.abs(right - x) <= sens && scrollPosX + width < scrollWidth) - (Math.abs(left - x) <= sens && !!scrollPosX);
  2407. var vy = canScrollY && (Math.abs(bottom - y) <= sens && scrollPosY + height < scrollHeight) - (Math.abs(top - y) <= sens && !!scrollPosY);
  2408. if (!autoScrolls[layersOut]) {
  2409. for (var i = 0; i <= layersOut; i++) {
  2410. if (!autoScrolls[i]) {
  2411. autoScrolls[i] = {};
  2412. }
  2413. }
  2414. }
  2415. if (autoScrolls[layersOut].vx != vx || autoScrolls[layersOut].vy != vy || autoScrolls[layersOut].el !== el) {
  2416. autoScrolls[layersOut].el = el;
  2417. autoScrolls[layersOut].vx = vx;
  2418. autoScrolls[layersOut].vy = vy;
  2419. clearInterval(autoScrolls[layersOut].pid);
  2420. if (vx != 0 || vy != 0) {
  2421. scrollThisInstance = true;
  2422. /* jshint loopfunc:true */
  2423. autoScrolls[layersOut].pid = setInterval(function () {
  2424. // emulate drag over during autoscroll (fallback), emulating native DnD behaviour
  2425. if (isFallback && this.layer === 0) {
  2426. Sortable.active._onTouchMove(touchEvt$1); // To move ghost if it is positioned absolutely
  2427. }
  2428. var scrollOffsetY = autoScrolls[this.layer].vy ? autoScrolls[this.layer].vy * speed : 0;
  2429. var scrollOffsetX = autoScrolls[this.layer].vx ? autoScrolls[this.layer].vx * speed : 0;
  2430. if (typeof scrollCustomFn === 'function') {
  2431. if (scrollCustomFn.call(Sortable.dragged.parentNode[expando], scrollOffsetX, scrollOffsetY, evt, touchEvt$1, autoScrolls[this.layer].el) !== 'continue') {
  2432. return;
  2433. }
  2434. }
  2435. scrollBy(autoScrolls[this.layer].el, scrollOffsetX, scrollOffsetY);
  2436. }.bind({
  2437. layer: layersOut
  2438. }), 24);
  2439. }
  2440. }
  2441. layersOut++;
  2442. } while (options.bubbleScroll && currentParent !== winScroller && (currentParent = getParentAutoScrollElement(currentParent, false)));
  2443. scrolling = scrollThisInstance; // in case another function catches scrolling as false in between when it is not
  2444. }, 30);
  2445. var drop = function drop(_ref) {
  2446. var originalEvent = _ref.originalEvent,
  2447. putSortable = _ref.putSortable,
  2448. dragEl = _ref.dragEl,
  2449. activeSortable = _ref.activeSortable,
  2450. dispatchSortableEvent = _ref.dispatchSortableEvent,
  2451. hideGhostForTarget = _ref.hideGhostForTarget,
  2452. unhideGhostForTarget = _ref.unhideGhostForTarget;
  2453. if (!originalEvent) return;
  2454. var toSortable = putSortable || activeSortable;
  2455. hideGhostForTarget();
  2456. var touch = originalEvent.changedTouches && originalEvent.changedTouches.length ? originalEvent.changedTouches[0] : originalEvent;
  2457. var target = document.elementFromPoint(touch.clientX, touch.clientY);
  2458. unhideGhostForTarget();
  2459. if (toSortable && !toSortable.el.contains(target)) {
  2460. dispatchSortableEvent('spill');
  2461. this.onSpill({
  2462. dragEl: dragEl,
  2463. putSortable: putSortable
  2464. });
  2465. }
  2466. };
  2467. function Revert() {}
  2468. Revert.prototype = {
  2469. startIndex: null,
  2470. dragStart: function dragStart(_ref2) {
  2471. var oldDraggableIndex = _ref2.oldDraggableIndex;
  2472. this.startIndex = oldDraggableIndex;
  2473. },
  2474. onSpill: function onSpill(_ref3) {
  2475. var dragEl = _ref3.dragEl,
  2476. putSortable = _ref3.putSortable;
  2477. this.sortable.captureAnimationState();
  2478. if (putSortable) {
  2479. putSortable.captureAnimationState();
  2480. }
  2481. var nextSibling = getChild(this.sortable.el, this.startIndex, this.options);
  2482. if (nextSibling) {
  2483. this.sortable.el.insertBefore(dragEl, nextSibling);
  2484. } else {
  2485. this.sortable.el.appendChild(dragEl);
  2486. }
  2487. this.sortable.animateAll();
  2488. if (putSortable) {
  2489. putSortable.animateAll();
  2490. }
  2491. },
  2492. drop: drop
  2493. };
  2494. _extends(Revert, {
  2495. pluginName: 'revertOnSpill'
  2496. });
  2497. function Remove() {}
  2498. Remove.prototype = {
  2499. onSpill: function onSpill(_ref4) {
  2500. var dragEl = _ref4.dragEl,
  2501. putSortable = _ref4.putSortable;
  2502. var parentSortable = putSortable || this.sortable;
  2503. parentSortable.captureAnimationState();
  2504. dragEl.parentNode && dragEl.parentNode.removeChild(dragEl);
  2505. parentSortable.animateAll();
  2506. },
  2507. drop: drop
  2508. };
  2509. _extends(Remove, {
  2510. pluginName: 'removeOnSpill'
  2511. });
  2512. Sortable.mount(new AutoScrollPlugin());
  2513. Sortable.mount(Remove, Revert);
  2514. exports.Sortable = Sortable;