splide.esm.js 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
  2. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  3. 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); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  5. function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  6. /*!
  7. * Splide.js
  8. * Version : 2.4.24
  9. * License : MIT
  10. * Copyright: 2020 Naotoshi Fujita
  11. */
  12. /**
  13. * The function for providing an Event object simply managing events.
  14. *
  15. * @author Naotoshi Fujita
  16. * @copyright Naotoshi Fujita. All rights reserved.
  17. */
  18. /**
  19. * The function for providing an Event object simply managing events.
  20. */
  21. var Event = function Event() {
  22. /**
  23. * Store all event data.
  24. *
  25. * @type {Array}
  26. */
  27. var data = [];
  28. var Event = {
  29. /**
  30. * Subscribe the given event(s).
  31. *
  32. * @param {string} events - An event name. Use space to separate multiple events.
  33. * Also, namespace is accepted by dot, such as 'resize.{namespace}'.
  34. * @param {function} handler - A callback function.
  35. * @param {Element} elm - Optional. Native event will be listened to when this arg is provided.
  36. * @param {Object} options - Optional. Options for addEventListener.
  37. */
  38. on: function on(events, handler, elm, options) {
  39. if (elm === void 0) {
  40. elm = null;
  41. }
  42. if (options === void 0) {
  43. options = {};
  44. }
  45. events.split(' ').forEach(function (event) {
  46. if (elm) {
  47. elm.addEventListener(event, handler, options);
  48. }
  49. data.push({
  50. event: event,
  51. handler: handler,
  52. elm: elm,
  53. options: options
  54. });
  55. });
  56. },
  57. /**
  58. * Unsubscribe the given event(s).
  59. *
  60. * @param {string} events - A event name or names split by space.
  61. * @param {Element} elm - Optional. removeEventListener() will be called when this arg is provided.
  62. */
  63. off: function off(events, elm) {
  64. if (elm === void 0) {
  65. elm = null;
  66. }
  67. events.split(' ').forEach(function (event) {
  68. data = data.filter(function (item) {
  69. if (item && item.event === event && item.elm === elm) {
  70. unsubscribe(item);
  71. return false;
  72. }
  73. return true;
  74. });
  75. });
  76. },
  77. /**
  78. * Emit an event.
  79. * This method is only for custom events.
  80. *
  81. * @param {string} event - An event name.
  82. * @param {*} args - Any number of arguments passed to handlers.
  83. */
  84. emit: function emit(event) {
  85. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  86. args[_key - 1] = arguments[_key];
  87. }
  88. data.forEach(function (item) {
  89. if (!item.elm && item.event.split('.')[0] === event) {
  90. item.handler.apply(item, args);
  91. }
  92. });
  93. },
  94. /**
  95. * Clear event data.
  96. */
  97. destroy: function destroy() {
  98. data.forEach(unsubscribe);
  99. data = [];
  100. }
  101. };
  102. /**
  103. * Remove the registered event listener.
  104. *
  105. * @param {Object} item - An object containing event data.
  106. */
  107. function unsubscribe(item) {
  108. if (item.elm) {
  109. item.elm.removeEventListener(item.event, item.handler, item.options);
  110. }
  111. }
  112. return Event;
  113. };
  114. /**
  115. * The function providing a super simple state system.
  116. *
  117. * @author Naotoshi Fujita
  118. * @copyright Naotoshi Fujita. All rights reserved.
  119. */
  120. /**
  121. * The function providing a super simple state system.
  122. *
  123. * @param {string|number} initialState - Provide the initial state value.
  124. */
  125. var State = function State(initialState) {
  126. /**
  127. * Store the current state.
  128. *
  129. * @type {string|number}
  130. */
  131. var curr = initialState;
  132. return {
  133. /**
  134. * Change state.
  135. *
  136. * @param {string|number} state - A new state.
  137. */
  138. set: function set(state) {
  139. curr = state;
  140. },
  141. /**
  142. * Verify if the current state is given one or not.
  143. *
  144. * @param {string|number} state - A state name to be verified.
  145. *
  146. * @return {boolean} - True if the current state is the given one.
  147. */
  148. is: function is(state) {
  149. return state === curr;
  150. }
  151. };
  152. };
  153. /**
  154. * Some utility functions related with Object, supporting IE.
  155. *
  156. * @author Naotoshi Fujita
  157. * @copyright Naotoshi Fujita. All rights reserved.
  158. */
  159. var keys = Object.keys;
  160. /**
  161. * Iterate an object like Array.forEach.
  162. * IE doesn't support forEach of HTMLCollection.
  163. *
  164. * @param {Object} obj - An object.
  165. * @param {function} callback - A function handling each value. Arguments are value, property and index.
  166. */
  167. function each(obj, callback) {
  168. keys(obj).some(function (key, index) {
  169. return callback(obj[key], key, index);
  170. });
  171. }
  172. /**
  173. * Return values of the given object as an array.
  174. * IE doesn't support Object.values.
  175. *
  176. * @param {Object} obj - An object.
  177. *
  178. * @return {Array} - An array containing all values of the given object.
  179. */
  180. function values(obj) {
  181. return keys(obj).map(function (key) {
  182. return obj[key];
  183. });
  184. }
  185. /**
  186. * Check if the given subject is object or not.
  187. *
  188. * @param {*} subject - A subject to be verified.
  189. *
  190. * @return {boolean} - True if object, false otherwise.
  191. */
  192. function isObject(subject) {
  193. return typeof subject === 'object';
  194. }
  195. /**
  196. * Merge two objects deeply.
  197. *
  198. * @param {Object} to - An object where "from" is merged.
  199. * @param {Object} from - An object merged to "to".
  200. *
  201. * @return {Object} - A merged object.
  202. */
  203. function merge(_ref, from) {
  204. var to = _extends({}, _ref);
  205. each(from, function (value, key) {
  206. if (isObject(value)) {
  207. if (!isObject(to[key])) {
  208. to[key] = {};
  209. }
  210. to[key] = merge(to[key], value);
  211. } else {
  212. to[key] = value;
  213. }
  214. });
  215. return to;
  216. }
  217. /**
  218. * Assign all properties "from" to "to" object.
  219. *
  220. * @param {Object} to - An object where properties are assigned.
  221. * @param {Object} from - An object whose properties are assigned to "to".
  222. *
  223. * @return {Object} - An assigned object.
  224. */
  225. function assign(to, from) {
  226. keys(from).forEach(function (key) {
  227. if (!to[key]) {
  228. Object.defineProperty(to, key, Object.getOwnPropertyDescriptor(from, key));
  229. }
  230. });
  231. return to;
  232. }
  233. /**
  234. * A package of some miscellaneous utility functions.
  235. *
  236. * @author Naotoshi Fujita
  237. * @copyright Naotoshi Fujita. All rights reserved.
  238. */
  239. /**
  240. * Convert the given value to array.
  241. *
  242. * @param {*} value - Any value.
  243. *
  244. * @return {*[]} - Array containing the given value.
  245. */
  246. function toArray(value) {
  247. return Array.isArray(value) ? value : [value];
  248. }
  249. /**
  250. * Check if the given value is between min and max.
  251. * Min will be returned when the value is less than min or max will do when greater than max.
  252. *
  253. * @param {number} value - A number to be checked.
  254. * @param {number} m1 - Minimum or maximum number.
  255. * @param {number} m2 - Maximum or minimum number.
  256. *
  257. * @return {number} - A value itself, min or max.
  258. */
  259. function between(value, m1, m2) {
  260. return Math.min(Math.max(value, m1 > m2 ? m2 : m1), m1 > m2 ? m1 : m2);
  261. }
  262. /**
  263. * The sprintf method with minimum functionality.
  264. *
  265. * @param {string} format - The string format.
  266. * @param {string|Array} replacements - Replacements accepting multiple arguments.
  267. *
  268. * @returns {string} - Converted string.
  269. */
  270. function sprintf(format, replacements) {
  271. var i = 0;
  272. return format.replace(/%s/g, function () {
  273. return toArray(replacements)[i++];
  274. });
  275. }
  276. /**
  277. * Append px unit to the given subject if necessary.
  278. *
  279. * @param {number|string} value - A value that may not include an unit.
  280. *
  281. * @return {string} - If the value is string, return itself.
  282. * If number, do value + "px". An empty string, otherwise.
  283. */
  284. function unit(value) {
  285. var type = typeof value;
  286. if (type === 'number' && value > 0) {
  287. return parseFloat(value) + 'px';
  288. }
  289. return type === 'string' ? value : '';
  290. }
  291. /**
  292. * Pad start with 0.
  293. *
  294. * @param {number} number - A number to be filled with 0.
  295. *
  296. * @return {string|number} - Padded number.
  297. */
  298. function pad(number) {
  299. return number < 10 ? '0' + number : number;
  300. }
  301. /**
  302. * Convert the given value to pixel.
  303. *
  304. * @param {Element} root - Root element where a dummy div is appended.
  305. * @param {string|number} value - CSS value to be converted, such as 10rem.
  306. *
  307. * @return {number} - Pixel.
  308. */
  309. function toPixel(root, value) {
  310. if (typeof value === 'string') {
  311. var div = create('div', {});
  312. applyStyle(div, {
  313. position: 'absolute',
  314. width: value
  315. });
  316. append(root, div);
  317. value = div.clientWidth;
  318. _remove(div);
  319. }
  320. return +value || 0;
  321. }
  322. /**
  323. * Some utility functions related with DOM.
  324. *
  325. * @author Naotoshi Fujita
  326. * @copyright Naotoshi Fujita. All rights reserved.
  327. */
  328. /**
  329. * Find the first element matching the given selector.
  330. * Be aware that all selectors after a space are ignored.
  331. *
  332. * @param {Element|Node} elm - An ancestor element.
  333. * @param {string} selector - DOMString.
  334. *
  335. * @return {Element|null} - A found element or null.
  336. */
  337. function find(elm, selector) {
  338. return elm ? elm.querySelector(selector.split(' ')[0]) : null;
  339. }
  340. /**
  341. * Find a first child having the given tag or class name.
  342. *
  343. * @param {Element} parent - A parent element.
  344. * @param {string} tagOrClassName - A tag or class name.
  345. *
  346. * @return {Element|undefined} - A found element on success or undefined on failure.
  347. */
  348. function child(parent, tagOrClassName) {
  349. return children(parent, tagOrClassName)[0];
  350. }
  351. /**
  352. * Return chile elements that matches the provided tag or class name.
  353. *
  354. * @param {Element} parent - A parent element.
  355. * @param {string} tagOrClassName - A tag or class name.
  356. *
  357. * @return {Element[]} - Found elements.
  358. */
  359. function children(parent, tagOrClassName) {
  360. if (parent) {
  361. return values(parent.children).filter(function (child) {
  362. return hasClass(child, tagOrClassName.split(' ')[0]) || child.tagName === tagOrClassName;
  363. });
  364. }
  365. return [];
  366. }
  367. /**
  368. * Create an element with some optional attributes.
  369. *
  370. * @param {string} tag - A tag name.
  371. * @param {Object} attrs - An object any attribute pairs of name and value.
  372. *
  373. * @return {Element} - A created element.
  374. */
  375. function create(tag, attrs) {
  376. var elm = document.createElement(tag);
  377. each(attrs, function (value, key) {
  378. return setAttribute(elm, key, value);
  379. });
  380. return elm;
  381. }
  382. /**
  383. * Convert HTML string to DOM node.
  384. *
  385. * @param {string} html - HTML string.
  386. *
  387. * @return {Node} - A created node.
  388. */
  389. function domify(html) {
  390. var div = create('div', {});
  391. div.innerHTML = html;
  392. return div.firstChild;
  393. }
  394. /**
  395. * Remove a given element from a DOM tree.
  396. *
  397. * @param {Element|Element[]} elms - Element(s) to be removed.
  398. */
  399. function _remove(elms) {
  400. toArray(elms).forEach(function (elm) {
  401. if (elm) {
  402. var parent = elm.parentElement;
  403. parent && parent.removeChild(elm);
  404. }
  405. });
  406. }
  407. /**
  408. * Append a child to a given element.
  409. *
  410. * @param {Element} parent - A parent element.
  411. * @param {Element} child - An element to be appended.
  412. */
  413. function append(parent, child) {
  414. if (parent) {
  415. parent.appendChild(child);
  416. }
  417. }
  418. /**
  419. * Insert an element before the reference element.
  420. *
  421. * @param {Element|Node} ref - A reference element.
  422. * @param {Element} elm - An element to be inserted.
  423. */
  424. function before(elm, ref) {
  425. if (elm && ref) {
  426. var parent = ref.parentElement;
  427. parent && parent.insertBefore(elm, ref);
  428. }
  429. }
  430. /**
  431. * Apply styles to the given element.
  432. *
  433. * @param {Element} elm - An element where styles are applied.
  434. * @param {Object} styles - Object containing styles.
  435. */
  436. function applyStyle(elm, styles) {
  437. if (elm) {
  438. each(styles, function (value, prop) {
  439. if (value !== null) {
  440. elm.style[prop] = value;
  441. }
  442. });
  443. }
  444. }
  445. /**
  446. * Add or remove classes to/from the element.
  447. * This function is for internal usage.
  448. *
  449. * @param {Element} elm - An element where classes are added.
  450. * @param {string|string[]} classes - Class names being added.
  451. * @param {boolean} remove - Whether to remove or add classes.
  452. */
  453. function addOrRemoveClasses(elm, classes, remove) {
  454. if (elm) {
  455. toArray(classes).forEach(function (name) {
  456. if (name) {
  457. elm.classList[remove ? 'remove' : 'add'](name);
  458. }
  459. });
  460. }
  461. }
  462. /**
  463. * Add classes to the element.
  464. *
  465. * @param {Element} elm - An element where classes are added.
  466. * @param {string|string[]} classes - Class names being added.
  467. */
  468. function addClass(elm, classes) {
  469. addOrRemoveClasses(elm, classes, false);
  470. }
  471. /**
  472. * Remove a class from the element.
  473. *
  474. * @param {Element} elm - An element where classes are removed.
  475. * @param {string|string[]} classes - A class name being removed.
  476. */
  477. function removeClass(elm, classes) {
  478. addOrRemoveClasses(elm, classes, true);
  479. }
  480. /**
  481. * Verify if the provided element has the class or not.
  482. *
  483. * @param {Element} elm - An element.
  484. * @param {string} className - A class name.
  485. *
  486. * @return {boolean} - True if the element has the class or false if not.
  487. */
  488. function hasClass(elm, className) {
  489. return !!elm && elm.classList.contains(className);
  490. }
  491. /**
  492. * Set attribute to the given element.
  493. *
  494. * @param {Element} elm - An element where an attribute is assigned.
  495. * @param {string} name - Attribute name.
  496. * @param {string|number|boolean} value - Attribute value.
  497. */
  498. function setAttribute(elm, name, value) {
  499. if (elm) {
  500. elm.setAttribute(name, value);
  501. }
  502. }
  503. /**
  504. * Get attribute from the given element.
  505. *
  506. * @param {Element} elm - An element where an attribute is assigned.
  507. * @param {string} name - Attribute name.
  508. *
  509. * @return {string} - The value of the given attribute if available. An empty string if not.
  510. */
  511. function getAttribute(elm, name) {
  512. return elm ? elm.getAttribute(name) : '';
  513. }
  514. /**
  515. * Remove attribute from the given element.
  516. *
  517. * @param {Element|Element[]} elms - An element where an attribute is removed.
  518. * @param {string|string[]} names - Attribute name.
  519. */
  520. function removeAttribute(elms, names) {
  521. toArray(names).forEach(function (name) {
  522. toArray(elms).forEach(function (elm) {
  523. return elm && elm.removeAttribute(name);
  524. });
  525. });
  526. }
  527. /**
  528. * Return the Rect object of the provided object.
  529. *
  530. * @param {Element} elm - An element.
  531. *
  532. * @return {ClientRect|DOMRect} - A rect object.
  533. */
  534. function getRect(elm) {
  535. return elm.getBoundingClientRect();
  536. }
  537. /**
  538. * Trigger the given callback after all images contained by the element are loaded.
  539. *
  540. * @param {Element} elm - Element that may contain images.
  541. * @param {Function} callback - Callback function fired right after all images are loaded.
  542. */
  543. function loaded(elm, callback) {
  544. var images = elm.querySelectorAll('img');
  545. var length = images.length;
  546. if (length) {
  547. var count = 0;
  548. each(images, function (img) {
  549. img.onload = img.onerror = function () {
  550. if (++count === length) {
  551. callback();
  552. }
  553. };
  554. });
  555. } else {
  556. // Trigger the callback immediately if there is no image.
  557. callback();
  558. }
  559. }
  560. /**
  561. * Export slider types.
  562. *
  563. * @author Naotoshi Fujita
  564. * @copyright Naotoshi Fujita. All rights reserved.
  565. */
  566. /**
  567. * Normal slider.
  568. *
  569. * @type {string}
  570. */
  571. var SLIDE = 'slide';
  572. /**
  573. * Loop after the last slide and before the first one.
  574. *
  575. * @type {string}
  576. */
  577. var LOOP = 'loop';
  578. /**
  579. * The track doesn't move.
  580. *
  581. * @type {string}
  582. */
  583. var FADE = 'fade';
  584. /**
  585. * The component for general slide effect transition.
  586. *
  587. * @author Naotoshi Fujita
  588. * @copyright Naotoshi Fujita. All rights reserved.
  589. */
  590. /**
  591. * The component for general slide effect transition.
  592. *
  593. * @param {Splide} Splide - A Splide instance.
  594. * @param {Object} Components - An object containing components.
  595. *
  596. * @return {Object} - The component object.
  597. */
  598. var Slide$1 = function Slide$1(Splide, Components) {
  599. /**
  600. * Hold the list element.
  601. *
  602. * @type {Element}
  603. */
  604. var list;
  605. /**
  606. * Hold the onEnd callback function.
  607. *
  608. * @type {function}
  609. */
  610. var endCallback;
  611. return {
  612. /**
  613. * Called when the component is mounted.
  614. */
  615. mount: function mount() {
  616. list = Components.Elements.list;
  617. Splide.on('transitionend', function (e) {
  618. if (e.target === list && endCallback) {
  619. endCallback();
  620. }
  621. }, list);
  622. },
  623. /**
  624. * Start transition.
  625. *
  626. * @param {number} destIndex - Destination slide index that might be clone's.
  627. * @param {number} newIndex - New index.
  628. * @param {number} prevIndex - Previous index.
  629. * @param {Object} coord - Destination coordinates.
  630. * @param {function} done - Callback function must be invoked when transition is completed.
  631. */
  632. start: function start(destIndex, newIndex, prevIndex, coord, done) {
  633. var options = Splide.options;
  634. var edgeIndex = Components.Controller.edgeIndex;
  635. var speed = options.speed;
  636. endCallback = done;
  637. if (Splide.is(SLIDE)) {
  638. if (prevIndex === 0 && newIndex >= edgeIndex || prevIndex >= edgeIndex && newIndex === 0) {
  639. speed = options.rewindSpeed || speed;
  640. }
  641. }
  642. applyStyle(list, {
  643. transition: "transform " + speed + "ms " + options.easing,
  644. transform: "translate(" + coord.x + "px," + coord.y + "px)"
  645. });
  646. }
  647. };
  648. };
  649. /**
  650. * The component for fade transition.
  651. *
  652. * @author Naotoshi Fujita
  653. * @copyright Naotoshi Fujita. All rights reserved.
  654. */
  655. /**
  656. * The component for fade transition.
  657. *
  658. * @param {Splide} Splide - A Splide instance.
  659. * @param {Object} Components - An object containing components.
  660. *
  661. * @return {Object} - The component object.
  662. */
  663. var Fade = function Fade(Splide, Components) {
  664. var Fade = {
  665. /**
  666. * Called when the component is mounted.
  667. * Apply transition style to the first slide.
  668. */
  669. mount: function mount() {
  670. apply(Splide.index);
  671. },
  672. /**
  673. * Start transition.
  674. *
  675. * @param {number} destIndex - Destination slide index that might be clone's.
  676. * @param {number} newIndex - New index.
  677. * @param {number} prevIndex - Previous index.
  678. * @param {Object} coord - Destination coordinates.
  679. * @param {function} done - Callback function must be invoked when transition is completed.
  680. */
  681. start: function start(destIndex, newIndex, prevIndex, coord, done) {
  682. var track = Components.Elements.track;
  683. applyStyle(track, {
  684. height: unit(track.clientHeight)
  685. });
  686. apply(newIndex);
  687. setTimeout(function () {
  688. done();
  689. applyStyle(track, {
  690. height: ''
  691. });
  692. });
  693. }
  694. };
  695. /**
  696. * Apply transition style to the slide specified by the given index.
  697. *
  698. * @param {number} index - A slide index.
  699. */
  700. function apply(index) {
  701. var options = Splide.options;
  702. applyStyle(Components.Elements.slides[index], {
  703. transition: "opacity " + options.speed + "ms " + options.easing
  704. });
  705. }
  706. return Fade;
  707. };
  708. /**
  709. * Provide a function for composing components.
  710. *
  711. * @author Naotoshi Fujita
  712. * @copyright Naotoshi Fujita. All rights reserved.
  713. */
  714. /**
  715. * Compose components.
  716. *
  717. * @param {Splide} Splide - Splide instance.
  718. * @param {Object} Components - Additional components.
  719. * @param {function} Transition - Change component for transition.
  720. *
  721. * @return {Object} - An object containing all components.
  722. */
  723. function compose(Splide, Components, Transition) {
  724. var components = {};
  725. each(Components, function (Component, name) {
  726. components[name] = Component(Splide, components, name.toLowerCase());
  727. });
  728. if (!Transition) {
  729. Transition = Splide.is(FADE) ? Fade : Slide$1;
  730. }
  731. components.Transition = Transition(Splide, components);
  732. return components;
  733. }
  734. /**
  735. * Utility functions for outputting logs.
  736. *
  737. * @author Naotoshi Fujita
  738. * @copyright Naotoshi Fujita. All rights reserved.
  739. */
  740. /**
  741. * Prefix of an error massage.
  742. *
  743. * @type {string}
  744. */
  745. var MESSAGE_PREFIX = '[SPLIDE]';
  746. /**
  747. * Display an error message on the browser console.
  748. *
  749. * @param {string} message - An error message.
  750. */
  751. function error(message) {
  752. console.error(MESSAGE_PREFIX + " " + message);
  753. }
  754. /**
  755. * Check existence of the given object and throw an error if it doesn't.
  756. *
  757. * @throws {Error}
  758. *
  759. * @param {*} subject - A subject to be confirmed.
  760. * @param {string} message - An error message.
  761. */
  762. function exist(subject, message) {
  763. if (!subject) {
  764. throw new Error(message);
  765. }
  766. }
  767. /**
  768. * Export class names.
  769. *
  770. * @author Naotoshi Fujita
  771. * @copyright Naotoshi Fujita. All rights reserved.
  772. */
  773. /**
  774. * A root class name.
  775. *
  776. * @type {string}
  777. */
  778. var ROOT = 'splide';
  779. /**
  780. * The definition table of all classes for elements.
  781. * They might be modified by options.
  782. *
  783. * @type {Object}
  784. */
  785. var ELEMENT_CLASSES = {
  786. root: ROOT,
  787. slider: ROOT + "__slider",
  788. track: ROOT + "__track",
  789. list: ROOT + "__list",
  790. slide: ROOT + "__slide",
  791. container: ROOT + "__slide__container",
  792. arrows: ROOT + "__arrows",
  793. arrow: ROOT + "__arrow",
  794. prev: ROOT + "__arrow--prev",
  795. next: ROOT + "__arrow--next",
  796. pagination: ROOT + "__pagination",
  797. page: ROOT + "__pagination__page",
  798. clone: ROOT + "__slide--clone",
  799. progress: ROOT + "__progress",
  800. bar: ROOT + "__progress__bar",
  801. autoplay: ROOT + "__autoplay",
  802. play: ROOT + "__play",
  803. pause: ROOT + "__pause",
  804. spinner: ROOT + "__spinner",
  805. sr: ROOT + "__sr"
  806. };
  807. /**
  808. * Definitions of status classes.
  809. *
  810. * @type {Object}
  811. */
  812. var STATUS_CLASSES = {
  813. active: 'is-active',
  814. visible: 'is-visible',
  815. loading: 'is-loading'
  816. };
  817. /**
  818. * Export i18n texts as object.
  819. *
  820. * @author Naotoshi Fujita
  821. * @copyright Naotoshi Fujita. All rights reserved.
  822. */
  823. /**
  824. * Texts for i18n.
  825. *
  826. * @type {Object}
  827. */
  828. var I18N = {
  829. prev: 'Previous slide',
  830. next: 'Next slide',
  831. first: 'Go to first slide',
  832. last: 'Go to last slide',
  833. slideX: 'Go to slide %s',
  834. pageX: 'Go to page %s',
  835. play: 'Start autoplay',
  836. pause: 'Pause autoplay'
  837. };
  838. /**
  839. * Export default options.
  840. *
  841. * @author Naotoshi Fujita
  842. * @copyright Naotoshi Fujita. All rights reserved.
  843. */
  844. var DEFAULTS = {
  845. /**
  846. * Determine a slider type.
  847. * - 'slide': Regular slider.
  848. * - 'loop' : Carousel slider.
  849. * - 'fade' : Change slides with fade transition. perPage, drag options are ignored.
  850. *
  851. * @type {string}
  852. */
  853. type: 'slide',
  854. /**
  855. * Whether to rewind a slider before the first slide or after the last one.
  856. * In "loop" mode, this option is ignored.
  857. *
  858. * @type {boolean}
  859. */
  860. rewind: false,
  861. /**
  862. * Transition speed in milliseconds.
  863. *
  864. * @type {number}
  865. */
  866. speed: 400,
  867. /**
  868. * Transition speed on rewind in milliseconds.
  869. *
  870. * @type {number}
  871. */
  872. rewindSpeed: 0,
  873. /**
  874. * Whether to prevent any actions while a slider is transitioning.
  875. * If false, navigation, drag and swipe work while the slider is running.
  876. * Even so, it will be forced to wait for transition in some cases in the loop mode to shift a slider.
  877. *
  878. * @type {boolean}
  879. */
  880. waitForTransition: true,
  881. /**
  882. * Define slider max width.
  883. *
  884. * @type {number}
  885. */
  886. width: 0,
  887. /**
  888. * Define slider height.
  889. *
  890. * @type {number}
  891. */
  892. height: 0,
  893. /**
  894. * Fix width of slides. CSS format is allowed such as 10em, 80% or 80vw.
  895. * perPage number will be ignored when this option is falsy.
  896. *
  897. * @type {number|string}
  898. */
  899. fixedWidth: 0,
  900. /**
  901. * Fix height of slides. CSS format is allowed such as 10em, 80vh but % unit is not accepted.
  902. * heightRatio option will be ignored when this option is falsy.
  903. *
  904. * @type {number|string}
  905. */
  906. fixedHeight: 0,
  907. /**
  908. * Determine height of slides by ratio to a slider width.
  909. * This will be ignored when the fixedHeight is provided.
  910. *
  911. * @type {number}
  912. */
  913. heightRatio: 0,
  914. /**
  915. * If true, slide width will be determined by the element width itself.
  916. * - perPage/perMove should be 1.
  917. *
  918. * @type {boolean}
  919. */
  920. autoWidth: false,
  921. /**
  922. * If true, slide height will be determined by the element width itself.
  923. * - perPage/perMove should be 1.
  924. *
  925. * @type {boolean}
  926. */
  927. autoHeight: false,
  928. /**
  929. * Determine how many slides should be displayed per page.
  930. *
  931. * @type {number}
  932. */
  933. perPage: 1,
  934. /**
  935. * Determine how many slides should be moved when a slider goes to next or perv.
  936. *
  937. * @type {number}
  938. */
  939. perMove: 0,
  940. /**
  941. * Determine manually how many clones should be generated on the left and right side.
  942. * The total number of clones will be twice of this number.
  943. *
  944. * @type {number}
  945. */
  946. clones: 0,
  947. /**
  948. * Start index.
  949. *
  950. * @type {number}
  951. */
  952. start: 0,
  953. /**
  954. * Determine which slide should be focused if there are multiple slides in a page.
  955. * A string "center" is acceptable for centering slides.
  956. *
  957. * @type {boolean|number|string}
  958. */
  959. focus: false,
  960. /**
  961. * Gap between slides. CSS format is allowed such as 1em.
  962. *
  963. * @type {number|string}
  964. */
  965. gap: 0,
  966. /**
  967. * Set padding-left/right in horizontal mode or padding-top/bottom in vertical one.
  968. * Give a single value to set a same size for both sides or
  969. * do an object for different sizes.
  970. * Also, CSS format is allowed such as 1em.
  971. *
  972. * @example
  973. * - 10: Number
  974. * - '1em': CSS format.
  975. * - { left: 0, right: 20 }: Object for different sizes in horizontal mode.
  976. * - { top: 0, bottom: 20 }: Object for different sizes in vertical mode.
  977. *
  978. * @type {number|string|Object}
  979. */
  980. padding: 0,
  981. /**
  982. * Whether to append arrows.
  983. *
  984. * @type {boolean}
  985. */
  986. arrows: true,
  987. /**
  988. * Change the arrow SVG path like 'm7.61 0.807-2.12...'.
  989. *
  990. * @type {string}
  991. */
  992. arrowPath: '',
  993. /**
  994. * Whether to append pagination(indicator dots) or not.
  995. *
  996. * @type {boolean}
  997. */
  998. pagination: true,
  999. /**
  1000. * Activate autoplay.
  1001. *
  1002. * @type {boolean}
  1003. */
  1004. autoplay: false,
  1005. /**
  1006. * Autoplay interval in milliseconds.
  1007. *
  1008. * @type {number}
  1009. */
  1010. interval: 5000,
  1011. /**
  1012. * Whether to stop autoplay when a slider is hovered.
  1013. *
  1014. * @type {boolean}
  1015. */
  1016. pauseOnHover: true,
  1017. /**
  1018. * Whether to stop autoplay when a slider elements are focused.
  1019. * True is recommended for accessibility.
  1020. *
  1021. * @type {boolean}
  1022. */
  1023. pauseOnFocus: true,
  1024. /**
  1025. * Whether to reset progress of the autoplay timer when resumed.
  1026. *
  1027. * @type {boolean}
  1028. */
  1029. resetProgress: true,
  1030. /**
  1031. * Loading images lazily.
  1032. * Image src must be provided by a data-splide-lazy attribute.
  1033. *
  1034. * - false: Do nothing.
  1035. * - 'nearby': Only images around an active slide will be loaded.
  1036. * - 'sequential': All images will be sequentially loaded.
  1037. *
  1038. * @type {boolean|string}
  1039. */
  1040. lazyLoad: false,
  1041. /**
  1042. * This option works only when a lazyLoad option is "nearby".
  1043. * Determine how many pages(not slides) around an active slide should be loaded beforehand.
  1044. *
  1045. * @type {number}
  1046. */
  1047. preloadPages: 1,
  1048. /**
  1049. * Easing for CSS transition. For example, linear, ease or cubic-bezier().
  1050. *
  1051. * @type {string}
  1052. */
  1053. easing: 'cubic-bezier(.42,.65,.27,.99)',
  1054. /**
  1055. * Whether to enable keyboard shortcuts
  1056. * - true or 'global': Listen to keydown event of the document.
  1057. * - 'focused': Listen to the keydown event of the slider root element. tabindex="0" will be added to the element.
  1058. * - false: Disable keyboard shortcuts.
  1059. *
  1060. * @type {boolean|string}
  1061. */
  1062. keyboard: 'global',
  1063. /**
  1064. * Whether to allow mouse drag and touch swipe.
  1065. *
  1066. * @type {boolean}
  1067. */
  1068. drag: true,
  1069. /**
  1070. * The angle threshold for drag.
  1071. * The slider starts moving only when the drag angle is less than this threshold.
  1072. *
  1073. * @type {number}
  1074. */
  1075. dragAngleThreshold: 30,
  1076. /**
  1077. * Distance threshold for determining if the action is "flick" or "swipe".
  1078. * When a drag distance is over this value, the action will be treated as "swipe", not "flick".
  1079. *
  1080. * @type {number}
  1081. */
  1082. swipeDistanceThreshold: 150,
  1083. /**
  1084. * Velocity threshold for determining if the action is "flick" or "swipe".
  1085. * Around 0.5 is recommended.
  1086. *
  1087. * @type {number}
  1088. */
  1089. flickVelocityThreshold: .6,
  1090. /**
  1091. * Determine power of flick. The larger number this is, the farther a slider runs by flick.
  1092. * Around 500 is recommended.
  1093. *
  1094. * @type {number}
  1095. */
  1096. flickPower: 600,
  1097. /**
  1098. * Limit a number of pages to move by flick.
  1099. *
  1100. * @type {number}
  1101. */
  1102. flickMaxPages: 1,
  1103. /**
  1104. * Slider direction.
  1105. * - 'ltr': Left to right.
  1106. * - 'rtl': Right to left.
  1107. * - 'ttb': Top to bottom.
  1108. *
  1109. * @type {string}
  1110. */
  1111. direction: 'ltr',
  1112. /**
  1113. * Set img src to background-image of its parent element.
  1114. * Images with various sizes can be displayed as same dimension without cropping work.
  1115. * fixedHeight or heightRatio is required.
  1116. *
  1117. * @type {boolean}
  1118. */
  1119. cover: false,
  1120. /**
  1121. * Whether to enable accessibility(aria and screen reader texts) or not.
  1122. *
  1123. * @type {boolean}
  1124. */
  1125. accessibility: true,
  1126. /**
  1127. * Whether to add tabindex="0" to visible slides or not.
  1128. *
  1129. * @type {boolean}
  1130. */
  1131. slideFocus: true,
  1132. /**
  1133. * Determine if a slider is navigation for another.
  1134. * Use "sync" API to synchronize two sliders.
  1135. *
  1136. * @type {boolean}
  1137. */
  1138. isNavigation: false,
  1139. /**
  1140. * Whether to trim spaces before the fist slide or after the last one when "focus" is not 0.
  1141. *
  1142. * @type {boolean}
  1143. */
  1144. trimSpace: true,
  1145. /**
  1146. * The "is-active" class is added after transition as default.
  1147. * If true, it will be added before move.
  1148. *
  1149. * @type {boolean}
  1150. */
  1151. updateOnMove: false,
  1152. /**
  1153. * Throttle duration in milliseconds for the resize event.
  1154. *
  1155. * @type {number}
  1156. */
  1157. throttle: 100,
  1158. /**
  1159. * Whether to destroy a slider or not.
  1160. *
  1161. * @type {boolean}
  1162. */
  1163. destroy: false,
  1164. /**
  1165. * Options for specific breakpoints.
  1166. *
  1167. * @example
  1168. * {
  1169. * 1000: {
  1170. * perPage: 3,
  1171. * gap: 20
  1172. * },
  1173. * 600: {
  1174. * perPage: 1,
  1175. * gap: 5,
  1176. * }
  1177. * }
  1178. *
  1179. * @type {boolean|Object}
  1180. */
  1181. breakpoints: false,
  1182. /**
  1183. * Collection of class names.
  1184. *
  1185. * @see ./classes.js
  1186. *
  1187. * @type {Object}
  1188. */
  1189. classes: ELEMENT_CLASSES,
  1190. /**
  1191. * Collection of i18n texts.
  1192. *
  1193. * @see ./i18n.js
  1194. *
  1195. * @type {Object}
  1196. */
  1197. i18n: I18N
  1198. };
  1199. /**
  1200. * Export state constants.
  1201. *
  1202. * @author Naotoshi Fujita
  1203. * @copyright Naotoshi Fujita. All rights reserved.
  1204. */
  1205. /**
  1206. * Splide has been just created.
  1207. *
  1208. * @type {number}
  1209. */
  1210. var CREATED = 1;
  1211. /**
  1212. * All components have been mounted and initialized.
  1213. *
  1214. * @type {number}
  1215. */
  1216. var MOUNTED = 2;
  1217. /**
  1218. * Splide is ready for transition.
  1219. *
  1220. * @type {number}
  1221. */
  1222. var IDLE = 3;
  1223. /**
  1224. * Splide is moving.
  1225. *
  1226. * @type {number}
  1227. */
  1228. var MOVING = 4;
  1229. /**
  1230. * Splide is moving.
  1231. *
  1232. * @type {number}
  1233. */
  1234. var DESTROYED = 5;
  1235. var STATES = /*#__PURE__*/Object.freeze({
  1236. __proto__: null,
  1237. CREATED: CREATED,
  1238. MOUNTED: MOUNTED,
  1239. IDLE: IDLE,
  1240. MOVING: MOVING,
  1241. DESTROYED: DESTROYED
  1242. });
  1243. /**
  1244. * The main class for applying Splide to an element.
  1245. *
  1246. * @author Naotoshi Fujita
  1247. * @copyright Naotoshi Fujita. All rights reserved.
  1248. */
  1249. /**
  1250. * The main class for applying Splide to an element,
  1251. * providing some APIs to control the behavior.
  1252. */
  1253. var Splide$1 = /*#__PURE__*/function () {
  1254. /**
  1255. * Splide constructor.
  1256. *
  1257. * @throws {Error} When the given root element or selector is invalid.
  1258. *
  1259. * @param {Element|string} root - A selector for a root element or an element itself.
  1260. * @param {Object} options - Optional. Options to change default behaviour.
  1261. * @param {Object} Components - Optional. Components.
  1262. */
  1263. function Splide$1(root, options, Components) {
  1264. if (options === void 0) {
  1265. options = {};
  1266. }
  1267. if (Components === void 0) {
  1268. Components = {};
  1269. }
  1270. this.root = root instanceof Element ? root : document.querySelector(root);
  1271. exist(this.root, 'An invalid element/selector was given.');
  1272. this.Components = null;
  1273. this.Event = Event();
  1274. this.State = State(CREATED);
  1275. this.STATES = STATES;
  1276. this._o = merge(DEFAULTS, options);
  1277. this._i = 0;
  1278. this._c = Components;
  1279. this._e = {}; // Extensions
  1280. this._t = null; // Transition
  1281. }
  1282. /**
  1283. * Compose and mount components.
  1284. *
  1285. * @param {Object} Extensions - Optional. Additional components.
  1286. * @param {function} Transition - Optional. Set a custom transition component.
  1287. *
  1288. * @return {Splide|undefined} - This instance or undefined if an exception occurred.
  1289. */
  1290. var _proto = Splide$1.prototype;
  1291. _proto.mount = function mount(Extensions, Transition) {
  1292. var _this = this;
  1293. if (Extensions === void 0) {
  1294. Extensions = this._e;
  1295. }
  1296. if (Transition === void 0) {
  1297. Transition = this._t;
  1298. }
  1299. // Reset the state.
  1300. this.State.set(CREATED);
  1301. this._e = Extensions;
  1302. this._t = Transition;
  1303. this.Components = compose(this, merge(this._c, Extensions), Transition);
  1304. try {
  1305. each(this.Components, function (component, key) {
  1306. var required = component.required;
  1307. if (required === undefined || required) {
  1308. component.mount && component.mount();
  1309. } else {
  1310. delete _this.Components[key];
  1311. }
  1312. });
  1313. } catch (e) {
  1314. error(e.message);
  1315. return;
  1316. }
  1317. var State = this.State;
  1318. State.set(MOUNTED);
  1319. each(this.Components, function (component) {
  1320. component.mounted && component.mounted();
  1321. });
  1322. this.emit('mounted');
  1323. State.set(IDLE);
  1324. this.emit('ready');
  1325. applyStyle(this.root, {
  1326. visibility: 'visible'
  1327. });
  1328. this.on('move drag', function () {
  1329. return State.set(MOVING);
  1330. }).on('moved dragged', function () {
  1331. return State.set(IDLE);
  1332. });
  1333. return this;
  1334. }
  1335. /**
  1336. * Set sync target.
  1337. *
  1338. * @param {Splide} splide - A Splide instance.
  1339. *
  1340. * @return {Splide} - This instance.
  1341. */
  1342. ;
  1343. _proto.sync = function sync(splide) {
  1344. this.sibling = splide;
  1345. return this;
  1346. }
  1347. /**
  1348. * Register callback fired on the given event(s).
  1349. *
  1350. * @param {string} events - An event name. Use space to separate multiple events.
  1351. * Also, namespace is accepted by dot, such as 'resize.{namespace}'.
  1352. * @param {function} handler - A callback function.
  1353. * @param {Element} elm - Optional. Native event will be listened to when this arg is provided.
  1354. * @param {Object} options - Optional. Options for addEventListener.
  1355. *
  1356. * @return {Splide} - This instance.
  1357. */
  1358. ;
  1359. _proto.on = function on(events, handler, elm, options) {
  1360. if (elm === void 0) {
  1361. elm = null;
  1362. }
  1363. if (options === void 0) {
  1364. options = {};
  1365. }
  1366. this.Event.on(events, handler, elm, options);
  1367. return this;
  1368. }
  1369. /**
  1370. * Unsubscribe the given event.
  1371. *
  1372. * @param {string} events - A event name.
  1373. * @param {Element} elm - Optional. removeEventListener() will be called when this arg is provided.
  1374. *
  1375. * @return {Splide} - This instance.
  1376. */
  1377. ;
  1378. _proto.off = function off(events, elm) {
  1379. if (elm === void 0) {
  1380. elm = null;
  1381. }
  1382. this.Event.off(events, elm);
  1383. return this;
  1384. }
  1385. /**
  1386. * Emit an event.
  1387. *
  1388. * @param {string} event - An event name.
  1389. * @param {*} args - Any number of arguments passed to handlers.
  1390. */
  1391. ;
  1392. _proto.emit = function emit(event) {
  1393. var _this$Event;
  1394. for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  1395. args[_key2 - 1] = arguments[_key2];
  1396. }
  1397. (_this$Event = this.Event).emit.apply(_this$Event, [event].concat(args));
  1398. return this;
  1399. }
  1400. /**
  1401. * Go to the slide specified by the given control.
  1402. *
  1403. * @param {string|number} control - A control pattern.
  1404. * @param {boolean} wait - Optional. Whether to wait for transition.
  1405. */
  1406. ;
  1407. _proto.go = function go(control, wait) {
  1408. if (wait === void 0) {
  1409. wait = this.options.waitForTransition;
  1410. }
  1411. if (this.State.is(IDLE) || this.State.is(MOVING) && !wait) {
  1412. this.Components.Controller.go(control, false);
  1413. }
  1414. return this;
  1415. }
  1416. /**
  1417. * Verify whether the slider type is the given one or not.
  1418. *
  1419. * @param {string} type - A slider type.
  1420. *
  1421. * @return {boolean} - True if the slider type is the provided type or false if not.
  1422. */
  1423. ;
  1424. _proto.is = function is(type) {
  1425. return type === this._o.type;
  1426. }
  1427. /**
  1428. * Insert a slide.
  1429. *
  1430. * @param {Element|string} slide - A slide element to be added.
  1431. * @param {number} index - A slide will be added at the position.
  1432. */
  1433. ;
  1434. _proto.add = function add(slide, index) {
  1435. if (index === void 0) {
  1436. index = -1;
  1437. }
  1438. this.Components.Elements.add(slide, index, this.refresh.bind(this));
  1439. return this;
  1440. }
  1441. /**
  1442. * Remove the slide designated by the index.
  1443. *
  1444. * @param {number} index - A slide index.
  1445. */
  1446. ;
  1447. _proto.remove = function remove(index) {
  1448. this.Components.Elements.remove(index);
  1449. this.refresh();
  1450. return this;
  1451. }
  1452. /**
  1453. * Destroy all Slide objects and clones and recreate them again.
  1454. */
  1455. ;
  1456. _proto.refresh = function refresh() {
  1457. this.emit('refresh:before').emit('refresh').emit('resize');
  1458. return this;
  1459. }
  1460. /**
  1461. * Destroy the Splide.
  1462. * "Completely" boolean is mainly for breakpoints.
  1463. *
  1464. * @param {boolean} completely - Destroy completely.
  1465. */
  1466. ;
  1467. _proto.destroy = function destroy(completely) {
  1468. var _this2 = this;
  1469. if (completely === void 0) {
  1470. completely = true;
  1471. }
  1472. // Postpone destroy because it should be done after mount.
  1473. if (this.State.is(CREATED)) {
  1474. this.on('ready', function () {
  1475. return _this2.destroy(completely);
  1476. });
  1477. return;
  1478. }
  1479. values(this.Components).reverse().forEach(function (component) {
  1480. component.destroy && component.destroy(completely);
  1481. });
  1482. this.emit('destroy', completely); // Destroy all event handlers, including ones for native events.
  1483. this.Event.destroy();
  1484. this.State.set(DESTROYED);
  1485. return this;
  1486. }
  1487. /**
  1488. * Return the current slide index.
  1489. *
  1490. * @return {number} - The current slide index.
  1491. // */
  1492. ;
  1493. _createClass(Splide$1, [{
  1494. key: "index",
  1495. get: function get() {
  1496. return this._i;
  1497. }
  1498. /**
  1499. * Set the current slide index.
  1500. *
  1501. * @param {number|string} index - A new index.
  1502. */
  1503. ,
  1504. set: function set(index) {
  1505. this._i = parseInt(index);
  1506. }
  1507. /**
  1508. * Return length of slides.
  1509. * This is an alias of Elements.length.
  1510. *
  1511. * @return {number} - A number of slides.
  1512. */
  1513. }, {
  1514. key: "length",
  1515. get: function get() {
  1516. return this.Components.Elements.length;
  1517. }
  1518. /**
  1519. * Return options.
  1520. *
  1521. * @return {Object} - Options object.
  1522. */
  1523. }, {
  1524. key: "options",
  1525. get: function get() {
  1526. return this._o;
  1527. }
  1528. /**
  1529. * Set options with merging the given object to the current one.
  1530. *
  1531. * @param {Object} options - New options.
  1532. */
  1533. ,
  1534. set: function set(options) {
  1535. var created = this.State.is(CREATED);
  1536. if (!created) {
  1537. this.emit('update');
  1538. }
  1539. this._o = merge(this._o, options);
  1540. if (!created) {
  1541. this.emit('updated', this._o);
  1542. }
  1543. }
  1544. /**
  1545. * Return the class list.
  1546. * This is an alias of Splide.options.classList.
  1547. *
  1548. * @return {Object} - An object containing all class list.
  1549. */
  1550. }, {
  1551. key: "classes",
  1552. get: function get() {
  1553. return this._o.classes;
  1554. }
  1555. /**
  1556. * Return the i18n strings.
  1557. * This is an alias of Splide.options.i18n.
  1558. *
  1559. * @return {Object} - An object containing all i18n strings.
  1560. */
  1561. }, {
  1562. key: "i18n",
  1563. get: function get() {
  1564. return this._o.i18n;
  1565. }
  1566. }]);
  1567. return Splide$1;
  1568. }();
  1569. /**
  1570. * The component for initializing options.
  1571. *
  1572. * @author Naotoshi Fujita
  1573. * @copyright Naotoshi Fujita. All rights reserved.
  1574. */
  1575. /**
  1576. * The component for initializing options.
  1577. *
  1578. * @param {Splide} Splide - A Splide instance.
  1579. *
  1580. * @return {Object} - The component object.
  1581. */
  1582. var Options = function Options(Splide) {
  1583. /**
  1584. * Retrieve options from the data attribute.
  1585. * Note that IE10 doesn't support dataset property.
  1586. *
  1587. * @type {string}
  1588. */
  1589. var options = getAttribute(Splide.root, 'data-splide');
  1590. if (options) {
  1591. try {
  1592. Splide.options = JSON.parse(options);
  1593. } catch (e) {
  1594. error(e.message);
  1595. }
  1596. }
  1597. return {
  1598. /**
  1599. * Called when the component is mounted.
  1600. */
  1601. mount: function mount() {
  1602. if (Splide.State.is(CREATED)) {
  1603. Splide.index = Splide.options.start;
  1604. }
  1605. }
  1606. };
  1607. };
  1608. /**
  1609. * Export layout modes.
  1610. *
  1611. * @author Naotoshi Fujita
  1612. * @copyright Naotoshi Fujita. All rights reserved.
  1613. */
  1614. /**
  1615. * Enumerate slides from right to left.
  1616. *
  1617. * @type {string}
  1618. */
  1619. var RTL = 'rtl';
  1620. /**
  1621. * Enumerate slides in a col.
  1622. *
  1623. * @type {string}
  1624. */
  1625. var TTB = 'ttb';
  1626. /**
  1627. * The sub component for handling each slide.
  1628. *
  1629. * @author Naotoshi Fujita
  1630. * @copyright Naotoshi Fujita. All rights reserved.
  1631. */
  1632. /**
  1633. * Events for restoring original styles.
  1634. *
  1635. * @type {string}
  1636. */
  1637. var STYLE_RESTORE_EVENTS = 'update.slide';
  1638. /**
  1639. * The sub component for handling each slide.
  1640. *
  1641. * @param {Splide} Splide - A Splide instance.
  1642. * @param {number} index - An unique slide index.
  1643. * @param {number} realIndex - Clones should pass a real slide index.
  1644. * @param {Element} slide - A slide element.
  1645. *
  1646. * @return {Object} - The sub component object.
  1647. */
  1648. var Slide = function Slide(Splide, index, realIndex, slide) {
  1649. /**
  1650. * Whether to update "is-active" class before or after transition.
  1651. *
  1652. * @type {boolean}
  1653. */
  1654. var updateOnMove = Splide.options.updateOnMove;
  1655. /**
  1656. * Events when the slide status is updated.
  1657. * Append a namespace to remove listeners later.
  1658. *
  1659. * @type {string}
  1660. */
  1661. var STATUS_UPDATE_EVENTS = 'ready.slide updated.slide resized.slide moved.slide' + (updateOnMove ? ' move.slide' : '');
  1662. /**
  1663. * Slide sub component object.
  1664. *
  1665. * @type {Object}
  1666. */
  1667. var Slide = {
  1668. /**
  1669. * Slide element.
  1670. *
  1671. * @type {Element}
  1672. */
  1673. slide: slide,
  1674. /**
  1675. * Slide index.
  1676. *
  1677. * @type {number}
  1678. */
  1679. index: index,
  1680. /**
  1681. * Real index for clones.
  1682. *
  1683. * @type {number}
  1684. */
  1685. realIndex: realIndex,
  1686. /**
  1687. * Container element if available.
  1688. *
  1689. * @type {Element|undefined}
  1690. */
  1691. container: child(slide, Splide.classes.container),
  1692. /**
  1693. * Whether this is a cloned slide or not.
  1694. *
  1695. * @type {boolean}
  1696. */
  1697. isClone: realIndex > -1,
  1698. /**
  1699. * Called when the component is mounted.
  1700. */
  1701. mount: function mount() {
  1702. var _this3 = this;
  1703. if (!this.isClone) {
  1704. slide.id = Splide.root.id + "-slide" + pad(index + 1);
  1705. }
  1706. Splide.on(STATUS_UPDATE_EVENTS, function () {
  1707. return _this3.update();
  1708. }).on(STYLE_RESTORE_EVENTS, restoreStyles).on('click', function () {
  1709. return Splide.emit('click', _this3);
  1710. }, slide);
  1711. /*
  1712. * Add "is-active" class to a clone element temporarily
  1713. * and it will be removed on "moved" event.
  1714. */
  1715. if (updateOnMove) {
  1716. Splide.on('move.slide', function (newIndex) {
  1717. if (newIndex === realIndex) {
  1718. _update(true, false);
  1719. }
  1720. });
  1721. } // Make sure the slide is shown.
  1722. applyStyle(slide, {
  1723. display: ''
  1724. }); // Hold the original styles.
  1725. this.styles = getAttribute(slide, 'style') || '';
  1726. },
  1727. /**
  1728. * Destroy.
  1729. */
  1730. destroy: function destroy() {
  1731. Splide.off(STATUS_UPDATE_EVENTS).off(STYLE_RESTORE_EVENTS).off('click', slide);
  1732. removeClass(slide, values(STATUS_CLASSES));
  1733. restoreStyles();
  1734. removeAttribute(this.container, 'style');
  1735. },
  1736. /**
  1737. * Update active and visible status.
  1738. */
  1739. update: function update() {
  1740. _update(this.isActive(), false);
  1741. _update(this.isVisible(), true);
  1742. },
  1743. /**
  1744. * Check whether this slide is active or not.
  1745. *
  1746. * @return {boolean} - True if the slide is active or false if not.
  1747. */
  1748. isActive: function isActive() {
  1749. return Splide.index === index;
  1750. },
  1751. /**
  1752. * Check whether this slide is visible in the viewport or not.
  1753. *
  1754. * @return {boolean} - True if the slide is visible or false if not.
  1755. */
  1756. isVisible: function isVisible() {
  1757. var active = this.isActive();
  1758. if (Splide.is(FADE) || active) {
  1759. return active;
  1760. }
  1761. var ceil = Math.ceil;
  1762. var trackRect = getRect(Splide.Components.Elements.track);
  1763. var slideRect = getRect(slide);
  1764. if (Splide.options.direction === TTB) {
  1765. return trackRect.top <= slideRect.top && slideRect.bottom <= ceil(trackRect.bottom);
  1766. }
  1767. return trackRect.left <= slideRect.left && slideRect.right <= ceil(trackRect.right);
  1768. },
  1769. /**
  1770. * Calculate how far this slide is from another slide and
  1771. * return true if the distance is within the given number.
  1772. *
  1773. * @param {number} from - Index of a target slide.
  1774. * @param {number} within - True if the slide is within this number.
  1775. *
  1776. * @return {boolean} - True if the slide is within the number or false otherwise.
  1777. */
  1778. isWithin: function isWithin(from, within) {
  1779. var diff = Math.abs(from - index);
  1780. if (!Splide.is(SLIDE) && !this.isClone) {
  1781. diff = Math.min(diff, Splide.length - diff);
  1782. }
  1783. return diff < within;
  1784. }
  1785. };
  1786. /**
  1787. * Update classes for activity or visibility.
  1788. *
  1789. * @param {boolean} active - Is active/visible or not.
  1790. * @param {boolean} forVisibility - Toggle classes for activity or visibility.
  1791. */
  1792. function _update(active, forVisibility) {
  1793. var type = forVisibility ? 'visible' : 'active';
  1794. var className = STATUS_CLASSES[type];
  1795. if (active) {
  1796. addClass(slide, className);
  1797. Splide.emit("" + type, Slide);
  1798. } else {
  1799. if (hasClass(slide, className)) {
  1800. removeClass(slide, className);
  1801. Splide.emit("" + (forVisibility ? 'hidden' : 'inactive'), Slide);
  1802. }
  1803. }
  1804. }
  1805. /**
  1806. * Restore the original styles.
  1807. */
  1808. function restoreStyles() {
  1809. setAttribute(slide, 'style', Slide.styles);
  1810. }
  1811. return Slide;
  1812. };
  1813. /**
  1814. * The component for main elements.
  1815. *
  1816. * @author Naotoshi Fujita
  1817. * @copyright Naotoshi Fujita. All rights reserved.
  1818. */
  1819. /**
  1820. * The property name for UID stored in a window object.
  1821. *
  1822. * @type {string}
  1823. */
  1824. var UID_NAME = 'uid';
  1825. /**
  1826. * The component for main elements.
  1827. *
  1828. * @param {Splide} Splide - A Splide instance.
  1829. * @param {Object} Components - An object containing components.
  1830. *
  1831. * @return {Object} - The component object.
  1832. */
  1833. var Elements = function Elements(Splide, Components) {
  1834. /**
  1835. * Hold the root element.
  1836. *
  1837. * @type {Element}
  1838. */
  1839. var root = Splide.root;
  1840. /**
  1841. * Hold the class list.
  1842. *
  1843. * @type {Object}
  1844. */
  1845. var classes = Splide.classes;
  1846. /**
  1847. * Store Slide objects.
  1848. *
  1849. * @type {Array}
  1850. */
  1851. var Slides = [];
  1852. /*
  1853. * Assign unique ID to the root element if it doesn't have the one.
  1854. * Note that IE doesn't support padStart() to fill the uid by 0.
  1855. */
  1856. if (!root.id) {
  1857. window.splide = window.splide || {};
  1858. var uid = window.splide[UID_NAME] || 0;
  1859. window.splide[UID_NAME] = ++uid;
  1860. root.id = "splide" + pad(uid);
  1861. }
  1862. /**
  1863. * Elements component object.
  1864. *
  1865. * @type {Object}
  1866. */
  1867. var Elements = {
  1868. /**
  1869. * Called when the component is mounted.
  1870. * Collect main elements and store them as member properties.
  1871. */
  1872. mount: function mount() {
  1873. var _this4 = this;
  1874. this.init();
  1875. Splide.on('refresh', function () {
  1876. _this4.destroy();
  1877. _this4.init();
  1878. }).on('updated', function () {
  1879. removeClass(root, getClasses());
  1880. addClass(root, getClasses());
  1881. });
  1882. },
  1883. /**
  1884. * Destroy.
  1885. */
  1886. destroy: function destroy() {
  1887. Slides.forEach(function (Slide) {
  1888. Slide.destroy();
  1889. });
  1890. Slides = [];
  1891. removeClass(root, getClasses());
  1892. },
  1893. /**
  1894. * Initialization.
  1895. */
  1896. init: function init() {
  1897. var _this5 = this;
  1898. collect();
  1899. addClass(root, getClasses());
  1900. this.slides.forEach(function (slide, index) {
  1901. _this5.register(slide, index, -1);
  1902. });
  1903. },
  1904. /**
  1905. * Register a slide to create a Slide object and handle its behavior.
  1906. *
  1907. * @param {Element} slide - A slide element.
  1908. * @param {number} index - A unique index. This can be negative.
  1909. * @param {number} realIndex - A real index for clones. Set -1 for real slides.
  1910. */
  1911. register: function register(slide, index, realIndex) {
  1912. var SlideObject = Slide(Splide, index, realIndex, slide);
  1913. SlideObject.mount();
  1914. Slides.push(SlideObject);
  1915. },
  1916. /**
  1917. * Return the Slide object designated by the index.
  1918. * Note that "find" is not supported by IE.
  1919. *
  1920. * @return {Object|undefined} - A Slide object if available. Undefined if not.
  1921. */
  1922. getSlide: function getSlide(index) {
  1923. return Slides.filter(function (Slide) {
  1924. return Slide.index === index;
  1925. })[0];
  1926. },
  1927. /**
  1928. * Return all Slide objects.
  1929. *
  1930. * @param {boolean} includeClones - Whether to include cloned slides or not.
  1931. *
  1932. * @return {Object[]} - Slide objects.
  1933. */
  1934. getSlides: function getSlides(includeClones) {
  1935. return includeClones ? Slides : Slides.filter(function (Slide) {
  1936. return !Slide.isClone;
  1937. });
  1938. },
  1939. /**
  1940. * Return Slide objects belonging to the given page.
  1941. *
  1942. * @param {number} page - A page number.
  1943. *
  1944. * @return {Object[]} - An array containing Slide objects.
  1945. */
  1946. getSlidesByPage: function getSlidesByPage(page) {
  1947. var idx = Components.Controller.toIndex(page);
  1948. var options = Splide.options;
  1949. var max = options.focus !== false ? 1 : options.perPage;
  1950. return Slides.filter(function (_ref2) {
  1951. var index = _ref2.index;
  1952. return idx <= index && index < idx + max;
  1953. });
  1954. },
  1955. /**
  1956. * Insert a slide to a slider.
  1957. * Need to refresh Splide after adding a slide.
  1958. *
  1959. * @param {Node|string} slide - A slide element to be added.
  1960. * @param {number} index - A slide will be added at the position.
  1961. * @param {Function} callback - Called right after the slide is added to the DOM tree.
  1962. */
  1963. add: function add(slide, index, callback) {
  1964. if (typeof slide === 'string') {
  1965. slide = domify(slide);
  1966. }
  1967. if (slide instanceof Element) {
  1968. var ref = this.slides[index]; // This will be removed in mount() of a Slide component.
  1969. applyStyle(slide, {
  1970. display: 'none'
  1971. });
  1972. if (ref) {
  1973. before(slide, ref);
  1974. this.slides.splice(index, 0, slide);
  1975. } else {
  1976. append(this.list, slide);
  1977. this.slides.push(slide);
  1978. }
  1979. loaded(slide, function () {
  1980. callback && callback(slide);
  1981. });
  1982. }
  1983. },
  1984. /**
  1985. * Remove a slide from a slider.
  1986. * Need to refresh Splide after removing a slide.
  1987. *
  1988. * @param index - Slide index.
  1989. */
  1990. remove: function remove(index) {
  1991. _remove(this.slides.splice(index, 1)[0]);
  1992. },
  1993. /**
  1994. * Trigger the provided callback for each Slide object.
  1995. *
  1996. * @param {Function} callback - A callback function. The first argument will be the Slide object.
  1997. */
  1998. each: function each(callback) {
  1999. Slides.forEach(callback);
  2000. },
  2001. /**
  2002. * Return slides length without clones.
  2003. *
  2004. * @return {number} - Slide length.
  2005. */
  2006. get length() {
  2007. return this.slides.length;
  2008. },
  2009. /**
  2010. * Return "SlideObjects" length including clones.
  2011. *
  2012. * @return {number} - Slide length including clones.
  2013. */
  2014. get total() {
  2015. return Slides.length;
  2016. }
  2017. };
  2018. /**
  2019. * Collect elements.
  2020. */
  2021. function collect() {
  2022. Elements.slider = child(root, classes.slider);
  2023. Elements.track = find(root, "." + classes.track);
  2024. Elements.list = child(Elements.track, classes.list);
  2025. exist(Elements.track && Elements.list, 'Track or list was not found.');
  2026. Elements.slides = children(Elements.list, classes.slide);
  2027. var arrows = findParts(classes.arrows);
  2028. Elements.arrows = {
  2029. prev: find(arrows, "." + classes.prev),
  2030. next: find(arrows, "." + classes.next)
  2031. };
  2032. var autoplay = findParts(classes.autoplay);
  2033. Elements.bar = find(findParts(classes.progress), "." + classes.bar);
  2034. Elements.play = find(autoplay, "." + classes.play);
  2035. Elements.pause = find(autoplay, "." + classes.pause);
  2036. Elements.track.id = Elements.track.id || root.id + "-track";
  2037. Elements.list.id = Elements.list.id || root.id + "-list";
  2038. }
  2039. /**
  2040. * Return class names for the root element.
  2041. */
  2042. function getClasses() {
  2043. var rootClass = classes.root;
  2044. var options = Splide.options;
  2045. return [rootClass + "--" + options.type, rootClass + "--" + options.direction, options.drag ? rootClass + "--draggable" : '', options.isNavigation ? rootClass + "--nav" : '', STATUS_CLASSES.active];
  2046. }
  2047. /**
  2048. * Find parts only from children of the root or track.
  2049. *
  2050. * @return {Element} - A found element or undefined.
  2051. */
  2052. function findParts(className) {
  2053. return child(root, className) || child(Elements.slider, className);
  2054. }
  2055. return Elements;
  2056. };
  2057. /**
  2058. * The component for controlling the track.
  2059. *
  2060. * @author Naotoshi Fujita
  2061. * @copyright Naotoshi Fujita. All rights reserved.
  2062. */
  2063. var floor = Math.floor;
  2064. /**
  2065. * The component for controlling the track.
  2066. *
  2067. * @param {Splide} Splide - A Splide instance.
  2068. * @param {Object} Components - An object containing components.
  2069. *
  2070. * @return {Object} - The component object.
  2071. */
  2072. var Controller = function Controller(Splide, Components) {
  2073. /**
  2074. * Store current options.
  2075. *
  2076. * @type {Object}
  2077. */
  2078. var options;
  2079. /**
  2080. * True if the slide is LOOP mode.
  2081. *
  2082. * @type {boolean}
  2083. */
  2084. var isLoop;
  2085. /**
  2086. * Controller component object.
  2087. *
  2088. * @type {Object}
  2089. */
  2090. var Controller = {
  2091. /**
  2092. * Called when the component is mounted.
  2093. */
  2094. mount: function mount() {
  2095. options = Splide.options;
  2096. isLoop = Splide.is(LOOP);
  2097. bind();
  2098. },
  2099. /**
  2100. * Make track run by the given control.
  2101. * - "+{i}" : Increment the slide index by i.
  2102. * - "-{i}" : Decrement the slide index by i.
  2103. * - "{i}" : Go to the slide whose index is i.
  2104. * - ">" : Go to next page.
  2105. * - "<" : Go to prev page.
  2106. * - ">{i}" : Go to page i.
  2107. *
  2108. * @param {string|number} control - A control pattern.
  2109. * @param {boolean} silently - Go to the destination without event emission.
  2110. */
  2111. go: function go(control, silently) {
  2112. var destIndex = this.trim(this.parse(control));
  2113. Components.Track.go(destIndex, this.rewind(destIndex), silently);
  2114. },
  2115. /**
  2116. * Parse the given control and return the destination index for the track.
  2117. *
  2118. * @param {string} control - A control target pattern.
  2119. *
  2120. * @return {number} - A parsed target.
  2121. */
  2122. parse: function parse(control) {
  2123. var index = Splide.index;
  2124. var matches = String(control).match(/([+\-<>]+)(\d+)?/);
  2125. var indicator = matches ? matches[1] : '';
  2126. var number = matches ? parseInt(matches[2]) : 0;
  2127. switch (indicator) {
  2128. case '+':
  2129. index += number || 1;
  2130. break;
  2131. case '-':
  2132. index -= number || 1;
  2133. break;
  2134. case '>':
  2135. case '<':
  2136. index = parsePage(number, index, indicator === '<');
  2137. break;
  2138. default:
  2139. index = parseInt(control);
  2140. }
  2141. return index;
  2142. },
  2143. /**
  2144. * Compute index from the given page number.
  2145. *
  2146. * @param {number} page - Page number.
  2147. *
  2148. * @return {number} - A computed page number.
  2149. */
  2150. toIndex: function toIndex(page) {
  2151. if (hasFocus()) {
  2152. return page;
  2153. }
  2154. var length = Splide.length;
  2155. var perPage = options.perPage;
  2156. var index = page * perPage;
  2157. index = index - (this.pageLength * perPage - length) * floor(index / length); // Adjustment for the last page.
  2158. if (length - perPage <= index && index < length) {
  2159. index = length - perPage;
  2160. }
  2161. return index;
  2162. },
  2163. /**
  2164. * Compute page number from the given slide index.
  2165. *
  2166. * @param {number} index - Slide index.
  2167. *
  2168. * @return {number} - A computed page number.
  2169. */
  2170. toPage: function toPage(index) {
  2171. if (hasFocus()) {
  2172. return index;
  2173. }
  2174. var length = Splide.length;
  2175. var perPage = options.perPage; // Make the last "perPage" number of slides belong to the last page.
  2176. if (length - perPage <= index && index < length) {
  2177. return floor((length - 1) / perPage);
  2178. }
  2179. return floor(index / perPage);
  2180. },
  2181. /**
  2182. * Trim the given index according to the current mode.
  2183. * Index being returned could be less than 0 or greater than the length in Loop mode.
  2184. *
  2185. * @param {number} index - An index being trimmed.
  2186. *
  2187. * @return {number} - A trimmed index.
  2188. */
  2189. trim: function trim(index) {
  2190. if (!isLoop) {
  2191. index = options.rewind ? this.rewind(index) : between(index, 0, this.edgeIndex);
  2192. }
  2193. return index;
  2194. },
  2195. /**
  2196. * Rewind the given index if it's out of range.
  2197. *
  2198. * @param {number} index - An index.
  2199. *
  2200. * @return {number} - A rewound index.
  2201. */
  2202. rewind: function rewind(index) {
  2203. var edge = this.edgeIndex;
  2204. if (isLoop) {
  2205. while (index > edge) {
  2206. index -= edge + 1;
  2207. }
  2208. while (index < 0) {
  2209. index += edge + 1;
  2210. }
  2211. } else {
  2212. if (index > edge) {
  2213. index = 0;
  2214. } else if (index < 0) {
  2215. index = edge;
  2216. }
  2217. }
  2218. return index;
  2219. },
  2220. /**
  2221. * Check if the direction is "rtl" or not.
  2222. *
  2223. * @return {boolean} - True if "rtl" or false if not.
  2224. */
  2225. isRtl: function isRtl() {
  2226. return options.direction === RTL;
  2227. },
  2228. /**
  2229. * Return the page length.
  2230. *
  2231. * @return {number} - Max page number.
  2232. */
  2233. get pageLength() {
  2234. var length = Splide.length;
  2235. return hasFocus() ? length : Math.ceil(length / options.perPage);
  2236. },
  2237. /**
  2238. * Return the edge index.
  2239. *
  2240. * @return {number} - Edge index.
  2241. */
  2242. get edgeIndex() {
  2243. var length = Splide.length;
  2244. if (!length) {
  2245. return 0;
  2246. }
  2247. if (hasFocus() || options.isNavigation || isLoop) {
  2248. return length - 1;
  2249. }
  2250. return length - options.perPage;
  2251. },
  2252. /**
  2253. * Return the index of the previous slide.
  2254. *
  2255. * @return {number} - The index of the previous slide if available. -1 otherwise.
  2256. */
  2257. get prevIndex() {
  2258. var prev = Splide.index - 1;
  2259. if (isLoop || options.rewind) {
  2260. prev = this.rewind(prev);
  2261. }
  2262. return prev > -1 ? prev : -1;
  2263. },
  2264. /**
  2265. * Return the index of the next slide.
  2266. *
  2267. * @return {number} - The index of the next slide if available. -1 otherwise.
  2268. */
  2269. get nextIndex() {
  2270. var next = Splide.index + 1;
  2271. if (isLoop || options.rewind) {
  2272. next = this.rewind(next);
  2273. }
  2274. return Splide.index < next && next <= this.edgeIndex || next === 0 ? next : -1;
  2275. }
  2276. };
  2277. /**
  2278. * Listen to some events.
  2279. */
  2280. function bind() {
  2281. Splide.on('move', function (newIndex) {
  2282. Splide.index = newIndex;
  2283. }).on('updated refresh', function (newOptions) {
  2284. options = newOptions || options;
  2285. Splide.index = between(Splide.index, 0, Controller.edgeIndex);
  2286. });
  2287. }
  2288. /**
  2289. * Verify if the focus option is available or not.
  2290. *
  2291. * @return {boolean} - True if a slider has the focus option.
  2292. */
  2293. function hasFocus() {
  2294. return options.focus !== false;
  2295. }
  2296. /**
  2297. * Return the next or previous page index computed by the page number and current index.
  2298. *
  2299. * @param {number} number - Specify the page number.
  2300. * @param {number} index - Current index.
  2301. * @param {boolean} prev - Prev or next.
  2302. *
  2303. * @return {number} - Slide index.
  2304. */
  2305. function parsePage(number, index, prev) {
  2306. if (number > -1) {
  2307. return Controller.toIndex(number);
  2308. }
  2309. var perMove = options.perMove;
  2310. var sign = prev ? -1 : 1;
  2311. if (perMove) {
  2312. return index + perMove * sign;
  2313. }
  2314. return Controller.toIndex(Controller.toPage(index) + sign);
  2315. }
  2316. return Controller;
  2317. };
  2318. /**
  2319. * The component for moving list in the track.
  2320. *
  2321. * @author Naotoshi Fujita
  2322. * @copyright Naotoshi Fujita. All rights reserved.
  2323. */
  2324. var abs$1 = Math.abs;
  2325. /**
  2326. * The component for moving list in the track.
  2327. *
  2328. * @param {Splide} Splide - A Splide instance.
  2329. * @param {Object} Components - An object containing components.
  2330. *
  2331. * @return {Object} - The component object.
  2332. */
  2333. var Track = function Track(Splide, Components) {
  2334. /**
  2335. * Hold the Layout component.
  2336. *
  2337. * @type {Object}
  2338. */
  2339. var Layout;
  2340. /**
  2341. * Hold the Layout component.
  2342. *
  2343. * @type {Object}
  2344. */
  2345. var Elements;
  2346. /**
  2347. * Store the list element.
  2348. *
  2349. * @type {Element}
  2350. */
  2351. var list;
  2352. /**
  2353. * Whether the current direction is vertical or not.
  2354. *
  2355. * @type {boolean}
  2356. */
  2357. var isVertical = Splide.options.direction === TTB;
  2358. /**
  2359. * Whether the slider type is FADE or not.
  2360. *
  2361. * @type {boolean}
  2362. */
  2363. var isFade = Splide.is(FADE);
  2364. /**
  2365. * Whether the slider direction is RTL or not.
  2366. *
  2367. * @type {boolean}
  2368. */
  2369. var isRTL = Splide.options.direction === RTL;
  2370. /**
  2371. * This will be true while transitioning from the last index to the first one.
  2372. *
  2373. * @type {boolean}
  2374. */
  2375. var isLoopPending = false;
  2376. /**
  2377. * Sign for the direction. Only RTL mode uses the positive sign.
  2378. *
  2379. * @type {number}
  2380. */
  2381. var sign = isRTL ? 1 : -1;
  2382. /**
  2383. * Track component object.
  2384. *
  2385. * @type {Object}
  2386. */
  2387. var Track = {
  2388. /**
  2389. * Make public the sign defined locally.
  2390. *
  2391. * @type {number}
  2392. */
  2393. sign: sign,
  2394. /**
  2395. * Called when the component is mounted.
  2396. */
  2397. mount: function mount() {
  2398. Elements = Components.Elements;
  2399. Layout = Components.Layout;
  2400. list = Elements.list;
  2401. },
  2402. /**
  2403. * Called after the component is mounted.
  2404. * The resize event must be registered after the Layout's one is done.
  2405. */
  2406. mounted: function mounted() {
  2407. var _this6 = this;
  2408. if (!isFade) {
  2409. this.jump(0);
  2410. Splide.on('mounted resize updated', function () {
  2411. _this6.jump(Splide.index);
  2412. });
  2413. }
  2414. },
  2415. /**
  2416. * Go to the given destination index.
  2417. * After arriving there, the track is jump to the new index without animation, mainly for loop mode.
  2418. *
  2419. * @param {number} destIndex - A destination index.
  2420. * This can be negative or greater than slides length for reaching clones.
  2421. * @param {number} newIndex - An actual new index. They are always same in Slide and Rewind mode.
  2422. * @param {boolean} silently - If true, suppress emitting events.
  2423. */
  2424. go: function go(destIndex, newIndex, silently) {
  2425. var newPosition = getTrimmedPosition(destIndex);
  2426. var prevIndex = Splide.index; // Prevent any actions while transitioning from the last index to the first one for jump.
  2427. if (Splide.State.is(MOVING) && isLoopPending) {
  2428. return;
  2429. }
  2430. isLoopPending = destIndex !== newIndex;
  2431. if (!silently) {
  2432. Splide.emit('move', newIndex, prevIndex, destIndex);
  2433. }
  2434. if (Math.abs(newPosition - this.position) >= 1 || isFade) {
  2435. Components.Transition.start(destIndex, newIndex, prevIndex, this.toCoord(newPosition), function () {
  2436. onTransitionEnd(destIndex, newIndex, prevIndex, silently);
  2437. });
  2438. } else {
  2439. if (destIndex !== prevIndex && Splide.options.trimSpace === 'move') {
  2440. Components.Controller.go(destIndex + destIndex - prevIndex, silently);
  2441. } else {
  2442. onTransitionEnd(destIndex, newIndex, prevIndex, silently);
  2443. }
  2444. }
  2445. },
  2446. /**
  2447. * Move the track to the specified index.
  2448. *
  2449. * @param {number} index - A destination index where the track jumps.
  2450. */
  2451. jump: function jump(index) {
  2452. this.translate(getTrimmedPosition(index));
  2453. },
  2454. /**
  2455. * Set the list position by CSS translate property.
  2456. *
  2457. * @param {number} position - A new position value.
  2458. */
  2459. translate: function translate(position) {
  2460. applyStyle(list, {
  2461. transform: "translate" + (isVertical ? 'Y' : 'X') + "(" + position + "px)"
  2462. });
  2463. },
  2464. /**
  2465. * Cancel the transition and set the list position.
  2466. * Also, loop the slider if necessary.
  2467. */
  2468. cancel: function cancel() {
  2469. if (Splide.is(LOOP)) {
  2470. this.shift();
  2471. } else {
  2472. // Ensure the current position.
  2473. this.translate(this.position);
  2474. }
  2475. applyStyle(list, {
  2476. transition: ''
  2477. });
  2478. },
  2479. /**
  2480. * Shift the slider if it exceeds borders on the edge.
  2481. */
  2482. shift: function shift() {
  2483. var position = abs$1(this.position);
  2484. var left = abs$1(this.toPosition(0));
  2485. var right = abs$1(this.toPosition(Splide.length));
  2486. var innerSize = right - left;
  2487. if (position < left) {
  2488. position += innerSize;
  2489. } else if (position > right) {
  2490. position -= innerSize;
  2491. }
  2492. this.translate(sign * position);
  2493. },
  2494. /**
  2495. * Trim redundant spaces on the left or right edge if necessary.
  2496. *
  2497. * @param {number} position - Position value to be trimmed.
  2498. *
  2499. * @return {number} - Trimmed position.
  2500. */
  2501. trim: function trim(position) {
  2502. if (!Splide.options.trimSpace || Splide.is(LOOP)) {
  2503. return position;
  2504. }
  2505. var edge = sign * (Layout.totalSize() - Layout.size - Layout.gap);
  2506. return between(position, edge, 0);
  2507. },
  2508. /**
  2509. * Calculate the closest slide index from the given position.
  2510. *
  2511. * @param {number} position - A position converted to an slide index.
  2512. *
  2513. * @return {number} - The closest slide index.
  2514. */
  2515. toIndex: function toIndex(position) {
  2516. var _this7 = this;
  2517. var index = 0;
  2518. var minDistance = Infinity;
  2519. Elements.getSlides(true).forEach(function (Slide) {
  2520. var slideIndex = Slide.index;
  2521. var distance = abs$1(_this7.toPosition(slideIndex) - position);
  2522. if (distance < minDistance) {
  2523. minDistance = distance;
  2524. index = slideIndex;
  2525. }
  2526. });
  2527. return index;
  2528. },
  2529. /**
  2530. * Return coordinates object by the given position.
  2531. *
  2532. * @param {number} position - A position value.
  2533. *
  2534. * @return {Object} - A coordinates object.
  2535. */
  2536. toCoord: function toCoord(position) {
  2537. return {
  2538. x: isVertical ? 0 : position,
  2539. y: isVertical ? position : 0
  2540. };
  2541. },
  2542. /**
  2543. * Calculate the track position by a slide index.
  2544. *
  2545. * @param {number} index - Slide index.
  2546. *
  2547. * @return {Object} - Calculated position.
  2548. */
  2549. toPosition: function toPosition(index) {
  2550. var position = Layout.totalSize(index) - Layout.slideSize(index) - Layout.gap;
  2551. return sign * (position + this.offset(index));
  2552. },
  2553. /**
  2554. * Return the current offset value, considering direction.
  2555. *
  2556. * @return {number} - Offset amount.
  2557. */
  2558. offset: function offset(index) {
  2559. var focus = Splide.options.focus;
  2560. var slideSize = Layout.slideSize(index);
  2561. if (focus === 'center') {
  2562. return -(Layout.size - slideSize) / 2;
  2563. }
  2564. return -(parseInt(focus) || 0) * (slideSize + Layout.gap);
  2565. },
  2566. /**
  2567. * Return the current position.
  2568. * This returns the correct position even while transitioning by CSS.
  2569. *
  2570. * @return {number} - Current position.
  2571. */
  2572. get position() {
  2573. var prop = isVertical ? 'top' : isRTL ? 'right' : 'left';
  2574. return getRect(list)[prop] - (getRect(Elements.track)[prop] - Layout.padding[prop] * sign);
  2575. }
  2576. };
  2577. /**
  2578. * Called whenever slides arrive at a destination.
  2579. *
  2580. * @param {number} destIndex - A destination index.
  2581. * @param {number} newIndex - A new index.
  2582. * @param {number} prevIndex - A previous index.
  2583. * @param {boolean} silently - If true, suppress emitting events.
  2584. */
  2585. function onTransitionEnd(destIndex, newIndex, prevIndex, silently) {
  2586. applyStyle(list, {
  2587. transition: ''
  2588. });
  2589. isLoopPending = false;
  2590. if (!isFade) {
  2591. Track.jump(newIndex);
  2592. }
  2593. if (!silently) {
  2594. Splide.emit('moved', newIndex, prevIndex, destIndex);
  2595. }
  2596. }
  2597. /**
  2598. * Convert index to the trimmed position.
  2599. *
  2600. * @return {number} - Trimmed position.
  2601. */
  2602. function getTrimmedPosition(index) {
  2603. return Track.trim(Track.toPosition(index));
  2604. }
  2605. return Track;
  2606. };
  2607. /**
  2608. * The component for cloning some slides for "loop" mode of the track.
  2609. *
  2610. * @author Naotoshi Fujita
  2611. * @copyright Naotoshi Fujita. All rights reserved.
  2612. */
  2613. /**
  2614. * The component for cloning some slides for "loop" mode of the track.
  2615. *
  2616. * @param {Splide} Splide - A Splide instance.
  2617. * @param {Object} Components - An object containing components.
  2618. *
  2619. * @return {Object} - The component object.
  2620. */
  2621. var Clones = function Clones(Splide, Components) {
  2622. /**
  2623. * Store information of all clones.
  2624. *
  2625. * @type {Array}
  2626. */
  2627. var clones = [];
  2628. /**
  2629. * Store the current clone count on one side.
  2630. *
  2631. * @type {number}
  2632. */
  2633. var cloneCount = 0;
  2634. /**
  2635. * Keep Elements component.
  2636. *
  2637. * @type {Object}
  2638. */
  2639. var Elements = Components.Elements;
  2640. /**
  2641. * Clones component object.
  2642. *
  2643. * @type {Object}
  2644. */
  2645. var Clones = {
  2646. /**
  2647. * Called when the component is mounted.
  2648. */
  2649. mount: function mount() {
  2650. var _this8 = this;
  2651. if (Splide.is(LOOP)) {
  2652. init();
  2653. Splide.on('refresh:before', function () {
  2654. _this8.destroy();
  2655. }).on('refresh', init).on('resize', function () {
  2656. if (cloneCount !== getCloneCount()) {
  2657. // Destroy before refresh not to collect clones by the Elements component.
  2658. _this8.destroy();
  2659. Splide.refresh();
  2660. }
  2661. });
  2662. }
  2663. },
  2664. /**
  2665. * Destroy.
  2666. */
  2667. destroy: function destroy() {
  2668. _remove(clones);
  2669. clones = [];
  2670. },
  2671. /**
  2672. * Return all clones.
  2673. *
  2674. * @return {Element[]} - Cloned elements.
  2675. */
  2676. get clones() {
  2677. return clones;
  2678. },
  2679. /**
  2680. * Return clone length.
  2681. *
  2682. * @return {number} - A length of clones.
  2683. */
  2684. get length() {
  2685. return clones.length;
  2686. }
  2687. };
  2688. /**
  2689. * Initialization.
  2690. */
  2691. function init() {
  2692. Clones.destroy();
  2693. cloneCount = getCloneCount();
  2694. generateClones(cloneCount);
  2695. }
  2696. /**
  2697. * Generate and append/prepend clones.
  2698. *
  2699. * @param {number} count - The half number of clones.
  2700. */
  2701. function generateClones(count) {
  2702. var length = Elements.length,
  2703. register = Elements.register;
  2704. if (length) {
  2705. var slides = Elements.slides;
  2706. while (slides.length < count) {
  2707. slides = slides.concat(slides);
  2708. } // Clones after the last element.
  2709. slides.slice(0, count).forEach(function (elm, index) {
  2710. var clone = cloneDeeply(elm);
  2711. append(Elements.list, clone);
  2712. clones.push(clone);
  2713. register(clone, index + length, index % length);
  2714. }); // Clones before the first element.
  2715. slides.slice(-count).forEach(function (elm, index) {
  2716. var clone = cloneDeeply(elm);
  2717. before(clone, slides[0]);
  2718. clones.push(clone);
  2719. register(clone, index - count, (length + index - count % length) % length);
  2720. });
  2721. }
  2722. }
  2723. /**
  2724. * Return half count of clones to be generated.
  2725. * Clone count is determined by:
  2726. * - "clones" value in the options.
  2727. * - Number of slides that can be placed in a view in "fixed" mode.
  2728. * - Max pages a flick action can move.
  2729. * - Whether the slide length is enough for perPage.
  2730. *
  2731. * @return {number} - Count for clones.
  2732. */
  2733. function getCloneCount() {
  2734. var options = Splide.options;
  2735. if (options.clones) {
  2736. return options.clones;
  2737. } // Use the slide length in autoWidth mode because the number cannot be calculated.
  2738. var baseCount = options.autoWidth || options.autoHeight ? Elements.length : options.perPage;
  2739. var dimension = options.direction === TTB ? 'Height' : 'Width';
  2740. var fixedSize = toPixel(Splide.root, options["fixed" + dimension]);
  2741. if (fixedSize) {
  2742. // Roughly calculate the count. This needs not to be strict.
  2743. baseCount = Math.ceil(Elements.track["client" + dimension] / fixedSize);
  2744. }
  2745. return baseCount * (options.drag ? options.flickMaxPages + 1 : 1);
  2746. }
  2747. /**
  2748. * Clone deeply the given element.
  2749. *
  2750. * @param {Element} elm - An element being duplicated.
  2751. *
  2752. * @return {Node} - A cloned node(element).
  2753. */
  2754. function cloneDeeply(elm) {
  2755. var clone = elm.cloneNode(true);
  2756. addClass(clone, Splide.classes.clone); // ID should not be duplicated.
  2757. removeAttribute(clone, 'id');
  2758. return clone;
  2759. }
  2760. return Clones;
  2761. };
  2762. /**
  2763. * The resolver component for horizontal layout.
  2764. *
  2765. * @author Naotoshi Fujita
  2766. * @copyright Naotoshi Fujita. All rights reserved.
  2767. */
  2768. /**
  2769. * The resolver component for horizontal layout.
  2770. *
  2771. * @param {Splide} Splide - A Splide instance.
  2772. * @param {Object} Components - An object containing components.
  2773. *
  2774. * @return {Object} - The resolver object.
  2775. */
  2776. var Horizontal = function Horizontal(Splide, Components) {
  2777. /**
  2778. * Keep the Elements component.
  2779. *
  2780. * @type {string}
  2781. */
  2782. var Elements = Components.Elements;
  2783. /**
  2784. * Keep the root element.
  2785. *
  2786. * @type {Element}
  2787. */
  2788. var root = Splide.root;
  2789. /**
  2790. * Keep the track element.
  2791. *
  2792. * @type {Element}
  2793. */
  2794. var track;
  2795. /**
  2796. * Keep the latest options.
  2797. *
  2798. * @type {Element}
  2799. */
  2800. var options = Splide.options;
  2801. return {
  2802. /**
  2803. * Margin property name.
  2804. *
  2805. * @type {string}
  2806. */
  2807. margin: 'margin' + (options.direction === RTL ? 'Left' : 'Right'),
  2808. /**
  2809. * Always 0 because the height will be determined by inner contents.
  2810. *
  2811. * @type {number}
  2812. */
  2813. height: 0,
  2814. /**
  2815. * Initialization.
  2816. */
  2817. init: function init() {
  2818. this.resize();
  2819. },
  2820. /**
  2821. * Resize gap and padding.
  2822. * This must be called on init.
  2823. */
  2824. resize: function resize() {
  2825. options = Splide.options;
  2826. track = Elements.track;
  2827. this.gap = toPixel(root, options.gap);
  2828. var padding = options.padding;
  2829. var left = toPixel(root, padding.left || padding);
  2830. var right = toPixel(root, padding.right || padding);
  2831. this.padding = {
  2832. left: left,
  2833. right: right
  2834. };
  2835. applyStyle(track, {
  2836. paddingLeft: unit(left),
  2837. paddingRight: unit(right)
  2838. });
  2839. },
  2840. /**
  2841. * Return total width from the left of the list to the right of the slide specified by the provided index.
  2842. *
  2843. * @param {number} index - Optional. A slide index. If undefined, total width of the slider will be returned.
  2844. *
  2845. * @return {number} - Total width to the right side of the specified slide, or 0 for an invalid index.
  2846. */
  2847. totalWidth: function totalWidth(index) {
  2848. if (index === void 0) {
  2849. index = Splide.length - 1;
  2850. }
  2851. var Slide = Elements.getSlide(index);
  2852. var width = 0;
  2853. if (Slide) {
  2854. var slideRect = getRect(Slide.slide);
  2855. var listRect = getRect(Elements.list);
  2856. if (options.direction === RTL) {
  2857. width = listRect.right - slideRect.left;
  2858. } else {
  2859. width = slideRect.right - listRect.left;
  2860. }
  2861. width += this.gap;
  2862. }
  2863. return width;
  2864. },
  2865. /**
  2866. * Return the slide width in px.
  2867. *
  2868. * @param {number} index - Slide index.
  2869. *
  2870. * @return {number} - The slide width.
  2871. */
  2872. slideWidth: function slideWidth(index) {
  2873. if (options.autoWidth) {
  2874. var _Slide = Elements.getSlide(index);
  2875. return _Slide ? _Slide.slide.offsetWidth : 0;
  2876. }
  2877. var width = options.fixedWidth || (this.width + this.gap) / options.perPage - this.gap;
  2878. return toPixel(root, width);
  2879. },
  2880. /**
  2881. * Return the slide height in px.
  2882. *
  2883. * @return {number} - The slide height.
  2884. */
  2885. slideHeight: function slideHeight() {
  2886. var height = options.height || options.fixedHeight || this.width * options.heightRatio;
  2887. return toPixel(root, height);
  2888. },
  2889. /**
  2890. * Return slider width without padding.
  2891. *
  2892. * @return {number} - Current slider width.
  2893. */
  2894. get width() {
  2895. return track.clientWidth - this.padding.left - this.padding.right;
  2896. }
  2897. };
  2898. };
  2899. /**
  2900. * The resolver component for vertical layout.
  2901. *
  2902. * @author Naotoshi Fujita
  2903. * @copyright Naotoshi Fujita. All rights reserved.
  2904. */
  2905. /**
  2906. * The resolver component for vertical layout.
  2907. *
  2908. * @param {Splide} Splide - A Splide instance.
  2909. * @param {Object} Components - An object containing components.
  2910. *
  2911. * @return {Object} - The resolver object.
  2912. */
  2913. var Vertical = function Vertical(Splide, Components) {
  2914. /**
  2915. * Keep the Elements component.
  2916. *
  2917. * @type {string}
  2918. */
  2919. var Elements = Components.Elements;
  2920. /**
  2921. * Keep the root element.
  2922. *
  2923. * @type {Element}
  2924. */
  2925. var root = Splide.root;
  2926. /**
  2927. * Keep the track element.
  2928. *
  2929. * @type {Element}
  2930. */
  2931. var track;
  2932. /**
  2933. * Keep the latest options.
  2934. *
  2935. * @type {Element}
  2936. */
  2937. var options;
  2938. return {
  2939. /**
  2940. * Margin property name.
  2941. *
  2942. * @type {string}
  2943. */
  2944. margin: 'marginBottom',
  2945. /**
  2946. * Initialization.
  2947. */
  2948. init: function init() {
  2949. this.resize();
  2950. },
  2951. /**
  2952. * Resize gap and padding.
  2953. * This must be called on init.
  2954. */
  2955. resize: function resize() {
  2956. options = Splide.options;
  2957. track = Elements.track;
  2958. this.gap = toPixel(root, options.gap);
  2959. var padding = options.padding;
  2960. var top = toPixel(root, padding.top || padding);
  2961. var bottom = toPixel(root, padding.bottom || padding);
  2962. this.padding = {
  2963. top: top,
  2964. bottom: bottom
  2965. };
  2966. applyStyle(track, {
  2967. paddingTop: unit(top),
  2968. paddingBottom: unit(bottom)
  2969. });
  2970. },
  2971. /**
  2972. * Return total height from the top of the list to the bottom of the slide specified by the provided index.
  2973. *
  2974. * @param {number} index - Optional. A slide index. If undefined, total height of the slider will be returned.
  2975. *
  2976. * @return {number} - Total height to the bottom of the specified slide, or 0 for an invalid index.
  2977. */
  2978. totalHeight: function totalHeight(index) {
  2979. if (index === void 0) {
  2980. index = Splide.length - 1;
  2981. }
  2982. var Slide = Elements.getSlide(index);
  2983. if (Slide) {
  2984. return getRect(Slide.slide).bottom - getRect(Elements.list).top + this.gap;
  2985. }
  2986. return 0;
  2987. },
  2988. /**
  2989. * Return the slide width in px.
  2990. *
  2991. * @return {number} - The slide width.
  2992. */
  2993. slideWidth: function slideWidth() {
  2994. return toPixel(root, options.fixedWidth || this.width);
  2995. },
  2996. /**
  2997. * Return the slide height in px.
  2998. *
  2999. * @param {number} index - Slide index.
  3000. *
  3001. * @return {number} - The slide height.
  3002. */
  3003. slideHeight: function slideHeight(index) {
  3004. if (options.autoHeight) {
  3005. var _Slide2 = Elements.getSlide(index);
  3006. return _Slide2 ? _Slide2.slide.offsetHeight : 0;
  3007. }
  3008. var height = options.fixedHeight || (this.height + this.gap) / options.perPage - this.gap;
  3009. return toPixel(root, height);
  3010. },
  3011. /**
  3012. * Return slider width without padding.
  3013. *
  3014. * @return {number} - Current slider width.
  3015. */
  3016. get width() {
  3017. return track.clientWidth;
  3018. },
  3019. /**
  3020. * Return slide height without padding.
  3021. *
  3022. * @return {number} - Slider height.
  3023. */
  3024. get height() {
  3025. var height = options.height || this.width * options.heightRatio;
  3026. exist(height, '"height" or "heightRatio" is missing.');
  3027. return toPixel(root, height) - this.padding.top - this.padding.bottom;
  3028. }
  3029. };
  3030. };
  3031. /**
  3032. * A package of utility functions related with time.
  3033. *
  3034. * @author Naotoshi Fujita
  3035. * @copyright Naotoshi Fujita. All rights reserved.
  3036. */
  3037. /**
  3038. * Simple throttle function that controls how often the given function is executed.
  3039. *
  3040. * @param {function} func - A function to be throttled.
  3041. * @param {number} wait - Time in millisecond for interval of execution.
  3042. *
  3043. * @return {Function} - A debounced function.
  3044. */
  3045. function throttle(func, wait) {
  3046. var timeout; // Declare function by the "function" keyword to prevent "this" from being inherited.
  3047. return function () {
  3048. if (!timeout) {
  3049. timeout = setTimeout(function () {
  3050. func();
  3051. timeout = null;
  3052. }, wait);
  3053. }
  3054. };
  3055. }
  3056. /**
  3057. * Custom setInterval function that provides progress rate as callback.
  3058. *
  3059. * @param {function} callback - A callback function fired every time the interval time passes.
  3060. * @param {number} interval - Interval duration in milliseconds.
  3061. * @param {function} progress - A callback function fired whenever the progress goes.
  3062. *
  3063. * @return {Object} - An object containing play() and pause() functions.
  3064. */
  3065. function createInterval(callback, interval, progress) {
  3066. var _window = window,
  3067. requestAnimationFrame = _window.requestAnimationFrame;
  3068. var start,
  3069. elapse,
  3070. rate,
  3071. _pause = true;
  3072. var step = function step(timestamp) {
  3073. if (!_pause) {
  3074. if (!start) {
  3075. start = timestamp;
  3076. if (rate && rate < 1) {
  3077. start -= rate * interval;
  3078. }
  3079. }
  3080. elapse = timestamp - start;
  3081. rate = elapse / interval;
  3082. if (elapse >= interval) {
  3083. start = 0;
  3084. rate = 1;
  3085. callback();
  3086. }
  3087. if (progress) {
  3088. progress(rate);
  3089. }
  3090. requestAnimationFrame(step);
  3091. }
  3092. };
  3093. return {
  3094. pause: function pause() {
  3095. _pause = true;
  3096. start = 0;
  3097. },
  3098. play: function play(reset) {
  3099. start = 0;
  3100. if (reset) {
  3101. rate = 0;
  3102. }
  3103. if (_pause) {
  3104. _pause = false;
  3105. requestAnimationFrame(step);
  3106. }
  3107. }
  3108. };
  3109. }
  3110. /**
  3111. * The component for handing slide layouts and their sizes.
  3112. *
  3113. * @author Naotoshi Fujita
  3114. * @copyright Naotoshi Fujita. All rights reserved.
  3115. */
  3116. /**
  3117. * The component for handing slide layouts and their sizes.
  3118. *
  3119. * @param {Splide} Splide - A Splide instance.
  3120. * @param {Object} Components - An object containing components.
  3121. *
  3122. * @return {Object} - The component object.
  3123. */
  3124. var Layout = function Layout(Splide, Components) {
  3125. /**
  3126. * Keep the Elements component.
  3127. *
  3128. * @type {string}
  3129. */
  3130. var Elements = Components.Elements;
  3131. /**
  3132. * Whether the slider is vertical or not.
  3133. *
  3134. * @type {boolean}
  3135. */
  3136. var isVertical = Splide.options.direction === TTB;
  3137. /**
  3138. * Layout component object.
  3139. *
  3140. * @type {Object}
  3141. */
  3142. var Layout = assign({
  3143. /**
  3144. * Called when the component is mounted.
  3145. */
  3146. mount: function mount() {
  3147. bind();
  3148. init(); // The word "size" means width for a horizontal slider and height for a vertical slider.
  3149. this.totalSize = isVertical ? this.totalHeight : this.totalWidth;
  3150. this.slideSize = isVertical ? this.slideHeight : this.slideWidth;
  3151. },
  3152. /**
  3153. * Destroy the component.
  3154. */
  3155. destroy: function destroy() {
  3156. removeAttribute([Elements.list, Elements.track], 'style');
  3157. },
  3158. /**
  3159. * Return the slider height on the vertical mode or width on the horizontal mode.
  3160. *
  3161. * @return {number}
  3162. */
  3163. get size() {
  3164. return isVertical ? this.height : this.width;
  3165. }
  3166. }, isVertical ? Vertical(Splide, Components) : Horizontal(Splide, Components));
  3167. /**
  3168. * Init slider styles according to options.
  3169. */
  3170. function init() {
  3171. Layout.init();
  3172. applyStyle(Splide.root, {
  3173. maxWidth: unit(Splide.options.width)
  3174. });
  3175. Elements.each(function (Slide) {
  3176. Slide.slide.style[Layout.margin] = unit(Layout.gap);
  3177. });
  3178. resize();
  3179. }
  3180. /**
  3181. * Listen the resize native event with throttle.
  3182. * Initialize when the component is mounted or options are updated.
  3183. */
  3184. function bind() {
  3185. Splide.on('resize load', throttle(function () {
  3186. Splide.emit('resize');
  3187. }, Splide.options.throttle), window).on('resize', resize).on('updated refresh', init);
  3188. }
  3189. /**
  3190. * Resize the track and slide elements.
  3191. */
  3192. function resize() {
  3193. var options = Splide.options;
  3194. Layout.resize();
  3195. applyStyle(Elements.track, {
  3196. height: unit(Layout.height)
  3197. });
  3198. var slideHeight = options.autoHeight ? null : unit(Layout.slideHeight());
  3199. Elements.each(function (Slide) {
  3200. applyStyle(Slide.container, {
  3201. height: slideHeight
  3202. });
  3203. applyStyle(Slide.slide, {
  3204. width: options.autoWidth ? null : unit(Layout.slideWidth(Slide.index)),
  3205. height: Slide.container ? null : slideHeight
  3206. });
  3207. });
  3208. Splide.emit('resized');
  3209. }
  3210. return Layout;
  3211. };
  3212. /**
  3213. * The component for supporting mouse drag and swipe.
  3214. *
  3215. * @author Naotoshi Fujita
  3216. * @copyright Naotoshi Fujita. All rights reserved.
  3217. */
  3218. var abs = Math.abs;
  3219. /**
  3220. * If the absolute velocity is greater thant this value,
  3221. * a slider always goes to a different slide after drag, not allowed to stay on a current slide.
  3222. */
  3223. var MIN_VELOCITY = 0.1;
  3224. /**
  3225. * Adjust how much the track can be pulled on the first or last page.
  3226. * The larger number this is, the farther the track moves.
  3227. * This should be around 5 - 9.
  3228. *
  3229. * @type {number}
  3230. */
  3231. var FRICTION_REDUCER = 7;
  3232. /**
  3233. * The component supporting mouse drag and swipe.
  3234. *
  3235. * @param {Splide} Splide - A Splide instance.
  3236. * @param {Object} Components - An object containing components.
  3237. *
  3238. * @return {Object} - The component object.
  3239. */
  3240. var Drag = function Drag(Splide, Components) {
  3241. /**
  3242. * Store the Move component.
  3243. *
  3244. * @type {Object}
  3245. */
  3246. var Track = Components.Track;
  3247. /**
  3248. * Store the Controller component.
  3249. *
  3250. * @type {Object}
  3251. */
  3252. var Controller = Components.Controller;
  3253. /**
  3254. * Coordinate of the track on starting drag.
  3255. *
  3256. * @type {Object}
  3257. */
  3258. var startCoord;
  3259. /**
  3260. * Analyzed info on starting drag.
  3261. *
  3262. * @type {Object|null}
  3263. */
  3264. var startInfo;
  3265. /**
  3266. * Analyzed info being updated while dragging/swiping.
  3267. *
  3268. * @type {Object}
  3269. */
  3270. var currentInfo;
  3271. /**
  3272. * Determine whether slides are being dragged or not.
  3273. *
  3274. * @type {boolean}
  3275. */
  3276. var isDragging;
  3277. /**
  3278. * Whether the slider direction is vertical or not.
  3279. *
  3280. * @type {boolean}
  3281. */
  3282. var isVertical = Splide.options.direction === TTB;
  3283. /**
  3284. * Axis for the direction.
  3285. *
  3286. * @type {string}
  3287. */
  3288. var axis = isVertical ? 'y' : 'x';
  3289. /**
  3290. * Drag component object.
  3291. *
  3292. * @type {Object}
  3293. */
  3294. var Drag = {
  3295. /**
  3296. * Whether dragging is disabled or not.
  3297. *
  3298. * @type {boolean}
  3299. */
  3300. disabled: false,
  3301. /**
  3302. * Called when the component is mounted.
  3303. */
  3304. mount: function mount() {
  3305. var _this9 = this;
  3306. var Elements = Components.Elements;
  3307. var track = Elements.track;
  3308. Splide.on('touchstart mousedown', start, track).on('touchmove mousemove', move, track, {
  3309. passive: false
  3310. }).on('touchend touchcancel mouseleave mouseup dragend', end, track).on('mounted refresh', function () {
  3311. // Prevent dragging an image or anchor itself.
  3312. each(Elements.list.querySelectorAll('img, a'), function (elm) {
  3313. Splide.off('dragstart', elm).on('dragstart', function (e) {
  3314. e.preventDefault();
  3315. }, elm, {
  3316. passive: false
  3317. });
  3318. });
  3319. }).on('mounted updated', function () {
  3320. _this9.disabled = !Splide.options.drag;
  3321. });
  3322. }
  3323. };
  3324. /**
  3325. * Called when the track starts to be dragged.
  3326. *
  3327. * @param {TouchEvent|MouseEvent} e - TouchEvent or MouseEvent object.
  3328. */
  3329. function start(e) {
  3330. if (!Drag.disabled && !isDragging) {
  3331. // These prams are used to evaluate whether the slider should start moving.
  3332. init(e);
  3333. }
  3334. }
  3335. /**
  3336. * Initialize parameters.
  3337. *
  3338. * @param {TouchEvent|MouseEvent} e - TouchEvent or MouseEvent object.
  3339. */
  3340. function init(e) {
  3341. startCoord = Track.toCoord(Track.position);
  3342. startInfo = analyze(e, {});
  3343. currentInfo = startInfo;
  3344. }
  3345. /**
  3346. * Called while the track being dragged.
  3347. *
  3348. * @param {TouchEvent|MouseEvent} e - TouchEvent or MouseEvent object.
  3349. */
  3350. function move(e) {
  3351. if (startInfo) {
  3352. currentInfo = analyze(e, startInfo);
  3353. if (isDragging) {
  3354. if (e.cancelable) {
  3355. e.preventDefault();
  3356. }
  3357. if (!Splide.is(FADE)) {
  3358. var position = startCoord[axis] + currentInfo.offset[axis];
  3359. Track.translate(resist(position));
  3360. }
  3361. } else {
  3362. if (shouldMove(currentInfo)) {
  3363. Splide.emit('drag', startInfo);
  3364. isDragging = true;
  3365. Track.cancel(); // These params are actual drag data.
  3366. init(e);
  3367. }
  3368. }
  3369. }
  3370. }
  3371. /**
  3372. * Determine whether to start moving the track or not by drag angle.
  3373. *
  3374. * @param {Object} info - An information object.
  3375. *
  3376. * @return {boolean} - True if the track should be moved or false if not.
  3377. */
  3378. function shouldMove(_ref3) {
  3379. var offset = _ref3.offset;
  3380. if (Splide.State.is(MOVING) && Splide.options.waitForTransition) {
  3381. return false;
  3382. }
  3383. var angle = Math.atan(abs(offset.y) / abs(offset.x)) * 180 / Math.PI;
  3384. if (isVertical) {
  3385. angle = 90 - angle;
  3386. }
  3387. return angle < Splide.options.dragAngleThreshold;
  3388. }
  3389. /**
  3390. * Resist dragging the track on the first/last page because there is no more.
  3391. *
  3392. * @param {number} position - A position being applied to the track.
  3393. *
  3394. * @return {Object} - Adjusted position.
  3395. */
  3396. function resist(position) {
  3397. if (Splide.is(SLIDE)) {
  3398. var sign = Track.sign;
  3399. var _start = sign * Track.trim(Track.toPosition(0));
  3400. var _end = sign * Track.trim(Track.toPosition(Controller.edgeIndex));
  3401. position *= sign;
  3402. if (position < _start) {
  3403. position = _start - FRICTION_REDUCER * Math.log(_start - position);
  3404. } else if (position > _end) {
  3405. position = _end + FRICTION_REDUCER * Math.log(position - _end);
  3406. }
  3407. position *= sign;
  3408. }
  3409. return position;
  3410. }
  3411. /**
  3412. * Called when dragging ends.
  3413. */
  3414. function end() {
  3415. startInfo = null;
  3416. if (isDragging) {
  3417. Splide.emit('dragged', currentInfo);
  3418. go(currentInfo);
  3419. isDragging = false;
  3420. }
  3421. }
  3422. /**
  3423. * Go to the slide determined by the analyzed data.
  3424. *
  3425. * @param {Object} info - An info object.
  3426. */
  3427. function go(info) {
  3428. var velocity = info.velocity[axis];
  3429. var absV = abs(velocity);
  3430. if (absV > 0) {
  3431. var options = Splide.options;
  3432. var index = Splide.index;
  3433. var sign = velocity < 0 ? -1 : 1;
  3434. var destIndex = index;
  3435. if (!Splide.is(FADE)) {
  3436. var destination = Track.position;
  3437. if (absV > options.flickVelocityThreshold && abs(info.offset[axis]) < options.swipeDistanceThreshold) {
  3438. destination += sign * Math.min(absV * options.flickPower, Components.Layout.size * (options.flickMaxPages || 1));
  3439. }
  3440. destIndex = Track.toIndex(destination);
  3441. }
  3442. /*
  3443. * Do not allow the track to go to a previous position if there is enough velocity.
  3444. * Always use the adjacent index for the fade mode.
  3445. */
  3446. if (destIndex === index && absV > MIN_VELOCITY) {
  3447. destIndex = index + sign * Track.sign;
  3448. }
  3449. if (Splide.is(SLIDE)) {
  3450. destIndex = between(destIndex, 0, Controller.edgeIndex);
  3451. }
  3452. Controller.go(destIndex, options.isNavigation);
  3453. }
  3454. }
  3455. /**
  3456. * Analyze the given event object and return important information for handling swipe behavior.
  3457. *
  3458. * @param {Event} e - Touch or Mouse event object.
  3459. * @param {Object} startInfo - Information analyzed on start for calculating difference from the current one.
  3460. *
  3461. * @return {Object} - An object containing analyzed information, such as offset, velocity, etc.
  3462. */
  3463. function analyze(e, startInfo) {
  3464. var timeStamp = e.timeStamp,
  3465. touches = e.touches;
  3466. var _ref4 = touches ? touches[0] : e,
  3467. clientX = _ref4.clientX,
  3468. clientY = _ref4.clientY;
  3469. var _ref5 = startInfo.to || {},
  3470. _ref5$x = _ref5.x,
  3471. fromX = _ref5$x === void 0 ? clientX : _ref5$x,
  3472. _ref5$y = _ref5.y,
  3473. fromY = _ref5$y === void 0 ? clientY : _ref5$y;
  3474. var startTime = startInfo.time || 0;
  3475. var offset = {
  3476. x: clientX - fromX,
  3477. y: clientY - fromY
  3478. };
  3479. var duration = timeStamp - startTime;
  3480. var velocity = {
  3481. x: offset.x / duration,
  3482. y: offset.y / duration
  3483. };
  3484. return {
  3485. to: {
  3486. x: clientX,
  3487. y: clientY
  3488. },
  3489. offset: offset,
  3490. time: timeStamp,
  3491. velocity: velocity
  3492. };
  3493. }
  3494. return Drag;
  3495. };
  3496. /**
  3497. * The component for handling a click event.
  3498. *
  3499. * @author Naotoshi Fujita
  3500. * @copyright Naotoshi Fujita. All rights reserved.
  3501. */
  3502. /**
  3503. * The component for handling a click event.
  3504. * Click should be disabled during drag/swipe.
  3505. *
  3506. * @param {Splide} Splide - A Splide instance.
  3507. * @param {Object} Components - An object containing components.
  3508. *
  3509. * @return {Object} - The component object.
  3510. */
  3511. var Click = function Click(Splide, Components) {
  3512. /**
  3513. * Whether click is disabled or not.
  3514. *
  3515. * @type {boolean}
  3516. */
  3517. var disabled = false;
  3518. /**
  3519. * Click component object.
  3520. *
  3521. * @type {Object}
  3522. */
  3523. var Click = {
  3524. /**
  3525. * Mount only when the drag is activated and the slide type is not "fade".
  3526. *
  3527. * @type {boolean}
  3528. */
  3529. required: Splide.options.drag,
  3530. /**
  3531. * Called when the component is mounted.
  3532. */
  3533. mount: function mount() {
  3534. Splide.on('click', onClick, Components.Elements.track, {
  3535. capture: true
  3536. }).on('drag', function () {
  3537. disabled = true;
  3538. }).on('dragged', function () {
  3539. // Make sure the flag is released after the click event is fired.
  3540. setTimeout(function () {
  3541. disabled = false;
  3542. });
  3543. });
  3544. }
  3545. };
  3546. /**
  3547. * Called when a track element is clicked.
  3548. *
  3549. * @param {Event} e - A click event.
  3550. */
  3551. function onClick(e) {
  3552. if (disabled) {
  3553. e.preventDefault();
  3554. e.stopPropagation();
  3555. e.stopImmediatePropagation();
  3556. }
  3557. }
  3558. return Click;
  3559. };
  3560. /**
  3561. * The component for playing slides automatically.
  3562. *
  3563. * @author Naotoshi Fujita
  3564. * @copyright Naotoshi Fujita. All rights reserved.
  3565. */
  3566. /**
  3567. * Set of pause flags.
  3568. */
  3569. var PAUSE_FLAGS = {
  3570. HOVER: 1,
  3571. FOCUS: 2,
  3572. MANUAL: 3
  3573. };
  3574. /**
  3575. * The component for playing slides automatically.
  3576. *
  3577. * @param {Splide} Splide - A Splide instance.
  3578. * @param {Object} Components - An object containing components.
  3579. * @param {string} name - A component name as a lowercase string.
  3580. *
  3581. * @return {Object} - The component object.
  3582. */
  3583. var Autoplay = function Autoplay(Splide, Components, name) {
  3584. /**
  3585. * Store pause flags.
  3586. *
  3587. * @type {Array}
  3588. */
  3589. var flags = [];
  3590. /**
  3591. * Store an interval object.
  3592. *
  3593. * @type {Object};
  3594. */
  3595. var interval;
  3596. /**
  3597. * Keep the Elements component.
  3598. *
  3599. * @type {string}
  3600. */
  3601. var Elements = Components.Elements;
  3602. /**
  3603. * Autoplay component object.
  3604. *
  3605. * @type {Object}
  3606. */
  3607. var Autoplay = {
  3608. /**
  3609. * Required only when the autoplay option is true.
  3610. *
  3611. * @type {boolean}
  3612. */
  3613. required: Splide.options.autoplay,
  3614. /**
  3615. * Called when the component is mounted.
  3616. * Note that autoplay starts only if there are slides over perPage number.
  3617. */
  3618. mount: function mount() {
  3619. var options = Splide.options;
  3620. if (Elements.slides.length > options.perPage) {
  3621. interval = createInterval(function () {
  3622. Splide.go('>');
  3623. }, options.interval, function (rate) {
  3624. Splide.emit(name + ":playing", rate);
  3625. if (Elements.bar) {
  3626. applyStyle(Elements.bar, {
  3627. width: rate * 100 + "%"
  3628. });
  3629. }
  3630. });
  3631. bind();
  3632. this.play();
  3633. }
  3634. },
  3635. /**
  3636. * Start autoplay.
  3637. *
  3638. * @param {number} flag - A pause flag to be removed.
  3639. */
  3640. play: function play(flag) {
  3641. if (flag === void 0) {
  3642. flag = 0;
  3643. }
  3644. flags = flags.filter(function (f) {
  3645. return f !== flag;
  3646. });
  3647. if (!flags.length) {
  3648. Splide.emit(name + ":play");
  3649. interval.play(Splide.options.resetProgress);
  3650. }
  3651. },
  3652. /**
  3653. * Pause autoplay.
  3654. * Note that Array.includes is not supported by IE.
  3655. *
  3656. * @param {number} flag - A pause flag to be added.
  3657. */
  3658. pause: function pause(flag) {
  3659. if (flag === void 0) {
  3660. flag = 0;
  3661. }
  3662. interval.pause();
  3663. if (flags.indexOf(flag) === -1) {
  3664. flags.push(flag);
  3665. }
  3666. if (flags.length === 1) {
  3667. Splide.emit(name + ":pause");
  3668. }
  3669. }
  3670. };
  3671. /**
  3672. * Listen some events.
  3673. */
  3674. function bind() {
  3675. var options = Splide.options;
  3676. var sibling = Splide.sibling;
  3677. var elms = [Splide.root, sibling ? sibling.root : null];
  3678. if (options.pauseOnHover) {
  3679. switchOn(elms, 'mouseleave', PAUSE_FLAGS.HOVER, true);
  3680. switchOn(elms, 'mouseenter', PAUSE_FLAGS.HOVER, false);
  3681. }
  3682. if (options.pauseOnFocus) {
  3683. switchOn(elms, 'focusout', PAUSE_FLAGS.FOCUS, true);
  3684. switchOn(elms, 'focusin', PAUSE_FLAGS.FOCUS, false);
  3685. }
  3686. if (Elements.play) {
  3687. Splide.on('click', function () {
  3688. // Need to be removed a focus flag at first.
  3689. Autoplay.play(PAUSE_FLAGS.FOCUS);
  3690. Autoplay.play(PAUSE_FLAGS.MANUAL);
  3691. }, Elements.play);
  3692. }
  3693. if (Elements.pause) {
  3694. switchOn([Elements.pause], 'click', PAUSE_FLAGS.MANUAL, false);
  3695. }
  3696. Splide.on('move refresh', function () {
  3697. Autoplay.play();
  3698. }) // Rewind the timer.
  3699. .on('destroy', function () {
  3700. Autoplay.pause();
  3701. });
  3702. }
  3703. /**
  3704. * Play or pause on the given event.
  3705. *
  3706. * @param {Element[]} elms - Elements.
  3707. * @param {string} event - An event name or names.
  3708. * @param {number} flag - A pause flag defined on the top.
  3709. * @param {boolean} play - Determine whether to play or pause.
  3710. */
  3711. function switchOn(elms, event, flag, play) {
  3712. elms.forEach(function (elm) {
  3713. Splide.on(event, function () {
  3714. Autoplay[play ? 'play' : 'pause'](flag);
  3715. }, elm);
  3716. });
  3717. }
  3718. return Autoplay;
  3719. };
  3720. /**
  3721. * The component for change an img element to background image of its wrapper.
  3722. *
  3723. * @author Naotoshi Fujita
  3724. * @copyright Naotoshi Fujita. All rights reserved.
  3725. */
  3726. /**
  3727. * The component for change an img element to background image of its wrapper.
  3728. *
  3729. * @param {Splide} Splide - A Splide instance.
  3730. * @param {Object} Components - An object containing components.
  3731. *
  3732. * @return {Object} - The component object.
  3733. */
  3734. var Cover = function Cover(Splide, Components) {
  3735. /**
  3736. * Hold options.
  3737. *
  3738. * @type {Object}
  3739. */
  3740. var options = Splide.options;
  3741. /**
  3742. * Cover component object.
  3743. *
  3744. * @type {Object}
  3745. */
  3746. var Cover = {
  3747. /**
  3748. * Required only when "cover" option is true.
  3749. *
  3750. * @type {boolean}
  3751. */
  3752. required: options.cover,
  3753. /**
  3754. * Called when the component is mounted.
  3755. */
  3756. mount: function mount() {
  3757. Splide.on('lazyload:loaded', function (img) {
  3758. cover(img, false);
  3759. });
  3760. Splide.on('mounted updated refresh', function () {
  3761. return apply(false);
  3762. });
  3763. },
  3764. /**
  3765. * Destroy.
  3766. */
  3767. destroy: function destroy() {
  3768. apply(true);
  3769. }
  3770. };
  3771. /**
  3772. * Apply "cover" to all slides.
  3773. *
  3774. * @param {boolean} uncover - If true, "cover" will be clear.
  3775. */
  3776. function apply(uncover) {
  3777. Components.Elements.each(function (Slide) {
  3778. var img = child(Slide.slide, 'IMG') || child(Slide.container, 'IMG');
  3779. if (img && img.src) {
  3780. cover(img, uncover);
  3781. }
  3782. });
  3783. }
  3784. /**
  3785. * Set background image of the parent element, using source of the given image element.
  3786. *
  3787. * @param {Element} img - An image element.
  3788. * @param {boolean} uncover - Reset "cover".
  3789. */
  3790. function cover(img, uncover) {
  3791. applyStyle(img.parentElement, {
  3792. background: uncover ? '' : "center/cover no-repeat url(\"" + img.src + "\")"
  3793. });
  3794. applyStyle(img, {
  3795. display: uncover ? '' : 'none'
  3796. });
  3797. }
  3798. return Cover;
  3799. };
  3800. /**
  3801. * Export vector path for an arrow.
  3802. *
  3803. * @author Naotoshi Fujita
  3804. * @copyright Naotoshi Fujita. All rights reserved.
  3805. */
  3806. /**
  3807. * Namespace definition for SVG element.
  3808. *
  3809. * @type {string}
  3810. */
  3811. var XML_NAME_SPACE = 'http://www.w3.org/2000/svg';
  3812. /**
  3813. * The arrow vector path.
  3814. *
  3815. * @type {number}
  3816. */
  3817. 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';
  3818. /**
  3819. * SVG width and height.
  3820. *
  3821. * @type {number}
  3822. */
  3823. var SIZE = 40;
  3824. /**
  3825. * The component for appending prev/next arrows.
  3826. *
  3827. * @author Naotoshi Fujita
  3828. * @copyright Naotoshi Fujita. All rights reserved.
  3829. */
  3830. /**
  3831. * The component for appending prev/next arrows.
  3832. *
  3833. * @param {Splide} Splide - A Splide instance.
  3834. * @param {Object} Components - An object containing components.
  3835. * @param {string} name - A component name as a lowercase string.
  3836. *
  3837. * @return {Object} - The component object.
  3838. */
  3839. var Arrows = function Arrows(Splide, Components, name) {
  3840. /**
  3841. * Previous arrow element.
  3842. *
  3843. * @type {Element|undefined}
  3844. */
  3845. var prev;
  3846. /**
  3847. * Next arrow element.
  3848. *
  3849. * @type {Element|undefined}
  3850. */
  3851. var next;
  3852. /**
  3853. * Store the class list.
  3854. *
  3855. * @type {Object}
  3856. */
  3857. var classes = Splide.classes;
  3858. /**
  3859. * Hold the root element.
  3860. *
  3861. * @type {Element}
  3862. */
  3863. var root = Splide.root;
  3864. /**
  3865. * Whether arrows are created programmatically or not.
  3866. *
  3867. * @type {boolean}
  3868. */
  3869. var created;
  3870. /**
  3871. * Hold the Elements component.
  3872. *
  3873. * @type {Object}
  3874. */
  3875. var Elements = Components.Elements;
  3876. /**
  3877. * Arrows component object.
  3878. *
  3879. * @type {Object}
  3880. */
  3881. var Arrows = {
  3882. /**
  3883. * Required when the arrows option is true.
  3884. *
  3885. * @type {boolean}
  3886. */
  3887. required: Splide.options.arrows,
  3888. /**
  3889. * Called when the component is mounted.
  3890. */
  3891. mount: function mount() {
  3892. // Attempt to get arrows from HTML source.
  3893. prev = Elements.arrows.prev;
  3894. next = Elements.arrows.next; // If arrows were not found in HTML, let's generate them.
  3895. if ((!prev || !next) && Splide.options.arrows) {
  3896. prev = createArrow(true);
  3897. next = createArrow(false);
  3898. created = true;
  3899. appendArrows();
  3900. }
  3901. if (prev && next) {
  3902. bind();
  3903. }
  3904. this.arrows = {
  3905. prev: prev,
  3906. next: next
  3907. };
  3908. },
  3909. /**
  3910. * Called after all components are mounted.
  3911. */
  3912. mounted: function mounted() {
  3913. Splide.emit(name + ":mounted", prev, next);
  3914. },
  3915. /**
  3916. * Destroy.
  3917. */
  3918. destroy: function destroy() {
  3919. removeAttribute([prev, next], 'disabled');
  3920. if (created) {
  3921. _remove(prev.parentElement);
  3922. }
  3923. }
  3924. };
  3925. /**
  3926. * Listen to native and custom events.
  3927. */
  3928. function bind() {
  3929. Splide.on('click', function () {
  3930. Splide.go('<');
  3931. }, prev).on('click', function () {
  3932. Splide.go('>');
  3933. }, next).on('mounted move updated refresh', updateDisabled);
  3934. }
  3935. /**
  3936. * Update a disabled attribute.
  3937. */
  3938. function updateDisabled() {
  3939. var _Components$Controlle = Components.Controller,
  3940. prevIndex = _Components$Controlle.prevIndex,
  3941. nextIndex = _Components$Controlle.nextIndex;
  3942. var isEnough = Splide.length > Splide.options.perPage || Splide.is(LOOP);
  3943. prev.disabled = prevIndex < 0 || !isEnough;
  3944. next.disabled = nextIndex < 0 || !isEnough;
  3945. Splide.emit(name + ":updated", prev, next, prevIndex, nextIndex);
  3946. }
  3947. /**
  3948. * Create a wrapper element and append arrows.
  3949. */
  3950. function appendArrows() {
  3951. var wrapper = create('div', {
  3952. class: classes.arrows
  3953. });
  3954. append(wrapper, prev);
  3955. append(wrapper, next);
  3956. var slider = Elements.slider;
  3957. var parent = Splide.options.arrows === 'slider' && slider ? slider : root;
  3958. before(wrapper, parent.firstElementChild);
  3959. }
  3960. /**
  3961. * Create an arrow element.
  3962. *
  3963. * @param {boolean} prev - Determine to create a prev arrow or next arrow.
  3964. *
  3965. * @return {Element} - A created arrow element.
  3966. */
  3967. function createArrow(prev) {
  3968. var arrow = "<button class=\"" + classes.arrow + " " + (prev ? classes.prev : classes.next) + "\" type=\"button\">" + ("<svg xmlns=\"" + XML_NAME_SPACE + "\"\tviewBox=\"0 0 " + SIZE + " " + SIZE + "\"\twidth=\"" + SIZE + "\"\theight=\"" + SIZE + "\">") + ("<path d=\"" + (Splide.options.arrowPath || PATH) + "\" />");
  3969. return domify(arrow);
  3970. }
  3971. return Arrows;
  3972. };
  3973. /**
  3974. * The component for handling pagination
  3975. *
  3976. * @author Naotoshi Fujita
  3977. * @copyright Naotoshi Fujita. All rights reserved.
  3978. */
  3979. /**
  3980. * The event name for updating some attributes of pagination nodes.
  3981. *
  3982. * @type {string}
  3983. */
  3984. var ATTRIBUTES_UPDATE_EVENT = 'move.page';
  3985. /**
  3986. * The event name for recreating pagination.
  3987. *
  3988. * @type {string}
  3989. */
  3990. var UPDATE_EVENT = 'updated.page refresh.page';
  3991. /**
  3992. * The component for handling pagination
  3993. *
  3994. * @param {Splide} Splide - A Splide instance.
  3995. * @param {Object} Components - An object containing components.
  3996. * @param {string} name - A component name as a lowercase string.
  3997. *
  3998. * @return {Object} - The component object.
  3999. */
  4000. var Pagination = function Pagination(Splide, Components, name) {
  4001. /**
  4002. * Store all data for pagination.
  4003. * - list: A list element.
  4004. * - items: An array that contains objects(li, button, index, page).
  4005. *
  4006. * @type {Object}
  4007. */
  4008. var data = {};
  4009. /**
  4010. * Hold the Elements component.
  4011. *
  4012. * @type {Object}
  4013. */
  4014. var Elements = Components.Elements;
  4015. /**
  4016. * Pagination component object.
  4017. *
  4018. * @type {Object}
  4019. */
  4020. var Pagination = {
  4021. /**
  4022. * Called when the component is mounted.
  4023. */
  4024. mount: function mount() {
  4025. var pagination = Splide.options.pagination;
  4026. if (pagination) {
  4027. data = createPagination();
  4028. var slider = Elements.slider;
  4029. var parent = pagination === 'slider' && slider ? slider : Splide.root;
  4030. append(parent, data.list);
  4031. Splide.on(ATTRIBUTES_UPDATE_EVENT, updateAttributes);
  4032. }
  4033. Splide.off(UPDATE_EVENT).on(UPDATE_EVENT, function () {
  4034. Pagination.destroy();
  4035. if (Splide.options.pagination) {
  4036. Pagination.mount();
  4037. Pagination.mounted();
  4038. }
  4039. });
  4040. },
  4041. /**
  4042. * Called after all components are mounted.
  4043. */
  4044. mounted: function mounted() {
  4045. if (Splide.options.pagination) {
  4046. var index = Splide.index;
  4047. Splide.emit(name + ":mounted", data, this.getItem(index));
  4048. updateAttributes(index, -1);
  4049. }
  4050. },
  4051. /**
  4052. * Destroy the pagination.
  4053. * Be aware that node.remove() is not supported by IE.
  4054. */
  4055. destroy: function destroy() {
  4056. _remove(data.list);
  4057. if (data.items) {
  4058. data.items.forEach(function (item) {
  4059. Splide.off('click', item.button);
  4060. });
  4061. } // Do not remove UPDATE_EVENT to recreate pagination if needed.
  4062. Splide.off(ATTRIBUTES_UPDATE_EVENT);
  4063. data = {};
  4064. },
  4065. /**
  4066. * Return an item by index.
  4067. *
  4068. * @param {number} index - A slide index.
  4069. *
  4070. * @return {Object|undefined} - An item object on success or undefined on failure.
  4071. */
  4072. getItem: function getItem(index) {
  4073. return data.items[Components.Controller.toPage(index)];
  4074. },
  4075. /**
  4076. * Return object containing pagination data.
  4077. *
  4078. * @return {Object} - Pagination data including list and items.
  4079. */
  4080. get data() {
  4081. return data;
  4082. }
  4083. };
  4084. /**
  4085. * Update attributes.
  4086. *
  4087. * @param {number} index - Active index.
  4088. * @param {number} prevIndex - Prev index.
  4089. */
  4090. function updateAttributes(index, prevIndex) {
  4091. var prev = Pagination.getItem(prevIndex);
  4092. var curr = Pagination.getItem(index);
  4093. var active = STATUS_CLASSES.active;
  4094. if (prev) {
  4095. removeClass(prev.button, active);
  4096. }
  4097. if (curr) {
  4098. addClass(curr.button, active);
  4099. }
  4100. Splide.emit(name + ":updated", data, prev, curr);
  4101. }
  4102. /**
  4103. * Create a wrapper and button elements.
  4104. *
  4105. * @return {Object} - An object contains all data.
  4106. */
  4107. function createPagination() {
  4108. var options = Splide.options;
  4109. var classes = Splide.classes;
  4110. var list = create('ul', {
  4111. class: classes.pagination
  4112. });
  4113. var items = Elements.getSlides(false).filter(function (Slide) {
  4114. return options.focus !== false || Slide.index % options.perPage === 0;
  4115. }).map(function (Slide, page) {
  4116. var li = create('li', {});
  4117. var button = create('button', {
  4118. class: classes.page,
  4119. type: 'button'
  4120. });
  4121. append(li, button);
  4122. append(list, li);
  4123. Splide.on('click', function () {
  4124. Splide.go(">" + page);
  4125. }, button);
  4126. return {
  4127. li: li,
  4128. button: button,
  4129. page: page,
  4130. Slides: Elements.getSlidesByPage(page)
  4131. };
  4132. });
  4133. return {
  4134. list: list,
  4135. items: items
  4136. };
  4137. }
  4138. return Pagination;
  4139. };
  4140. /**
  4141. * The component for loading slider images lazily.
  4142. *
  4143. * @author Naotoshi Fujita
  4144. * @copyright Naotoshi Fujita. All rights reserved.
  4145. */
  4146. /**
  4147. * The name for a data attribute of src.
  4148. *
  4149. * @type {string}
  4150. */
  4151. var SRC_DATA_NAME = 'data-splide-lazy';
  4152. /**
  4153. * The name for a data attribute of srcset.
  4154. *
  4155. * @type {string}
  4156. */
  4157. var SRCSET_DATA_NAME = 'data-splide-lazy-srcset';
  4158. /**
  4159. * The component for loading slider images lazily.
  4160. *
  4161. * @param {Splide} Splide - A Splide instance.
  4162. * @param {Object} Components - An object containing components.
  4163. * @param {string} name - A component name as a lowercase string.
  4164. *
  4165. * @return {Object} - The component object.
  4166. */
  4167. var LazyLoad = function LazyLoad(Splide, Components, name) {
  4168. /**
  4169. * Next index for sequential loading.
  4170. *
  4171. * @type {number}
  4172. */
  4173. var nextIndex;
  4174. /**
  4175. * Store objects containing an img element and a Slide object.
  4176. *
  4177. * @type {Object[]}
  4178. */
  4179. var images;
  4180. /**
  4181. * Store the options.
  4182. *
  4183. * @type {Object}
  4184. */
  4185. var options = Splide.options;
  4186. /**
  4187. * Whether to load images sequentially or not.
  4188. *
  4189. * @type {boolean}
  4190. */
  4191. var isSequential = options.lazyLoad === 'sequential';
  4192. /**
  4193. * Lazyload component object.
  4194. *
  4195. * @type {Object}
  4196. */
  4197. var Lazyload = {
  4198. /**
  4199. * Mount only when the lazyload option is provided.
  4200. *
  4201. * @type {boolean}
  4202. */
  4203. required: options.lazyLoad,
  4204. /**
  4205. * Called when the component is mounted.
  4206. */
  4207. mount: function mount() {
  4208. Splide.on('mounted refresh', function () {
  4209. init();
  4210. Components.Elements.each(function (Slide) {
  4211. each(Slide.slide.querySelectorAll("[" + SRC_DATA_NAME + "], [" + SRCSET_DATA_NAME + "]"), function (img) {
  4212. if (!img.src && !img.srcset) {
  4213. images.push({
  4214. img: img,
  4215. Slide: Slide
  4216. });
  4217. applyStyle(img, {
  4218. display: 'none'
  4219. });
  4220. }
  4221. });
  4222. });
  4223. if (isSequential) {
  4224. loadNext();
  4225. }
  4226. });
  4227. if (!isSequential) {
  4228. Splide.on("mounted refresh moved." + name, check);
  4229. }
  4230. },
  4231. /**
  4232. * Destroy.
  4233. */
  4234. destroy: init
  4235. };
  4236. /**
  4237. * Initialize parameters.
  4238. */
  4239. function init() {
  4240. images = [];
  4241. nextIndex = 0;
  4242. }
  4243. /**
  4244. * Check how close each image is from the active slide and
  4245. * determine whether to start loading or not, according to the distance.
  4246. *
  4247. * @param {number} index - Current index.
  4248. */
  4249. function check(index) {
  4250. index = isNaN(index) ? Splide.index : index;
  4251. images = images.filter(function (image) {
  4252. if (image.Slide.isWithin(index, options.perPage * (options.preloadPages + 1))) {
  4253. load(image.img, image.Slide);
  4254. return false;
  4255. }
  4256. return true;
  4257. }); // Unbind if all images are loaded.
  4258. if (!images[0]) {
  4259. Splide.off("moved." + name);
  4260. }
  4261. }
  4262. /**
  4263. * Start loading an image.
  4264. * Creating a clone of the image element since setting src attribute directly to it
  4265. * often occurs 'hitch', blocking some other processes of a browser.
  4266. *
  4267. * @param {Element} img - An image element.
  4268. * @param {Object} Slide - A Slide object.
  4269. */
  4270. function load(img, Slide) {
  4271. addClass(Slide.slide, STATUS_CLASSES.loading);
  4272. var spinner = create('span', {
  4273. class: Splide.classes.spinner
  4274. });
  4275. append(img.parentElement, spinner);
  4276. img.onload = function () {
  4277. loaded(img, spinner, Slide, false);
  4278. };
  4279. img.onerror = function () {
  4280. loaded(img, spinner, Slide, true);
  4281. };
  4282. setAttribute(img, 'srcset', getAttribute(img, SRCSET_DATA_NAME) || '');
  4283. setAttribute(img, 'src', getAttribute(img, SRC_DATA_NAME) || '');
  4284. }
  4285. /**
  4286. * Start loading a next image in images array.
  4287. */
  4288. function loadNext() {
  4289. if (nextIndex < images.length) {
  4290. var image = images[nextIndex];
  4291. load(image.img, image.Slide);
  4292. }
  4293. nextIndex++;
  4294. }
  4295. /**
  4296. * Called just after the image was loaded or loading was aborted by some error.
  4297. *
  4298. * @param {Element} img - An image element.
  4299. * @param {Element} spinner - A spinner element.
  4300. * @param {Object} Slide - A Slide object.
  4301. * @param {boolean} error - True if the image was loaded successfully or false on error.
  4302. */
  4303. function loaded(img, spinner, Slide, error) {
  4304. removeClass(Slide.slide, STATUS_CLASSES.loading);
  4305. if (!error) {
  4306. _remove(spinner);
  4307. applyStyle(img, {
  4308. display: ''
  4309. });
  4310. Splide.emit(name + ":loaded", img).emit('resize');
  4311. }
  4312. if (isSequential) {
  4313. loadNext();
  4314. }
  4315. }
  4316. return Lazyload;
  4317. };
  4318. /**
  4319. * Export aria attribute names.
  4320. *
  4321. * @author Naotoshi Fujita
  4322. * @copyright Naotoshi Fujita. All rights reserved.
  4323. */
  4324. /**
  4325. * Attribute name for aria-current.
  4326. *
  4327. * @type {string}
  4328. */
  4329. var ARIA_CURRENRT = 'aria-current';
  4330. /**
  4331. * Attribute name for aria-control.
  4332. *
  4333. * @type {string}
  4334. */
  4335. var ARIA_CONTROLS = 'aria-controls';
  4336. /**
  4337. * Attribute name for aria-control.
  4338. *
  4339. * @type {string}
  4340. */
  4341. var ARIA_LABEL = 'aria-label';
  4342. /**
  4343. * Attribute name for aria-hidden.
  4344. *
  4345. * @type {string}
  4346. */
  4347. var ARIA_HIDDEN = 'aria-hidden';
  4348. /**
  4349. * Attribute name for tab-index.
  4350. *
  4351. * @type {string}
  4352. */
  4353. var TAB_INDEX = 'tabindex';
  4354. /**
  4355. * The component for controlling slides via keyboard.
  4356. *
  4357. * @author Naotoshi Fujita
  4358. * @copyright Naotoshi Fujita. All rights reserved.
  4359. */
  4360. /**
  4361. * Map a key to a slide control.
  4362. *
  4363. * @type {Object}
  4364. */
  4365. var KEY_MAP = {
  4366. ltr: {
  4367. ArrowLeft: '<',
  4368. ArrowRight: '>',
  4369. // For IE.
  4370. Left: '<',
  4371. Right: '>'
  4372. },
  4373. rtl: {
  4374. ArrowLeft: '>',
  4375. ArrowRight: '<',
  4376. // For IE.
  4377. Left: '>',
  4378. Right: '<'
  4379. },
  4380. ttb: {
  4381. ArrowUp: '<',
  4382. ArrowDown: '>',
  4383. // For IE.
  4384. Up: '<',
  4385. Down: '>'
  4386. }
  4387. };
  4388. /**
  4389. * The component for controlling slides via keyboard.
  4390. *
  4391. * @param {Splide} Splide - A Splide instance.
  4392. *
  4393. * @return {Object} - The component object.
  4394. */
  4395. var Keyboard = function Keyboard(Splide) {
  4396. /**
  4397. * Hold the target element.
  4398. *
  4399. * @type {Element|Document|undefined}
  4400. */
  4401. var target;
  4402. return {
  4403. /**
  4404. * Called when the component is mounted.
  4405. */
  4406. mount: function mount() {
  4407. Splide.on('mounted updated', function () {
  4408. var options = Splide.options;
  4409. var root = Splide.root;
  4410. var map = KEY_MAP[options.direction];
  4411. var keyboard = options.keyboard;
  4412. if (target) {
  4413. Splide.off('keydown', target);
  4414. removeAttribute(root, TAB_INDEX);
  4415. }
  4416. if (keyboard) {
  4417. if (keyboard === 'focused') {
  4418. target = root;
  4419. setAttribute(root, TAB_INDEX, 0);
  4420. } else {
  4421. target = document;
  4422. }
  4423. Splide.on('keydown', function (e) {
  4424. if (map[e.key]) {
  4425. Splide.go(map[e.key]);
  4426. }
  4427. }, target);
  4428. }
  4429. });
  4430. }
  4431. };
  4432. };
  4433. /**
  4434. * The component for enhancing accessibility.
  4435. *
  4436. * @author Naotoshi Fujita
  4437. * @copyright Naotoshi Fujita. All rights reserved.
  4438. */
  4439. /**
  4440. * The component for enhancing accessibility.
  4441. *
  4442. * @param {Splide} Splide - A Splide instance.
  4443. * @param {Object} Components - An object containing components.
  4444. *
  4445. * @return {Object} - The component object.
  4446. */
  4447. var A11y = function A11y(Splide, Components) {
  4448. /**
  4449. * Hold a i18n object.
  4450. *
  4451. * @type {Object}
  4452. */
  4453. var i18n = Splide.i18n;
  4454. /**
  4455. * Hold the Elements component.
  4456. *
  4457. * @type {Object}
  4458. */
  4459. var Elements = Components.Elements;
  4460. /**
  4461. * All attributes related with A11y.
  4462. *
  4463. * @type {string[]}
  4464. */
  4465. var allAttributes = [ARIA_HIDDEN, TAB_INDEX, ARIA_CONTROLS, ARIA_LABEL, ARIA_CURRENRT, 'role'];
  4466. /**
  4467. * A11y component object.
  4468. *
  4469. * @type {Object}
  4470. */
  4471. var A11y = {
  4472. /**
  4473. * Required only when the accessibility option is true.
  4474. *
  4475. * @type {boolean}
  4476. */
  4477. required: Splide.options.accessibility,
  4478. /**
  4479. * Called when the component is mounted.
  4480. */
  4481. mount: function mount() {
  4482. Splide.on('visible', function (Slide) {
  4483. updateSlide(Slide.slide, true);
  4484. }).on('hidden', function (Slide) {
  4485. updateSlide(Slide.slide, false);
  4486. }).on('arrows:mounted', initArrows).on('arrows:updated', updateArrows).on('pagination:mounted', initPagination).on('pagination:updated', updatePagination).on('refresh', function () {
  4487. removeAttribute(Components.Clones.clones, allAttributes);
  4488. });
  4489. if (Splide.options.isNavigation) {
  4490. Splide.on('navigation:mounted navigation:updated', initNavigation).on('active', function (Slide) {
  4491. updateNavigation(Slide, true);
  4492. }).on('inactive', function (Slide) {
  4493. updateNavigation(Slide, false);
  4494. });
  4495. }
  4496. initAutoplay();
  4497. },
  4498. /**
  4499. * Destroy.
  4500. */
  4501. destroy: function destroy() {
  4502. var Arrows = Components.Arrows;
  4503. var arrows = Arrows ? Arrows.arrows : {};
  4504. removeAttribute(Elements.slides.concat([arrows.prev, arrows.next, Elements.play, Elements.pause]), allAttributes);
  4505. }
  4506. };
  4507. /**
  4508. * Update slide attributes when it gets visible or hidden.
  4509. *
  4510. * @param {Element} slide - A slide element.
  4511. * @param {Boolean} visible - True when the slide gets visible, or false when hidden.
  4512. */
  4513. function updateSlide(slide, visible) {
  4514. setAttribute(slide, ARIA_HIDDEN, !visible);
  4515. if (Splide.options.slideFocus) {
  4516. setAttribute(slide, TAB_INDEX, visible ? 0 : -1);
  4517. }
  4518. }
  4519. /**
  4520. * Initialize arrows if they are available.
  4521. * Append screen reader elements and add aria-controls attribute.
  4522. *
  4523. * @param {Element} prev - Previous arrow element.
  4524. * @param {Element} next - Next arrow element.
  4525. */
  4526. function initArrows(prev, next) {
  4527. var controls = Elements.track.id;
  4528. setAttribute(prev, ARIA_CONTROLS, controls);
  4529. setAttribute(next, ARIA_CONTROLS, controls);
  4530. }
  4531. /**
  4532. * Update arrow attributes.
  4533. *
  4534. * @param {Element} prev - Previous arrow element.
  4535. * @param {Element} next - Next arrow element.
  4536. * @param {number} prevIndex - Previous slide index or -1 when there is no precede slide.
  4537. * @param {number} nextIndex - Next slide index or -1 when there is no next slide.
  4538. */
  4539. function updateArrows(prev, next, prevIndex, nextIndex) {
  4540. var index = Splide.index;
  4541. var prevLabel = prevIndex > -1 && index < prevIndex ? i18n.last : i18n.prev;
  4542. var nextLabel = nextIndex > -1 && index > nextIndex ? i18n.first : i18n.next;
  4543. setAttribute(prev, ARIA_LABEL, prevLabel);
  4544. setAttribute(next, ARIA_LABEL, nextLabel);
  4545. }
  4546. /**
  4547. * Initialize pagination if it's available.
  4548. * Append a screen reader element and add aria-controls/label attribute to each item.
  4549. *
  4550. * @param {Object} data - Data object containing all items.
  4551. * @param {Object} activeItem - An initial active item.
  4552. */
  4553. function initPagination(data, activeItem) {
  4554. if (activeItem) {
  4555. setAttribute(activeItem.button, ARIA_CURRENRT, true);
  4556. }
  4557. data.items.forEach(function (item) {
  4558. var options = Splide.options;
  4559. var text = options.focus === false && options.perPage > 1 ? i18n.pageX : i18n.slideX;
  4560. var label = sprintf(text, item.page + 1);
  4561. var button = item.button;
  4562. var controls = item.Slides.map(function (Slide) {
  4563. return Slide.slide.id;
  4564. });
  4565. setAttribute(button, ARIA_CONTROLS, controls.join(' '));
  4566. setAttribute(button, ARIA_LABEL, label);
  4567. });
  4568. }
  4569. /**
  4570. * Update pagination attributes.
  4571. *
  4572. * @param {Object} data - Data object containing all items.
  4573. * @param {Element} prev - A previous active element.
  4574. * @param {Element} curr - A current active element.
  4575. */
  4576. function updatePagination(data, prev, curr) {
  4577. if (prev) {
  4578. removeAttribute(prev.button, ARIA_CURRENRT);
  4579. }
  4580. if (curr) {
  4581. setAttribute(curr.button, ARIA_CURRENRT, true);
  4582. }
  4583. }
  4584. /**
  4585. * Initialize autoplay buttons.
  4586. */
  4587. function initAutoplay() {
  4588. ['play', 'pause'].forEach(function (name) {
  4589. var elm = Elements[name];
  4590. if (elm) {
  4591. if (!isButton(elm)) {
  4592. setAttribute(elm, 'role', 'button');
  4593. }
  4594. setAttribute(elm, ARIA_CONTROLS, Elements.track.id);
  4595. setAttribute(elm, ARIA_LABEL, i18n[name]);
  4596. }
  4597. });
  4598. }
  4599. /**
  4600. * Initialize navigation slider.
  4601. * Add button role, aria-label, aria-controls to slide elements and append screen reader text to them.
  4602. *
  4603. * @param {Splide} main - A main Splide instance.
  4604. */
  4605. function initNavigation(main) {
  4606. Elements.each(function (Slide) {
  4607. var slide = Slide.slide;
  4608. var realIndex = Slide.realIndex;
  4609. if (!isButton(slide)) {
  4610. setAttribute(slide, 'role', 'button');
  4611. }
  4612. var slideIndex = realIndex > -1 ? realIndex : Slide.index;
  4613. var label = sprintf(i18n.slideX, slideIndex + 1);
  4614. var mainSlide = main.Components.Elements.getSlide(slideIndex);
  4615. setAttribute(slide, ARIA_LABEL, label);
  4616. if (mainSlide) {
  4617. setAttribute(slide, ARIA_CONTROLS, mainSlide.slide.id);
  4618. }
  4619. });
  4620. }
  4621. /**
  4622. * Update navigation attributes.
  4623. *
  4624. * @param {Object} Slide - A target Slide object.
  4625. * @param {boolean} active - True if the slide is active or false if inactive.
  4626. */
  4627. function updateNavigation(_ref6, active) {
  4628. var slide = _ref6.slide;
  4629. if (active) {
  4630. setAttribute(slide, ARIA_CURRENRT, true);
  4631. } else {
  4632. removeAttribute(slide, ARIA_CURRENRT);
  4633. }
  4634. }
  4635. /**
  4636. * Check if the given element is button or not.
  4637. *
  4638. * @param {Element} elm - An element to be checked.
  4639. *
  4640. * @return {boolean} - True if the given element is button.
  4641. */
  4642. function isButton(elm) {
  4643. return elm.tagName === 'BUTTON';
  4644. }
  4645. return A11y;
  4646. };
  4647. /**
  4648. * The component for synchronizing a slider with another.
  4649. *
  4650. * @author Naotoshi Fujita
  4651. * @copyright Naotoshi Fujita. All rights reserved.
  4652. */
  4653. /**
  4654. * The event name for sync.
  4655. *
  4656. * @type {string}
  4657. */
  4658. var SYNC_EVENT = 'move.sync';
  4659. /**
  4660. * The event names for click navigation.
  4661. * @type {string}
  4662. */
  4663. var CLICK_EVENTS = 'mouseup touchend';
  4664. /**
  4665. * The keys for triggering the navigation button.
  4666. *
  4667. * @type {String[]}
  4668. */
  4669. var TRIGGER_KEYS = [' ', 'Enter', 'Spacebar'];
  4670. /**
  4671. * The component for synchronizing a slider with another.
  4672. *
  4673. * @param {Splide} Splide - A Splide instance.
  4674. *
  4675. * @return {Object} - The component object.
  4676. */
  4677. var Sync = function Sync(Splide) {
  4678. /**
  4679. * Keep the sibling Splide instance.
  4680. *
  4681. * @type {Splide}
  4682. */
  4683. var sibling = Splide.sibling;
  4684. /**
  4685. * Whether the sibling slider is navigation or not.
  4686. *
  4687. * @type {Splide|boolean}
  4688. */
  4689. var isNavigation = sibling && sibling.options.isNavigation;
  4690. /**
  4691. * Layout component object.
  4692. *
  4693. * @type {Object}
  4694. */
  4695. var Sync = {
  4696. /**
  4697. * Required only when the sub slider is available.
  4698. *
  4699. * @type {boolean}
  4700. */
  4701. required: !!sibling,
  4702. /**
  4703. * Called when the component is mounted.
  4704. */
  4705. mount: function mount() {
  4706. syncMain();
  4707. syncSibling();
  4708. if (isNavigation) {
  4709. bind();
  4710. Splide.on('refresh', function () {
  4711. setTimeout(function () {
  4712. bind();
  4713. sibling.emit('navigation:updated', Splide);
  4714. });
  4715. });
  4716. }
  4717. },
  4718. /**
  4719. * Called after all components are mounted.
  4720. */
  4721. mounted: function mounted() {
  4722. if (isNavigation) {
  4723. sibling.emit('navigation:mounted', Splide);
  4724. }
  4725. }
  4726. };
  4727. /**
  4728. * Listen the primary slider event to move secondary one.
  4729. * Must unbind a handler at first to avoid infinite loop.
  4730. */
  4731. function syncMain() {
  4732. Splide.on(SYNC_EVENT, function (newIndex, prevIndex, destIndex) {
  4733. sibling.off(SYNC_EVENT).go(sibling.is(LOOP) ? destIndex : newIndex, false);
  4734. syncSibling();
  4735. });
  4736. }
  4737. /**
  4738. * Listen the secondary slider event to move primary one.
  4739. * Must unbind a handler at first to avoid infinite loop.
  4740. */
  4741. function syncSibling() {
  4742. sibling.on(SYNC_EVENT, function (newIndex, prevIndex, destIndex) {
  4743. Splide.off(SYNC_EVENT).go(Splide.is(LOOP) ? destIndex : newIndex, false);
  4744. syncMain();
  4745. });
  4746. }
  4747. /**
  4748. * Listen some events on each slide.
  4749. */
  4750. function bind() {
  4751. sibling.Components.Elements.each(function (_ref7) {
  4752. var slide = _ref7.slide,
  4753. index = _ref7.index;
  4754. /*
  4755. * Listen mouseup and touchend events to handle click.
  4756. */
  4757. Splide.off(CLICK_EVENTS, slide).on(CLICK_EVENTS, function (e) {
  4758. // Ignore a middle or right click.
  4759. if (!e.button || e.button === 0) {
  4760. moveSibling(index);
  4761. }
  4762. }, slide);
  4763. /*
  4764. * Subscribe keyup to handle Enter and Space key.
  4765. * Note that Array.includes is not supported by IE.
  4766. */
  4767. Splide.off('keyup', slide).on('keyup', function (e) {
  4768. if (TRIGGER_KEYS.indexOf(e.key) > -1) {
  4769. e.preventDefault();
  4770. moveSibling(index);
  4771. }
  4772. }, slide, {
  4773. passive: false
  4774. });
  4775. });
  4776. }
  4777. /**
  4778. * Move the sibling to the given index.
  4779. * Need to check "IDLE" status because slides can be moving by Drag component.
  4780. *
  4781. * @param {number} index - Target index.
  4782. */
  4783. function moveSibling(index) {
  4784. if (Splide.State.is(IDLE)) {
  4785. sibling.go(index);
  4786. }
  4787. }
  4788. return Sync;
  4789. };
  4790. /**
  4791. * The component for updating options according to a current window width.
  4792. *
  4793. * @author Naotoshi Fujita
  4794. * @copyright Naotoshi Fujita. All rights reserved.
  4795. */
  4796. /**
  4797. * Interval time for throttle.
  4798. *
  4799. * @type {number}
  4800. */
  4801. var THROTTLE = 50;
  4802. /**
  4803. * The component for updating options according to a current window width.
  4804. *
  4805. * @param {Splide} Splide - A Splide instance.
  4806. *
  4807. * @return {Object} - The component object.
  4808. */
  4809. var Breakpoints = function Breakpoints(Splide) {
  4810. /**
  4811. * Store breakpoints.
  4812. *
  4813. * @type {Object|boolean}
  4814. */
  4815. var breakpoints = Splide.options.breakpoints;
  4816. /**
  4817. * The check function whose frequency of call is reduced.
  4818. *
  4819. * @type {Function}
  4820. */
  4821. var throttledCheck = throttle(check, THROTTLE);
  4822. /**
  4823. * Keep initial options.
  4824. *
  4825. * @type {Object}
  4826. */
  4827. var initialOptions;
  4828. /**
  4829. * An array containing objects of point and MediaQueryList.
  4830. *
  4831. * @type {Object[]}
  4832. */
  4833. var map = [];
  4834. /**
  4835. * Hold the previous breakpoint.
  4836. *
  4837. * @type {number|undefined}
  4838. */
  4839. var prevPoint;
  4840. /**
  4841. * Breakpoints component object.
  4842. *
  4843. * @type {Object}
  4844. */
  4845. var Breakpoints = {
  4846. /**
  4847. * Required only when the breakpoints definition is provided and browser supports matchMedia.
  4848. *
  4849. * @type {boolean}
  4850. */
  4851. required: breakpoints && matchMedia,
  4852. /**
  4853. * Called when the component is mounted.
  4854. */
  4855. mount: function mount() {
  4856. map = Object.keys(breakpoints).sort(function (n, m) {
  4857. return +n - +m;
  4858. }).map(function (point) {
  4859. return {
  4860. point: point,
  4861. mql: matchMedia("(max-width:" + point + "px)")
  4862. };
  4863. });
  4864. /*
  4865. * To keep monitoring resize event after destruction without "completely",
  4866. * use native addEventListener instead of Splide.on.
  4867. */
  4868. this.destroy(true);
  4869. addEventListener('resize', throttledCheck); // Keep initial options to apply them when no breakpoint matches.
  4870. initialOptions = Splide.options;
  4871. check();
  4872. },
  4873. /**
  4874. * Destroy.
  4875. *
  4876. * @param {boolean} completely - Whether to destroy Splide completely.
  4877. */
  4878. destroy: function destroy(completely) {
  4879. if (completely) {
  4880. removeEventListener('resize', throttledCheck);
  4881. }
  4882. }
  4883. };
  4884. /**
  4885. * Check the breakpoint.
  4886. */
  4887. function check() {
  4888. var point = getPoint();
  4889. if (point !== prevPoint) {
  4890. prevPoint = point;
  4891. var _State = Splide.State;
  4892. var options = breakpoints[point] || initialOptions;
  4893. var destroy = options.destroy;
  4894. if (destroy) {
  4895. Splide.options = initialOptions;
  4896. Splide.destroy(destroy === 'completely');
  4897. } else {
  4898. if (_State.is(DESTROYED)) {
  4899. Splide.mount();
  4900. }
  4901. Splide.options = options;
  4902. }
  4903. }
  4904. }
  4905. /**
  4906. * Return the breakpoint matching current window width.
  4907. * Note that Array.prototype.find is not supported by IE.
  4908. *
  4909. * @return {number|string} - A breakpoint as number or string. -1 if no point matches.
  4910. */
  4911. function getPoint() {
  4912. var item = map.filter(function (item) {
  4913. return item.mql.matches;
  4914. })[0];
  4915. return item ? item.point : -1;
  4916. }
  4917. return Breakpoints;
  4918. };
  4919. /**
  4920. * Export components.
  4921. *
  4922. * @author Naotoshi Fujita
  4923. * @copyright Naotoshi Fujita. All rights reserved.
  4924. */
  4925. var COMPLETE = {
  4926. Options: Options,
  4927. Breakpoints: Breakpoints,
  4928. Controller: Controller,
  4929. Elements: Elements,
  4930. Track: Track,
  4931. Clones: Clones,
  4932. Layout: Layout,
  4933. Drag: Drag,
  4934. Click: Click,
  4935. Autoplay: Autoplay,
  4936. Cover: Cover,
  4937. Arrows: Arrows,
  4938. Pagination: Pagination,
  4939. LazyLoad: LazyLoad,
  4940. Keyboard: Keyboard,
  4941. Sync: Sync,
  4942. A11y: A11y
  4943. };
  4944. /**
  4945. * Exports the Splide class with all components.
  4946. *
  4947. * @since 1.0.0
  4948. */
  4949. var Splide = /*#__PURE__*/function (_Splide$) {
  4950. _inheritsLoose(Splide, _Splide$);
  4951. function Splide(root, options) {
  4952. return _Splide$.call(this, root, options, COMPLETE) || this;
  4953. }
  4954. return Splide;
  4955. }(Splide$1);
  4956. export { Splide, Splide as default };