splide.esm.js 121 KB

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