splide.cjs.js 65 KB

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