splide.esm.js 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946
  1. /*!
  2. * Splide.js
  3. * Version : 4.1.3
  4. * License : MIT
  5. * Copyright: 2022 Naotoshi Fujita
  6. */
  7. const MEDIA_PREFERS_REDUCED_MOTION = "(prefers-reduced-motion: reduce)";
  8. const CREATED = 1;
  9. const MOUNTED = 2;
  10. const IDLE = 3;
  11. const MOVING = 4;
  12. const SCROLLING = 5;
  13. const DRAGGING = 6;
  14. const DESTROYED = 7;
  15. const STATES = {
  16. CREATED,
  17. MOUNTED,
  18. IDLE,
  19. MOVING,
  20. SCROLLING,
  21. DRAGGING,
  22. DESTROYED
  23. };
  24. function empty(array) {
  25. array.length = 0;
  26. }
  27. function apply(func, ...args) {
  28. return func.bind(null, ...args);
  29. }
  30. const nextTick = setTimeout;
  31. const noop = () => {
  32. };
  33. function raf(func) {
  34. return requestAnimationFrame(func);
  35. }
  36. function typeOf(type, subject) {
  37. return typeof subject === type;
  38. }
  39. function isObject(subject) {
  40. return !isNull(subject) && typeOf("object", subject);
  41. }
  42. const isArray = Array.isArray;
  43. const isFunction = apply(typeOf, "function");
  44. const isString = apply(typeOf, "string");
  45. apply(typeOf, "boolean");
  46. const isUndefined = apply(typeOf, "undefined");
  47. function isNull(subject) {
  48. return subject === null;
  49. }
  50. function isHTMLElement(subject) {
  51. try {
  52. return subject instanceof (subject.ownerDocument.defaultView || window).HTMLElement;
  53. } catch (e) {
  54. return false;
  55. }
  56. }
  57. function toArray(value) {
  58. return isArray(value) ? value : [value];
  59. }
  60. function forEach(values, iteratee) {
  61. toArray(values).forEach(iteratee);
  62. }
  63. function push(array, items) {
  64. array.push(...toArray(items));
  65. return array;
  66. }
  67. const arrayProto = Array.prototype;
  68. function slice(arrayLike, start, end) {
  69. return arrayProto.slice.call(arrayLike, start, end);
  70. }
  71. function includes(arrayLike, value) {
  72. return arrayProto.includes.call(arrayLike, value);
  73. }
  74. function toggleClass(elm, classes, force) {
  75. if (elm) {
  76. forEach(isString(classes) ? classes.split(" ") : classes, (className) => {
  77. className && elm.classList.toggle(className, force);
  78. });
  79. }
  80. }
  81. function addClass(elm, classes) {
  82. toggleClass(elm, classes, true);
  83. }
  84. function append(parent, ...children2) {
  85. parent && parent.append(...children2);
  86. }
  87. function before(ref, ...nodes) {
  88. ref && ref.before(...nodes);
  89. }
  90. function matches(elm, selector) {
  91. return isHTMLElement(elm) && elm.matches(selector);
  92. }
  93. function children(parent, selector) {
  94. const children2 = parent ? slice(parent.children) : [];
  95. return selector ? children2.filter((child2) => matches(child2, selector)) : children2;
  96. }
  97. function child(parent, selector) {
  98. return selector ? children(parent, selector)[0] : parent.firstElementChild;
  99. }
  100. function closest(from, selector) {
  101. return from.closest(selector);
  102. }
  103. const assign = Object.assign;
  104. const ownKeys = Object.keys;
  105. function forOwn(object, iteratee, right) {
  106. if (object) {
  107. (right ? ownKeys(object).reverse() : ownKeys(object)).forEach((key) => {
  108. key !== "__proto__" && iteratee(object[key], key);
  109. });
  110. }
  111. return object;
  112. }
  113. function merge(object, ...sources) {
  114. sources.forEach((source) => {
  115. forOwn(source, (value, key) => {
  116. if (isArray(value)) {
  117. value = value.slice();
  118. } else if (isObject(value)) {
  119. value = merge({}, isObject(object[key]) ? object[key] : {}, value);
  120. }
  121. object[key] = value;
  122. });
  123. });
  124. return object;
  125. }
  126. function omit(object, keys) {
  127. forEach(keys || ownKeys(object), (key) => {
  128. delete object[key];
  129. });
  130. return object;
  131. }
  132. function removeAttribute(elms, attrs) {
  133. forEach(elms, (elm) => {
  134. forEach(attrs, (attr) => {
  135. elm && elm.removeAttribute(attr);
  136. });
  137. });
  138. }
  139. function setAttribute(elms, attrs, value) {
  140. if (isObject(attrs)) {
  141. forOwn(attrs, (value2, name) => {
  142. setAttribute(elms, name, value2);
  143. });
  144. } else {
  145. forEach(elms, (elm) => {
  146. isNull(value) || value === "" ? removeAttribute(elm, attrs) : elm.setAttribute(attrs, String(value));
  147. });
  148. }
  149. }
  150. function create(tag2, attrs, parent) {
  151. const elm = document.createElement(tag2);
  152. if (attrs) {
  153. isString(attrs) ? addClass(elm, attrs) : setAttribute(elm, attrs);
  154. }
  155. parent && append(parent, elm);
  156. return elm;
  157. }
  158. function style(elm, prop, value) {
  159. if (isUndefined(value)) {
  160. return getComputedStyle(elm)[prop];
  161. }
  162. if (!isNull(value)) {
  163. elm.style[prop] = `${value}`;
  164. }
  165. }
  166. function display(elm, display2) {
  167. style(elm, "display", display2);
  168. }
  169. function focus(elm) {
  170. isHTMLElement(elm) && elm.focus({ preventScroll: true });
  171. }
  172. function getAttribute(elm, attr) {
  173. return elm.getAttribute(attr);
  174. }
  175. function hasClass(elm, className) {
  176. return !!elm && elm.classList.contains(className);
  177. }
  178. function rect(target) {
  179. return target.getBoundingClientRect();
  180. }
  181. function removeNode(nodes) {
  182. forEach(nodes, (node) => {
  183. if (node && node.parentNode) {
  184. node.parentNode.removeChild(node);
  185. }
  186. });
  187. }
  188. function parseHtml(html) {
  189. return child(new DOMParser().parseFromString(html, "text/html").body);
  190. }
  191. function prevent(e, stopPropagation) {
  192. e.preventDefault();
  193. if (stopPropagation) {
  194. e.stopPropagation();
  195. e.stopImmediatePropagation();
  196. }
  197. }
  198. function query(parent, selector) {
  199. return parent && parent.querySelector(selector);
  200. }
  201. function queryAll(parent, selector) {
  202. return selector && parent ? slice(parent.querySelectorAll(selector)) : [];
  203. }
  204. function removeClass(elm, classes) {
  205. toggleClass(elm, classes, false);
  206. }
  207. function timeOf(e) {
  208. return e.timeStamp;
  209. }
  210. function unit(value) {
  211. return isString(value) ? value : value ? `${value}px` : "";
  212. }
  213. const { min, max, floor, ceil, abs, sign } = Math;
  214. function approximatelyEqual(x, y, epsilon) {
  215. return abs(x - y) < epsilon;
  216. }
  217. function between(number, x, y, exclusive) {
  218. const minimum = min(x, y);
  219. const maximum = max(x, y);
  220. return exclusive ? minimum < number && number < maximum : minimum <= number && number <= maximum;
  221. }
  222. function clamp(number, x, y) {
  223. const minimum = min(x, y);
  224. const maximum = max(x, y);
  225. return min(max(minimum, number), maximum);
  226. }
  227. function camelToKebab(string) {
  228. return string.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
  229. }
  230. function format(string, replacements) {
  231. forEach(replacements, (replacement) => {
  232. string = string.replace("%s", `${replacement}`);
  233. });
  234. return string;
  235. }
  236. function pad(number, length = 2) {
  237. return `${number}`.padStart(length, "0");
  238. }
  239. const ids = {};
  240. function uniqueId(prefix) {
  241. return `${prefix}${pad(ids[prefix] = (ids[prefix] || 0) + 1)}`;
  242. }
  243. function forEachEvent(events, iteratee) {
  244. forEach(events, (event) => {
  245. isString(event) && event.split(" ").forEach(iteratee);
  246. });
  247. }
  248. function arrayRemove(array, predicate) {
  249. const removed = [];
  250. for (let i = array.length - 1; i >= 0; i--) {
  251. if (predicate(array[i])) {
  252. removed.push(...array.splice(i, 1));
  253. }
  254. }
  255. return removed;
  256. }
  257. function EventBinder(removersRef) {
  258. const removers = removersRef || [];
  259. const key = removersRef ? {} : void 0;
  260. function bind(target, events, callback, options) {
  261. forEachEvent(events, (event) => {
  262. target.addEventListener(event, callback, options);
  263. removers.push([target.removeEventListener.bind(target, event, callback, options), key]);
  264. });
  265. }
  266. function destroy() {
  267. if (key) {
  268. arrayRemove(removers, (remover) => remover[1] === key).forEach((remover) => {
  269. remover[0]();
  270. });
  271. } else {
  272. removers.forEach((remover) => {
  273. remover[0]();
  274. });
  275. empty(removers);
  276. }
  277. }
  278. return {
  279. bind,
  280. create: apply(EventBinder, removers),
  281. destroy
  282. };
  283. }
  284. function EventBus(listenersRef) {
  285. const listeners = listenersRef || [];
  286. const key = listenersRef ? {} : void 0;
  287. function on(events, callback) {
  288. forEachEvent(events, (event) => {
  289. listeners.push([event, callback, key]);
  290. });
  291. }
  292. function off(events, callback) {
  293. forEachEvent(events, (event) => {
  294. arrayRemove(listeners, (listener) => {
  295. return listener[0] === event && (!callback || listener[1] === callback) && listener[2] === key;
  296. });
  297. });
  298. }
  299. function emit(event, ...args) {
  300. listeners.slice().forEach((listener) => {
  301. listener[0] === event && listener[1](...args);
  302. });
  303. }
  304. function destroy() {
  305. if (key) {
  306. arrayRemove(listeners, (listener) => listener[2] === key);
  307. } else {
  308. empty(listeners);
  309. }
  310. }
  311. return {
  312. on,
  313. off,
  314. emit,
  315. create: apply(EventBus, listeners),
  316. destroy
  317. };
  318. }
  319. function EventInterface(binder = EventBinder(), bus = EventBus()) {
  320. function create2() {
  321. return EventInterface(binder.create(), bus.create());
  322. }
  323. function destroy() {
  324. binder.destroy();
  325. bus.destroy();
  326. }
  327. return assign({}, binder, bus, { create: create2, destroy });
  328. }
  329. function RequestInterval(interval, onInterval, onUpdate, limit) {
  330. const { now } = Date;
  331. let startTime;
  332. let rate = 0;
  333. let id;
  334. let paused = true;
  335. let count = 0;
  336. function update() {
  337. if (!paused) {
  338. rate = interval ? min((now() - startTime) / interval, 1) : 1;
  339. onUpdate && onUpdate(rate);
  340. if (rate >= 1) {
  341. onInterval();
  342. startTime = now();
  343. if (limit && ++count >= limit) {
  344. return pause();
  345. }
  346. }
  347. id = raf(update);
  348. }
  349. }
  350. function start(resume) {
  351. resume || cancel();
  352. startTime = now() - (resume ? rate * interval : 0);
  353. paused = false;
  354. id = raf(update);
  355. }
  356. function pause() {
  357. paused = true;
  358. }
  359. function rewind() {
  360. startTime = now();
  361. rate = 0;
  362. if (onUpdate) {
  363. onUpdate(rate);
  364. }
  365. }
  366. function cancel() {
  367. id && cancelAnimationFrame(id);
  368. rate = 0;
  369. id = 0;
  370. paused = true;
  371. }
  372. function set(time) {
  373. interval = time;
  374. }
  375. function isPaused() {
  376. return paused;
  377. }
  378. return {
  379. start,
  380. rewind,
  381. pause,
  382. cancel,
  383. set,
  384. isPaused
  385. };
  386. }
  387. function State(initialState) {
  388. let state = initialState;
  389. function set(value) {
  390. state = value;
  391. }
  392. function is(states) {
  393. return includes(toArray(states), state);
  394. }
  395. return { set, is };
  396. }
  397. function Throttle(func, duration) {
  398. const interval = RequestInterval(duration || 0, func, void 0, 1);
  399. return () => {
  400. interval.isPaused() && interval.start();
  401. };
  402. }
  403. const EVENT_MOUNTED = "mounted";
  404. const EVENT_READY = "ready";
  405. const EVENT_MOVE = "move";
  406. const EVENT_MOVED = "moved";
  407. const EVENT_CLICK = "click";
  408. const EVENT_ACTIVE = "active";
  409. const EVENT_INACTIVE = "inactive";
  410. const EVENT_VISIBLE = "visible";
  411. const EVENT_HIDDEN = "hidden";
  412. const EVENT_REFRESH = "refresh";
  413. const EVENT_UPDATED = "updated";
  414. const EVENT_RESIZE = "resize";
  415. const EVENT_RESIZED = "resized";
  416. const EVENT_DRAG = "drag";
  417. const EVENT_DRAGGING = "dragging";
  418. const EVENT_DRAGGED = "dragged";
  419. const EVENT_SCROLL = "scroll";
  420. const EVENT_SCROLLING = "scrolling";
  421. const EVENT_SCROLLED = "scrolled";
  422. const EVENT_OVERFLOW = "overflow";
  423. const EVENT_DESTROY = "destroy";
  424. const EVENT_ARROWS_MOUNTED = "arrows:mounted";
  425. const EVENT_ARROWS_UPDATED = "arrows:updated";
  426. const EVENT_PAGINATION_MOUNTED = "pagination:mounted";
  427. const EVENT_PAGINATION_UPDATED = "pagination:updated";
  428. const EVENT_NAVIGATION_MOUNTED = "navigation:mounted";
  429. const EVENT_AUTOPLAY_PLAY = "autoplay:play";
  430. const EVENT_AUTOPLAY_PLAYING = "autoplay:playing";
  431. const EVENT_AUTOPLAY_PAUSE = "autoplay:pause";
  432. const EVENT_LAZYLOAD_LOADED = "lazyload:loaded";
  433. const EVENT_LAZYLOAD_ERROR = "lazyload:error";
  434. const EVENT_SLIDE_KEYDOWN = "_sk";
  435. const EVENT_SHIFTED = "_sh";
  436. const EVENT_END_INDEX_CHANGED = "_ei";
  437. const NOT_OVERFLOW_KEY = "!overflow";
  438. const Breakpoints = (Splide, Components, options, event) => {
  439. const { state } = Splide;
  440. const breakpoints = options.breakpoints || {};
  441. const reducedMotion = options.reducedMotion || {};
  442. const binder = EventBinder();
  443. const entries = [];
  444. function setup() {
  445. const isMin = options.mediaQuery === "min";
  446. ownKeys(breakpoints).sort((n, m) => isMin ? +n - +m : +m - +n).forEach((key) => {
  447. if (key !== NOT_OVERFLOW_KEY) {
  448. register(breakpoints[key], `(${isMin ? "min" : "max"}-width:${key}px)`);
  449. }
  450. });
  451. if (breakpoints[NOT_OVERFLOW_KEY]) {
  452. entries.push([breakpoints[NOT_OVERFLOW_KEY], () => Components.Layout && !Components.Layout.isOverflow()]);
  453. event.on(EVENT_OVERFLOW, update);
  454. }
  455. register(reducedMotion, MEDIA_PREFERS_REDUCED_MOTION);
  456. update();
  457. }
  458. function destroy(completely) {
  459. if (completely) {
  460. binder.destroy();
  461. }
  462. }
  463. function register(options2, query) {
  464. const queryList = matchMedia(query);
  465. binder.bind(queryList, "change", update);
  466. entries.push([options2, () => queryList.matches]);
  467. }
  468. function update() {
  469. const destroyed = state.is(DESTROYED);
  470. const direction = options.direction;
  471. const merged = entries.reduce((merged2, entry) => {
  472. return merge(merged2, entry[1]() ? entry[0] : {});
  473. }, {});
  474. omit(options);
  475. set(merged, false, !state.is(CREATED));
  476. if (options.destroy) {
  477. Splide.destroy(options.destroy === "completely");
  478. } else if (destroyed) {
  479. destroy(true);
  480. Splide.mount();
  481. } else {
  482. direction !== options.direction && Splide.refresh();
  483. }
  484. }
  485. function reduce(enable) {
  486. if (matchMedia(MEDIA_PREFERS_REDUCED_MOTION).matches) {
  487. enable ? merge(options, reducedMotion) : omit(options, ownKeys(reducedMotion));
  488. }
  489. }
  490. function set(opts, base, notify) {
  491. merge(options, opts);
  492. base && merge(Object.getPrototypeOf(options), opts);
  493. if (notify) {
  494. Splide.emit(EVENT_UPDATED, options);
  495. }
  496. }
  497. return {
  498. setup,
  499. destroy,
  500. reduce,
  501. set
  502. };
  503. };
  504. const ARROW = "Arrow";
  505. const ARROW_LEFT = `${ARROW}Left`;
  506. const ARROW_RIGHT = `${ARROW}Right`;
  507. const ARROW_UP = `${ARROW}Up`;
  508. const ARROW_DOWN = `${ARROW}Down`;
  509. const LTR = "ltr";
  510. const RTL = "rtl";
  511. const TTB = "ttb";
  512. const ORIENTATION_MAP = {
  513. width: ["height"],
  514. left: ["top", "right"],
  515. right: ["bottom", "left"],
  516. x: ["y"],
  517. X: ["Y"],
  518. Y: ["X"],
  519. ArrowLeft: [ARROW_UP, ARROW_RIGHT],
  520. ArrowRight: [ARROW_DOWN, ARROW_LEFT]
  521. };
  522. const Direction = (Splide2, Components2, options) => {
  523. function resolve(prop, axisOnly, direction = options.direction) {
  524. const index = direction === RTL && !axisOnly ? 1 : direction === TTB ? 0 : -1;
  525. return ORIENTATION_MAP[prop] && ORIENTATION_MAP[prop][index] || prop.replace(/width|left|right/i, (match, offset) => {
  526. const replacement = ORIENTATION_MAP[match.toLowerCase()][index] || match;
  527. return offset > 0 ? replacement.charAt(0).toUpperCase() + replacement.slice(1) : replacement;
  528. });
  529. }
  530. function orient(value) {
  531. return value * (options.direction === RTL ? 1 : -1);
  532. }
  533. return {
  534. resolve,
  535. orient,
  536. left: apply(resolve, "left"),
  537. right: apply(resolve, "right"),
  538. width: apply(resolve, "width")
  539. };
  540. };
  541. const ROLE = "role";
  542. const TAB_INDEX = "tabindex";
  543. const DISABLED = "disabled";
  544. const ARIA_PREFIX = "aria-";
  545. const ARIA_CONTROLS = `${ARIA_PREFIX}controls`;
  546. const ARIA_CURRENT = `${ARIA_PREFIX}current`;
  547. const ARIA_SELECTED = `${ARIA_PREFIX}selected`;
  548. const ARIA_LABEL = `${ARIA_PREFIX}label`;
  549. const ARIA_LABELLEDBY = `${ARIA_PREFIX}labelledby`;
  550. const ARIA_HIDDEN = `${ARIA_PREFIX}hidden`;
  551. const ARIA_ORIENTATION = `${ARIA_PREFIX}orientation`;
  552. const ARIA_ROLEDESCRIPTION = `${ARIA_PREFIX}roledescription`;
  553. const ARIA_LIVE = `${ARIA_PREFIX}live`;
  554. const ARIA_BUSY = `${ARIA_PREFIX}busy`;
  555. const ARIA_ATOMIC = `${ARIA_PREFIX}atomic`;
  556. const ALL_ATTRIBUTES = [
  557. ROLE,
  558. TAB_INDEX,
  559. DISABLED,
  560. ARIA_CONTROLS,
  561. ARIA_CURRENT,
  562. ARIA_LABEL,
  563. ARIA_LABELLEDBY,
  564. ARIA_HIDDEN,
  565. ARIA_ORIENTATION,
  566. ARIA_ROLEDESCRIPTION
  567. ];
  568. const PROJECT_CODE = "splide";
  569. const DATA_ATTRIBUTE = `data-${PROJECT_CODE}`;
  570. const CLASS_PREFIX = `${PROJECT_CODE}__`;
  571. const STATUS_CLASS_PREFIX = "is-";
  572. const CLASS_ROOT = PROJECT_CODE;
  573. const CLASS_TRACK = `${CLASS_PREFIX}track`;
  574. const CLASS_LIST = `${CLASS_PREFIX}list`;
  575. const CLASS_SLIDE = `${CLASS_PREFIX}slide`;
  576. const CLASS_CLONE = `${CLASS_SLIDE}--clone`;
  577. const CLASS_CONTAINER = `${CLASS_SLIDE}__container`;
  578. const CLASS_ARROWS = `${CLASS_PREFIX}arrows`;
  579. const CLASS_ARROW = `${CLASS_PREFIX}arrow`;
  580. const CLASS_ARROW_PREV = `${CLASS_ARROW}--prev`;
  581. const CLASS_ARROW_NEXT = `${CLASS_ARROW}--next`;
  582. const CLASS_PAGINATION = `${CLASS_PREFIX}pagination`;
  583. const CLASS_PAGINATION_PAGE = `${CLASS_PAGINATION}__page`;
  584. const CLASS_PROGRESS = `${CLASS_PREFIX}progress`;
  585. const CLASS_PROGRESS_BAR = `${CLASS_PROGRESS}__bar`;
  586. const CLASS_TOGGLE = `${CLASS_PREFIX}toggle`;
  587. const CLASS_TOGGLE_PLAY = `${CLASS_TOGGLE}__play`;
  588. const CLASS_TOGGLE_PAUSE = `${CLASS_TOGGLE}__pause`;
  589. const CLASS_SPINNER = `${CLASS_PREFIX}spinner`;
  590. const CLASS_SR = `${CLASS_PREFIX}sr`;
  591. const CLASS_INITIALIZED = `${STATUS_CLASS_PREFIX}initialized`;
  592. const CLASS_ACTIVE = `${STATUS_CLASS_PREFIX}active`;
  593. const CLASS_PREV = `${STATUS_CLASS_PREFIX}prev`;
  594. const CLASS_NEXT = `${STATUS_CLASS_PREFIX}next`;
  595. const CLASS_VISIBLE = `${STATUS_CLASS_PREFIX}visible`;
  596. const CLASS_LOADING = `${STATUS_CLASS_PREFIX}loading`;
  597. const CLASS_FOCUS_IN = `${STATUS_CLASS_PREFIX}focus-in`;
  598. const CLASS_OVERFLOW = `${STATUS_CLASS_PREFIX}overflow`;
  599. const STATUS_CLASSES = [
  600. CLASS_ACTIVE,
  601. CLASS_VISIBLE,
  602. CLASS_PREV,
  603. CLASS_NEXT,
  604. CLASS_LOADING,
  605. CLASS_FOCUS_IN,
  606. CLASS_OVERFLOW
  607. ];
  608. const CLASSES = {
  609. slide: CLASS_SLIDE,
  610. clone: CLASS_CLONE,
  611. arrows: CLASS_ARROWS,
  612. arrow: CLASS_ARROW,
  613. prev: CLASS_ARROW_PREV,
  614. next: CLASS_ARROW_NEXT,
  615. pagination: CLASS_PAGINATION,
  616. page: CLASS_PAGINATION_PAGE,
  617. spinner: CLASS_SPINNER
  618. };
  619. function assert(condition, message) {
  620. if (!condition) {
  621. throw new Error(`[${PROJECT_CODE}] ${message || ""}`);
  622. }
  623. }
  624. const FRICTION = 5;
  625. const LOG_INTERVAL = 200;
  626. const POINTER_DOWN_EVENTS = "touchstart mousedown";
  627. const POINTER_MOVE_EVENTS = "touchmove mousemove";
  628. const POINTER_UP_EVENTS = "touchend touchcancel mouseup click";
  629. const Elements = (Splide, Components, options, event) => {
  630. const { on, bind } = event;
  631. const { root } = Splide;
  632. const { i18n } = options;
  633. const elements = {};
  634. const slides = [];
  635. let rootClasses = [];
  636. let trackClasses = [];
  637. let track;
  638. let list;
  639. let isUsingKey;
  640. function setup() {
  641. collect();
  642. init();
  643. update();
  644. }
  645. function mount() {
  646. on(EVENT_REFRESH, destroy);
  647. on(EVENT_REFRESH, setup);
  648. on(EVENT_UPDATED, update);
  649. bind(document, `${POINTER_DOWN_EVENTS} keydown`, (e) => {
  650. isUsingKey = e.type === "keydown";
  651. }, { capture: true });
  652. bind(root, "focusin", () => {
  653. toggleClass(root, CLASS_FOCUS_IN, !!isUsingKey);
  654. });
  655. }
  656. function destroy(completely) {
  657. const attrs = ALL_ATTRIBUTES.concat("style");
  658. empty(slides);
  659. removeClass(root, rootClasses);
  660. removeClass(track, trackClasses);
  661. removeAttribute([track, list], attrs);
  662. removeAttribute(root, completely ? attrs : ["style", ARIA_ROLEDESCRIPTION]);
  663. }
  664. function update() {
  665. removeClass(root, rootClasses);
  666. removeClass(track, trackClasses);
  667. rootClasses = getClasses(CLASS_ROOT);
  668. trackClasses = getClasses(CLASS_TRACK);
  669. addClass(root, rootClasses);
  670. addClass(track, trackClasses);
  671. setAttribute(root, ARIA_LABEL, options.label);
  672. setAttribute(root, ARIA_LABELLEDBY, options.labelledby);
  673. }
  674. function collect() {
  675. track = find(CLASS_TRACK);
  676. list = child(track, `.${CLASS_LIST}`);
  677. assert(track && list, "A track/list element is missing.");
  678. push(slides, children(list, `.${CLASS_SLIDE}:not(.${CLASS_CLONE})`));
  679. assign(elements, {
  680. root,
  681. track,
  682. list,
  683. slides,
  684. arrows: find(CLASS_ARROWS),
  685. pagination: find(CLASS_PAGINATION),
  686. prev: find(CLASS_ARROW_PREV),
  687. next: find(CLASS_ARROW_NEXT),
  688. bar: find(CLASS_PROGRESS_BAR),
  689. toggle: find(CLASS_TOGGLE)
  690. });
  691. }
  692. function init() {
  693. const { role = "region" } = options;
  694. const id = root.id || uniqueId(PROJECT_CODE);
  695. root.id = id;
  696. track.id = track.id || `${id}-track`;
  697. list.id = list.id || `${id}-list`;
  698. if (!getAttribute(root, ROLE) && root.tagName !== "SECTION" && role) {
  699. setAttribute(root, ROLE, role);
  700. }
  701. setAttribute(root, ARIA_ROLEDESCRIPTION, i18n.carousel);
  702. setAttribute(list, ROLE, "presentation");
  703. }
  704. function find(className) {
  705. const elm = query(root, `.${className}`);
  706. return elm && closest(elm, `.${CLASS_ROOT}`) === root ? elm : void 0;
  707. }
  708. function getClasses(base) {
  709. return [
  710. `${base}--${options.type}`,
  711. `${base}--${options.direction}`,
  712. options.drag && `${base}--draggable`,
  713. options.isNavigation && `${base}--nav`,
  714. base === CLASS_ROOT && CLASS_ACTIVE
  715. ];
  716. }
  717. return assign(elements, {
  718. setup,
  719. mount,
  720. destroy
  721. });
  722. };
  723. const SLIDE = "slide";
  724. const LOOP = "loop";
  725. const FADE = "fade";
  726. const Slide$1 = (Splide2, index, slideIndex, slide) => {
  727. const event = Splide2.event.create();
  728. const { on, emit, bind } = event;
  729. const { Components, root, options } = Splide2;
  730. const { isNavigation, updateOnMove, i18n, pagination, slideFocus } = options;
  731. const { Elements } = Components;
  732. const { resolve } = Components.Direction;
  733. const styles = getAttribute(slide, "style");
  734. const label = getAttribute(slide, ARIA_LABEL);
  735. const isClone = slideIndex > -1;
  736. const container = child(slide, `.${CLASS_CONTAINER}`);
  737. let destroyed;
  738. function mount() {
  739. if (!isClone) {
  740. slide.id = `${root.id}-slide${pad(index + 1)}`;
  741. setAttribute(slide, ROLE, pagination ? "tabpanel" : "group");
  742. setAttribute(slide, ARIA_ROLEDESCRIPTION, i18n.slide);
  743. setAttribute(slide, ARIA_LABEL, label || format(i18n.slideLabel, [index + 1, Splide2.length]));
  744. }
  745. listen();
  746. }
  747. function listen() {
  748. bind(slide, "click", apply(emit, EVENT_CLICK, self));
  749. bind(slide, "keydown", apply(emit, EVENT_SLIDE_KEYDOWN, self));
  750. on([EVENT_MOVED, EVENT_SHIFTED, EVENT_SCROLLED], update);
  751. on(EVENT_NAVIGATION_MOUNTED, initNavigation);
  752. if (updateOnMove) {
  753. on(EVENT_MOVE, onMove);
  754. }
  755. }
  756. function destroy() {
  757. destroyed = true;
  758. event.destroy();
  759. removeClass(slide, STATUS_CLASSES);
  760. removeAttribute(slide, ALL_ATTRIBUTES);
  761. setAttribute(slide, "style", styles);
  762. setAttribute(slide, ARIA_LABEL, label || "");
  763. }
  764. function initNavigation() {
  765. const controls = Splide2.splides.map((target) => {
  766. const Slide2 = target.splide.Components.Slides.getAt(index);
  767. return Slide2 ? Slide2.slide.id : "";
  768. }).join(" ");
  769. setAttribute(slide, ARIA_LABEL, format(i18n.slideX, (isClone ? slideIndex : index) + 1));
  770. setAttribute(slide, ARIA_CONTROLS, controls);
  771. setAttribute(slide, ROLE, slideFocus ? "button" : "");
  772. slideFocus && removeAttribute(slide, ARIA_ROLEDESCRIPTION);
  773. }
  774. function onMove() {
  775. if (!destroyed) {
  776. update();
  777. }
  778. }
  779. function update() {
  780. if (!destroyed) {
  781. const { index: curr } = Splide2;
  782. updateActivity();
  783. updateVisibility();
  784. toggleClass(slide, CLASS_PREV, index === curr - 1);
  785. toggleClass(slide, CLASS_NEXT, index === curr + 1);
  786. }
  787. }
  788. function updateActivity() {
  789. const active = isActive();
  790. if (active !== hasClass(slide, CLASS_ACTIVE)) {
  791. toggleClass(slide, CLASS_ACTIVE, active);
  792. setAttribute(slide, ARIA_CURRENT, isNavigation && active || "");
  793. emit(active ? EVENT_ACTIVE : EVENT_INACTIVE, self);
  794. }
  795. }
  796. function updateVisibility() {
  797. const visible = isVisible();
  798. const hidden = !visible && (!isActive() || isClone);
  799. if (!Splide2.state.is([MOVING, SCROLLING])) {
  800. setAttribute(slide, ARIA_HIDDEN, hidden || "");
  801. }
  802. setAttribute(queryAll(slide, options.focusableNodes || ""), TAB_INDEX, hidden ? -1 : "");
  803. if (slideFocus) {
  804. setAttribute(slide, TAB_INDEX, hidden ? -1 : 0);
  805. }
  806. if (visible !== hasClass(slide, CLASS_VISIBLE)) {
  807. toggleClass(slide, CLASS_VISIBLE, visible);
  808. emit(visible ? EVENT_VISIBLE : EVENT_HIDDEN, self);
  809. }
  810. if (!visible && document.activeElement === slide) {
  811. const Slide2 = Components.Slides.getAt(Splide2.index);
  812. Slide2 && focus(Slide2.slide);
  813. }
  814. }
  815. function style$1(prop, value, useContainer) {
  816. style(useContainer && container || slide, prop, value);
  817. }
  818. function isActive() {
  819. const { index: curr } = Splide2;
  820. const { cloneStatus = true } = options;
  821. return curr === index || cloneStatus && curr === slideIndex;
  822. }
  823. function isVisible(partial) {
  824. if (Splide2.is(FADE)) {
  825. return isActive();
  826. }
  827. const trackRect = rect(Elements.track);
  828. const slideRect = rect(slide);
  829. const left = resolve("left", true);
  830. const right = resolve("right", true);
  831. return floor(trackRect[left]) <= ceil(slideRect[partial ? right : left]) && floor(slideRect[partial ? left : right]) <= ceil(trackRect[right]);
  832. }
  833. function isWithin(from, distance) {
  834. let diff = abs(from - index);
  835. if (!isClone && (options.rewind || Splide2.is(LOOP))) {
  836. diff = min(diff, Splide2.length - diff);
  837. }
  838. return diff <= distance;
  839. }
  840. const self = {
  841. index,
  842. slideIndex,
  843. slide,
  844. container,
  845. isClone,
  846. mount,
  847. destroy,
  848. update,
  849. style: style$1,
  850. isVisible,
  851. isWithin
  852. };
  853. return self;
  854. };
  855. const Slides = (Splide, Components, options, event) => {
  856. const { on, emit, bind } = event;
  857. const { slides, list } = Components.Elements;
  858. const Slides2 = [];
  859. function mount() {
  860. init();
  861. on(EVENT_REFRESH, destroy);
  862. on(EVENT_REFRESH, init);
  863. }
  864. function init() {
  865. slides.forEach((slide, index) => {
  866. register(slide, index, -1);
  867. });
  868. }
  869. function destroy() {
  870. forEach$1((Slide2) => {
  871. Slide2.destroy();
  872. });
  873. empty(Slides2);
  874. }
  875. function update() {
  876. forEach$1((Slide2) => {
  877. Slide2.update();
  878. });
  879. }
  880. function register(slide, index, slideIndex) {
  881. const object = Slide$1(Splide, index, slideIndex, slide);
  882. object.mount();
  883. Slides2.push(object);
  884. Slides2.sort((Slide1, Slide2) => Slide1.index - Slide2.index);
  885. }
  886. function get(excludeClones) {
  887. return excludeClones ? filter((Slide2) => !Slide2.isClone) : Slides2;
  888. }
  889. function getIn(page) {
  890. const { Controller } = Components;
  891. const index = Controller.toIndex(page);
  892. const max = Controller.hasFocus() ? 1 : options.perPage;
  893. return filter((Slide2) => between(Slide2.index, index, index + max - 1));
  894. }
  895. function getAt(index) {
  896. return filter(index)[0];
  897. }
  898. function add(items, index) {
  899. forEach(items, (slide) => {
  900. if (isString(slide)) {
  901. slide = parseHtml(slide);
  902. }
  903. if (isHTMLElement(slide)) {
  904. const ref = slides[index];
  905. ref ? before(ref, slide) : append(list, slide);
  906. addClass(slide, options.classes.slide);
  907. observeImages(slide, apply(emit, EVENT_RESIZE));
  908. }
  909. });
  910. emit(EVENT_REFRESH);
  911. }
  912. function remove(matcher) {
  913. removeNode(filter(matcher).map((Slide2) => Slide2.slide));
  914. emit(EVENT_REFRESH);
  915. }
  916. function forEach$1(iteratee, excludeClones) {
  917. get(excludeClones).forEach(iteratee);
  918. }
  919. function filter(matcher) {
  920. return Slides2.filter(
  921. isFunction(matcher) ? matcher : (Slide2) => isString(matcher) ? matches(Slide2.slide, matcher) : includes(toArray(matcher), Slide2.index)
  922. );
  923. }
  924. function style(prop, value, useContainer) {
  925. forEach$1((Slide2) => {
  926. Slide2.style(prop, value, useContainer);
  927. });
  928. }
  929. function observeImages(elm, callback) {
  930. const images = queryAll(elm, "img");
  931. let { length } = images;
  932. if (length) {
  933. images.forEach((img) => {
  934. bind(img, "load error", () => {
  935. if (!--length) {
  936. callback();
  937. }
  938. });
  939. });
  940. } else {
  941. callback();
  942. }
  943. }
  944. function getLength(excludeClones) {
  945. return excludeClones ? slides.length : Slides2.length;
  946. }
  947. function isEnough() {
  948. return Slides2.length > options.perPage;
  949. }
  950. return {
  951. mount,
  952. destroy,
  953. update,
  954. register,
  955. get,
  956. getIn,
  957. getAt,
  958. add,
  959. remove,
  960. forEach: forEach$1,
  961. filter,
  962. style,
  963. getLength,
  964. isEnough
  965. };
  966. };
  967. const Layout = (Splide, Components, options, event) => {
  968. const { on, bind, emit } = event;
  969. const { Slides } = Components;
  970. const { resolve, left, right, width } = Components.Direction;
  971. const { root, track, list } = Components.Elements;
  972. const { getAt, style: styleSlides } = Slides;
  973. let vertical;
  974. let rootRect;
  975. let overflow;
  976. function mount() {
  977. init();
  978. bind(window, "resize load", Throttle(apply(emit, EVENT_RESIZE)));
  979. on([EVENT_UPDATED, EVENT_REFRESH], init);
  980. on(EVENT_RESIZE, resize);
  981. }
  982. function init() {
  983. vertical = options.direction === TTB;
  984. style(root, "maxWidth", unit(options.width));
  985. style(track, resolve("paddingLeft"), cssPadding(false));
  986. style(track, resolve("paddingRight"), cssPadding(true));
  987. resize(true);
  988. }
  989. function resize(force) {
  990. const newRect = rect(root);
  991. if (force || rootRect.width !== newRect.width || rootRect.height !== newRect.height) {
  992. style(track, "height", cssTrackHeight());
  993. styleSlides(resolve("marginRight"), unit(options.gap));
  994. styleSlides("width", cssSlideWidth());
  995. styleSlides("height", cssSlideHeight(), true);
  996. rootRect = newRect;
  997. emit(EVENT_RESIZED);
  998. if (overflow !== (overflow = isOverflow())) {
  999. toggleClass(root, CLASS_OVERFLOW, overflow);
  1000. emit(EVENT_OVERFLOW, overflow);
  1001. }
  1002. }
  1003. }
  1004. function cssPadding(rightPadding) {
  1005. const { padding } = options;
  1006. const prop = rightPadding ? right() : left();
  1007. return padding && unit(padding[prop] || (isObject(padding) ? 0 : padding)) || "0px";
  1008. }
  1009. function cssTrackHeight() {
  1010. let height = "";
  1011. if (vertical) {
  1012. height = cssHeight();
  1013. assert(height, "height or heightRatio is missing.");
  1014. height = `calc(${height} - ${cssPadding(false)} - ${cssPadding(true)})`;
  1015. }
  1016. return height;
  1017. }
  1018. function cssHeight() {
  1019. return unit(options.height || rect(list).width * options.heightRatio);
  1020. }
  1021. function cssSlideWidth() {
  1022. return options.autoWidth ? null : unit(options.fixedWidth) || (vertical ? "" : cssSlideSize());
  1023. }
  1024. function cssSlideHeight() {
  1025. return unit(options.fixedHeight) || (vertical ? options.autoHeight ? null : cssSlideSize() : cssHeight());
  1026. }
  1027. function cssSlideSize() {
  1028. const gap = unit(options.gap);
  1029. return `calc((100%${gap && ` + ${gap}`})/${options.perPage || 1}${gap && ` - ${gap}`})`;
  1030. }
  1031. function trackSize() {
  1032. return rect(track)[width()];
  1033. }
  1034. function listSize(full) {
  1035. return full ? list[resolve("scrollWidth")] : rect(list)[width()];
  1036. }
  1037. function slideSize(index = 0, withoutGap) {
  1038. const slide = getAt(index);
  1039. return (slide ? rect(slide.slide)[width()] : 0) + (withoutGap ? 0 : getGap());
  1040. }
  1041. function totalSize(index, withoutGap) {
  1042. const first = Components.Slides.get()[0];
  1043. const target = getAt(index);
  1044. return first && target ? abs(rect(target.slide)[right()] - rect(first.slide)[left()]) + (withoutGap ? 0 : getGap()) : 0;
  1045. }
  1046. function sliderSize(withoutGap) {
  1047. return totalSize(Splide.length - 1) - totalSize(0) + slideSize(0, withoutGap);
  1048. }
  1049. function getGap() {
  1050. const first = getAt(0);
  1051. const second = getAt(1);
  1052. if (first && second) {
  1053. const firstRect = rect(first.slide);
  1054. return abs(rect(second.slide)[left()] - firstRect[left()]) - firstRect[width()];
  1055. }
  1056. return 0;
  1057. }
  1058. function getPadding(right2) {
  1059. return parseFloat(style(
  1060. track,
  1061. resolve(`padding${right2 ? "Right" : "Left"}`)
  1062. )) || 0;
  1063. }
  1064. function isOverflow() {
  1065. return Splide.is(FADE) || sliderSize(true) > listSize();
  1066. }
  1067. return {
  1068. mount,
  1069. resize,
  1070. trackSize,
  1071. listSize,
  1072. slideSize,
  1073. sliderSize,
  1074. totalSize,
  1075. getPadding,
  1076. isOverflow
  1077. };
  1078. };
  1079. const MULTIPLIER = 2;
  1080. const Clones = (Splide, Components, options, event) => {
  1081. const { on } = event;
  1082. const { Elements, Slides, Layout: { resize, trackSize } } = Components;
  1083. const { resolve } = Components.Direction;
  1084. const clones = [];
  1085. let cloneCount;
  1086. function mount() {
  1087. on(EVENT_REFRESH, remount);
  1088. on([EVENT_UPDATED, EVENT_RESIZE], observe);
  1089. if (cloneCount = computeCloneCount()) {
  1090. generate(cloneCount);
  1091. resize(true);
  1092. }
  1093. }
  1094. function remount() {
  1095. destroy();
  1096. mount();
  1097. resize(true);
  1098. }
  1099. function destroy() {
  1100. removeNode(clones);
  1101. empty(clones);
  1102. event.destroy();
  1103. }
  1104. function observe() {
  1105. const count = computeCloneCount();
  1106. if (cloneCount !== count) {
  1107. if (cloneCount < count || !count) {
  1108. !count && Splide.go(0);
  1109. event.emit(EVENT_REFRESH);
  1110. }
  1111. }
  1112. }
  1113. function generate(count) {
  1114. const slides = Slides.get().slice();
  1115. const { length } = slides;
  1116. if (length) {
  1117. while (slides.length < count) {
  1118. push(slides, slides);
  1119. }
  1120. push(slides.slice(-count), slides.slice(0, count)).forEach((Slide, index) => {
  1121. const isHead = index < count;
  1122. const clone = cloneDeep(Slide.slide, index);
  1123. isHead ? before(slides[0].slide, clone) : append(Elements.list, clone);
  1124. push(clones, clone);
  1125. Slides.register(clone, index - count + (isHead ? 0 : length), Slide.index);
  1126. });
  1127. }
  1128. }
  1129. function cloneDeep(elm, index) {
  1130. const clone = elm.cloneNode(true);
  1131. addClass(clone, options.classes.clone);
  1132. clone.id = `${Splide.root.id}-clone${pad(index + 1)}`;
  1133. return clone;
  1134. }
  1135. function computeCloneCount() {
  1136. let { clones: clones2 } = options;
  1137. if (!Splide.is(LOOP)) {
  1138. clones2 = 0;
  1139. } else if (isUndefined(clones2)) {
  1140. const fixedSize = options[resolve("fixedWidth")] && Components.Layout.slideSize(0);
  1141. const fixedCount = fixedSize && ceil(trackSize() / fixedSize);
  1142. clones2 = fixedCount || options[resolve("autoWidth")] && Splide.length || options.perPage * MULTIPLIER;
  1143. }
  1144. return clones2;
  1145. }
  1146. return {
  1147. mount,
  1148. destroy
  1149. };
  1150. };
  1151. const Move = (Splide, Components, options, event) => {
  1152. const { on, emit } = event;
  1153. const { set } = Splide.state;
  1154. const { Slides } = Components;
  1155. const { slideSize, getPadding, listSize, sliderSize, totalSize, trackSize } = Components.Layout;
  1156. const { resolve, orient } = Components.Direction;
  1157. const { list, track } = Components.Elements;
  1158. let Transition;
  1159. let indices;
  1160. function mount() {
  1161. Transition = Components.Transition;
  1162. on([EVENT_MOUNTED, EVENT_RESIZED, EVENT_UPDATED, EVENT_REFRESH], reposition);
  1163. }
  1164. function reposition() {
  1165. if (!Components.Controller.isBusy()) {
  1166. Components.Scroll.cancel();
  1167. jump(Splide.index);
  1168. Slides.update();
  1169. }
  1170. }
  1171. function move(dest, index, prev, callback) {
  1172. const forward = dest > prev;
  1173. cancel();
  1174. if ((dest !== index || exceededLimit(forward)) && canShift(forward)) {
  1175. translate(shift(getPosition(), forward), true);
  1176. }
  1177. indices = [index, prev, dest];
  1178. set(MOVING);
  1179. emit(EVENT_MOVE, index, prev, dest);
  1180. Transition.start(index, () => {
  1181. set(IDLE);
  1182. emit(EVENT_MOVED, index, prev, dest);
  1183. callback && callback();
  1184. });
  1185. }
  1186. function jump(index) {
  1187. translate(toPosition(index));
  1188. }
  1189. function translate(position, preventLoop) {
  1190. if (!Splide.is(FADE)) {
  1191. const destination = preventLoop ? position : loop(position);
  1192. style(list, "transform", `translate${resolve("X")}(${destination}px)`);
  1193. position !== destination && emit(EVENT_SHIFTED);
  1194. }
  1195. }
  1196. function loop(position) {
  1197. if (Splide.is(LOOP)) {
  1198. const diff = orient(position) - orient(getPosition());
  1199. if (diff && exceededLimit(diff > 0, position)) {
  1200. position = shift(position, diff > 0);
  1201. }
  1202. }
  1203. return position;
  1204. }
  1205. function shift(position, backwards) {
  1206. const excess = position - getLimit(backwards);
  1207. const size = sliderSize();
  1208. position -= orient(size * (ceil(abs(excess) / size) || 1)) * (backwards ? 1 : -1);
  1209. return position;
  1210. }
  1211. function cancel() {
  1212. if (Splide.state.is(MOVING) && indices) {
  1213. translate(getPosition(), true);
  1214. Transition.cancel();
  1215. emit(EVENT_MOVED, ...indices);
  1216. }
  1217. }
  1218. function toIndex(position) {
  1219. const slides = Slides.get();
  1220. let index = 0;
  1221. let minDistance = Infinity;
  1222. for (let i = 0; i < slides.length; i++) {
  1223. const slideIndex = slides[i].index;
  1224. const distance = abs(toPosition(slideIndex) - position);
  1225. if (distance <= minDistance) {
  1226. minDistance = distance;
  1227. index = slideIndex;
  1228. } else {
  1229. break;
  1230. }
  1231. }
  1232. return index;
  1233. }
  1234. function toPosition(index) {
  1235. let position = orient(totalSize(index - 1) - offset(index));
  1236. if (options.trimSpace && Splide.is(SLIDE)) {
  1237. position = clamp(position, 0, orient(sliderSize(true) - listSize()));
  1238. }
  1239. return position;
  1240. }
  1241. function getPosition() {
  1242. const left = resolve("left");
  1243. return rect(list)[left] - rect(track)[left] + orient(getPadding(false));
  1244. }
  1245. function getRate() {
  1246. let rate;
  1247. if (Splide.is(FADE)) {
  1248. rate = Splide.index / (Splide.length - 1);
  1249. } else {
  1250. const isLoop = Splide.is(LOOP);
  1251. const position = orient(getPosition());
  1252. const min = orient(getLimit(false));
  1253. const max = orient(getLimit(true));
  1254. const size = sliderSize();
  1255. const curr = (position - min) % size;
  1256. const base = isLoop ? size : max - min;
  1257. rate = curr / base || 0;
  1258. if (isLoop && rate < 0) {
  1259. rate += 1;
  1260. }
  1261. }
  1262. return clamp(rate, 0, 1);
  1263. }
  1264. function offset(index) {
  1265. const { focus } = options;
  1266. return focus === "center" ? (listSize() - slideSize(index, true)) / 2 : +focus * slideSize(index) || 0;
  1267. }
  1268. function getLimit(max) {
  1269. return toPosition(max ? Components.Controller.getEnd() : 0);
  1270. }
  1271. function canShift(backwards) {
  1272. const padding = getPadding(false);
  1273. const shifted = orient(shift(getPosition(), backwards));
  1274. return backwards ? shifted >= padding : shifted <= listSize(true) - trackSize() + padding;
  1275. }
  1276. function exceededLimit(max, position = getPosition()) {
  1277. const exceededMin = max !== true && orient(position) < orient(getLimit(false));
  1278. const exceededMax = max !== false && orient(position) > orient(getLimit(true));
  1279. return exceededMin || exceededMax;
  1280. }
  1281. return {
  1282. mount,
  1283. move,
  1284. jump,
  1285. translate,
  1286. shift,
  1287. cancel,
  1288. toIndex,
  1289. toPosition,
  1290. getPosition,
  1291. getRate,
  1292. getLimit,
  1293. exceededLimit,
  1294. reposition,
  1295. canShift
  1296. };
  1297. };
  1298. const Controller = (Splide, Components, options, event) => {
  1299. const { on, emit } = event;
  1300. const { Move, Scroll } = Components;
  1301. const { getPosition, getLimit, toPosition } = Move;
  1302. const { isEnough, getLength } = Components.Slides;
  1303. const { omitEnd } = options;
  1304. const isLoop = Splide.is(LOOP);
  1305. const isSlide = Splide.is(SLIDE);
  1306. const getNext = apply(getAdjacent, false);
  1307. const getPrev = apply(getAdjacent, true);
  1308. let currIndex = options.start || 0;
  1309. let endIndex;
  1310. let prevIndex = currIndex;
  1311. let slideCount;
  1312. let perMove;
  1313. let perPage;
  1314. function mount() {
  1315. init();
  1316. on([EVENT_UPDATED, EVENT_REFRESH, EVENT_END_INDEX_CHANGED], init);
  1317. on(EVENT_RESIZED, onResized);
  1318. }
  1319. function init() {
  1320. slideCount = getLength(true);
  1321. perMove = options.perMove;
  1322. perPage = options.perPage;
  1323. endIndex = getEnd();
  1324. const end = omitEnd ? endIndex : slideCount - 1;
  1325. const index = clamp(currIndex, 0, end);
  1326. prevIndex = index;
  1327. if (index !== currIndex) {
  1328. currIndex = index;
  1329. Move.reposition();
  1330. }
  1331. }
  1332. function onResized() {
  1333. if (endIndex !== getEnd()) {
  1334. emit(EVENT_END_INDEX_CHANGED);
  1335. }
  1336. }
  1337. function go(control, callback) {
  1338. if (!isBusy()) {
  1339. const dest = parse(control);
  1340. const index = loop(dest);
  1341. if (canGo(dest, index)) {
  1342. Scroll.cancel();
  1343. setIndex(index);
  1344. Move.move(dest, index, prevIndex, callback);
  1345. }
  1346. }
  1347. }
  1348. function canGo(dest, index) {
  1349. const forward = dest > prevIndex;
  1350. return index > -1 && (index !== currIndex || !isMoving()) && (dest === index || Move.exceededLimit(!forward) || Move.canShift(forward));
  1351. }
  1352. function jump(control) {
  1353. const { set } = Components.Breakpoints;
  1354. const { speed } = options;
  1355. set({ speed: 0 });
  1356. go(control);
  1357. set({ speed });
  1358. }
  1359. function scroll(destination, duration, snap, callback) {
  1360. Scroll.scroll(destination, duration, snap, () => {
  1361. const index = loop(Move.toIndex(getPosition()));
  1362. setIndex(omitEnd ? min(index, endIndex) : index);
  1363. callback && callback();
  1364. });
  1365. }
  1366. function parse(control) {
  1367. let index = currIndex;
  1368. if (isString(control)) {
  1369. const [, indicator, number] = control.match(/([+\-<>])(\d+)?/) || [];
  1370. if (indicator === "+" || indicator === "-") {
  1371. index = computeDestIndex(currIndex + +`${indicator}${+number || 1}`, currIndex);
  1372. } else if (indicator === ">") {
  1373. index = number ? toIndex(+number) : getNext(true);
  1374. } else if (indicator === "<") {
  1375. index = getPrev(true);
  1376. }
  1377. } else {
  1378. index = isLoop ? control : clamp(control, 0, endIndex);
  1379. }
  1380. return index;
  1381. }
  1382. function getAdjacent(prev, destination) {
  1383. const number = perMove || (hasFocus() ? 1 : perPage);
  1384. const dest = computeDestIndex(currIndex + number * (prev ? -1 : 1), currIndex, !(perMove || hasFocus()));
  1385. if (dest === -1 && isSlide) {
  1386. if (!approximatelyEqual(getPosition(), getLimit(!prev), 1)) {
  1387. return prev ? 0 : endIndex;
  1388. }
  1389. }
  1390. return destination ? dest : loop(dest);
  1391. }
  1392. function computeDestIndex(dest, from, snapPage) {
  1393. if (isEnough() || hasFocus()) {
  1394. const index = computeMovableDestIndex(dest);
  1395. if (index !== dest) {
  1396. from = dest;
  1397. dest = index;
  1398. snapPage = false;
  1399. }
  1400. if (dest < 0 || dest > endIndex) {
  1401. if (!perMove && (between(0, dest, from, true) || between(endIndex, from, dest, true))) {
  1402. dest = toIndex(toPage(dest));
  1403. } else {
  1404. if (isLoop) {
  1405. dest = snapPage ? dest < 0 ? -(slideCount % perPage || perPage) : slideCount : dest;
  1406. } else if (options.rewind) {
  1407. dest = dest < 0 ? endIndex : 0;
  1408. } else {
  1409. dest = -1;
  1410. }
  1411. }
  1412. } else {
  1413. if (snapPage && dest !== from) {
  1414. dest = toIndex(toPage(from) + (dest < from ? -1 : 1));
  1415. }
  1416. }
  1417. } else {
  1418. dest = -1;
  1419. }
  1420. return dest;
  1421. }
  1422. function computeMovableDestIndex(dest) {
  1423. if (isSlide && options.trimSpace === "move" && dest !== currIndex) {
  1424. const position = getPosition();
  1425. while (position === toPosition(dest) && between(dest, 0, Splide.length - 1, !options.rewind)) {
  1426. dest < currIndex ? --dest : ++dest;
  1427. }
  1428. }
  1429. return dest;
  1430. }
  1431. function loop(index) {
  1432. return isLoop ? (index + slideCount) % slideCount || 0 : index;
  1433. }
  1434. function getEnd() {
  1435. let end = slideCount - (hasFocus() || isLoop && perMove ? 1 : perPage);
  1436. while (omitEnd && end-- > 0) {
  1437. if (toPosition(slideCount - 1) !== toPosition(end)) {
  1438. end++;
  1439. break;
  1440. }
  1441. }
  1442. return clamp(end, 0, slideCount - 1);
  1443. }
  1444. function toIndex(page) {
  1445. return clamp(hasFocus() ? page : perPage * page, 0, endIndex);
  1446. }
  1447. function toPage(index) {
  1448. return hasFocus() ? min(index, endIndex) : floor((index >= endIndex ? slideCount - 1 : index) / perPage);
  1449. }
  1450. function toDest(destination) {
  1451. const closest = Move.toIndex(destination);
  1452. return isSlide ? clamp(closest, 0, endIndex) : closest;
  1453. }
  1454. function setIndex(index) {
  1455. if (index !== currIndex) {
  1456. prevIndex = currIndex;
  1457. currIndex = index;
  1458. }
  1459. }
  1460. function getIndex(prev) {
  1461. return prev ? prevIndex : currIndex;
  1462. }
  1463. function hasFocus() {
  1464. return !isUndefined(options.focus) || options.isNavigation;
  1465. }
  1466. function isMoving() {
  1467. return Splide.state.is([MOVING, SCROLLING]);
  1468. }
  1469. function isBusy() {
  1470. return isMoving() && !!options.waitForTransition;
  1471. }
  1472. return {
  1473. mount,
  1474. go,
  1475. jump,
  1476. scroll,
  1477. getNext,
  1478. getPrev,
  1479. getAdjacent,
  1480. getEnd,
  1481. setIndex,
  1482. getIndex,
  1483. toIndex,
  1484. toPage,
  1485. toDest,
  1486. hasFocus,
  1487. isBusy
  1488. };
  1489. };
  1490. const XML_NAME_SPACE = "http://www.w3.org/2000/svg";
  1491. const PATH = "m15.5 0.932-4.3 4.38 14.5 14.6-14.5 14.5 4.3 4.4 14.6-14.6 4.4-4.3-4.4-4.4-14.6-14.6z";
  1492. const SIZE = 40;
  1493. const Arrows = (Splide, Components, options, event) => {
  1494. const { on, bind, emit } = event;
  1495. const { classes, i18n } = options;
  1496. const { Elements, Controller } = Components;
  1497. const { arrows: placeholder, track } = Elements;
  1498. let wrapper = placeholder;
  1499. let prev = Elements.prev;
  1500. let next = Elements.next;
  1501. let created;
  1502. let wrapperClasses;
  1503. const arrows = {};
  1504. function mount() {
  1505. init();
  1506. on(EVENT_UPDATED, remount);
  1507. }
  1508. function remount() {
  1509. destroy();
  1510. mount();
  1511. }
  1512. function init() {
  1513. const { arrows: enabled = true } = options;
  1514. if (enabled && !(prev && next)) {
  1515. createArrows();
  1516. }
  1517. if (prev && next) {
  1518. assign(arrows, { prev, next });
  1519. display(wrapper, enabled ? "" : "none");
  1520. addClass(wrapper, wrapperClasses = `${CLASS_ARROWS}--${options.direction}`);
  1521. if (enabled) {
  1522. listen();
  1523. update();
  1524. setAttribute([prev, next], ARIA_CONTROLS, track.id);
  1525. emit(EVENT_ARROWS_MOUNTED, prev, next);
  1526. }
  1527. }
  1528. }
  1529. function destroy() {
  1530. event.destroy();
  1531. removeClass(wrapper, wrapperClasses);
  1532. if (created) {
  1533. removeNode(placeholder ? [prev, next] : wrapper);
  1534. prev = next = null;
  1535. } else {
  1536. removeAttribute([prev, next], ALL_ATTRIBUTES);
  1537. }
  1538. }
  1539. function listen() {
  1540. on([EVENT_MOUNTED, EVENT_MOVED, EVENT_REFRESH, EVENT_SCROLLED, EVENT_END_INDEX_CHANGED], update);
  1541. bind(next, "click", apply(go, ">"));
  1542. bind(prev, "click", apply(go, "<"));
  1543. }
  1544. function go(control) {
  1545. Controller.go(control);
  1546. }
  1547. function createArrows() {
  1548. wrapper = placeholder || create("div", classes.arrows);
  1549. prev = createArrow(true);
  1550. next = createArrow(false);
  1551. created = true;
  1552. append(wrapper, prev, next);
  1553. !placeholder && before(track, wrapper);
  1554. }
  1555. function createArrow(prev2) {
  1556. const arrow = `<button class="${classes.arrow} ${prev2 ? classes.prev : classes.next}" type="button"><svg xmlns="${XML_NAME_SPACE}" viewBox="0 0 ${SIZE} ${SIZE}" width="${SIZE}" height="${SIZE}"><path d="${options.arrowPath || PATH}" />`;
  1557. return parseHtml(arrow);
  1558. }
  1559. function update() {
  1560. if (prev && next) {
  1561. const index = Splide.index;
  1562. const prevIndex = Controller.getPrev();
  1563. const nextIndex = Controller.getNext();
  1564. const prevLabel = prevIndex > -1 && index < prevIndex ? i18n.last : i18n.prev;
  1565. const nextLabel = nextIndex > -1 && index > nextIndex ? i18n.first : i18n.next;
  1566. prev.disabled = prevIndex < 0;
  1567. next.disabled = nextIndex < 0;
  1568. setAttribute(prev, ARIA_LABEL, prevLabel);
  1569. setAttribute(next, ARIA_LABEL, nextLabel);
  1570. emit(EVENT_ARROWS_UPDATED, prev, next, prevIndex, nextIndex);
  1571. }
  1572. }
  1573. return {
  1574. arrows,
  1575. mount,
  1576. destroy,
  1577. update
  1578. };
  1579. };
  1580. const INTERVAL_DATA_ATTRIBUTE = `${DATA_ATTRIBUTE}-interval`;
  1581. const Autoplay = (Splide, Components, options, event) => {
  1582. const { on, bind, emit } = event;
  1583. const { interval: duration = 5e3, pauseOnHover = true, pauseOnFocus = true, resetProgress = true } = options;
  1584. const interval = RequestInterval(duration, Splide.go.bind(Splide, ">"), onAnimationFrame);
  1585. const { isPaused } = interval;
  1586. const { Elements, Elements: { root, toggle } } = Components;
  1587. const { autoplay } = options;
  1588. let hovered;
  1589. let focused;
  1590. let stopped = autoplay === "pause";
  1591. function mount() {
  1592. if (autoplay) {
  1593. listen();
  1594. toggle && setAttribute(toggle, ARIA_CONTROLS, Elements.track.id);
  1595. stopped || play();
  1596. update();
  1597. }
  1598. }
  1599. function listen() {
  1600. if (pauseOnHover) {
  1601. bind(root, "mouseenter mouseleave", (e) => {
  1602. hovered = e.type === "mouseenter";
  1603. autoToggle();
  1604. });
  1605. }
  1606. if (pauseOnFocus) {
  1607. bind(root, "focusin focusout", (e) => {
  1608. focused = e.type === "focusin";
  1609. autoToggle();
  1610. });
  1611. }
  1612. if (toggle) {
  1613. bind(toggle, "click", () => {
  1614. stopped ? play() : pause(true);
  1615. });
  1616. }
  1617. on([EVENT_MOVE, EVENT_SCROLL, EVENT_REFRESH], interval.rewind);
  1618. on(EVENT_MOVE, onMove);
  1619. }
  1620. function play() {
  1621. if (isPaused() && Components.Slides.isEnough()) {
  1622. interval.start(!resetProgress);
  1623. focused = hovered = stopped = false;
  1624. update();
  1625. emit(EVENT_AUTOPLAY_PLAY);
  1626. }
  1627. }
  1628. function pause(stop = true) {
  1629. stopped = !!stop;
  1630. update();
  1631. if (!isPaused()) {
  1632. interval.pause();
  1633. emit(EVENT_AUTOPLAY_PAUSE);
  1634. }
  1635. }
  1636. function autoToggle() {
  1637. if (!stopped) {
  1638. hovered || focused ? pause(false) : play();
  1639. }
  1640. }
  1641. function update() {
  1642. if (toggle) {
  1643. toggleClass(toggle, CLASS_ACTIVE, !stopped);
  1644. setAttribute(toggle, ARIA_LABEL, options.i18n[stopped ? "play" : "pause"]);
  1645. }
  1646. }
  1647. function onAnimationFrame(rate) {
  1648. const { bar } = Elements;
  1649. bar && style(bar, "width", `${rate * 100}%`);
  1650. emit(EVENT_AUTOPLAY_PLAYING, rate);
  1651. }
  1652. function onMove(index) {
  1653. const Slide = Components.Slides.getAt(index);
  1654. interval.set(Slide && +getAttribute(Slide.slide, INTERVAL_DATA_ATTRIBUTE) || options.interval);
  1655. }
  1656. return {
  1657. mount,
  1658. destroy: interval.cancel,
  1659. play,
  1660. pause,
  1661. isPaused
  1662. };
  1663. };
  1664. const BOUNCE_DIFF_THRESHOLD = 10;
  1665. const BOUNCE_DURATION = 600;
  1666. const FRICTION_FACTOR = 0.6;
  1667. const BASE_VELOCITY = 1.5;
  1668. const MIN_DURATION = 800;
  1669. const Scroll = (Splide, Components, options, event) => {
  1670. const { on, emit } = event;
  1671. const { state: { set } } = Splide;
  1672. const { Move } = Components;
  1673. const { getPosition, getLimit, exceededLimit, translate } = Move;
  1674. const isSlide = Splide.is(SLIDE);
  1675. let interval;
  1676. let callback;
  1677. let friction = 1;
  1678. function mount() {
  1679. on(EVENT_MOVE, clear);
  1680. on([EVENT_UPDATED, EVENT_REFRESH], cancel);
  1681. }
  1682. function scroll(destination, duration, snap, onScrolled, noConstrain) {
  1683. clear();
  1684. const dest = computeDestination(destination, snap);
  1685. const from = getPosition();
  1686. const immediately = approximatelyEqual(from, dest, 1) || duration === 0;
  1687. friction = 1;
  1688. duration = immediately ? 0 : duration || max(abs(dest - from) / BASE_VELOCITY, MIN_DURATION);
  1689. callback = onScrolled;
  1690. interval = RequestInterval(duration, onEnd, apply(update, from, dest, noConstrain), 1);
  1691. set(SCROLLING);
  1692. emit(EVENT_SCROLL);
  1693. interval.start();
  1694. }
  1695. function computeDestination(destination, snap) {
  1696. if (snap) {
  1697. if (!isSlide || !exceededLimit()) {
  1698. const position = destination % Components.Layout.sliderSize();
  1699. const snapped = Move.toPosition(Components.Controller.toDest(position));
  1700. destination -= position - snapped;
  1701. }
  1702. }
  1703. return destination;
  1704. }
  1705. function onEnd() {
  1706. set(IDLE);
  1707. callback && callback();
  1708. emit(EVENT_SCROLLED);
  1709. }
  1710. function update(from, to, noConstrain, rate) {
  1711. const { easingFunc = (t) => 1 - Math.pow(1 - t, 4) } = options;
  1712. const position = getPosition();
  1713. const target = from + (to - from) * easingFunc(rate);
  1714. const diff = (target - position) * friction;
  1715. translate(position + diff);
  1716. emit(EVENT_SCROLLING);
  1717. if (isSlide && !noConstrain && exceededLimit()) {
  1718. friction *= FRICTION_FACTOR;
  1719. if (abs(diff) < BOUNCE_DIFF_THRESHOLD) {
  1720. scroll(getLimit(exceededLimit(true)), BOUNCE_DURATION, false, callback, true);
  1721. }
  1722. }
  1723. }
  1724. function clear() {
  1725. if (interval) {
  1726. interval.cancel();
  1727. }
  1728. }
  1729. function cancel() {
  1730. if (interval && !interval.isPaused()) {
  1731. clear();
  1732. onEnd();
  1733. }
  1734. }
  1735. return {
  1736. mount,
  1737. destroy: clear,
  1738. scroll,
  1739. cancel
  1740. };
  1741. };
  1742. const SCROLL_LISTENER_OPTIONS = { passive: false, capture: true };
  1743. const Drag = (Splide, Components, options, event) => {
  1744. const { on, emit, bind } = event;
  1745. const binder = event.create();
  1746. const { state } = Splide;
  1747. const { Move, Scroll, Controller, Elements: { track }, Breakpoints: { reduce } } = Components;
  1748. const { resolve, orient } = Components.Direction;
  1749. const { getPosition, exceededLimit } = Move;
  1750. let basePosition;
  1751. let baseEvent;
  1752. let prevBaseEvent;
  1753. let isFree;
  1754. let dragging;
  1755. let exceeded = false;
  1756. let clickPrevented;
  1757. let disabled;
  1758. let target;
  1759. function mount() {
  1760. bind(track, POINTER_MOVE_EVENTS, noop, SCROLL_LISTENER_OPTIONS);
  1761. bind(track, POINTER_UP_EVENTS, noop, SCROLL_LISTENER_OPTIONS);
  1762. bind(track, POINTER_DOWN_EVENTS, onPointerDown, SCROLL_LISTENER_OPTIONS);
  1763. bind(track, "click", onClick, { capture: true });
  1764. bind(track, "dragstart", prevent);
  1765. on([EVENT_MOUNTED, EVENT_UPDATED], init);
  1766. }
  1767. function init() {
  1768. const { drag } = options;
  1769. disable(!drag);
  1770. isFree = drag === "free";
  1771. }
  1772. function onPointerDown(e) {
  1773. clickPrevented = false;
  1774. if (!disabled) {
  1775. const isTouch = isTouchEvent(e);
  1776. if (isDraggable(e.target) && (isTouch || !e.button)) {
  1777. if (!Controller.isBusy()) {
  1778. target = isTouch ? track : window;
  1779. dragging = state.is([MOVING, SCROLLING]);
  1780. prevBaseEvent = null;
  1781. binder.bind(target, POINTER_MOVE_EVENTS, onPointerMove, SCROLL_LISTENER_OPTIONS);
  1782. binder.bind(target, POINTER_UP_EVENTS, onPointerUp, SCROLL_LISTENER_OPTIONS);
  1783. Move.cancel();
  1784. Scroll.cancel();
  1785. save(e);
  1786. } else {
  1787. prevent(e, true);
  1788. }
  1789. }
  1790. }
  1791. }
  1792. function onPointerMove(e) {
  1793. if (!state.is(DRAGGING)) {
  1794. state.set(DRAGGING);
  1795. emit(EVENT_DRAG);
  1796. }
  1797. if (shouldRelease(e)) {
  1798. return onPointerUp(e);
  1799. }
  1800. if (e.cancelable) {
  1801. if (dragging) {
  1802. Move.translate(basePosition + constrain(diffCoord(e)));
  1803. const expired = diffTime(e) > LOG_INTERVAL;
  1804. const hasExceeded = exceeded !== (exceeded = exceededLimit());
  1805. if (expired || hasExceeded) {
  1806. save(e);
  1807. }
  1808. clickPrevented = true;
  1809. emit(EVENT_DRAGGING);
  1810. prevent(e);
  1811. } else if (isSliderDirection(e)) {
  1812. dragging = shouldStart(e);
  1813. prevent(e);
  1814. }
  1815. }
  1816. }
  1817. function onPointerUp(e) {
  1818. if (state.is(DRAGGING)) {
  1819. state.set(IDLE);
  1820. emit(EVENT_DRAGGED);
  1821. }
  1822. if (dragging) {
  1823. move(e);
  1824. prevent(e);
  1825. }
  1826. binder.destroy();
  1827. dragging = false;
  1828. exceeded = false;
  1829. }
  1830. function onClick(e) {
  1831. if (!disabled && clickPrevented) {
  1832. prevent(e, true);
  1833. }
  1834. }
  1835. function save(e) {
  1836. prevBaseEvent = baseEvent;
  1837. baseEvent = e;
  1838. basePosition = getPosition();
  1839. }
  1840. function move(e) {
  1841. const { updateOnDragged = true } = options;
  1842. const velocity = computeVelocity(e);
  1843. const destination = computeDestination(velocity);
  1844. const rewind = options.rewind && options.rewindByDrag;
  1845. const scroll = updateOnDragged ? Controller.scroll : Scroll.scroll;
  1846. reduce(false);
  1847. if (isFree) {
  1848. scroll(destination, void 0, options.snap);
  1849. } else if (Splide.is(FADE)) {
  1850. Controller.go(orient(sign(velocity)) < 0 ? rewind ? "<" : "-" : rewind ? ">" : "+");
  1851. } else if (Splide.is(SLIDE) && exceeded && rewind) {
  1852. Controller.go(exceededLimit(true) ? ">" : "<");
  1853. } else {
  1854. Controller.go(Controller.toDest(destination));
  1855. }
  1856. reduce(true);
  1857. }
  1858. function shouldRelease(e) {
  1859. if (options.releaseTouch && Splide.is(SLIDE) && isTouchEvent(e)) {
  1860. const { index } = Splide;
  1861. const diff = diffCoord(e);
  1862. if (exceededLimit() || index === 0 && diff > 0 || index === Splide.length - 1 && diff < 0) {
  1863. return true;
  1864. }
  1865. }
  1866. return false;
  1867. }
  1868. function shouldStart(e) {
  1869. const { dragMinThreshold: thresholds } = options;
  1870. const isObj = isObject(thresholds);
  1871. const mouse = isObj && thresholds.mouse || 0;
  1872. const touch = (isObj ? thresholds.touch : +thresholds) || 10;
  1873. return abs(diffCoord(e)) > (isTouchEvent(e) ? touch : mouse);
  1874. }
  1875. function isSliderDirection(e) {
  1876. return abs(diffCoord(e)) > abs(diffCoord(e, true));
  1877. }
  1878. function computeVelocity(e) {
  1879. if (Splide.is(LOOP) || !exceeded) {
  1880. const time = diffTime(e);
  1881. if (time && time < LOG_INTERVAL) {
  1882. return diffCoord(e) / time;
  1883. }
  1884. }
  1885. return 0;
  1886. }
  1887. function computeDestination(velocity) {
  1888. return getPosition() + sign(velocity) * min(
  1889. abs(velocity) * (options.flickPower || 600),
  1890. isFree ? Infinity : Components.Layout.listSize() * (options.flickMaxPages || 1)
  1891. );
  1892. }
  1893. function diffCoord(e, orthogonal) {
  1894. return coordOf(e, orthogonal) - coordOf(getBaseEvent(e), orthogonal);
  1895. }
  1896. function diffTime(e) {
  1897. return timeOf(e) - timeOf(getBaseEvent(e));
  1898. }
  1899. function getBaseEvent(e) {
  1900. return baseEvent === e && prevBaseEvent || baseEvent;
  1901. }
  1902. function coordOf(e, orthogonal) {
  1903. return (isTouchEvent(e) ? e.changedTouches[0] : e)[`page${resolve(orthogonal ? "Y" : "X")}`];
  1904. }
  1905. function constrain(diff) {
  1906. return diff / (exceeded && Splide.is(SLIDE) ? FRICTION : 1);
  1907. }
  1908. function isDraggable(target2) {
  1909. const { noDrag } = options;
  1910. return !matches(target2, `.${CLASS_PAGINATION_PAGE}, .${CLASS_ARROW}`) && (!noDrag || !matches(target2, noDrag));
  1911. }
  1912. function isTouchEvent(e) {
  1913. return typeof TouchEvent !== "undefined" && e instanceof TouchEvent;
  1914. }
  1915. function isDragging() {
  1916. return dragging;
  1917. }
  1918. function disable(value) {
  1919. disabled = value;
  1920. }
  1921. return {
  1922. mount,
  1923. disable,
  1924. isDragging
  1925. };
  1926. };
  1927. const KEYBOARD_EVENT = "keydown";
  1928. const Keyboard = (Splide, Components, options, event) => {
  1929. const { destroy } = event;
  1930. const { resolve } = Components.Direction;
  1931. function mount() {
  1932. const { keyboard } = options;
  1933. destroy();
  1934. keyboard && event.bind(keyboard === "global" ? window : Splide.root, KEYBOARD_EVENT, onKeydown);
  1935. event.on(EVENT_UPDATED, mount);
  1936. }
  1937. function disable(value) {
  1938. value ? destroy() : mount();
  1939. }
  1940. function onKeydown(e) {
  1941. if (e.key === resolve(ARROW_LEFT)) {
  1942. Splide.go("<");
  1943. prevent(e, true);
  1944. } else if (e.key === resolve(ARROW_RIGHT)) {
  1945. Splide.go(">");
  1946. prevent(e, true);
  1947. }
  1948. }
  1949. return {
  1950. mount,
  1951. destroy,
  1952. disable
  1953. };
  1954. };
  1955. const SRC_DATA_ATTRIBUTE = `${DATA_ATTRIBUTE}-lazy`;
  1956. const SRCSET_DATA_ATTRIBUTE = `${SRC_DATA_ATTRIBUTE}-srcset`;
  1957. const IMAGE_SELECTOR = `[${SRC_DATA_ATTRIBUTE}], [${SRCSET_DATA_ATTRIBUTE}]`;
  1958. const LazyLoad = (Splide, Components, options, event) => {
  1959. const { on, off, bind, emit } = event;
  1960. const isSequential = options.lazyLoad === "sequential";
  1961. const events = [EVENT_MOVED, EVENT_SCROLLED];
  1962. let entries = [];
  1963. function mount() {
  1964. if (options.lazyLoad) {
  1965. init();
  1966. on(EVENT_REFRESH, init);
  1967. }
  1968. }
  1969. function init() {
  1970. empty(entries);
  1971. register();
  1972. if (isSequential) {
  1973. loadNext();
  1974. } else {
  1975. off(events);
  1976. on(events, check);
  1977. check();
  1978. }
  1979. }
  1980. function register() {
  1981. Components.Slides.forEach((Slide) => {
  1982. queryAll(Slide.slide, IMAGE_SELECTOR).forEach((img) => {
  1983. const src = getAttribute(img, SRC_DATA_ATTRIBUTE);
  1984. const srcset = getAttribute(img, SRCSET_DATA_ATTRIBUTE);
  1985. if (src !== img.src || srcset !== img.srcset) {
  1986. const parent = img.parentElement;
  1987. const spinner = child(parent, `.${CLASS_SPINNER}`) || create("span", options.classes.spinner, parent);
  1988. entries.push([img, Slide, spinner]);
  1989. img.src || display(img, "none");
  1990. }
  1991. });
  1992. });
  1993. }
  1994. function check() {
  1995. entries = entries.filter((data) => {
  1996. const distance = options.perPage * ((options.preloadPages || 1) + 1) - 1;
  1997. return data[1].isWithin(Splide.index, distance) ? load(data) : true;
  1998. });
  1999. entries.length || off(events);
  2000. }
  2001. function load(data) {
  2002. const [img] = data;
  2003. addClass(data[1].slide, CLASS_LOADING);
  2004. bind(img, "load error", apply(onLoad, data));
  2005. setAttribute(img, "src", getAttribute(img, SRC_DATA_ATTRIBUTE));
  2006. setAttribute(img, "srcset", getAttribute(img, SRCSET_DATA_ATTRIBUTE));
  2007. removeAttribute(img, [SRC_DATA_ATTRIBUTE, SRCSET_DATA_ATTRIBUTE]);
  2008. }
  2009. function onLoad(data, e) {
  2010. const [img, Slide] = data;
  2011. removeClass(Slide.slide, CLASS_LOADING);
  2012. if (e.type !== "error") {
  2013. removeNode(data[2]);
  2014. display(img, "");
  2015. emit(EVENT_LAZYLOAD_LOADED, img, Slide);
  2016. emit(EVENT_RESIZE);
  2017. } else {
  2018. emit(EVENT_LAZYLOAD_ERROR, img, Slide);
  2019. }
  2020. isSequential && loadNext();
  2021. }
  2022. function loadNext() {
  2023. entries.length && load(entries.shift());
  2024. }
  2025. return {
  2026. mount,
  2027. destroy: apply(empty, entries),
  2028. check
  2029. };
  2030. };
  2031. const Pagination = (Splide, Components, options, event) => {
  2032. const { on, emit, bind } = event;
  2033. const { Slides, Elements, Controller } = Components;
  2034. const { hasFocus, getIndex, go } = Controller;
  2035. const { resolve } = Components.Direction;
  2036. const { pagination: placeholder } = Elements;
  2037. const items = [];
  2038. let list;
  2039. let paginationClasses;
  2040. function mount() {
  2041. destroy();
  2042. on([EVENT_UPDATED, EVENT_REFRESH, EVENT_END_INDEX_CHANGED], mount);
  2043. const { pagination: enabled = true } = options;
  2044. placeholder && display(placeholder, enabled ? "" : "none");
  2045. if (enabled) {
  2046. on([EVENT_MOVE, EVENT_SCROLL, EVENT_SCROLLED], update);
  2047. createPagination();
  2048. update();
  2049. emit(EVENT_PAGINATION_MOUNTED, { list, items }, getAt(Splide.index));
  2050. }
  2051. }
  2052. function destroy() {
  2053. if (list) {
  2054. removeNode(placeholder ? slice(list.children) : list);
  2055. removeClass(list, paginationClasses);
  2056. empty(items);
  2057. list = null;
  2058. }
  2059. event.destroy();
  2060. }
  2061. function createPagination() {
  2062. const { length } = Splide;
  2063. const { classes, i18n, perPage, paginationKeyboard = true } = options;
  2064. const max = hasFocus() ? Controller.getEnd() + 1 : ceil(length / perPage);
  2065. const dir = getDirection();
  2066. list = placeholder || create("ul", classes.pagination, Elements.track.parentElement);
  2067. addClass(list, paginationClasses = `${CLASS_PAGINATION}--${dir}`);
  2068. setAttribute(list, ROLE, "tablist");
  2069. setAttribute(list, ARIA_LABEL, i18n.select);
  2070. setAttribute(list, ARIA_ORIENTATION, dir === TTB ? "vertical" : "");
  2071. for (let i = 0; i < max; i++) {
  2072. const li = create("li", null, list);
  2073. const button = create("button", { class: classes.page, type: "button" }, li);
  2074. const controls = Slides.getIn(i).map((Slide) => Slide.slide.id);
  2075. const text = !hasFocus() && perPage > 1 ? i18n.pageX : i18n.slideX;
  2076. bind(button, "click", () => {
  2077. go(`>${i}`);
  2078. });
  2079. paginationKeyboard && bind(button, "keydown", apply(onKeydown, i));
  2080. setAttribute(li, ROLE, "presentation");
  2081. setAttribute(button, ROLE, "tab");
  2082. setAttribute(button, ARIA_CONTROLS, controls.join(" "));
  2083. setAttribute(button, ARIA_LABEL, format(text, i + 1));
  2084. setAttribute(button, TAB_INDEX, -1);
  2085. items.push({ li, button, page: i });
  2086. }
  2087. }
  2088. function onKeydown(page, e) {
  2089. const { length } = items;
  2090. const { key } = e;
  2091. const dir = getDirection();
  2092. let nextPage = -1;
  2093. if (key === resolve(ARROW_RIGHT, false, dir)) {
  2094. nextPage = ++page % length;
  2095. } else if (key === resolve(ARROW_LEFT, false, dir)) {
  2096. nextPage = (--page + length) % length;
  2097. } else if (key === "Home") {
  2098. nextPage = 0;
  2099. } else if (key === "End") {
  2100. nextPage = length - 1;
  2101. }
  2102. const item = items[nextPage];
  2103. if (item) {
  2104. focus(item.button);
  2105. go(`>${nextPage}`);
  2106. prevent(e, true);
  2107. }
  2108. }
  2109. function getDirection() {
  2110. return options.paginationDirection || options.direction;
  2111. }
  2112. function getAt(index) {
  2113. return items[Controller.toPage(index)];
  2114. }
  2115. function update() {
  2116. const prev = getAt(getIndex(true));
  2117. const curr = getAt(getIndex());
  2118. if (prev) {
  2119. const { button } = prev;
  2120. removeClass(button, CLASS_ACTIVE);
  2121. removeAttribute(button, ARIA_SELECTED);
  2122. setAttribute(button, TAB_INDEX, -1);
  2123. }
  2124. if (curr) {
  2125. const { button } = curr;
  2126. addClass(button, CLASS_ACTIVE);
  2127. setAttribute(button, ARIA_SELECTED, true);
  2128. setAttribute(button, TAB_INDEX, "");
  2129. }
  2130. emit(EVENT_PAGINATION_UPDATED, { list, items }, prev, curr);
  2131. }
  2132. return {
  2133. items,
  2134. mount,
  2135. destroy,
  2136. getAt,
  2137. update
  2138. };
  2139. };
  2140. const TRIGGER_KEYS = [" ", "Enter"];
  2141. const Sync = (Splide2, Components, options, event) => {
  2142. const { isNavigation, slideFocus } = options;
  2143. const events = [];
  2144. function mount() {
  2145. Splide2.splides.forEach((target) => {
  2146. if (!target.isParent) {
  2147. sync(Splide2, target.splide);
  2148. sync(target.splide, Splide2);
  2149. }
  2150. });
  2151. if (isNavigation) {
  2152. navigate();
  2153. }
  2154. }
  2155. function destroy() {
  2156. events.forEach((event2) => {
  2157. event2.destroy();
  2158. });
  2159. empty(events);
  2160. }
  2161. function remount() {
  2162. destroy();
  2163. mount();
  2164. }
  2165. function sync(splide, target) {
  2166. const event2 = splide.event.create();
  2167. event2.on(EVENT_MOVE, (index, prev, dest) => {
  2168. target.go(target.is(LOOP) ? dest : index);
  2169. });
  2170. events.push(event2);
  2171. }
  2172. function navigate() {
  2173. const ev = event.create();
  2174. const { on } = ev;
  2175. on(EVENT_CLICK, onClick);
  2176. on(EVENT_SLIDE_KEYDOWN, onKeydown);
  2177. on([EVENT_MOUNTED, EVENT_UPDATED], update);
  2178. events.push(ev);
  2179. ev.emit(EVENT_NAVIGATION_MOUNTED, Splide2.splides);
  2180. }
  2181. function update() {
  2182. setAttribute(
  2183. Components.Elements.list,
  2184. ARIA_ORIENTATION,
  2185. options.direction === TTB ? "vertical" : ""
  2186. );
  2187. }
  2188. function onClick(Slide) {
  2189. Splide2.go(Slide.index);
  2190. }
  2191. function onKeydown(Slide, e) {
  2192. if (includes(TRIGGER_KEYS, e.key)) {
  2193. onClick(Slide);
  2194. prevent(e);
  2195. }
  2196. }
  2197. return {
  2198. setup: apply(
  2199. Components.Breakpoints.set,
  2200. { slideFocus: isUndefined(slideFocus) ? isNavigation : slideFocus },
  2201. true
  2202. ),
  2203. mount,
  2204. destroy,
  2205. remount
  2206. };
  2207. };
  2208. const Wheel = (Splide, Components, options, event) => {
  2209. let lastTime = 0;
  2210. function mount() {
  2211. event.destroy();
  2212. if (options.wheel) {
  2213. event.bind(Components.Elements.track, "wheel", onWheel, SCROLL_LISTENER_OPTIONS);
  2214. }
  2215. event.on(EVENT_UPDATED, mount);
  2216. }
  2217. function onWheel(e) {
  2218. if (e.cancelable) {
  2219. const delta = parse(e);
  2220. const backwards = delta < 0;
  2221. const timeStamp = timeOf(e);
  2222. const min = options.wheelMinThreshold || 0;
  2223. const sleep = options.wheelSleep || 0;
  2224. if (abs(delta) > min && timeStamp - lastTime > sleep) {
  2225. Splide.go(delta < 0 ? "<" : ">");
  2226. lastTime = timeStamp;
  2227. }
  2228. shouldPrevent(backwards) && prevent(e);
  2229. }
  2230. }
  2231. function parse(e) {
  2232. const { wheelAxis = "y" } = options;
  2233. const { deltaX, deltaY } = e;
  2234. const x = includes(wheelAxis, "x") ? Components.Direction.orient(-deltaX) : 0;
  2235. const y = includes(wheelAxis, "y") ? deltaY : 0;
  2236. return x || y;
  2237. }
  2238. function shouldPrevent(backwards) {
  2239. return !options.releaseWheel || Splide.state.is(MOVING) || Components.Controller.getAdjacent(backwards) !== -1;
  2240. }
  2241. return {
  2242. mount
  2243. };
  2244. };
  2245. const SR_REMOVAL_DELAY = 90;
  2246. const Live = (Splide, Components, options, event) => {
  2247. const { on } = event;
  2248. const { track } = Components.Elements;
  2249. const { live = true } = options;
  2250. const enabled = live && !options.isNavigation;
  2251. const sr = create("span", CLASS_SR);
  2252. const interval = RequestInterval(SR_REMOVAL_DELAY, apply(toggle, false));
  2253. function mount() {
  2254. if (enabled) {
  2255. disable(!Components.Autoplay.isPaused());
  2256. setAttribute(track, ARIA_ATOMIC, true);
  2257. sr.textContent = "\u2026";
  2258. on(EVENT_AUTOPLAY_PLAY, apply(disable, true));
  2259. on(EVENT_AUTOPLAY_PAUSE, apply(disable, false));
  2260. on([EVENT_MOVED, EVENT_SCROLLED], apply(toggle, true));
  2261. }
  2262. }
  2263. function toggle(active) {
  2264. setAttribute(track, ARIA_BUSY, active);
  2265. if (active) {
  2266. append(track, sr);
  2267. interval.start();
  2268. } else {
  2269. removeNode(sr);
  2270. interval.cancel();
  2271. }
  2272. }
  2273. function destroy() {
  2274. removeAttribute(track, [ARIA_LIVE, ARIA_ATOMIC, ARIA_BUSY]);
  2275. removeNode(sr);
  2276. }
  2277. function disable(disabled) {
  2278. if (enabled) {
  2279. setAttribute(track, ARIA_LIVE, disabled ? "off" : "polite");
  2280. }
  2281. }
  2282. return {
  2283. mount,
  2284. disable,
  2285. destroy
  2286. };
  2287. };
  2288. const COMPONENTS = {
  2289. Breakpoints,
  2290. Direction,
  2291. Elements,
  2292. Slides,
  2293. Layout,
  2294. Clones,
  2295. Move,
  2296. Scroll,
  2297. Controller,
  2298. Arrows,
  2299. Autoplay,
  2300. Drag,
  2301. Keyboard,
  2302. LazyLoad,
  2303. Pagination,
  2304. Sync,
  2305. Wheel,
  2306. Live
  2307. };
  2308. const I18N = {
  2309. prev: "Previous slide",
  2310. next: "Next slide",
  2311. first: "Go to first slide",
  2312. last: "Go to last slide",
  2313. slideX: "Go to slide %s",
  2314. pageX: "Go to page %s",
  2315. play: "Start autoplay",
  2316. pause: "Pause autoplay",
  2317. carousel: "carousel",
  2318. slide: "slide",
  2319. select: "Select a slide to show",
  2320. slideLabel: "%s of %s"
  2321. };
  2322. const DEFAULTS = {
  2323. type: "slide",
  2324. speed: 400,
  2325. perPage: 1,
  2326. easing: "cubic-bezier(0.25, 1, 0.5, 1)",
  2327. drag: true,
  2328. direction: "ltr",
  2329. trimSpace: true,
  2330. focusableNodes: "a, button, textarea, input, select, iframe",
  2331. classes: CLASSES,
  2332. i18n: I18N,
  2333. reducedMotion: {
  2334. speed: 0,
  2335. rewindSpeed: 0,
  2336. autoplay: "pause"
  2337. }
  2338. };
  2339. const Fade = (Splide, Components, options, event) => {
  2340. const { Slides } = Components;
  2341. function mount() {
  2342. event.on([EVENT_MOUNTED, EVENT_REFRESH], init);
  2343. }
  2344. function init() {
  2345. Slides.forEach((Slide) => {
  2346. Slide.style("transform", `translateX(-${100 * Slide.index}%)`);
  2347. });
  2348. }
  2349. function start(index, done) {
  2350. Slides.style("transition", `opacity ${options.speed}ms ${options.easing}`);
  2351. nextTick(done);
  2352. }
  2353. return {
  2354. mount,
  2355. start,
  2356. cancel: noop
  2357. };
  2358. };
  2359. const Slide = (Splide, Components, options, event) => {
  2360. const { Move, Controller, Scroll } = Components;
  2361. const { list } = Components.Elements;
  2362. const transition = apply(style, list, "transition");
  2363. let endCallback;
  2364. function mount() {
  2365. event.bind(list, "transitionend", (e) => {
  2366. if (e.target === list && endCallback) {
  2367. cancel();
  2368. endCallback();
  2369. }
  2370. });
  2371. }
  2372. function start(index, done) {
  2373. const destination = Move.toPosition(index);
  2374. const position = Move.getPosition();
  2375. const speed = getSpeed(index);
  2376. if (abs(destination - position) >= 1 && speed >= 1) {
  2377. if (options.useScroll) {
  2378. Scroll.scroll(destination, speed, false, done);
  2379. } else {
  2380. transition(`transform ${speed}ms ${options.easing}`);
  2381. Move.translate(destination, true);
  2382. endCallback = done;
  2383. }
  2384. } else {
  2385. Move.jump(index);
  2386. done();
  2387. }
  2388. }
  2389. function cancel() {
  2390. transition("");
  2391. Scroll.cancel();
  2392. }
  2393. function getSpeed(index) {
  2394. const { rewindSpeed } = options;
  2395. if (Splide.is(SLIDE) && rewindSpeed) {
  2396. const prev = Controller.getIndex(true);
  2397. const end = Controller.getEnd();
  2398. if (prev === 0 && index >= end || prev >= end && index === 0) {
  2399. return rewindSpeed;
  2400. }
  2401. }
  2402. return options.speed;
  2403. }
  2404. return {
  2405. mount,
  2406. start,
  2407. cancel
  2408. };
  2409. };
  2410. class Splide {
  2411. static defaults = {};
  2412. static STATES = STATES;
  2413. root;
  2414. event = EventInterface();
  2415. Components = {};
  2416. state = State(CREATED);
  2417. splides = [];
  2418. _o = {};
  2419. _C;
  2420. _E = {};
  2421. _T;
  2422. constructor(target, options = {}) {
  2423. const root = isString(target) ? query(document, target) : target;
  2424. assert(root, `${root} is invalid.`);
  2425. this.root = root;
  2426. options = merge({
  2427. label: getAttribute(root, ARIA_LABEL) || "",
  2428. labelledby: getAttribute(root, ARIA_LABELLEDBY) || ""
  2429. }, DEFAULTS, Splide.defaults, options);
  2430. try {
  2431. merge(options, JSON.parse(getAttribute(root, DATA_ATTRIBUTE)));
  2432. } catch (e) {
  2433. assert(false, "Invalid JSON");
  2434. }
  2435. this._o = Object.create(merge({}, options));
  2436. }
  2437. mount(Extensions = this._E, Transition = this._T) {
  2438. const { state, Components: Components2 } = this;
  2439. assert(state.is([CREATED, DESTROYED]), "Already mounted!");
  2440. state.set(CREATED);
  2441. this._C = Components2;
  2442. this._T = Transition || (this.is(FADE) ? Fade : Slide);
  2443. this._E = Extensions;
  2444. const Constructors = assign({}, COMPONENTS, this._E, { Transition: this._T });
  2445. forOwn(Constructors, (Component, key) => {
  2446. const component = Component(this, Components2, this._o, this.event.create());
  2447. Components2[key] = component;
  2448. component.setup && component.setup();
  2449. });
  2450. forOwn(Components2, (component) => {
  2451. component.mount && component.mount();
  2452. });
  2453. this.emit(EVENT_MOUNTED);
  2454. addClass(this.root, CLASS_INITIALIZED);
  2455. state.set(IDLE);
  2456. this.emit(EVENT_READY);
  2457. return this;
  2458. }
  2459. sync(splide) {
  2460. this.splides.push({ splide });
  2461. splide.splides.push({ splide: this, isParent: true });
  2462. if (this.state.is(IDLE)) {
  2463. this._C.Sync.remount();
  2464. splide.Components.Sync.remount();
  2465. }
  2466. return this;
  2467. }
  2468. go(control) {
  2469. this._C.Controller.go(control);
  2470. return this;
  2471. }
  2472. jump(control) {
  2473. this._C.Controller.jump(control);
  2474. return this;
  2475. }
  2476. on(events, callback) {
  2477. this.event.on(events, callback);
  2478. return this;
  2479. }
  2480. off(events, callback) {
  2481. this.event.off(events, callback);
  2482. return this;
  2483. }
  2484. emit(event, ...args) {
  2485. this.event.emit(event, ...args);
  2486. return this;
  2487. }
  2488. add(slides, index) {
  2489. this._C.Slides.add(slides, index);
  2490. return this;
  2491. }
  2492. remove(matcher) {
  2493. this._C.Slides.remove(matcher);
  2494. return this;
  2495. }
  2496. is(type) {
  2497. return this._o.type === type;
  2498. }
  2499. refresh() {
  2500. this.emit(EVENT_REFRESH);
  2501. return this;
  2502. }
  2503. destroy(completely = true) {
  2504. const { event, state } = this;
  2505. if (state.is(CREATED)) {
  2506. this.on(EVENT_READY, this.destroy.bind(this, completely));
  2507. } else {
  2508. forOwn(this._C, (component) => {
  2509. component.destroy && component.destroy(completely);
  2510. }, true);
  2511. event.emit(EVENT_DESTROY);
  2512. event.destroy();
  2513. completely && empty(this.splides);
  2514. state.set(DESTROYED);
  2515. }
  2516. return this;
  2517. }
  2518. get options() {
  2519. return this._o;
  2520. }
  2521. set options(options) {
  2522. this._C.Breakpoints.set(options, true, true);
  2523. }
  2524. get length() {
  2525. return this._C.Slides.getLength(true);
  2526. }
  2527. get index() {
  2528. return this._C.Controller.getIndex();
  2529. }
  2530. }
  2531. const CLASS_RENDERED = "is-rendered";
  2532. const RENDERER_DEFAULT_CONFIG = {
  2533. listTag: "ul",
  2534. slideTag: "li"
  2535. };
  2536. class Style {
  2537. styles = {};
  2538. id;
  2539. options;
  2540. constructor(id, options) {
  2541. this.id = id;
  2542. this.options = options;
  2543. }
  2544. rule(selector, prop, value, breakpoint) {
  2545. breakpoint = breakpoint || "default";
  2546. const selectors = this.styles[breakpoint] = this.styles[breakpoint] || {};
  2547. const styles = selectors[selector] = selectors[selector] || {};
  2548. styles[prop] = value;
  2549. }
  2550. build() {
  2551. let css = "";
  2552. if (this.styles.default) {
  2553. css += this.buildSelectors(this.styles.default);
  2554. }
  2555. Object.keys(this.styles).sort((n, m) => this.options.mediaQuery === "min" ? +n - +m : +m - +n).forEach((breakpoint) => {
  2556. if (breakpoint !== "default") {
  2557. css += `@media screen and (max-width: ${breakpoint}px) {`;
  2558. css += this.buildSelectors(this.styles[breakpoint]);
  2559. css += `}`;
  2560. }
  2561. });
  2562. return css;
  2563. }
  2564. buildSelectors(selectors) {
  2565. let css = "";
  2566. forOwn(selectors, (styles, selector) => {
  2567. css += `${`#${this.id} ${selector}`.trim()} {`;
  2568. forOwn(styles, (value, prop) => {
  2569. if (value || value === 0) {
  2570. css += `${prop}: ${value};`;
  2571. }
  2572. });
  2573. css += "}";
  2574. });
  2575. return css;
  2576. }
  2577. }
  2578. class SplideRenderer {
  2579. static clean(splide) {
  2580. const { on } = splide.event;
  2581. const { root } = splide;
  2582. const clones = queryAll(root, `.${CLASS_CLONE}`);
  2583. on(EVENT_MOUNTED, () => {
  2584. removeNode(child(root, "style"));
  2585. });
  2586. removeNode(clones);
  2587. }
  2588. contents;
  2589. slides = [];
  2590. Direction;
  2591. Style;
  2592. options = {};
  2593. config;
  2594. id;
  2595. breakpoints = [];
  2596. constructor(contents, options, config, defaults) {
  2597. merge(DEFAULTS, defaults || {});
  2598. merge(merge(this.options, DEFAULTS), options || {});
  2599. this.contents = contents;
  2600. this.config = assign({}, RENDERER_DEFAULT_CONFIG, config || {});
  2601. this.id = this.config.id || uniqueId("splide");
  2602. this.Style = new Style(this.id, this.options);
  2603. this.Direction = Direction(null, null, this.options);
  2604. assert(this.contents.length, "Provide at least 1 content.");
  2605. this.init();
  2606. }
  2607. init() {
  2608. this.parseBreakpoints();
  2609. this.initSlides();
  2610. this.registerRootStyles();
  2611. this.registerTrackStyles();
  2612. this.registerSlideStyles();
  2613. this.registerListStyles();
  2614. }
  2615. initSlides() {
  2616. push(this.slides, this.contents.map((content, index) => {
  2617. content = isString(content) ? { html: content } : content;
  2618. content.styles = content.styles || {};
  2619. content.attrs = content.attrs || {};
  2620. this.cover(content);
  2621. const classes = `${this.options.classes.slide} ${index === 0 ? CLASS_ACTIVE : ""}`;
  2622. assign(content.attrs, {
  2623. class: `${classes} ${content.attrs.class || ""}`.trim(),
  2624. style: this.buildStyles(content.styles)
  2625. });
  2626. return content;
  2627. }));
  2628. if (this.isLoop()) {
  2629. this.generateClones(this.slides);
  2630. }
  2631. }
  2632. registerRootStyles() {
  2633. this.breakpoints.forEach(([width, options]) => {
  2634. this.Style.rule(" ", "max-width", unit(options.width), width);
  2635. });
  2636. }
  2637. registerTrackStyles() {
  2638. const { Style: Style2 } = this;
  2639. const selector = `.${CLASS_TRACK}`;
  2640. this.breakpoints.forEach(([width, options]) => {
  2641. Style2.rule(selector, this.resolve("paddingLeft"), this.cssPadding(options, false), width);
  2642. Style2.rule(selector, this.resolve("paddingRight"), this.cssPadding(options, true), width);
  2643. Style2.rule(selector, "height", this.cssTrackHeight(options), width);
  2644. });
  2645. }
  2646. registerListStyles() {
  2647. const { Style: Style2 } = this;
  2648. const selector = `.${CLASS_LIST}`;
  2649. this.breakpoints.forEach(([width, options]) => {
  2650. Style2.rule(selector, "transform", this.buildTranslate(options), width);
  2651. if (!this.cssSlideHeight(options)) {
  2652. Style2.rule(selector, "aspect-ratio", this.cssAspectRatio(options), width);
  2653. }
  2654. });
  2655. }
  2656. registerSlideStyles() {
  2657. const { Style: Style2 } = this;
  2658. const selector = `.${CLASS_SLIDE}`;
  2659. this.breakpoints.forEach(([width, options]) => {
  2660. Style2.rule(selector, "width", this.cssSlideWidth(options), width);
  2661. Style2.rule(selector, "height", this.cssSlideHeight(options) || "100%", width);
  2662. Style2.rule(selector, this.resolve("marginRight"), unit(options.gap) || "0px", width);
  2663. Style2.rule(`${selector} > img`, "display", options.cover ? "none" : "inline", width);
  2664. });
  2665. }
  2666. buildTranslate(options) {
  2667. const { resolve, orient } = this.Direction;
  2668. const values = [];
  2669. values.push(this.cssOffsetClones(options));
  2670. values.push(this.cssOffsetGaps(options));
  2671. if (this.isCenter(options)) {
  2672. values.push(this.buildCssValue(orient(-50), "%"));
  2673. values.push(...this.cssOffsetCenter(options));
  2674. }
  2675. return values.filter(Boolean).map((value) => `translate${resolve("X")}(${value})`).join(" ");
  2676. }
  2677. cssOffsetClones(options) {
  2678. const { resolve, orient } = this.Direction;
  2679. const cloneCount = this.getCloneCount();
  2680. if (this.isFixedWidth(options)) {
  2681. const { value, unit: unit2 } = this.parseCssValue(options[resolve("fixedWidth")]);
  2682. return this.buildCssValue(orient(value) * cloneCount, unit2);
  2683. }
  2684. const percent = 100 * cloneCount / options.perPage;
  2685. return `${orient(percent)}%`;
  2686. }
  2687. cssOffsetCenter(options) {
  2688. const { resolve, orient } = this.Direction;
  2689. if (this.isFixedWidth(options)) {
  2690. const { value, unit: unit2 } = this.parseCssValue(options[resolve("fixedWidth")]);
  2691. return [this.buildCssValue(orient(value / 2), unit2)];
  2692. }
  2693. const values = [];
  2694. const { perPage, gap } = options;
  2695. values.push(`${orient(50 / perPage)}%`);
  2696. if (gap) {
  2697. const { value, unit: unit2 } = this.parseCssValue(gap);
  2698. const gapOffset = (value / perPage - value) / 2;
  2699. values.push(this.buildCssValue(orient(gapOffset), unit2));
  2700. }
  2701. return values;
  2702. }
  2703. cssOffsetGaps(options) {
  2704. const cloneCount = this.getCloneCount();
  2705. if (cloneCount && options.gap) {
  2706. const { orient } = this.Direction;
  2707. const { value, unit: unit2 } = this.parseCssValue(options.gap);
  2708. if (this.isFixedWidth(options)) {
  2709. return this.buildCssValue(orient(value * cloneCount), unit2);
  2710. }
  2711. const { perPage } = options;
  2712. const gaps = cloneCount / perPage;
  2713. return this.buildCssValue(orient(gaps * value), unit2);
  2714. }
  2715. return "";
  2716. }
  2717. resolve(prop) {
  2718. return camelToKebab(this.Direction.resolve(prop));
  2719. }
  2720. cssPadding(options, right) {
  2721. const { padding } = options;
  2722. const prop = this.Direction.resolve(right ? "right" : "left", true);
  2723. return padding && unit(padding[prop] || (isObject(padding) ? 0 : padding)) || "0px";
  2724. }
  2725. cssTrackHeight(options) {
  2726. let height = "";
  2727. if (this.isVertical()) {
  2728. height = this.cssHeight(options);
  2729. assert(height, '"height" is missing.');
  2730. height = `calc(${height} - ${this.cssPadding(options, false)} - ${this.cssPadding(options, true)})`;
  2731. }
  2732. return height;
  2733. }
  2734. cssHeight(options) {
  2735. return unit(options.height);
  2736. }
  2737. cssSlideWidth(options) {
  2738. return options.autoWidth ? "" : unit(options.fixedWidth) || (this.isVertical() ? "" : this.cssSlideSize(options));
  2739. }
  2740. cssSlideHeight(options) {
  2741. return unit(options.fixedHeight) || (this.isVertical() ? options.autoHeight ? "" : this.cssSlideSize(options) : this.cssHeight(options));
  2742. }
  2743. cssSlideSize(options) {
  2744. const gap = unit(options.gap);
  2745. return `calc((100%${gap && ` + ${gap}`})/${options.perPage || 1}${gap && ` - ${gap}`})`;
  2746. }
  2747. cssAspectRatio(options) {
  2748. const { heightRatio } = options;
  2749. return heightRatio ? `${1 / heightRatio}` : "";
  2750. }
  2751. buildCssValue(value, unit2) {
  2752. return `${value}${unit2}`;
  2753. }
  2754. parseCssValue(value) {
  2755. if (isString(value)) {
  2756. const number = parseFloat(value) || 0;
  2757. const unit2 = value.replace(/\d*(\.\d*)?/, "") || "px";
  2758. return { value: number, unit: unit2 };
  2759. }
  2760. return { value, unit: "px" };
  2761. }
  2762. parseBreakpoints() {
  2763. const { breakpoints } = this.options;
  2764. this.breakpoints.push(["default", this.options]);
  2765. if (breakpoints) {
  2766. forOwn(breakpoints, (options, width) => {
  2767. this.breakpoints.push([width, merge(merge({}, this.options), options)]);
  2768. });
  2769. }
  2770. }
  2771. isFixedWidth(options) {
  2772. return !!options[this.Direction.resolve("fixedWidth")];
  2773. }
  2774. isLoop() {
  2775. return this.options.type === LOOP;
  2776. }
  2777. isCenter(options) {
  2778. if (options.focus === "center") {
  2779. if (this.isLoop()) {
  2780. return true;
  2781. }
  2782. if (this.options.type === SLIDE) {
  2783. return !this.options.trimSpace;
  2784. }
  2785. }
  2786. return false;
  2787. }
  2788. isVertical() {
  2789. return this.options.direction === TTB;
  2790. }
  2791. buildClasses() {
  2792. const { options } = this;
  2793. return [
  2794. CLASS_ROOT,
  2795. `${CLASS_ROOT}--${options.type}`,
  2796. `${CLASS_ROOT}--${options.direction}`,
  2797. options.drag && `${CLASS_ROOT}--draggable`,
  2798. options.isNavigation && `${CLASS_ROOT}--nav`,
  2799. CLASS_ACTIVE,
  2800. !this.config.hidden && CLASS_RENDERED
  2801. ].filter(Boolean).join(" ");
  2802. }
  2803. buildAttrs(attrs) {
  2804. let attr = "";
  2805. forOwn(attrs, (value, key) => {
  2806. attr += value ? ` ${camelToKebab(key)}="${value}"` : "";
  2807. });
  2808. return attr.trim();
  2809. }
  2810. buildStyles(styles) {
  2811. let style = "";
  2812. forOwn(styles, (value, key) => {
  2813. style += ` ${camelToKebab(key)}:${value};`;
  2814. });
  2815. return style.trim();
  2816. }
  2817. renderSlides() {
  2818. const { slideTag: tag } = this.config;
  2819. return this.slides.map((content) => {
  2820. return `<${tag} ${this.buildAttrs(content.attrs)}>${content.html || ""}</${tag}>`;
  2821. }).join("");
  2822. }
  2823. cover(content) {
  2824. const { styles, html = "" } = content;
  2825. if (this.options.cover && !this.options.lazyLoad) {
  2826. const src = html.match(/<img.*?src\s*=\s*(['"])(.+?)\1.*?>/);
  2827. if (src && src[2]) {
  2828. styles.background = `center/cover no-repeat url('${src[2]}')`;
  2829. }
  2830. }
  2831. }
  2832. generateClones(contents) {
  2833. const { classes } = this.options;
  2834. const count = this.getCloneCount();
  2835. const slides = contents.slice();
  2836. while (slides.length < count) {
  2837. push(slides, slides);
  2838. }
  2839. push(slides.slice(-count).reverse(), slides.slice(0, count)).forEach((content, index) => {
  2840. const attrs = assign({}, content.attrs, { class: `${content.attrs.class} ${classes.clone}` });
  2841. const clone = assign({}, content, { attrs });
  2842. index < count ? contents.unshift(clone) : contents.push(clone);
  2843. });
  2844. }
  2845. getCloneCount() {
  2846. if (this.isLoop()) {
  2847. const { options } = this;
  2848. if (options.clones) {
  2849. return options.clones;
  2850. }
  2851. const perPage = max(...this.breakpoints.map(([, options2]) => options2.perPage));
  2852. return perPage * ((options.flickMaxPages || 1) + 1);
  2853. }
  2854. return 0;
  2855. }
  2856. renderArrows() {
  2857. let html = "";
  2858. html += `<div class="${this.options.classes.arrows}">`;
  2859. html += this.renderArrow(true);
  2860. html += this.renderArrow(false);
  2861. html += `</div>`;
  2862. return html;
  2863. }
  2864. renderArrow(prev) {
  2865. const { classes, i18n } = this.options;
  2866. const attrs = {
  2867. class: `${classes.arrow} ${prev ? classes.prev : classes.next}`,
  2868. type: "button",
  2869. ariaLabel: prev ? i18n.prev : i18n.next
  2870. };
  2871. return `<button ${this.buildAttrs(attrs)}><svg xmlns="${XML_NAME_SPACE}" viewBox="0 0 ${SIZE} ${SIZE}" width="${SIZE}" height="${SIZE}"><path d="${this.options.arrowPath || PATH}" /></svg></button>`;
  2872. }
  2873. html() {
  2874. const { rootClass, listTag, arrows, beforeTrack, afterTrack, slider, beforeSlider, afterSlider } = this.config;
  2875. let html = "";
  2876. html += `<div id="${this.id}" class="${this.buildClasses()} ${rootClass || ""}">`;
  2877. html += `<style>${this.Style.build()}</style>`;
  2878. if (slider) {
  2879. html += beforeSlider || "";
  2880. html += `<div class="splide__slider">`;
  2881. }
  2882. html += beforeTrack || "";
  2883. if (arrows) {
  2884. html += this.renderArrows();
  2885. }
  2886. html += `<div class="splide__track">`;
  2887. html += `<${listTag} class="splide__list">`;
  2888. html += this.renderSlides();
  2889. html += `</${listTag}>`;
  2890. html += `</div>`;
  2891. html += afterTrack || "";
  2892. if (slider) {
  2893. html += `</div>`;
  2894. html += afterSlider || "";
  2895. }
  2896. html += `</div>`;
  2897. return html;
  2898. }
  2899. }
  2900. export { CLASSES, CLASS_ACTIVE, CLASS_ARROW, CLASS_ARROWS, CLASS_ARROW_NEXT, CLASS_ARROW_PREV, CLASS_CLONE, CLASS_CONTAINER, CLASS_FOCUS_IN, CLASS_INITIALIZED, CLASS_LIST, CLASS_LOADING, CLASS_NEXT, CLASS_OVERFLOW, CLASS_PAGINATION, CLASS_PAGINATION_PAGE, CLASS_PREV, CLASS_PROGRESS, CLASS_PROGRESS_BAR, CLASS_ROOT, CLASS_SLIDE, CLASS_SPINNER, CLASS_SR, CLASS_TOGGLE, CLASS_TOGGLE_PAUSE, CLASS_TOGGLE_PLAY, CLASS_TRACK, CLASS_VISIBLE, DEFAULTS, EVENT_ACTIVE, EVENT_ARROWS_MOUNTED, EVENT_ARROWS_UPDATED, EVENT_AUTOPLAY_PAUSE, EVENT_AUTOPLAY_PLAY, EVENT_AUTOPLAY_PLAYING, EVENT_CLICK, EVENT_DESTROY, EVENT_DRAG, EVENT_DRAGGED, EVENT_DRAGGING, EVENT_END_INDEX_CHANGED, EVENT_HIDDEN, EVENT_INACTIVE, EVENT_LAZYLOAD_ERROR, EVENT_LAZYLOAD_LOADED, EVENT_MOUNTED, EVENT_MOVE, EVENT_MOVED, EVENT_NAVIGATION_MOUNTED, EVENT_OVERFLOW, EVENT_PAGINATION_MOUNTED, EVENT_PAGINATION_UPDATED, EVENT_READY, EVENT_REFRESH, EVENT_RESIZE, EVENT_RESIZED, EVENT_SCROLL, EVENT_SCROLLED, EVENT_SCROLLING, EVENT_SHIFTED, EVENT_SLIDE_KEYDOWN, EVENT_UPDATED, EVENT_VISIBLE, FADE, LOOP, LTR, RTL, SLIDE, STATUS_CLASSES, Splide, SplideRenderer, TTB, Splide as default };