splide.cjs.js 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460
  1. /*!
  2. * Splide.js
  3. * Version : 3.0.0
  4. * License : MIT
  5. * Copyright: 2021 Naotoshi Fujita
  6. */
  7. 'use strict';
  8. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  9. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  10. Object.defineProperty(exports, '__esModule', {
  11. value: true
  12. });
  13. /**
  14. * The project code.
  15. *
  16. * @since 3.0.0
  17. */
  18. var PROJECT_CODE = 'splide';
  19. /**
  20. * The data attribute prefix.
  21. *
  22. * @since 3.0.0
  23. */
  24. var DATA_ATTRIBUTE = "data-" + PROJECT_CODE;
  25. /**
  26. * Splide has been just created.
  27. */
  28. var CREATED = 1;
  29. /**
  30. * Splide has mounted components.
  31. */
  32. var MOUNTED = 2;
  33. /**
  34. * Splide is ready.
  35. */
  36. var IDLE = 3;
  37. /**
  38. * Splide is moving.
  39. */
  40. var MOVING = 4;
  41. /**
  42. * Splide has been destroyed.
  43. */
  44. var DESTROYED = 5;
  45. /**
  46. * The collection of all states.
  47. *
  48. * @since 3.0.0
  49. */
  50. var STATES = {
  51. CREATED: CREATED,
  52. MOUNTED: MOUNTED,
  53. IDLE: IDLE,
  54. MOVING: MOVING,
  55. DESTROYED: DESTROYED
  56. };
  57. /**
  58. * Empties the array.
  59. *
  60. * @param array - A array to empty.
  61. */
  62. function empty(array) {
  63. array.length = 0;
  64. }
  65. /**
  66. * Checks if the given subject is an object or not.
  67. *
  68. * @param subject - A subject to check.
  69. *
  70. * @return `true` if the subject is an object, or otherwise `false`.
  71. */
  72. function isObject(subject) {
  73. return !isNull(subject) && typeof subject === 'object';
  74. }
  75. /**
  76. * Checks if the given subject is an array or not.
  77. *
  78. * @param subject - A subject to check.
  79. *
  80. * @return `true` if the subject is an array, or otherwise `false`.
  81. */
  82. function isArray(subject) {
  83. return Array.isArray(subject);
  84. }
  85. /**
  86. * Checks if the given subject is a function or not.
  87. *
  88. * @param subject - A subject to check.
  89. *
  90. * @return `true` if the subject is a function, or otherwise `false`.
  91. */
  92. function isFunction(subject) {
  93. return typeof subject === 'function';
  94. }
  95. /**
  96. * Checks if the given subject is a string or not.
  97. *
  98. * @param subject - A subject to check.
  99. *
  100. * @return `true` if the subject is a string, or otherwise `false`.
  101. */
  102. function isString(subject) {
  103. return typeof subject === 'string';
  104. }
  105. /**
  106. * Checks if the given subject is `undefined` or not.
  107. *
  108. * @param subject - A subject to check.
  109. *
  110. * @return `true` if the subject is `undefined`, or otherwise `false`.
  111. */
  112. function isUndefined(subject) {
  113. return typeof subject === 'undefined';
  114. }
  115. /**
  116. * Checks if the given subject is `null` or not.
  117. *
  118. * @param subject - A subject to check.
  119. *
  120. * @return `true` if the subject is `null`, or otherwise `false`.
  121. */
  122. function isNull(subject) {
  123. return subject === null;
  124. }
  125. /**
  126. * Checks if the given subject is an HTMLElement or not.
  127. *
  128. * @param subject - A subject to check.
  129. *
  130. * @return `true` if the subject is an HTMLElement instance, or otherwise `false`.
  131. */
  132. function isHTMLElement(subject) {
  133. return subject instanceof HTMLElement;
  134. }
  135. /**
  136. * Checks if the given subject is an HTMLButtonElement or not.
  137. *
  138. * @param subject - A subject to check.
  139. *
  140. * @return `true` if the subject is an HTMLButtonElement, or otherwise `false`.
  141. */
  142. function isHTMLButtonElement(subject) {
  143. return subject instanceof HTMLButtonElement;
  144. }
  145. /**
  146. * Push the provided value to an array if the value is not an array.
  147. *
  148. * @param value - A value to push.
  149. *
  150. * @return An array containing the value, or the value itself if it is already an array.
  151. */
  152. function toArray(value) {
  153. return isArray(value) ? value : [value];
  154. }
  155. /**
  156. * The extended `Array#forEach` method that accepts a single value as an argument.
  157. *
  158. * @param values - A value or values to iterate over.
  159. * @param iteratee - An iteratee function.
  160. */
  161. function forEach(values, iteratee) {
  162. toArray(values).forEach(iteratee);
  163. }
  164. /**
  165. * Checks if the array includes the value or not.
  166. * `Array#includes` is not supported by IE.
  167. *
  168. * @param array - An array.
  169. * @param value - A value to search for.
  170. *
  171. * @return `true` if the array includes the value, or otherwise `false`.
  172. */
  173. function includes(array, value) {
  174. return array.indexOf(value) > -1;
  175. }
  176. /**
  177. * Extended `Array#push()` that accepts an item or an array with items.
  178. *
  179. * @param array - An array to push items.
  180. * @param items - An item or items to push.
  181. *
  182. * @return A provided array itself.
  183. */
  184. function push(array, items) {
  185. array.push.apply(array, toArray(items));
  186. return array;
  187. }
  188. var arrayProto = Array.prototype;
  189. /**
  190. * The slice method for an array-like object.
  191. *
  192. * @param arrayLike - An array-like object.
  193. * @param start - Optional. A start index.
  194. * @param end - Optional. A end index.
  195. *
  196. * @return An array with sliced elements.
  197. */
  198. function slice(arrayLike, start, end) {
  199. return arrayProto.slice.call(arrayLike, start, end);
  200. }
  201. /**
  202. * The splice method for an array-like object.
  203. *
  204. * @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
  205. *
  206. * @param arrayLike - An array-like object.
  207. * @param start - A start index.
  208. * @param deleteCount - Optional. A number of elements to remove from the `start` index.
  209. * @param args - Optional. Any number of items to add.
  210. *
  211. * @return An array with deleted items.
  212. */
  213. function splice(arrayLike, start, deleteCount) {
  214. var _arrayProto$splice;
  215. for (var _len = arguments.length, args = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
  216. args[_key - 3] = arguments[_key];
  217. }
  218. return (_arrayProto$splice = arrayProto.splice).call.apply(_arrayProto$splice, [arrayLike, start, deleteCount].concat(args));
  219. }
  220. /**
  221. * The find method for an array or array-like object, works in IE.
  222. * This method is not performant for a huge array.
  223. *
  224. * @param arrayLike - An array-like object.
  225. * @param predicate - The predicate function to test each element in the object.
  226. *
  227. * @return A found value if available, or otherwise `undefined`.
  228. */
  229. function find(arrayLike, predicate) {
  230. return slice(arrayLike).filter(predicate)[0];
  231. }
  232. /**
  233. * Toggles the provided class or classes by following the `add` boolean.
  234. *
  235. * @param elm - An element whose classes are toggled.
  236. * @param classes - A class or class names.
  237. * @param add - Whether to add or remove a class.
  238. */
  239. function toggleClass(elm, classes, add) {
  240. if (elm) {
  241. forEach(classes, function (name) {
  242. if (name) {
  243. elm.classList[add ? 'add' : 'remove'](name);
  244. }
  245. });
  246. }
  247. }
  248. /**
  249. * Adds classes to the element.
  250. *
  251. * @param elm - An element to add classes to.
  252. * @param classes - Classes to add.
  253. */
  254. function addClass(elm, classes) {
  255. toggleClass(elm, classes, true);
  256. }
  257. /**
  258. * Appends children to the parent element.
  259. *
  260. * @param parent - A parent element.
  261. * @param children - A child or children to append to the parent.
  262. */
  263. function append(parent, children) {
  264. forEach(children, parent.appendChild.bind(parent));
  265. }
  266. /**
  267. * Inserts a node or nodes before the specified reference node.
  268. *
  269. * @param nodes - A node or nodes to insert.
  270. * @param ref - A reference node.
  271. */
  272. function before(nodes, ref) {
  273. forEach(nodes, function (node) {
  274. var parent = ref.parentNode;
  275. if (parent) {
  276. parent.insertBefore(node, ref);
  277. }
  278. });
  279. }
  280. /**
  281. * Checks if the element can be selected by the provided selector or not.
  282. *
  283. * @param elm - An element to check.
  284. * @param selector - A selector to test.
  285. *
  286. * @return `true` if the selector matches the element, or otherwise `false`.
  287. */
  288. function matches(elm, selector) {
  289. return (elm['msMatchesSelector'] || elm.matches).call(elm, selector);
  290. }
  291. /**
  292. * Finds children that has the specified tag or class name.
  293. *
  294. * @param parent - A parent element.
  295. * @param selector - A selector to filter children.
  296. *
  297. * @return An array with filtered children.
  298. */
  299. function children(parent, selector) {
  300. return parent ? slice(parent.children).filter(function (child) {
  301. return matches(child, selector);
  302. }) : [];
  303. }
  304. /**
  305. * Returns a child element that matches the specified tag or class name.
  306. *
  307. * @param parent - A parent element.
  308. * @param selector - A selector to filter children.
  309. *
  310. * @return A matched child element if available, or otherwise `undefined`.
  311. */
  312. function child(parent, selector) {
  313. return selector ? children(parent, selector)[0] : parent.firstElementChild;
  314. }
  315. /**
  316. * Iterates over the provided object by own enumerable keys with calling the iteratee function.
  317. *
  318. * @param object - An object to iterate over.
  319. * @param iteratee - An iteratee function that takes the value and key as arguments.
  320. *
  321. * @return A provided object itself.
  322. */
  323. function forOwn(object, iteratee) {
  324. if (object) {
  325. var keys = Object.keys(object);
  326. for (var i = 0; i < keys.length; i++) {
  327. var key = keys[i];
  328. if (key !== '__proto__') {
  329. if (iteratee(object[key], key) === false) {
  330. break;
  331. }
  332. }
  333. }
  334. }
  335. return object;
  336. }
  337. /**
  338. * Assigns all own enumerable properties of all source objects to the provided object.
  339. * `undefined` in source objects will be skipped.
  340. *
  341. * @param object - An object to assign properties to.
  342. * @param sources - Objects to assign properties from.
  343. *
  344. * @return An object assigned properties of the sources to.
  345. */
  346. function assign(object) {
  347. for (var _len2 = arguments.length, sources = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  348. sources[_key2 - 1] = arguments[_key2];
  349. }
  350. sources.forEach(function (source) {
  351. forOwn(source, function (value, key) {
  352. object[key] = source[key];
  353. });
  354. });
  355. return object;
  356. }
  357. /**
  358. * Recursively merges source properties to the object.
  359. *
  360. * @param object - An object to merge properties to.
  361. * @param source - A source object to merge properties from.
  362. *
  363. * @return A new object with merged properties.
  364. */
  365. function merge(object, source) {
  366. forOwn(source, function (value, key) {
  367. object[key] = isObject(value) ? merge(isObject(object[key]) ? object[key] : {}, value) : value;
  368. });
  369. return object;
  370. }
  371. /**
  372. * Removes attributes from the element.
  373. *
  374. * @param elm - An element.
  375. * @param attrs - An attribute or attributes to remove.
  376. */
  377. function removeAttribute(elm, attrs) {
  378. if (elm) {
  379. forEach(attrs, function (attr) {
  380. elm.removeAttribute(attr);
  381. });
  382. }
  383. }
  384. function setAttribute(elm, attrs, value) {
  385. if (isObject(attrs)) {
  386. forOwn(attrs, function (value, name) {
  387. setAttribute(elm, name, value);
  388. });
  389. } else {
  390. isNull(value) ? removeAttribute(elm, attrs) : elm.setAttribute(attrs, String(value));
  391. }
  392. }
  393. /**
  394. * Creates a HTML element.
  395. *
  396. * @param tag - A tag name.
  397. * @param attrs - Optional. An object with attributes to apply the created element to, or a string with classes.
  398. * @param parent - Optional. A parent element where the created element is appended.
  399. */
  400. function create(tag, attrs, parent) {
  401. var elm = document.createElement(tag);
  402. if (attrs) {
  403. if (isString(attrs) || isArray(attrs)) {
  404. addClass(elm, attrs);
  405. } else {
  406. setAttribute(elm, attrs);
  407. }
  408. }
  409. if (parent) {
  410. append(parent, elm);
  411. }
  412. return elm;
  413. }
  414. /**
  415. * Applies inline styles to the provided element by an object literal.
  416. *
  417. * @param elms - An element or elements to apply styles to.
  418. * @param styles - An object literal with styles.
  419. */
  420. function style(elms, styles) {
  421. if (isString(styles)) {
  422. return isArray(elms) ? null : getComputedStyle(elms)[styles];
  423. }
  424. forOwn(styles, function (value, key) {
  425. if (!isNull(value)) {
  426. forEach(elms, function (elm) {
  427. if (elm) {
  428. elm.style[key] = "" + value;
  429. }
  430. });
  431. }
  432. });
  433. }
  434. /**
  435. * Sets the `display` CSS value to the element.
  436. *
  437. * @param elm - An element to set a new value to.
  438. * @param display - A new `display` value.
  439. */
  440. function display(elm, display) {
  441. style(elm, {
  442. display: display
  443. });
  444. }
  445. /**
  446. * Returns the specified attribute value.
  447. *
  448. * @param elm - An element.
  449. * @param attr - An attribute to get.
  450. */
  451. function getAttribute(elm, attr) {
  452. return elm.getAttribute(attr);
  453. }
  454. /**
  455. * Checks if the element contains the specified class or not.
  456. *
  457. * @param elm - An element to check.
  458. * @param className - A class name that may be contained by the element.
  459. *
  460. * @return `true` if the element contains the class, or otherwise `false`.
  461. */
  462. function hasClass(elm, className) {
  463. return elm && elm.classList.contains(className);
  464. }
  465. /**
  466. * Parses the provided HTML string and returns the first element.
  467. *
  468. * @param html - An HTML string to parse.
  469. *
  470. * @return An Element on success, or otherwise `undefined`.
  471. */
  472. function parseHtml(html) {
  473. return child(new DOMParser().parseFromString(html, 'text/html').body);
  474. }
  475. /**
  476. * Call the `preventDefault()` of the provided event.
  477. *
  478. * @param e - An Event object.
  479. * @param stopPropagation - Optional. Whether to stop the event propagation or not.
  480. */
  481. function prevent(e, stopPropagation) {
  482. e.preventDefault();
  483. if (stopPropagation) {
  484. e.stopPropagation();
  485. e.stopImmediatePropagation();
  486. }
  487. }
  488. /**
  489. * Returns an element that matches the provided selector.
  490. *
  491. * @param parent - A parent element to start searching from.
  492. * @param selector - A selector to query.
  493. *
  494. * @return A found element or `null`.
  495. */
  496. function query(parent, selector) {
  497. return parent && parent.querySelector(selector);
  498. }
  499. /**
  500. * Returns elements that match the provided selector.
  501. *
  502. * @param parent - A parent element to start searching from.
  503. * @param selector - A selector to query.
  504. *
  505. * @return An array with matched elements.
  506. */
  507. function queryAll(parent, selector) {
  508. return slice(parent.querySelectorAll(selector));
  509. }
  510. /**
  511. * Returns a DOMRect object of the provided element.
  512. *
  513. * @param target - An element.
  514. */
  515. function rect(target) {
  516. return target.getBoundingClientRect();
  517. }
  518. /**
  519. * Removes the provided node from its parent.
  520. *
  521. * @param nodes - A node or nodes to remove.
  522. */
  523. function remove(nodes) {
  524. forEach(nodes, function (node) {
  525. if (node && node.parentNode) {
  526. node.parentNode.removeChild(node);
  527. }
  528. });
  529. }
  530. /**
  531. * Removes classes from the element.
  532. *
  533. * @param elm - An element to remove classes from.
  534. * @param classes - Classes to remove.
  535. */
  536. function removeClass(elm, classes) {
  537. toggleClass(elm, classes, false);
  538. }
  539. /**
  540. * Appends `px` to the provided number.
  541. * If the value is already string, just returns it.
  542. *
  543. * @param value - A value to append `px` to.
  544. *
  545. * @return A string with the CSS unit.
  546. */
  547. function unit(value) {
  548. return isString(value) ? value : value ? value + "px" : '';
  549. }
  550. /**
  551. * Throws an error if the provided condition is falsy.
  552. *
  553. * @param condition - If falsy, an error is thrown.
  554. * @param message - Optional. A message to display.
  555. */
  556. function assert(condition, message) {
  557. if (message === void 0) {
  558. message = '';
  559. }
  560. if (!condition) {
  561. throw new Error("[" + PROJECT_CODE + "] " + message);
  562. }
  563. }
  564. /**
  565. * Invokes the callback on the next tick.
  566. *
  567. * @param callback - A callback function.
  568. */
  569. function nextTick(callback) {
  570. setTimeout(callback);
  571. }
  572. /**
  573. * No operation.
  574. */
  575. var noop = function noop() {}; // eslint-disable-line no-empty-function, @typescript-eslint/no-empty-function
  576. /**
  577. * The arias of `window.requestAnimationFrame()`.
  578. */
  579. function raf(func) {
  580. return requestAnimationFrame(func);
  581. }
  582. /**
  583. * Checks if the subject number is between `minOrMax` and `maxOrMin`.
  584. *
  585. * @param number - A subject number to check.
  586. * @param minOrMax - A min or max number.
  587. * @param maxOrMin - A max or min number.
  588. * @param exclusive - Optional. Whether to exclude `x` or `y`.
  589. */
  590. function between(number, minOrMax, maxOrMin, exclusive) {
  591. var min = Math.min(minOrMax, maxOrMin);
  592. var max = Math.max(minOrMax, maxOrMin);
  593. return exclusive ? min < number && number < max : min <= number && number <= max;
  594. }
  595. var max$1 = Math.max,
  596. min$1 = Math.min;
  597. /**
  598. * Clamps a number.
  599. *
  600. * @param number - A subject number to check.
  601. * @param x - A min or max number.
  602. * @param y - A min or max number.
  603. */
  604. function clamp(number, x, y) {
  605. var minimum = min$1(x, y);
  606. var maximum = max$1(x, y);
  607. return min$1(max$1(minimum, number), maximum);
  608. }
  609. /**
  610. * Returns the sign of the provided number.
  611. *
  612. * @param x - A number.
  613. *
  614. * @return `1` for positive numbers, `-1` for negative numbers, or `0` for `0`.
  615. */
  616. function sign(x) {
  617. return +(x > 0) - +(x < 0);
  618. }
  619. var min = Math.min,
  620. max = Math.max,
  621. floor = Math.floor,
  622. ceil = Math.ceil,
  623. abs = Math.abs,
  624. round = Math.round;
  625. var index = /*#__PURE__*/Object.freeze({
  626. __proto__: null,
  627. empty: empty,
  628. forEach: forEach,
  629. includes: includes,
  630. push: push,
  631. toArray: toArray,
  632. arrayProto: arrayProto,
  633. slice: slice,
  634. splice: splice,
  635. find: find,
  636. addClass: addClass,
  637. append: append,
  638. before: before,
  639. child: child,
  640. children: children,
  641. create: create,
  642. display: display,
  643. getAttribute: getAttribute,
  644. hasClass: hasClass,
  645. matches: matches,
  646. parseHtml: parseHtml,
  647. prevent: prevent,
  648. query: query,
  649. queryAll: queryAll,
  650. rect: rect,
  651. remove: remove,
  652. removeAttribute: removeAttribute,
  653. removeClass: removeClass,
  654. setAttribute: setAttribute,
  655. style: style,
  656. toggleClass: toggleClass,
  657. unit: unit,
  658. assert: assert,
  659. nextTick: nextTick,
  660. noop: noop,
  661. raf: raf,
  662. between: between,
  663. clamp: clamp,
  664. sign: sign,
  665. min: min,
  666. max: max,
  667. floor: floor,
  668. ceil: ceil,
  669. abs: abs,
  670. round: round,
  671. assign: assign,
  672. forOwn: forOwn,
  673. merge: merge,
  674. isObject: isObject,
  675. isArray: isArray,
  676. isFunction: isFunction,
  677. isString: isString,
  678. isUndefined: isUndefined,
  679. isNull: isNull,
  680. isHTMLElement: isHTMLElement,
  681. isHTMLButtonElement: isHTMLButtonElement
  682. });
  683. /**
  684. * The component for managing options.
  685. *
  686. * @since 3.0.0
  687. *
  688. * @param Splide - A Splide instance.
  689. * @param Components - A collection of components.
  690. * @param options - Options.
  691. *
  692. * @return An Options component object.
  693. */
  694. function Options(Splide, Components, options) {
  695. try {
  696. merge(options, JSON.parse(getAttribute(Splide.root, DATA_ATTRIBUTE)));
  697. } catch (e) {
  698. assert(false, e.message);
  699. }
  700. var initialOptions = merge({}, options);
  701. var breakpoints = options.breakpoints;
  702. /**
  703. * Stores breakpoints with the MediaQueryList object.
  704. */
  705. var points;
  706. /**
  707. * Holds the current breakpoint.
  708. */
  709. var currPoint;
  710. /**
  711. * Called when the component is mounted.
  712. */
  713. function mount() {
  714. if (breakpoints) {
  715. points = Object.keys(breakpoints).sort(function (n, m) {
  716. return +n - +m;
  717. }).map(function (point) {
  718. return [point, matchMedia("(" + (options.mediaQuery || 'max') + "-width:" + point + "px)")];
  719. });
  720. addEventListener('resize', observe);
  721. observe();
  722. }
  723. }
  724. /**
  725. * Destroys the component.
  726. *
  727. * @param completely - Will be `true` for complete destruction.
  728. */
  729. function destroy(completely) {
  730. if (completely) {
  731. removeEventListener('resize', observe);
  732. }
  733. }
  734. /**
  735. * Observes breakpoints.
  736. * The `currPoint` may be `undefined`.
  737. */
  738. function observe() {
  739. var item = find(points, function (item) {
  740. return item[1].matches;
  741. }) || [];
  742. if (item[0] !== currPoint) {
  743. onMatch(currPoint = item[0]);
  744. }
  745. }
  746. /**
  747. * Called when the media query matches breakpoints.
  748. *
  749. * @param point - A matched point, or `undefined` that means no breakpoint matches a media query.
  750. */
  751. function onMatch(point) {
  752. var options = breakpoints[point] || initialOptions;
  753. if (options.destroy) {
  754. Splide.options = initialOptions;
  755. Splide.destroy(options.destroy === 'completely');
  756. } else {
  757. if (Splide.state.is(DESTROYED)) {
  758. destroy(true);
  759. Splide.mount();
  760. }
  761. Splide.options = options;
  762. }
  763. }
  764. return {
  765. mount: mount,
  766. destroy: destroy
  767. };
  768. }
  769. /**
  770. * Enumerates slides from left to right.
  771. */
  772. /**
  773. * Enumerates slides from right to left.
  774. */
  775. var RTL = 'rtl';
  776. /**
  777. * Enumerates slides in a col.
  778. */
  779. var TTB = 'ttb';
  780. /**
  781. * The translation map for directions.
  782. *
  783. * @since 3.0.0
  784. */
  785. var ORIENTATION_MAP = {
  786. marginRight: ['marginBottom', 'marginLeft'],
  787. width: ['height'],
  788. autoWidth: ['autoHeight'],
  789. fixedWidth: ['fixedHeight'],
  790. paddingLeft: ['paddingTop', 'paddingRight'],
  791. paddingRight: ['paddingBottom', 'paddingLeft'],
  792. left: ['top', 'right'],
  793. right: ['bottom', 'left'],
  794. x: ['y'],
  795. X: ['Y'],
  796. pageX: ['pageY'],
  797. ArrowLeft: ['ArrowUp', 'ArrowRight'],
  798. ArrowRight: ['ArrowDown', 'ArrowLeft']
  799. };
  800. /**
  801. * The component that absorbs the difference among directions.
  802. *
  803. * @since 3.0.0
  804. *
  805. * @param Splide - A Splide instance.
  806. * @param Components - A collection of components.
  807. * @param options - Options.
  808. *
  809. * @return A Direction component object.
  810. */
  811. function Direction(Splide, Components, options) {
  812. /**
  813. * Resolves the provided property name.
  814. *
  815. * @param prop - A property name to translate.
  816. * @param axisOnly - Optional. If `ture`, returns the same property for LTR and RTL.
  817. */
  818. function resolve(prop, axisOnly) {
  819. var direction = options.direction;
  820. var index = direction === RTL && !axisOnly ? 1 : direction === TTB ? 0 : -1;
  821. return ORIENTATION_MAP[prop][index] || prop;
  822. }
  823. /**
  824. * Orients the value towards the current direction.
  825. *
  826. * @param value - A value to orient.
  827. *
  828. * @return The oriented value.
  829. */
  830. function orient(value) {
  831. return value * (options.direction === RTL ? 1 : -1);
  832. }
  833. return {
  834. resolve: resolve,
  835. orient: orient
  836. };
  837. }
  838. var CLASS_ROOT = PROJECT_CODE;
  839. var CLASS_SLIDER = PROJECT_CODE + "__slider";
  840. var CLASS_TRACK = PROJECT_CODE + "__track";
  841. var CLASS_LIST = PROJECT_CODE + "__list";
  842. var CLASS_SLIDE = PROJECT_CODE + "__slide";
  843. var CLASS_CLONE = CLASS_SLIDE + "--clone";
  844. var CLASS_CONTAINER = CLASS_SLIDE + "__container";
  845. var CLASS_ARROWS = PROJECT_CODE + "__arrows";
  846. var CLASS_ARROW = PROJECT_CODE + "__arrow";
  847. var CLASS_ARROW_PREV = CLASS_ARROW + "--prev";
  848. var CLASS_ARROW_NEXT = CLASS_ARROW + "--next";
  849. var CLASS_PAGINATION = PROJECT_CODE + "__pagination";
  850. var CLASS_PAGINATION_PAGE = CLASS_PAGINATION + "__page";
  851. var CLASS_PROGRESS = PROJECT_CODE + "__progress";
  852. var CLASS_PROGRESS_BAR = CLASS_PROGRESS + "__bar";
  853. var CLASS_AUTOPLAY = PROJECT_CODE + "__autoplay";
  854. var CLASS_PLAY = PROJECT_CODE + "__play";
  855. var CLASS_PAUSE = PROJECT_CODE + "__pause";
  856. var CLASS_SPINNER = PROJECT_CODE + "__spinner";
  857. var CLASS_INITIALIZED = 'is-initialized';
  858. var CLASS_ACTIVE = 'is-active';
  859. var CLASS_PREV = 'is-prev';
  860. var CLASS_NEXT = 'is-next';
  861. var CLASS_VISIBLE = 'is-visible';
  862. var CLASS_LOADING = 'is-loading';
  863. /**
  864. * The array with all status classes.
  865. *
  866. * @since 3.0.0
  867. */
  868. var STATUS_CLASSES = [CLASS_ACTIVE, CLASS_VISIBLE, CLASS_PREV, CLASS_NEXT, CLASS_LOADING];
  869. /**
  870. * The collection of classes for elements that Splide dynamically creates.
  871. *
  872. * @since 3.0.0
  873. */
  874. var CLASSES = {
  875. slide: CLASS_SLIDE,
  876. clone: CLASS_CLONE,
  877. arrows: CLASS_ARROWS,
  878. arrow: CLASS_ARROW,
  879. prev: CLASS_ARROW_PREV,
  880. next: CLASS_ARROW_NEXT,
  881. pagination: CLASS_PAGINATION,
  882. page: CLASS_PAGINATION_PAGE,
  883. spinner: CLASS_SPINNER
  884. };
  885. var EVENT_MOUNTED = 'mounted';
  886. var EVENT_READY = 'ready';
  887. var EVENT_MOVE = 'move';
  888. var EVENT_MOVED = 'moved';
  889. var EVENT_CLICK = 'click';
  890. var EVENT_ACTIVE = 'active';
  891. var EVENT_INACTIVE = 'inactive';
  892. var EVENT_VISIBLE = 'visible';
  893. var EVENT_HIDDEN = 'hidden';
  894. var EVENT_SLIDE_KEYDOWN = 'slide:keydown';
  895. var EVENT_REFRESH = 'refresh';
  896. var EVENT_UPDATED = 'undated';
  897. var EVENT_RESIZE = 'resize';
  898. var EVENT_RESIZED = 'resized';
  899. var EVENT_DRAG = 'drag';
  900. var EVENT_DRAGGING = 'dragging';
  901. var EVENT_DRAGGED = 'dragged';
  902. var EVENT_SCROLL = 'scroll';
  903. var EVENT_SCROLLED = 'scrolled';
  904. var EVENT_DESTROY = 'destroy';
  905. var EVENT_ARROWS_MOUNTED = 'arrows:mounted';
  906. var EVENT_ARROWS_UPDATED = 'arrows:updated';
  907. var EVENT_PAGINATION_MOUNTED = 'pagination:mounted';
  908. var EVENT_PAGINATION_PAGE = 'pagination:page';
  909. var EVENT_PAGINATION_UPDATED = 'pagination:updated';
  910. var EVENT_NAVIGATION_MOUNTED = 'navigation:mounted';
  911. var EVENT_AUTOPLAY_PLAY = 'autoplay:play';
  912. var EVENT_AUTOPLAY_PLAYING = 'autoplay:playing';
  913. var EVENT_AUTOPLAY_PAUSE = 'autoplay:pause';
  914. var EVENT_LAZYLOAD_LOADED = 'lazyload:loaded';
  915. /**
  916. * The constructor to provided a simple event system.
  917. *
  918. * @since 3.0.0
  919. *
  920. * @return An EventBus object.
  921. */
  922. function EventBus() {
  923. /**
  924. * The collection of registered handlers.
  925. */
  926. var handlers = {};
  927. /**
  928. * Registers an event handler.
  929. *
  930. * @param events - An event name or names separated by spaces. Use a dot(.) to add a namespace.
  931. * @param callback - A callback function to register.
  932. * @param key - Optional. An object for an identifier of the handler.
  933. * @param priority - Optional. A priority number for the order in which the callbacks are invoked.
  934. * Lower numbers correspond with earlier execution. The default value is 10.
  935. */
  936. function on(events, callback, key, priority) {
  937. if (priority === void 0) {
  938. priority = 10;
  939. }
  940. forEachEvent(events, function (event, namespace) {
  941. handlers[event] = handlers[event] || [];
  942. push(handlers[event], {
  943. event: event,
  944. callback: callback,
  945. namespace: namespace,
  946. priority: priority,
  947. key: key
  948. }).sort(function (handler1, handler2) {
  949. return handler1.priority - handler2.priority;
  950. });
  951. });
  952. }
  953. /**
  954. * Removes event handlers registered by `on()`.
  955. * If only the event name is provided, all handlers that associate with the event are removed.
  956. * If the event name and namespace are specified, handlers that associate with the event and namespace are removed.
  957. *
  958. * @param events - An event name or names separated by spaces. Use a dot(.) to add a namespace.
  959. * @param key - Optional. An object for an identifier of the handler.
  960. */
  961. function off(events, key) {
  962. forEachEvent(events, function (event, namespace) {
  963. var eventHandlers = handlers[event];
  964. handlers[event] = eventHandlers && eventHandlers.filter(function (handler) {
  965. return handler.key ? handler.key !== key : handler.namespace !== namespace;
  966. });
  967. });
  968. }
  969. /**
  970. * Removes all handlers locked by the specified key.
  971. *
  972. * @param key - A key.
  973. */
  974. function offBy(key) {
  975. forOwn(handlers, function (eventHandlers, event) {
  976. off(event, key);
  977. });
  978. }
  979. /**
  980. * Triggers callback functions.
  981. * This accepts additional arguments and passes them to callbacks.
  982. *
  983. * @param event - An event name.
  984. */
  985. function emit(event) {
  986. var _arguments = arguments;
  987. (handlers[event] || []).forEach(function (handler) {
  988. handler.callback.apply(handler, slice(_arguments, 1));
  989. });
  990. }
  991. /**
  992. * Removes all handlers.
  993. */
  994. function destroy() {
  995. handlers = {};
  996. }
  997. /**
  998. * Parses provided events and iterates over them.
  999. *
  1000. * @param events - An event or events.
  1001. * @param iteratee - An iteratee function.
  1002. */
  1003. function forEachEvent(events, iteratee) {
  1004. toArray(events).join(' ').split(' ').forEach(function (eventNS) {
  1005. var fragments = eventNS.split('.');
  1006. iteratee(fragments[0], fragments[1]);
  1007. });
  1008. }
  1009. return {
  1010. on: on,
  1011. off: off,
  1012. offBy: offBy,
  1013. emit: emit,
  1014. destroy: destroy
  1015. };
  1016. }
  1017. /**
  1018. * The function that provides interface for internal and native events.
  1019. *
  1020. * @since 3.0.0
  1021. *
  1022. * @param Splide - A Splide instance.
  1023. *
  1024. * @return A collection of interface functions.
  1025. */
  1026. function EventInterface(Splide) {
  1027. /**
  1028. * Holds the event object.
  1029. */
  1030. var event = Splide.event;
  1031. /**
  1032. * The key for events.
  1033. */
  1034. var key = {};
  1035. /**
  1036. * Stores all handlers that listen to native events.
  1037. */
  1038. var listeners = [];
  1039. /**
  1040. * Registers an event handler with an unique key.
  1041. * It can only be removed by `off()` method below.
  1042. *
  1043. * @param events - An event name or names separated by spaces. Use a dot(.) to add a namespace.
  1044. * @param callback - A callback function to register.
  1045. * @param priority - Optional. A priority number for the order in which the callbacks are invoked.
  1046. * Lower numbers correspond with earlier execution. The default value is 10.
  1047. */
  1048. function on(events, callback, priority) {
  1049. event.on(events, callback, key, priority);
  1050. }
  1051. /**
  1052. * Removes event handlers registered by `on()`.
  1053. *
  1054. * @param events - An event name or names separated by spaces. Use a dot(.) to add a namespace.
  1055. */
  1056. function off(events) {
  1057. event.off(events, key);
  1058. }
  1059. /**
  1060. * Listens to native events.
  1061. * Splide#destory() will remove all registered listeners.
  1062. *
  1063. * @param targets - A target element, the window object or the document object.
  1064. * @param events - An event or events to listen to.
  1065. * @param callback - A callback function.
  1066. * @param options - Optional. The options to pass to the `addEventListener` function.
  1067. */
  1068. function bind(targets, events, callback, options) {
  1069. forEachEvent(targets, events, function (target, event) {
  1070. listeners.push([target, event, callback, options]);
  1071. target.addEventListener(event, callback, options);
  1072. });
  1073. }
  1074. /**
  1075. * Removes the event handler.
  1076. *
  1077. * @param targets - A target element, the window object or the document object.
  1078. * @param events - An event name or names to remove.
  1079. */
  1080. function unbind(targets, events) {
  1081. forEachEvent(targets, events, function (target, event) {
  1082. listeners = listeners.filter(function (listener) {
  1083. if (listener[0] === target && listener[1] === event) {
  1084. target.removeEventListener(event, listener[2], listener[3]);
  1085. return false;
  1086. }
  1087. return true;
  1088. });
  1089. });
  1090. }
  1091. /**
  1092. * Iterates over each target and event.
  1093. *
  1094. * @param targets - A target element, the window object or the document object.
  1095. * @param events - An event name or names.
  1096. * @param iteratee - An iteratee function.
  1097. */
  1098. function forEachEvent(targets, events, iteratee) {
  1099. forEach(targets, function (target) {
  1100. if (target) {
  1101. events.split(' ').forEach(iteratee.bind(null, target));
  1102. }
  1103. });
  1104. }
  1105. /**
  1106. * Removes all listeners.
  1107. */
  1108. function destroy() {
  1109. listeners = listeners.filter(function (data) {
  1110. return unbind(data[0], data[1]);
  1111. });
  1112. event.offBy(key);
  1113. }
  1114. /**
  1115. * Invokes destroy when the slider is destroyed.
  1116. */
  1117. event.on(EVENT_DESTROY, destroy, key);
  1118. return {
  1119. on: on,
  1120. off: off,
  1121. emit: event.emit,
  1122. bind: bind,
  1123. unbind: unbind,
  1124. destroy: destroy
  1125. };
  1126. }
  1127. /**
  1128. * Requests interval like the native `setInterval()` with using `requestAnimationFrame`.
  1129. *
  1130. * @since 3.0.0
  1131. *
  1132. * @param interval - The interval duration in milliseconds.
  1133. * @param onInterval - The callback fired on every interval.
  1134. * @param onUpdate - Optional. Called on every animation frame, taking the progress rate.
  1135. * @param limit - Optional. Limits the number of interval.
  1136. */
  1137. function RequestInterval(interval, onInterval, onUpdate, limit) {
  1138. var now = Date.now;
  1139. /**
  1140. * The time when the interval starts.
  1141. */
  1142. var startTime;
  1143. /**
  1144. * The current progress rate.
  1145. */
  1146. var rate = 0;
  1147. /**
  1148. * The animation frame ID.
  1149. */
  1150. var id;
  1151. /**
  1152. * Indicates whether the interval is currently paused or not.
  1153. */
  1154. var paused = true;
  1155. /**
  1156. * The loop count. This only works when the `limit` argument is provided.
  1157. */
  1158. var count = 0;
  1159. /**
  1160. * The update function called on every animation frame.
  1161. */
  1162. function update() {
  1163. if (!paused) {
  1164. var elapsed = now() - startTime;
  1165. if (elapsed >= interval) {
  1166. rate = 1;
  1167. startTime = now();
  1168. } else {
  1169. rate = elapsed / interval;
  1170. }
  1171. if (onUpdate) {
  1172. onUpdate(rate);
  1173. }
  1174. if (rate === 1) {
  1175. onInterval();
  1176. if (limit && ++count >= limit) {
  1177. pause();
  1178. return;
  1179. }
  1180. }
  1181. raf(update);
  1182. }
  1183. }
  1184. /**
  1185. * Starts the interval.
  1186. *
  1187. * @param resume - Optional. Whether to resume the paused progress or not.
  1188. */
  1189. function start(resume) {
  1190. !resume && cancel();
  1191. startTime = now() - (resume ? rate * interval : 0);
  1192. paused = false;
  1193. raf(update);
  1194. }
  1195. /**
  1196. * Pauses the interval.
  1197. */
  1198. function pause() {
  1199. paused = true;
  1200. }
  1201. /**
  1202. * Rewinds the current progress.
  1203. */
  1204. function rewind() {
  1205. startTime = now();
  1206. rate = 0;
  1207. if (onUpdate) {
  1208. onUpdate(rate);
  1209. }
  1210. }
  1211. /**
  1212. * Cancels the interval.
  1213. */
  1214. function cancel() {
  1215. cancelAnimationFrame(id);
  1216. rate = 0;
  1217. id = 0;
  1218. paused = true;
  1219. }
  1220. /**
  1221. * Checks if the interval is paused or not.
  1222. *
  1223. * @return `true` if the interval is paused, or otherwise `false`.
  1224. */
  1225. function isPaused() {
  1226. return paused;
  1227. }
  1228. return {
  1229. start: start,
  1230. rewind: rewind,
  1231. pause: pause,
  1232. cancel: cancel,
  1233. isPaused: isPaused
  1234. };
  1235. }
  1236. /**
  1237. * The function providing a super simple state system.
  1238. *
  1239. * @param initialState - Specifies the initial state.
  1240. */
  1241. function State(initialState) {
  1242. /**
  1243. * The current state.
  1244. */
  1245. var state = initialState;
  1246. /**
  1247. * Sets a new state.
  1248. *
  1249. * @param value - A new state value.
  1250. */
  1251. function set(value) {
  1252. state = value;
  1253. }
  1254. /**
  1255. * Checks if the current state matches the provided one.
  1256. *
  1257. * @param states - A state to check.
  1258. *
  1259. * @return `true` if the current state is the provided one.
  1260. */
  1261. function is(states) {
  1262. return includes(toArray(states), state);
  1263. }
  1264. return {
  1265. set: set,
  1266. is: is
  1267. };
  1268. }
  1269. /**
  1270. * Returns the throttled function.
  1271. *
  1272. * @param func - A function to throttle.
  1273. * @param duration - Optional. Throttle duration in milliseconds.
  1274. *
  1275. * @return A throttled function.
  1276. */
  1277. function Throttle(func, duration) {
  1278. var interval;
  1279. function throttled() {
  1280. var _arguments2 = arguments,
  1281. _this = this;
  1282. if (!interval) {
  1283. interval = RequestInterval(duration || 0, function () {
  1284. func.apply(_this, _arguments2);
  1285. interval = null;
  1286. }, null, 1);
  1287. interval.start();
  1288. }
  1289. }
  1290. return throttled;
  1291. }
  1292. /**
  1293. * Formats a string.
  1294. *
  1295. * @param string - A string to format.
  1296. * @param replacements - A replacement or replacements.
  1297. *
  1298. * @return A formatted string.
  1299. */
  1300. function format(string, replacements) {
  1301. forEach(replacements, function (replacement) {
  1302. string = string.replace('%s', "" + replacement);
  1303. });
  1304. return string;
  1305. }
  1306. /**
  1307. * Pads the number with 0.
  1308. *
  1309. * @param number - A number to pad.
  1310. *
  1311. * @return string - Padded number.
  1312. */
  1313. function pad(number) {
  1314. return number < 10 ? "0" + number : "" + number;
  1315. }
  1316. /**
  1317. * Stores unique IDs.
  1318. *
  1319. * @since 3.0.0
  1320. */
  1321. var ids = {};
  1322. /**
  1323. * Returns a sequential unique ID as "{ prefix }-{ number }".
  1324. *
  1325. * @param prefix - A prefix for the ID.
  1326. */
  1327. function uniqueId(prefix) {
  1328. return "" + prefix + pad(ids[prefix] = (ids[prefix] || 0) + 1);
  1329. }
  1330. /**
  1331. * The component that collects and handles elements which the slider consists of.
  1332. *
  1333. * @since 3.0.0
  1334. *
  1335. * @param Splide - A Splide instance.
  1336. * @param Components - A collection of components.
  1337. * @param options - Options.
  1338. *
  1339. * @return An Elements component object.
  1340. */
  1341. function Elements(Splide, Components, options) {
  1342. var _EventInterface = EventInterface(Splide),
  1343. on = _EventInterface.on;
  1344. var root = Splide.root;
  1345. var elements = {};
  1346. /**
  1347. * Stores all slide elements.
  1348. */
  1349. var slides = [];
  1350. /**
  1351. * Stores all root classes.
  1352. */
  1353. var classes;
  1354. /**
  1355. * The slider element that may be `undefined`.
  1356. */
  1357. var slider;
  1358. /**
  1359. * The track element.
  1360. */
  1361. var track;
  1362. /**
  1363. * The list element.
  1364. */
  1365. var list;
  1366. /**
  1367. * Called when the component is mounted.
  1368. */
  1369. function mount() {
  1370. init();
  1371. identify();
  1372. on(EVENT_REFRESH, function () {
  1373. destroy();
  1374. init();
  1375. });
  1376. on(EVENT_UPDATED, function () {
  1377. removeClass(root, classes);
  1378. addClass(root, classes = getClasses());
  1379. });
  1380. }
  1381. /**
  1382. * Initializes the component.
  1383. */
  1384. function init() {
  1385. collect();
  1386. addClass(root, classes = getClasses());
  1387. }
  1388. /**
  1389. * Destroys the component.
  1390. */
  1391. function destroy() {
  1392. empty(slides);
  1393. removeClass(root, classes);
  1394. }
  1395. /**
  1396. * Collects elements which the slider consists of.
  1397. */
  1398. function collect() {
  1399. slider = child(root, "." + CLASS_SLIDER);
  1400. track = query(root, "." + CLASS_TRACK);
  1401. list = child(track, "." + CLASS_LIST);
  1402. assert(track && list, 'Missing a track/list element.');
  1403. push(slides, children(list, "." + CLASS_SLIDE + ":not(." + CLASS_CLONE + ")"));
  1404. var autoplay = find("." + CLASS_AUTOPLAY);
  1405. var arrows = find("." + CLASS_ARROWS);
  1406. assign(elements, {
  1407. root: root,
  1408. slider: slider,
  1409. track: track,
  1410. list: list,
  1411. slides: slides,
  1412. arrows: arrows,
  1413. prev: query(arrows, "." + CLASS_ARROW_PREV),
  1414. next: query(arrows, "." + CLASS_ARROW_NEXT),
  1415. bar: query(find("." + CLASS_PROGRESS), "." + CLASS_PROGRESS_BAR),
  1416. play: query(autoplay, "." + CLASS_PLAY),
  1417. pause: query(autoplay, "." + CLASS_PAUSE)
  1418. });
  1419. }
  1420. /**
  1421. * Assigns unique IDs to essential elements.
  1422. */
  1423. function identify() {
  1424. var id = root.id || uniqueId(PROJECT_CODE);
  1425. root.id = id;
  1426. track.id = track.id || id + "-track";
  1427. list.id = list.id || id + "-list";
  1428. }
  1429. /**
  1430. * Finds an element only in children of the root or slider element.
  1431. *
  1432. * @return {Element} - A found element or undefined.
  1433. */
  1434. function find(selector) {
  1435. return child(root, selector) || child(slider, selector);
  1436. }
  1437. /**
  1438. * Return an array with classes for the root element.
  1439. *
  1440. * @return An array with classes.
  1441. */
  1442. function getClasses() {
  1443. return [CLASS_ROOT + "--" + options.type, CLASS_ROOT + "--" + options.direction, options.drag && CLASS_ROOT + "--draggable", options.isNavigation && CLASS_ROOT + "--nav", CLASS_ACTIVE];
  1444. }
  1445. return assign(elements, {
  1446. mount: mount,
  1447. destroy: destroy
  1448. });
  1449. }
  1450. /**
  1451. * The component for managing styles of the slider.
  1452. *
  1453. * @since 3.0.0
  1454. *
  1455. * @return A Style component object.
  1456. */
  1457. function Style() {
  1458. /**
  1459. * The style element for the slider.
  1460. */
  1461. var style;
  1462. /**
  1463. * The CSSStyleSheet object of the created style element.
  1464. */
  1465. var sheet;
  1466. /**
  1467. * Called when the component is mounted.
  1468. */
  1469. function mount() {
  1470. style = create('style', {}, document.head);
  1471. sheet = style.sheet;
  1472. }
  1473. /**
  1474. * Destroys the component.
  1475. */
  1476. function destroy() {
  1477. remove(style);
  1478. sheet = null;
  1479. }
  1480. /**
  1481. * Registers the style for the selector.
  1482. *
  1483. * @param selector - A selector string.
  1484. * @param prop - A CSS property, accepting the camel case.
  1485. * @param value - A CSS value.
  1486. */
  1487. function rule(selector, prop, value) {
  1488. var _sheet = sheet,
  1489. cssRules = _sheet.cssRules;
  1490. var cssRule = find(cssRules, function (cssRule) {
  1491. return isCSSStyleRule(cssRule) && cssRule.selectorText === selector;
  1492. }) || cssRules[sheet.insertRule(selector + "{}", 0)];
  1493. if (isCSSStyleRule(cssRule)) {
  1494. cssRule.style[prop] = "" + value;
  1495. }
  1496. }
  1497. /**
  1498. * Registers the style by the element or the ID.
  1499. *
  1500. * @param target - A target element or ID.
  1501. * @param prop - A CSS property, accepting the camel case.
  1502. * @param value - A CSS value.
  1503. */
  1504. function ruleBy(target, prop, value) {
  1505. rule("#" + (isHTMLElement(target) ? target.id : target), prop, value);
  1506. }
  1507. /**
  1508. * Checks if the provided rule is a CSSStyleRule instance or not.
  1509. *
  1510. * @param cssRule - An instance to check.
  1511. *
  1512. * @return `true` if the cssRule is an instance of CSSStyleRule, or otherwise `false`.
  1513. */
  1514. function isCSSStyleRule(cssRule) {
  1515. return cssRule instanceof CSSStyleRule;
  1516. }
  1517. return {
  1518. mount: mount,
  1519. destroy: destroy,
  1520. rule: rule,
  1521. ruleBy: ruleBy
  1522. };
  1523. }
  1524. var ROLE = 'role';
  1525. var ARIA_CONTROLS = 'aria-controls';
  1526. var ARIA_CURRENT = 'aria-current';
  1527. var ARIA_LABEL = 'aria-label';
  1528. var ARIA_HIDDEN = 'aria-hidden';
  1529. var TAB_INDEX = 'tabindex';
  1530. var DISABLED = 'disabled';
  1531. /**
  1532. * The array with all attributes.
  1533. *
  1534. * @since 3.0.0
  1535. */
  1536. var ALL_ATTRIBUTES = [ROLE, ARIA_CONTROLS, ARIA_CURRENT, ARIA_LABEL, ARIA_HIDDEN, TAB_INDEX, DISABLED];
  1537. /**
  1538. * The type for the regular slider.
  1539. *
  1540. * @since 3.0.0
  1541. */
  1542. var SLIDE = 'slide';
  1543. /**
  1544. * The type for the carousel slider.
  1545. *
  1546. * @since 3.0.0
  1547. */
  1548. var LOOP = 'loop';
  1549. /**
  1550. * The type for the fade slider that can not have multiple slides in a page.
  1551. *
  1552. * @since 3.0.0
  1553. */
  1554. var FADE = 'fade';
  1555. /**
  1556. * The sub component for managing each slide.
  1557. *
  1558. * @since 3.0.0
  1559. *
  1560. * @param Splide - A Splide instance.
  1561. * @param index - A slide index.
  1562. * @param slideIndex - A slide index for clones. This must be `-1` if the slide is not clone.
  1563. * @param slide - A slide element.
  1564. *
  1565. * @return A Slide sub component.
  1566. */
  1567. function Slide$1(Splide, index, slideIndex, slide) {
  1568. var _EventInterface2 = EventInterface(Splide),
  1569. on = _EventInterface2.on,
  1570. emit = _EventInterface2.emit,
  1571. bind = _EventInterface2.bind,
  1572. destroyEvents = _EventInterface2.destroy;
  1573. var Components = Splide.Components,
  1574. root = Splide.root,
  1575. options = Splide.options;
  1576. var isNavigation = options.isNavigation,
  1577. updateOnMove = options.updateOnMove;
  1578. var resolve = Components.Direction.resolve;
  1579. var isClone = slideIndex > -1;
  1580. var container = child(slide, "." + CLASS_CONTAINER);
  1581. /**
  1582. * Called when the component is mounted.
  1583. */
  1584. function mount() {
  1585. var _this2 = this;
  1586. init();
  1587. bind(slide, 'click keydown', function (e) {
  1588. emit(e.type === 'click' ? EVENT_CLICK : EVENT_SLIDE_KEYDOWN, _this2, e);
  1589. });
  1590. on([EVENT_MOUNTED, EVENT_MOVED, EVENT_UPDATED, EVENT_RESIZED, EVENT_SCROLLED], update.bind(this));
  1591. if (updateOnMove) {
  1592. on(EVENT_MOVE, onMove);
  1593. }
  1594. }
  1595. /**
  1596. * If the `updateOnMove` option is `true`, called when the slider starts moving.
  1597. *
  1598. * @param next - A next index.
  1599. * @param prev - A previous index.
  1600. * @param dest - A destination index.
  1601. */
  1602. function onMove(next, prev, dest) {
  1603. if (dest === index) {
  1604. updateActivity.call(this, true);
  1605. }
  1606. update.call(this);
  1607. }
  1608. /**
  1609. * Initializes the component.
  1610. */
  1611. function init() {
  1612. if (!isClone) {
  1613. slide.id = root.id + "-slide" + pad(index + 1);
  1614. }
  1615. if (isNavigation) {
  1616. if (!isHTMLButtonElement(slide)) {
  1617. setAttribute(slide, ROLE, 'button');
  1618. }
  1619. var idx = isClone ? slideIndex : index;
  1620. var label = format(options.i18n.slideX, idx + 1);
  1621. var controls = Splide.splides.map(function (splide) {
  1622. return splide.root.id;
  1623. }).join(' ');
  1624. setAttribute(slide, ARIA_LABEL, label);
  1625. setAttribute(slide, ARIA_CONTROLS, controls);
  1626. }
  1627. }
  1628. /**
  1629. * Destroys the component.
  1630. */
  1631. function destroy() {
  1632. destroyEvents();
  1633. removeClass(slide, STATUS_CLASSES);
  1634. removeAttribute(slide, ALL_ATTRIBUTES);
  1635. }
  1636. /**
  1637. * Updates attribute and classes of the slide.
  1638. */
  1639. function update() {
  1640. if (Components.Controller) {
  1641. var currIndex = Splide.index;
  1642. updateActivity.call(this, isActive());
  1643. updateVisibility.call(this, isVisible());
  1644. toggleClass(slide, CLASS_PREV, index === currIndex - 1);
  1645. toggleClass(slide, CLASS_NEXT, index === currIndex + 1);
  1646. }
  1647. }
  1648. /**
  1649. * Updates the status related with activity.
  1650. *
  1651. * @param active - Set `true` if the slide is active.
  1652. */
  1653. function updateActivity(active) {
  1654. toggleClass(slide, CLASS_ACTIVE, active);
  1655. if (active) {
  1656. if (!hasClass(slide, CLASS_ACTIVE)) {
  1657. isNavigation && setAttribute(slide, ARIA_CURRENT, true);
  1658. emit(EVENT_ACTIVE, this);
  1659. }
  1660. } else {
  1661. if (hasClass(slide, CLASS_ACTIVE)) {
  1662. removeAttribute(slide, ARIA_CURRENT);
  1663. emit(EVENT_INACTIVE, this);
  1664. }
  1665. }
  1666. }
  1667. /**
  1668. * Updates the status related with visibility.
  1669. *
  1670. * @param visible - Set `true` if the slide is visible.
  1671. */
  1672. function updateVisibility(visible) {
  1673. toggleClass(slide, CLASS_VISIBLE, visible);
  1674. setAttribute(slide, ARIA_HIDDEN, !visible || null);
  1675. setAttribute(slide, TAB_INDEX, visible && options.slideFocus ? 0 : null);
  1676. if (visible) {
  1677. if (!hasClass(slide, CLASS_VISIBLE)) {
  1678. emit(EVENT_VISIBLE, this);
  1679. }
  1680. } else {
  1681. if (hasClass(slide, CLASS_VISIBLE)) {
  1682. emit(EVENT_HIDDEN, this);
  1683. }
  1684. }
  1685. }
  1686. /**
  1687. * Adds a CSS rule to the slider or the container.
  1688. *
  1689. * @param prop - A property name.
  1690. * @param value - A CSS value to add.
  1691. * @param useContainer - Optional. Determines whether to apply the rule to the container or not.
  1692. */
  1693. function rule(prop, value, useContainer) {
  1694. var selector = "#" + slide.id + (container && useContainer ? " > ." + CLASS_CONTAINER : '');
  1695. Components.Style.rule(selector, prop, value);
  1696. }
  1697. /**
  1698. * Checks if the slide is active or not.
  1699. *
  1700. * @return `true` if the slide is active.
  1701. */
  1702. function isActive() {
  1703. return Splide.index === index;
  1704. }
  1705. /**
  1706. * Checks if the slide is visible or not.
  1707. */
  1708. function isVisible() {
  1709. if (Splide.is(FADE)) {
  1710. return isActive();
  1711. }
  1712. var trackRect = rect(Components.Elements.track);
  1713. var slideRect = rect(slide);
  1714. var left = resolve('left');
  1715. var right = resolve('right');
  1716. return floor(trackRect[left]) <= slideRect[left] && slideRect[right] <= ceil(trackRect[right]);
  1717. }
  1718. /**
  1719. * Calculates how far this slide is from another slide and
  1720. * returns `true` if the distance is within the given number.
  1721. *
  1722. * @param from - An index of a base slide.
  1723. * @param distance - `true` if the slide is within this number.
  1724. *
  1725. * @return `true` if the slide is within the `distance` from the base slide, or otherwise `false`.
  1726. */
  1727. function isWithin(from, distance) {
  1728. var diff = abs(from - index);
  1729. if (!Splide.is(SLIDE) && !isClone) {
  1730. diff = min(diff, Splide.length - diff);
  1731. }
  1732. return diff <= distance;
  1733. }
  1734. return {
  1735. index: index,
  1736. slideIndex: slideIndex,
  1737. slide: slide,
  1738. container: container,
  1739. isClone: isClone,
  1740. mount: mount,
  1741. destroy: destroy,
  1742. rule: rule,
  1743. isWithin: isWithin
  1744. };
  1745. }
  1746. /**
  1747. * The component for managing all slides include clones.
  1748. *
  1749. * @since 3.0.0
  1750. *
  1751. * @param Splide - A Splide instance.
  1752. * @param Components - A collection of components.
  1753. * @param options - Options.
  1754. *
  1755. * @return An Slides component object.
  1756. */
  1757. function Slides(Splide, Components, options) {
  1758. var _EventInterface3 = EventInterface(Splide),
  1759. on = _EventInterface3.on,
  1760. emit = _EventInterface3.emit,
  1761. bind = _EventInterface3.bind;
  1762. var _Components$Elements = Components.Elements,
  1763. slides = _Components$Elements.slides,
  1764. list = _Components$Elements.list;
  1765. /**
  1766. * Stores all SlideComponent objects.
  1767. */
  1768. var Slides = [];
  1769. /**
  1770. * Called when the component is mounted.
  1771. */
  1772. function mount() {
  1773. init();
  1774. on(EVENT_REFRESH, refresh);
  1775. }
  1776. /**
  1777. * Initializes the component.
  1778. */
  1779. function init() {
  1780. slides.forEach(function (slide, index) {
  1781. register(slide, index, -1);
  1782. });
  1783. }
  1784. /**
  1785. * Destroys the component.
  1786. */
  1787. function destroy() {
  1788. forEach$1(function (Slide) {
  1789. Slide.destroy();
  1790. });
  1791. empty(Slides);
  1792. }
  1793. /**
  1794. * Discards all Slide components and regenerates them.
  1795. */
  1796. function refresh() {
  1797. destroy();
  1798. init();
  1799. }
  1800. /**
  1801. * Registers a slide element and creates a Slide object.
  1802. *
  1803. * @param slide - A slide element to register.
  1804. * @param index - A slide index.
  1805. * @param slideIndex - A slide index for clones. This must be `-1` for regular slides.
  1806. */
  1807. function register(slide, index, slideIndex) {
  1808. var object = Slide$1(Splide, index, slideIndex, slide);
  1809. object.mount();
  1810. Slides.push(object);
  1811. }
  1812. /**
  1813. * Returns all Slide objects.
  1814. *
  1815. * @param excludeClones - Optional. Determines whether to exclude clones or not.
  1816. *
  1817. * @return An array with Slide objects.
  1818. */
  1819. function get(excludeClones) {
  1820. return excludeClones ? filter(function (Slide) {
  1821. return !Slide.isClone;
  1822. }) : Slides;
  1823. }
  1824. /**
  1825. * Returns slides in the specified page.
  1826. *
  1827. * @param page - A page index.
  1828. *
  1829. * @return An array with slides that belong to the page.
  1830. */
  1831. function getIn(page) {
  1832. var Controller = Components.Controller;
  1833. var index = Controller.toIndex(page);
  1834. var max = Controller.hasFocus() ? 1 : options.perPage;
  1835. return filter(function (Slide) {
  1836. return between(Slide.index, index, index + max - 1);
  1837. });
  1838. }
  1839. /**
  1840. * Returns a Slide object at the specified index.
  1841. *
  1842. * @param index - A slide index.
  1843. *
  1844. * @return A Slide object if available, or otherwise `undefined`.
  1845. */
  1846. function getAt(index) {
  1847. return filter(index)[0];
  1848. }
  1849. /**
  1850. * Inserts a slide or slides at a specified index.
  1851. *
  1852. * @param items - A slide element, an HTML string or an array with them.
  1853. * @param index - Optional. An index to insert the slide at. If omitted, appends it to the list.
  1854. */
  1855. function add(items, index) {
  1856. forEach(items, function (slide) {
  1857. if (isString(slide)) {
  1858. slide = parseHtml(slide);
  1859. }
  1860. if (isHTMLElement(slide)) {
  1861. var ref = slides[index];
  1862. ref ? before(slide, ref) : append(list, slide);
  1863. addClass(slide, options.classes.slide);
  1864. observeImages(slide, emit.bind(null, EVENT_RESIZE));
  1865. }
  1866. });
  1867. emit(EVENT_REFRESH);
  1868. }
  1869. /**
  1870. * Removes slides that match the matcher
  1871. * that can be an index, an array with indices, a selector, or an iteratee function.
  1872. *
  1873. * @param matcher - An index, an array with indices, a selector string, or an iteratee function.
  1874. */
  1875. function remove$1(matcher) {
  1876. remove(filter(matcher).map(function (Slide) {
  1877. return Slide.slide;
  1878. }));
  1879. emit(EVENT_REFRESH);
  1880. }
  1881. /**
  1882. * Iterates over Slide objects by the iteratee function.
  1883. *
  1884. * @param iteratee - An iteratee function that takes a Slide object, an index and an array with Slides.
  1885. * @param excludeClones - Optional. Determines whether to exclude clones or not.
  1886. */
  1887. function forEach$1(iteratee, excludeClones) {
  1888. get(excludeClones).forEach(iteratee);
  1889. }
  1890. /**
  1891. * Filters Slides by the matcher
  1892. * that can be an index, an array with indices, a selector, or an predicate function.
  1893. *
  1894. * @param matcher - An index, an array with indices, a selector string, or an predicate function.
  1895. *
  1896. * @return An array with SlideComponent objects.
  1897. */
  1898. function filter(matcher) {
  1899. return Slides.filter(isFunction(matcher) ? matcher : function (Slide) {
  1900. return isString(matcher) ? matches(Slide.slide, matcher) : includes(toArray(matcher), Slide.index);
  1901. });
  1902. }
  1903. /**
  1904. * Adds a CSS rule to all slides or containers.
  1905. *
  1906. * @param prop - A property name.
  1907. * @param value - A CSS value to add.
  1908. * @param useContainer - Optional. Determines whether to apply the rule to the container or not.
  1909. */
  1910. function rule(prop, value, useContainer) {
  1911. forEach$1(function (Slide) {
  1912. Slide.rule(prop, value, useContainer);
  1913. });
  1914. }
  1915. /**
  1916. * Invokes the callback after all images in the element are loaded.
  1917. *
  1918. * @param elm - An element that may contain images.
  1919. * @param callback - A callback function.
  1920. */
  1921. function observeImages(elm, callback) {
  1922. var images = queryAll(elm, 'img');
  1923. var length = images.length;
  1924. if (length) {
  1925. images.forEach(function (img) {
  1926. bind(img, 'load error', function () {
  1927. if (! --length) {
  1928. callback();
  1929. }
  1930. });
  1931. });
  1932. } else {
  1933. callback();
  1934. }
  1935. }
  1936. /**
  1937. * Returns the length of slides.
  1938. *
  1939. * @param excludeClones - Optional. Determines whether to exclude clones or not.
  1940. *
  1941. * @return The length of slides.
  1942. */
  1943. function getLength(excludeClones) {
  1944. return excludeClones ? slides.length : Slides.length;
  1945. }
  1946. /**
  1947. * Checks if the number of slides is over than the `perPage` option, including clones.
  1948. *
  1949. * @return `true` if there are enough slides, or otherwise `false`.
  1950. */
  1951. function isEnough() {
  1952. return Slides.length > options.perPage;
  1953. }
  1954. return {
  1955. mount: mount,
  1956. destroy: destroy,
  1957. register: register,
  1958. get: get,
  1959. getIn: getIn,
  1960. getAt: getAt,
  1961. add: add,
  1962. remove: remove$1,
  1963. forEach: forEach$1,
  1964. filter: filter,
  1965. rule: rule,
  1966. getLength: getLength,
  1967. isEnough: isEnough
  1968. };
  1969. }
  1970. /**
  1971. * The component that generates clones for the loop slider.
  1972. *
  1973. * @since 3.0.0
  1974. *
  1975. * @param Splide - A Splide instance.
  1976. * @param Components - A collection of components.
  1977. * @param options - Options.
  1978. *
  1979. * @return A Clones component object.
  1980. */
  1981. function Clones(Splide, Components, options) {
  1982. var _EventInterface4 = EventInterface(Splide),
  1983. on = _EventInterface4.on,
  1984. emit = _EventInterface4.emit;
  1985. var Elements = Components.Elements,
  1986. Slides = Components.Slides;
  1987. var resolve = Components.Direction.resolve;
  1988. var clones = [];
  1989. /**
  1990. * Keeps the current number of clones.
  1991. */
  1992. var cloneCount;
  1993. /**
  1994. * The index used for generating IDs.
  1995. */
  1996. var cloneIndex;
  1997. /**
  1998. * Called when the component is mounted.
  1999. */
  2000. function mount() {
  2001. init();
  2002. on(EVENT_REFRESH, refresh);
  2003. on([EVENT_UPDATED, EVENT_RESIZE], observe);
  2004. }
  2005. /**
  2006. * Removes all clones if available, and generates new clones.
  2007. */
  2008. function init() {
  2009. if (cloneCount = computeCloneCount()) {
  2010. generate(cloneCount);
  2011. }
  2012. }
  2013. /**
  2014. * Destroys clones.
  2015. */
  2016. function destroy() {
  2017. remove(clones);
  2018. empty(clones);
  2019. }
  2020. /**
  2021. * Discards all clones and regenerates them.
  2022. * Must do this before the Elements component collects slide elements.
  2023. */
  2024. function refresh() {
  2025. destroy();
  2026. init();
  2027. }
  2028. /**
  2029. * Observes the required clone count and refreshes the slider if necessary.
  2030. */
  2031. function observe() {
  2032. if (cloneCount !== computeCloneCount()) {
  2033. emit(EVENT_REFRESH);
  2034. }
  2035. }
  2036. /**
  2037. * Generates the specified number of clones.
  2038. *
  2039. * @param count - The number of clones to generate for each side.
  2040. */
  2041. function generate(count) {
  2042. var slides = Slides.get().slice();
  2043. var length = slides.length;
  2044. if (length) {
  2045. cloneIndex = 0;
  2046. while (slides.length < count) {
  2047. push(slides, slides);
  2048. }
  2049. slides.slice(-count).concat(slides.slice(0, count)).forEach(function (Slide, index) {
  2050. var isHead = index < count;
  2051. var clone = cloneDeep(Slide.slide);
  2052. isHead ? before(clone, slides[0].slide) : append(Elements.list, clone);
  2053. push(clones, clone);
  2054. Slides.register(clone, index - count + (isHead ? 0 : length), Slide.index);
  2055. });
  2056. }
  2057. }
  2058. /**
  2059. * Deeply clones the provided element with removing the ID attribute.
  2060. *
  2061. * @param elm - An element to clone.
  2062. *
  2063. * @return A cloned element.
  2064. */
  2065. function cloneDeep(elm) {
  2066. var clone = elm.cloneNode(true);
  2067. addClass(clone, options.classes.clone);
  2068. clone.id = Splide.root.id + "-clone" + pad(++cloneIndex);
  2069. return clone;
  2070. }
  2071. /**
  2072. * Returns the number of elements to generate.
  2073. * This always returns 0 if the slider type is not `'loop'`.
  2074. *
  2075. * @return The number of clones.
  2076. */
  2077. function computeCloneCount() {
  2078. var clones = options.clones;
  2079. if (!Splide.is(LOOP)) {
  2080. clones = 0;
  2081. } else if (!clones) {
  2082. var fixedSize = options[resolve('fixedWidth')];
  2083. var fixedCount = fixedSize && ceil(rect(Elements.track)[resolve('width')] / fixedSize);
  2084. var baseCount = fixedCount || options[resolve('autoWidth')] && Splide.length || options.perPage;
  2085. clones = baseCount * (options.drag ? (options.flickMaxPages || 1) + 1 : 2);
  2086. }
  2087. return clones;
  2088. }
  2089. return {
  2090. mount: mount,
  2091. destroy: destroy
  2092. };
  2093. }
  2094. /**
  2095. * The component that layouts slider components and provides methods for dimensions.
  2096. *
  2097. * @since 3.0.0
  2098. *
  2099. * @param Splide - A Splide instance.
  2100. * @param Components - A collection of components.
  2101. * @param options - Options.
  2102. *
  2103. * @return An Layout component object.
  2104. */
  2105. function Layout(Splide, Components, options) {
  2106. var _EventInterface5 = EventInterface(Splide),
  2107. on = _EventInterface5.on,
  2108. bind = _EventInterface5.bind,
  2109. emit = _EventInterface5.emit;
  2110. var Slides = Components.Slides;
  2111. var ruleBy = Components.Style.ruleBy;
  2112. var resolve = Components.Direction.resolve;
  2113. var _Components$Elements2 = Components.Elements,
  2114. root = _Components$Elements2.root,
  2115. track = _Components$Elements2.track,
  2116. list = _Components$Elements2.list;
  2117. var getAt = Slides.getAt;
  2118. var vertical = options.direction === TTB;
  2119. /**
  2120. * Called when the component is mounted.
  2121. */
  2122. function mount() {
  2123. init();
  2124. bind(window, 'resize load', Throttle(emit.bind(this, EVENT_RESIZE)));
  2125. on([EVENT_UPDATED, EVENT_REFRESH], init);
  2126. on(EVENT_RESIZE, resize);
  2127. }
  2128. /**
  2129. * Initializes the component on `mount` or `updated`.
  2130. * Uses `max-width` for the root to prevent the slider from exceeding the parent element.
  2131. */
  2132. function init() {
  2133. ruleBy(root, 'maxWidth', unit(options.width));
  2134. ruleBy(track, resolve('paddingLeft'), cssPadding(false));
  2135. ruleBy(track, resolve('paddingRight'), cssPadding(true));
  2136. Slides.rule(resolve('marginRight'), unit(options.gap));
  2137. Slides.rule('width', cssSlideWidth());
  2138. setSlidesHeight();
  2139. resize();
  2140. }
  2141. /**
  2142. * Updates dimensions of some elements when the slider is resized.
  2143. */
  2144. function resize() {
  2145. ruleBy(track, 'height', cssTrackHeight());
  2146. options.heightRatio && setSlidesHeight();
  2147. emit(EVENT_RESIZED);
  2148. }
  2149. /**
  2150. * Updates the height of slides or their container elements if available.
  2151. */
  2152. function setSlidesHeight() {
  2153. Slides.rule('height', cssSlideHeight(), true);
  2154. }
  2155. /**
  2156. * Parses the padding option and returns the value for each side.
  2157. * This method returns `paddingTop` or `paddingBottom` for the vertical slider.
  2158. *
  2159. * @param right - Determines whether to get `paddingRight/Bottom` or `paddingLeft/Top`.
  2160. *
  2161. * @return The padding value as a CSS string.
  2162. */
  2163. function cssPadding(right) {
  2164. var padding = options.padding;
  2165. var prop = resolve(right ? 'right' : 'left', true);
  2166. return padding ? unit(padding[prop] || (isObject(padding) ? '0' : padding)) : '';
  2167. }
  2168. /**
  2169. * Returns the height of the track element as a CSS string.
  2170. *
  2171. * @return The height of the track.
  2172. */
  2173. function cssTrackHeight() {
  2174. var height = '';
  2175. if (vertical) {
  2176. height = cssHeight();
  2177. assert(height, '"height" or "heightRatio" is missing.');
  2178. var paddingTop = cssPadding(false);
  2179. var paddingBottom = cssPadding(true);
  2180. if (paddingTop || paddingBottom) {
  2181. height = "calc(" + height;
  2182. height += "" + (paddingTop ? " - " + paddingTop : '') + (paddingBottom ? " - " + paddingBottom : '') + ")";
  2183. }
  2184. }
  2185. return height;
  2186. }
  2187. /**
  2188. * Converts options related with height to a CSS string.
  2189. *
  2190. * @return The height as a CSS string if available, or otherwise an empty string.
  2191. */
  2192. function cssHeight() {
  2193. return unit(options.height || rect(list).width * options.heightRatio);
  2194. }
  2195. /**
  2196. * Returns the width of the slide as a CSS string.
  2197. *
  2198. * @return The width of the slide.
  2199. */
  2200. function cssSlideWidth() {
  2201. return options.autoWidth ? '' : unit(options.fixedWidth) || (vertical ? '' : cssSlideSize());
  2202. }
  2203. /**
  2204. * Returns the height of the slide as a CSS string.
  2205. *
  2206. * @return The height of the slide.
  2207. */
  2208. function cssSlideHeight() {
  2209. return unit(options.fixedHeight) || (vertical ? options.autoHeight ? '' : cssSlideSize() : cssHeight());
  2210. }
  2211. /**
  2212. * Returns the CSS string for slide width or height without gap.
  2213. *
  2214. * @return The CSS string for slide width or height.
  2215. */
  2216. function cssSlideSize() {
  2217. var gap = unit(options.gap);
  2218. return "calc((100%" + (gap && " + " + gap) + ")/" + (options.perPage || 1) + (gap && " - " + gap) + ")";
  2219. }
  2220. /**
  2221. * Returns the list width for the horizontal slider, or the height for the vertical slider.
  2222. *
  2223. * @return The size of the track element in pixel.
  2224. */
  2225. function listSize() {
  2226. return rect(list)[resolve('width')];
  2227. }
  2228. /**
  2229. * Returns the slide width for the horizontal slider, or the height for the vertical slider.
  2230. *
  2231. * @param index - Optional. A slide index.
  2232. * @param withoutGap - Optional. Determines whether to exclude the gap amount or not.
  2233. *
  2234. * @return The size of the specified slide element in pixel.
  2235. */
  2236. function slideSize(index, withoutGap) {
  2237. var Slide = getAt(index || 0);
  2238. return Slide ? rect(Slide.slide)[resolve('width')] + (withoutGap ? 0 : getGap()) : 0;
  2239. }
  2240. /**
  2241. * Returns the total width or height of slides from 0 to the specified index.
  2242. *
  2243. * @param index - A slide index. If omitted, uses the last index.
  2244. * @param withoutGap - Optional. Determines whether to exclude the last gap or not.
  2245. *
  2246. * @return The total width of slides in the horizontal slider, or the height in the vertical one.
  2247. */
  2248. function totalSize(index, withoutGap) {
  2249. var Slide = getAt(index);
  2250. if (Slide) {
  2251. var right = rect(Slide.slide)[resolve('right')];
  2252. var left = rect(list)[resolve('left')];
  2253. return abs(right - left) + (withoutGap ? 0 : getGap());
  2254. }
  2255. return 0;
  2256. }
  2257. /**
  2258. * Returns the slider size without clones.
  2259. *
  2260. * @return The slider size.
  2261. */
  2262. function sliderSize() {
  2263. var firstSlide = getAt(0);
  2264. var lastSlide = getAt(Slides.getLength(true) - 1);
  2265. if (firstSlide && lastSlide) {
  2266. return rect(lastSlide.slide)[resolve('right')] - rect(firstSlide.slide)[resolve('left')];
  2267. }
  2268. return 0;
  2269. }
  2270. /**
  2271. * Returns the gap value.
  2272. *
  2273. *
  2274. * @return The gap value in pixel.
  2275. */
  2276. function getGap() {
  2277. var Slide = getAt(0);
  2278. return Slide ? parseFloat(style(Slide.slide, resolve('marginRight'))) || 0 : 0;
  2279. }
  2280. /**
  2281. * Returns the padding value.
  2282. *
  2283. * @param right - Determines whether to get `paddingRight/Bottom` or `paddingLeft/Top`.
  2284. *
  2285. * @return The padding value in pixel.
  2286. */
  2287. function getPadding(right) {
  2288. return parseFloat(style(track, resolve("padding" + (right ? 'Right' : 'Left'), true))) || 0;
  2289. }
  2290. return {
  2291. mount: mount,
  2292. listSize: listSize,
  2293. slideSize: slideSize,
  2294. sliderSize: sliderSize,
  2295. totalSize: totalSize,
  2296. getPadding: getPadding
  2297. };
  2298. }
  2299. /**
  2300. * The component for moving the slider.
  2301. *
  2302. * @since 3.0.0
  2303. *
  2304. * @param Splide - A Splide instance.
  2305. * @param Components - A collection of components.
  2306. * @param options - Options.
  2307. *
  2308. * @return A Move component object.
  2309. */
  2310. function Move(Splide, Components, options) {
  2311. var _EventInterface6 = EventInterface(Splide),
  2312. on = _EventInterface6.on,
  2313. emit = _EventInterface6.emit;
  2314. var _Components$Layout = Components.Layout,
  2315. slideSize = _Components$Layout.slideSize,
  2316. getPadding = _Components$Layout.getPadding,
  2317. totalSize = _Components$Layout.totalSize,
  2318. listSize = _Components$Layout.listSize,
  2319. sliderSize = _Components$Layout.sliderSize;
  2320. var _Components$Direction = Components.Direction,
  2321. resolve = _Components$Direction.resolve,
  2322. orient = _Components$Direction.orient;
  2323. var _Components$Elements3 = Components.Elements,
  2324. list = _Components$Elements3.list,
  2325. track = _Components$Elements3.track;
  2326. /**
  2327. * Indicates whether the slider is just looping or not.
  2328. */
  2329. var looping;
  2330. /**
  2331. * Indicates whether the component can move the slider or not.
  2332. */
  2333. var waiting;
  2334. /**
  2335. * Keeps the current position.
  2336. */
  2337. var currPosition = 0;
  2338. /**
  2339. * Keeps the rate of position to the slider width.
  2340. */
  2341. var positionRate = 0;
  2342. /**
  2343. * Called when the component is mounted.
  2344. */
  2345. function mount() {
  2346. on([EVENT_RESIZE, EVENT_UPDATED, EVENT_REFRESH], reposition);
  2347. }
  2348. /**
  2349. * Repositions the slider.
  2350. */
  2351. function reposition() {
  2352. if (options.drag !== 'free') {
  2353. jump(Splide.index);
  2354. } else {
  2355. if (!options[resolve('fixedWidth')] && !options[resolve('autoWidth')]) {
  2356. translate(listSize() * positionRate);
  2357. }
  2358. if (isExceededMax(currPosition)) {
  2359. translate(getLimit(true));
  2360. }
  2361. }
  2362. }
  2363. /**
  2364. * Goes to the slide at the specified index with the Transition component.
  2365. *
  2366. * @param dest - A destination index to go to.
  2367. * @param index - A slide index.
  2368. * @param prev - A previous index.
  2369. */
  2370. function move(dest, index, prev) {
  2371. if (!isBusy()) {
  2372. var position = getPosition();
  2373. looping = dest !== index;
  2374. waiting = options.waitForTransition;
  2375. Splide.state.set(MOVING);
  2376. emit(EVENT_MOVE, index, prev, dest);
  2377. Components.Transition.start(dest, function () {
  2378. onMoved(dest, index, prev, position);
  2379. });
  2380. }
  2381. }
  2382. /**
  2383. * Called after the transition ends.
  2384. *
  2385. * @param dest - A destination index to go to.
  2386. * @param index - A slide index.
  2387. * @param prev - A previous index.
  2388. * @param oldPosition - An old position.
  2389. */
  2390. function onMoved(dest, index, prev, oldPosition) {
  2391. if (looping) {
  2392. jump(index);
  2393. looping = false;
  2394. }
  2395. waiting = false;
  2396. Splide.state.set(IDLE);
  2397. emit(EVENT_MOVED, index, prev, dest);
  2398. if (options.trimSpace === 'move' && dest !== prev && oldPosition === getPosition()) {
  2399. Components.Controller.go(dest > prev ? '>' : '<');
  2400. }
  2401. }
  2402. /**
  2403. * Jumps to the slide at the specified index.
  2404. *
  2405. * @param index - An index to jump to.
  2406. */
  2407. function jump(index) {
  2408. translate(toPosition(index, true));
  2409. }
  2410. /**
  2411. * Moves the slider to the specified position.
  2412. *
  2413. * @param position - The destination.
  2414. */
  2415. function translate(position) {
  2416. currPosition = loop(position);
  2417. positionRate = currPosition / listSize();
  2418. Components.Style.ruleBy(list, 'transform', "translate" + resolve('X') + "(" + currPosition + "px)");
  2419. }
  2420. /**
  2421. * Loops the provided position if it exceeds limits.
  2422. *
  2423. * @param position - A position to loop.
  2424. */
  2425. function loop(position) {
  2426. if (!looping && Splide.is(LOOP)) {
  2427. var diff = position - currPosition;
  2428. var exceededMin = isExceededMin(position);
  2429. var exceededMax = isExceededMax(position);
  2430. if (exceededMin && diff > 0 || exceededMax && diff < 0) {
  2431. position += orient(sliderSize() * (exceededMin ? 1 : -1));
  2432. }
  2433. }
  2434. return position;
  2435. }
  2436. /**
  2437. * Cancels transition.
  2438. */
  2439. function cancel() {
  2440. translate(getPosition());
  2441. Components.Transition.cancel();
  2442. }
  2443. /**
  2444. * Returns the closest index to the position.
  2445. *
  2446. * @param position - A position to convert.
  2447. *
  2448. * @return The closest index to the position.
  2449. */
  2450. function toIndex(position) {
  2451. var Slides = Components.Slides.get();
  2452. var index = 0;
  2453. var minDistance = Infinity;
  2454. for (var i = 0; i < Slides.length; i++) {
  2455. var slideIndex = Slides[i].index;
  2456. var distance = abs(toPosition(slideIndex) - position);
  2457. if (distance < minDistance) {
  2458. minDistance = distance;
  2459. index = slideIndex;
  2460. } else {
  2461. break;
  2462. }
  2463. }
  2464. return index;
  2465. }
  2466. /**
  2467. * Converts the slide index to the position.
  2468. *
  2469. * @param index - An index to convert.
  2470. * @param trimming - Optional. Whether to trim edge spaces or not.
  2471. *
  2472. * @return The position corresponding with the index.
  2473. */
  2474. function toPosition(index, trimming) {
  2475. var position = orient(totalSize(index - 1) - offset(index));
  2476. return trimming ? trim(position) : position;
  2477. }
  2478. /**
  2479. * Returns the current position.
  2480. *
  2481. * @return The position of the list element.
  2482. */
  2483. function getPosition() {
  2484. var left = resolve('left');
  2485. return rect(list)[left] - rect(track)[left] + orient(getPadding(false));
  2486. }
  2487. /**
  2488. * Trims spaces on the edge of the slider.
  2489. *
  2490. * @param position - A position to trim.
  2491. *
  2492. * @return A trimmed position.
  2493. */
  2494. function trim(position) {
  2495. if (options.trimSpace && Splide.is(SLIDE)) {
  2496. position = clamp(position, 0, orient(sliderSize() - listSize()));
  2497. }
  2498. return position;
  2499. }
  2500. /**
  2501. * Returns the offset amount.
  2502. *
  2503. * @param index - An index.
  2504. */
  2505. function offset(index) {
  2506. var focus = options.focus;
  2507. if (focus === 'center') {
  2508. return (listSize() - slideSize(index, true)) / 2;
  2509. }
  2510. return (+focus || 0) * slideSize(index);
  2511. }
  2512. /**
  2513. * Returns the limit number that the slider can move to.
  2514. *
  2515. * @param max - Determines whether to return the maximum or minimum limit.
  2516. *
  2517. * @return The border number.
  2518. */
  2519. function getLimit(max) {
  2520. var trimming = !!options.trimSpace;
  2521. return max ? toPosition(Components.Controller.getEnd(), trimming) : toPosition(0, trimming);
  2522. }
  2523. /**
  2524. * Checks if the slider can move now or not.
  2525. *
  2526. * @return `true` if the slider can move, or otherwise `false`.
  2527. */
  2528. function isBusy() {
  2529. return !!(looping || waiting);
  2530. }
  2531. /**
  2532. * Checks if the provided position exceeds the minimum limit or not.
  2533. *
  2534. * @param position - A position to test.
  2535. * @param offset - Optional. Offsets the limit in pixel.
  2536. *
  2537. * @return `true` if the position exceeds the limit, or otherwise `false`.
  2538. */
  2539. function isExceededMin(position, offset) {
  2540. return orient(position) + (offset || 0) < orient(getLimit(false));
  2541. }
  2542. /**
  2543. * Checks if the provided position exceeds the maximum limit or not.
  2544. *
  2545. * @param position - A position to test.
  2546. * @param offset - Optional. Offsets the limit in pixel.
  2547. *
  2548. * @return `true` if the position exceeds the limit, or otherwise `false`.
  2549. */
  2550. function isExceededMax(position, offset) {
  2551. return orient(position) + (offset || 0) > orient(getLimit(true));
  2552. }
  2553. /**
  2554. * Checks if the slider position exceeds borders or not.
  2555. *
  2556. * @return `true` if the position is over borders, or otherwise `false`.
  2557. */
  2558. function isExceeded() {
  2559. return isExceededMin(currPosition) || isExceededMax(currPosition);
  2560. }
  2561. return {
  2562. mount: mount,
  2563. move: move,
  2564. jump: jump,
  2565. translate: translate,
  2566. cancel: cancel,
  2567. toIndex: toIndex,
  2568. toPosition: toPosition,
  2569. getPosition: getPosition,
  2570. getLimit: getLimit,
  2571. isBusy: isBusy,
  2572. isExceededMin: isExceededMin,
  2573. isExceededMax: isExceededMax,
  2574. isExceeded: isExceeded
  2575. };
  2576. }
  2577. /**
  2578. * The component for controlling the slider.
  2579. *
  2580. * @since 3.0.0
  2581. *
  2582. * @param Splide - A Splide instance.
  2583. * @param Components - A collection of components.
  2584. * @param options - Options.
  2585. *
  2586. * @return A Controller component object.
  2587. */
  2588. function Controller(Splide, Components, options) {
  2589. var _EventInterface7 = EventInterface(Splide),
  2590. on = _EventInterface7.on;
  2591. var Move = Components.Move;
  2592. var _Components$Slides = Components.Slides,
  2593. isEnough = _Components$Slides.isEnough,
  2594. getLength = _Components$Slides.getLength;
  2595. var isLoop = Splide.is(LOOP);
  2596. /**
  2597. * The current index.
  2598. */
  2599. var currIndex = options.start || 0;
  2600. /**
  2601. * The previous index.
  2602. */
  2603. var prevIndex = currIndex;
  2604. /**
  2605. * The latest number of slides.
  2606. */
  2607. var slideCount = getLength(true);
  2608. /**
  2609. * The latest `perMove` value.
  2610. */
  2611. var perMove = options.perMove;
  2612. /**
  2613. * The latest `perMove` value.
  2614. */
  2615. var perPage = options.perPage;
  2616. /**
  2617. * Called when the component is mounted.
  2618. */
  2619. function mount() {
  2620. Move.jump(currIndex);
  2621. on([EVENT_UPDATED, EVENT_REFRESH], function () {
  2622. slideCount = getLength(true);
  2623. perMove = options.perMove;
  2624. perPage = options.perPage;
  2625. });
  2626. on(EVENT_SCROLLED, function () {
  2627. setIndex(Move.toIndex(Move.getPosition()));
  2628. }, 0);
  2629. }
  2630. /**
  2631. * Moves the slider by the control pattern.
  2632. *
  2633. * @see `Splide#go()`
  2634. *
  2635. * @param control - A control pattern.
  2636. * @param allowSameIndex - Optional. Determines whether to allow to go to the current index or not.
  2637. */
  2638. function go(control, allowSameIndex) {
  2639. var dest = parse(control);
  2640. var index = loop(dest);
  2641. if (!Move.isBusy() && index > -1 && (allowSameIndex || index !== currIndex)) {
  2642. setIndex(index);
  2643. Move.move(dest, index, prevIndex);
  2644. }
  2645. }
  2646. /**
  2647. * Parses the control and returns a slide index.
  2648. *
  2649. * @param control - A control pattern to parse.
  2650. */
  2651. function parse(control) {
  2652. var index = currIndex;
  2653. if (isString(control)) {
  2654. var _ref = control.match(/([+\-<>])(\d+)?/) || [],
  2655. indicator = _ref[1],
  2656. number = _ref[2];
  2657. if (indicator === '+' || indicator === '-') {
  2658. index = computeDestIndex(currIndex + +("" + indicator + (+number || 1)), currIndex, true);
  2659. } else if (indicator === '>') {
  2660. index = number ? toIndex(+number) : getNext(true);
  2661. } else if (indicator === '<') {
  2662. index = getPrev(true);
  2663. }
  2664. } else {
  2665. if (isLoop) {
  2666. index = clamp(control, -perPage, slideCount + perPage - 1);
  2667. } else {
  2668. index = clamp(control, 0, getEnd());
  2669. }
  2670. }
  2671. return index;
  2672. }
  2673. /**
  2674. * Returns a next destination index.
  2675. *
  2676. * @param destination - Optional. Determines whether to get a destination index or a slide one.
  2677. *
  2678. * @return A next index if available, or otherwise `-1`.
  2679. */
  2680. function getNext(destination) {
  2681. return getAdjacent(false, destination);
  2682. }
  2683. /**
  2684. * Returns a previous destination index.
  2685. *
  2686. * @param destination - Optional. Determines whether to get a destination index or a slide one.
  2687. *
  2688. * @return A previous index if available, or otherwise `-1`.
  2689. */
  2690. function getPrev(destination) {
  2691. return getAdjacent(true, destination);
  2692. }
  2693. /**
  2694. * Returns an adjacent destination index.
  2695. *
  2696. * @param prev - Determines whether to return a previous or next index.
  2697. * @param destination - Optional. Determines whether to get a destination index or a slide one.
  2698. *
  2699. * @return An adjacent index if available, or otherwise `-1`.
  2700. */
  2701. function getAdjacent(prev, destination) {
  2702. var dest = computeDestIndex(currIndex + getPerMove() * (prev ? -1 : 1), currIndex);
  2703. return destination ? dest : loop(dest);
  2704. }
  2705. /**
  2706. * Converts the desired destination index to the valid one.
  2707. * - This may return clone indices if the editor is the loop mode,
  2708. * or `-1` if there is no slide to go.
  2709. * - There are still slides where the slider can go if borders are between `from` and `dest`.
  2710. *
  2711. * @param dest - The desired destination.
  2712. * @param from - A base index.
  2713. * @param incremental - Optional. Whether the control is incremental or not.
  2714. *
  2715. * @return A converted destination index, including clones.
  2716. */
  2717. function computeDestIndex(dest, from, incremental) {
  2718. if (isEnough()) {
  2719. var end = getEnd(); // Will overrun:
  2720. if (dest < 0 || dest > end) {
  2721. if (between(0, dest, from, true) || between(end, from, dest, true)) {
  2722. dest = toIndex(toPage(dest));
  2723. } else {
  2724. if (isLoop) {
  2725. dest = perMove ? dest : dest < 0 ? -(slideCount % perPage || perPage) : slideCount;
  2726. } else if (options.rewind) {
  2727. dest = dest < 0 ? end : 0;
  2728. } else {
  2729. dest = -1;
  2730. }
  2731. }
  2732. } else {
  2733. if (!isLoop && !incremental && dest !== from) {
  2734. dest = toIndex(toPage(from) + (dest < from ? -1 : 1));
  2735. }
  2736. }
  2737. } else {
  2738. dest = -1;
  2739. }
  2740. return dest;
  2741. }
  2742. /**
  2743. * Returns the end index where the slider can go.
  2744. * For example, if the slider has 10 slides and the `perPage` option is 3,
  2745. * the slider can go to the slide 8 (the index is 7).
  2746. *
  2747. * @return An end index.
  2748. */
  2749. function getEnd() {
  2750. var end = slideCount - perPage;
  2751. if (hasFocus() || isLoop && perMove) {
  2752. end = slideCount - 1;
  2753. }
  2754. return max(end, 0);
  2755. }
  2756. /**
  2757. * Loops the provided index only in the loop mode.
  2758. *
  2759. * @param index - An index to loop.
  2760. *
  2761. * @return A looped index.
  2762. */
  2763. function loop(index) {
  2764. if (isLoop) {
  2765. return isEnough() ? index % slideCount + (index < 0 ? slideCount : 0) : -1;
  2766. }
  2767. return index;
  2768. }
  2769. /**
  2770. * Converts the page index to the slide index.
  2771. *
  2772. * @param page - A page index to convert.
  2773. *
  2774. * @return A slide index.
  2775. */
  2776. function toIndex(page) {
  2777. return clamp(hasFocus() ? page : perPage * page, 0, getEnd());
  2778. }
  2779. /**
  2780. * Converts the slide index to the page index.
  2781. *
  2782. * @param index - An index to convert.
  2783. */
  2784. function toPage(index) {
  2785. if (!hasFocus()) {
  2786. index = between(index, slideCount - perPage, slideCount - 1) ? slideCount - 1 : index;
  2787. index = floor(index / perPage);
  2788. }
  2789. return index;
  2790. }
  2791. /**
  2792. * Returns the number of slides to move for '>' and '<'.
  2793. *
  2794. * @return The number of slides to move.
  2795. */
  2796. function getPerMove() {
  2797. return perMove || hasFocus() ? 1 : perPage;
  2798. }
  2799. /**
  2800. * Sets a new index and retains old one.
  2801. *
  2802. * @param index - A new index to set.
  2803. */
  2804. function setIndex(index) {
  2805. if (index !== currIndex) {
  2806. prevIndex = currIndex;
  2807. currIndex = index;
  2808. }
  2809. }
  2810. /**
  2811. * Returns the current/previous index slide index.
  2812. *
  2813. * @param prev - Optional. Whether to return previous index or not.
  2814. */
  2815. function getIndex(prev) {
  2816. return prev ? prevIndex : currIndex;
  2817. }
  2818. /**
  2819. * Verifies if the focus option is available or not.
  2820. *
  2821. * @return `true` if the slider has the focus option.
  2822. */
  2823. function hasFocus() {
  2824. return !isUndefined(options.focus) || options.isNavigation;
  2825. }
  2826. return {
  2827. mount: mount,
  2828. go: go,
  2829. getNext: getNext,
  2830. getPrev: getPrev,
  2831. getEnd: getEnd,
  2832. getIndex: getIndex,
  2833. toIndex: toIndex,
  2834. toPage: toPage,
  2835. hasFocus: hasFocus
  2836. };
  2837. }
  2838. /**
  2839. * The namespace for SVG elements.
  2840. */
  2841. var XML_NAME_SPACE = 'http://www.w3.org/2000/svg';
  2842. /**
  2843. * The arrow path.
  2844. */
  2845. var 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';
  2846. /**
  2847. * SVG width and height.
  2848. */
  2849. var SIZE = 40;
  2850. /**
  2851. * The component for handling previous and next arrows.
  2852. *
  2853. * @since 3.0.0
  2854. *
  2855. * @param Splide - A Splide instance.
  2856. * @param Components - A collection of components.
  2857. * @param options - Options.
  2858. *
  2859. * @return An Arrows component object.
  2860. */
  2861. function Arrows(Splide, Components, options) {
  2862. var _EventInterface8 = EventInterface(Splide),
  2863. on = _EventInterface8.on,
  2864. bind = _EventInterface8.bind,
  2865. emit = _EventInterface8.emit;
  2866. var classes = options.classes,
  2867. i18n = options.i18n;
  2868. var Elements = Components.Elements,
  2869. Controller = Components.Controller;
  2870. var slider = Elements.slider,
  2871. track = Elements.track;
  2872. /**
  2873. * The wrapper element.
  2874. */
  2875. var wrapper = Elements.arrows;
  2876. /**
  2877. * The previous arrow element.
  2878. */
  2879. var prev = Elements.prev;
  2880. /**
  2881. * The next arrow element.
  2882. */
  2883. var next = Elements.next;
  2884. /**
  2885. * Indicates whether the component creates arrows or retrieved from the DOM.
  2886. */
  2887. var created;
  2888. /**
  2889. * An object with previous and next arrows.
  2890. */
  2891. var arrows = {};
  2892. /**
  2893. * Called when the component is mounted.
  2894. */
  2895. function mount() {
  2896. init();
  2897. on(EVENT_UPDATED, init);
  2898. }
  2899. /**
  2900. * Initializes the component.
  2901. */
  2902. function init() {
  2903. if (options.arrows) {
  2904. if (!prev || !next) {
  2905. createArrows();
  2906. }
  2907. }
  2908. if (prev && next) {
  2909. if (!arrows.prev) {
  2910. setAttribute(prev, ARIA_CONTROLS, track.id);
  2911. setAttribute(next, ARIA_CONTROLS, track.id);
  2912. arrows.prev = prev;
  2913. arrows.next = next;
  2914. listen();
  2915. emit(EVENT_ARROWS_MOUNTED, prev, next);
  2916. } else {
  2917. display(wrapper, options.arrows === false ? 'none' : '');
  2918. }
  2919. }
  2920. }
  2921. /**
  2922. * Destroys the component.
  2923. */
  2924. function destroy() {
  2925. if (created) {
  2926. remove(wrapper);
  2927. } else {
  2928. removeAttribute(prev, ALL_ATTRIBUTES);
  2929. removeAttribute(next, ALL_ATTRIBUTES);
  2930. }
  2931. }
  2932. /**
  2933. * Listens to some events.
  2934. */
  2935. function listen() {
  2936. var go = Controller.go;
  2937. on([EVENT_MOUNTED, EVENT_MOVE, EVENT_UPDATED, EVENT_REFRESH, EVENT_SCROLLED], update);
  2938. bind(next, 'click', function () {
  2939. go('>');
  2940. });
  2941. bind(prev, 'click', function () {
  2942. go('<');
  2943. });
  2944. }
  2945. /**
  2946. * Create arrows and append them to the slider.
  2947. */
  2948. function createArrows() {
  2949. var parent = options.arrows === 'slider' && slider ? slider : Splide.root;
  2950. wrapper = create('div', classes.arrows);
  2951. prev = createArrow(true);
  2952. next = createArrow(false);
  2953. created = true;
  2954. append(wrapper, [prev, next]);
  2955. before(wrapper, child(parent));
  2956. }
  2957. /**
  2958. * Creates an arrow button.
  2959. *
  2960. * @param prev - Determines whether to create a previous or next arrow.
  2961. *
  2962. * @return A created button element.
  2963. */
  2964. function createArrow(prev) {
  2965. var arrow = "<button class=\"" + classes.arrow + " " + (prev ? 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) + "\" />");
  2966. return parseHtml(arrow);
  2967. }
  2968. /**
  2969. * Updates status of arrows, such as `disabled` and `aria-label`.
  2970. */
  2971. function update() {
  2972. var index = Splide.index;
  2973. var prevIndex = Controller.getPrev();
  2974. var nextIndex = Controller.getNext();
  2975. var prevLabel = prevIndex > -1 && index < prevIndex ? i18n.last : i18n.prev;
  2976. var nextLabel = nextIndex > -1 && index > nextIndex ? i18n.first : i18n.next;
  2977. prev.disabled = prevIndex < 0;
  2978. next.disabled = nextIndex < 0;
  2979. setAttribute(prev, ARIA_LABEL, prevLabel);
  2980. setAttribute(next, ARIA_LABEL, nextLabel);
  2981. emit(EVENT_ARROWS_UPDATED, prev, next, prevIndex, nextIndex);
  2982. }
  2983. return {
  2984. arrows: arrows,
  2985. mount: mount,
  2986. destroy: destroy
  2987. };
  2988. }
  2989. /**
  2990. * The component for auto playing sliders.
  2991. *
  2992. * @since 3.0.0
  2993. *
  2994. * @param Splide - A Splide instance.
  2995. * @param Components - A collection of components.
  2996. * @param options - Options.
  2997. *
  2998. * @return An Autoplay component object.
  2999. */
  3000. function Autoplay(Splide, Components, options) {
  3001. var _EventInterface9 = EventInterface(Splide),
  3002. on = _EventInterface9.on,
  3003. bind = _EventInterface9.bind,
  3004. emit = _EventInterface9.emit;
  3005. var _Components$Elements4 = Components.Elements,
  3006. root = _Components$Elements4.root,
  3007. track = _Components$Elements4.track,
  3008. bar = _Components$Elements4.bar,
  3009. playButton = _Components$Elements4.play,
  3010. pauseButton = _Components$Elements4.pause;
  3011. var interval = RequestInterval(options.interval, Splide.go.bind(Splide, '>'), update);
  3012. var isPaused = interval.isPaused;
  3013. /**
  3014. * Indicates whether the slider is hovered or not.
  3015. */
  3016. var hovered;
  3017. /**
  3018. * Indicates whether one of slider elements has focus or not.
  3019. */
  3020. var focused;
  3021. /**
  3022. * Turns into `true` when autoplay is manually paused.
  3023. */
  3024. var paused;
  3025. /**
  3026. * Called when the component is mounted.
  3027. */
  3028. function mount() {
  3029. var autoplay = options.autoplay;
  3030. if (autoplay) {
  3031. initButton(true);
  3032. initButton(false);
  3033. listen();
  3034. if (autoplay !== 'pause') {
  3035. play();
  3036. }
  3037. }
  3038. }
  3039. /**
  3040. * Initializes a play/pause button.
  3041. *
  3042. * @param forPause - Determines whether to initialize a pause or play button.
  3043. */
  3044. function initButton(forPause) {
  3045. var button = forPause ? pauseButton : playButton;
  3046. if (button) {
  3047. if (!isHTMLButtonElement(button)) {
  3048. setAttribute(button, ROLE, 'button');
  3049. }
  3050. setAttribute(button, ARIA_CONTROLS, track.id);
  3051. setAttribute(button, ARIA_LABEL, options.i18n[forPause ? 'pause' : 'play']);
  3052. bind(button, 'click', forPause ? pause : play);
  3053. }
  3054. }
  3055. /**
  3056. * Listens to some events.
  3057. */
  3058. function listen() {
  3059. if (options.pauseOnHover) {
  3060. bind(root, 'mouseenter mouseleave', function (e) {
  3061. hovered = e.type === 'mouseenter';
  3062. autoToggle();
  3063. });
  3064. }
  3065. if (options.pauseOnFocus) {
  3066. bind(root, 'focusin focusout', function (e) {
  3067. focused = e.type === 'focusin';
  3068. autoToggle();
  3069. });
  3070. }
  3071. on([EVENT_MOVE, EVENT_SCROLL, EVENT_REFRESH], interval.rewind);
  3072. }
  3073. /**
  3074. * Starts autoplay and clears all flags.
  3075. */
  3076. function play() {
  3077. if (isPaused() && Components.Slides.isEnough()) {
  3078. interval.start(!options.resetProgress);
  3079. focused = false;
  3080. hovered = false;
  3081. emit(EVENT_AUTOPLAY_PLAY);
  3082. }
  3083. }
  3084. /**
  3085. * Pauses autoplay.
  3086. *
  3087. * @param manual - If `true`, autoplay keeps paused until `play()` is explicitly called.
  3088. */
  3089. function pause(manual) {
  3090. if (manual === void 0) {
  3091. manual = true;
  3092. }
  3093. if (!isPaused()) {
  3094. interval.pause();
  3095. emit(EVENT_AUTOPLAY_PAUSE);
  3096. }
  3097. paused = manual;
  3098. }
  3099. /**
  3100. * Toggles play/pause according to current flags.
  3101. * If autoplay is manually paused, this will do nothing.
  3102. */
  3103. function autoToggle() {
  3104. if (!paused) {
  3105. if (!hovered && !focused) {
  3106. play();
  3107. } else {
  3108. pause(false);
  3109. }
  3110. }
  3111. }
  3112. /**
  3113. * Called on every animation frame when auto playing.
  3114. *
  3115. * @param rate - The progress rate between 0 to 1.
  3116. */
  3117. function update(rate) {
  3118. emit(EVENT_AUTOPLAY_PLAYING, rate);
  3119. if (bar) {
  3120. style(bar, {
  3121. width: rate * 100 + "%"
  3122. });
  3123. }
  3124. }
  3125. return {
  3126. mount: mount,
  3127. destroy: interval.cancel,
  3128. play: play,
  3129. pause: pause,
  3130. isPaused: isPaused
  3131. };
  3132. }
  3133. /**
  3134. * The component for setting the image as the slide background.
  3135. *
  3136. * @since 3.0.0
  3137. *
  3138. * @param Splide - A Splide instance.
  3139. * @param Components - A collection of components.
  3140. * @param options - Options.
  3141. *
  3142. * @return A Cover component object.
  3143. */
  3144. function Cover(Splide, Components, options) {
  3145. var _EventInterface10 = EventInterface(Splide),
  3146. on = _EventInterface10.on;
  3147. /**
  3148. * Called when the component is mounted.
  3149. */
  3150. function mount() {
  3151. if (options.cover) {
  3152. on(EVENT_LAZYLOAD_LOADED, function (img, Slide) {
  3153. toggle(true, img, Slide);
  3154. });
  3155. on([EVENT_MOUNTED, EVENT_UPDATED, EVENT_REFRESH], apply.bind(null, true));
  3156. }
  3157. }
  3158. /**
  3159. * Destroys the component.
  3160. */
  3161. function destroy() {
  3162. apply(false);
  3163. }
  3164. /**
  3165. * Sets/removes the background image to/from all slides.
  3166. *
  3167. * @param cover - If `false`, removes the background image.
  3168. */
  3169. function apply(cover) {
  3170. Components.Slides.forEach(function (Slide) {
  3171. var img = child(Slide.container || Slide.slide, 'img');
  3172. if (img && img.src) {
  3173. toggle(cover, img, Slide);
  3174. }
  3175. });
  3176. }
  3177. /**
  3178. * Sets/removes the background image to/from the parent element.
  3179. *
  3180. * @param cover - If `false`, removes the background image.
  3181. * @param img - A target image element.
  3182. * @param Slide - A SlideComponent object where the image belongs.
  3183. */
  3184. function toggle(cover, img, Slide) {
  3185. Slide.rule('background', cover ? "center/cover no-repeat url(\"" + img.src + "\")" : '', true);
  3186. display(img, cover ? 'none' : '');
  3187. }
  3188. return {
  3189. mount: mount,
  3190. destroy: destroy
  3191. };
  3192. }
  3193. /**
  3194. * Triggers the bounce effect when the diff becomes less than this value.
  3195. *
  3196. * @since 3.0.0
  3197. */
  3198. var BOUNCE_DIFF_THRESHOLD = 10;
  3199. /**
  3200. * The duration of the bounce effect.
  3201. *
  3202. * @since 3.0.0
  3203. */
  3204. var BOUNCE_DURATION = 600;
  3205. /**
  3206. * The friction factor.
  3207. *
  3208. * @since 3.0.0
  3209. */
  3210. var FRICTION_FACTOR = 0.6;
  3211. /**
  3212. * The velocity to calculate the scroll duration.
  3213. *
  3214. * @since 3.0.0
  3215. */
  3216. var BASE_VELOCITY = 1.2;
  3217. /**
  3218. * The minimum duration of scroll.
  3219. *
  3220. * @since 3.0.0
  3221. */
  3222. var MIN_DURATION = 800;
  3223. /**
  3224. * The component for scrolling the slider.
  3225. *
  3226. * @since 3.0.0
  3227. *
  3228. * @param Splide - A Splide instance.
  3229. * @param Components - A collection of components.
  3230. * @param options - Options.
  3231. *
  3232. * @return A Scroll component object.
  3233. */
  3234. function Scroll(Splide, Components, options) {
  3235. var _EventInterface11 = EventInterface(Splide),
  3236. on = _EventInterface11.on,
  3237. emit = _EventInterface11.emit;
  3238. var Move = Components.Move;
  3239. var getPosition = Move.getPosition,
  3240. getLimit = Move.getLimit;
  3241. /**
  3242. * Retains the active RequestInterval object.
  3243. */
  3244. var interval;
  3245. /**
  3246. * Called when the component is mounted.
  3247. */
  3248. function mount() {
  3249. on(EVENT_MOVE, clear);
  3250. on([EVENT_UPDATED, EVENT_REFRESH], cancel);
  3251. }
  3252. /**
  3253. * Scrolls the slider to the provided destination.
  3254. *
  3255. * @param destination - The destination to scroll to.
  3256. * @param duration - Optional. The scroll duration. If omitted, calculates it by the distance.
  3257. * @param suppressConstraint - Optional. Whether to suppress constraint process when the slider exceeds bounds.
  3258. */
  3259. function scroll(destination, duration, suppressConstraint) {
  3260. var start = getPosition();
  3261. var friction = 1;
  3262. duration = duration || computeDuration(abs(destination - start));
  3263. clear();
  3264. interval = RequestInterval(duration, onScrolled, function (rate) {
  3265. var position = getPosition();
  3266. var target = start + (destination - start) * easing(rate);
  3267. var diff = (target - getPosition()) * friction;
  3268. Move.translate(position + diff);
  3269. if (Splide.is(SLIDE) && !suppressConstraint && Move.isExceeded()) {
  3270. friction *= FRICTION_FACTOR;
  3271. if (abs(diff) < BOUNCE_DIFF_THRESHOLD) {
  3272. bounce(Move.isExceededMin(getPosition()));
  3273. }
  3274. }
  3275. }, 1);
  3276. emit(EVENT_SCROLL);
  3277. interval.start();
  3278. }
  3279. /**
  3280. * Triggers the bounce effect when the slider reaches bounds.
  3281. *
  3282. * @param backwards - The direction the slider is going towards.
  3283. */
  3284. function bounce(backwards) {
  3285. scroll(getLimit(!backwards), BOUNCE_DURATION, true);
  3286. }
  3287. /**
  3288. * Called when scroll ends or is canceled.
  3289. */
  3290. function onScrolled() {
  3291. emit(EVENT_SCROLLED);
  3292. }
  3293. /**
  3294. * Computes the scroll duration by the distance and the base velocity.
  3295. *
  3296. * @param distance - Distance in pixel.
  3297. *
  3298. * @return The duration for scroll.
  3299. */
  3300. function computeDuration(distance) {
  3301. return max(distance / BASE_VELOCITY, MIN_DURATION);
  3302. }
  3303. /**
  3304. * Clears the active interval.
  3305. */
  3306. function clear() {
  3307. if (interval) {
  3308. interval.cancel();
  3309. }
  3310. }
  3311. /**
  3312. * Cancels the active interval and emits the `scrolled` event.
  3313. */
  3314. function cancel() {
  3315. if (interval && !interval.isPaused()) {
  3316. clear();
  3317. onScrolled();
  3318. }
  3319. }
  3320. /**
  3321. * The easing function.
  3322. *
  3323. * @param t - A value to ease.
  3324. *
  3325. * @return An eased value.
  3326. */
  3327. function easing(t) {
  3328. var easingFunc = options.easingFunc;
  3329. return easingFunc ? easingFunc(t) : 1 - Math.pow(1 - t, 4);
  3330. }
  3331. return {
  3332. mount: mount,
  3333. destroy: clear,
  3334. scroll: scroll,
  3335. cancel: cancel
  3336. };
  3337. }
  3338. /**
  3339. * The power of the friction.
  3340. *
  3341. * @since 3.0.0
  3342. */
  3343. var FRICTION = 5;
  3344. /**
  3345. * If the user stops dragging for this duration with keeping the pointer down, updates the base coord and time.
  3346. *
  3347. * @since 3.0.0
  3348. */
  3349. var SAMPLING_INTERVAL = 50;
  3350. /**
  3351. * Start events for dragging.
  3352. *
  3353. * @since 3.0.0
  3354. */
  3355. var POINTER_DOWN_EVENTS = 'touchstart mousedown';
  3356. /**
  3357. * Update events for dragging.
  3358. *
  3359. * @since 3.0.0
  3360. */
  3361. var POINTER_MOVE_EVENTS = 'touchmove mousemove';
  3362. /**
  3363. * End events for dragging.
  3364. *
  3365. * @since 3.0.0
  3366. */
  3367. var POINTER_UP_EVENTS = 'touchend touchcancel mouseup mouseleave';
  3368. /**
  3369. * The component for dragging the slider.
  3370. *
  3371. * @since 3.0.0
  3372. *
  3373. * @param Splide - A Splide instance.
  3374. * @param Components - A collection of components.
  3375. * @param options - Options.
  3376. *
  3377. * @return A Drag component object.
  3378. */
  3379. function Drag(Splide, Components, options) {
  3380. var _EventInterface12 = EventInterface(Splide),
  3381. emit = _EventInterface12.emit,
  3382. bind = _EventInterface12.bind,
  3383. unbind = _EventInterface12.unbind;
  3384. var track = Components.Elements.track;
  3385. var _Components$Direction2 = Components.Direction,
  3386. resolve = _Components$Direction2.resolve,
  3387. orient = _Components$Direction2.orient;
  3388. var listSize = Components.Layout.listSize;
  3389. var _Components$Controlle = Components.Controller,
  3390. go = _Components$Controlle.go,
  3391. getEnd = _Components$Controlle.getEnd;
  3392. var Move = Components.Move,
  3393. Scroll = Components.Scroll;
  3394. var translate = Move.translate,
  3395. toIndex = Move.toIndex,
  3396. getPosition = Move.getPosition,
  3397. isExceeded = Move.isExceeded;
  3398. var isSlide = Splide.is(SLIDE);
  3399. var isFade = Splide.is(FADE);
  3400. var isFree = options.drag === 'free';
  3401. /**
  3402. * The coord where a pointer becomes active.
  3403. */
  3404. var startCoord;
  3405. /**
  3406. * Keeps the last time when the component detects dragging.
  3407. */
  3408. var lastTime;
  3409. /**
  3410. * The base slider position where the diff of coords is applied.
  3411. */
  3412. var basePosition;
  3413. /**
  3414. * The base coord to calculate the diff of coords.
  3415. */
  3416. var baseCoord;
  3417. /**
  3418. * The base time when the base position and the base coord are saved.
  3419. */
  3420. var baseTime;
  3421. /**
  3422. * Keeps the last TouchEvent/MouseEvent object.
  3423. */
  3424. var lastEvent;
  3425. /**
  3426. * Indicates whether the user is dragging the slider or not.
  3427. */
  3428. var moving;
  3429. /**
  3430. * Indicates whether the user drags the slider by the mouse or not.
  3431. */
  3432. var isMouse;
  3433. var target;
  3434. /**
  3435. * Indicates whether the slider exceeds borders or not.
  3436. */
  3437. var exceeded;
  3438. /**
  3439. * Called when the component is mounted.
  3440. */
  3441. function mount() {
  3442. if (options.drag) {
  3443. bind(track, POINTER_DOWN_EVENTS, onPointerDown);
  3444. }
  3445. }
  3446. /**
  3447. * Called when the user clicks or touches the slider.
  3448. *
  3449. * @param e - A TouchEvent or MouseEvent object
  3450. */
  3451. function onPointerDown(e) {
  3452. isMouse = e.type === 'mousedown';
  3453. target = isMouse ? window : track;
  3454. if (!(isMouse && e.button)) {
  3455. if (!Move.isBusy()) {
  3456. bind(target, POINTER_MOVE_EVENTS, onPointerMove);
  3457. bind(target, POINTER_UP_EVENTS, onPointerUp);
  3458. Move.cancel();
  3459. Scroll.cancel();
  3460. startCoord = getCoord(e);
  3461. } else {
  3462. prevent(e);
  3463. }
  3464. }
  3465. }
  3466. /**
  3467. * Called while the user moves the pointer on the slider.
  3468. *
  3469. * @param e - A TouchEvent or MouseEvent object
  3470. */
  3471. function onPointerMove(e) {
  3472. if (e.cancelable) {
  3473. var _min = options.dragMinThreshold || 15;
  3474. if (isMouse || abs(getCoord(e) - startCoord) > _min) {
  3475. moving = true;
  3476. onDrag();
  3477. }
  3478. if (moving) {
  3479. onDragging(e);
  3480. prevent(e, true);
  3481. }
  3482. } else {
  3483. onPointerUp(e);
  3484. }
  3485. }
  3486. /**
  3487. * Called when the user releases pointing devices.
  3488. * Be aware that the TouchEvent object provided by the `touchend` does not contain `Touch` objects,
  3489. * which means the last touch position is not available.
  3490. *
  3491. * @param e - A TouchEvent or MouseEvent object
  3492. */
  3493. function onPointerUp(e) {
  3494. unbind(target, POINTER_MOVE_EVENTS + " " + POINTER_UP_EVENTS);
  3495. moving = false;
  3496. if (lastEvent) {
  3497. onDragged(e);
  3498. lastEvent = null;
  3499. }
  3500. }
  3501. /**
  3502. * Called when the user starts dragging the slider.
  3503. */
  3504. function onDrag() {
  3505. bind(track, 'click', function (e) {
  3506. unbind(track, 'click');
  3507. prevent(e, true);
  3508. }, {
  3509. capture: true
  3510. });
  3511. emit(EVENT_DRAG);
  3512. }
  3513. /**
  3514. * Called while the user is dragging the slider.
  3515. *
  3516. * @param e - A TouchEvent or MouseEvent object
  3517. */
  3518. function onDragging(e) {
  3519. var timeStamp = e.timeStamp;
  3520. var expired = !lastTime || timeStamp - lastTime > SAMPLING_INTERVAL;
  3521. if (expired || isExceeded() !== exceeded) {
  3522. basePosition = getPosition();
  3523. baseCoord = getCoord(e);
  3524. baseTime = timeStamp;
  3525. }
  3526. exceeded = isExceeded();
  3527. lastTime = timeStamp;
  3528. lastEvent = e;
  3529. if (!isFade) {
  3530. translate(basePosition + constrain(getCoord(e) - baseCoord));
  3531. }
  3532. emit(EVENT_DRAGGING);
  3533. }
  3534. /**
  3535. * Called when the user finishes dragging.
  3536. *
  3537. * @param e - A TouchEvent or MouseEvent object
  3538. */
  3539. function onDragged(e) {
  3540. var velocity = computeVelocity(e);
  3541. if (isFade) {
  3542. go(Splide.index + orient(sign(velocity)));
  3543. } else {
  3544. var destination = computeDestination(velocity);
  3545. if (isFree) {
  3546. Scroll.scroll(destination);
  3547. } else {
  3548. go(computeIndex(destination), true);
  3549. }
  3550. }
  3551. lastTime = 0;
  3552. emit(EVENT_DRAGGED);
  3553. }
  3554. /**
  3555. * Computes the drag velocity.
  3556. *
  3557. * @param e - A TouchEvent or MouseEvent object
  3558. *
  3559. * @return The drag velocity.
  3560. */
  3561. function computeVelocity(e) {
  3562. if (Splide.is(LOOP) || !isExceeded()) {
  3563. var diffCoord = getCoord(lastEvent) - baseCoord;
  3564. var diffTime = lastEvent.timeStamp - baseTime;
  3565. var isFlick = e.timeStamp - lastTime < SAMPLING_INTERVAL;
  3566. if (diffTime && isFlick) {
  3567. return diffCoord / diffTime;
  3568. }
  3569. }
  3570. return 0;
  3571. }
  3572. /**
  3573. * Computes the destination by the velocity and the `flickPower` option.
  3574. *
  3575. * @param velocity - The drag velocity.
  3576. *
  3577. * @return The destination.
  3578. */
  3579. function computeDestination(velocity) {
  3580. var flickPower = options.flickPower || 600;
  3581. return getPosition() + sign(velocity) * min(abs(velocity) * flickPower, isFree ? Infinity : listSize() * (options.flickMaxPages || 1));
  3582. }
  3583. /**
  3584. * Converts the destination to the slide index.
  3585. *
  3586. * @param destination - The target destination.
  3587. *
  3588. * @return The destination index.
  3589. */
  3590. function computeIndex(destination) {
  3591. var dest = toIndex(destination);
  3592. return isSlide ? clamp(dest, 0, getEnd()) : dest;
  3593. }
  3594. /**
  3595. * Returns the `pageX` and `pageY` coordinates provided by the event.
  3596. * Be aware that IE does not support both TouchEvent and MouseEvent constructors.
  3597. *
  3598. * @param e - A TouchEvent or MouseEvent object.
  3599. *
  3600. * @return A pageX or pageY coordinate.
  3601. */
  3602. function getCoord(e) {
  3603. return (isMouse ? e : e.touches[0])[resolve('pageX')];
  3604. }
  3605. /**
  3606. * Reduces the distance to move by the predefined friction.
  3607. * This does nothing when the slider type is not `slide`, or the position is inside borders.
  3608. *
  3609. * @param diff - Diff to constrain.
  3610. *
  3611. * @return The constrained diff.
  3612. */
  3613. function constrain(diff) {
  3614. return diff / (exceeded && isSlide ? FRICTION : 1);
  3615. }
  3616. return {
  3617. mount: mount
  3618. };
  3619. }
  3620. /**
  3621. * The collection of arrow keys of IE.
  3622. *
  3623. * @since 3.0.0
  3624. */
  3625. var IE_ARROW_KEYS = ['Left', 'Right', 'Up', 'Down'];
  3626. /**
  3627. * The component for controlling the slider by keyboards.
  3628. *
  3629. * @since 3.0.0
  3630. *
  3631. * @param Splide - A Splide instance.
  3632. * @param Components - A collection of components.
  3633. * @param options - Options.
  3634. *
  3635. * @return A Keyboard component object.
  3636. */
  3637. function Keyboard(Splide, Components, options) {
  3638. var _EventInterface13 = EventInterface(Splide),
  3639. on = _EventInterface13.on,
  3640. bind = _EventInterface13.bind,
  3641. unbind = _EventInterface13.unbind;
  3642. var root = Components.Elements.root;
  3643. var resolve = Components.Direction.resolve;
  3644. /**
  3645. * The target element of the keyboard event.
  3646. */
  3647. var target;
  3648. /**
  3649. * Called when the component is mounted.
  3650. */
  3651. function mount() {
  3652. init();
  3653. on(EVENT_UPDATED, function () {
  3654. destroy();
  3655. init();
  3656. });
  3657. }
  3658. /**
  3659. * Initializes the component.
  3660. */
  3661. function init() {
  3662. var _options$keyboard = options.keyboard,
  3663. keyboard = _options$keyboard === void 0 ? 'global' : _options$keyboard;
  3664. if (keyboard) {
  3665. if (keyboard === 'focused') {
  3666. target = root;
  3667. setAttribute(root, TAB_INDEX, 0);
  3668. } else {
  3669. target = window;
  3670. }
  3671. bind(target, 'keydown', function (e) {
  3672. var key = normalize(e.key);
  3673. if (key === resolve('ArrowLeft')) {
  3674. Splide.go('<');
  3675. } else if (key === resolve('ArrowRight')) {
  3676. Splide.go('>');
  3677. }
  3678. });
  3679. }
  3680. }
  3681. /**
  3682. * Destroys the component.
  3683. */
  3684. function destroy() {
  3685. if (target) {
  3686. unbind(target, 'keydown');
  3687. if (isHTMLElement(target)) {
  3688. removeAttribute(target, TAB_INDEX);
  3689. }
  3690. }
  3691. }
  3692. /**
  3693. * Absorbs the difference of key names among browsers.
  3694. *
  3695. * @param key - A key to normalize.
  3696. *
  3697. * @return A normalized key.
  3698. */
  3699. function normalize(key) {
  3700. return includes(IE_ARROW_KEYS, key) ? "Arrow" + key : key;
  3701. }
  3702. return {
  3703. mount: mount,
  3704. destroy: destroy
  3705. };
  3706. }
  3707. /**
  3708. * The data attribute for the src value.
  3709. *
  3710. * @since 3.0.0
  3711. */
  3712. var SRC_DATA_ATTRIBUTE = DATA_ATTRIBUTE + "-lazy";
  3713. /**
  3714. * The data attribute for the srcset value.
  3715. *
  3716. * @since 3.0.0
  3717. */
  3718. var SRCSET_DATA_ATTRIBUTE = SRC_DATA_ATTRIBUTE + "-srcset";
  3719. /**
  3720. * The selector string for images to load.
  3721. *
  3722. * @since 3.0.0
  3723. */
  3724. var IMAGE_SELECTOR = "[" + SRC_DATA_ATTRIBUTE + "], [" + SRCSET_DATA_ATTRIBUTE + "]";
  3725. /**
  3726. * The component for lazily loading images.
  3727. *
  3728. * @since 3.0.0
  3729. *
  3730. * @param Splide - A Splide instance.
  3731. * @param Components - A collection of components.
  3732. * @param options - Options.
  3733. *
  3734. * @return An LazyLoad component object.
  3735. */
  3736. function LazyLoad(Splide, Components, options) {
  3737. var _EventInterface14 = EventInterface(Splide),
  3738. on = _EventInterface14.on,
  3739. off = _EventInterface14.off,
  3740. bind = _EventInterface14.bind,
  3741. emit = _EventInterface14.emit;
  3742. var isSequential = options.lazyLoad === 'sequential';
  3743. /**
  3744. * Stores data of images.
  3745. */
  3746. var images = [];
  3747. /**
  3748. * The current index of images.
  3749. */
  3750. var index = 0;
  3751. /**
  3752. * Called when the component is mounted.
  3753. */
  3754. function mount() {
  3755. if (options.lazyLoad) {
  3756. on([EVENT_MOUNTED, EVENT_REFRESH], function () {
  3757. destroy();
  3758. init();
  3759. });
  3760. if (!isSequential) {
  3761. on([EVENT_MOUNTED, EVENT_REFRESH, EVENT_MOVED], observe);
  3762. }
  3763. }
  3764. }
  3765. /**
  3766. * Finds images that contain specific data attributes.
  3767. */
  3768. function init() {
  3769. Components.Slides.forEach(function (Slide) {
  3770. queryAll(Slide.slide, IMAGE_SELECTOR).forEach(function (img) {
  3771. var src = getAttribute(img, SRC_DATA_ATTRIBUTE);
  3772. var srcset = getAttribute(img, SRCSET_DATA_ATTRIBUTE);
  3773. if (src !== img.src || srcset !== img.srcset) {
  3774. var spinner = create('span', options.classes.spinner, img.parentElement);
  3775. setAttribute(spinner, ROLE, 'presentation');
  3776. images.push({
  3777. img: img,
  3778. Slide: Slide,
  3779. src: src,
  3780. srcset: srcset,
  3781. spinner: spinner
  3782. });
  3783. display(img, 'none');
  3784. }
  3785. });
  3786. });
  3787. if (isSequential) {
  3788. loadNext();
  3789. }
  3790. }
  3791. /**
  3792. * Destroys the component.
  3793. */
  3794. function destroy() {
  3795. index = 0;
  3796. images = [];
  3797. }
  3798. /**
  3799. * Checks how close each image is from the active slide, and determines whether to start loading or not.
  3800. * The last `+1` is for the current page.
  3801. */
  3802. function observe() {
  3803. images = images.filter(function (data) {
  3804. if (data.Slide.isWithin(Splide.index, options.perPage * ((options.preloadPages || 1) + 1))) {
  3805. return load(data);
  3806. }
  3807. return true;
  3808. });
  3809. if (!images.length) {
  3810. off(EVENT_MOVED);
  3811. }
  3812. }
  3813. /**
  3814. * Starts loading the image in the data.
  3815. *
  3816. * @param data - A LazyLoadImagesData object.
  3817. */
  3818. function load(data) {
  3819. var img = data.img;
  3820. addClass(data.Slide.slide, CLASS_LOADING);
  3821. bind(img, 'load error', function (e) {
  3822. onLoad(data, e.type === 'error');
  3823. });
  3824. ['src', 'srcset'].forEach(function (name) {
  3825. if (data[name]) {
  3826. setAttribute(img, name, data[name]);
  3827. removeAttribute(img, name === 'src' ? SRC_DATA_ATTRIBUTE : SRCSET_DATA_ATTRIBUTE);
  3828. }
  3829. });
  3830. }
  3831. /**
  3832. * Called when the image is loaded or any error occurs.
  3833. *
  3834. * @param data - A LazyLoadImagesData object.
  3835. * @param error - `true` if this method is called on error.
  3836. */
  3837. function onLoad(data, error) {
  3838. var Slide = data.Slide;
  3839. removeClass(Slide.slide, CLASS_LOADING);
  3840. if (!error) {
  3841. remove(data.spinner);
  3842. display(data.img, '');
  3843. emit(EVENT_LAZYLOAD_LOADED, data.img, Slide);
  3844. emit(EVENT_RESIZE);
  3845. }
  3846. if (isSequential) {
  3847. loadNext();
  3848. }
  3849. }
  3850. /**
  3851. * Starts loading a next image.
  3852. */
  3853. function loadNext() {
  3854. if (index < images.length) {
  3855. load(images[index++]);
  3856. }
  3857. }
  3858. return {
  3859. mount: mount,
  3860. destroy: destroy
  3861. };
  3862. }
  3863. /**
  3864. * The component for handling previous and next arrows.
  3865. *
  3866. * @since 3.0.0
  3867. *
  3868. * @param Splide - A Splide instance.
  3869. * @param Components - A collection of components.
  3870. * @param options - Options.
  3871. *
  3872. * @return A Arrows component object.
  3873. */
  3874. function Pagination(Splide, Components, options) {
  3875. var _EventInterface15 = EventInterface(Splide),
  3876. on = _EventInterface15.on,
  3877. emit = _EventInterface15.emit,
  3878. bind = _EventInterface15.bind,
  3879. unbind = _EventInterface15.unbind;
  3880. var Slides = Components.Slides;
  3881. var _Components$Controlle2 = Components.Controller,
  3882. go = _Components$Controlle2.go,
  3883. toPage = _Components$Controlle2.toPage,
  3884. hasFocus = _Components$Controlle2.hasFocus,
  3885. getIndex = _Components$Controlle2.getIndex;
  3886. /**
  3887. * Stores all pagination items.
  3888. */
  3889. var items = [];
  3890. /**
  3891. * The pagination element.
  3892. */
  3893. var list;
  3894. /**
  3895. * Called when the component is mounted.
  3896. */
  3897. function mount() {
  3898. init();
  3899. on([EVENT_UPDATED, EVENT_REFRESH], init);
  3900. on([EVENT_MOVE, EVENT_SCROLLED], update);
  3901. }
  3902. /**
  3903. * Initializes the pagination.
  3904. */
  3905. function init() {
  3906. destroy();
  3907. if (options.pagination && Slides.isEnough()) {
  3908. createPagination();
  3909. emit(EVENT_PAGINATION_MOUNTED, {
  3910. list: list,
  3911. items: items
  3912. }, getAt(Splide.index));
  3913. update();
  3914. }
  3915. }
  3916. /**
  3917. * Destroys the component.
  3918. */
  3919. function destroy() {
  3920. if (list) {
  3921. remove(list);
  3922. items.forEach(function (item) {
  3923. unbind(item.button, 'click');
  3924. });
  3925. empty(items);
  3926. list = null;
  3927. }
  3928. }
  3929. /**
  3930. * Creates the pagination element and appends it to the slider.
  3931. */
  3932. function createPagination() {
  3933. var length = Splide.length;
  3934. var classes = options.classes,
  3935. i18n = options.i18n,
  3936. perPage = options.perPage;
  3937. var _Components$Elements5 = Components.Elements,
  3938. slider = _Components$Elements5.slider,
  3939. root = _Components$Elements5.root;
  3940. var parent = options.pagination === 'slider' && slider ? slider : root;
  3941. var max = hasFocus() ? length : ceil(length / perPage);
  3942. list = create('ul', classes.pagination, parent);
  3943. var _loop = function _loop(i) {
  3944. var li = create('li', null, list);
  3945. var button = create('button', {
  3946. class: classes.page,
  3947. type: 'button'
  3948. }, li);
  3949. var controls = Slides.getIn(i).map(function (Slide) {
  3950. return Slide.slide.id;
  3951. });
  3952. var text = !hasFocus() && perPage > 1 ? i18n.pageX : i18n.slideX;
  3953. bind(button, 'click', function () {
  3954. go(">" + i);
  3955. });
  3956. setAttribute(button, ARIA_CONTROLS, controls.join(' '));
  3957. setAttribute(button, ARIA_LABEL, format(text, i + 1));
  3958. emit(EVENT_PAGINATION_PAGE, list, li, button, i);
  3959. items.push({
  3960. li: li,
  3961. button: button,
  3962. page: i
  3963. });
  3964. };
  3965. for (var i = 0; i < max; i++) {
  3966. _loop(i);
  3967. }
  3968. }
  3969. /**
  3970. * Returns the pagination item at the specified index.
  3971. *
  3972. * @param index - An index.
  3973. *
  3974. * @return A pagination item object if available, or otherwise `undefined`.
  3975. */
  3976. function getAt(index) {
  3977. return items[toPage(index)];
  3978. }
  3979. /**
  3980. * Updates the pagination status.
  3981. */
  3982. function update() {
  3983. var prev = getAt(getIndex(true));
  3984. var curr = getAt(getIndex());
  3985. if (prev) {
  3986. removeClass(prev.button, CLASS_ACTIVE);
  3987. removeAttribute(prev.button, ARIA_CURRENT);
  3988. }
  3989. if (curr) {
  3990. addClass(curr.button, CLASS_ACTIVE);
  3991. setAttribute(curr.button, ARIA_CURRENT, true);
  3992. }
  3993. emit(EVENT_PAGINATION_UPDATED, {
  3994. list: list,
  3995. items: items
  3996. }, prev, curr);
  3997. }
  3998. return {
  3999. items: items,
  4000. mount: mount,
  4001. destroy: destroy,
  4002. getAt: getAt
  4003. };
  4004. }
  4005. /**
  4006. * The keys for triggering the navigation slide.
  4007. *
  4008. * @since 3.0.0
  4009. */
  4010. var TRIGGER_KEYS = [' ', 'Enter', 'Spacebar'];
  4011. /**
  4012. * The component for syncing multiple sliders.
  4013. *
  4014. * @since 3.0.0
  4015. *
  4016. * @param Splide - A Splide instance.
  4017. * @param Components - A collection of components.
  4018. * @param options - Options.
  4019. *
  4020. * @return A Sync component object.
  4021. */
  4022. function Sync(Splide, Components, options) {
  4023. var splides = Splide.splides;
  4024. /**
  4025. * Called when the component is mounted.
  4026. */
  4027. function mount() {
  4028. if (options.isNavigation) {
  4029. navigate();
  4030. } else {
  4031. sync();
  4032. }
  4033. }
  4034. /**
  4035. * Syncs the current index among all slides.
  4036. * The `processed` array prevents recursive call of handlers.
  4037. */
  4038. function sync() {
  4039. var processed = [];
  4040. splides.concat(Splide).forEach(function (splide, index, instances) {
  4041. EventInterface(splide).on(EVENT_MOVE, function (index, prev, dest) {
  4042. instances.forEach(function (instance) {
  4043. if (instance !== splide && !includes(processed, splide)) {
  4044. processed.push(instance);
  4045. instance.go(instance.is(LOOP) ? dest : index);
  4046. }
  4047. });
  4048. empty(processed);
  4049. });
  4050. });
  4051. }
  4052. /**
  4053. * Makes slides clickable and moves the slider to the index of clicked slide.
  4054. */
  4055. function navigate() {
  4056. var _EventInterface16 = EventInterface(Splide),
  4057. on = _EventInterface16.on,
  4058. emit = _EventInterface16.emit;
  4059. on(EVENT_CLICK, function (Slide) {
  4060. Splide.go(Slide.index);
  4061. });
  4062. on(EVENT_SLIDE_KEYDOWN, function (Slide, e) {
  4063. if (includes(TRIGGER_KEYS, e.key)) {
  4064. Splide.go(Slide.index);
  4065. prevent(e);
  4066. }
  4067. });
  4068. emit(EVENT_NAVIGATION_MOUNTED, Splide.splides);
  4069. }
  4070. return {
  4071. mount: mount
  4072. };
  4073. }
  4074. /**
  4075. * The component for observing the mouse wheel and moving the slider.
  4076. *
  4077. * @since 3.0.0
  4078. *
  4079. * @param Splide - A Splide instance.
  4080. * @param Components - A collection of components.
  4081. * @param options - Options.
  4082. *
  4083. * @return A Wheel component object.
  4084. */
  4085. function Wheel(Splide, Components, options) {
  4086. var _EventInterface17 = EventInterface(Splide),
  4087. bind = _EventInterface17.bind;
  4088. /**
  4089. * Called when the component is mounted.
  4090. */
  4091. function mount() {
  4092. if (options.wheel) {
  4093. bind(Components.Elements.track, 'wheel', onWheel);
  4094. }
  4095. }
  4096. /**
  4097. * Called when the user rotates the mouse wheel.
  4098. *
  4099. * @param e - A WheelEvent object.
  4100. */
  4101. function onWheel(e) {
  4102. var deltaY = e.deltaY;
  4103. if (deltaY) {
  4104. Splide.go(deltaY < 0 ? '<' : '>');
  4105. prevent(e);
  4106. }
  4107. }
  4108. return {
  4109. mount: mount
  4110. };
  4111. }
  4112. var CoreComponents = /*#__PURE__*/Object.freeze({
  4113. __proto__: null,
  4114. Options: Options,
  4115. Direction: Direction,
  4116. Elements: Elements,
  4117. Style: Style,
  4118. Slides: Slides,
  4119. Clones: Clones,
  4120. Layout: Layout,
  4121. Move: Move,
  4122. Controller: Controller,
  4123. Arrows: Arrows,
  4124. Autoplay: Autoplay,
  4125. Cover: Cover,
  4126. Scroll: Scroll,
  4127. Drag: Drag,
  4128. Keyboard: Keyboard,
  4129. LazyLoad: LazyLoad,
  4130. Pagination: Pagination,
  4131. Sync: Sync,
  4132. Wheel: Wheel
  4133. });
  4134. /**
  4135. * The collection of i18n strings.
  4136. *
  4137. * @since 3.0.0
  4138. */
  4139. var I18N = {
  4140. prev: 'Previous slide',
  4141. next: 'Next slide',
  4142. first: 'Go to first slide',
  4143. last: 'Go to last slide',
  4144. slideX: 'Go to slide %s',
  4145. pageX: 'Go to page %s',
  4146. play: 'Start autoplay',
  4147. pause: 'Pause autoplay'
  4148. };
  4149. /**
  4150. * The collection of default options.
  4151. * Note that this collection does not contain all options.
  4152. *
  4153. * @since 3.0.0
  4154. */
  4155. var DEFAULTS = {
  4156. type: 'slide',
  4157. speed: 400,
  4158. waitForTransition: true,
  4159. perPage: 1,
  4160. arrows: true,
  4161. pagination: true,
  4162. interval: 5000,
  4163. pauseOnHover: true,
  4164. pauseOnFocus: true,
  4165. resetProgress: true,
  4166. easing: 'cubic-bezier(.42,.65,.27,.99)',
  4167. drag: true,
  4168. direction: 'ltr',
  4169. slideFocus: true,
  4170. trimSpace: true,
  4171. classes: CLASSES,
  4172. i18n: I18N
  4173. };
  4174. /**
  4175. * The component for the fade transition.
  4176. *
  4177. * @since 3.0.0
  4178. *
  4179. * @param Splide - A Splide instance.
  4180. * @param Components - A collection of components.
  4181. * @param options - Options.
  4182. *
  4183. * @return A Transition component object.
  4184. */
  4185. function Fade(Splide, Components, options) {
  4186. var _EventInterface18 = EventInterface(Splide),
  4187. on = _EventInterface18.on;
  4188. var ruleBy = Components.Style.ruleBy;
  4189. /**
  4190. * Called when the component is mounted.
  4191. * The nextTick disables the initial fade transition of the first slide.
  4192. */
  4193. function mount() {
  4194. on([EVENT_MOUNTED, EVENT_REFRESH], function () {
  4195. nextTick(function () {
  4196. Components.Slides.forEach(function (Slide) {
  4197. ruleBy(Slide.slide, 'transition', "opacity " + options.speed + "ms " + options.easing);
  4198. });
  4199. });
  4200. });
  4201. }
  4202. /**
  4203. * Starts the transition.
  4204. * Explicitly sets the track height to avoid it will collapse in Safari.
  4205. *
  4206. * @param index - A destination index.
  4207. * @param done - The callback function that must be called after the transition ends.
  4208. */
  4209. function start(index, done) {
  4210. var track = Components.Elements.track;
  4211. ruleBy(track, 'height', unit(rect(track).height));
  4212. nextTick(function () {
  4213. done();
  4214. ruleBy(track, 'height', '');
  4215. });
  4216. }
  4217. return {
  4218. mount: mount,
  4219. start: start,
  4220. cancel: noop
  4221. };
  4222. }
  4223. /**
  4224. * The component for the slide transition.
  4225. *
  4226. * @since 3.0.0
  4227. *
  4228. * @param Splide - A Splide instance.
  4229. * @param Components - A collection of components.
  4230. * @param options - Options.
  4231. *
  4232. * @return A Transition component object.
  4233. */
  4234. function Slide(Splide, Components, options) {
  4235. var _EventInterface19 = EventInterface(Splide),
  4236. bind = _EventInterface19.bind;
  4237. var Move = Components.Move,
  4238. Controller = Components.Controller;
  4239. var list = Components.Elements.list;
  4240. /**
  4241. * Holds the `done` callback function.
  4242. */
  4243. var endCallback;
  4244. /**
  4245. * Called when the component is mounted.
  4246. */
  4247. function mount() {
  4248. bind(list, 'transitionend', function (e) {
  4249. if (e.target === list && endCallback) {
  4250. cancel();
  4251. endCallback();
  4252. }
  4253. });
  4254. }
  4255. /**
  4256. * Starts the transition.
  4257. * The Move component calls this method just before the slider moves.
  4258. *
  4259. * @param index - A destination index.
  4260. * @param done - The callback function that must be called after the transition ends.
  4261. */
  4262. function start(index, done) {
  4263. var destination = Move.toPosition(index, true);
  4264. var position = Move.getPosition();
  4265. var speed = getSpeed(index);
  4266. if (abs(destination - position) >= 1 && speed >= 1) {
  4267. apply("transform " + speed + "ms " + options.easing);
  4268. Move.translate(destination);
  4269. endCallback = done;
  4270. } else {
  4271. Move.jump(index);
  4272. done();
  4273. }
  4274. }
  4275. /**
  4276. * Cancels the transition.
  4277. */
  4278. function cancel() {
  4279. apply('');
  4280. }
  4281. /**
  4282. * Returns the transition speed.
  4283. *
  4284. * @param index - A destination index.
  4285. */
  4286. function getSpeed(index) {
  4287. var rewindSpeed = options.rewindSpeed;
  4288. if (Splide.is(SLIDE) && rewindSpeed) {
  4289. var prev = Controller.getIndex(true);
  4290. var end = Controller.getEnd();
  4291. if (prev === 0 && index >= end || prev >= end && index === 0) {
  4292. return rewindSpeed;
  4293. }
  4294. }
  4295. return options.speed;
  4296. }
  4297. /**
  4298. * Applies the transition CSS property to the list element.
  4299. *
  4300. * @param transition - A transition CSS value.
  4301. */
  4302. function apply(transition) {
  4303. Components.Style.ruleBy(list, 'transition', transition);
  4304. }
  4305. return {
  4306. mount: mount,
  4307. start: start,
  4308. cancel: cancel
  4309. };
  4310. }
  4311. /**
  4312. * The frontend class for the Splide slider.
  4313. *
  4314. * @since 3.0.0
  4315. */
  4316. var Splide = /*#__PURE__*/function () {
  4317. /**
  4318. * The Splide constructor.
  4319. *
  4320. * @param target - The selector for the target element, or the element itself.
  4321. * @param options - Optional. An object with options.
  4322. */
  4323. function Splide(target, options) {
  4324. /**
  4325. * The EventBusObject object.
  4326. */
  4327. this.event = EventBus();
  4328. /**
  4329. * The collection of all component objects.
  4330. */
  4331. this.Components = {};
  4332. /**
  4333. * The StateObject object.
  4334. */
  4335. this.state = State(CREATED);
  4336. /**
  4337. * Splide instances to sync with.
  4338. */
  4339. this.splides = [];
  4340. /**
  4341. * The collection of options.
  4342. */
  4343. this.opts = {};
  4344. /**
  4345. * The collection of extensions.
  4346. */
  4347. this.Extensions = {};
  4348. var root = isString(target) ? query(document, target) : target;
  4349. assert(root, root + " is invalid.");
  4350. this.root = root;
  4351. merge(DEFAULTS, Splide.defaults);
  4352. merge(merge(this.opts, DEFAULTS), options || {});
  4353. }
  4354. /**
  4355. * Initializes the instance.
  4356. *
  4357. * @param Extensions - Optional. An object with extensions.
  4358. * @param Transition - Optional. A Transition component.
  4359. *
  4360. * @return `this`
  4361. */
  4362. var _proto = Splide.prototype;
  4363. _proto.mount = function mount(Extensions, Transition) {
  4364. var _this3 = this;
  4365. this.state.set(CREATED);
  4366. this.Transition = Transition || this.Transition || (this.is(FADE) ? Fade : Slide);
  4367. this.Extensions = Extensions || this.Extensions;
  4368. var Components = assign({}, CoreComponents, this.Extensions, {
  4369. Transition: this.Transition
  4370. });
  4371. forOwn(Components, function (Component, key) {
  4372. var component = Component(_this3, _this3.Components, _this3.opts);
  4373. _this3.Components[key] = component;
  4374. component.mount && component.mount();
  4375. });
  4376. forOwn(this.Components, function (component) {
  4377. component.mounted && component.mounted();
  4378. });
  4379. this.emit(EVENT_MOUNTED);
  4380. addClass(this.root, CLASS_INITIALIZED);
  4381. this.state.set(IDLE);
  4382. this.emit(EVENT_READY);
  4383. return this;
  4384. }
  4385. /**
  4386. * Syncs the slider with the provided one.
  4387. * This method must be called before the `mount()`.
  4388. *
  4389. * @example
  4390. * ```ts
  4391. * var primary = new Splide();
  4392. * var secondary = new Splide();
  4393. *
  4394. * primary.sync( secondary );
  4395. * primary.mount();
  4396. * secondary.mount();
  4397. * ```
  4398. *
  4399. * @param splide - A Splide instance to sync with.
  4400. *
  4401. * @return `this`
  4402. */
  4403. ;
  4404. _proto.sync = function sync(splide) {
  4405. this.splides.push(splide);
  4406. splide.splides.push(this);
  4407. return this;
  4408. }
  4409. /**
  4410. * Moves the slider with the following control pattern.
  4411. *
  4412. * | Pattern | Description |
  4413. * |---|---|
  4414. * | `i` | Goes to the slide `i` |
  4415. * | `'+${i}'` | Increments the slide index by `i` |
  4416. * | `'-${i}'` | Decrements the slide index by `i` |
  4417. * | `'>'` | Goes to the next page |
  4418. * | `'<'` | Goes to the previous page |
  4419. * | `>${i}` | Goes to the page `i` |
  4420. *
  4421. * In most cases, `'>'` and `'<'` notations are enough to control the slider
  4422. * because they respect `perPage` and `perMove` options.
  4423. *
  4424. * @example
  4425. * ```ts
  4426. * var splide = new Splide();
  4427. *
  4428. * // Goes to the slide 1:
  4429. * splide.go( 1 );
  4430. *
  4431. * // Increments the index:
  4432. * splide.go( '+2' );
  4433. *
  4434. * // Goes to the next page:
  4435. * splide.go( '>' );
  4436. *
  4437. * // Goes to the page 2:
  4438. * splide.go( '>2' );
  4439. * ```
  4440. *
  4441. * @param control
  4442. */
  4443. ;
  4444. _proto.go = function go(control) {
  4445. this.Components.Controller.go(control);
  4446. }
  4447. /**
  4448. * Registers an event handler.
  4449. *
  4450. * @example
  4451. * ```ts
  4452. * var splide = new Splide();
  4453. *
  4454. * // Listens to a single event:
  4455. * splide.on( 'move', function() {} );
  4456. *
  4457. * // Listens to multiple events:
  4458. * splide.on( 'move resize', function() {} );
  4459. *
  4460. * // Appends a namespace:
  4461. * splide.on( 'move.myNamespace resize.myNamespace', function() {} );
  4462. * ```
  4463. *
  4464. * @param events - An event name or names separated by spaces. Use a dot(.) to append a namespace.
  4465. * @param callback - A callback function.
  4466. *
  4467. * @return `this`
  4468. */
  4469. ;
  4470. _proto.on = function on(events, callback) {
  4471. this.event.on(events, callback);
  4472. return this;
  4473. }
  4474. /**
  4475. * Removes the registered all handlers for the specified event or events.
  4476. * If you want to only remove a particular handler, use namespace to identify it.
  4477. *
  4478. * @example
  4479. * ```ts
  4480. * var splide = new Splide();
  4481. *
  4482. * // Removes all handlers assigned to "move":
  4483. * splide.off( 'move' );
  4484. *
  4485. * // Only removes handlers that belong to the specified namespace:
  4486. * splide.off( 'move.myNamespace' );
  4487. * ```
  4488. *
  4489. * @param events - An event name or names separated by spaces. Use a dot(.) to append a namespace.
  4490. *
  4491. * @return `this`
  4492. */
  4493. ;
  4494. _proto.off = function off(events) {
  4495. this.event.off(events);
  4496. return this;
  4497. }
  4498. /**
  4499. * Emits an event and triggers registered handlers.
  4500. *
  4501. * @param event - An event name to emit.
  4502. * @param args - Optional. Any number of arguments to pass to handlers.
  4503. *
  4504. * @return `this`
  4505. */
  4506. ;
  4507. _proto.emit = function emit(event) {
  4508. var _this$event;
  4509. for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
  4510. args[_key3 - 1] = arguments[_key3];
  4511. }
  4512. (_this$event = this.event).emit.apply(_this$event, [event].concat(args));
  4513. return this;
  4514. }
  4515. /**
  4516. * Inserts a slide at the specified position.
  4517. *
  4518. * @example
  4519. * ```ts
  4520. * var splide = new Splide();
  4521. * splide.mount();
  4522. *
  4523. * // Adds the slide by the HTML:
  4524. * splide.add( '<li></li> );
  4525. *
  4526. * // or adds the element:
  4527. * splide.add( document.createElement( 'li' ) );
  4528. * ```
  4529. *
  4530. * @param slides - A slide element, an HTML string that represents a slide, or an array with them.
  4531. * @param index - Optional. An index to insert a slide at.
  4532. *
  4533. * @return `this`
  4534. */
  4535. ;
  4536. _proto.add = function add(slides, index) {
  4537. this.Components.Slides.add(slides, index);
  4538. return this;
  4539. }
  4540. /**
  4541. * Removes slides that match the matcher
  4542. * that can be an index, an array with indices, a selector, or an iteratee function.
  4543. *
  4544. * @param matcher - An index, an array with indices, a selector string, or an iteratee function.
  4545. */
  4546. ;
  4547. _proto.remove = function remove(matcher) {
  4548. this.Components.Slides.remove(matcher);
  4549. return this;
  4550. }
  4551. /**
  4552. * Checks the slider type.
  4553. *
  4554. * @param type - A type to test.
  4555. *
  4556. * @return `true` if the type matches the current one, or otherwise `false`.
  4557. */
  4558. ;
  4559. _proto.is = function is(type) {
  4560. return this.opts.type === type;
  4561. }
  4562. /**
  4563. * Refreshes the slider.
  4564. *
  4565. * @return `this`
  4566. */
  4567. ;
  4568. _proto.refresh = function refresh() {
  4569. this.emit(EVENT_REFRESH);
  4570. return this;
  4571. }
  4572. /**
  4573. * Destroys the slider.
  4574. *
  4575. * @param completely - Optional. If `true`, Splide will not remount the slider by breakpoints.
  4576. *
  4577. * @return `this`
  4578. */
  4579. ;
  4580. _proto.destroy = function destroy(completely) {
  4581. var event = this.event,
  4582. state = this.state;
  4583. if (state.is(CREATED)) {
  4584. // Postpones destruction requested before the slider becomes ready.
  4585. event.on(EVENT_READY, this.destroy.bind(this, completely), this);
  4586. } else {
  4587. forOwn(this.Components, function (component) {
  4588. component.destroy && component.destroy(completely);
  4589. });
  4590. event.emit(EVENT_DESTROY);
  4591. event.destroy();
  4592. empty(this.splides);
  4593. state.set(DESTROYED);
  4594. }
  4595. return this;
  4596. }
  4597. /**
  4598. * Returns options.
  4599. *
  4600. * @return An object with the latest options.
  4601. */
  4602. ;
  4603. _createClass(Splide, [{
  4604. key: "options",
  4605. get: function get() {
  4606. return this.opts;
  4607. }
  4608. /**
  4609. * Merges options to the current options and emits `updated` event.
  4610. *
  4611. * @param options - An object with new options.
  4612. */
  4613. ,
  4614. set: function set(options) {
  4615. var opts = this.opts;
  4616. merge(opts, options);
  4617. if (!this.state.is(CREATED)) {
  4618. this.emit(EVENT_UPDATED, opts);
  4619. }
  4620. }
  4621. /**
  4622. * Returns the number of slides without clones.
  4623. *
  4624. * @return The number of slides.
  4625. */
  4626. }, {
  4627. key: "length",
  4628. get: function get() {
  4629. return this.Components.Slides.getLength(true);
  4630. }
  4631. /**
  4632. * Returns the active slide index.
  4633. *
  4634. * @return The active slide index.
  4635. */
  4636. }, {
  4637. key: "index",
  4638. get: function get() {
  4639. return this.Components.Controller.getIndex();
  4640. }
  4641. }]);
  4642. return Splide;
  4643. }();
  4644. /**
  4645. * Changes the default options for all Splide instances.
  4646. */
  4647. Splide.defaults = {};
  4648. /**
  4649. * The collection of state numbers.
  4650. */
  4651. Splide.STATES = STATES;
  4652. exports.EventBus = EventBus;
  4653. exports.EventInterface = EventInterface;
  4654. exports.RequestInterval = RequestInterval;
  4655. exports.Splide = Splide;
  4656. exports.State = State;
  4657. exports.Throttle = Throttle;
  4658. exports['default'] = Splide;
  4659. exports.utils = index;