Sortable.js 116 KB

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