splide.esm.js 85 KB

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