splide.esm.js 121 KB

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