sortable.js 118 KB

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