splide.esm.js 85 KB

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