sortable.js 121 KB

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