splide.js 77 KB

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