splide.esm.js 64 KB

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