splide.esm.js 64 KB

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