splide.cjs.js 129 KB

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