splide.esm.js 122 KB

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