Sortable.js 118 KB

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