sortable.complete.esm.js 111 KB

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