splide.js 77 KB

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