router.mjs 309 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592
  1. /**
  2. * @license Angular v19.2.4
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import * as i0 from '@angular/core';
  7. import { ɵisPromise as _isPromise, ɵRuntimeError as _RuntimeError, Injectable, ɵisNgModule as _isNgModule, isStandalone, createEnvironmentInjector, InjectionToken, EventEmitter, input, inject, ViewContainerRef, ChangeDetectorRef, Output, Input, Directive, reflectComponentType, ɵisInjectable as _isInjectable, runInInjectionContext, Component, NgModuleFactory, Compiler, NgZone, afterNextRender, EnvironmentInjector, DestroyRef, ɵConsole as _Console, ɵPendingTasksInternal as _PendingTasksInternal, ɵɵsanitizeUrlOrResourceUrl as __sanitizeUrlOrResourceUrl, booleanAttribute, HostListener, HostBinding, Attribute, ContentChildren, Optional, makeEnvironmentProviders, APP_BOOTSTRAP_LISTENER, ENVIRONMENT_INITIALIZER, Injector, ApplicationRef, InjectFlags, APP_INITIALIZER, SkipSelf, NgModule, Version } from '@angular/core';
  8. import { isObservable, from, of, BehaviorSubject, combineLatest, EmptyError, concat, defer, pipe, throwError, EMPTY, ConnectableObservable, Subject, Subscription } from 'rxjs';
  9. import * as i3 from '@angular/common';
  10. import { DOCUMENT, Location, HashLocationStrategy, LocationStrategy, ViewportScroller, LOCATION_INITIALIZED, PathLocationStrategy } from '@angular/common';
  11. import { map, switchMap, take, startWith, filter, mergeMap, first, concatMap, tap, catchError, scan, defaultIfEmpty, last as last$1, takeLast, finalize, refCount, takeUntil, mergeAll } from 'rxjs/operators';
  12. import * as i1 from '@angular/platform-browser';
  13. /**
  14. * The primary routing outlet.
  15. *
  16. * @publicApi
  17. */
  18. const PRIMARY_OUTLET = 'primary';
  19. /**
  20. * A private symbol used to store the value of `Route.title` inside the `Route.data` if it is a
  21. * static string or `Route.resolve` if anything else. This allows us to reuse the existing route
  22. * data/resolvers to support the title feature without new instrumentation in the `Router` pipeline.
  23. */
  24. const RouteTitleKey = /* @__PURE__ */ Symbol('RouteTitle');
  25. class ParamsAsMap {
  26. params;
  27. constructor(params) {
  28. this.params = params || {};
  29. }
  30. has(name) {
  31. return Object.prototype.hasOwnProperty.call(this.params, name);
  32. }
  33. get(name) {
  34. if (this.has(name)) {
  35. const v = this.params[name];
  36. return Array.isArray(v) ? v[0] : v;
  37. }
  38. return null;
  39. }
  40. getAll(name) {
  41. if (this.has(name)) {
  42. const v = this.params[name];
  43. return Array.isArray(v) ? v : [v];
  44. }
  45. return [];
  46. }
  47. get keys() {
  48. return Object.keys(this.params);
  49. }
  50. }
  51. /**
  52. * Converts a `Params` instance to a `ParamMap`.
  53. * @param params The instance to convert.
  54. * @returns The new map instance.
  55. *
  56. * @publicApi
  57. */
  58. function convertToParamMap(params) {
  59. return new ParamsAsMap(params);
  60. }
  61. /**
  62. * Matches the route configuration (`route`) against the actual URL (`segments`).
  63. *
  64. * When no matcher is defined on a `Route`, this is the matcher used by the Router by default.
  65. *
  66. * @param segments The remaining unmatched segments in the current navigation
  67. * @param segmentGroup The current segment group being matched
  68. * @param route The `Route` to match against.
  69. *
  70. * @see {@link UrlMatchResult}
  71. * @see {@link Route}
  72. *
  73. * @returns The resulting match information or `null` if the `route` should not match.
  74. * @publicApi
  75. */
  76. function defaultUrlMatcher(segments, segmentGroup, route) {
  77. const parts = route.path.split('/');
  78. if (parts.length > segments.length) {
  79. // The actual URL is shorter than the config, no match
  80. return null;
  81. }
  82. if (route.pathMatch === 'full' &&
  83. (segmentGroup.hasChildren() || parts.length < segments.length)) {
  84. // The config is longer than the actual URL but we are looking for a full match, return null
  85. return null;
  86. }
  87. const posParams = {};
  88. // Check each config part against the actual URL
  89. for (let index = 0; index < parts.length; index++) {
  90. const part = parts[index];
  91. const segment = segments[index];
  92. const isParameter = part[0] === ':';
  93. if (isParameter) {
  94. posParams[part.substring(1)] = segment;
  95. }
  96. else if (part !== segment.path) {
  97. // The actual URL part does not match the config, no match
  98. return null;
  99. }
  100. }
  101. return { consumed: segments.slice(0, parts.length), posParams };
  102. }
  103. function shallowEqualArrays(a, b) {
  104. if (a.length !== b.length)
  105. return false;
  106. for (let i = 0; i < a.length; ++i) {
  107. if (!shallowEqual(a[i], b[i]))
  108. return false;
  109. }
  110. return true;
  111. }
  112. function shallowEqual(a, b) {
  113. // While `undefined` should never be possible, it would sometimes be the case in IE 11
  114. // and pre-chromium Edge. The check below accounts for this edge case.
  115. const k1 = a ? getDataKeys(a) : undefined;
  116. const k2 = b ? getDataKeys(b) : undefined;
  117. if (!k1 || !k2 || k1.length != k2.length) {
  118. return false;
  119. }
  120. let key;
  121. for (let i = 0; i < k1.length; i++) {
  122. key = k1[i];
  123. if (!equalArraysOrString(a[key], b[key])) {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. /**
  130. * Gets the keys of an object, including `symbol` keys.
  131. */
  132. function getDataKeys(obj) {
  133. return [...Object.keys(obj), ...Object.getOwnPropertySymbols(obj)];
  134. }
  135. /**
  136. * Test equality for arrays of strings or a string.
  137. */
  138. function equalArraysOrString(a, b) {
  139. if (Array.isArray(a) && Array.isArray(b)) {
  140. if (a.length !== b.length)
  141. return false;
  142. const aSorted = [...a].sort();
  143. const bSorted = [...b].sort();
  144. return aSorted.every((val, index) => bSorted[index] === val);
  145. }
  146. else {
  147. return a === b;
  148. }
  149. }
  150. /**
  151. * Return the last element of an array.
  152. */
  153. function last(a) {
  154. return a.length > 0 ? a[a.length - 1] : null;
  155. }
  156. function wrapIntoObservable(value) {
  157. if (isObservable(value)) {
  158. return value;
  159. }
  160. if (_isPromise(value)) {
  161. // Use `Promise.resolve()` to wrap promise-like instances.
  162. // Required ie when a Resolver returns a AngularJS `$q` promise to correctly trigger the
  163. // change detection.
  164. return from(Promise.resolve(value));
  165. }
  166. return of(value);
  167. }
  168. const pathCompareMap = {
  169. 'exact': equalSegmentGroups,
  170. 'subset': containsSegmentGroup,
  171. };
  172. const paramCompareMap = {
  173. 'exact': equalParams,
  174. 'subset': containsParams,
  175. 'ignored': () => true,
  176. };
  177. function containsTree(container, containee, options) {
  178. return (pathCompareMap[options.paths](container.root, containee.root, options.matrixParams) &&
  179. paramCompareMap[options.queryParams](container.queryParams, containee.queryParams) &&
  180. !(options.fragment === 'exact' && container.fragment !== containee.fragment));
  181. }
  182. function equalParams(container, containee) {
  183. // TODO: This does not handle array params correctly.
  184. return shallowEqual(container, containee);
  185. }
  186. function equalSegmentGroups(container, containee, matrixParams) {
  187. if (!equalPath(container.segments, containee.segments))
  188. return false;
  189. if (!matrixParamsMatch(container.segments, containee.segments, matrixParams)) {
  190. return false;
  191. }
  192. if (container.numberOfChildren !== containee.numberOfChildren)
  193. return false;
  194. for (const c in containee.children) {
  195. if (!container.children[c])
  196. return false;
  197. if (!equalSegmentGroups(container.children[c], containee.children[c], matrixParams))
  198. return false;
  199. }
  200. return true;
  201. }
  202. function containsParams(container, containee) {
  203. return (Object.keys(containee).length <= Object.keys(container).length &&
  204. Object.keys(containee).every((key) => equalArraysOrString(container[key], containee[key])));
  205. }
  206. function containsSegmentGroup(container, containee, matrixParams) {
  207. return containsSegmentGroupHelper(container, containee, containee.segments, matrixParams);
  208. }
  209. function containsSegmentGroupHelper(container, containee, containeePaths, matrixParams) {
  210. if (container.segments.length > containeePaths.length) {
  211. const current = container.segments.slice(0, containeePaths.length);
  212. if (!equalPath(current, containeePaths))
  213. return false;
  214. if (containee.hasChildren())
  215. return false;
  216. if (!matrixParamsMatch(current, containeePaths, matrixParams))
  217. return false;
  218. return true;
  219. }
  220. else if (container.segments.length === containeePaths.length) {
  221. if (!equalPath(container.segments, containeePaths))
  222. return false;
  223. if (!matrixParamsMatch(container.segments, containeePaths, matrixParams))
  224. return false;
  225. for (const c in containee.children) {
  226. if (!container.children[c])
  227. return false;
  228. if (!containsSegmentGroup(container.children[c], containee.children[c], matrixParams)) {
  229. return false;
  230. }
  231. }
  232. return true;
  233. }
  234. else {
  235. const current = containeePaths.slice(0, container.segments.length);
  236. const next = containeePaths.slice(container.segments.length);
  237. if (!equalPath(container.segments, current))
  238. return false;
  239. if (!matrixParamsMatch(container.segments, current, matrixParams))
  240. return false;
  241. if (!container.children[PRIMARY_OUTLET])
  242. return false;
  243. return containsSegmentGroupHelper(container.children[PRIMARY_OUTLET], containee, next, matrixParams);
  244. }
  245. }
  246. function matrixParamsMatch(containerPaths, containeePaths, options) {
  247. return containeePaths.every((containeeSegment, i) => {
  248. return paramCompareMap[options](containerPaths[i].parameters, containeeSegment.parameters);
  249. });
  250. }
  251. /**
  252. * @description
  253. *
  254. * Represents the parsed URL.
  255. *
  256. * Since a router state is a tree, and the URL is nothing but a serialized state, the URL is a
  257. * serialized tree.
  258. * UrlTree is a data structure that provides a lot of affordances in dealing with URLs
  259. *
  260. * @usageNotes
  261. * ### Example
  262. *
  263. * ```ts
  264. * @Component({templateUrl:'template.html'})
  265. * class MyComponent {
  266. * constructor(router: Router) {
  267. * const tree: UrlTree =
  268. * router.parseUrl('/team/33/(user/victor//support:help)?debug=true#fragment');
  269. * const f = tree.fragment; // return 'fragment'
  270. * const q = tree.queryParams; // returns {debug: 'true'}
  271. * const g: UrlSegmentGroup = tree.root.children[PRIMARY_OUTLET];
  272. * const s: UrlSegment[] = g.segments; // returns 2 segments 'team' and '33'
  273. * g.children[PRIMARY_OUTLET].segments; // returns 2 segments 'user' and 'victor'
  274. * g.children['support'].segments; // return 1 segment 'help'
  275. * }
  276. * }
  277. * ```
  278. *
  279. * @publicApi
  280. */
  281. class UrlTree {
  282. root;
  283. queryParams;
  284. fragment;
  285. /** @internal */
  286. _queryParamMap;
  287. constructor(
  288. /** The root segment group of the URL tree */
  289. root = new UrlSegmentGroup([], {}),
  290. /** The query params of the URL */
  291. queryParams = {},
  292. /** The fragment of the URL */
  293. fragment = null) {
  294. this.root = root;
  295. this.queryParams = queryParams;
  296. this.fragment = fragment;
  297. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  298. if (root.segments.length > 0) {
  299. throw new _RuntimeError(4015 /* RuntimeErrorCode.INVALID_ROOT_URL_SEGMENT */, 'The root `UrlSegmentGroup` should not contain `segments`. ' +
  300. 'Instead, these segments belong in the `children` so they can be associated with a named outlet.');
  301. }
  302. }
  303. }
  304. get queryParamMap() {
  305. this._queryParamMap ??= convertToParamMap(this.queryParams);
  306. return this._queryParamMap;
  307. }
  308. /** @docsNotRequired */
  309. toString() {
  310. return DEFAULT_SERIALIZER.serialize(this);
  311. }
  312. }
  313. /**
  314. * @description
  315. *
  316. * Represents the parsed URL segment group.
  317. *
  318. * See `UrlTree` for more information.
  319. *
  320. * @publicApi
  321. */
  322. class UrlSegmentGroup {
  323. segments;
  324. children;
  325. /** The parent node in the url tree */
  326. parent = null;
  327. constructor(
  328. /** The URL segments of this group. See `UrlSegment` for more information */
  329. segments,
  330. /** The list of children of this group */
  331. children) {
  332. this.segments = segments;
  333. this.children = children;
  334. Object.values(children).forEach((v) => (v.parent = this));
  335. }
  336. /** Whether the segment has child segments */
  337. hasChildren() {
  338. return this.numberOfChildren > 0;
  339. }
  340. /** Number of child segments */
  341. get numberOfChildren() {
  342. return Object.keys(this.children).length;
  343. }
  344. /** @docsNotRequired */
  345. toString() {
  346. return serializePaths(this);
  347. }
  348. }
  349. /**
  350. * @description
  351. *
  352. * Represents a single URL segment.
  353. *
  354. * A UrlSegment is a part of a URL between the two slashes. It contains a path and the matrix
  355. * parameters associated with the segment.
  356. *
  357. * @usageNotes
  358. * ### Example
  359. *
  360. * ```ts
  361. * @Component({templateUrl:'template.html'})
  362. * class MyComponent {
  363. * constructor(router: Router) {
  364. * const tree: UrlTree = router.parseUrl('/team;id=33');
  365. * const g: UrlSegmentGroup = tree.root.children[PRIMARY_OUTLET];
  366. * const s: UrlSegment[] = g.segments;
  367. * s[0].path; // returns 'team'
  368. * s[0].parameters; // returns {id: 33}
  369. * }
  370. * }
  371. * ```
  372. *
  373. * @publicApi
  374. */
  375. class UrlSegment {
  376. path;
  377. parameters;
  378. /** @internal */
  379. _parameterMap;
  380. constructor(
  381. /** The path part of a URL segment */
  382. path,
  383. /** The matrix parameters associated with a segment */
  384. parameters) {
  385. this.path = path;
  386. this.parameters = parameters;
  387. }
  388. get parameterMap() {
  389. this._parameterMap ??= convertToParamMap(this.parameters);
  390. return this._parameterMap;
  391. }
  392. /** @docsNotRequired */
  393. toString() {
  394. return serializePath(this);
  395. }
  396. }
  397. function equalSegments(as, bs) {
  398. return equalPath(as, bs) && as.every((a, i) => shallowEqual(a.parameters, bs[i].parameters));
  399. }
  400. function equalPath(as, bs) {
  401. if (as.length !== bs.length)
  402. return false;
  403. return as.every((a, i) => a.path === bs[i].path);
  404. }
  405. function mapChildrenIntoArray(segment, fn) {
  406. let res = [];
  407. Object.entries(segment.children).forEach(([childOutlet, child]) => {
  408. if (childOutlet === PRIMARY_OUTLET) {
  409. res = res.concat(fn(child, childOutlet));
  410. }
  411. });
  412. Object.entries(segment.children).forEach(([childOutlet, child]) => {
  413. if (childOutlet !== PRIMARY_OUTLET) {
  414. res = res.concat(fn(child, childOutlet));
  415. }
  416. });
  417. return res;
  418. }
  419. /**
  420. * @description
  421. *
  422. * Serializes and deserializes a URL string into a URL tree.
  423. *
  424. * The url serialization strategy is customizable. You can
  425. * make all URLs case insensitive by providing a custom UrlSerializer.
  426. *
  427. * See `DefaultUrlSerializer` for an example of a URL serializer.
  428. *
  429. * @publicApi
  430. */
  431. class UrlSerializer {
  432. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: UrlSerializer, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  433. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: UrlSerializer, providedIn: 'root', useFactory: () => new DefaultUrlSerializer() });
  434. }
  435. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: UrlSerializer, decorators: [{
  436. type: Injectable,
  437. args: [{ providedIn: 'root', useFactory: () => new DefaultUrlSerializer() }]
  438. }] });
  439. /**
  440. * @description
  441. *
  442. * A default implementation of the `UrlSerializer`.
  443. *
  444. * Example URLs:
  445. *
  446. * ```
  447. * /inbox/33(popup:compose)
  448. * /inbox/33;open=true/messages/44
  449. * ```
  450. *
  451. * DefaultUrlSerializer uses parentheses to serialize secondary segments (e.g., popup:compose), the
  452. * colon syntax to specify the outlet, and the ';parameter=value' syntax (e.g., open=true) to
  453. * specify route specific parameters.
  454. *
  455. * @publicApi
  456. */
  457. class DefaultUrlSerializer {
  458. /** Parses a url into a `UrlTree` */
  459. parse(url) {
  460. const p = new UrlParser(url);
  461. return new UrlTree(p.parseRootSegment(), p.parseQueryParams(), p.parseFragment());
  462. }
  463. /** Converts a `UrlTree` into a url */
  464. serialize(tree) {
  465. const segment = `/${serializeSegment(tree.root, true)}`;
  466. const query = serializeQueryParams(tree.queryParams);
  467. const fragment = typeof tree.fragment === `string` ? `#${encodeUriFragment(tree.fragment)}` : '';
  468. return `${segment}${query}${fragment}`;
  469. }
  470. }
  471. const DEFAULT_SERIALIZER = new DefaultUrlSerializer();
  472. function serializePaths(segment) {
  473. return segment.segments.map((p) => serializePath(p)).join('/');
  474. }
  475. function serializeSegment(segment, root) {
  476. if (!segment.hasChildren()) {
  477. return serializePaths(segment);
  478. }
  479. if (root) {
  480. const primary = segment.children[PRIMARY_OUTLET]
  481. ? serializeSegment(segment.children[PRIMARY_OUTLET], false)
  482. : '';
  483. const children = [];
  484. Object.entries(segment.children).forEach(([k, v]) => {
  485. if (k !== PRIMARY_OUTLET) {
  486. children.push(`${k}:${serializeSegment(v, false)}`);
  487. }
  488. });
  489. return children.length > 0 ? `${primary}(${children.join('//')})` : primary;
  490. }
  491. else {
  492. const children = mapChildrenIntoArray(segment, (v, k) => {
  493. if (k === PRIMARY_OUTLET) {
  494. return [serializeSegment(segment.children[PRIMARY_OUTLET], false)];
  495. }
  496. return [`${k}:${serializeSegment(v, false)}`];
  497. });
  498. // use no parenthesis if the only child is a primary outlet route
  499. if (Object.keys(segment.children).length === 1 && segment.children[PRIMARY_OUTLET] != null) {
  500. return `${serializePaths(segment)}/${children[0]}`;
  501. }
  502. return `${serializePaths(segment)}/(${children.join('//')})`;
  503. }
  504. }
  505. /**
  506. * Encodes a URI string with the default encoding. This function will only ever be called from
  507. * `encodeUriQuery` or `encodeUriSegment` as it's the base set of encodings to be used. We need
  508. * a custom encoding because encodeURIComponent is too aggressive and encodes stuff that doesn't
  509. * have to be encoded per https://url.spec.whatwg.org.
  510. */
  511. function encodeUriString(s) {
  512. return encodeURIComponent(s)
  513. .replace(/%40/g, '@')
  514. .replace(/%3A/gi, ':')
  515. .replace(/%24/g, '$')
  516. .replace(/%2C/gi, ',');
  517. }
  518. /**
  519. * This function should be used to encode both keys and values in a query string key/value. In
  520. * the following URL, you need to call encodeUriQuery on "k" and "v":
  521. *
  522. * http://www.site.org/html;mk=mv?k=v#f
  523. */
  524. function encodeUriQuery(s) {
  525. return encodeUriString(s).replace(/%3B/gi, ';');
  526. }
  527. /**
  528. * This function should be used to encode a URL fragment. In the following URL, you need to call
  529. * encodeUriFragment on "f":
  530. *
  531. * http://www.site.org/html;mk=mv?k=v#f
  532. */
  533. function encodeUriFragment(s) {
  534. return encodeURI(s);
  535. }
  536. /**
  537. * This function should be run on any URI segment as well as the key and value in a key/value
  538. * pair for matrix params. In the following URL, you need to call encodeUriSegment on "html",
  539. * "mk", and "mv":
  540. *
  541. * http://www.site.org/html;mk=mv?k=v#f
  542. */
  543. function encodeUriSegment(s) {
  544. return encodeUriString(s).replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/%26/gi, '&');
  545. }
  546. function decode(s) {
  547. return decodeURIComponent(s);
  548. }
  549. // Query keys/values should have the "+" replaced first, as "+" in a query string is " ".
  550. // decodeURIComponent function will not decode "+" as a space.
  551. function decodeQuery(s) {
  552. return decode(s.replace(/\+/g, '%20'));
  553. }
  554. function serializePath(path) {
  555. return `${encodeUriSegment(path.path)}${serializeMatrixParams(path.parameters)}`;
  556. }
  557. function serializeMatrixParams(params) {
  558. return Object.entries(params)
  559. .map(([key, value]) => `;${encodeUriSegment(key)}=${encodeUriSegment(value)}`)
  560. .join('');
  561. }
  562. function serializeQueryParams(params) {
  563. const strParams = Object.entries(params)
  564. .map(([name, value]) => {
  565. return Array.isArray(value)
  566. ? value.map((v) => `${encodeUriQuery(name)}=${encodeUriQuery(v)}`).join('&')
  567. : `${encodeUriQuery(name)}=${encodeUriQuery(value)}`;
  568. })
  569. .filter((s) => s);
  570. return strParams.length ? `?${strParams.join('&')}` : '';
  571. }
  572. const SEGMENT_RE = /^[^\/()?;#]+/;
  573. function matchSegments(str) {
  574. const match = str.match(SEGMENT_RE);
  575. return match ? match[0] : '';
  576. }
  577. const MATRIX_PARAM_SEGMENT_RE = /^[^\/()?;=#]+/;
  578. function matchMatrixKeySegments(str) {
  579. const match = str.match(MATRIX_PARAM_SEGMENT_RE);
  580. return match ? match[0] : '';
  581. }
  582. const QUERY_PARAM_RE = /^[^=?&#]+/;
  583. // Return the name of the query param at the start of the string or an empty string
  584. function matchQueryParams(str) {
  585. const match = str.match(QUERY_PARAM_RE);
  586. return match ? match[0] : '';
  587. }
  588. const QUERY_PARAM_VALUE_RE = /^[^&#]+/;
  589. // Return the value of the query param at the start of the string or an empty string
  590. function matchUrlQueryParamValue(str) {
  591. const match = str.match(QUERY_PARAM_VALUE_RE);
  592. return match ? match[0] : '';
  593. }
  594. class UrlParser {
  595. url;
  596. remaining;
  597. constructor(url) {
  598. this.url = url;
  599. this.remaining = url;
  600. }
  601. parseRootSegment() {
  602. this.consumeOptional('/');
  603. if (this.remaining === '' || this.peekStartsWith('?') || this.peekStartsWith('#')) {
  604. return new UrlSegmentGroup([], {});
  605. }
  606. // The root segment group never has segments
  607. return new UrlSegmentGroup([], this.parseChildren());
  608. }
  609. parseQueryParams() {
  610. const params = {};
  611. if (this.consumeOptional('?')) {
  612. do {
  613. this.parseQueryParam(params);
  614. } while (this.consumeOptional('&'));
  615. }
  616. return params;
  617. }
  618. parseFragment() {
  619. return this.consumeOptional('#') ? decodeURIComponent(this.remaining) : null;
  620. }
  621. parseChildren() {
  622. if (this.remaining === '') {
  623. return {};
  624. }
  625. this.consumeOptional('/');
  626. const segments = [];
  627. if (!this.peekStartsWith('(')) {
  628. segments.push(this.parseSegment());
  629. }
  630. while (this.peekStartsWith('/') && !this.peekStartsWith('//') && !this.peekStartsWith('/(')) {
  631. this.capture('/');
  632. segments.push(this.parseSegment());
  633. }
  634. let children = {};
  635. if (this.peekStartsWith('/(')) {
  636. this.capture('/');
  637. children = this.parseParens(true);
  638. }
  639. let res = {};
  640. if (this.peekStartsWith('(')) {
  641. res = this.parseParens(false);
  642. }
  643. if (segments.length > 0 || Object.keys(children).length > 0) {
  644. res[PRIMARY_OUTLET] = new UrlSegmentGroup(segments, children);
  645. }
  646. return res;
  647. }
  648. // parse a segment with its matrix parameters
  649. // ie `name;k1=v1;k2`
  650. parseSegment() {
  651. const path = matchSegments(this.remaining);
  652. if (path === '' && this.peekStartsWith(';')) {
  653. throw new _RuntimeError(4009 /* RuntimeErrorCode.EMPTY_PATH_WITH_PARAMS */, (typeof ngDevMode === 'undefined' || ngDevMode) &&
  654. `Empty path url segment cannot have parameters: '${this.remaining}'.`);
  655. }
  656. this.capture(path);
  657. return new UrlSegment(decode(path), this.parseMatrixParams());
  658. }
  659. parseMatrixParams() {
  660. const params = {};
  661. while (this.consumeOptional(';')) {
  662. this.parseParam(params);
  663. }
  664. return params;
  665. }
  666. parseParam(params) {
  667. const key = matchMatrixKeySegments(this.remaining);
  668. if (!key) {
  669. return;
  670. }
  671. this.capture(key);
  672. let value = '';
  673. if (this.consumeOptional('=')) {
  674. const valueMatch = matchSegments(this.remaining);
  675. if (valueMatch) {
  676. value = valueMatch;
  677. this.capture(value);
  678. }
  679. }
  680. params[decode(key)] = decode(value);
  681. }
  682. // Parse a single query parameter `name[=value]`
  683. parseQueryParam(params) {
  684. const key = matchQueryParams(this.remaining);
  685. if (!key) {
  686. return;
  687. }
  688. this.capture(key);
  689. let value = '';
  690. if (this.consumeOptional('=')) {
  691. const valueMatch = matchUrlQueryParamValue(this.remaining);
  692. if (valueMatch) {
  693. value = valueMatch;
  694. this.capture(value);
  695. }
  696. }
  697. const decodedKey = decodeQuery(key);
  698. const decodedVal = decodeQuery(value);
  699. if (params.hasOwnProperty(decodedKey)) {
  700. // Append to existing values
  701. let currentVal = params[decodedKey];
  702. if (!Array.isArray(currentVal)) {
  703. currentVal = [currentVal];
  704. params[decodedKey] = currentVal;
  705. }
  706. currentVal.push(decodedVal);
  707. }
  708. else {
  709. // Create a new value
  710. params[decodedKey] = decodedVal;
  711. }
  712. }
  713. // parse `(a/b//outlet_name:c/d)`
  714. parseParens(allowPrimary) {
  715. const segments = {};
  716. this.capture('(');
  717. while (!this.consumeOptional(')') && this.remaining.length > 0) {
  718. const path = matchSegments(this.remaining);
  719. const next = this.remaining[path.length];
  720. // if is is not one of these characters, then the segment was unescaped
  721. // or the group was not closed
  722. if (next !== '/' && next !== ')' && next !== ';') {
  723. throw new _RuntimeError(4010 /* RuntimeErrorCode.UNPARSABLE_URL */, (typeof ngDevMode === 'undefined' || ngDevMode) && `Cannot parse url '${this.url}'`);
  724. }
  725. let outletName = undefined;
  726. if (path.indexOf(':') > -1) {
  727. outletName = path.slice(0, path.indexOf(':'));
  728. this.capture(outletName);
  729. this.capture(':');
  730. }
  731. else if (allowPrimary) {
  732. outletName = PRIMARY_OUTLET;
  733. }
  734. const children = this.parseChildren();
  735. segments[outletName] =
  736. Object.keys(children).length === 1
  737. ? children[PRIMARY_OUTLET]
  738. : new UrlSegmentGroup([], children);
  739. this.consumeOptional('//');
  740. }
  741. return segments;
  742. }
  743. peekStartsWith(str) {
  744. return this.remaining.startsWith(str);
  745. }
  746. // Consumes the prefix when it is present and returns whether it has been consumed
  747. consumeOptional(str) {
  748. if (this.peekStartsWith(str)) {
  749. this.remaining = this.remaining.substring(str.length);
  750. return true;
  751. }
  752. return false;
  753. }
  754. capture(str) {
  755. if (!this.consumeOptional(str)) {
  756. throw new _RuntimeError(4011 /* RuntimeErrorCode.UNEXPECTED_VALUE_IN_URL */, (typeof ngDevMode === 'undefined' || ngDevMode) && `Expected "${str}".`);
  757. }
  758. }
  759. }
  760. function createRoot(rootCandidate) {
  761. return rootCandidate.segments.length > 0
  762. ? new UrlSegmentGroup([], { [PRIMARY_OUTLET]: rootCandidate })
  763. : rootCandidate;
  764. }
  765. /**
  766. * Recursively
  767. * - merges primary segment children into their parents
  768. * - drops empty children (those which have no segments and no children themselves). This latter
  769. * prevents serializing a group into something like `/a(aux:)`, where `aux` is an empty child
  770. * segment.
  771. * - merges named outlets without a primary segment sibling into the children. This prevents
  772. * serializing a URL like `//(a:a)(b:b) instead of `/(a:a//b:b)` when the aux b route lives on the
  773. * root but the `a` route lives under an empty path primary route.
  774. */
  775. function squashSegmentGroup(segmentGroup) {
  776. const newChildren = {};
  777. for (const [childOutlet, child] of Object.entries(segmentGroup.children)) {
  778. const childCandidate = squashSegmentGroup(child);
  779. // moves named children in an empty path primary child into this group
  780. if (childOutlet === PRIMARY_OUTLET &&
  781. childCandidate.segments.length === 0 &&
  782. childCandidate.hasChildren()) {
  783. for (const [grandChildOutlet, grandChild] of Object.entries(childCandidate.children)) {
  784. newChildren[grandChildOutlet] = grandChild;
  785. }
  786. } // don't add empty children
  787. else if (childCandidate.segments.length > 0 || childCandidate.hasChildren()) {
  788. newChildren[childOutlet] = childCandidate;
  789. }
  790. }
  791. const s = new UrlSegmentGroup(segmentGroup.segments, newChildren);
  792. return mergeTrivialChildren(s);
  793. }
  794. /**
  795. * When possible, merges the primary outlet child into the parent `UrlSegmentGroup`.
  796. *
  797. * When a segment group has only one child which is a primary outlet, merges that child into the
  798. * parent. That is, the child segment group's segments are merged into the `s` and the child's
  799. * children become the children of `s`. Think of this like a 'squash', merging the child segment
  800. * group into the parent.
  801. */
  802. function mergeTrivialChildren(s) {
  803. if (s.numberOfChildren === 1 && s.children[PRIMARY_OUTLET]) {
  804. const c = s.children[PRIMARY_OUTLET];
  805. return new UrlSegmentGroup(s.segments.concat(c.segments), c.children);
  806. }
  807. return s;
  808. }
  809. function isUrlTree(v) {
  810. return v instanceof UrlTree;
  811. }
  812. /**
  813. * Creates a `UrlTree` relative to an `ActivatedRouteSnapshot`.
  814. *
  815. * @publicApi
  816. *
  817. *
  818. * @param relativeTo The `ActivatedRouteSnapshot` to apply the commands to
  819. * @param commands An array of URL fragments with which to construct the new URL tree.
  820. * If the path is static, can be the literal URL string. For a dynamic path, pass an array of path
  821. * segments, followed by the parameters for each segment.
  822. * The fragments are applied to the one provided in the `relativeTo` parameter.
  823. * @param queryParams The query parameters for the `UrlTree`. `null` if the `UrlTree` does not have
  824. * any query parameters.
  825. * @param fragment The fragment for the `UrlTree`. `null` if the `UrlTree` does not have a fragment.
  826. *
  827. * @usageNotes
  828. *
  829. * ```ts
  830. * // create /team/33/user/11
  831. * createUrlTreeFromSnapshot(snapshot, ['/team', 33, 'user', 11]);
  832. *
  833. * // create /team/33;expand=true/user/11
  834. * createUrlTreeFromSnapshot(snapshot, ['/team', 33, {expand: true}, 'user', 11]);
  835. *
  836. * // you can collapse static segments like this (this works only with the first passed-in value):
  837. * createUrlTreeFromSnapshot(snapshot, ['/team/33/user', userId]);
  838. *
  839. * // If the first segment can contain slashes, and you do not want the router to split it,
  840. * // you can do the following:
  841. * createUrlTreeFromSnapshot(snapshot, [{segmentPath: '/one/two'}]);
  842. *
  843. * // create /team/33/(user/11//right:chat)
  844. * createUrlTreeFromSnapshot(snapshot, ['/team', 33, {outlets: {primary: 'user/11', right:
  845. * 'chat'}}], null, null);
  846. *
  847. * // remove the right secondary node
  848. * createUrlTreeFromSnapshot(snapshot, ['/team', 33, {outlets: {primary: 'user/11', right: null}}]);
  849. *
  850. * // For the examples below, assume the current URL is for the `/team/33/user/11` and the
  851. * `ActivatedRouteSnapshot` points to `user/11`:
  852. *
  853. * // navigate to /team/33/user/11/details
  854. * createUrlTreeFromSnapshot(snapshot, ['details']);
  855. *
  856. * // navigate to /team/33/user/22
  857. * createUrlTreeFromSnapshot(snapshot, ['../22']);
  858. *
  859. * // navigate to /team/44/user/22
  860. * createUrlTreeFromSnapshot(snapshot, ['../../team/44/user/22']);
  861. * ```
  862. */
  863. function createUrlTreeFromSnapshot(relativeTo, commands, queryParams = null, fragment = null) {
  864. const relativeToUrlSegmentGroup = createSegmentGroupFromRoute(relativeTo);
  865. return createUrlTreeFromSegmentGroup(relativeToUrlSegmentGroup, commands, queryParams, fragment);
  866. }
  867. function createSegmentGroupFromRoute(route) {
  868. let targetGroup;
  869. function createSegmentGroupFromRouteRecursive(currentRoute) {
  870. const childOutlets = {};
  871. for (const childSnapshot of currentRoute.children) {
  872. const root = createSegmentGroupFromRouteRecursive(childSnapshot);
  873. childOutlets[childSnapshot.outlet] = root;
  874. }
  875. const segmentGroup = new UrlSegmentGroup(currentRoute.url, childOutlets);
  876. if (currentRoute === route) {
  877. targetGroup = segmentGroup;
  878. }
  879. return segmentGroup;
  880. }
  881. const rootCandidate = createSegmentGroupFromRouteRecursive(route.root);
  882. const rootSegmentGroup = createRoot(rootCandidate);
  883. return targetGroup ?? rootSegmentGroup;
  884. }
  885. function createUrlTreeFromSegmentGroup(relativeTo, commands, queryParams, fragment) {
  886. let root = relativeTo;
  887. while (root.parent) {
  888. root = root.parent;
  889. }
  890. // There are no commands so the `UrlTree` goes to the same path as the one created from the
  891. // `UrlSegmentGroup`. All we need to do is update the `queryParams` and `fragment` without
  892. // applying any other logic.
  893. if (commands.length === 0) {
  894. return tree(root, root, root, queryParams, fragment);
  895. }
  896. const nav = computeNavigation(commands);
  897. if (nav.toRoot()) {
  898. return tree(root, root, new UrlSegmentGroup([], {}), queryParams, fragment);
  899. }
  900. const position = findStartingPositionForTargetGroup(nav, root, relativeTo);
  901. const newSegmentGroup = position.processChildren
  902. ? updateSegmentGroupChildren(position.segmentGroup, position.index, nav.commands)
  903. : updateSegmentGroup(position.segmentGroup, position.index, nav.commands);
  904. return tree(root, position.segmentGroup, newSegmentGroup, queryParams, fragment);
  905. }
  906. function isMatrixParams(command) {
  907. return typeof command === 'object' && command != null && !command.outlets && !command.segmentPath;
  908. }
  909. /**
  910. * Determines if a given command has an `outlets` map. When we encounter a command
  911. * with an outlets k/v map, we need to apply each outlet individually to the existing segment.
  912. */
  913. function isCommandWithOutlets(command) {
  914. return typeof command === 'object' && command != null && command.outlets;
  915. }
  916. function tree(oldRoot, oldSegmentGroup, newSegmentGroup, queryParams, fragment) {
  917. let qp = {};
  918. if (queryParams) {
  919. Object.entries(queryParams).forEach(([name, value]) => {
  920. qp[name] = Array.isArray(value) ? value.map((v) => `${v}`) : `${value}`;
  921. });
  922. }
  923. let rootCandidate;
  924. if (oldRoot === oldSegmentGroup) {
  925. rootCandidate = newSegmentGroup;
  926. }
  927. else {
  928. rootCandidate = replaceSegment(oldRoot, oldSegmentGroup, newSegmentGroup);
  929. }
  930. const newRoot = createRoot(squashSegmentGroup(rootCandidate));
  931. return new UrlTree(newRoot, qp, fragment);
  932. }
  933. /**
  934. * Replaces the `oldSegment` which is located in some child of the `current` with the `newSegment`.
  935. * This also has the effect of creating new `UrlSegmentGroup` copies to update references. This
  936. * shouldn't be necessary but the fallback logic for an invalid ActivatedRoute in the creation uses
  937. * the Router's current url tree. If we don't create new segment groups, we end up modifying that
  938. * value.
  939. */
  940. function replaceSegment(current, oldSegment, newSegment) {
  941. const children = {};
  942. Object.entries(current.children).forEach(([outletName, c]) => {
  943. if (c === oldSegment) {
  944. children[outletName] = newSegment;
  945. }
  946. else {
  947. children[outletName] = replaceSegment(c, oldSegment, newSegment);
  948. }
  949. });
  950. return new UrlSegmentGroup(current.segments, children);
  951. }
  952. class Navigation {
  953. isAbsolute;
  954. numberOfDoubleDots;
  955. commands;
  956. constructor(isAbsolute, numberOfDoubleDots, commands) {
  957. this.isAbsolute = isAbsolute;
  958. this.numberOfDoubleDots = numberOfDoubleDots;
  959. this.commands = commands;
  960. if (isAbsolute && commands.length > 0 && isMatrixParams(commands[0])) {
  961. throw new _RuntimeError(4003 /* RuntimeErrorCode.ROOT_SEGMENT_MATRIX_PARAMS */, (typeof ngDevMode === 'undefined' || ngDevMode) &&
  962. 'Root segment cannot have matrix parameters');
  963. }
  964. const cmdWithOutlet = commands.find(isCommandWithOutlets);
  965. if (cmdWithOutlet && cmdWithOutlet !== last(commands)) {
  966. throw new _RuntimeError(4004 /* RuntimeErrorCode.MISPLACED_OUTLETS_COMMAND */, (typeof ngDevMode === 'undefined' || ngDevMode) &&
  967. '{outlets:{}} has to be the last command');
  968. }
  969. }
  970. toRoot() {
  971. return this.isAbsolute && this.commands.length === 1 && this.commands[0] == '/';
  972. }
  973. }
  974. /** Transforms commands to a normalized `Navigation` */
  975. function computeNavigation(commands) {
  976. if (typeof commands[0] === 'string' && commands.length === 1 && commands[0] === '/') {
  977. return new Navigation(true, 0, commands);
  978. }
  979. let numberOfDoubleDots = 0;
  980. let isAbsolute = false;
  981. const res = commands.reduce((res, cmd, cmdIdx) => {
  982. if (typeof cmd === 'object' && cmd != null) {
  983. if (cmd.outlets) {
  984. const outlets = {};
  985. Object.entries(cmd.outlets).forEach(([name, commands]) => {
  986. outlets[name] = typeof commands === 'string' ? commands.split('/') : commands;
  987. });
  988. return [...res, { outlets }];
  989. }
  990. if (cmd.segmentPath) {
  991. return [...res, cmd.segmentPath];
  992. }
  993. }
  994. if (!(typeof cmd === 'string')) {
  995. return [...res, cmd];
  996. }
  997. if (cmdIdx === 0) {
  998. cmd.split('/').forEach((urlPart, partIndex) => {
  999. if (partIndex == 0 && urlPart === '.') ;
  1000. else if (partIndex == 0 && urlPart === '') {
  1001. // '/a'
  1002. isAbsolute = true;
  1003. }
  1004. else if (urlPart === '..') {
  1005. // '../a'
  1006. numberOfDoubleDots++;
  1007. }
  1008. else if (urlPart != '') {
  1009. res.push(urlPart);
  1010. }
  1011. });
  1012. return res;
  1013. }
  1014. return [...res, cmd];
  1015. }, []);
  1016. return new Navigation(isAbsolute, numberOfDoubleDots, res);
  1017. }
  1018. class Position {
  1019. segmentGroup;
  1020. processChildren;
  1021. index;
  1022. constructor(segmentGroup, processChildren, index) {
  1023. this.segmentGroup = segmentGroup;
  1024. this.processChildren = processChildren;
  1025. this.index = index;
  1026. }
  1027. }
  1028. function findStartingPositionForTargetGroup(nav, root, target) {
  1029. if (nav.isAbsolute) {
  1030. return new Position(root, true, 0);
  1031. }
  1032. if (!target) {
  1033. // `NaN` is used only to maintain backwards compatibility with incorrectly mocked
  1034. // `ActivatedRouteSnapshot` in tests. In prior versions of this code, the position here was
  1035. // determined based on an internal property that was rarely mocked, resulting in `NaN`. In
  1036. // reality, this code path should _never_ be touched since `target` is not allowed to be falsey.
  1037. return new Position(root, false, NaN);
  1038. }
  1039. if (target.parent === null) {
  1040. return new Position(target, true, 0);
  1041. }
  1042. const modifier = isMatrixParams(nav.commands[0]) ? 0 : 1;
  1043. const index = target.segments.length - 1 + modifier;
  1044. return createPositionApplyingDoubleDots(target, index, nav.numberOfDoubleDots);
  1045. }
  1046. function createPositionApplyingDoubleDots(group, index, numberOfDoubleDots) {
  1047. let g = group;
  1048. let ci = index;
  1049. let dd = numberOfDoubleDots;
  1050. while (dd > ci) {
  1051. dd -= ci;
  1052. g = g.parent;
  1053. if (!g) {
  1054. throw new _RuntimeError(4005 /* RuntimeErrorCode.INVALID_DOUBLE_DOTS */, (typeof ngDevMode === 'undefined' || ngDevMode) && "Invalid number of '../'");
  1055. }
  1056. ci = g.segments.length;
  1057. }
  1058. return new Position(g, false, ci - dd);
  1059. }
  1060. function getOutlets(commands) {
  1061. if (isCommandWithOutlets(commands[0])) {
  1062. return commands[0].outlets;
  1063. }
  1064. return { [PRIMARY_OUTLET]: commands };
  1065. }
  1066. function updateSegmentGroup(segmentGroup, startIndex, commands) {
  1067. segmentGroup ??= new UrlSegmentGroup([], {});
  1068. if (segmentGroup.segments.length === 0 && segmentGroup.hasChildren()) {
  1069. return updateSegmentGroupChildren(segmentGroup, startIndex, commands);
  1070. }
  1071. const m = prefixedWith(segmentGroup, startIndex, commands);
  1072. const slicedCommands = commands.slice(m.commandIndex);
  1073. if (m.match && m.pathIndex < segmentGroup.segments.length) {
  1074. const g = new UrlSegmentGroup(segmentGroup.segments.slice(0, m.pathIndex), {});
  1075. g.children[PRIMARY_OUTLET] = new UrlSegmentGroup(segmentGroup.segments.slice(m.pathIndex), segmentGroup.children);
  1076. return updateSegmentGroupChildren(g, 0, slicedCommands);
  1077. }
  1078. else if (m.match && slicedCommands.length === 0) {
  1079. return new UrlSegmentGroup(segmentGroup.segments, {});
  1080. }
  1081. else if (m.match && !segmentGroup.hasChildren()) {
  1082. return createNewSegmentGroup(segmentGroup, startIndex, commands);
  1083. }
  1084. else if (m.match) {
  1085. return updateSegmentGroupChildren(segmentGroup, 0, slicedCommands);
  1086. }
  1087. else {
  1088. return createNewSegmentGroup(segmentGroup, startIndex, commands);
  1089. }
  1090. }
  1091. function updateSegmentGroupChildren(segmentGroup, startIndex, commands) {
  1092. if (commands.length === 0) {
  1093. return new UrlSegmentGroup(segmentGroup.segments, {});
  1094. }
  1095. else {
  1096. const outlets = getOutlets(commands);
  1097. const children = {};
  1098. // If the set of commands applies to anything other than the primary outlet and the child
  1099. // segment is an empty path primary segment on its own, we want to apply the commands to the
  1100. // empty child path rather than here. The outcome is that the empty primary child is effectively
  1101. // removed from the final output UrlTree. Imagine the following config:
  1102. //
  1103. // {path: '', children: [{path: '**', outlet: 'popup'}]}.
  1104. //
  1105. // Navigation to /(popup:a) will activate the child outlet correctly Given a follow-up
  1106. // navigation with commands
  1107. // ['/', {outlets: {'popup': 'b'}}], we _would not_ want to apply the outlet commands to the
  1108. // root segment because that would result in
  1109. // //(popup:a)(popup:b) since the outlet command got applied one level above where it appears in
  1110. // the `ActivatedRoute` rather than updating the existing one.
  1111. //
  1112. // Because empty paths do not appear in the URL segments and the fact that the segments used in
  1113. // the output `UrlTree` are squashed to eliminate these empty paths where possible
  1114. // https://github.com/angular/angular/blob/13f10de40e25c6900ca55bd83b36bd533dacfa9e/packages/router/src/url_tree.ts#L755
  1115. // it can be hard to determine what is the right thing to do when applying commands to a
  1116. // `UrlSegmentGroup` that is created from an "unsquashed"/expanded `ActivatedRoute` tree.
  1117. // This code effectively "squashes" empty path primary routes when they have no siblings on
  1118. // the same level of the tree.
  1119. if (Object.keys(outlets).some((o) => o !== PRIMARY_OUTLET) &&
  1120. segmentGroup.children[PRIMARY_OUTLET] &&
  1121. segmentGroup.numberOfChildren === 1 &&
  1122. segmentGroup.children[PRIMARY_OUTLET].segments.length === 0) {
  1123. const childrenOfEmptyChild = updateSegmentGroupChildren(segmentGroup.children[PRIMARY_OUTLET], startIndex, commands);
  1124. return new UrlSegmentGroup(segmentGroup.segments, childrenOfEmptyChild.children);
  1125. }
  1126. Object.entries(outlets).forEach(([outlet, commands]) => {
  1127. if (typeof commands === 'string') {
  1128. commands = [commands];
  1129. }
  1130. if (commands !== null) {
  1131. children[outlet] = updateSegmentGroup(segmentGroup.children[outlet], startIndex, commands);
  1132. }
  1133. });
  1134. Object.entries(segmentGroup.children).forEach(([childOutlet, child]) => {
  1135. if (outlets[childOutlet] === undefined) {
  1136. children[childOutlet] = child;
  1137. }
  1138. });
  1139. return new UrlSegmentGroup(segmentGroup.segments, children);
  1140. }
  1141. }
  1142. function prefixedWith(segmentGroup, startIndex, commands) {
  1143. let currentCommandIndex = 0;
  1144. let currentPathIndex = startIndex;
  1145. const noMatch = { match: false, pathIndex: 0, commandIndex: 0 };
  1146. while (currentPathIndex < segmentGroup.segments.length) {
  1147. if (currentCommandIndex >= commands.length)
  1148. return noMatch;
  1149. const path = segmentGroup.segments[currentPathIndex];
  1150. const command = commands[currentCommandIndex];
  1151. // Do not try to consume command as part of the prefixing if it has outlets because it can
  1152. // contain outlets other than the one being processed. Consuming the outlets command would
  1153. // result in other outlets being ignored.
  1154. if (isCommandWithOutlets(command)) {
  1155. break;
  1156. }
  1157. const curr = `${command}`;
  1158. const next = currentCommandIndex < commands.length - 1 ? commands[currentCommandIndex + 1] : null;
  1159. if (currentPathIndex > 0 && curr === undefined)
  1160. break;
  1161. if (curr && next && typeof next === 'object' && next.outlets === undefined) {
  1162. if (!compare(curr, next, path))
  1163. return noMatch;
  1164. currentCommandIndex += 2;
  1165. }
  1166. else {
  1167. if (!compare(curr, {}, path))
  1168. return noMatch;
  1169. currentCommandIndex++;
  1170. }
  1171. currentPathIndex++;
  1172. }
  1173. return { match: true, pathIndex: currentPathIndex, commandIndex: currentCommandIndex };
  1174. }
  1175. function createNewSegmentGroup(segmentGroup, startIndex, commands) {
  1176. const paths = segmentGroup.segments.slice(0, startIndex);
  1177. let i = 0;
  1178. while (i < commands.length) {
  1179. const command = commands[i];
  1180. if (isCommandWithOutlets(command)) {
  1181. const children = createNewSegmentChildren(command.outlets);
  1182. return new UrlSegmentGroup(paths, children);
  1183. }
  1184. // if we start with an object literal, we need to reuse the path part from the segment
  1185. if (i === 0 && isMatrixParams(commands[0])) {
  1186. const p = segmentGroup.segments[startIndex];
  1187. paths.push(new UrlSegment(p.path, stringify(commands[0])));
  1188. i++;
  1189. continue;
  1190. }
  1191. const curr = isCommandWithOutlets(command) ? command.outlets[PRIMARY_OUTLET] : `${command}`;
  1192. const next = i < commands.length - 1 ? commands[i + 1] : null;
  1193. if (curr && next && isMatrixParams(next)) {
  1194. paths.push(new UrlSegment(curr, stringify(next)));
  1195. i += 2;
  1196. }
  1197. else {
  1198. paths.push(new UrlSegment(curr, {}));
  1199. i++;
  1200. }
  1201. }
  1202. return new UrlSegmentGroup(paths, {});
  1203. }
  1204. function createNewSegmentChildren(outlets) {
  1205. const children = {};
  1206. Object.entries(outlets).forEach(([outlet, commands]) => {
  1207. if (typeof commands === 'string') {
  1208. commands = [commands];
  1209. }
  1210. if (commands !== null) {
  1211. children[outlet] = createNewSegmentGroup(new UrlSegmentGroup([], {}), 0, commands);
  1212. }
  1213. });
  1214. return children;
  1215. }
  1216. function stringify(params) {
  1217. const res = {};
  1218. Object.entries(params).forEach(([k, v]) => (res[k] = `${v}`));
  1219. return res;
  1220. }
  1221. function compare(path, params, segment) {
  1222. return path == segment.path && shallowEqual(params, segment.parameters);
  1223. }
  1224. const IMPERATIVE_NAVIGATION = 'imperative';
  1225. /**
  1226. * Identifies the type of a router event.
  1227. *
  1228. * @publicApi
  1229. */
  1230. var EventType;
  1231. (function (EventType) {
  1232. EventType[EventType["NavigationStart"] = 0] = "NavigationStart";
  1233. EventType[EventType["NavigationEnd"] = 1] = "NavigationEnd";
  1234. EventType[EventType["NavigationCancel"] = 2] = "NavigationCancel";
  1235. EventType[EventType["NavigationError"] = 3] = "NavigationError";
  1236. EventType[EventType["RoutesRecognized"] = 4] = "RoutesRecognized";
  1237. EventType[EventType["ResolveStart"] = 5] = "ResolveStart";
  1238. EventType[EventType["ResolveEnd"] = 6] = "ResolveEnd";
  1239. EventType[EventType["GuardsCheckStart"] = 7] = "GuardsCheckStart";
  1240. EventType[EventType["GuardsCheckEnd"] = 8] = "GuardsCheckEnd";
  1241. EventType[EventType["RouteConfigLoadStart"] = 9] = "RouteConfigLoadStart";
  1242. EventType[EventType["RouteConfigLoadEnd"] = 10] = "RouteConfigLoadEnd";
  1243. EventType[EventType["ChildActivationStart"] = 11] = "ChildActivationStart";
  1244. EventType[EventType["ChildActivationEnd"] = 12] = "ChildActivationEnd";
  1245. EventType[EventType["ActivationStart"] = 13] = "ActivationStart";
  1246. EventType[EventType["ActivationEnd"] = 14] = "ActivationEnd";
  1247. EventType[EventType["Scroll"] = 15] = "Scroll";
  1248. EventType[EventType["NavigationSkipped"] = 16] = "NavigationSkipped";
  1249. })(EventType || (EventType = {}));
  1250. /**
  1251. * Base for events the router goes through, as opposed to events tied to a specific
  1252. * route. Fired one time for any given navigation.
  1253. *
  1254. * The following code shows how a class subscribes to router events.
  1255. *
  1256. * ```ts
  1257. * import {Event, RouterEvent, Router} from '@angular/router';
  1258. *
  1259. * class MyService {
  1260. * constructor(public router: Router) {
  1261. * router.events.pipe(
  1262. * filter((e: Event | RouterEvent): e is RouterEvent => e instanceof RouterEvent)
  1263. * ).subscribe((e: RouterEvent) => {
  1264. * // Do something
  1265. * });
  1266. * }
  1267. * }
  1268. * ```
  1269. *
  1270. * @see {@link Event}
  1271. * @see [Router events summary](guide/routing/router-reference#router-events)
  1272. * @publicApi
  1273. */
  1274. class RouterEvent {
  1275. id;
  1276. url;
  1277. constructor(
  1278. /** A unique ID that the router assigns to every router navigation. */
  1279. id,
  1280. /** The URL that is the destination for this navigation. */
  1281. url) {
  1282. this.id = id;
  1283. this.url = url;
  1284. }
  1285. }
  1286. /**
  1287. * An event triggered when a navigation starts.
  1288. *
  1289. * @publicApi
  1290. */
  1291. class NavigationStart extends RouterEvent {
  1292. type = EventType.NavigationStart;
  1293. /**
  1294. * Identifies the call or event that triggered the navigation.
  1295. * An `imperative` trigger is a call to `router.navigateByUrl()` or `router.navigate()`.
  1296. *
  1297. * @see {@link NavigationEnd}
  1298. * @see {@link NavigationCancel}
  1299. * @see {@link NavigationError}
  1300. */
  1301. navigationTrigger;
  1302. /**
  1303. * The navigation state that was previously supplied to the `pushState` call,
  1304. * when the navigation is triggered by a `popstate` event. Otherwise null.
  1305. *
  1306. * The state object is defined by `NavigationExtras`, and contains any
  1307. * developer-defined state value, as well as a unique ID that
  1308. * the router assigns to every router transition/navigation.
  1309. *
  1310. * From the perspective of the router, the router never "goes back".
  1311. * When the user clicks on the back button in the browser,
  1312. * a new navigation ID is created.
  1313. *
  1314. * Use the ID in this previous-state object to differentiate between a newly created
  1315. * state and one returned to by a `popstate` event, so that you can restore some
  1316. * remembered state, such as scroll position.
  1317. *
  1318. */
  1319. restoredState;
  1320. constructor(
  1321. /** @docsNotRequired */
  1322. id,
  1323. /** @docsNotRequired */
  1324. url,
  1325. /** @docsNotRequired */
  1326. navigationTrigger = 'imperative',
  1327. /** @docsNotRequired */
  1328. restoredState = null) {
  1329. super(id, url);
  1330. this.navigationTrigger = navigationTrigger;
  1331. this.restoredState = restoredState;
  1332. }
  1333. /** @docsNotRequired */
  1334. toString() {
  1335. return `NavigationStart(id: ${this.id}, url: '${this.url}')`;
  1336. }
  1337. }
  1338. /**
  1339. * An event triggered when a navigation ends successfully.
  1340. *
  1341. * @see {@link NavigationStart}
  1342. * @see {@link NavigationCancel}
  1343. * @see {@link NavigationError}
  1344. *
  1345. * @publicApi
  1346. */
  1347. class NavigationEnd extends RouterEvent {
  1348. urlAfterRedirects;
  1349. type = EventType.NavigationEnd;
  1350. constructor(
  1351. /** @docsNotRequired */
  1352. id,
  1353. /** @docsNotRequired */
  1354. url,
  1355. /** @docsNotRequired */
  1356. urlAfterRedirects) {
  1357. super(id, url);
  1358. this.urlAfterRedirects = urlAfterRedirects;
  1359. }
  1360. /** @docsNotRequired */
  1361. toString() {
  1362. return `NavigationEnd(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}')`;
  1363. }
  1364. }
  1365. /**
  1366. * A code for the `NavigationCancel` event of the `Router` to indicate the
  1367. * reason a navigation failed.
  1368. *
  1369. * @publicApi
  1370. */
  1371. var NavigationCancellationCode;
  1372. (function (NavigationCancellationCode) {
  1373. /**
  1374. * A navigation failed because a guard returned a `UrlTree` to redirect.
  1375. */
  1376. NavigationCancellationCode[NavigationCancellationCode["Redirect"] = 0] = "Redirect";
  1377. /**
  1378. * A navigation failed because a more recent navigation started.
  1379. */
  1380. NavigationCancellationCode[NavigationCancellationCode["SupersededByNewNavigation"] = 1] = "SupersededByNewNavigation";
  1381. /**
  1382. * A navigation failed because one of the resolvers completed without emitting a value.
  1383. */
  1384. NavigationCancellationCode[NavigationCancellationCode["NoDataFromResolver"] = 2] = "NoDataFromResolver";
  1385. /**
  1386. * A navigation failed because a guard returned `false`.
  1387. */
  1388. NavigationCancellationCode[NavigationCancellationCode["GuardRejected"] = 3] = "GuardRejected";
  1389. })(NavigationCancellationCode || (NavigationCancellationCode = {}));
  1390. /**
  1391. * A code for the `NavigationSkipped` event of the `Router` to indicate the
  1392. * reason a navigation was skipped.
  1393. *
  1394. * @publicApi
  1395. */
  1396. var NavigationSkippedCode;
  1397. (function (NavigationSkippedCode) {
  1398. /**
  1399. * A navigation was skipped because the navigation URL was the same as the current Router URL.
  1400. */
  1401. NavigationSkippedCode[NavigationSkippedCode["IgnoredSameUrlNavigation"] = 0] = "IgnoredSameUrlNavigation";
  1402. /**
  1403. * A navigation was skipped because the configured `UrlHandlingStrategy` return `false` for both
  1404. * the current Router URL and the target of the navigation.
  1405. *
  1406. * @see {@link UrlHandlingStrategy}
  1407. */
  1408. NavigationSkippedCode[NavigationSkippedCode["IgnoredByUrlHandlingStrategy"] = 1] = "IgnoredByUrlHandlingStrategy";
  1409. })(NavigationSkippedCode || (NavigationSkippedCode = {}));
  1410. /**
  1411. * An event triggered when a navigation is canceled, directly or indirectly.
  1412. * This can happen for several reasons including when a route guard
  1413. * returns `false` or initiates a redirect by returning a `UrlTree`.
  1414. *
  1415. * @see {@link NavigationStart}
  1416. * @see {@link NavigationEnd}
  1417. * @see {@link NavigationError}
  1418. *
  1419. * @publicApi
  1420. */
  1421. class NavigationCancel extends RouterEvent {
  1422. reason;
  1423. code;
  1424. type = EventType.NavigationCancel;
  1425. constructor(
  1426. /** @docsNotRequired */
  1427. id,
  1428. /** @docsNotRequired */
  1429. url,
  1430. /**
  1431. * A description of why the navigation was cancelled. For debug purposes only. Use `code`
  1432. * instead for a stable cancellation reason that can be used in production.
  1433. */
  1434. reason,
  1435. /**
  1436. * A code to indicate why the navigation was canceled. This cancellation code is stable for
  1437. * the reason and can be relied on whereas the `reason` string could change and should not be
  1438. * used in production.
  1439. */
  1440. code) {
  1441. super(id, url);
  1442. this.reason = reason;
  1443. this.code = code;
  1444. }
  1445. /** @docsNotRequired */
  1446. toString() {
  1447. return `NavigationCancel(id: ${this.id}, url: '${this.url}')`;
  1448. }
  1449. }
  1450. /**
  1451. * An event triggered when a navigation is skipped.
  1452. * This can happen for a couple reasons including onSameUrlHandling
  1453. * is set to `ignore` and the navigation URL is not different than the
  1454. * current state.
  1455. *
  1456. * @publicApi
  1457. */
  1458. class NavigationSkipped extends RouterEvent {
  1459. reason;
  1460. code;
  1461. type = EventType.NavigationSkipped;
  1462. constructor(
  1463. /** @docsNotRequired */
  1464. id,
  1465. /** @docsNotRequired */
  1466. url,
  1467. /**
  1468. * A description of why the navigation was skipped. For debug purposes only. Use `code`
  1469. * instead for a stable skipped reason that can be used in production.
  1470. */
  1471. reason,
  1472. /**
  1473. * A code to indicate why the navigation was skipped. This code is stable for
  1474. * the reason and can be relied on whereas the `reason` string could change and should not be
  1475. * used in production.
  1476. */
  1477. code) {
  1478. super(id, url);
  1479. this.reason = reason;
  1480. this.code = code;
  1481. }
  1482. }
  1483. /**
  1484. * An event triggered when a navigation fails due to an unexpected error.
  1485. *
  1486. * @see {@link NavigationStart}
  1487. * @see {@link NavigationEnd}
  1488. * @see {@link NavigationCancel}
  1489. *
  1490. * @publicApi
  1491. */
  1492. class NavigationError extends RouterEvent {
  1493. error;
  1494. target;
  1495. type = EventType.NavigationError;
  1496. constructor(
  1497. /** @docsNotRequired */
  1498. id,
  1499. /** @docsNotRequired */
  1500. url,
  1501. /** @docsNotRequired */
  1502. error,
  1503. /**
  1504. * The target of the navigation when the error occurred.
  1505. *
  1506. * Note that this can be `undefined` because an error could have occurred before the
  1507. * `RouterStateSnapshot` was created for the navigation.
  1508. */
  1509. target) {
  1510. super(id, url);
  1511. this.error = error;
  1512. this.target = target;
  1513. }
  1514. /** @docsNotRequired */
  1515. toString() {
  1516. return `NavigationError(id: ${this.id}, url: '${this.url}', error: ${this.error})`;
  1517. }
  1518. }
  1519. /**
  1520. * An event triggered when routes are recognized.
  1521. *
  1522. * @publicApi
  1523. */
  1524. class RoutesRecognized extends RouterEvent {
  1525. urlAfterRedirects;
  1526. state;
  1527. type = EventType.RoutesRecognized;
  1528. constructor(
  1529. /** @docsNotRequired */
  1530. id,
  1531. /** @docsNotRequired */
  1532. url,
  1533. /** @docsNotRequired */
  1534. urlAfterRedirects,
  1535. /** @docsNotRequired */
  1536. state) {
  1537. super(id, url);
  1538. this.urlAfterRedirects = urlAfterRedirects;
  1539. this.state = state;
  1540. }
  1541. /** @docsNotRequired */
  1542. toString() {
  1543. return `RoutesRecognized(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state})`;
  1544. }
  1545. }
  1546. /**
  1547. * An event triggered at the start of the Guard phase of routing.
  1548. *
  1549. * @see {@link GuardsCheckEnd}
  1550. *
  1551. * @publicApi
  1552. */
  1553. class GuardsCheckStart extends RouterEvent {
  1554. urlAfterRedirects;
  1555. state;
  1556. type = EventType.GuardsCheckStart;
  1557. constructor(
  1558. /** @docsNotRequired */
  1559. id,
  1560. /** @docsNotRequired */
  1561. url,
  1562. /** @docsNotRequired */
  1563. urlAfterRedirects,
  1564. /** @docsNotRequired */
  1565. state) {
  1566. super(id, url);
  1567. this.urlAfterRedirects = urlAfterRedirects;
  1568. this.state = state;
  1569. }
  1570. toString() {
  1571. return `GuardsCheckStart(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state})`;
  1572. }
  1573. }
  1574. /**
  1575. * An event triggered at the end of the Guard phase of routing.
  1576. *
  1577. * @see {@link GuardsCheckStart}
  1578. *
  1579. * @publicApi
  1580. */
  1581. class GuardsCheckEnd extends RouterEvent {
  1582. urlAfterRedirects;
  1583. state;
  1584. shouldActivate;
  1585. type = EventType.GuardsCheckEnd;
  1586. constructor(
  1587. /** @docsNotRequired */
  1588. id,
  1589. /** @docsNotRequired */
  1590. url,
  1591. /** @docsNotRequired */
  1592. urlAfterRedirects,
  1593. /** @docsNotRequired */
  1594. state,
  1595. /** @docsNotRequired */
  1596. shouldActivate) {
  1597. super(id, url);
  1598. this.urlAfterRedirects = urlAfterRedirects;
  1599. this.state = state;
  1600. this.shouldActivate = shouldActivate;
  1601. }
  1602. toString() {
  1603. return `GuardsCheckEnd(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state}, shouldActivate: ${this.shouldActivate})`;
  1604. }
  1605. }
  1606. /**
  1607. * An event triggered at the start of the Resolve phase of routing.
  1608. *
  1609. * Runs in the "resolve" phase whether or not there is anything to resolve.
  1610. * In future, may change to only run when there are things to be resolved.
  1611. *
  1612. * @see {@link ResolveEnd}
  1613. *
  1614. * @publicApi
  1615. */
  1616. class ResolveStart extends RouterEvent {
  1617. urlAfterRedirects;
  1618. state;
  1619. type = EventType.ResolveStart;
  1620. constructor(
  1621. /** @docsNotRequired */
  1622. id,
  1623. /** @docsNotRequired */
  1624. url,
  1625. /** @docsNotRequired */
  1626. urlAfterRedirects,
  1627. /** @docsNotRequired */
  1628. state) {
  1629. super(id, url);
  1630. this.urlAfterRedirects = urlAfterRedirects;
  1631. this.state = state;
  1632. }
  1633. toString() {
  1634. return `ResolveStart(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state})`;
  1635. }
  1636. }
  1637. /**
  1638. * An event triggered at the end of the Resolve phase of routing.
  1639. * @see {@link ResolveStart}
  1640. *
  1641. * @publicApi
  1642. */
  1643. class ResolveEnd extends RouterEvent {
  1644. urlAfterRedirects;
  1645. state;
  1646. type = EventType.ResolveEnd;
  1647. constructor(
  1648. /** @docsNotRequired */
  1649. id,
  1650. /** @docsNotRequired */
  1651. url,
  1652. /** @docsNotRequired */
  1653. urlAfterRedirects,
  1654. /** @docsNotRequired */
  1655. state) {
  1656. super(id, url);
  1657. this.urlAfterRedirects = urlAfterRedirects;
  1658. this.state = state;
  1659. }
  1660. toString() {
  1661. return `ResolveEnd(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state})`;
  1662. }
  1663. }
  1664. /**
  1665. * An event triggered before lazy loading a route configuration.
  1666. *
  1667. * @see {@link RouteConfigLoadEnd}
  1668. *
  1669. * @publicApi
  1670. */
  1671. class RouteConfigLoadStart {
  1672. route;
  1673. type = EventType.RouteConfigLoadStart;
  1674. constructor(
  1675. /** @docsNotRequired */
  1676. route) {
  1677. this.route = route;
  1678. }
  1679. toString() {
  1680. return `RouteConfigLoadStart(path: ${this.route.path})`;
  1681. }
  1682. }
  1683. /**
  1684. * An event triggered when a route has been lazy loaded.
  1685. *
  1686. * @see {@link RouteConfigLoadStart}
  1687. *
  1688. * @publicApi
  1689. */
  1690. class RouteConfigLoadEnd {
  1691. route;
  1692. type = EventType.RouteConfigLoadEnd;
  1693. constructor(
  1694. /** @docsNotRequired */
  1695. route) {
  1696. this.route = route;
  1697. }
  1698. toString() {
  1699. return `RouteConfigLoadEnd(path: ${this.route.path})`;
  1700. }
  1701. }
  1702. /**
  1703. * An event triggered at the start of the child-activation
  1704. * part of the Resolve phase of routing.
  1705. * @see {@link ChildActivationEnd}
  1706. * @see {@link ResolveStart}
  1707. *
  1708. * @publicApi
  1709. */
  1710. class ChildActivationStart {
  1711. snapshot;
  1712. type = EventType.ChildActivationStart;
  1713. constructor(
  1714. /** @docsNotRequired */
  1715. snapshot) {
  1716. this.snapshot = snapshot;
  1717. }
  1718. toString() {
  1719. const path = (this.snapshot.routeConfig && this.snapshot.routeConfig.path) || '';
  1720. return `ChildActivationStart(path: '${path}')`;
  1721. }
  1722. }
  1723. /**
  1724. * An event triggered at the end of the child-activation part
  1725. * of the Resolve phase of routing.
  1726. * @see {@link ChildActivationStart}
  1727. * @see {@link ResolveStart}
  1728. * @publicApi
  1729. */
  1730. class ChildActivationEnd {
  1731. snapshot;
  1732. type = EventType.ChildActivationEnd;
  1733. constructor(
  1734. /** @docsNotRequired */
  1735. snapshot) {
  1736. this.snapshot = snapshot;
  1737. }
  1738. toString() {
  1739. const path = (this.snapshot.routeConfig && this.snapshot.routeConfig.path) || '';
  1740. return `ChildActivationEnd(path: '${path}')`;
  1741. }
  1742. }
  1743. /**
  1744. * An event triggered at the start of the activation part
  1745. * of the Resolve phase of routing.
  1746. * @see {@link ActivationEnd}
  1747. * @see {@link ResolveStart}
  1748. *
  1749. * @publicApi
  1750. */
  1751. class ActivationStart {
  1752. snapshot;
  1753. type = EventType.ActivationStart;
  1754. constructor(
  1755. /** @docsNotRequired */
  1756. snapshot) {
  1757. this.snapshot = snapshot;
  1758. }
  1759. toString() {
  1760. const path = (this.snapshot.routeConfig && this.snapshot.routeConfig.path) || '';
  1761. return `ActivationStart(path: '${path}')`;
  1762. }
  1763. }
  1764. /**
  1765. * An event triggered at the end of the activation part
  1766. * of the Resolve phase of routing.
  1767. * @see {@link ActivationStart}
  1768. * @see {@link ResolveStart}
  1769. *
  1770. * @publicApi
  1771. */
  1772. class ActivationEnd {
  1773. snapshot;
  1774. type = EventType.ActivationEnd;
  1775. constructor(
  1776. /** @docsNotRequired */
  1777. snapshot) {
  1778. this.snapshot = snapshot;
  1779. }
  1780. toString() {
  1781. const path = (this.snapshot.routeConfig && this.snapshot.routeConfig.path) || '';
  1782. return `ActivationEnd(path: '${path}')`;
  1783. }
  1784. }
  1785. /**
  1786. * An event triggered by scrolling.
  1787. *
  1788. * @publicApi
  1789. */
  1790. class Scroll {
  1791. routerEvent;
  1792. position;
  1793. anchor;
  1794. type = EventType.Scroll;
  1795. constructor(
  1796. /** @docsNotRequired */
  1797. routerEvent,
  1798. /** @docsNotRequired */
  1799. position,
  1800. /** @docsNotRequired */
  1801. anchor) {
  1802. this.routerEvent = routerEvent;
  1803. this.position = position;
  1804. this.anchor = anchor;
  1805. }
  1806. toString() {
  1807. const pos = this.position ? `${this.position[0]}, ${this.position[1]}` : null;
  1808. return `Scroll(anchor: '${this.anchor}', position: '${pos}')`;
  1809. }
  1810. }
  1811. class BeforeActivateRoutes {
  1812. }
  1813. class RedirectRequest {
  1814. url;
  1815. navigationBehaviorOptions;
  1816. constructor(url, navigationBehaviorOptions) {
  1817. this.url = url;
  1818. this.navigationBehaviorOptions = navigationBehaviorOptions;
  1819. }
  1820. }
  1821. function stringifyEvent(routerEvent) {
  1822. switch (routerEvent.type) {
  1823. case EventType.ActivationEnd:
  1824. return `ActivationEnd(path: '${routerEvent.snapshot.routeConfig?.path || ''}')`;
  1825. case EventType.ActivationStart:
  1826. return `ActivationStart(path: '${routerEvent.snapshot.routeConfig?.path || ''}')`;
  1827. case EventType.ChildActivationEnd:
  1828. return `ChildActivationEnd(path: '${routerEvent.snapshot.routeConfig?.path || ''}')`;
  1829. case EventType.ChildActivationStart:
  1830. return `ChildActivationStart(path: '${routerEvent.snapshot.routeConfig?.path || ''}')`;
  1831. case EventType.GuardsCheckEnd:
  1832. return `GuardsCheckEnd(id: ${routerEvent.id}, url: '${routerEvent.url}', urlAfterRedirects: '${routerEvent.urlAfterRedirects}', state: ${routerEvent.state}, shouldActivate: ${routerEvent.shouldActivate})`;
  1833. case EventType.GuardsCheckStart:
  1834. return `GuardsCheckStart(id: ${routerEvent.id}, url: '${routerEvent.url}', urlAfterRedirects: '${routerEvent.urlAfterRedirects}', state: ${routerEvent.state})`;
  1835. case EventType.NavigationCancel:
  1836. return `NavigationCancel(id: ${routerEvent.id}, url: '${routerEvent.url}')`;
  1837. case EventType.NavigationSkipped:
  1838. return `NavigationSkipped(id: ${routerEvent.id}, url: '${routerEvent.url}')`;
  1839. case EventType.NavigationEnd:
  1840. return `NavigationEnd(id: ${routerEvent.id}, url: '${routerEvent.url}', urlAfterRedirects: '${routerEvent.urlAfterRedirects}')`;
  1841. case EventType.NavigationError:
  1842. return `NavigationError(id: ${routerEvent.id}, url: '${routerEvent.url}', error: ${routerEvent.error})`;
  1843. case EventType.NavigationStart:
  1844. return `NavigationStart(id: ${routerEvent.id}, url: '${routerEvent.url}')`;
  1845. case EventType.ResolveEnd:
  1846. return `ResolveEnd(id: ${routerEvent.id}, url: '${routerEvent.url}', urlAfterRedirects: '${routerEvent.urlAfterRedirects}', state: ${routerEvent.state})`;
  1847. case EventType.ResolveStart:
  1848. return `ResolveStart(id: ${routerEvent.id}, url: '${routerEvent.url}', urlAfterRedirects: '${routerEvent.urlAfterRedirects}', state: ${routerEvent.state})`;
  1849. case EventType.RouteConfigLoadEnd:
  1850. return `RouteConfigLoadEnd(path: ${routerEvent.route.path})`;
  1851. case EventType.RouteConfigLoadStart:
  1852. return `RouteConfigLoadStart(path: ${routerEvent.route.path})`;
  1853. case EventType.RoutesRecognized:
  1854. return `RoutesRecognized(id: ${routerEvent.id}, url: '${routerEvent.url}', urlAfterRedirects: '${routerEvent.urlAfterRedirects}', state: ${routerEvent.state})`;
  1855. case EventType.Scroll:
  1856. const pos = routerEvent.position
  1857. ? `${routerEvent.position[0]}, ${routerEvent.position[1]}`
  1858. : null;
  1859. return `Scroll(anchor: '${routerEvent.anchor}', position: '${pos}')`;
  1860. }
  1861. }
  1862. /**
  1863. * Creates an `EnvironmentInjector` if the `Route` has providers and one does not already exist
  1864. * and returns the injector. Otherwise, if the `Route` does not have `providers`, returns the
  1865. * `currentInjector`.
  1866. *
  1867. * @param route The route that might have providers
  1868. * @param currentInjector The parent injector of the `Route`
  1869. */
  1870. function getOrCreateRouteInjectorIfNeeded(route, currentInjector) {
  1871. if (route.providers && !route._injector) {
  1872. route._injector = createEnvironmentInjector(route.providers, currentInjector, `Route: ${route.path}`);
  1873. }
  1874. return route._injector ?? currentInjector;
  1875. }
  1876. function validateConfig(config, parentPath = '', requireStandaloneComponents = false) {
  1877. // forEach doesn't iterate undefined values
  1878. for (let i = 0; i < config.length; i++) {
  1879. const route = config[i];
  1880. const fullPath = getFullPath(parentPath, route);
  1881. validateNode(route, fullPath, requireStandaloneComponents);
  1882. }
  1883. }
  1884. function assertStandalone(fullPath, component) {
  1885. if (component && _isNgModule(component)) {
  1886. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}'. You are using 'loadComponent' with a module, ` +
  1887. `but it must be used with standalone components. Use 'loadChildren' instead.`);
  1888. }
  1889. else if (component && !isStandalone(component)) {
  1890. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}'. The component must be standalone.`);
  1891. }
  1892. }
  1893. function validateNode(route, fullPath, requireStandaloneComponents) {
  1894. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  1895. if (!route) {
  1896. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `
  1897. Invalid configuration of route '${fullPath}': Encountered undefined route.
  1898. The reason might be an extra comma.
  1899. Example:
  1900. const routes: Routes = [
  1901. { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
  1902. { path: 'dashboard', component: DashboardComponent },, << two commas
  1903. { path: 'detail/:id', component: HeroDetailComponent }
  1904. ];
  1905. `);
  1906. }
  1907. if (Array.isArray(route)) {
  1908. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': Array cannot be specified`);
  1909. }
  1910. if (!route.redirectTo &&
  1911. !route.component &&
  1912. !route.loadComponent &&
  1913. !route.children &&
  1914. !route.loadChildren &&
  1915. route.outlet &&
  1916. route.outlet !== PRIMARY_OUTLET) {
  1917. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': a componentless route without children or loadChildren cannot have a named outlet set`);
  1918. }
  1919. if (route.redirectTo && route.children) {
  1920. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': redirectTo and children cannot be used together`);
  1921. }
  1922. if (route.redirectTo && route.loadChildren) {
  1923. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': redirectTo and loadChildren cannot be used together`);
  1924. }
  1925. if (route.children && route.loadChildren) {
  1926. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': children and loadChildren cannot be used together`);
  1927. }
  1928. if (route.redirectTo && (route.component || route.loadComponent)) {
  1929. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': redirectTo and component/loadComponent cannot be used together`);
  1930. }
  1931. if (route.component && route.loadComponent) {
  1932. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': component and loadComponent cannot be used together`);
  1933. }
  1934. if (route.redirectTo && route.canActivate) {
  1935. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': redirectTo and canActivate cannot be used together. Redirects happen before activation ` +
  1936. `so canActivate will never be executed.`);
  1937. }
  1938. if (route.path && route.matcher) {
  1939. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': path and matcher cannot be used together`);
  1940. }
  1941. if (route.redirectTo === void 0 &&
  1942. !route.component &&
  1943. !route.loadComponent &&
  1944. !route.children &&
  1945. !route.loadChildren) {
  1946. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}'. One of the following must be provided: component, loadComponent, redirectTo, children or loadChildren`);
  1947. }
  1948. if (route.path === void 0 && route.matcher === void 0) {
  1949. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': routes must have either a path or a matcher specified`);
  1950. }
  1951. if (typeof route.path === 'string' && route.path.charAt(0) === '/') {
  1952. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '${fullPath}': path cannot start with a slash`);
  1953. }
  1954. if (route.path === '' && route.redirectTo !== void 0 && route.pathMatch === void 0) {
  1955. const exp = `The default value of 'pathMatch' is 'prefix', but often the intent is to use 'full'.`;
  1956. throw new _RuntimeError(4014 /* RuntimeErrorCode.INVALID_ROUTE_CONFIG */, `Invalid configuration of route '{path: "${fullPath}", redirectTo: "${route.redirectTo}"}': please provide 'pathMatch'. ${exp}`);
  1957. }
  1958. if (requireStandaloneComponents) {
  1959. assertStandalone(fullPath, route.component);
  1960. }
  1961. }
  1962. if (route.children) {
  1963. validateConfig(route.children, fullPath, requireStandaloneComponents);
  1964. }
  1965. }
  1966. function getFullPath(parentPath, currentRoute) {
  1967. if (!currentRoute) {
  1968. return parentPath;
  1969. }
  1970. if (!parentPath && !currentRoute.path) {
  1971. return '';
  1972. }
  1973. else if (parentPath && !currentRoute.path) {
  1974. return `${parentPath}/`;
  1975. }
  1976. else if (!parentPath && currentRoute.path) {
  1977. return currentRoute.path;
  1978. }
  1979. else {
  1980. return `${parentPath}/${currentRoute.path}`;
  1981. }
  1982. }
  1983. /** Returns the `route.outlet` or PRIMARY_OUTLET if none exists. */
  1984. function getOutlet(route) {
  1985. return route.outlet || PRIMARY_OUTLET;
  1986. }
  1987. /**
  1988. * Sorts the `routes` such that the ones with an outlet matching `outletName` come first.
  1989. * The order of the configs is otherwise preserved.
  1990. */
  1991. function sortByMatchingOutlets(routes, outletName) {
  1992. const sortedConfig = routes.filter((r) => getOutlet(r) === outletName);
  1993. sortedConfig.push(...routes.filter((r) => getOutlet(r) !== outletName));
  1994. return sortedConfig;
  1995. }
  1996. /**
  1997. * Gets the first injector in the snapshot's parent tree.
  1998. *
  1999. * If the `Route` has a static list of providers, the returned injector will be the one created from
  2000. * those. If it does not exist, the returned injector may come from the parents, which may be from a
  2001. * loaded config or their static providers.
  2002. *
  2003. * Returns `null` if there is neither this nor any parents have a stored injector.
  2004. *
  2005. * Generally used for retrieving the injector to use for getting tokens for guards/resolvers and
  2006. * also used for getting the correct injector to use for creating components.
  2007. */
  2008. function getClosestRouteInjector(snapshot) {
  2009. if (!snapshot)
  2010. return null;
  2011. // If the current route has its own injector, which is created from the static providers on the
  2012. // route itself, we should use that. Otherwise, we start at the parent since we do not want to
  2013. // include the lazy loaded injector from this route.
  2014. if (snapshot.routeConfig?._injector) {
  2015. return snapshot.routeConfig._injector;
  2016. }
  2017. for (let s = snapshot.parent; s; s = s.parent) {
  2018. const route = s.routeConfig;
  2019. // Note that the order here is important. `_loadedInjector` stored on the route with
  2020. // `loadChildren: () => NgModule` so it applies to child routes with priority. The `_injector`
  2021. // is created from the static providers on that parent route, so it applies to the children as
  2022. // well, but only if there is no lazy loaded NgModuleRef injector.
  2023. if (route?._loadedInjector)
  2024. return route._loadedInjector;
  2025. if (route?._injector)
  2026. return route._injector;
  2027. }
  2028. return null;
  2029. }
  2030. /**
  2031. * Store contextual information about a `RouterOutlet`
  2032. *
  2033. * @publicApi
  2034. */
  2035. class OutletContext {
  2036. rootInjector;
  2037. outlet = null;
  2038. route = null;
  2039. children;
  2040. attachRef = null;
  2041. get injector() {
  2042. return getClosestRouteInjector(this.route?.snapshot) ?? this.rootInjector;
  2043. }
  2044. constructor(rootInjector) {
  2045. this.rootInjector = rootInjector;
  2046. this.children = new ChildrenOutletContexts(this.rootInjector);
  2047. }
  2048. }
  2049. /**
  2050. * Store contextual information about the children (= nested) `RouterOutlet`
  2051. *
  2052. * @publicApi
  2053. */
  2054. class ChildrenOutletContexts {
  2055. rootInjector;
  2056. // contexts for child outlets, by name.
  2057. contexts = new Map();
  2058. /** @nodoc */
  2059. constructor(rootInjector) {
  2060. this.rootInjector = rootInjector;
  2061. }
  2062. /** Called when a `RouterOutlet` directive is instantiated */
  2063. onChildOutletCreated(childName, outlet) {
  2064. const context = this.getOrCreateContext(childName);
  2065. context.outlet = outlet;
  2066. this.contexts.set(childName, context);
  2067. }
  2068. /**
  2069. * Called when a `RouterOutlet` directive is destroyed.
  2070. * We need to keep the context as the outlet could be destroyed inside a NgIf and might be
  2071. * re-created later.
  2072. */
  2073. onChildOutletDestroyed(childName) {
  2074. const context = this.getContext(childName);
  2075. if (context) {
  2076. context.outlet = null;
  2077. context.attachRef = null;
  2078. }
  2079. }
  2080. /**
  2081. * Called when the corresponding route is deactivated during navigation.
  2082. * Because the component get destroyed, all children outlet are destroyed.
  2083. */
  2084. onOutletDeactivated() {
  2085. const contexts = this.contexts;
  2086. this.contexts = new Map();
  2087. return contexts;
  2088. }
  2089. onOutletReAttached(contexts) {
  2090. this.contexts = contexts;
  2091. }
  2092. getOrCreateContext(childName) {
  2093. let context = this.getContext(childName);
  2094. if (!context) {
  2095. context = new OutletContext(this.rootInjector);
  2096. this.contexts.set(childName, context);
  2097. }
  2098. return context;
  2099. }
  2100. getContext(childName) {
  2101. return this.contexts.get(childName) || null;
  2102. }
  2103. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: ChildrenOutletContexts, deps: [{ token: i0.EnvironmentInjector }], target: i0.ɵɵFactoryTarget.Injectable });
  2104. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: ChildrenOutletContexts, providedIn: 'root' });
  2105. }
  2106. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: ChildrenOutletContexts, decorators: [{
  2107. type: Injectable,
  2108. args: [{ providedIn: 'root' }]
  2109. }], ctorParameters: () => [{ type: i0.EnvironmentInjector }] });
  2110. class Tree {
  2111. /** @internal */
  2112. _root;
  2113. constructor(root) {
  2114. this._root = root;
  2115. }
  2116. get root() {
  2117. return this._root.value;
  2118. }
  2119. /**
  2120. * @internal
  2121. */
  2122. parent(t) {
  2123. const p = this.pathFromRoot(t);
  2124. return p.length > 1 ? p[p.length - 2] : null;
  2125. }
  2126. /**
  2127. * @internal
  2128. */
  2129. children(t) {
  2130. const n = findNode(t, this._root);
  2131. return n ? n.children.map((t) => t.value) : [];
  2132. }
  2133. /**
  2134. * @internal
  2135. */
  2136. firstChild(t) {
  2137. const n = findNode(t, this._root);
  2138. return n && n.children.length > 0 ? n.children[0].value : null;
  2139. }
  2140. /**
  2141. * @internal
  2142. */
  2143. siblings(t) {
  2144. const p = findPath(t, this._root);
  2145. if (p.length < 2)
  2146. return [];
  2147. const c = p[p.length - 2].children.map((c) => c.value);
  2148. return c.filter((cc) => cc !== t);
  2149. }
  2150. /**
  2151. * @internal
  2152. */
  2153. pathFromRoot(t) {
  2154. return findPath(t, this._root).map((s) => s.value);
  2155. }
  2156. }
  2157. // DFS for the node matching the value
  2158. function findNode(value, node) {
  2159. if (value === node.value)
  2160. return node;
  2161. for (const child of node.children) {
  2162. const node = findNode(value, child);
  2163. if (node)
  2164. return node;
  2165. }
  2166. return null;
  2167. }
  2168. // Return the path to the node with the given value using DFS
  2169. function findPath(value, node) {
  2170. if (value === node.value)
  2171. return [node];
  2172. for (const child of node.children) {
  2173. const path = findPath(value, child);
  2174. if (path.length) {
  2175. path.unshift(node);
  2176. return path;
  2177. }
  2178. }
  2179. return [];
  2180. }
  2181. class TreeNode {
  2182. value;
  2183. children;
  2184. constructor(value, children) {
  2185. this.value = value;
  2186. this.children = children;
  2187. }
  2188. toString() {
  2189. return `TreeNode(${this.value})`;
  2190. }
  2191. }
  2192. // Return the list of T indexed by outlet name
  2193. function nodeChildrenAsMap(node) {
  2194. const map = {};
  2195. if (node) {
  2196. node.children.forEach((child) => (map[child.value.outlet] = child));
  2197. }
  2198. return map;
  2199. }
  2200. /**
  2201. * Represents the state of the router as a tree of activated routes.
  2202. *
  2203. * @usageNotes
  2204. *
  2205. * Every node in the route tree is an `ActivatedRoute` instance
  2206. * that knows about the "consumed" URL segments, the extracted parameters,
  2207. * and the resolved data.
  2208. * Use the `ActivatedRoute` properties to traverse the tree from any node.
  2209. *
  2210. * The following fragment shows how a component gets the root node
  2211. * of the current state to establish its own route tree:
  2212. *
  2213. * ```ts
  2214. * @Component({templateUrl:'template.html'})
  2215. * class MyComponent {
  2216. * constructor(router: Router) {
  2217. * const state: RouterState = router.routerState;
  2218. * const root: ActivatedRoute = state.root;
  2219. * const child = root.firstChild;
  2220. * const id: Observable<string> = child.params.map(p => p.id);
  2221. * //...
  2222. * }
  2223. * }
  2224. * ```
  2225. *
  2226. * @see {@link ActivatedRoute}
  2227. * @see [Getting route information](guide/routing/common-router-tasks#getting-route-information)
  2228. *
  2229. * @publicApi
  2230. */
  2231. class RouterState extends Tree {
  2232. snapshot;
  2233. /** @internal */
  2234. constructor(root,
  2235. /** The current snapshot of the router state */
  2236. snapshot) {
  2237. super(root);
  2238. this.snapshot = snapshot;
  2239. setRouterState(this, root);
  2240. }
  2241. toString() {
  2242. return this.snapshot.toString();
  2243. }
  2244. }
  2245. function createEmptyState(rootComponent) {
  2246. const snapshot = createEmptyStateSnapshot(rootComponent);
  2247. const emptyUrl = new BehaviorSubject([new UrlSegment('', {})]);
  2248. const emptyParams = new BehaviorSubject({});
  2249. const emptyData = new BehaviorSubject({});
  2250. const emptyQueryParams = new BehaviorSubject({});
  2251. const fragment = new BehaviorSubject('');
  2252. const activated = new ActivatedRoute(emptyUrl, emptyParams, emptyQueryParams, fragment, emptyData, PRIMARY_OUTLET, rootComponent, snapshot.root);
  2253. activated.snapshot = snapshot.root;
  2254. return new RouterState(new TreeNode(activated, []), snapshot);
  2255. }
  2256. function createEmptyStateSnapshot(rootComponent) {
  2257. const emptyParams = {};
  2258. const emptyData = {};
  2259. const emptyQueryParams = {};
  2260. const fragment = '';
  2261. const activated = new ActivatedRouteSnapshot([], emptyParams, emptyQueryParams, fragment, emptyData, PRIMARY_OUTLET, rootComponent, null, {});
  2262. return new RouterStateSnapshot('', new TreeNode(activated, []));
  2263. }
  2264. /**
  2265. * Provides access to information about a route associated with a component
  2266. * that is loaded in an outlet.
  2267. * Use to traverse the `RouterState` tree and extract information from nodes.
  2268. *
  2269. * The following example shows how to construct a component using information from a
  2270. * currently activated route.
  2271. *
  2272. * Note: the observables in this class only emit when the current and previous values differ based
  2273. * on shallow equality. For example, changing deeply nested properties in resolved `data` will not
  2274. * cause the `ActivatedRoute.data` `Observable` to emit a new value.
  2275. *
  2276. * {@example router/activated-route/module.ts region="activated-route"
  2277. * header="activated-route.component.ts"}
  2278. *
  2279. * @see [Getting route information](guide/routing/common-router-tasks#getting-route-information)
  2280. *
  2281. * @publicApi
  2282. */
  2283. class ActivatedRoute {
  2284. urlSubject;
  2285. paramsSubject;
  2286. queryParamsSubject;
  2287. fragmentSubject;
  2288. dataSubject;
  2289. outlet;
  2290. component;
  2291. /** The current snapshot of this route */
  2292. snapshot;
  2293. /** @internal */
  2294. _futureSnapshot;
  2295. /** @internal */
  2296. _routerState;
  2297. /** @internal */
  2298. _paramMap;
  2299. /** @internal */
  2300. _queryParamMap;
  2301. /** An Observable of the resolved route title */
  2302. title;
  2303. /** An observable of the URL segments matched by this route. */
  2304. url;
  2305. /** An observable of the matrix parameters scoped to this route. */
  2306. params;
  2307. /** An observable of the query parameters shared by all the routes. */
  2308. queryParams;
  2309. /** An observable of the URL fragment shared by all the routes. */
  2310. fragment;
  2311. /** An observable of the static and resolved data of this route. */
  2312. data;
  2313. /** @internal */
  2314. constructor(
  2315. /** @internal */
  2316. urlSubject,
  2317. /** @internal */
  2318. paramsSubject,
  2319. /** @internal */
  2320. queryParamsSubject,
  2321. /** @internal */
  2322. fragmentSubject,
  2323. /** @internal */
  2324. dataSubject,
  2325. /** The outlet name of the route, a constant. */
  2326. outlet,
  2327. /** The component of the route, a constant. */
  2328. component, futureSnapshot) {
  2329. this.urlSubject = urlSubject;
  2330. this.paramsSubject = paramsSubject;
  2331. this.queryParamsSubject = queryParamsSubject;
  2332. this.fragmentSubject = fragmentSubject;
  2333. this.dataSubject = dataSubject;
  2334. this.outlet = outlet;
  2335. this.component = component;
  2336. this._futureSnapshot = futureSnapshot;
  2337. this.title = this.dataSubject?.pipe(map((d) => d[RouteTitleKey])) ?? of(undefined);
  2338. // TODO(atscott): Verify that these can be changed to `.asObservable()` with TGP.
  2339. this.url = urlSubject;
  2340. this.params = paramsSubject;
  2341. this.queryParams = queryParamsSubject;
  2342. this.fragment = fragmentSubject;
  2343. this.data = dataSubject;
  2344. }
  2345. /** The configuration used to match this route. */
  2346. get routeConfig() {
  2347. return this._futureSnapshot.routeConfig;
  2348. }
  2349. /** The root of the router state. */
  2350. get root() {
  2351. return this._routerState.root;
  2352. }
  2353. /** The parent of this route in the router state tree. */
  2354. get parent() {
  2355. return this._routerState.parent(this);
  2356. }
  2357. /** The first child of this route in the router state tree. */
  2358. get firstChild() {
  2359. return this._routerState.firstChild(this);
  2360. }
  2361. /** The children of this route in the router state tree. */
  2362. get children() {
  2363. return this._routerState.children(this);
  2364. }
  2365. /** The path from the root of the router state tree to this route. */
  2366. get pathFromRoot() {
  2367. return this._routerState.pathFromRoot(this);
  2368. }
  2369. /**
  2370. * An Observable that contains a map of the required and optional parameters
  2371. * specific to the route.
  2372. * The map supports retrieving single and multiple values from the same parameter.
  2373. */
  2374. get paramMap() {
  2375. this._paramMap ??= this.params.pipe(map((p) => convertToParamMap(p)));
  2376. return this._paramMap;
  2377. }
  2378. /**
  2379. * An Observable that contains a map of the query parameters available to all routes.
  2380. * The map supports retrieving single and multiple values from the query parameter.
  2381. */
  2382. get queryParamMap() {
  2383. this._queryParamMap ??= this.queryParams.pipe(map((p) => convertToParamMap(p)));
  2384. return this._queryParamMap;
  2385. }
  2386. toString() {
  2387. return this.snapshot ? this.snapshot.toString() : `Future(${this._futureSnapshot})`;
  2388. }
  2389. }
  2390. /**
  2391. * Returns the inherited params, data, and resolve for a given route.
  2392. *
  2393. * By default, we do not inherit parent data unless the current route is path-less or the parent
  2394. * route is component-less.
  2395. */
  2396. function getInherited(route, parent, paramsInheritanceStrategy = 'emptyOnly') {
  2397. let inherited;
  2398. const { routeConfig } = route;
  2399. if (parent !== null &&
  2400. (paramsInheritanceStrategy === 'always' ||
  2401. // inherit parent data if route is empty path
  2402. routeConfig?.path === '' ||
  2403. // inherit parent data if parent was componentless
  2404. (!parent.component && !parent.routeConfig?.loadComponent))) {
  2405. inherited = {
  2406. params: { ...parent.params, ...route.params },
  2407. data: { ...parent.data, ...route.data },
  2408. resolve: {
  2409. // Snapshots are created with data inherited from parent and guards (i.e. canActivate) can
  2410. // change data because it's not frozen...
  2411. // This first line could be deleted chose to break/disallow mutating the `data` object in
  2412. // guards.
  2413. // Note that data from parents still override this mutated data so anyone relying on this
  2414. // might be surprised that it doesn't work if parent data is inherited but otherwise does.
  2415. ...route.data,
  2416. // Ensure inherited resolved data overrides inherited static data
  2417. ...parent.data,
  2418. // static data from the current route overrides any inherited data
  2419. ...routeConfig?.data,
  2420. // resolved data from current route overrides everything
  2421. ...route._resolvedData,
  2422. },
  2423. };
  2424. }
  2425. else {
  2426. inherited = {
  2427. params: { ...route.params },
  2428. data: { ...route.data },
  2429. resolve: { ...route.data, ...(route._resolvedData ?? {}) },
  2430. };
  2431. }
  2432. if (routeConfig && hasStaticTitle(routeConfig)) {
  2433. inherited.resolve[RouteTitleKey] = routeConfig.title;
  2434. }
  2435. return inherited;
  2436. }
  2437. /**
  2438. * @description
  2439. *
  2440. * Contains the information about a route associated with a component loaded in an
  2441. * outlet at a particular moment in time. ActivatedRouteSnapshot can also be used to
  2442. * traverse the router state tree.
  2443. *
  2444. * The following example initializes a component with route information extracted
  2445. * from the snapshot of the root node at the time of creation.
  2446. *
  2447. * ```ts
  2448. * @Component({templateUrl:'./my-component.html'})
  2449. * class MyComponent {
  2450. * constructor(route: ActivatedRoute) {
  2451. * const id: string = route.snapshot.params.id;
  2452. * const url: string = route.snapshot.url.join('');
  2453. * const user = route.snapshot.data.user;
  2454. * }
  2455. * }
  2456. * ```
  2457. *
  2458. * @publicApi
  2459. */
  2460. class ActivatedRouteSnapshot {
  2461. url;
  2462. params;
  2463. queryParams;
  2464. fragment;
  2465. data;
  2466. outlet;
  2467. component;
  2468. /** The configuration used to match this route **/
  2469. routeConfig;
  2470. /** @internal */
  2471. _resolve;
  2472. /** @internal */
  2473. _resolvedData;
  2474. /** @internal */
  2475. _routerState;
  2476. /** @internal */
  2477. _paramMap;
  2478. /** @internal */
  2479. _queryParamMap;
  2480. /** The resolved route title */
  2481. get title() {
  2482. // Note: This _must_ be a getter because the data is mutated in the resolvers. Title will not be
  2483. // available at the time of class instantiation.
  2484. return this.data?.[RouteTitleKey];
  2485. }
  2486. /** @internal */
  2487. constructor(
  2488. /** The URL segments matched by this route */
  2489. url,
  2490. /**
  2491. * The matrix parameters scoped to this route.
  2492. *
  2493. * You can compute all params (or data) in the router state or to get params outside
  2494. * of an activated component by traversing the `RouterState` tree as in the following
  2495. * example:
  2496. * ```ts
  2497. * collectRouteParams(router: Router) {
  2498. * let params = {};
  2499. * let stack: ActivatedRouteSnapshot[] = [router.routerState.snapshot.root];
  2500. * while (stack.length > 0) {
  2501. * const route = stack.pop()!;
  2502. * params = {...params, ...route.params};
  2503. * stack.push(...route.children);
  2504. * }
  2505. * return params;
  2506. * }
  2507. * ```
  2508. */
  2509. params,
  2510. /** The query parameters shared by all the routes */
  2511. queryParams,
  2512. /** The URL fragment shared by all the routes */
  2513. fragment,
  2514. /** The static and resolved data of this route */
  2515. data,
  2516. /** The outlet name of the route */
  2517. outlet,
  2518. /** The component of the route */
  2519. component, routeConfig, resolve) {
  2520. this.url = url;
  2521. this.params = params;
  2522. this.queryParams = queryParams;
  2523. this.fragment = fragment;
  2524. this.data = data;
  2525. this.outlet = outlet;
  2526. this.component = component;
  2527. this.routeConfig = routeConfig;
  2528. this._resolve = resolve;
  2529. }
  2530. /** The root of the router state */
  2531. get root() {
  2532. return this._routerState.root;
  2533. }
  2534. /** The parent of this route in the router state tree */
  2535. get parent() {
  2536. return this._routerState.parent(this);
  2537. }
  2538. /** The first child of this route in the router state tree */
  2539. get firstChild() {
  2540. return this._routerState.firstChild(this);
  2541. }
  2542. /** The children of this route in the router state tree */
  2543. get children() {
  2544. return this._routerState.children(this);
  2545. }
  2546. /** The path from the root of the router state tree to this route */
  2547. get pathFromRoot() {
  2548. return this._routerState.pathFromRoot(this);
  2549. }
  2550. get paramMap() {
  2551. this._paramMap ??= convertToParamMap(this.params);
  2552. return this._paramMap;
  2553. }
  2554. get queryParamMap() {
  2555. this._queryParamMap ??= convertToParamMap(this.queryParams);
  2556. return this._queryParamMap;
  2557. }
  2558. toString() {
  2559. const url = this.url.map((segment) => segment.toString()).join('/');
  2560. const matched = this.routeConfig ? this.routeConfig.path : '';
  2561. return `Route(url:'${url}', path:'${matched}')`;
  2562. }
  2563. }
  2564. /**
  2565. * @description
  2566. *
  2567. * Represents the state of the router at a moment in time.
  2568. *
  2569. * This is a tree of activated route snapshots. Every node in this tree knows about
  2570. * the "consumed" URL segments, the extracted parameters, and the resolved data.
  2571. *
  2572. * The following example shows how a component is initialized with information
  2573. * from the snapshot of the root node's state at the time of creation.
  2574. *
  2575. * ```ts
  2576. * @Component({templateUrl:'template.html'})
  2577. * class MyComponent {
  2578. * constructor(router: Router) {
  2579. * const state: RouterState = router.routerState;
  2580. * const snapshot: RouterStateSnapshot = state.snapshot;
  2581. * const root: ActivatedRouteSnapshot = snapshot.root;
  2582. * const child = root.firstChild;
  2583. * const id: Observable<string> = child.params.map(p => p.id);
  2584. * //...
  2585. * }
  2586. * }
  2587. * ```
  2588. *
  2589. * @publicApi
  2590. */
  2591. class RouterStateSnapshot extends Tree {
  2592. url;
  2593. /** @internal */
  2594. constructor(
  2595. /** The url from which this snapshot was created */
  2596. url, root) {
  2597. super(root);
  2598. this.url = url;
  2599. setRouterState(this, root);
  2600. }
  2601. toString() {
  2602. return serializeNode(this._root);
  2603. }
  2604. }
  2605. function setRouterState(state, node) {
  2606. node.value._routerState = state;
  2607. node.children.forEach((c) => setRouterState(state, c));
  2608. }
  2609. function serializeNode(node) {
  2610. const c = node.children.length > 0 ? ` { ${node.children.map(serializeNode).join(', ')} } ` : '';
  2611. return `${node.value}${c}`;
  2612. }
  2613. /**
  2614. * The expectation is that the activate route is created with the right set of parameters.
  2615. * So we push new values into the observables only when they are not the initial values.
  2616. * And we detect that by checking if the snapshot field is set.
  2617. */
  2618. function advanceActivatedRoute(route) {
  2619. if (route.snapshot) {
  2620. const currentSnapshot = route.snapshot;
  2621. const nextSnapshot = route._futureSnapshot;
  2622. route.snapshot = nextSnapshot;
  2623. if (!shallowEqual(currentSnapshot.queryParams, nextSnapshot.queryParams)) {
  2624. route.queryParamsSubject.next(nextSnapshot.queryParams);
  2625. }
  2626. if (currentSnapshot.fragment !== nextSnapshot.fragment) {
  2627. route.fragmentSubject.next(nextSnapshot.fragment);
  2628. }
  2629. if (!shallowEqual(currentSnapshot.params, nextSnapshot.params)) {
  2630. route.paramsSubject.next(nextSnapshot.params);
  2631. }
  2632. if (!shallowEqualArrays(currentSnapshot.url, nextSnapshot.url)) {
  2633. route.urlSubject.next(nextSnapshot.url);
  2634. }
  2635. if (!shallowEqual(currentSnapshot.data, nextSnapshot.data)) {
  2636. route.dataSubject.next(nextSnapshot.data);
  2637. }
  2638. }
  2639. else {
  2640. route.snapshot = route._futureSnapshot;
  2641. // this is for resolved data
  2642. route.dataSubject.next(route._futureSnapshot.data);
  2643. }
  2644. }
  2645. function equalParamsAndUrlSegments(a, b) {
  2646. const equalUrlParams = shallowEqual(a.params, b.params) && equalSegments(a.url, b.url);
  2647. const parentsMismatch = !a.parent !== !b.parent;
  2648. return (equalUrlParams &&
  2649. !parentsMismatch &&
  2650. (!a.parent || equalParamsAndUrlSegments(a.parent, b.parent)));
  2651. }
  2652. function hasStaticTitle(config) {
  2653. return typeof config.title === 'string' || config.title === null;
  2654. }
  2655. /**
  2656. * An `InjectionToken` provided by the `RouterOutlet` and can be set using the `routerOutletData`
  2657. * input.
  2658. *
  2659. * When unset, this value is `null` by default.
  2660. *
  2661. * @usageNotes
  2662. *
  2663. * To set the data from the template of the component with `router-outlet`:
  2664. * ```html
  2665. * <router-outlet [routerOutletData]="{name: 'Angular'}" />
  2666. * ```
  2667. *
  2668. * To read the data in the routed component:
  2669. * ```ts
  2670. * data = inject(ROUTER_OUTLET_DATA) as Signal<{name: string}>;
  2671. * ```
  2672. *
  2673. * @publicApi
  2674. */
  2675. const ROUTER_OUTLET_DATA = new InjectionToken(ngDevMode ? 'RouterOutlet data' : '');
  2676. /**
  2677. * @description
  2678. *
  2679. * Acts as a placeholder that Angular dynamically fills based on the current router state.
  2680. *
  2681. * Each outlet can have a unique name, determined by the optional `name` attribute.
  2682. * The name cannot be set or changed dynamically. If not set, default value is "primary".
  2683. *
  2684. * ```html
  2685. * <router-outlet></router-outlet>
  2686. * <router-outlet name='left'></router-outlet>
  2687. * <router-outlet name='right'></router-outlet>
  2688. * ```
  2689. *
  2690. * Named outlets can be the targets of secondary routes.
  2691. * The `Route` object for a secondary route has an `outlet` property to identify the target outlet:
  2692. *
  2693. * `{path: <base-path>, component: <component>, outlet: <target_outlet_name>}`
  2694. *
  2695. * Using named outlets and secondary routes, you can target multiple outlets in
  2696. * the same `RouterLink` directive.
  2697. *
  2698. * The router keeps track of separate branches in a navigation tree for each named outlet and
  2699. * generates a representation of that tree in the URL.
  2700. * The URL for a secondary route uses the following syntax to specify both the primary and secondary
  2701. * routes at the same time:
  2702. *
  2703. * `http://base-path/primary-route-path(outlet-name:route-path)`
  2704. *
  2705. * A router outlet emits an activate event when a new component is instantiated,
  2706. * deactivate event when a component is destroyed.
  2707. * An attached event emits when the `RouteReuseStrategy` instructs the outlet to reattach the
  2708. * subtree, and the detached event emits when the `RouteReuseStrategy` instructs the outlet to
  2709. * detach the subtree.
  2710. *
  2711. * ```html
  2712. * <router-outlet
  2713. * (activate)='onActivate($event)'
  2714. * (deactivate)='onDeactivate($event)'
  2715. * (attach)='onAttach($event)'
  2716. * (detach)='onDetach($event)'></router-outlet>
  2717. * ```
  2718. *
  2719. * @see {@link RouterLink}
  2720. * @see {@link Route}
  2721. * @ngModule RouterModule
  2722. *
  2723. * @publicApi
  2724. */
  2725. class RouterOutlet {
  2726. activated = null;
  2727. /** @internal */
  2728. get activatedComponentRef() {
  2729. return this.activated;
  2730. }
  2731. _activatedRoute = null;
  2732. /**
  2733. * The name of the outlet
  2734. *
  2735. */
  2736. name = PRIMARY_OUTLET;
  2737. activateEvents = new EventEmitter();
  2738. deactivateEvents = new EventEmitter();
  2739. /**
  2740. * Emits an attached component instance when the `RouteReuseStrategy` instructs to re-attach a
  2741. * previously detached subtree.
  2742. **/
  2743. attachEvents = new EventEmitter();
  2744. /**
  2745. * Emits a detached component instance when the `RouteReuseStrategy` instructs to detach the
  2746. * subtree.
  2747. */
  2748. detachEvents = new EventEmitter();
  2749. /**
  2750. * Data that will be provided to the child injector through the `ROUTER_OUTLET_DATA` token.
  2751. *
  2752. * When unset, the value of the token is `undefined` by default.
  2753. */
  2754. routerOutletData = input(undefined);
  2755. parentContexts = inject(ChildrenOutletContexts);
  2756. location = inject(ViewContainerRef);
  2757. changeDetector = inject(ChangeDetectorRef);
  2758. inputBinder = inject(INPUT_BINDER, { optional: true });
  2759. /** @nodoc */
  2760. supportsBindingToComponentInputs = true;
  2761. /** @nodoc */
  2762. ngOnChanges(changes) {
  2763. if (changes['name']) {
  2764. const { firstChange, previousValue } = changes['name'];
  2765. if (firstChange) {
  2766. // The first change is handled by ngOnInit. Because ngOnChanges doesn't get called when no
  2767. // input is set at all, we need to centrally handle the first change there.
  2768. return;
  2769. }
  2770. // unregister with the old name
  2771. if (this.isTrackedInParentContexts(previousValue)) {
  2772. this.deactivate();
  2773. this.parentContexts.onChildOutletDestroyed(previousValue);
  2774. }
  2775. // register the new name
  2776. this.initializeOutletWithName();
  2777. }
  2778. }
  2779. /** @nodoc */
  2780. ngOnDestroy() {
  2781. // Ensure that the registered outlet is this one before removing it on the context.
  2782. if (this.isTrackedInParentContexts(this.name)) {
  2783. this.parentContexts.onChildOutletDestroyed(this.name);
  2784. }
  2785. this.inputBinder?.unsubscribeFromRouteData(this);
  2786. }
  2787. isTrackedInParentContexts(outletName) {
  2788. return this.parentContexts.getContext(outletName)?.outlet === this;
  2789. }
  2790. /** @nodoc */
  2791. ngOnInit() {
  2792. this.initializeOutletWithName();
  2793. }
  2794. initializeOutletWithName() {
  2795. this.parentContexts.onChildOutletCreated(this.name, this);
  2796. if (this.activated) {
  2797. return;
  2798. }
  2799. // If the outlet was not instantiated at the time the route got activated we need to populate
  2800. // the outlet when it is initialized (ie inside a NgIf)
  2801. const context = this.parentContexts.getContext(this.name);
  2802. if (context?.route) {
  2803. if (context.attachRef) {
  2804. // `attachRef` is populated when there is an existing component to mount
  2805. this.attach(context.attachRef, context.route);
  2806. }
  2807. else {
  2808. // otherwise the component defined in the configuration is created
  2809. this.activateWith(context.route, context.injector);
  2810. }
  2811. }
  2812. }
  2813. get isActivated() {
  2814. return !!this.activated;
  2815. }
  2816. /**
  2817. * @returns The currently activated component instance.
  2818. * @throws An error if the outlet is not activated.
  2819. */
  2820. get component() {
  2821. if (!this.activated)
  2822. throw new _RuntimeError(4012 /* RuntimeErrorCode.OUTLET_NOT_ACTIVATED */, (typeof ngDevMode === 'undefined' || ngDevMode) && 'Outlet is not activated');
  2823. return this.activated.instance;
  2824. }
  2825. get activatedRoute() {
  2826. if (!this.activated)
  2827. throw new _RuntimeError(4012 /* RuntimeErrorCode.OUTLET_NOT_ACTIVATED */, (typeof ngDevMode === 'undefined' || ngDevMode) && 'Outlet is not activated');
  2828. return this._activatedRoute;
  2829. }
  2830. get activatedRouteData() {
  2831. if (this._activatedRoute) {
  2832. return this._activatedRoute.snapshot.data;
  2833. }
  2834. return {};
  2835. }
  2836. /**
  2837. * Called when the `RouteReuseStrategy` instructs to detach the subtree
  2838. */
  2839. detach() {
  2840. if (!this.activated)
  2841. throw new _RuntimeError(4012 /* RuntimeErrorCode.OUTLET_NOT_ACTIVATED */, (typeof ngDevMode === 'undefined' || ngDevMode) && 'Outlet is not activated');
  2842. this.location.detach();
  2843. const cmp = this.activated;
  2844. this.activated = null;
  2845. this._activatedRoute = null;
  2846. this.detachEvents.emit(cmp.instance);
  2847. return cmp;
  2848. }
  2849. /**
  2850. * Called when the `RouteReuseStrategy` instructs to re-attach a previously detached subtree
  2851. */
  2852. attach(ref, activatedRoute) {
  2853. this.activated = ref;
  2854. this._activatedRoute = activatedRoute;
  2855. this.location.insert(ref.hostView);
  2856. this.inputBinder?.bindActivatedRouteToOutletComponent(this);
  2857. this.attachEvents.emit(ref.instance);
  2858. }
  2859. deactivate() {
  2860. if (this.activated) {
  2861. const c = this.component;
  2862. this.activated.destroy();
  2863. this.activated = null;
  2864. this._activatedRoute = null;
  2865. this.deactivateEvents.emit(c);
  2866. }
  2867. }
  2868. activateWith(activatedRoute, environmentInjector) {
  2869. if (this.isActivated) {
  2870. throw new _RuntimeError(4013 /* RuntimeErrorCode.OUTLET_ALREADY_ACTIVATED */, (typeof ngDevMode === 'undefined' || ngDevMode) &&
  2871. 'Cannot activate an already activated outlet');
  2872. }
  2873. this._activatedRoute = activatedRoute;
  2874. const location = this.location;
  2875. const snapshot = activatedRoute.snapshot;
  2876. const component = snapshot.component;
  2877. const childContexts = this.parentContexts.getOrCreateContext(this.name).children;
  2878. const injector = new OutletInjector(activatedRoute, childContexts, location.injector, this.routerOutletData);
  2879. this.activated = location.createComponent(component, {
  2880. index: location.length,
  2881. injector,
  2882. environmentInjector: environmentInjector,
  2883. });
  2884. // Calling `markForCheck` to make sure we will run the change detection when the
  2885. // `RouterOutlet` is inside a `ChangeDetectionStrategy.OnPush` component.
  2886. this.changeDetector.markForCheck();
  2887. this.inputBinder?.bindActivatedRouteToOutletComponent(this);
  2888. this.activateEvents.emit(this.activated.instance);
  2889. }
  2890. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterOutlet, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  2891. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "19.2.4", type: RouterOutlet, isStandalone: true, selector: "router-outlet", inputs: { name: { classPropertyName: "name", publicName: "name", isSignal: false, isRequired: false, transformFunction: null }, routerOutletData: { classPropertyName: "routerOutletData", publicName: "routerOutletData", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { activateEvents: "activate", deactivateEvents: "deactivate", attachEvents: "attach", detachEvents: "detach" }, exportAs: ["outlet"], usesOnChanges: true, ngImport: i0 });
  2892. }
  2893. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterOutlet, decorators: [{
  2894. type: Directive,
  2895. args: [{
  2896. selector: 'router-outlet',
  2897. exportAs: 'outlet',
  2898. }]
  2899. }], propDecorators: { name: [{
  2900. type: Input
  2901. }], activateEvents: [{
  2902. type: Output,
  2903. args: ['activate']
  2904. }], deactivateEvents: [{
  2905. type: Output,
  2906. args: ['deactivate']
  2907. }], attachEvents: [{
  2908. type: Output,
  2909. args: ['attach']
  2910. }], detachEvents: [{
  2911. type: Output,
  2912. args: ['detach']
  2913. }] } });
  2914. class OutletInjector {
  2915. route;
  2916. childContexts;
  2917. parent;
  2918. outletData;
  2919. /**
  2920. * This injector has a special handing for the `ActivatedRoute` and
  2921. * `ChildrenOutletContexts` tokens: it returns corresponding values for those
  2922. * tokens dynamically. This behavior is different from the regular injector logic,
  2923. * when we initialize and store a value, which is later returned for all inject
  2924. * requests.
  2925. *
  2926. * In some cases (e.g. when using `@defer`), this dynamic behavior requires special
  2927. * handling. This function allows to identify an instance of the `OutletInjector` and
  2928. * create an instance of it without referring to the class itself (so this logic can
  2929. * be invoked from the `core` package). This helps to retain dynamic behavior for the
  2930. * mentioned tokens.
  2931. *
  2932. * Note: it's a temporary solution and we should explore how to support this case better.
  2933. */
  2934. __ngOutletInjector(parentInjector) {
  2935. return new OutletInjector(this.route, this.childContexts, parentInjector, this.outletData);
  2936. }
  2937. constructor(route, childContexts, parent, outletData) {
  2938. this.route = route;
  2939. this.childContexts = childContexts;
  2940. this.parent = parent;
  2941. this.outletData = outletData;
  2942. }
  2943. get(token, notFoundValue) {
  2944. if (token === ActivatedRoute) {
  2945. return this.route;
  2946. }
  2947. if (token === ChildrenOutletContexts) {
  2948. return this.childContexts;
  2949. }
  2950. if (token === ROUTER_OUTLET_DATA) {
  2951. return this.outletData;
  2952. }
  2953. return this.parent.get(token, notFoundValue);
  2954. }
  2955. }
  2956. const INPUT_BINDER = new InjectionToken('');
  2957. /**
  2958. * Injectable used as a tree-shakable provider for opting in to binding router data to component
  2959. * inputs.
  2960. *
  2961. * The RouterOutlet registers itself with this service when an `ActivatedRoute` is attached or
  2962. * activated. When this happens, the service subscribes to the `ActivatedRoute` observables (params,
  2963. * queryParams, data) and sets the inputs of the component using `ComponentRef.setInput`.
  2964. * Importantly, when an input does not have an item in the route data with a matching key, this
  2965. * input is set to `undefined`. If it were not done this way, the previous information would be
  2966. * retained if the data got removed from the route (i.e. if a query parameter is removed).
  2967. *
  2968. * The `RouterOutlet` should unregister itself when destroyed via `unsubscribeFromRouteData` so that
  2969. * the subscriptions are cleaned up.
  2970. */
  2971. class RoutedComponentInputBinder {
  2972. outletDataSubscriptions = new Map();
  2973. bindActivatedRouteToOutletComponent(outlet) {
  2974. this.unsubscribeFromRouteData(outlet);
  2975. this.subscribeToRouteData(outlet);
  2976. }
  2977. unsubscribeFromRouteData(outlet) {
  2978. this.outletDataSubscriptions.get(outlet)?.unsubscribe();
  2979. this.outletDataSubscriptions.delete(outlet);
  2980. }
  2981. subscribeToRouteData(outlet) {
  2982. const { activatedRoute } = outlet;
  2983. const dataSubscription = combineLatest([
  2984. activatedRoute.queryParams,
  2985. activatedRoute.params,
  2986. activatedRoute.data,
  2987. ])
  2988. .pipe(switchMap(([queryParams, params, data], index) => {
  2989. data = { ...queryParams, ...params, ...data };
  2990. // Get the first result from the data subscription synchronously so it's available to
  2991. // the component as soon as possible (and doesn't require a second change detection).
  2992. if (index === 0) {
  2993. return of(data);
  2994. }
  2995. // Promise.resolve is used to avoid synchronously writing the wrong data when
  2996. // two of the Observables in the `combineLatest` stream emit one after
  2997. // another.
  2998. return Promise.resolve(data);
  2999. }))
  3000. .subscribe((data) => {
  3001. // Outlet may have been deactivated or changed names to be associated with a different
  3002. // route
  3003. if (!outlet.isActivated ||
  3004. !outlet.activatedComponentRef ||
  3005. outlet.activatedRoute !== activatedRoute ||
  3006. activatedRoute.component === null) {
  3007. this.unsubscribeFromRouteData(outlet);
  3008. return;
  3009. }
  3010. const mirror = reflectComponentType(activatedRoute.component);
  3011. if (!mirror) {
  3012. this.unsubscribeFromRouteData(outlet);
  3013. return;
  3014. }
  3015. for (const { templateName } of mirror.inputs) {
  3016. outlet.activatedComponentRef.setInput(templateName, data[templateName]);
  3017. }
  3018. });
  3019. this.outletDataSubscriptions.set(outlet, dataSubscription);
  3020. }
  3021. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RoutedComponentInputBinder, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  3022. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RoutedComponentInputBinder });
  3023. }
  3024. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RoutedComponentInputBinder, decorators: [{
  3025. type: Injectable
  3026. }] });
  3027. function createRouterState(routeReuseStrategy, curr, prevState) {
  3028. const root = createNode(routeReuseStrategy, curr._root, prevState ? prevState._root : undefined);
  3029. return new RouterState(root, curr);
  3030. }
  3031. function createNode(routeReuseStrategy, curr, prevState) {
  3032. // reuse an activated route that is currently displayed on the screen
  3033. if (prevState && routeReuseStrategy.shouldReuseRoute(curr.value, prevState.value.snapshot)) {
  3034. const value = prevState.value;
  3035. value._futureSnapshot = curr.value;
  3036. const children = createOrReuseChildren(routeReuseStrategy, curr, prevState);
  3037. return new TreeNode(value, children);
  3038. }
  3039. else {
  3040. if (routeReuseStrategy.shouldAttach(curr.value)) {
  3041. // retrieve an activated route that is used to be displayed, but is not currently displayed
  3042. const detachedRouteHandle = routeReuseStrategy.retrieve(curr.value);
  3043. if (detachedRouteHandle !== null) {
  3044. const tree = detachedRouteHandle.route;
  3045. tree.value._futureSnapshot = curr.value;
  3046. tree.children = curr.children.map((c) => createNode(routeReuseStrategy, c));
  3047. return tree;
  3048. }
  3049. }
  3050. const value = createActivatedRoute(curr.value);
  3051. const children = curr.children.map((c) => createNode(routeReuseStrategy, c));
  3052. return new TreeNode(value, children);
  3053. }
  3054. }
  3055. function createOrReuseChildren(routeReuseStrategy, curr, prevState) {
  3056. return curr.children.map((child) => {
  3057. for (const p of prevState.children) {
  3058. if (routeReuseStrategy.shouldReuseRoute(child.value, p.value.snapshot)) {
  3059. return createNode(routeReuseStrategy, child, p);
  3060. }
  3061. }
  3062. return createNode(routeReuseStrategy, child);
  3063. });
  3064. }
  3065. function createActivatedRoute(c) {
  3066. return new ActivatedRoute(new BehaviorSubject(c.url), new BehaviorSubject(c.params), new BehaviorSubject(c.queryParams), new BehaviorSubject(c.fragment), new BehaviorSubject(c.data), c.outlet, c.component, c);
  3067. }
  3068. /**
  3069. * Can be returned by a `Router` guard to instruct the `Router` to redirect rather than continue
  3070. * processing the path of the in-flight navigation. The `redirectTo` indicates _where_ the new
  3071. * navigation should go to and the optional `navigationBehaviorOptions` can provide more information
  3072. * about _how_ to perform the navigation.
  3073. *
  3074. * ```ts
  3075. * const route: Route = {
  3076. * path: "user/:userId",
  3077. * component: User,
  3078. * canActivate: [
  3079. * () => {
  3080. * const router = inject(Router);
  3081. * const authService = inject(AuthenticationService);
  3082. *
  3083. * if (!authService.isLoggedIn()) {
  3084. * const loginPath = router.parseUrl("/login");
  3085. * return new RedirectCommand(loginPath, {
  3086. * skipLocationChange: "true",
  3087. * });
  3088. * }
  3089. *
  3090. * return true;
  3091. * },
  3092. * ],
  3093. * };
  3094. * ```
  3095. * @see [Routing guide](guide/routing/common-router-tasks#preventing-unauthorized-access)
  3096. *
  3097. * @publicApi
  3098. */
  3099. class RedirectCommand {
  3100. redirectTo;
  3101. navigationBehaviorOptions;
  3102. constructor(redirectTo, navigationBehaviorOptions) {
  3103. this.redirectTo = redirectTo;
  3104. this.navigationBehaviorOptions = navigationBehaviorOptions;
  3105. }
  3106. }
  3107. const NAVIGATION_CANCELING_ERROR = 'ngNavigationCancelingError';
  3108. function redirectingNavigationError(urlSerializer, redirect) {
  3109. const { redirectTo, navigationBehaviorOptions } = isUrlTree(redirect)
  3110. ? { redirectTo: redirect, navigationBehaviorOptions: undefined }
  3111. : redirect;
  3112. const error = navigationCancelingError(ngDevMode && `Redirecting to "${urlSerializer.serialize(redirectTo)}"`, NavigationCancellationCode.Redirect);
  3113. error.url = redirectTo;
  3114. error.navigationBehaviorOptions = navigationBehaviorOptions;
  3115. return error;
  3116. }
  3117. function navigationCancelingError(message, code) {
  3118. const error = new Error(`NavigationCancelingError: ${message || ''}`);
  3119. error[NAVIGATION_CANCELING_ERROR] = true;
  3120. error.cancellationCode = code;
  3121. return error;
  3122. }
  3123. function isRedirectingNavigationCancelingError(error) {
  3124. return (isNavigationCancelingError(error) &&
  3125. isUrlTree(error.url));
  3126. }
  3127. function isNavigationCancelingError(error) {
  3128. return !!error && error[NAVIGATION_CANCELING_ERROR];
  3129. }
  3130. let warnedAboutUnsupportedInputBinding = false;
  3131. const activateRoutes = (rootContexts, routeReuseStrategy, forwardEvent, inputBindingEnabled) => map((t) => {
  3132. new ActivateRoutes(routeReuseStrategy, t.targetRouterState, t.currentRouterState, forwardEvent, inputBindingEnabled).activate(rootContexts);
  3133. return t;
  3134. });
  3135. class ActivateRoutes {
  3136. routeReuseStrategy;
  3137. futureState;
  3138. currState;
  3139. forwardEvent;
  3140. inputBindingEnabled;
  3141. constructor(routeReuseStrategy, futureState, currState, forwardEvent, inputBindingEnabled) {
  3142. this.routeReuseStrategy = routeReuseStrategy;
  3143. this.futureState = futureState;
  3144. this.currState = currState;
  3145. this.forwardEvent = forwardEvent;
  3146. this.inputBindingEnabled = inputBindingEnabled;
  3147. }
  3148. activate(parentContexts) {
  3149. const futureRoot = this.futureState._root;
  3150. const currRoot = this.currState ? this.currState._root : null;
  3151. this.deactivateChildRoutes(futureRoot, currRoot, parentContexts);
  3152. advanceActivatedRoute(this.futureState.root);
  3153. this.activateChildRoutes(futureRoot, currRoot, parentContexts);
  3154. }
  3155. // De-activate the child route that are not re-used for the future state
  3156. deactivateChildRoutes(futureNode, currNode, contexts) {
  3157. const children = nodeChildrenAsMap(currNode);
  3158. // Recurse on the routes active in the future state to de-activate deeper children
  3159. futureNode.children.forEach((futureChild) => {
  3160. const childOutletName = futureChild.value.outlet;
  3161. this.deactivateRoutes(futureChild, children[childOutletName], contexts);
  3162. delete children[childOutletName];
  3163. });
  3164. // De-activate the routes that will not be re-used
  3165. Object.values(children).forEach((v) => {
  3166. this.deactivateRouteAndItsChildren(v, contexts);
  3167. });
  3168. }
  3169. deactivateRoutes(futureNode, currNode, parentContext) {
  3170. const future = futureNode.value;
  3171. const curr = currNode ? currNode.value : null;
  3172. if (future === curr) {
  3173. // Reusing the node, check to see if the children need to be de-activated
  3174. if (future.component) {
  3175. // If we have a normal route, we need to go through an outlet.
  3176. const context = parentContext.getContext(future.outlet);
  3177. if (context) {
  3178. this.deactivateChildRoutes(futureNode, currNode, context.children);
  3179. }
  3180. }
  3181. else {
  3182. // if we have a componentless route, we recurse but keep the same outlet map.
  3183. this.deactivateChildRoutes(futureNode, currNode, parentContext);
  3184. }
  3185. }
  3186. else {
  3187. if (curr) {
  3188. // Deactivate the current route which will not be re-used
  3189. this.deactivateRouteAndItsChildren(currNode, parentContext);
  3190. }
  3191. }
  3192. }
  3193. deactivateRouteAndItsChildren(route, parentContexts) {
  3194. // If there is no component, the Route is never attached to an outlet (because there is no
  3195. // component to attach).
  3196. if (route.value.component && this.routeReuseStrategy.shouldDetach(route.value.snapshot)) {
  3197. this.detachAndStoreRouteSubtree(route, parentContexts);
  3198. }
  3199. else {
  3200. this.deactivateRouteAndOutlet(route, parentContexts);
  3201. }
  3202. }
  3203. detachAndStoreRouteSubtree(route, parentContexts) {
  3204. const context = parentContexts.getContext(route.value.outlet);
  3205. const contexts = context && route.value.component ? context.children : parentContexts;
  3206. const children = nodeChildrenAsMap(route);
  3207. for (const treeNode of Object.values(children)) {
  3208. this.deactivateRouteAndItsChildren(treeNode, contexts);
  3209. }
  3210. if (context && context.outlet) {
  3211. const componentRef = context.outlet.detach();
  3212. const contexts = context.children.onOutletDeactivated();
  3213. this.routeReuseStrategy.store(route.value.snapshot, { componentRef, route, contexts });
  3214. }
  3215. }
  3216. deactivateRouteAndOutlet(route, parentContexts) {
  3217. const context = parentContexts.getContext(route.value.outlet);
  3218. // The context could be `null` if we are on a componentless route but there may still be
  3219. // children that need deactivating.
  3220. const contexts = context && route.value.component ? context.children : parentContexts;
  3221. const children = nodeChildrenAsMap(route);
  3222. for (const treeNode of Object.values(children)) {
  3223. this.deactivateRouteAndItsChildren(treeNode, contexts);
  3224. }
  3225. if (context) {
  3226. if (context.outlet) {
  3227. // Destroy the component
  3228. context.outlet.deactivate();
  3229. // Destroy the contexts for all the outlets that were in the component
  3230. context.children.onOutletDeactivated();
  3231. }
  3232. // Clear the information about the attached component on the context but keep the reference to
  3233. // the outlet. Clear even if outlet was not yet activated to avoid activating later with old
  3234. // info
  3235. context.attachRef = null;
  3236. context.route = null;
  3237. }
  3238. }
  3239. activateChildRoutes(futureNode, currNode, contexts) {
  3240. const children = nodeChildrenAsMap(currNode);
  3241. futureNode.children.forEach((c) => {
  3242. this.activateRoutes(c, children[c.value.outlet], contexts);
  3243. this.forwardEvent(new ActivationEnd(c.value.snapshot));
  3244. });
  3245. if (futureNode.children.length) {
  3246. this.forwardEvent(new ChildActivationEnd(futureNode.value.snapshot));
  3247. }
  3248. }
  3249. activateRoutes(futureNode, currNode, parentContexts) {
  3250. const future = futureNode.value;
  3251. const curr = currNode ? currNode.value : null;
  3252. advanceActivatedRoute(future);
  3253. // reusing the node
  3254. if (future === curr) {
  3255. if (future.component) {
  3256. // If we have a normal route, we need to go through an outlet.
  3257. const context = parentContexts.getOrCreateContext(future.outlet);
  3258. this.activateChildRoutes(futureNode, currNode, context.children);
  3259. }
  3260. else {
  3261. // if we have a componentless route, we recurse but keep the same outlet map.
  3262. this.activateChildRoutes(futureNode, currNode, parentContexts);
  3263. }
  3264. }
  3265. else {
  3266. if (future.component) {
  3267. // if we have a normal route, we need to place the component into the outlet and recurse.
  3268. const context = parentContexts.getOrCreateContext(future.outlet);
  3269. if (this.routeReuseStrategy.shouldAttach(future.snapshot)) {
  3270. const stored = (this.routeReuseStrategy.retrieve(future.snapshot));
  3271. this.routeReuseStrategy.store(future.snapshot, null);
  3272. context.children.onOutletReAttached(stored.contexts);
  3273. context.attachRef = stored.componentRef;
  3274. context.route = stored.route.value;
  3275. if (context.outlet) {
  3276. // Attach right away when the outlet has already been instantiated
  3277. // Otherwise attach from `RouterOutlet.ngOnInit` when it is instantiated
  3278. context.outlet.attach(stored.componentRef, stored.route.value);
  3279. }
  3280. advanceActivatedRoute(stored.route.value);
  3281. this.activateChildRoutes(futureNode, null, context.children);
  3282. }
  3283. else {
  3284. context.attachRef = null;
  3285. context.route = future;
  3286. if (context.outlet) {
  3287. // Activate the outlet when it has already been instantiated
  3288. // Otherwise it will get activated from its `ngOnInit` when instantiated
  3289. context.outlet.activateWith(future, context.injector);
  3290. }
  3291. this.activateChildRoutes(futureNode, null, context.children);
  3292. }
  3293. }
  3294. else {
  3295. // if we have a componentless route, we recurse but keep the same outlet map.
  3296. this.activateChildRoutes(futureNode, null, parentContexts);
  3297. }
  3298. }
  3299. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  3300. const context = parentContexts.getOrCreateContext(future.outlet);
  3301. const outlet = context.outlet;
  3302. if (outlet &&
  3303. this.inputBindingEnabled &&
  3304. !outlet.supportsBindingToComponentInputs &&
  3305. !warnedAboutUnsupportedInputBinding) {
  3306. console.warn(`'withComponentInputBinding' feature is enabled but ` +
  3307. `this application is using an outlet that may not support binding to component inputs.`);
  3308. warnedAboutUnsupportedInputBinding = true;
  3309. }
  3310. }
  3311. }
  3312. }
  3313. class CanActivate {
  3314. path;
  3315. route;
  3316. constructor(path) {
  3317. this.path = path;
  3318. this.route = this.path[this.path.length - 1];
  3319. }
  3320. }
  3321. class CanDeactivate {
  3322. component;
  3323. route;
  3324. constructor(component, route) {
  3325. this.component = component;
  3326. this.route = route;
  3327. }
  3328. }
  3329. function getAllRouteGuards(future, curr, parentContexts) {
  3330. const futureRoot = future._root;
  3331. const currRoot = curr ? curr._root : null;
  3332. return getChildRouteGuards(futureRoot, currRoot, parentContexts, [futureRoot.value]);
  3333. }
  3334. function getCanActivateChild(p) {
  3335. const canActivateChild = p.routeConfig ? p.routeConfig.canActivateChild : null;
  3336. if (!canActivateChild || canActivateChild.length === 0)
  3337. return null;
  3338. return { node: p, guards: canActivateChild };
  3339. }
  3340. function getTokenOrFunctionIdentity(tokenOrFunction, injector) {
  3341. const NOT_FOUND = Symbol();
  3342. const result = injector.get(tokenOrFunction, NOT_FOUND);
  3343. if (result === NOT_FOUND) {
  3344. if (typeof tokenOrFunction === 'function' && !_isInjectable(tokenOrFunction)) {
  3345. // We think the token is just a function so return it as-is
  3346. return tokenOrFunction;
  3347. }
  3348. else {
  3349. // This will throw the not found error
  3350. return injector.get(tokenOrFunction);
  3351. }
  3352. }
  3353. return result;
  3354. }
  3355. function getChildRouteGuards(futureNode, currNode, contexts, futurePath, checks = {
  3356. canDeactivateChecks: [],
  3357. canActivateChecks: [],
  3358. }) {
  3359. const prevChildren = nodeChildrenAsMap(currNode);
  3360. // Process the children of the future route
  3361. futureNode.children.forEach((c) => {
  3362. getRouteGuards(c, prevChildren[c.value.outlet], contexts, futurePath.concat([c.value]), checks);
  3363. delete prevChildren[c.value.outlet];
  3364. });
  3365. // Process any children left from the current route (not active for the future route)
  3366. Object.entries(prevChildren).forEach(([k, v]) => deactivateRouteAndItsChildren(v, contexts.getContext(k), checks));
  3367. return checks;
  3368. }
  3369. function getRouteGuards(futureNode, currNode, parentContexts, futurePath, checks = {
  3370. canDeactivateChecks: [],
  3371. canActivateChecks: [],
  3372. }) {
  3373. const future = futureNode.value;
  3374. const curr = currNode ? currNode.value : null;
  3375. const context = parentContexts ? parentContexts.getContext(futureNode.value.outlet) : null;
  3376. // reusing the node
  3377. if (curr && future.routeConfig === curr.routeConfig) {
  3378. const shouldRun = shouldRunGuardsAndResolvers(curr, future, future.routeConfig.runGuardsAndResolvers);
  3379. if (shouldRun) {
  3380. checks.canActivateChecks.push(new CanActivate(futurePath));
  3381. }
  3382. else {
  3383. // we need to set the data
  3384. future.data = curr.data;
  3385. future._resolvedData = curr._resolvedData;
  3386. }
  3387. // If we have a component, we need to go through an outlet.
  3388. if (future.component) {
  3389. getChildRouteGuards(futureNode, currNode, context ? context.children : null, futurePath, checks);
  3390. // if we have a componentless route, we recurse but keep the same outlet map.
  3391. }
  3392. else {
  3393. getChildRouteGuards(futureNode, currNode, parentContexts, futurePath, checks);
  3394. }
  3395. if (shouldRun && context && context.outlet && context.outlet.isActivated) {
  3396. checks.canDeactivateChecks.push(new CanDeactivate(context.outlet.component, curr));
  3397. }
  3398. }
  3399. else {
  3400. if (curr) {
  3401. deactivateRouteAndItsChildren(currNode, context, checks);
  3402. }
  3403. checks.canActivateChecks.push(new CanActivate(futurePath));
  3404. // If we have a component, we need to go through an outlet.
  3405. if (future.component) {
  3406. getChildRouteGuards(futureNode, null, context ? context.children : null, futurePath, checks);
  3407. // if we have a componentless route, we recurse but keep the same outlet map.
  3408. }
  3409. else {
  3410. getChildRouteGuards(futureNode, null, parentContexts, futurePath, checks);
  3411. }
  3412. }
  3413. return checks;
  3414. }
  3415. function shouldRunGuardsAndResolvers(curr, future, mode) {
  3416. if (typeof mode === 'function') {
  3417. return mode(curr, future);
  3418. }
  3419. switch (mode) {
  3420. case 'pathParamsChange':
  3421. return !equalPath(curr.url, future.url);
  3422. case 'pathParamsOrQueryParamsChange':
  3423. return (!equalPath(curr.url, future.url) || !shallowEqual(curr.queryParams, future.queryParams));
  3424. case 'always':
  3425. return true;
  3426. case 'paramsOrQueryParamsChange':
  3427. return (!equalParamsAndUrlSegments(curr, future) ||
  3428. !shallowEqual(curr.queryParams, future.queryParams));
  3429. case 'paramsChange':
  3430. default:
  3431. return !equalParamsAndUrlSegments(curr, future);
  3432. }
  3433. }
  3434. function deactivateRouteAndItsChildren(route, context, checks) {
  3435. const children = nodeChildrenAsMap(route);
  3436. const r = route.value;
  3437. Object.entries(children).forEach(([childName, node]) => {
  3438. if (!r.component) {
  3439. deactivateRouteAndItsChildren(node, context, checks);
  3440. }
  3441. else if (context) {
  3442. deactivateRouteAndItsChildren(node, context.children.getContext(childName), checks);
  3443. }
  3444. else {
  3445. deactivateRouteAndItsChildren(node, null, checks);
  3446. }
  3447. });
  3448. if (!r.component) {
  3449. checks.canDeactivateChecks.push(new CanDeactivate(null, r));
  3450. }
  3451. else if (context && context.outlet && context.outlet.isActivated) {
  3452. checks.canDeactivateChecks.push(new CanDeactivate(context.outlet.component, r));
  3453. }
  3454. else {
  3455. checks.canDeactivateChecks.push(new CanDeactivate(null, r));
  3456. }
  3457. }
  3458. /**
  3459. * Simple function check, but generic so type inference will flow. Example:
  3460. *
  3461. * function product(a: number, b: number) {
  3462. * return a * b;
  3463. * }
  3464. *
  3465. * if (isFunction<product>(fn)) {
  3466. * return fn(1, 2);
  3467. * } else {
  3468. * throw "Must provide the `product` function";
  3469. * }
  3470. */
  3471. function isFunction(v) {
  3472. return typeof v === 'function';
  3473. }
  3474. function isBoolean(v) {
  3475. return typeof v === 'boolean';
  3476. }
  3477. function isCanLoad(guard) {
  3478. return guard && isFunction(guard.canLoad);
  3479. }
  3480. function isCanActivate(guard) {
  3481. return guard && isFunction(guard.canActivate);
  3482. }
  3483. function isCanActivateChild(guard) {
  3484. return guard && isFunction(guard.canActivateChild);
  3485. }
  3486. function isCanDeactivate(guard) {
  3487. return guard && isFunction(guard.canDeactivate);
  3488. }
  3489. function isCanMatch(guard) {
  3490. return guard && isFunction(guard.canMatch);
  3491. }
  3492. function isEmptyError(e) {
  3493. return e instanceof EmptyError || e?.name === 'EmptyError';
  3494. }
  3495. const INITIAL_VALUE = /* @__PURE__ */ Symbol('INITIAL_VALUE');
  3496. function prioritizedGuardValue() {
  3497. return switchMap((obs) => {
  3498. return combineLatest(obs.map((o) => o.pipe(take(1), startWith(INITIAL_VALUE)))).pipe(map((results) => {
  3499. for (const result of results) {
  3500. if (result === true) {
  3501. // If result is true, check the next one
  3502. continue;
  3503. }
  3504. else if (result === INITIAL_VALUE) {
  3505. // If guard has not finished, we need to stop processing.
  3506. return INITIAL_VALUE;
  3507. }
  3508. else if (result === false || isRedirect(result)) {
  3509. // Result finished and was not true. Return the result.
  3510. // Note that we only allow false/UrlTree/RedirectCommand. Other values are considered invalid and
  3511. // ignored.
  3512. return result;
  3513. }
  3514. }
  3515. // Everything resolved to true. Return true.
  3516. return true;
  3517. }), filter((item) => item !== INITIAL_VALUE), take(1));
  3518. });
  3519. }
  3520. function isRedirect(val) {
  3521. return isUrlTree(val) || val instanceof RedirectCommand;
  3522. }
  3523. function checkGuards(injector, forwardEvent) {
  3524. return mergeMap((t) => {
  3525. const { targetSnapshot, currentSnapshot, guards: { canActivateChecks, canDeactivateChecks }, } = t;
  3526. if (canDeactivateChecks.length === 0 && canActivateChecks.length === 0) {
  3527. return of({ ...t, guardsResult: true });
  3528. }
  3529. return runCanDeactivateChecks(canDeactivateChecks, targetSnapshot, currentSnapshot, injector).pipe(mergeMap((canDeactivate) => {
  3530. return canDeactivate && isBoolean(canDeactivate)
  3531. ? runCanActivateChecks(targetSnapshot, canActivateChecks, injector, forwardEvent)
  3532. : of(canDeactivate);
  3533. }), map((guardsResult) => ({ ...t, guardsResult })));
  3534. });
  3535. }
  3536. function runCanDeactivateChecks(checks, futureRSS, currRSS, injector) {
  3537. return from(checks).pipe(mergeMap((check) => runCanDeactivate(check.component, check.route, currRSS, futureRSS, injector)), first((result) => {
  3538. return result !== true;
  3539. }, true));
  3540. }
  3541. function runCanActivateChecks(futureSnapshot, checks, injector, forwardEvent) {
  3542. return from(checks).pipe(concatMap((check) => {
  3543. return concat(fireChildActivationStart(check.route.parent, forwardEvent), fireActivationStart(check.route, forwardEvent), runCanActivateChild(futureSnapshot, check.path, injector), runCanActivate(futureSnapshot, check.route, injector));
  3544. }), first((result) => {
  3545. return result !== true;
  3546. }, true));
  3547. }
  3548. /**
  3549. * This should fire off `ActivationStart` events for each route being activated at this
  3550. * level.
  3551. * In other words, if you're activating `a` and `b` below, `path` will contain the
  3552. * `ActivatedRouteSnapshot`s for both and we will fire `ActivationStart` for both. Always
  3553. * return
  3554. * `true` so checks continue to run.
  3555. */
  3556. function fireActivationStart(snapshot, forwardEvent) {
  3557. if (snapshot !== null && forwardEvent) {
  3558. forwardEvent(new ActivationStart(snapshot));
  3559. }
  3560. return of(true);
  3561. }
  3562. /**
  3563. * This should fire off `ChildActivationStart` events for each route being activated at this
  3564. * level.
  3565. * In other words, if you're activating `a` and `b` below, `path` will contain the
  3566. * `ActivatedRouteSnapshot`s for both and we will fire `ChildActivationStart` for both. Always
  3567. * return
  3568. * `true` so checks continue to run.
  3569. */
  3570. function fireChildActivationStart(snapshot, forwardEvent) {
  3571. if (snapshot !== null && forwardEvent) {
  3572. forwardEvent(new ChildActivationStart(snapshot));
  3573. }
  3574. return of(true);
  3575. }
  3576. function runCanActivate(futureRSS, futureARS, injector) {
  3577. const canActivate = futureARS.routeConfig ? futureARS.routeConfig.canActivate : null;
  3578. if (!canActivate || canActivate.length === 0)
  3579. return of(true);
  3580. const canActivateObservables = canActivate.map((canActivate) => {
  3581. return defer(() => {
  3582. const closestInjector = getClosestRouteInjector(futureARS) ?? injector;
  3583. const guard = getTokenOrFunctionIdentity(canActivate, closestInjector);
  3584. const guardVal = isCanActivate(guard)
  3585. ? guard.canActivate(futureARS, futureRSS)
  3586. : runInInjectionContext(closestInjector, () => guard(futureARS, futureRSS));
  3587. return wrapIntoObservable(guardVal).pipe(first());
  3588. });
  3589. });
  3590. return of(canActivateObservables).pipe(prioritizedGuardValue());
  3591. }
  3592. function runCanActivateChild(futureRSS, path, injector) {
  3593. const futureARS = path[path.length - 1];
  3594. const canActivateChildGuards = path
  3595. .slice(0, path.length - 1)
  3596. .reverse()
  3597. .map((p) => getCanActivateChild(p))
  3598. .filter((_) => _ !== null);
  3599. const canActivateChildGuardsMapped = canActivateChildGuards.map((d) => {
  3600. return defer(() => {
  3601. const guardsMapped = d.guards.map((canActivateChild) => {
  3602. const closestInjector = getClosestRouteInjector(d.node) ?? injector;
  3603. const guard = getTokenOrFunctionIdentity(canActivateChild, closestInjector);
  3604. const guardVal = isCanActivateChild(guard)
  3605. ? guard.canActivateChild(futureARS, futureRSS)
  3606. : runInInjectionContext(closestInjector, () => guard(futureARS, futureRSS));
  3607. return wrapIntoObservable(guardVal).pipe(first());
  3608. });
  3609. return of(guardsMapped).pipe(prioritizedGuardValue());
  3610. });
  3611. });
  3612. return of(canActivateChildGuardsMapped).pipe(prioritizedGuardValue());
  3613. }
  3614. function runCanDeactivate(component, currARS, currRSS, futureRSS, injector) {
  3615. const canDeactivate = currARS && currARS.routeConfig ? currARS.routeConfig.canDeactivate : null;
  3616. if (!canDeactivate || canDeactivate.length === 0)
  3617. return of(true);
  3618. const canDeactivateObservables = canDeactivate.map((c) => {
  3619. const closestInjector = getClosestRouteInjector(currARS) ?? injector;
  3620. const guard = getTokenOrFunctionIdentity(c, closestInjector);
  3621. const guardVal = isCanDeactivate(guard)
  3622. ? guard.canDeactivate(component, currARS, currRSS, futureRSS)
  3623. : runInInjectionContext(closestInjector, () => guard(component, currARS, currRSS, futureRSS));
  3624. return wrapIntoObservable(guardVal).pipe(first());
  3625. });
  3626. return of(canDeactivateObservables).pipe(prioritizedGuardValue());
  3627. }
  3628. function runCanLoadGuards(injector, route, segments, urlSerializer) {
  3629. const canLoad = route.canLoad;
  3630. if (canLoad === undefined || canLoad.length === 0) {
  3631. return of(true);
  3632. }
  3633. const canLoadObservables = canLoad.map((injectionToken) => {
  3634. const guard = getTokenOrFunctionIdentity(injectionToken, injector);
  3635. const guardVal = isCanLoad(guard)
  3636. ? guard.canLoad(route, segments)
  3637. : runInInjectionContext(injector, () => guard(route, segments));
  3638. return wrapIntoObservable(guardVal);
  3639. });
  3640. return of(canLoadObservables).pipe(prioritizedGuardValue(), redirectIfUrlTree(urlSerializer));
  3641. }
  3642. function redirectIfUrlTree(urlSerializer) {
  3643. return pipe(tap((result) => {
  3644. if (typeof result === 'boolean')
  3645. return;
  3646. throw redirectingNavigationError(urlSerializer, result);
  3647. }), map((result) => result === true));
  3648. }
  3649. function runCanMatchGuards(injector, route, segments, urlSerializer) {
  3650. const canMatch = route.canMatch;
  3651. if (!canMatch || canMatch.length === 0)
  3652. return of(true);
  3653. const canMatchObservables = canMatch.map((injectionToken) => {
  3654. const guard = getTokenOrFunctionIdentity(injectionToken, injector);
  3655. const guardVal = isCanMatch(guard)
  3656. ? guard.canMatch(route, segments)
  3657. : runInInjectionContext(injector, () => guard(route, segments));
  3658. return wrapIntoObservable(guardVal);
  3659. });
  3660. return of(canMatchObservables).pipe(prioritizedGuardValue(), redirectIfUrlTree(urlSerializer));
  3661. }
  3662. class NoMatch {
  3663. segmentGroup;
  3664. constructor(segmentGroup) {
  3665. this.segmentGroup = segmentGroup || null;
  3666. }
  3667. }
  3668. class AbsoluteRedirect extends Error {
  3669. urlTree;
  3670. constructor(urlTree) {
  3671. super();
  3672. this.urlTree = urlTree;
  3673. }
  3674. }
  3675. function noMatch$1(segmentGroup) {
  3676. return throwError(new NoMatch(segmentGroup));
  3677. }
  3678. function namedOutletsRedirect(redirectTo) {
  3679. return throwError(new _RuntimeError(4000 /* RuntimeErrorCode.NAMED_OUTLET_REDIRECT */, (typeof ngDevMode === 'undefined' || ngDevMode) &&
  3680. `Only absolute redirects can have named outlets. redirectTo: '${redirectTo}'`));
  3681. }
  3682. function canLoadFails(route) {
  3683. return throwError(navigationCancelingError((typeof ngDevMode === 'undefined' || ngDevMode) &&
  3684. `Cannot load children because the guard of the route "path: '${route.path}'" returned false`, NavigationCancellationCode.GuardRejected));
  3685. }
  3686. class ApplyRedirects {
  3687. urlSerializer;
  3688. urlTree;
  3689. constructor(urlSerializer, urlTree) {
  3690. this.urlSerializer = urlSerializer;
  3691. this.urlTree = urlTree;
  3692. }
  3693. lineralizeSegments(route, urlTree) {
  3694. let res = [];
  3695. let c = urlTree.root;
  3696. while (true) {
  3697. res = res.concat(c.segments);
  3698. if (c.numberOfChildren === 0) {
  3699. return of(res);
  3700. }
  3701. if (c.numberOfChildren > 1 || !c.children[PRIMARY_OUTLET]) {
  3702. return namedOutletsRedirect(`${route.redirectTo}`);
  3703. }
  3704. c = c.children[PRIMARY_OUTLET];
  3705. }
  3706. }
  3707. applyRedirectCommands(segments, redirectTo, posParams, currentSnapshot, injector) {
  3708. if (typeof redirectTo !== 'string') {
  3709. const redirectToFn = redirectTo;
  3710. const { queryParams, fragment, routeConfig, url, outlet, params, data, title } = currentSnapshot;
  3711. const newRedirect = runInInjectionContext(injector, () => redirectToFn({ params, data, queryParams, fragment, routeConfig, url, outlet, title }));
  3712. if (newRedirect instanceof UrlTree) {
  3713. throw new AbsoluteRedirect(newRedirect);
  3714. }
  3715. redirectTo = newRedirect;
  3716. }
  3717. const newTree = this.applyRedirectCreateUrlTree(redirectTo, this.urlSerializer.parse(redirectTo), segments, posParams);
  3718. if (redirectTo[0] === '/') {
  3719. throw new AbsoluteRedirect(newTree);
  3720. }
  3721. return newTree;
  3722. }
  3723. applyRedirectCreateUrlTree(redirectTo, urlTree, segments, posParams) {
  3724. const newRoot = this.createSegmentGroup(redirectTo, urlTree.root, segments, posParams);
  3725. return new UrlTree(newRoot, this.createQueryParams(urlTree.queryParams, this.urlTree.queryParams), urlTree.fragment);
  3726. }
  3727. createQueryParams(redirectToParams, actualParams) {
  3728. const res = {};
  3729. Object.entries(redirectToParams).forEach(([k, v]) => {
  3730. const copySourceValue = typeof v === 'string' && v[0] === ':';
  3731. if (copySourceValue) {
  3732. const sourceName = v.substring(1);
  3733. res[k] = actualParams[sourceName];
  3734. }
  3735. else {
  3736. res[k] = v;
  3737. }
  3738. });
  3739. return res;
  3740. }
  3741. createSegmentGroup(redirectTo, group, segments, posParams) {
  3742. const updatedSegments = this.createSegments(redirectTo, group.segments, segments, posParams);
  3743. let children = {};
  3744. Object.entries(group.children).forEach(([name, child]) => {
  3745. children[name] = this.createSegmentGroup(redirectTo, child, segments, posParams);
  3746. });
  3747. return new UrlSegmentGroup(updatedSegments, children);
  3748. }
  3749. createSegments(redirectTo, redirectToSegments, actualSegments, posParams) {
  3750. return redirectToSegments.map((s) => s.path[0] === ':'
  3751. ? this.findPosParam(redirectTo, s, posParams)
  3752. : this.findOrReturn(s, actualSegments));
  3753. }
  3754. findPosParam(redirectTo, redirectToUrlSegment, posParams) {
  3755. const pos = posParams[redirectToUrlSegment.path.substring(1)];
  3756. if (!pos)
  3757. throw new _RuntimeError(4001 /* RuntimeErrorCode.MISSING_REDIRECT */, (typeof ngDevMode === 'undefined' || ngDevMode) &&
  3758. `Cannot redirect to '${redirectTo}'. Cannot find '${redirectToUrlSegment.path}'.`);
  3759. return pos;
  3760. }
  3761. findOrReturn(redirectToUrlSegment, actualSegments) {
  3762. let idx = 0;
  3763. for (const s of actualSegments) {
  3764. if (s.path === redirectToUrlSegment.path) {
  3765. actualSegments.splice(idx);
  3766. return s;
  3767. }
  3768. idx++;
  3769. }
  3770. return redirectToUrlSegment;
  3771. }
  3772. }
  3773. const noMatch = {
  3774. matched: false,
  3775. consumedSegments: [],
  3776. remainingSegments: [],
  3777. parameters: {},
  3778. positionalParamSegments: {},
  3779. };
  3780. function matchWithChecks(segmentGroup, route, segments, injector, urlSerializer) {
  3781. const result = match(segmentGroup, route, segments);
  3782. if (!result.matched) {
  3783. return of(result);
  3784. }
  3785. // Only create the Route's `EnvironmentInjector` if it matches the attempted
  3786. // navigation
  3787. injector = getOrCreateRouteInjectorIfNeeded(route, injector);
  3788. return runCanMatchGuards(injector, route, segments, urlSerializer).pipe(map((v) => (v === true ? result : { ...noMatch })));
  3789. }
  3790. function match(segmentGroup, route, segments) {
  3791. if (route.path === '**') {
  3792. return createWildcardMatchResult(segments);
  3793. }
  3794. if (route.path === '') {
  3795. if (route.pathMatch === 'full' && (segmentGroup.hasChildren() || segments.length > 0)) {
  3796. return { ...noMatch };
  3797. }
  3798. return {
  3799. matched: true,
  3800. consumedSegments: [],
  3801. remainingSegments: segments,
  3802. parameters: {},
  3803. positionalParamSegments: {},
  3804. };
  3805. }
  3806. const matcher = route.matcher || defaultUrlMatcher;
  3807. const res = matcher(segments, segmentGroup, route);
  3808. if (!res)
  3809. return { ...noMatch };
  3810. const posParams = {};
  3811. Object.entries(res.posParams ?? {}).forEach(([k, v]) => {
  3812. posParams[k] = v.path;
  3813. });
  3814. const parameters = res.consumed.length > 0
  3815. ? { ...posParams, ...res.consumed[res.consumed.length - 1].parameters }
  3816. : posParams;
  3817. return {
  3818. matched: true,
  3819. consumedSegments: res.consumed,
  3820. remainingSegments: segments.slice(res.consumed.length),
  3821. // TODO(atscott): investigate combining parameters and positionalParamSegments
  3822. parameters,
  3823. positionalParamSegments: res.posParams ?? {},
  3824. };
  3825. }
  3826. function createWildcardMatchResult(segments) {
  3827. return {
  3828. matched: true,
  3829. parameters: segments.length > 0 ? last(segments).parameters : {},
  3830. consumedSegments: segments,
  3831. remainingSegments: [],
  3832. positionalParamSegments: {},
  3833. };
  3834. }
  3835. function split(segmentGroup, consumedSegments, slicedSegments, config) {
  3836. if (slicedSegments.length > 0 &&
  3837. containsEmptyPathMatchesWithNamedOutlets(segmentGroup, slicedSegments, config)) {
  3838. const s = new UrlSegmentGroup(consumedSegments, createChildrenForEmptyPaths(config, new UrlSegmentGroup(slicedSegments, segmentGroup.children)));
  3839. return { segmentGroup: s, slicedSegments: [] };
  3840. }
  3841. if (slicedSegments.length === 0 &&
  3842. containsEmptyPathMatches(segmentGroup, slicedSegments, config)) {
  3843. const s = new UrlSegmentGroup(segmentGroup.segments, addEmptyPathsToChildrenIfNeeded(segmentGroup, slicedSegments, config, segmentGroup.children));
  3844. return { segmentGroup: s, slicedSegments };
  3845. }
  3846. const s = new UrlSegmentGroup(segmentGroup.segments, segmentGroup.children);
  3847. return { segmentGroup: s, slicedSegments };
  3848. }
  3849. function addEmptyPathsToChildrenIfNeeded(segmentGroup, slicedSegments, routes, children) {
  3850. const res = {};
  3851. for (const r of routes) {
  3852. if (emptyPathMatch(segmentGroup, slicedSegments, r) && !children[getOutlet(r)]) {
  3853. const s = new UrlSegmentGroup([], {});
  3854. res[getOutlet(r)] = s;
  3855. }
  3856. }
  3857. return { ...children, ...res };
  3858. }
  3859. function createChildrenForEmptyPaths(routes, primarySegment) {
  3860. const res = {};
  3861. res[PRIMARY_OUTLET] = primarySegment;
  3862. for (const r of routes) {
  3863. if (r.path === '' && getOutlet(r) !== PRIMARY_OUTLET) {
  3864. const s = new UrlSegmentGroup([], {});
  3865. res[getOutlet(r)] = s;
  3866. }
  3867. }
  3868. return res;
  3869. }
  3870. function containsEmptyPathMatchesWithNamedOutlets(segmentGroup, slicedSegments, routes) {
  3871. return routes.some((r) => emptyPathMatch(segmentGroup, slicedSegments, r) && getOutlet(r) !== PRIMARY_OUTLET);
  3872. }
  3873. function containsEmptyPathMatches(segmentGroup, slicedSegments, routes) {
  3874. return routes.some((r) => emptyPathMatch(segmentGroup, slicedSegments, r));
  3875. }
  3876. function emptyPathMatch(segmentGroup, slicedSegments, r) {
  3877. if ((segmentGroup.hasChildren() || slicedSegments.length > 0) && r.pathMatch === 'full') {
  3878. return false;
  3879. }
  3880. return r.path === '';
  3881. }
  3882. function noLeftoversInUrl(segmentGroup, segments, outlet) {
  3883. return segments.length === 0 && !segmentGroup.children[outlet];
  3884. }
  3885. /**
  3886. * Class used to indicate there were no additional route config matches but that all segments of
  3887. * the URL were consumed during matching so the route was URL matched. When this happens, we still
  3888. * try to match child configs in case there are empty path children.
  3889. */
  3890. class NoLeftoversInUrl {
  3891. }
  3892. function recognize$1(injector, configLoader, rootComponentType, config, urlTree, urlSerializer, paramsInheritanceStrategy = 'emptyOnly') {
  3893. return new Recognizer(injector, configLoader, rootComponentType, config, urlTree, paramsInheritanceStrategy, urlSerializer).recognize();
  3894. }
  3895. const MAX_ALLOWED_REDIRECTS = 31;
  3896. class Recognizer {
  3897. injector;
  3898. configLoader;
  3899. rootComponentType;
  3900. config;
  3901. urlTree;
  3902. paramsInheritanceStrategy;
  3903. urlSerializer;
  3904. applyRedirects;
  3905. absoluteRedirectCount = 0;
  3906. allowRedirects = true;
  3907. constructor(injector, configLoader, rootComponentType, config, urlTree, paramsInheritanceStrategy, urlSerializer) {
  3908. this.injector = injector;
  3909. this.configLoader = configLoader;
  3910. this.rootComponentType = rootComponentType;
  3911. this.config = config;
  3912. this.urlTree = urlTree;
  3913. this.paramsInheritanceStrategy = paramsInheritanceStrategy;
  3914. this.urlSerializer = urlSerializer;
  3915. this.applyRedirects = new ApplyRedirects(this.urlSerializer, this.urlTree);
  3916. }
  3917. noMatchError(e) {
  3918. return new _RuntimeError(4002 /* RuntimeErrorCode.NO_MATCH */, typeof ngDevMode === 'undefined' || ngDevMode
  3919. ? `Cannot match any routes. URL Segment: '${e.segmentGroup}'`
  3920. : `'${e.segmentGroup}'`);
  3921. }
  3922. recognize() {
  3923. const rootSegmentGroup = split(this.urlTree.root, [], [], this.config).segmentGroup;
  3924. return this.match(rootSegmentGroup).pipe(map(({ children, rootSnapshot }) => {
  3925. const rootNode = new TreeNode(rootSnapshot, children);
  3926. const routeState = new RouterStateSnapshot('', rootNode);
  3927. const tree = createUrlTreeFromSnapshot(rootSnapshot, [], this.urlTree.queryParams, this.urlTree.fragment);
  3928. // https://github.com/angular/angular/issues/47307
  3929. // Creating the tree stringifies the query params
  3930. // We don't want to do this here so reassign them to the original.
  3931. tree.queryParams = this.urlTree.queryParams;
  3932. routeState.url = this.urlSerializer.serialize(tree);
  3933. return { state: routeState, tree };
  3934. }));
  3935. }
  3936. match(rootSegmentGroup) {
  3937. // Use Object.freeze to prevent readers of the Router state from modifying it outside
  3938. // of a navigation, resulting in the router being out of sync with the browser.
  3939. const rootSnapshot = new ActivatedRouteSnapshot([], Object.freeze({}), Object.freeze({ ...this.urlTree.queryParams }), this.urlTree.fragment, Object.freeze({}), PRIMARY_OUTLET, this.rootComponentType, null, {});
  3940. return this.processSegmentGroup(this.injector, this.config, rootSegmentGroup, PRIMARY_OUTLET, rootSnapshot).pipe(map((children) => {
  3941. return { children, rootSnapshot };
  3942. }), catchError((e) => {
  3943. if (e instanceof AbsoluteRedirect) {
  3944. this.urlTree = e.urlTree;
  3945. return this.match(e.urlTree.root);
  3946. }
  3947. if (e instanceof NoMatch) {
  3948. throw this.noMatchError(e);
  3949. }
  3950. throw e;
  3951. }));
  3952. }
  3953. processSegmentGroup(injector, config, segmentGroup, outlet, parentRoute) {
  3954. if (segmentGroup.segments.length === 0 && segmentGroup.hasChildren()) {
  3955. return this.processChildren(injector, config, segmentGroup, parentRoute);
  3956. }
  3957. return this.processSegment(injector, config, segmentGroup, segmentGroup.segments, outlet, true, parentRoute).pipe(map((child) => (child instanceof TreeNode ? [child] : [])));
  3958. }
  3959. /**
  3960. * Matches every child outlet in the `segmentGroup` to a `Route` in the config. Returns `null` if
  3961. * we cannot find a match for _any_ of the children.
  3962. *
  3963. * @param config - The `Routes` to match against
  3964. * @param segmentGroup - The `UrlSegmentGroup` whose children need to be matched against the
  3965. * config.
  3966. */
  3967. processChildren(injector, config, segmentGroup, parentRoute) {
  3968. // Expand outlets one at a time, starting with the primary outlet. We need to do it this way
  3969. // because an absolute redirect from the primary outlet takes precedence.
  3970. const childOutlets = [];
  3971. for (const child of Object.keys(segmentGroup.children)) {
  3972. if (child === 'primary') {
  3973. childOutlets.unshift(child);
  3974. }
  3975. else {
  3976. childOutlets.push(child);
  3977. }
  3978. }
  3979. return from(childOutlets).pipe(concatMap((childOutlet) => {
  3980. const child = segmentGroup.children[childOutlet];
  3981. // Sort the config so that routes with outlets that match the one being activated
  3982. // appear first, followed by routes for other outlets, which might match if they have
  3983. // an empty path.
  3984. const sortedConfig = sortByMatchingOutlets(config, childOutlet);
  3985. return this.processSegmentGroup(injector, sortedConfig, child, childOutlet, parentRoute);
  3986. }), scan((children, outletChildren) => {
  3987. children.push(...outletChildren);
  3988. return children;
  3989. }), defaultIfEmpty(null), last$1(), mergeMap((children) => {
  3990. if (children === null)
  3991. return noMatch$1(segmentGroup);
  3992. // Because we may have matched two outlets to the same empty path segment, we can have
  3993. // multiple activated results for the same outlet. We should merge the children of
  3994. // these results so the final return value is only one `TreeNode` per outlet.
  3995. const mergedChildren = mergeEmptyPathMatches(children);
  3996. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  3997. // This should really never happen - we are only taking the first match for each
  3998. // outlet and merge the empty path matches.
  3999. checkOutletNameUniqueness(mergedChildren);
  4000. }
  4001. sortActivatedRouteSnapshots(mergedChildren);
  4002. return of(mergedChildren);
  4003. }));
  4004. }
  4005. processSegment(injector, routes, segmentGroup, segments, outlet, allowRedirects, parentRoute) {
  4006. return from(routes).pipe(concatMap((r) => {
  4007. return this.processSegmentAgainstRoute(r._injector ?? injector, routes, r, segmentGroup, segments, outlet, allowRedirects, parentRoute).pipe(catchError((e) => {
  4008. if (e instanceof NoMatch) {
  4009. return of(null);
  4010. }
  4011. throw e;
  4012. }));
  4013. }), first((x) => !!x), catchError((e) => {
  4014. if (isEmptyError(e)) {
  4015. if (noLeftoversInUrl(segmentGroup, segments, outlet)) {
  4016. return of(new NoLeftoversInUrl());
  4017. }
  4018. return noMatch$1(segmentGroup);
  4019. }
  4020. throw e;
  4021. }));
  4022. }
  4023. processSegmentAgainstRoute(injector, routes, route, rawSegment, segments, outlet, allowRedirects, parentRoute) {
  4024. // We allow matches to empty paths when the outlets differ so we can match a url like `/(b:b)` to
  4025. // a config like
  4026. // * `{path: '', children: [{path: 'b', outlet: 'b'}]}`
  4027. // or even
  4028. // * `{path: '', outlet: 'a', children: [{path: 'b', outlet: 'b'}]`
  4029. //
  4030. // The exception here is when the segment outlet is for the primary outlet. This would
  4031. // result in a match inside the named outlet because all children there are written as primary
  4032. // outlets. So we need to prevent child named outlet matches in a url like `/b` in a config like
  4033. // * `{path: '', outlet: 'x' children: [{path: 'b'}]}`
  4034. // This should only match if the url is `/(x:b)`.
  4035. if (getOutlet(route) !== outlet &&
  4036. (outlet === PRIMARY_OUTLET || !emptyPathMatch(rawSegment, segments, route))) {
  4037. return noMatch$1(rawSegment);
  4038. }
  4039. if (route.redirectTo === undefined) {
  4040. return this.matchSegmentAgainstRoute(injector, rawSegment, route, segments, outlet, parentRoute);
  4041. }
  4042. if (this.allowRedirects && allowRedirects) {
  4043. return this.expandSegmentAgainstRouteUsingRedirect(injector, rawSegment, routes, route, segments, outlet, parentRoute);
  4044. }
  4045. return noMatch$1(rawSegment);
  4046. }
  4047. expandSegmentAgainstRouteUsingRedirect(injector, segmentGroup, routes, route, segments, outlet, parentRoute) {
  4048. const { matched, parameters, consumedSegments, positionalParamSegments, remainingSegments } = match(segmentGroup, route, segments);
  4049. if (!matched)
  4050. return noMatch$1(segmentGroup);
  4051. // TODO(atscott): Move all of this under an if(ngDevMode) as a breaking change and allow stack
  4052. // size exceeded in production
  4053. if (typeof route.redirectTo === 'string' && route.redirectTo[0] === '/') {
  4054. this.absoluteRedirectCount++;
  4055. if (this.absoluteRedirectCount > MAX_ALLOWED_REDIRECTS) {
  4056. if (ngDevMode) {
  4057. throw new _RuntimeError(4016 /* RuntimeErrorCode.INFINITE_REDIRECT */, `Detected possible infinite redirect when redirecting from '${this.urlTree}' to '${route.redirectTo}'.\n` +
  4058. `This is currently a dev mode only error but will become a` +
  4059. ` call stack size exceeded error in production in a future major version.`);
  4060. }
  4061. this.allowRedirects = false;
  4062. }
  4063. }
  4064. const currentSnapshot = new ActivatedRouteSnapshot(segments, parameters, Object.freeze({ ...this.urlTree.queryParams }), this.urlTree.fragment, getData(route), getOutlet(route), route.component ?? route._loadedComponent ?? null, route, getResolve(route));
  4065. const inherited = getInherited(currentSnapshot, parentRoute, this.paramsInheritanceStrategy);
  4066. currentSnapshot.params = Object.freeze(inherited.params);
  4067. currentSnapshot.data = Object.freeze(inherited.data);
  4068. const newTree = this.applyRedirects.applyRedirectCommands(consumedSegments, route.redirectTo, positionalParamSegments, currentSnapshot, injector);
  4069. return this.applyRedirects.lineralizeSegments(route, newTree).pipe(mergeMap((newSegments) => {
  4070. return this.processSegment(injector, routes, segmentGroup, newSegments.concat(remainingSegments), outlet, false, parentRoute);
  4071. }));
  4072. }
  4073. matchSegmentAgainstRoute(injector, rawSegment, route, segments, outlet, parentRoute) {
  4074. const matchResult = matchWithChecks(rawSegment, route, segments, injector, this.urlSerializer);
  4075. if (route.path === '**') {
  4076. // Prior versions of the route matching algorithm would stop matching at the wildcard route.
  4077. // We should investigate a better strategy for any existing children. Otherwise, these
  4078. // child segments are silently dropped from the navigation.
  4079. // https://github.com/angular/angular/issues/40089
  4080. rawSegment.children = {};
  4081. }
  4082. return matchResult.pipe(switchMap((result) => {
  4083. if (!result.matched) {
  4084. return noMatch$1(rawSegment);
  4085. }
  4086. // If the route has an injector created from providers, we should start using that.
  4087. injector = route._injector ?? injector;
  4088. return this.getChildConfig(injector, route, segments).pipe(switchMap(({ routes: childConfig }) => {
  4089. const childInjector = route._loadedInjector ?? injector;
  4090. const { parameters, consumedSegments, remainingSegments } = result;
  4091. const snapshot = new ActivatedRouteSnapshot(consumedSegments, parameters, Object.freeze({ ...this.urlTree.queryParams }), this.urlTree.fragment, getData(route), getOutlet(route), route.component ?? route._loadedComponent ?? null, route, getResolve(route));
  4092. const inherited = getInherited(snapshot, parentRoute, this.paramsInheritanceStrategy);
  4093. snapshot.params = Object.freeze(inherited.params);
  4094. snapshot.data = Object.freeze(inherited.data);
  4095. const { segmentGroup, slicedSegments } = split(rawSegment, consumedSegments, remainingSegments, childConfig);
  4096. if (slicedSegments.length === 0 && segmentGroup.hasChildren()) {
  4097. return this.processChildren(childInjector, childConfig, segmentGroup, snapshot).pipe(map((children) => {
  4098. return new TreeNode(snapshot, children);
  4099. }));
  4100. }
  4101. if (childConfig.length === 0 && slicedSegments.length === 0) {
  4102. return of(new TreeNode(snapshot, []));
  4103. }
  4104. const matchedOnOutlet = getOutlet(route) === outlet;
  4105. // If we matched a config due to empty path match on a different outlet, we need to
  4106. // continue passing the current outlet for the segment rather than switch to PRIMARY.
  4107. // Note that we switch to primary when we have a match because outlet configs look like
  4108. // this: {path: 'a', outlet: 'a', children: [
  4109. // {path: 'b', component: B},
  4110. // {path: 'c', component: C},
  4111. // ]}
  4112. // Notice that the children of the named outlet are configured with the primary outlet
  4113. return this.processSegment(childInjector, childConfig, segmentGroup, slicedSegments, matchedOnOutlet ? PRIMARY_OUTLET : outlet, true, snapshot).pipe(map((child) => {
  4114. return new TreeNode(snapshot, child instanceof TreeNode ? [child] : []);
  4115. }));
  4116. }));
  4117. }));
  4118. }
  4119. getChildConfig(injector, route, segments) {
  4120. if (route.children) {
  4121. // The children belong to the same module
  4122. return of({ routes: route.children, injector });
  4123. }
  4124. if (route.loadChildren) {
  4125. // lazy children belong to the loaded module
  4126. if (route._loadedRoutes !== undefined) {
  4127. return of({ routes: route._loadedRoutes, injector: route._loadedInjector });
  4128. }
  4129. return runCanLoadGuards(injector, route, segments, this.urlSerializer).pipe(mergeMap((shouldLoadResult) => {
  4130. if (shouldLoadResult) {
  4131. return this.configLoader.loadChildren(injector, route).pipe(tap((cfg) => {
  4132. route._loadedRoutes = cfg.routes;
  4133. route._loadedInjector = cfg.injector;
  4134. }));
  4135. }
  4136. return canLoadFails(route);
  4137. }));
  4138. }
  4139. return of({ routes: [], injector });
  4140. }
  4141. }
  4142. function sortActivatedRouteSnapshots(nodes) {
  4143. nodes.sort((a, b) => {
  4144. if (a.value.outlet === PRIMARY_OUTLET)
  4145. return -1;
  4146. if (b.value.outlet === PRIMARY_OUTLET)
  4147. return 1;
  4148. return a.value.outlet.localeCompare(b.value.outlet);
  4149. });
  4150. }
  4151. function hasEmptyPathConfig(node) {
  4152. const config = node.value.routeConfig;
  4153. return config && config.path === '';
  4154. }
  4155. /**
  4156. * Finds `TreeNode`s with matching empty path route configs and merges them into `TreeNode` with
  4157. * the children from each duplicate. This is necessary because different outlets can match a
  4158. * single empty path route config and the results need to then be merged.
  4159. */
  4160. function mergeEmptyPathMatches(nodes) {
  4161. const result = [];
  4162. // The set of nodes which contain children that were merged from two duplicate empty path nodes.
  4163. const mergedNodes = new Set();
  4164. for (const node of nodes) {
  4165. if (!hasEmptyPathConfig(node)) {
  4166. result.push(node);
  4167. continue;
  4168. }
  4169. const duplicateEmptyPathNode = result.find((resultNode) => node.value.routeConfig === resultNode.value.routeConfig);
  4170. if (duplicateEmptyPathNode !== undefined) {
  4171. duplicateEmptyPathNode.children.push(...node.children);
  4172. mergedNodes.add(duplicateEmptyPathNode);
  4173. }
  4174. else {
  4175. result.push(node);
  4176. }
  4177. }
  4178. // For each node which has children from multiple sources, we need to recompute a new `TreeNode`
  4179. // by also merging those children. This is necessary when there are multiple empty path configs
  4180. // in a row. Put another way: whenever we combine children of two nodes, we need to also check
  4181. // if any of those children can be combined into a single node as well.
  4182. for (const mergedNode of mergedNodes) {
  4183. const mergedChildren = mergeEmptyPathMatches(mergedNode.children);
  4184. result.push(new TreeNode(mergedNode.value, mergedChildren));
  4185. }
  4186. return result.filter((n) => !mergedNodes.has(n));
  4187. }
  4188. function checkOutletNameUniqueness(nodes) {
  4189. const names = {};
  4190. nodes.forEach((n) => {
  4191. const routeWithSameOutletName = names[n.value.outlet];
  4192. if (routeWithSameOutletName) {
  4193. const p = routeWithSameOutletName.url.map((s) => s.toString()).join('/');
  4194. const c = n.value.url.map((s) => s.toString()).join('/');
  4195. throw new _RuntimeError(4006 /* RuntimeErrorCode.TWO_SEGMENTS_WITH_SAME_OUTLET */, (typeof ngDevMode === 'undefined' || ngDevMode) &&
  4196. `Two segments cannot have the same outlet name: '${p}' and '${c}'.`);
  4197. }
  4198. names[n.value.outlet] = n.value;
  4199. });
  4200. }
  4201. function getData(route) {
  4202. return route.data || {};
  4203. }
  4204. function getResolve(route) {
  4205. return route.resolve || {};
  4206. }
  4207. function recognize(injector, configLoader, rootComponentType, config, serializer, paramsInheritanceStrategy) {
  4208. return mergeMap((t) => recognize$1(injector, configLoader, rootComponentType, config, t.extractedUrl, serializer, paramsInheritanceStrategy).pipe(map(({ state: targetSnapshot, tree: urlAfterRedirects }) => {
  4209. return { ...t, targetSnapshot, urlAfterRedirects };
  4210. })));
  4211. }
  4212. function resolveData(paramsInheritanceStrategy, injector) {
  4213. return mergeMap((t) => {
  4214. const { targetSnapshot, guards: { canActivateChecks }, } = t;
  4215. if (!canActivateChecks.length) {
  4216. return of(t);
  4217. }
  4218. // Iterating a Set in javascript happens in insertion order so it is safe to use a `Set` to
  4219. // preserve the correct order that the resolvers should run in.
  4220. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#description
  4221. const routesWithResolversToRun = new Set(canActivateChecks.map((check) => check.route));
  4222. const routesNeedingDataUpdates = new Set();
  4223. for (const route of routesWithResolversToRun) {
  4224. if (routesNeedingDataUpdates.has(route)) {
  4225. continue;
  4226. }
  4227. // All children under the route with a resolver to run need to recompute inherited data.
  4228. for (const newRoute of flattenRouteTree(route)) {
  4229. routesNeedingDataUpdates.add(newRoute);
  4230. }
  4231. }
  4232. let routesProcessed = 0;
  4233. return from(routesNeedingDataUpdates).pipe(concatMap((route) => {
  4234. if (routesWithResolversToRun.has(route)) {
  4235. return runResolve(route, targetSnapshot, paramsInheritanceStrategy, injector);
  4236. }
  4237. else {
  4238. route.data = getInherited(route, route.parent, paramsInheritanceStrategy).resolve;
  4239. return of(void 0);
  4240. }
  4241. }), tap(() => routesProcessed++), takeLast(1), mergeMap((_) => (routesProcessed === routesNeedingDataUpdates.size ? of(t) : EMPTY)));
  4242. });
  4243. }
  4244. /**
  4245. * Returns the `ActivatedRouteSnapshot` tree as an array, using DFS to traverse the route tree.
  4246. */
  4247. function flattenRouteTree(route) {
  4248. const descendants = route.children.map((child) => flattenRouteTree(child)).flat();
  4249. return [route, ...descendants];
  4250. }
  4251. function runResolve(futureARS, futureRSS, paramsInheritanceStrategy, injector) {
  4252. const config = futureARS.routeConfig;
  4253. const resolve = futureARS._resolve;
  4254. if (config?.title !== undefined && !hasStaticTitle(config)) {
  4255. resolve[RouteTitleKey] = config.title;
  4256. }
  4257. return resolveNode(resolve, futureARS, futureRSS, injector).pipe(map((resolvedData) => {
  4258. futureARS._resolvedData = resolvedData;
  4259. futureARS.data = getInherited(futureARS, futureARS.parent, paramsInheritanceStrategy).resolve;
  4260. return null;
  4261. }));
  4262. }
  4263. function resolveNode(resolve, futureARS, futureRSS, injector) {
  4264. const keys = getDataKeys(resolve);
  4265. if (keys.length === 0) {
  4266. return of({});
  4267. }
  4268. const data = {};
  4269. return from(keys).pipe(mergeMap((key) => getResolver(resolve[key], futureARS, futureRSS, injector).pipe(first(), tap((value) => {
  4270. if (value instanceof RedirectCommand) {
  4271. throw redirectingNavigationError(new DefaultUrlSerializer(), value);
  4272. }
  4273. data[key] = value;
  4274. }))), takeLast(1), map(() => data), catchError((e) => (isEmptyError(e) ? EMPTY : throwError(e))));
  4275. }
  4276. function getResolver(injectionToken, futureARS, futureRSS, injector) {
  4277. const closestInjector = getClosestRouteInjector(futureARS) ?? injector;
  4278. const resolver = getTokenOrFunctionIdentity(injectionToken, closestInjector);
  4279. const resolverValue = resolver.resolve
  4280. ? resolver.resolve(futureARS, futureRSS)
  4281. : runInInjectionContext(closestInjector, () => resolver(futureARS, futureRSS));
  4282. return wrapIntoObservable(resolverValue);
  4283. }
  4284. /**
  4285. * Perform a side effect through a switchMap for every emission on the source Observable,
  4286. * but return an Observable that is identical to the source. It's essentially the same as
  4287. * the `tap` operator, but if the side effectful `next` function returns an ObservableInput,
  4288. * it will wait before continuing with the original value.
  4289. */
  4290. function switchTap(next) {
  4291. return switchMap((v) => {
  4292. const nextResult = next(v);
  4293. if (nextResult) {
  4294. return from(nextResult).pipe(map(() => v));
  4295. }
  4296. return of(v);
  4297. });
  4298. }
  4299. /**
  4300. * Provides a strategy for setting the page title after a router navigation.
  4301. *
  4302. * The built-in implementation traverses the router state snapshot and finds the deepest primary
  4303. * outlet with `title` property. Given the `Routes` below, navigating to
  4304. * `/base/child(popup:aux)` would result in the document title being set to "child".
  4305. * ```ts
  4306. * [
  4307. * {path: 'base', title: 'base', children: [
  4308. * {path: 'child', title: 'child'},
  4309. * ],
  4310. * {path: 'aux', outlet: 'popup', title: 'popupTitle'}
  4311. * ]
  4312. * ```
  4313. *
  4314. * This class can be used as a base class for custom title strategies. That is, you can create your
  4315. * own class that extends the `TitleStrategy`. Note that in the above example, the `title`
  4316. * from the named outlet is never used. However, a custom strategy might be implemented to
  4317. * incorporate titles in named outlets.
  4318. *
  4319. * @publicApi
  4320. * @see [Page title guide](guide/routing/common-router-tasks#setting-the-page-title)
  4321. */
  4322. class TitleStrategy {
  4323. /**
  4324. * @returns The `title` of the deepest primary route.
  4325. */
  4326. buildTitle(snapshot) {
  4327. let pageTitle;
  4328. let route = snapshot.root;
  4329. while (route !== undefined) {
  4330. pageTitle = this.getResolvedTitleForRoute(route) ?? pageTitle;
  4331. route = route.children.find((child) => child.outlet === PRIMARY_OUTLET);
  4332. }
  4333. return pageTitle;
  4334. }
  4335. /**
  4336. * Given an `ActivatedRouteSnapshot`, returns the final value of the
  4337. * `Route.title` property, which can either be a static string or a resolved value.
  4338. */
  4339. getResolvedTitleForRoute(snapshot) {
  4340. return snapshot.data[RouteTitleKey];
  4341. }
  4342. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: TitleStrategy, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  4343. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: TitleStrategy, providedIn: 'root', useFactory: () => inject(DefaultTitleStrategy) });
  4344. }
  4345. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: TitleStrategy, decorators: [{
  4346. type: Injectable,
  4347. args: [{ providedIn: 'root', useFactory: () => inject(DefaultTitleStrategy) }]
  4348. }] });
  4349. /**
  4350. * The default `TitleStrategy` used by the router that updates the title using the `Title` service.
  4351. */
  4352. class DefaultTitleStrategy extends TitleStrategy {
  4353. title;
  4354. constructor(title) {
  4355. super();
  4356. this.title = title;
  4357. }
  4358. /**
  4359. * Sets the title of the browser to the given value.
  4360. *
  4361. * @param title The `pageTitle` from the deepest primary route.
  4362. */
  4363. updateTitle(snapshot) {
  4364. const title = this.buildTitle(snapshot);
  4365. if (title !== undefined) {
  4366. this.title.setTitle(title);
  4367. }
  4368. }
  4369. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: DefaultTitleStrategy, deps: [{ token: i1.Title }], target: i0.ɵɵFactoryTarget.Injectable });
  4370. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: DefaultTitleStrategy, providedIn: 'root' });
  4371. }
  4372. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: DefaultTitleStrategy, decorators: [{
  4373. type: Injectable,
  4374. args: [{ providedIn: 'root' }]
  4375. }], ctorParameters: () => [{ type: i1.Title }] });
  4376. /**
  4377. * A DI token for the router service.
  4378. *
  4379. * @publicApi
  4380. */
  4381. const ROUTER_CONFIGURATION = new InjectionToken(typeof ngDevMode === 'undefined' || ngDevMode ? 'router config' : '', {
  4382. providedIn: 'root',
  4383. factory: () => ({}),
  4384. });
  4385. /**
  4386. * This component is used internally within the router to be a placeholder when an empty
  4387. * router-outlet is needed. For example, with a config such as:
  4388. *
  4389. * `{path: 'parent', outlet: 'nav', children: [...]}`
  4390. *
  4391. * In order to render, there needs to be a component on this config, which will default
  4392. * to this `EmptyOutletComponent`.
  4393. */
  4394. class ɵEmptyOutletComponent {
  4395. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: ɵEmptyOutletComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
  4396. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.2.4", type: ɵEmptyOutletComponent, isStandalone: true, selector: "ng-component", exportAs: ["emptyRouterOutlet"], ngImport: i0, template: `<router-outlet/>`, isInline: true, dependencies: [{ kind: "directive", type: RouterOutlet, selector: "router-outlet", inputs: ["name", "routerOutletData"], outputs: ["activate", "deactivate", "attach", "detach"], exportAs: ["outlet"] }] });
  4397. }
  4398. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: ɵEmptyOutletComponent, decorators: [{
  4399. type: Component,
  4400. args: [{
  4401. template: `<router-outlet/>`,
  4402. imports: [RouterOutlet],
  4403. // Used to avoid component ID collisions with user code.
  4404. exportAs: 'emptyRouterOutlet',
  4405. }]
  4406. }] });
  4407. /**
  4408. * Makes a copy of the config and adds any default required properties.
  4409. */
  4410. function standardizeConfig(r) {
  4411. const children = r.children && r.children.map(standardizeConfig);
  4412. const c = children ? { ...r, children } : { ...r };
  4413. if (!c.component &&
  4414. !c.loadComponent &&
  4415. (children || c.loadChildren) &&
  4416. c.outlet &&
  4417. c.outlet !== PRIMARY_OUTLET) {
  4418. c.component = ɵEmptyOutletComponent;
  4419. }
  4420. return c;
  4421. }
  4422. /**
  4423. * The DI token for a router configuration.
  4424. *
  4425. * `ROUTES` is a low level API for router configuration via dependency injection.
  4426. *
  4427. * We recommend that in almost all cases to use higher level APIs such as `RouterModule.forRoot()`,
  4428. * `provideRouter`, or `Router.resetConfig()`.
  4429. *
  4430. * @publicApi
  4431. */
  4432. const ROUTES = new InjectionToken(ngDevMode ? 'ROUTES' : '');
  4433. class RouterConfigLoader {
  4434. componentLoaders = new WeakMap();
  4435. childrenLoaders = new WeakMap();
  4436. onLoadStartListener;
  4437. onLoadEndListener;
  4438. compiler = inject(Compiler);
  4439. loadComponent(route) {
  4440. if (this.componentLoaders.get(route)) {
  4441. return this.componentLoaders.get(route);
  4442. }
  4443. else if (route._loadedComponent) {
  4444. return of(route._loadedComponent);
  4445. }
  4446. if (this.onLoadStartListener) {
  4447. this.onLoadStartListener(route);
  4448. }
  4449. const loadRunner = wrapIntoObservable(route.loadComponent()).pipe(map(maybeUnwrapDefaultExport), tap((component) => {
  4450. if (this.onLoadEndListener) {
  4451. this.onLoadEndListener(route);
  4452. }
  4453. (typeof ngDevMode === 'undefined' || ngDevMode) &&
  4454. assertStandalone(route.path ?? '', component);
  4455. route._loadedComponent = component;
  4456. }), finalize(() => {
  4457. this.componentLoaders.delete(route);
  4458. }));
  4459. // Use custom ConnectableObservable as share in runners pipe increasing the bundle size too much
  4460. const loader = new ConnectableObservable(loadRunner, () => new Subject()).pipe(refCount());
  4461. this.componentLoaders.set(route, loader);
  4462. return loader;
  4463. }
  4464. loadChildren(parentInjector, route) {
  4465. if (this.childrenLoaders.get(route)) {
  4466. return this.childrenLoaders.get(route);
  4467. }
  4468. else if (route._loadedRoutes) {
  4469. return of({ routes: route._loadedRoutes, injector: route._loadedInjector });
  4470. }
  4471. if (this.onLoadStartListener) {
  4472. this.onLoadStartListener(route);
  4473. }
  4474. const moduleFactoryOrRoutes$ = loadChildren(route, this.compiler, parentInjector, this.onLoadEndListener);
  4475. const loadRunner = moduleFactoryOrRoutes$.pipe(finalize(() => {
  4476. this.childrenLoaders.delete(route);
  4477. }));
  4478. // Use custom ConnectableObservable as share in runners pipe increasing the bundle size too much
  4479. const loader = new ConnectableObservable(loadRunner, () => new Subject()).pipe(refCount());
  4480. this.childrenLoaders.set(route, loader);
  4481. return loader;
  4482. }
  4483. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterConfigLoader, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  4484. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterConfigLoader, providedIn: 'root' });
  4485. }
  4486. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterConfigLoader, decorators: [{
  4487. type: Injectable,
  4488. args: [{ providedIn: 'root' }]
  4489. }] });
  4490. /**
  4491. * Executes a `route.loadChildren` callback and converts the result to an array of child routes and
  4492. * an injector if that callback returned a module.
  4493. *
  4494. * This function is used for the route discovery during prerendering
  4495. * in @angular-devkit/build-angular. If there are any updates to the contract here, it will require
  4496. * an update to the extractor.
  4497. */
  4498. function loadChildren(route, compiler, parentInjector, onLoadEndListener) {
  4499. return wrapIntoObservable(route.loadChildren()).pipe(map(maybeUnwrapDefaultExport), mergeMap((t) => {
  4500. if (t instanceof NgModuleFactory || Array.isArray(t)) {
  4501. return of(t);
  4502. }
  4503. else {
  4504. return from(compiler.compileModuleAsync(t));
  4505. }
  4506. }), map((factoryOrRoutes) => {
  4507. if (onLoadEndListener) {
  4508. onLoadEndListener(route);
  4509. }
  4510. // This injector comes from the `NgModuleRef` when lazy loading an `NgModule`. There is
  4511. // no injector associated with lazy loading a `Route` array.
  4512. let injector;
  4513. let rawRoutes;
  4514. let requireStandaloneComponents = false;
  4515. if (Array.isArray(factoryOrRoutes)) {
  4516. rawRoutes = factoryOrRoutes;
  4517. requireStandaloneComponents = true;
  4518. }
  4519. else {
  4520. injector = factoryOrRoutes.create(parentInjector).injector;
  4521. // When loading a module that doesn't provide `RouterModule.forChild()` preloader
  4522. // will get stuck in an infinite loop. The child module's Injector will look to
  4523. // its parent `Injector` when it doesn't find any ROUTES so it will return routes
  4524. // for it's parent module instead.
  4525. rawRoutes = injector.get(ROUTES, [], { optional: true, self: true }).flat();
  4526. }
  4527. const routes = rawRoutes.map(standardizeConfig);
  4528. (typeof ngDevMode === 'undefined' || ngDevMode) &&
  4529. validateConfig(routes, route.path, requireStandaloneComponents);
  4530. return { routes, injector };
  4531. }));
  4532. }
  4533. function isWrappedDefaultExport(value) {
  4534. // We use `in` here with a string key `'default'`, because we expect `DefaultExport` objects to be
  4535. // dynamically imported ES modules with a spec-mandated `default` key. Thus we don't expect that
  4536. // `default` will be a renamed property.
  4537. return value && typeof value === 'object' && 'default' in value;
  4538. }
  4539. function maybeUnwrapDefaultExport(input) {
  4540. // As per `isWrappedDefaultExport`, the `default` key here is generated by the browser and not
  4541. // subject to property renaming, so we reference it with bracket access.
  4542. return isWrappedDefaultExport(input) ? input['default'] : input;
  4543. }
  4544. /**
  4545. * @description
  4546. *
  4547. * Provides a way to migrate AngularJS applications to Angular.
  4548. *
  4549. * @publicApi
  4550. */
  4551. class UrlHandlingStrategy {
  4552. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: UrlHandlingStrategy, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  4553. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: UrlHandlingStrategy, providedIn: 'root', useFactory: () => inject(DefaultUrlHandlingStrategy) });
  4554. }
  4555. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: UrlHandlingStrategy, decorators: [{
  4556. type: Injectable,
  4557. args: [{ providedIn: 'root', useFactory: () => inject(DefaultUrlHandlingStrategy) }]
  4558. }] });
  4559. /**
  4560. * @publicApi
  4561. */
  4562. class DefaultUrlHandlingStrategy {
  4563. shouldProcessUrl(url) {
  4564. return true;
  4565. }
  4566. extract(url) {
  4567. return url;
  4568. }
  4569. merge(newUrlPart, wholeUrl) {
  4570. return newUrlPart;
  4571. }
  4572. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: DefaultUrlHandlingStrategy, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  4573. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: DefaultUrlHandlingStrategy, providedIn: 'root' });
  4574. }
  4575. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: DefaultUrlHandlingStrategy, decorators: [{
  4576. type: Injectable,
  4577. args: [{ providedIn: 'root' }]
  4578. }] });
  4579. /// <reference types="dom-view-transitions" />
  4580. const CREATE_VIEW_TRANSITION = new InjectionToken(ngDevMode ? 'view transition helper' : '');
  4581. const VIEW_TRANSITION_OPTIONS = new InjectionToken(ngDevMode ? 'view transition options' : '');
  4582. /**
  4583. * A helper function for using browser view transitions. This function skips the call to
  4584. * `startViewTransition` if the browser does not support it.
  4585. *
  4586. * @returns A Promise that resolves when the view transition callback begins.
  4587. */
  4588. function createViewTransition(injector, from, to) {
  4589. const transitionOptions = injector.get(VIEW_TRANSITION_OPTIONS);
  4590. const document = injector.get(DOCUMENT);
  4591. // Create promises outside the Angular zone to avoid causing extra change detections
  4592. return injector.get(NgZone).runOutsideAngular(() => {
  4593. if (!document.startViewTransition || transitionOptions.skipNextTransition) {
  4594. transitionOptions.skipNextTransition = false;
  4595. // The timing of `startViewTransition` is closer to a macrotask. It won't be called
  4596. // until the current event loop exits so we use a promise resolved in a timeout instead
  4597. // of Promise.resolve().
  4598. return new Promise((resolve) => setTimeout(resolve));
  4599. }
  4600. let resolveViewTransitionStarted;
  4601. const viewTransitionStarted = new Promise((resolve) => {
  4602. resolveViewTransitionStarted = resolve;
  4603. });
  4604. const transition = document.startViewTransition(() => {
  4605. resolveViewTransitionStarted();
  4606. // We don't actually update dom within the transition callback. The resolving of the above
  4607. // promise unblocks the Router navigation, which synchronously activates and deactivates
  4608. // routes (the DOM update). This view transition waits for the next change detection to
  4609. // complete (below), which includes the update phase of the routed components.
  4610. return createRenderPromise(injector);
  4611. });
  4612. const { onViewTransitionCreated } = transitionOptions;
  4613. if (onViewTransitionCreated) {
  4614. runInInjectionContext(injector, () => onViewTransitionCreated({ transition, from, to }));
  4615. }
  4616. return viewTransitionStarted;
  4617. });
  4618. }
  4619. /**
  4620. * Creates a promise that resolves after next render.
  4621. */
  4622. function createRenderPromise(injector) {
  4623. return new Promise((resolve) => {
  4624. // Wait for the microtask queue to empty after the next render happens (by waiting a macrotask).
  4625. // This ensures any follow-up renders in the microtask queue are completed before the
  4626. // view transition starts animating.
  4627. afterNextRender({ read: () => setTimeout(resolve) }, { injector });
  4628. });
  4629. }
  4630. const NAVIGATION_ERROR_HANDLER = new InjectionToken(typeof ngDevMode === 'undefined' || ngDevMode ? 'navigation error handler' : '');
  4631. class NavigationTransitions {
  4632. currentNavigation = null;
  4633. currentTransition = null;
  4634. lastSuccessfulNavigation = null;
  4635. /**
  4636. * These events are used to communicate back to the Router about the state of the transition. The
  4637. * Router wants to respond to these events in various ways. Because the `NavigationTransition`
  4638. * class is not public, this event subject is not publicly exposed.
  4639. */
  4640. events = new Subject();
  4641. /**
  4642. * Used to abort the current transition with an error.
  4643. */
  4644. transitionAbortSubject = new Subject();
  4645. configLoader = inject(RouterConfigLoader);
  4646. environmentInjector = inject(EnvironmentInjector);
  4647. destroyRef = inject(DestroyRef);
  4648. urlSerializer = inject(UrlSerializer);
  4649. rootContexts = inject(ChildrenOutletContexts);
  4650. location = inject(Location);
  4651. inputBindingEnabled = inject(INPUT_BINDER, { optional: true }) !== null;
  4652. titleStrategy = inject(TitleStrategy);
  4653. options = inject(ROUTER_CONFIGURATION, { optional: true }) || {};
  4654. paramsInheritanceStrategy = this.options.paramsInheritanceStrategy || 'emptyOnly';
  4655. urlHandlingStrategy = inject(UrlHandlingStrategy);
  4656. createViewTransition = inject(CREATE_VIEW_TRANSITION, { optional: true });
  4657. navigationErrorHandler = inject(NAVIGATION_ERROR_HANDLER, { optional: true });
  4658. navigationId = 0;
  4659. get hasRequestedNavigation() {
  4660. return this.navigationId !== 0;
  4661. }
  4662. transitions;
  4663. /**
  4664. * Hook that enables you to pause navigation after the preactivation phase.
  4665. * Used by `RouterModule`.
  4666. *
  4667. * @internal
  4668. */
  4669. afterPreactivation = () => of(void 0);
  4670. /** @internal */
  4671. rootComponentType = null;
  4672. destroyed = false;
  4673. constructor() {
  4674. const onLoadStart = (r) => this.events.next(new RouteConfigLoadStart(r));
  4675. const onLoadEnd = (r) => this.events.next(new RouteConfigLoadEnd(r));
  4676. this.configLoader.onLoadEndListener = onLoadEnd;
  4677. this.configLoader.onLoadStartListener = onLoadStart;
  4678. this.destroyRef.onDestroy(() => {
  4679. this.destroyed = true;
  4680. });
  4681. }
  4682. complete() {
  4683. this.transitions?.complete();
  4684. }
  4685. handleNavigationRequest(request) {
  4686. const id = ++this.navigationId;
  4687. this.transitions?.next({
  4688. ...request,
  4689. extractedUrl: this.urlHandlingStrategy.extract(request.rawUrl),
  4690. targetSnapshot: null,
  4691. targetRouterState: null,
  4692. guards: { canActivateChecks: [], canDeactivateChecks: [] },
  4693. guardsResult: null,
  4694. id,
  4695. });
  4696. }
  4697. setupNavigations(router) {
  4698. this.transitions = new BehaviorSubject(null);
  4699. return this.transitions.pipe(filter((t) => t !== null),
  4700. // Using switchMap so we cancel executing navigations when a new one comes in
  4701. switchMap((overallTransitionState) => {
  4702. let completed = false;
  4703. let errored = false;
  4704. return of(overallTransitionState).pipe(switchMap((t) => {
  4705. // It is possible that `switchMap` fails to cancel previous navigations if a new one happens synchronously while the operator
  4706. // is processing the `next` notification of that previous navigation. This can happen when a new navigation (say 2) cancels a
  4707. // previous one (1) and yet another navigation (3) happens synchronously in response to the `NavigationCancel` event for (1).
  4708. // https://github.com/ReactiveX/rxjs/issues/7455
  4709. if (this.navigationId > overallTransitionState.id) {
  4710. const cancellationReason = typeof ngDevMode === 'undefined' || ngDevMode
  4711. ? `Navigation ID ${overallTransitionState.id} is not equal to the current navigation id ${this.navigationId}`
  4712. : '';
  4713. this.cancelNavigationTransition(overallTransitionState, cancellationReason, NavigationCancellationCode.SupersededByNewNavigation);
  4714. return EMPTY;
  4715. }
  4716. this.currentTransition = overallTransitionState;
  4717. // Store the Navigation object
  4718. this.currentNavigation = {
  4719. id: t.id,
  4720. initialUrl: t.rawUrl,
  4721. extractedUrl: t.extractedUrl,
  4722. targetBrowserUrl: typeof t.extras.browserUrl === 'string'
  4723. ? this.urlSerializer.parse(t.extras.browserUrl)
  4724. : t.extras.browserUrl,
  4725. trigger: t.source,
  4726. extras: t.extras,
  4727. previousNavigation: !this.lastSuccessfulNavigation
  4728. ? null
  4729. : {
  4730. ...this.lastSuccessfulNavigation,
  4731. previousNavigation: null,
  4732. },
  4733. };
  4734. const urlTransition = !router.navigated || this.isUpdatingInternalState() || this.isUpdatedBrowserUrl();
  4735. const onSameUrlNavigation = t.extras.onSameUrlNavigation ?? router.onSameUrlNavigation;
  4736. if (!urlTransition && onSameUrlNavigation !== 'reload') {
  4737. const reason = typeof ngDevMode === 'undefined' || ngDevMode
  4738. ? `Navigation to ${t.rawUrl} was ignored because it is the same as the current Router URL.`
  4739. : '';
  4740. this.events.next(new NavigationSkipped(t.id, this.urlSerializer.serialize(t.rawUrl), reason, NavigationSkippedCode.IgnoredSameUrlNavigation));
  4741. t.resolve(false);
  4742. return EMPTY;
  4743. }
  4744. if (this.urlHandlingStrategy.shouldProcessUrl(t.rawUrl)) {
  4745. return of(t).pipe(
  4746. // Fire NavigationStart event
  4747. switchMap((t) => {
  4748. this.events.next(new NavigationStart(t.id, this.urlSerializer.serialize(t.extractedUrl), t.source, t.restoredState));
  4749. if (t.id !== this.navigationId) {
  4750. return EMPTY;
  4751. }
  4752. // This delay is required to match old behavior that forced
  4753. // navigation to always be async
  4754. return Promise.resolve(t);
  4755. }),
  4756. // Recognize
  4757. recognize(this.environmentInjector, this.configLoader, this.rootComponentType, router.config, this.urlSerializer, this.paramsInheritanceStrategy),
  4758. // Update URL if in `eager` update mode
  4759. tap((t) => {
  4760. overallTransitionState.targetSnapshot = t.targetSnapshot;
  4761. overallTransitionState.urlAfterRedirects = t.urlAfterRedirects;
  4762. this.currentNavigation = {
  4763. ...this.currentNavigation,
  4764. finalUrl: t.urlAfterRedirects,
  4765. };
  4766. // Fire RoutesRecognized
  4767. const routesRecognized = new RoutesRecognized(t.id, this.urlSerializer.serialize(t.extractedUrl), this.urlSerializer.serialize(t.urlAfterRedirects), t.targetSnapshot);
  4768. this.events.next(routesRecognized);
  4769. }));
  4770. }
  4771. else if (urlTransition &&
  4772. this.urlHandlingStrategy.shouldProcessUrl(t.currentRawUrl)) {
  4773. /* When the current URL shouldn't be processed, but the previous one
  4774. * was, we handle this "error condition" by navigating to the
  4775. * previously successful URL, but leaving the URL intact.*/
  4776. const { id, extractedUrl, source, restoredState, extras } = t;
  4777. const navStart = new NavigationStart(id, this.urlSerializer.serialize(extractedUrl), source, restoredState);
  4778. this.events.next(navStart);
  4779. const targetSnapshot = createEmptyState(this.rootComponentType).snapshot;
  4780. this.currentTransition = overallTransitionState = {
  4781. ...t,
  4782. targetSnapshot,
  4783. urlAfterRedirects: extractedUrl,
  4784. extras: { ...extras, skipLocationChange: false, replaceUrl: false },
  4785. };
  4786. this.currentNavigation.finalUrl = extractedUrl;
  4787. return of(overallTransitionState);
  4788. }
  4789. else {
  4790. /* When neither the current or previous URL can be processed, do
  4791. * nothing other than update router's internal reference to the
  4792. * current "settled" URL. This way the next navigation will be coming
  4793. * from the current URL in the browser.
  4794. */
  4795. const reason = typeof ngDevMode === 'undefined' || ngDevMode
  4796. ? `Navigation was ignored because the UrlHandlingStrategy` +
  4797. ` indicated neither the current URL ${t.currentRawUrl} nor target URL ${t.rawUrl} should be processed.`
  4798. : '';
  4799. this.events.next(new NavigationSkipped(t.id, this.urlSerializer.serialize(t.extractedUrl), reason, NavigationSkippedCode.IgnoredByUrlHandlingStrategy));
  4800. t.resolve(false);
  4801. return EMPTY;
  4802. }
  4803. }),
  4804. // --- GUARDS ---
  4805. tap((t) => {
  4806. const guardsStart = new GuardsCheckStart(t.id, this.urlSerializer.serialize(t.extractedUrl), this.urlSerializer.serialize(t.urlAfterRedirects), t.targetSnapshot);
  4807. this.events.next(guardsStart);
  4808. }), map((t) => {
  4809. this.currentTransition = overallTransitionState = {
  4810. ...t,
  4811. guards: getAllRouteGuards(t.targetSnapshot, t.currentSnapshot, this.rootContexts),
  4812. };
  4813. return overallTransitionState;
  4814. }), checkGuards(this.environmentInjector, (evt) => this.events.next(evt)), tap((t) => {
  4815. overallTransitionState.guardsResult = t.guardsResult;
  4816. if (t.guardsResult && typeof t.guardsResult !== 'boolean') {
  4817. throw redirectingNavigationError(this.urlSerializer, t.guardsResult);
  4818. }
  4819. const guardsEnd = new GuardsCheckEnd(t.id, this.urlSerializer.serialize(t.extractedUrl), this.urlSerializer.serialize(t.urlAfterRedirects), t.targetSnapshot, !!t.guardsResult);
  4820. this.events.next(guardsEnd);
  4821. }), filter((t) => {
  4822. if (!t.guardsResult) {
  4823. this.cancelNavigationTransition(t, '', NavigationCancellationCode.GuardRejected);
  4824. return false;
  4825. }
  4826. return true;
  4827. }),
  4828. // --- RESOLVE ---
  4829. switchTap((t) => {
  4830. if (t.guards.canActivateChecks.length === 0) {
  4831. return undefined;
  4832. }
  4833. return of(t).pipe(tap((t) => {
  4834. const resolveStart = new ResolveStart(t.id, this.urlSerializer.serialize(t.extractedUrl), this.urlSerializer.serialize(t.urlAfterRedirects), t.targetSnapshot);
  4835. this.events.next(resolveStart);
  4836. }), switchMap((t) => {
  4837. let dataResolved = false;
  4838. return of(t).pipe(resolveData(this.paramsInheritanceStrategy, this.environmentInjector), tap({
  4839. next: () => (dataResolved = true),
  4840. complete: () => {
  4841. if (!dataResolved) {
  4842. this.cancelNavigationTransition(t, typeof ngDevMode === 'undefined' || ngDevMode
  4843. ? `At least one route resolver didn't emit any value.`
  4844. : '', NavigationCancellationCode.NoDataFromResolver);
  4845. }
  4846. },
  4847. }));
  4848. }), tap((t) => {
  4849. const resolveEnd = new ResolveEnd(t.id, this.urlSerializer.serialize(t.extractedUrl), this.urlSerializer.serialize(t.urlAfterRedirects), t.targetSnapshot);
  4850. this.events.next(resolveEnd);
  4851. }));
  4852. }),
  4853. // --- LOAD COMPONENTS ---
  4854. switchTap((t) => {
  4855. const loadComponents = (route) => {
  4856. const loaders = [];
  4857. if (route.routeConfig?.loadComponent && !route.routeConfig._loadedComponent) {
  4858. loaders.push(this.configLoader.loadComponent(route.routeConfig).pipe(tap((loadedComponent) => {
  4859. route.component = loadedComponent;
  4860. }), map(() => void 0)));
  4861. }
  4862. for (const child of route.children) {
  4863. loaders.push(...loadComponents(child));
  4864. }
  4865. return loaders;
  4866. };
  4867. return combineLatest(loadComponents(t.targetSnapshot.root)).pipe(defaultIfEmpty(null), take(1));
  4868. }), switchTap(() => this.afterPreactivation()), switchMap(() => {
  4869. const { currentSnapshot, targetSnapshot } = overallTransitionState;
  4870. const viewTransitionStarted = this.createViewTransition?.(this.environmentInjector, currentSnapshot.root, targetSnapshot.root);
  4871. // If view transitions are enabled, block the navigation until the view
  4872. // transition callback starts. Otherwise, continue immediately.
  4873. return viewTransitionStarted
  4874. ? from(viewTransitionStarted).pipe(map(() => overallTransitionState))
  4875. : of(overallTransitionState);
  4876. }), map((t) => {
  4877. const targetRouterState = createRouterState(router.routeReuseStrategy, t.targetSnapshot, t.currentRouterState);
  4878. this.currentTransition = overallTransitionState = { ...t, targetRouterState };
  4879. this.currentNavigation.targetRouterState = targetRouterState;
  4880. return overallTransitionState;
  4881. }), tap(() => {
  4882. this.events.next(new BeforeActivateRoutes());
  4883. }), activateRoutes(this.rootContexts, router.routeReuseStrategy, (evt) => this.events.next(evt), this.inputBindingEnabled),
  4884. // Ensure that if some observable used to drive the transition doesn't
  4885. // complete, the navigation still finalizes This should never happen, but
  4886. // this is done as a safety measure to avoid surfacing this error (#49567).
  4887. take(1), tap({
  4888. next: (t) => {
  4889. completed = true;
  4890. this.lastSuccessfulNavigation = this.currentNavigation;
  4891. this.events.next(new NavigationEnd(t.id, this.urlSerializer.serialize(t.extractedUrl), this.urlSerializer.serialize(t.urlAfterRedirects)));
  4892. this.titleStrategy?.updateTitle(t.targetRouterState.snapshot);
  4893. t.resolve(true);
  4894. },
  4895. complete: () => {
  4896. completed = true;
  4897. },
  4898. }),
  4899. // There used to be a lot more logic happening directly within the
  4900. // transition Observable. Some of this logic has been refactored out to
  4901. // other places but there may still be errors that happen there. This gives
  4902. // us a way to cancel the transition from the outside. This may also be
  4903. // required in the future to support something like the abort signal of the
  4904. // Navigation API where the navigation gets aborted from outside the
  4905. // transition.
  4906. takeUntil(this.transitionAbortSubject.pipe(tap((err) => {
  4907. throw err;
  4908. }))), finalize(() => {
  4909. /* When the navigation stream finishes either through error or success,
  4910. * we set the `completed` or `errored` flag. However, there are some
  4911. * situations where we could get here without either of those being set.
  4912. * For instance, a redirect during NavigationStart. Therefore, this is a
  4913. * catch-all to make sure the NavigationCancel event is fired when a
  4914. * navigation gets cancelled but not caught by other means. */
  4915. if (!completed && !errored) {
  4916. const cancelationReason = typeof ngDevMode === 'undefined' || ngDevMode
  4917. ? `Navigation ID ${overallTransitionState.id} is not equal to the current navigation id ${this.navigationId}`
  4918. : '';
  4919. this.cancelNavigationTransition(overallTransitionState, cancelationReason, NavigationCancellationCode.SupersededByNewNavigation);
  4920. }
  4921. // Only clear current navigation if it is still set to the one that
  4922. // finalized.
  4923. if (this.currentTransition?.id === overallTransitionState.id) {
  4924. this.currentNavigation = null;
  4925. this.currentTransition = null;
  4926. }
  4927. }), catchError((e) => {
  4928. // If the application is already destroyed, the catch block should not
  4929. // execute anything in practice because other resources have already
  4930. // been released and destroyed.
  4931. if (this.destroyed) {
  4932. overallTransitionState.resolve(false);
  4933. return EMPTY;
  4934. }
  4935. errored = true;
  4936. /* This error type is issued during Redirect, and is handled as a
  4937. * cancellation rather than an error. */
  4938. if (isNavigationCancelingError(e)) {
  4939. this.events.next(new NavigationCancel(overallTransitionState.id, this.urlSerializer.serialize(overallTransitionState.extractedUrl), e.message, e.cancellationCode));
  4940. // When redirecting, we need to delay resolving the navigation
  4941. // promise and push it to the redirect navigation
  4942. if (!isRedirectingNavigationCancelingError(e)) {
  4943. overallTransitionState.resolve(false);
  4944. }
  4945. else {
  4946. this.events.next(new RedirectRequest(e.url, e.navigationBehaviorOptions));
  4947. }
  4948. /* All other errors should reset to the router's internal URL reference
  4949. * to the pre-error state. */
  4950. }
  4951. else {
  4952. const navigationError = new NavigationError(overallTransitionState.id, this.urlSerializer.serialize(overallTransitionState.extractedUrl), e, overallTransitionState.targetSnapshot ?? undefined);
  4953. try {
  4954. const navigationErrorHandlerResult = runInInjectionContext(this.environmentInjector, () => this.navigationErrorHandler?.(navigationError));
  4955. if (navigationErrorHandlerResult instanceof RedirectCommand) {
  4956. const { message, cancellationCode } = redirectingNavigationError(this.urlSerializer, navigationErrorHandlerResult);
  4957. this.events.next(new NavigationCancel(overallTransitionState.id, this.urlSerializer.serialize(overallTransitionState.extractedUrl), message, cancellationCode));
  4958. this.events.next(new RedirectRequest(navigationErrorHandlerResult.redirectTo, navigationErrorHandlerResult.navigationBehaviorOptions));
  4959. }
  4960. else {
  4961. this.events.next(navigationError);
  4962. throw e;
  4963. }
  4964. }
  4965. catch (ee) {
  4966. // TODO(atscott): consider flipping the default behavior of
  4967. // resolveNavigationPromiseOnError to be `resolve(false)` when
  4968. // undefined. This is the most sane thing to do given that
  4969. // applications very rarely handle the promise rejection and, as a
  4970. // result, would get "unhandled promise rejection" console logs.
  4971. // The vast majority of applications would not be affected by this
  4972. // change so omitting a migration seems reasonable. Instead,
  4973. // applications that rely on rejection can specifically opt-in to the
  4974. // old behavior.
  4975. if (this.options.resolveNavigationPromiseOnError) {
  4976. overallTransitionState.resolve(false);
  4977. }
  4978. else {
  4979. overallTransitionState.reject(ee);
  4980. }
  4981. }
  4982. }
  4983. return EMPTY;
  4984. }));
  4985. // casting because `pipe` returns observable({}) when called with 8+ arguments
  4986. }));
  4987. }
  4988. cancelNavigationTransition(t, reason, code) {
  4989. const navCancel = new NavigationCancel(t.id, this.urlSerializer.serialize(t.extractedUrl), reason, code);
  4990. this.events.next(navCancel);
  4991. t.resolve(false);
  4992. }
  4993. /**
  4994. * @returns Whether we're navigating to somewhere that is not what the Router is
  4995. * currently set to.
  4996. */
  4997. isUpdatingInternalState() {
  4998. // TODO(atscott): The serializer should likely be used instead of
  4999. // `UrlTree.toString()`. Custom serializers are often written to handle
  5000. // things better than the default one (objects, for example will be
  5001. // [Object object] with the custom serializer and be "the same" when they
  5002. // aren't).
  5003. // (Same for isUpdatedBrowserUrl)
  5004. return (this.currentTransition?.extractedUrl.toString() !==
  5005. this.currentTransition?.currentUrlTree.toString());
  5006. }
  5007. /**
  5008. * @returns Whether we're updating the browser URL to something new (navigation is going
  5009. * to somewhere not displayed in the URL bar and we will update the URL
  5010. * bar if navigation succeeds).
  5011. */
  5012. isUpdatedBrowserUrl() {
  5013. // The extracted URL is the part of the URL that this application cares about. `extract` may
  5014. // return only part of the browser URL and that part may have not changed even if some other
  5015. // portion of the URL did.
  5016. const currentBrowserUrl = this.urlHandlingStrategy.extract(this.urlSerializer.parse(this.location.path(true)));
  5017. const targetBrowserUrl = this.currentNavigation?.targetBrowserUrl ?? this.currentNavigation?.extractedUrl;
  5018. return (currentBrowserUrl.toString() !== targetBrowserUrl?.toString() &&
  5019. !this.currentNavigation?.extras.skipLocationChange);
  5020. }
  5021. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: NavigationTransitions, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  5022. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: NavigationTransitions, providedIn: 'root' });
  5023. }
  5024. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: NavigationTransitions, decorators: [{
  5025. type: Injectable,
  5026. args: [{ providedIn: 'root' }]
  5027. }], ctorParameters: () => [] });
  5028. function isBrowserTriggeredNavigation(source) {
  5029. return source !== IMPERATIVE_NAVIGATION;
  5030. }
  5031. /**
  5032. * @description
  5033. *
  5034. * Provides a way to customize when activated routes get reused.
  5035. *
  5036. * @publicApi
  5037. */
  5038. class RouteReuseStrategy {
  5039. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouteReuseStrategy, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  5040. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouteReuseStrategy, providedIn: 'root', useFactory: () => inject(DefaultRouteReuseStrategy) });
  5041. }
  5042. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouteReuseStrategy, decorators: [{
  5043. type: Injectable,
  5044. args: [{ providedIn: 'root', useFactory: () => inject(DefaultRouteReuseStrategy) }]
  5045. }] });
  5046. /**
  5047. * @description
  5048. *
  5049. * This base route reuse strategy only reuses routes when the matched router configs are
  5050. * identical. This prevents components from being destroyed and recreated
  5051. * when just the route parameters, query parameters or fragment change
  5052. * (that is, the existing component is _reused_).
  5053. *
  5054. * This strategy does not store any routes for later reuse.
  5055. *
  5056. * Angular uses this strategy by default.
  5057. *
  5058. *
  5059. * It can be used as a base class for custom route reuse strategies, i.e. you can create your own
  5060. * class that extends the `BaseRouteReuseStrategy` one.
  5061. * @publicApi
  5062. */
  5063. class BaseRouteReuseStrategy {
  5064. /**
  5065. * Whether the given route should detach for later reuse.
  5066. * Always returns false for `BaseRouteReuseStrategy`.
  5067. * */
  5068. shouldDetach(route) {
  5069. return false;
  5070. }
  5071. /**
  5072. * A no-op; the route is never stored since this strategy never detaches routes for later re-use.
  5073. */
  5074. store(route, detachedTree) { }
  5075. /** Returns `false`, meaning the route (and its subtree) is never reattached */
  5076. shouldAttach(route) {
  5077. return false;
  5078. }
  5079. /** Returns `null` because this strategy does not store routes for later re-use. */
  5080. retrieve(route) {
  5081. return null;
  5082. }
  5083. /**
  5084. * Determines if a route should be reused.
  5085. * This strategy returns `true` when the future route config and current route config are
  5086. * identical.
  5087. */
  5088. shouldReuseRoute(future, curr) {
  5089. return future.routeConfig === curr.routeConfig;
  5090. }
  5091. }
  5092. class DefaultRouteReuseStrategy extends BaseRouteReuseStrategy {
  5093. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: DefaultRouteReuseStrategy, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
  5094. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: DefaultRouteReuseStrategy, providedIn: 'root' });
  5095. }
  5096. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: DefaultRouteReuseStrategy, decorators: [{
  5097. type: Injectable,
  5098. args: [{ providedIn: 'root' }]
  5099. }] });
  5100. class StateManager {
  5101. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: StateManager, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  5102. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: StateManager, providedIn: 'root', useFactory: () => inject(HistoryStateManager) });
  5103. }
  5104. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: StateManager, decorators: [{
  5105. type: Injectable,
  5106. args: [{ providedIn: 'root', useFactory: () => inject(HistoryStateManager) }]
  5107. }] });
  5108. class HistoryStateManager extends StateManager {
  5109. location = inject(Location);
  5110. urlSerializer = inject(UrlSerializer);
  5111. options = inject(ROUTER_CONFIGURATION, { optional: true }) || {};
  5112. canceledNavigationResolution = this.options.canceledNavigationResolution || 'replace';
  5113. urlHandlingStrategy = inject(UrlHandlingStrategy);
  5114. urlUpdateStrategy = this.options.urlUpdateStrategy || 'deferred';
  5115. currentUrlTree = new UrlTree();
  5116. getCurrentUrlTree() {
  5117. return this.currentUrlTree;
  5118. }
  5119. rawUrlTree = this.currentUrlTree;
  5120. getRawUrlTree() {
  5121. return this.rawUrlTree;
  5122. }
  5123. /**
  5124. * The id of the currently active page in the router.
  5125. * Updated to the transition's target id on a successful navigation.
  5126. *
  5127. * This is used to track what page the router last activated. When an attempted navigation fails,
  5128. * the router can then use this to compute how to restore the state back to the previously active
  5129. * page.
  5130. */
  5131. currentPageId = 0;
  5132. lastSuccessfulId = -1;
  5133. restoredState() {
  5134. return this.location.getState();
  5135. }
  5136. /**
  5137. * The ɵrouterPageId of whatever page is currently active in the browser history. This is
  5138. * important for computing the target page id for new navigations because we need to ensure each
  5139. * page id in the browser history is 1 more than the previous entry.
  5140. */
  5141. get browserPageId() {
  5142. if (this.canceledNavigationResolution !== 'computed') {
  5143. return this.currentPageId;
  5144. }
  5145. return this.restoredState()?.ɵrouterPageId ?? this.currentPageId;
  5146. }
  5147. routerState = createEmptyState(null);
  5148. getRouterState() {
  5149. return this.routerState;
  5150. }
  5151. stateMemento = this.createStateMemento();
  5152. createStateMemento() {
  5153. return {
  5154. rawUrlTree: this.rawUrlTree,
  5155. currentUrlTree: this.currentUrlTree,
  5156. routerState: this.routerState,
  5157. };
  5158. }
  5159. registerNonRouterCurrentEntryChangeListener(listener) {
  5160. return this.location.subscribe((event) => {
  5161. if (event['type'] === 'popstate') {
  5162. listener(event['url'], event.state);
  5163. }
  5164. });
  5165. }
  5166. handleRouterEvent(e, currentTransition) {
  5167. if (e instanceof NavigationStart) {
  5168. this.stateMemento = this.createStateMemento();
  5169. }
  5170. else if (e instanceof NavigationSkipped) {
  5171. this.rawUrlTree = currentTransition.initialUrl;
  5172. }
  5173. else if (e instanceof RoutesRecognized) {
  5174. if (this.urlUpdateStrategy === 'eager') {
  5175. if (!currentTransition.extras.skipLocationChange) {
  5176. const rawUrl = this.urlHandlingStrategy.merge(currentTransition.finalUrl, currentTransition.initialUrl);
  5177. this.setBrowserUrl(currentTransition.targetBrowserUrl ?? rawUrl, currentTransition);
  5178. }
  5179. }
  5180. }
  5181. else if (e instanceof BeforeActivateRoutes) {
  5182. this.currentUrlTree = currentTransition.finalUrl;
  5183. this.rawUrlTree = this.urlHandlingStrategy.merge(currentTransition.finalUrl, currentTransition.initialUrl);
  5184. this.routerState = currentTransition.targetRouterState;
  5185. if (this.urlUpdateStrategy === 'deferred' && !currentTransition.extras.skipLocationChange) {
  5186. this.setBrowserUrl(currentTransition.targetBrowserUrl ?? this.rawUrlTree, currentTransition);
  5187. }
  5188. }
  5189. else if (e instanceof NavigationCancel &&
  5190. (e.code === NavigationCancellationCode.GuardRejected ||
  5191. e.code === NavigationCancellationCode.NoDataFromResolver)) {
  5192. this.restoreHistory(currentTransition);
  5193. }
  5194. else if (e instanceof NavigationError) {
  5195. this.restoreHistory(currentTransition, true);
  5196. }
  5197. else if (e instanceof NavigationEnd) {
  5198. this.lastSuccessfulId = e.id;
  5199. this.currentPageId = this.browserPageId;
  5200. }
  5201. }
  5202. setBrowserUrl(url, transition) {
  5203. const path = url instanceof UrlTree ? this.urlSerializer.serialize(url) : url;
  5204. if (this.location.isCurrentPathEqualTo(path) || !!transition.extras.replaceUrl) {
  5205. // replacements do not update the target page
  5206. const currentBrowserPageId = this.browserPageId;
  5207. const state = {
  5208. ...transition.extras.state,
  5209. ...this.generateNgRouterState(transition.id, currentBrowserPageId),
  5210. };
  5211. this.location.replaceState(path, '', state);
  5212. }
  5213. else {
  5214. const state = {
  5215. ...transition.extras.state,
  5216. ...this.generateNgRouterState(transition.id, this.browserPageId + 1),
  5217. };
  5218. this.location.go(path, '', state);
  5219. }
  5220. }
  5221. /**
  5222. * Performs the necessary rollback action to restore the browser URL to the
  5223. * state before the transition.
  5224. */
  5225. restoreHistory(navigation, restoringFromCaughtError = false) {
  5226. if (this.canceledNavigationResolution === 'computed') {
  5227. const currentBrowserPageId = this.browserPageId;
  5228. const targetPagePosition = this.currentPageId - currentBrowserPageId;
  5229. if (targetPagePosition !== 0) {
  5230. this.location.historyGo(targetPagePosition);
  5231. }
  5232. else if (this.currentUrlTree === navigation.finalUrl && targetPagePosition === 0) {
  5233. // We got to the activation stage (where currentUrlTree is set to the navigation's
  5234. // finalUrl), but we weren't moving anywhere in history (skipLocationChange or replaceUrl).
  5235. // We still need to reset the router state back to what it was when the navigation started.
  5236. this.resetState(navigation);
  5237. this.resetUrlToCurrentUrlTree();
  5238. }
  5239. else ;
  5240. }
  5241. else if (this.canceledNavigationResolution === 'replace') {
  5242. // TODO(atscott): It seems like we should _always_ reset the state here. It would be a no-op
  5243. // for `deferred` navigations that haven't change the internal state yet because guards
  5244. // reject. For 'eager' navigations, it seems like we also really should reset the state
  5245. // because the navigation was cancelled. Investigate if this can be done by running TGP.
  5246. if (restoringFromCaughtError) {
  5247. this.resetState(navigation);
  5248. }
  5249. this.resetUrlToCurrentUrlTree();
  5250. }
  5251. }
  5252. resetState(navigation) {
  5253. this.routerState = this.stateMemento.routerState;
  5254. this.currentUrlTree = this.stateMemento.currentUrlTree;
  5255. // Note here that we use the urlHandlingStrategy to get the reset `rawUrlTree` because it may be
  5256. // configured to handle only part of the navigation URL. This means we would only want to reset
  5257. // the part of the navigation handled by the Angular router rather than the whole URL. In
  5258. // addition, the URLHandlingStrategy may be configured to specifically preserve parts of the URL
  5259. // when merging, such as the query params so they are not lost on a refresh.
  5260. this.rawUrlTree = this.urlHandlingStrategy.merge(this.currentUrlTree, navigation.finalUrl ?? this.rawUrlTree);
  5261. }
  5262. resetUrlToCurrentUrlTree() {
  5263. this.location.replaceState(this.urlSerializer.serialize(this.rawUrlTree), '', this.generateNgRouterState(this.lastSuccessfulId, this.currentPageId));
  5264. }
  5265. generateNgRouterState(navigationId, routerPageId) {
  5266. if (this.canceledNavigationResolution === 'computed') {
  5267. return { navigationId, ɵrouterPageId: routerPageId };
  5268. }
  5269. return { navigationId };
  5270. }
  5271. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: HistoryStateManager, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
  5272. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: HistoryStateManager, providedIn: 'root' });
  5273. }
  5274. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: HistoryStateManager, decorators: [{
  5275. type: Injectable,
  5276. args: [{ providedIn: 'root' }]
  5277. }] });
  5278. /**
  5279. * Performs the given action once the router finishes its next/current navigation.
  5280. *
  5281. * The navigation is considered complete under the following conditions:
  5282. * - `NavigationCancel` event emits and the code is not `NavigationCancellationCode.Redirect` or
  5283. * `NavigationCancellationCode.SupersededByNewNavigation`. In these cases, the
  5284. * redirecting/superseding navigation must finish.
  5285. * - `NavigationError`, `NavigationEnd`, or `NavigationSkipped` event emits
  5286. */
  5287. function afterNextNavigation(router, action) {
  5288. router.events
  5289. .pipe(filter((e) => e instanceof NavigationEnd ||
  5290. e instanceof NavigationCancel ||
  5291. e instanceof NavigationError ||
  5292. e instanceof NavigationSkipped), map((e) => {
  5293. if (e instanceof NavigationEnd || e instanceof NavigationSkipped) {
  5294. return 0 /* NavigationResult.COMPLETE */;
  5295. }
  5296. const redirecting = e instanceof NavigationCancel
  5297. ? e.code === NavigationCancellationCode.Redirect ||
  5298. e.code === NavigationCancellationCode.SupersededByNewNavigation
  5299. : false;
  5300. return redirecting ? 2 /* NavigationResult.REDIRECTING */ : 1 /* NavigationResult.FAILED */;
  5301. }), filter((result) => result !== 2 /* NavigationResult.REDIRECTING */), take(1))
  5302. .subscribe(() => {
  5303. action();
  5304. });
  5305. }
  5306. /**
  5307. * The equivalent `IsActiveMatchOptions` options for `Router.isActive` is called with `true`
  5308. * (exact = true).
  5309. */
  5310. const exactMatchOptions = {
  5311. paths: 'exact',
  5312. fragment: 'ignored',
  5313. matrixParams: 'ignored',
  5314. queryParams: 'exact',
  5315. };
  5316. /**
  5317. * The equivalent `IsActiveMatchOptions` options for `Router.isActive` is called with `false`
  5318. * (exact = false).
  5319. */
  5320. const subsetMatchOptions = {
  5321. paths: 'subset',
  5322. fragment: 'ignored',
  5323. matrixParams: 'ignored',
  5324. queryParams: 'subset',
  5325. };
  5326. /**
  5327. * @description
  5328. *
  5329. * A service that facilitates navigation among views and URL manipulation capabilities.
  5330. * This service is provided in the root scope and configured with [provideRouter](api/router/provideRouter).
  5331. *
  5332. * @see {@link Route}
  5333. * @see {@link provideRouter}
  5334. * @see [Routing and Navigation Guide](guide/routing/common-router-tasks).
  5335. *
  5336. * @ngModule RouterModule
  5337. *
  5338. * @publicApi
  5339. */
  5340. class Router {
  5341. get currentUrlTree() {
  5342. return this.stateManager.getCurrentUrlTree();
  5343. }
  5344. get rawUrlTree() {
  5345. return this.stateManager.getRawUrlTree();
  5346. }
  5347. disposed = false;
  5348. nonRouterCurrentEntryChangeSubscription;
  5349. console = inject(_Console);
  5350. stateManager = inject(StateManager);
  5351. options = inject(ROUTER_CONFIGURATION, { optional: true }) || {};
  5352. pendingTasks = inject(_PendingTasksInternal);
  5353. urlUpdateStrategy = this.options.urlUpdateStrategy || 'deferred';
  5354. navigationTransitions = inject(NavigationTransitions);
  5355. urlSerializer = inject(UrlSerializer);
  5356. location = inject(Location);
  5357. urlHandlingStrategy = inject(UrlHandlingStrategy);
  5358. /**
  5359. * The private `Subject` type for the public events exposed in the getter. This is used internally
  5360. * to push events to. The separate field allows us to expose separate types in the public API
  5361. * (i.e., an Observable rather than the Subject).
  5362. */
  5363. _events = new Subject();
  5364. /**
  5365. * An event stream for routing events.
  5366. */
  5367. get events() {
  5368. // TODO(atscott): This _should_ be events.asObservable(). However, this change requires internal
  5369. // cleanup: tests are doing `(route.events as Subject<Event>).next(...)`. This isn't
  5370. // allowed/supported but we still have to fix these or file bugs against the teams before making
  5371. // the change.
  5372. return this._events;
  5373. }
  5374. /**
  5375. * The current state of routing in this NgModule.
  5376. */
  5377. get routerState() {
  5378. return this.stateManager.getRouterState();
  5379. }
  5380. /**
  5381. * True if at least one navigation event has occurred,
  5382. * false otherwise.
  5383. */
  5384. navigated = false;
  5385. /**
  5386. * A strategy for re-using routes.
  5387. *
  5388. * @deprecated Configure using `providers` instead:
  5389. * `{provide: RouteReuseStrategy, useClass: MyStrategy}`.
  5390. */
  5391. routeReuseStrategy = inject(RouteReuseStrategy);
  5392. /**
  5393. * How to handle a navigation request to the current URL.
  5394. *
  5395. *
  5396. * @deprecated Configure this through `provideRouter` or `RouterModule.forRoot` instead.
  5397. * @see {@link withRouterConfig}
  5398. * @see {@link provideRouter}
  5399. * @see {@link RouterModule}
  5400. */
  5401. onSameUrlNavigation = this.options.onSameUrlNavigation || 'ignore';
  5402. config = inject(ROUTES, { optional: true })?.flat() ?? [];
  5403. /**
  5404. * Indicates whether the application has opted in to binding Router data to component inputs.
  5405. *
  5406. * This option is enabled by the `withComponentInputBinding` feature of `provideRouter` or
  5407. * `bindToComponentInputs` in the `ExtraOptions` of `RouterModule.forRoot`.
  5408. */
  5409. componentInputBindingEnabled = !!inject(INPUT_BINDER, { optional: true });
  5410. constructor() {
  5411. this.resetConfig(this.config);
  5412. this.navigationTransitions.setupNavigations(this).subscribe({
  5413. error: (e) => {
  5414. this.console.warn(ngDevMode ? `Unhandled Navigation Error: ${e}` : e);
  5415. },
  5416. });
  5417. this.subscribeToNavigationEvents();
  5418. }
  5419. eventsSubscription = new Subscription();
  5420. subscribeToNavigationEvents() {
  5421. const subscription = this.navigationTransitions.events.subscribe((e) => {
  5422. try {
  5423. const currentTransition = this.navigationTransitions.currentTransition;
  5424. const currentNavigation = this.navigationTransitions.currentNavigation;
  5425. if (currentTransition !== null && currentNavigation !== null) {
  5426. this.stateManager.handleRouterEvent(e, currentNavigation);
  5427. if (e instanceof NavigationCancel &&
  5428. e.code !== NavigationCancellationCode.Redirect &&
  5429. e.code !== NavigationCancellationCode.SupersededByNewNavigation) {
  5430. // It seems weird that `navigated` is set to `true` when the navigation is rejected,
  5431. // however it's how things were written initially. Investigation would need to be done
  5432. // to determine if this can be removed.
  5433. this.navigated = true;
  5434. }
  5435. else if (e instanceof NavigationEnd) {
  5436. this.navigated = true;
  5437. }
  5438. else if (e instanceof RedirectRequest) {
  5439. const opts = e.navigationBehaviorOptions;
  5440. const mergedTree = this.urlHandlingStrategy.merge(e.url, currentTransition.currentRawUrl);
  5441. const extras = {
  5442. browserUrl: currentTransition.extras.browserUrl,
  5443. info: currentTransition.extras.info,
  5444. skipLocationChange: currentTransition.extras.skipLocationChange,
  5445. // The URL is already updated at this point if we have 'eager' URL
  5446. // updates or if the navigation was triggered by the browser (back
  5447. // button, URL bar, etc). We want to replace that item in history
  5448. // if the navigation is rejected.
  5449. replaceUrl: currentTransition.extras.replaceUrl ||
  5450. this.urlUpdateStrategy === 'eager' ||
  5451. isBrowserTriggeredNavigation(currentTransition.source),
  5452. // allow developer to override default options with RedirectCommand
  5453. ...opts,
  5454. };
  5455. this.scheduleNavigation(mergedTree, IMPERATIVE_NAVIGATION, null, extras, {
  5456. resolve: currentTransition.resolve,
  5457. reject: currentTransition.reject,
  5458. promise: currentTransition.promise,
  5459. });
  5460. }
  5461. }
  5462. // Note that it's important to have the Router process the events _before_ the event is
  5463. // pushed through the public observable. This ensures the correct router state is in place
  5464. // before applications observe the events.
  5465. if (isPublicRouterEvent(e)) {
  5466. this._events.next(e);
  5467. }
  5468. }
  5469. catch (e) {
  5470. this.navigationTransitions.transitionAbortSubject.next(e);
  5471. }
  5472. });
  5473. this.eventsSubscription.add(subscription);
  5474. }
  5475. /** @internal */
  5476. resetRootComponentType(rootComponentType) {
  5477. // TODO: vsavkin router 4.0 should make the root component set to null
  5478. // this will simplify the lifecycle of the router.
  5479. this.routerState.root.component = rootComponentType;
  5480. this.navigationTransitions.rootComponentType = rootComponentType;
  5481. }
  5482. /**
  5483. * Sets up the location change listener and performs the initial navigation.
  5484. */
  5485. initialNavigation() {
  5486. this.setUpLocationChangeListener();
  5487. if (!this.navigationTransitions.hasRequestedNavigation) {
  5488. this.navigateToSyncWithBrowser(this.location.path(true), IMPERATIVE_NAVIGATION, this.stateManager.restoredState());
  5489. }
  5490. }
  5491. /**
  5492. * Sets up the location change listener. This listener detects navigations triggered from outside
  5493. * the Router (the browser back/forward buttons, for example) and schedules a corresponding Router
  5494. * navigation so that the correct events, guards, etc. are triggered.
  5495. */
  5496. setUpLocationChangeListener() {
  5497. // Don't need to use Zone.wrap any more, because zone.js
  5498. // already patch onPopState, so location change callback will
  5499. // run into ngZone
  5500. this.nonRouterCurrentEntryChangeSubscription ??=
  5501. this.stateManager.registerNonRouterCurrentEntryChangeListener((url, state) => {
  5502. // The `setTimeout` was added in #12160 and is likely to support Angular/AngularJS
  5503. // hybrid apps.
  5504. setTimeout(() => {
  5505. this.navigateToSyncWithBrowser(url, 'popstate', state);
  5506. }, 0);
  5507. });
  5508. }
  5509. /**
  5510. * Schedules a router navigation to synchronize Router state with the browser state.
  5511. *
  5512. * This is done as a response to a popstate event and the initial navigation. These
  5513. * two scenarios represent times when the browser URL/state has been updated and
  5514. * the Router needs to respond to ensure its internal state matches.
  5515. */
  5516. navigateToSyncWithBrowser(url, source, state) {
  5517. const extras = { replaceUrl: true };
  5518. // TODO: restoredState should always include the entire state, regardless
  5519. // of navigationId. This requires a breaking change to update the type on
  5520. // NavigationStart’s restoredState, which currently requires navigationId
  5521. // to always be present. The Router used to only restore history state if
  5522. // a navigationId was present.
  5523. // The stored navigationId is used by the RouterScroller to retrieve the scroll
  5524. // position for the page.
  5525. const restoredState = state?.navigationId ? state : null;
  5526. // Separate to NavigationStart.restoredState, we must also restore the state to
  5527. // history.state and generate a new navigationId, since it will be overwritten
  5528. if (state) {
  5529. const stateCopy = { ...state };
  5530. delete stateCopy.navigationId;
  5531. delete stateCopy.ɵrouterPageId;
  5532. if (Object.keys(stateCopy).length !== 0) {
  5533. extras.state = stateCopy;
  5534. }
  5535. }
  5536. const urlTree = this.parseUrl(url);
  5537. this.scheduleNavigation(urlTree, source, restoredState, extras);
  5538. }
  5539. /** The current URL. */
  5540. get url() {
  5541. return this.serializeUrl(this.currentUrlTree);
  5542. }
  5543. /**
  5544. * Returns the current `Navigation` object when the router is navigating,
  5545. * and `null` when idle.
  5546. */
  5547. getCurrentNavigation() {
  5548. return this.navigationTransitions.currentNavigation;
  5549. }
  5550. /**
  5551. * The `Navigation` object of the most recent navigation to succeed and `null` if there
  5552. * has not been a successful navigation yet.
  5553. */
  5554. get lastSuccessfulNavigation() {
  5555. return this.navigationTransitions.lastSuccessfulNavigation;
  5556. }
  5557. /**
  5558. * Resets the route configuration used for navigation and generating links.
  5559. *
  5560. * @param config The route array for the new configuration.
  5561. *
  5562. * @usageNotes
  5563. *
  5564. * ```ts
  5565. * router.resetConfig([
  5566. * { path: 'team/:id', component: TeamCmp, children: [
  5567. * { path: 'simple', component: SimpleCmp },
  5568. * { path: 'user/:name', component: UserCmp }
  5569. * ]}
  5570. * ]);
  5571. * ```
  5572. */
  5573. resetConfig(config) {
  5574. (typeof ngDevMode === 'undefined' || ngDevMode) && validateConfig(config);
  5575. this.config = config.map(standardizeConfig);
  5576. this.navigated = false;
  5577. }
  5578. /** @nodoc */
  5579. ngOnDestroy() {
  5580. this.dispose();
  5581. }
  5582. /** Disposes of the router. */
  5583. dispose() {
  5584. // We call `unsubscribe()` to release observers, as users may forget to
  5585. // unsubscribe manually when subscribing to `router.events`. We do not call
  5586. // `complete()` because it is unsafe; if someone subscribes using the `first`
  5587. // operator and the observable completes before emitting a value,
  5588. // RxJS will throw an error.
  5589. this._events.unsubscribe();
  5590. this.navigationTransitions.complete();
  5591. if (this.nonRouterCurrentEntryChangeSubscription) {
  5592. this.nonRouterCurrentEntryChangeSubscription.unsubscribe();
  5593. this.nonRouterCurrentEntryChangeSubscription = undefined;
  5594. }
  5595. this.disposed = true;
  5596. this.eventsSubscription.unsubscribe();
  5597. }
  5598. /**
  5599. * Appends URL segments to the current URL tree to create a new URL tree.
  5600. *
  5601. * @param commands An array of URL fragments with which to construct the new URL tree.
  5602. * If the path is static, can be the literal URL string. For a dynamic path, pass an array of path
  5603. * segments, followed by the parameters for each segment.
  5604. * The fragments are applied to the current URL tree or the one provided in the `relativeTo`
  5605. * property of the options object, if supplied.
  5606. * @param navigationExtras Options that control the navigation strategy.
  5607. * @returns The new URL tree.
  5608. *
  5609. * @usageNotes
  5610. *
  5611. * ```
  5612. * // create /team/33/user/11
  5613. * router.createUrlTree(['/team', 33, 'user', 11]);
  5614. *
  5615. * // create /team/33;expand=true/user/11
  5616. * router.createUrlTree(['/team', 33, {expand: true}, 'user', 11]);
  5617. *
  5618. * // you can collapse static segments like this (this works only with the first passed-in value):
  5619. * router.createUrlTree(['/team/33/user', userId]);
  5620. *
  5621. * // If the first segment can contain slashes, and you do not want the router to split it,
  5622. * // you can do the following:
  5623. * router.createUrlTree([{segmentPath: '/one/two'}]);
  5624. *
  5625. * // create /team/33/(user/11//right:chat)
  5626. * router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: 'chat'}}]);
  5627. *
  5628. * // remove the right secondary node
  5629. * router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: null}}]);
  5630. *
  5631. * // assuming the current url is `/team/33/user/11` and the route points to `user/11`
  5632. *
  5633. * // navigate to /team/33/user/11/details
  5634. * router.createUrlTree(['details'], {relativeTo: route});
  5635. *
  5636. * // navigate to /team/33/user/22
  5637. * router.createUrlTree(['../22'], {relativeTo: route});
  5638. *
  5639. * // navigate to /team/44/user/22
  5640. * router.createUrlTree(['../../team/44/user/22'], {relativeTo: route});
  5641. *
  5642. * Note that a value of `null` or `undefined` for `relativeTo` indicates that the
  5643. * tree should be created relative to the root.
  5644. * ```
  5645. */
  5646. createUrlTree(commands, navigationExtras = {}) {
  5647. const { relativeTo, queryParams, fragment, queryParamsHandling, preserveFragment } = navigationExtras;
  5648. const f = preserveFragment ? this.currentUrlTree.fragment : fragment;
  5649. let q = null;
  5650. switch (queryParamsHandling ?? this.options.defaultQueryParamsHandling) {
  5651. case 'merge':
  5652. q = { ...this.currentUrlTree.queryParams, ...queryParams };
  5653. break;
  5654. case 'preserve':
  5655. q = this.currentUrlTree.queryParams;
  5656. break;
  5657. default:
  5658. q = queryParams || null;
  5659. }
  5660. if (q !== null) {
  5661. q = this.removeEmptyProps(q);
  5662. }
  5663. let relativeToUrlSegmentGroup;
  5664. try {
  5665. const relativeToSnapshot = relativeTo ? relativeTo.snapshot : this.routerState.snapshot.root;
  5666. relativeToUrlSegmentGroup = createSegmentGroupFromRoute(relativeToSnapshot);
  5667. }
  5668. catch (e) {
  5669. // This is strictly for backwards compatibility with tests that create
  5670. // invalid `ActivatedRoute` mocks.
  5671. // Note: the difference between having this fallback for invalid `ActivatedRoute` setups and
  5672. // just throwing is ~500 test failures. Fixing all of those tests by hand is not feasible at
  5673. // the moment.
  5674. if (typeof commands[0] !== 'string' || commands[0][0] !== '/') {
  5675. // Navigations that were absolute in the old way of creating UrlTrees
  5676. // would still work because they wouldn't attempt to match the
  5677. // segments in the `ActivatedRoute` to the `currentUrlTree` but
  5678. // instead just replace the root segment with the navigation result.
  5679. // Non-absolute navigations would fail to apply the commands because
  5680. // the logic could not find the segment to replace (so they'd act like there were no
  5681. // commands).
  5682. commands = [];
  5683. }
  5684. relativeToUrlSegmentGroup = this.currentUrlTree.root;
  5685. }
  5686. return createUrlTreeFromSegmentGroup(relativeToUrlSegmentGroup, commands, q, f ?? null);
  5687. }
  5688. /**
  5689. * Navigates to a view using an absolute route path.
  5690. *
  5691. * @param url An absolute path for a defined route. The function does not apply any delta to the
  5692. * current URL.
  5693. * @param extras An object containing properties that modify the navigation strategy.
  5694. *
  5695. * @returns A Promise that resolves to 'true' when navigation succeeds,
  5696. * to 'false' when navigation fails, or is rejected on error.
  5697. *
  5698. * @usageNotes
  5699. *
  5700. * The following calls request navigation to an absolute path.
  5701. *
  5702. * ```ts
  5703. * router.navigateByUrl("/team/33/user/11");
  5704. *
  5705. * // Navigate without updating the URL
  5706. * router.navigateByUrl("/team/33/user/11", { skipLocationChange: true });
  5707. * ```
  5708. *
  5709. * @see [Routing and Navigation guide](guide/routing/common-router-tasks)
  5710. *
  5711. */
  5712. navigateByUrl(url, extras = {
  5713. skipLocationChange: false,
  5714. }) {
  5715. const urlTree = isUrlTree(url) ? url : this.parseUrl(url);
  5716. const mergedTree = this.urlHandlingStrategy.merge(urlTree, this.rawUrlTree);
  5717. return this.scheduleNavigation(mergedTree, IMPERATIVE_NAVIGATION, null, extras);
  5718. }
  5719. /**
  5720. * Navigate based on the provided array of commands and a starting point.
  5721. * If no starting route is provided, the navigation is absolute.
  5722. *
  5723. * @param commands An array of URL fragments with which to construct the target URL.
  5724. * If the path is static, can be the literal URL string. For a dynamic path, pass an array of path
  5725. * segments, followed by the parameters for each segment.
  5726. * The fragments are applied to the current URL or the one provided in the `relativeTo` property
  5727. * of the options object, if supplied.
  5728. * @param extras An options object that determines how the URL should be constructed or
  5729. * interpreted.
  5730. *
  5731. * @returns A Promise that resolves to `true` when navigation succeeds, or `false` when navigation
  5732. * fails. The Promise is rejected when an error occurs if `resolveNavigationPromiseOnError` is
  5733. * not `true`.
  5734. *
  5735. * @usageNotes
  5736. *
  5737. * The following calls request navigation to a dynamic route path relative to the current URL.
  5738. *
  5739. * ```ts
  5740. * router.navigate(['team', 33, 'user', 11], {relativeTo: route});
  5741. *
  5742. * // Navigate without updating the URL, overriding the default behavior
  5743. * router.navigate(['team', 33, 'user', 11], {relativeTo: route, skipLocationChange: true});
  5744. * ```
  5745. *
  5746. * @see [Routing and Navigation guide](guide/routing/common-router-tasks)
  5747. *
  5748. */
  5749. navigate(commands, extras = { skipLocationChange: false }) {
  5750. validateCommands(commands);
  5751. return this.navigateByUrl(this.createUrlTree(commands, extras), extras);
  5752. }
  5753. /** Serializes a `UrlTree` into a string */
  5754. serializeUrl(url) {
  5755. return this.urlSerializer.serialize(url);
  5756. }
  5757. /** Parses a string into a `UrlTree` */
  5758. parseUrl(url) {
  5759. try {
  5760. return this.urlSerializer.parse(url);
  5761. }
  5762. catch {
  5763. return this.urlSerializer.parse('/');
  5764. }
  5765. }
  5766. isActive(url, matchOptions) {
  5767. let options;
  5768. if (matchOptions === true) {
  5769. options = { ...exactMatchOptions };
  5770. }
  5771. else if (matchOptions === false) {
  5772. options = { ...subsetMatchOptions };
  5773. }
  5774. else {
  5775. options = matchOptions;
  5776. }
  5777. if (isUrlTree(url)) {
  5778. return containsTree(this.currentUrlTree, url, options);
  5779. }
  5780. const urlTree = this.parseUrl(url);
  5781. return containsTree(this.currentUrlTree, urlTree, options);
  5782. }
  5783. removeEmptyProps(params) {
  5784. return Object.entries(params).reduce((result, [key, value]) => {
  5785. if (value !== null && value !== undefined) {
  5786. result[key] = value;
  5787. }
  5788. return result;
  5789. }, {});
  5790. }
  5791. scheduleNavigation(rawUrl, source, restoredState, extras, priorPromise) {
  5792. if (this.disposed) {
  5793. return Promise.resolve(false);
  5794. }
  5795. let resolve;
  5796. let reject;
  5797. let promise;
  5798. if (priorPromise) {
  5799. resolve = priorPromise.resolve;
  5800. reject = priorPromise.reject;
  5801. promise = priorPromise.promise;
  5802. }
  5803. else {
  5804. promise = new Promise((res, rej) => {
  5805. resolve = res;
  5806. reject = rej;
  5807. });
  5808. }
  5809. // Indicate that the navigation is happening.
  5810. const taskId = this.pendingTasks.add();
  5811. afterNextNavigation(this, () => {
  5812. // Remove pending task in a microtask to allow for cancelled
  5813. // initial navigations and redirects within the same task.
  5814. queueMicrotask(() => this.pendingTasks.remove(taskId));
  5815. });
  5816. this.navigationTransitions.handleNavigationRequest({
  5817. source,
  5818. restoredState,
  5819. currentUrlTree: this.currentUrlTree,
  5820. currentRawUrl: this.currentUrlTree,
  5821. rawUrl,
  5822. extras,
  5823. resolve: resolve,
  5824. reject: reject,
  5825. promise,
  5826. currentSnapshot: this.routerState.snapshot,
  5827. currentRouterState: this.routerState,
  5828. });
  5829. // Make sure that the error is propagated even though `processNavigations` catch
  5830. // handler does not rethrow
  5831. return promise.catch((e) => {
  5832. return Promise.reject(e);
  5833. });
  5834. }
  5835. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: Router, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  5836. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: Router, providedIn: 'root' });
  5837. }
  5838. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: Router, decorators: [{
  5839. type: Injectable,
  5840. args: [{ providedIn: 'root' }]
  5841. }], ctorParameters: () => [] });
  5842. function validateCommands(commands) {
  5843. for (let i = 0; i < commands.length; i++) {
  5844. const cmd = commands[i];
  5845. if (cmd == null) {
  5846. throw new _RuntimeError(4008 /* RuntimeErrorCode.NULLISH_COMMAND */, (typeof ngDevMode === 'undefined' || ngDevMode) &&
  5847. `The requested path contains ${cmd} segment at index ${i}`);
  5848. }
  5849. }
  5850. }
  5851. function isPublicRouterEvent(e) {
  5852. return !(e instanceof BeforeActivateRoutes) && !(e instanceof RedirectRequest);
  5853. }
  5854. /**
  5855. * @description
  5856. *
  5857. * When applied to an element in a template, makes that element a link
  5858. * that initiates navigation to a route. Navigation opens one or more routed components
  5859. * in one or more `<router-outlet>` locations on the page.
  5860. *
  5861. * Given a route configuration `[{ path: 'user/:name', component: UserCmp }]`,
  5862. * the following creates a static link to the route:
  5863. * `<a routerLink="/user/bob">link to user component</a>`
  5864. *
  5865. * You can use dynamic values to generate the link.
  5866. * For a dynamic link, pass an array of path segments,
  5867. * followed by the params for each segment.
  5868. * For example, `['/team', teamId, 'user', userName, {details: true}]`
  5869. * generates a link to `/team/11/user/bob;details=true`.
  5870. *
  5871. * Multiple static segments can be merged into one term and combined with dynamic segments.
  5872. * For example, `['/team/11/user', userName, {details: true}]`
  5873. *
  5874. * The input that you provide to the link is treated as a delta to the current URL.
  5875. * For instance, suppose the current URL is `/user/(box//aux:team)`.
  5876. * The link `<a [routerLink]="['/user/jim']">Jim</a>` creates the URL
  5877. * `/user/(jim//aux:team)`.
  5878. * See {@link Router#createUrlTree} for more information.
  5879. *
  5880. * @usageNotes
  5881. *
  5882. * You can use absolute or relative paths in a link, set query parameters,
  5883. * control how parameters are handled, and keep a history of navigation states.
  5884. *
  5885. * ### Relative link paths
  5886. *
  5887. * The first segment name can be prepended with `/`, `./`, or `../`.
  5888. * * If the first segment begins with `/`, the router looks up the route from the root of the
  5889. * app.
  5890. * * If the first segment begins with `./`, or doesn't begin with a slash, the router
  5891. * looks in the children of the current activated route.
  5892. * * If the first segment begins with `../`, the router goes up one level in the route tree.
  5893. *
  5894. * ### Setting and handling query params and fragments
  5895. *
  5896. * The following link adds a query parameter and a fragment to the generated URL:
  5897. *
  5898. * ```html
  5899. * <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education">
  5900. * link to user component
  5901. * </a>
  5902. * ```
  5903. * By default, the directive constructs the new URL using the given query parameters.
  5904. * The example generates the link: `/user/bob?debug=true#education`.
  5905. *
  5906. * You can instruct the directive to handle query parameters differently
  5907. * by specifying the `queryParamsHandling` option in the link.
  5908. * Allowed values are:
  5909. *
  5910. * - `'merge'`: Merge the given `queryParams` into the current query params.
  5911. * - `'preserve'`: Preserve the current query params.
  5912. *
  5913. * For example:
  5914. *
  5915. * ```html
  5916. * <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" queryParamsHandling="merge">
  5917. * link to user component
  5918. * </a>
  5919. * ```
  5920. *
  5921. * `queryParams`, `fragment`, `queryParamsHandling`, `preserveFragment`, and `relativeTo`
  5922. * cannot be used when the `routerLink` input is a `UrlTree`.
  5923. *
  5924. * See {@link UrlCreationOptions#queryParamsHandling}.
  5925. *
  5926. * ### Preserving navigation history
  5927. *
  5928. * You can provide a `state` value to be persisted to the browser's
  5929. * [`History.state` property](https://developer.mozilla.org/en-US/docs/Web/API/History#Properties).
  5930. * For example:
  5931. *
  5932. * ```html
  5933. * <a [routerLink]="['/user/bob']" [state]="{tracingId: 123}">
  5934. * link to user component
  5935. * </a>
  5936. * ```
  5937. *
  5938. * Use {@link Router#getCurrentNavigation} to retrieve a saved
  5939. * navigation-state value. For example, to capture the `tracingId` during the `NavigationStart`
  5940. * event:
  5941. *
  5942. * ```ts
  5943. * // Get NavigationStart events
  5944. * router.events.pipe(filter(e => e instanceof NavigationStart)).subscribe(e => {
  5945. * const navigation = router.getCurrentNavigation();
  5946. * tracingService.trace({id: navigation.extras.state.tracingId});
  5947. * });
  5948. * ```
  5949. *
  5950. * @ngModule RouterModule
  5951. *
  5952. * @publicApi
  5953. */
  5954. class RouterLink {
  5955. router;
  5956. route;
  5957. tabIndexAttribute;
  5958. renderer;
  5959. el;
  5960. locationStrategy;
  5961. /**
  5962. * Represents an `href` attribute value applied to a host element,
  5963. * when a host element is `<a>`. For other tags, the value is `null`.
  5964. */
  5965. href = null;
  5966. /**
  5967. * Represents the `target` attribute on a host element.
  5968. * This is only used when the host element is an `<a>` tag.
  5969. */
  5970. target;
  5971. /**
  5972. * Passed to {@link Router#createUrlTree} as part of the
  5973. * `UrlCreationOptions`.
  5974. * @see {@link UrlCreationOptions#queryParams}
  5975. * @see {@link Router#createUrlTree}
  5976. */
  5977. queryParams;
  5978. /**
  5979. * Passed to {@link Router#createUrlTree} as part of the
  5980. * `UrlCreationOptions`.
  5981. * @see {@link UrlCreationOptions#fragment}
  5982. * @see {@link Router#createUrlTree}
  5983. */
  5984. fragment;
  5985. /**
  5986. * Passed to {@link Router#createUrlTree} as part of the
  5987. * `UrlCreationOptions`.
  5988. * @see {@link UrlCreationOptions#queryParamsHandling}
  5989. * @see {@link Router#createUrlTree}
  5990. */
  5991. queryParamsHandling;
  5992. /**
  5993. * Passed to {@link Router#navigateByUrl} as part of the
  5994. * `NavigationBehaviorOptions`.
  5995. * @see {@link NavigationBehaviorOptions#state}
  5996. * @see {@link Router#navigateByUrl}
  5997. */
  5998. state;
  5999. /**
  6000. * Passed to {@link Router#navigateByUrl} as part of the
  6001. * `NavigationBehaviorOptions`.
  6002. * @see {@link NavigationBehaviorOptions#info}
  6003. * @see {@link Router#navigateByUrl}
  6004. */
  6005. info;
  6006. /**
  6007. * Passed to {@link Router#createUrlTree} as part of the
  6008. * `UrlCreationOptions`.
  6009. * Specify a value here when you do not want to use the default value
  6010. * for `routerLink`, which is the current activated route.
  6011. * Note that a value of `undefined` here will use the `routerLink` default.
  6012. * @see {@link UrlCreationOptions#relativeTo}
  6013. * @see {@link Router#createUrlTree}
  6014. */
  6015. relativeTo;
  6016. /** Whether a host element is an `<a>` tag. */
  6017. isAnchorElement;
  6018. subscription;
  6019. /** @internal */
  6020. onChanges = new Subject();
  6021. constructor(router, route, tabIndexAttribute, renderer, el, locationStrategy) {
  6022. this.router = router;
  6023. this.route = route;
  6024. this.tabIndexAttribute = tabIndexAttribute;
  6025. this.renderer = renderer;
  6026. this.el = el;
  6027. this.locationStrategy = locationStrategy;
  6028. const tagName = el.nativeElement.tagName?.toLowerCase();
  6029. this.isAnchorElement = tagName === 'a' || tagName === 'area';
  6030. if (this.isAnchorElement) {
  6031. this.subscription = router.events.subscribe((s) => {
  6032. if (s instanceof NavigationEnd) {
  6033. this.updateHref();
  6034. }
  6035. });
  6036. }
  6037. else {
  6038. this.setTabIndexIfNotOnNativeEl('0');
  6039. }
  6040. }
  6041. /**
  6042. * Passed to {@link Router#createUrlTree} as part of the
  6043. * `UrlCreationOptions`.
  6044. * @see {@link UrlCreationOptions#preserveFragment}
  6045. * @see {@link Router#createUrlTree}
  6046. */
  6047. preserveFragment = false;
  6048. /**
  6049. * Passed to {@link Router#navigateByUrl} as part of the
  6050. * `NavigationBehaviorOptions`.
  6051. * @see {@link NavigationBehaviorOptions#skipLocationChange}
  6052. * @see {@link Router#navigateByUrl}
  6053. */
  6054. skipLocationChange = false;
  6055. /**
  6056. * Passed to {@link Router#navigateByUrl} as part of the
  6057. * `NavigationBehaviorOptions`.
  6058. * @see {@link NavigationBehaviorOptions#replaceUrl}
  6059. * @see {@link Router#navigateByUrl}
  6060. */
  6061. replaceUrl = false;
  6062. /**
  6063. * Modifies the tab index if there was not a tabindex attribute on the element during
  6064. * instantiation.
  6065. */
  6066. setTabIndexIfNotOnNativeEl(newTabIndex) {
  6067. if (this.tabIndexAttribute != null /* both `null` and `undefined` */ || this.isAnchorElement) {
  6068. return;
  6069. }
  6070. this.applyAttributeValue('tabindex', newTabIndex);
  6071. }
  6072. /** @nodoc */
  6073. // TODO(atscott): Remove changes parameter in major version as a breaking change.
  6074. ngOnChanges(changes) {
  6075. if (ngDevMode &&
  6076. isUrlTree(this.routerLinkInput) &&
  6077. (this.fragment !== undefined ||
  6078. this.queryParams ||
  6079. this.queryParamsHandling ||
  6080. this.preserveFragment ||
  6081. this.relativeTo)) {
  6082. throw new _RuntimeError(4016 /* RuntimeErrorCode.INVALID_ROUTER_LINK_INPUTS */, 'Cannot configure queryParams or fragment when using a UrlTree as the routerLink input value.');
  6083. }
  6084. if (this.isAnchorElement) {
  6085. this.updateHref();
  6086. }
  6087. // This is subscribed to by `RouterLinkActive` so that it knows to update when there are changes
  6088. // to the RouterLinks it's tracking.
  6089. this.onChanges.next(this);
  6090. }
  6091. routerLinkInput = null;
  6092. /**
  6093. * Commands to pass to {@link Router#createUrlTree} or a `UrlTree`.
  6094. * - **array**: commands to pass to {@link Router#createUrlTree}.
  6095. * - **string**: shorthand for array of commands with just the string, i.e. `['/route']`
  6096. * - **UrlTree**: a `UrlTree` for this link rather than creating one from the commands
  6097. * and other inputs that correspond to properties of `UrlCreationOptions`.
  6098. * - **null|undefined**: effectively disables the `routerLink`
  6099. * @see {@link Router#createUrlTree}
  6100. */
  6101. set routerLink(commandsOrUrlTree) {
  6102. if (commandsOrUrlTree == null) {
  6103. this.routerLinkInput = null;
  6104. this.setTabIndexIfNotOnNativeEl(null);
  6105. }
  6106. else {
  6107. if (isUrlTree(commandsOrUrlTree)) {
  6108. this.routerLinkInput = commandsOrUrlTree;
  6109. }
  6110. else {
  6111. this.routerLinkInput = Array.isArray(commandsOrUrlTree)
  6112. ? commandsOrUrlTree
  6113. : [commandsOrUrlTree];
  6114. }
  6115. this.setTabIndexIfNotOnNativeEl('0');
  6116. }
  6117. }
  6118. /** @nodoc */
  6119. onClick(button, ctrlKey, shiftKey, altKey, metaKey) {
  6120. const urlTree = this.urlTree;
  6121. if (urlTree === null) {
  6122. return true;
  6123. }
  6124. if (this.isAnchorElement) {
  6125. if (button !== 0 || ctrlKey || shiftKey || altKey || metaKey) {
  6126. return true;
  6127. }
  6128. if (typeof this.target === 'string' && this.target != '_self') {
  6129. return true;
  6130. }
  6131. }
  6132. const extras = {
  6133. skipLocationChange: this.skipLocationChange,
  6134. replaceUrl: this.replaceUrl,
  6135. state: this.state,
  6136. info: this.info,
  6137. };
  6138. this.router.navigateByUrl(urlTree, extras);
  6139. // Return `false` for `<a>` elements to prevent default action
  6140. // and cancel the native behavior, since the navigation is handled
  6141. // by the Router.
  6142. return !this.isAnchorElement;
  6143. }
  6144. /** @nodoc */
  6145. ngOnDestroy() {
  6146. this.subscription?.unsubscribe();
  6147. }
  6148. updateHref() {
  6149. const urlTree = this.urlTree;
  6150. this.href =
  6151. urlTree !== null && this.locationStrategy
  6152. ? this.locationStrategy?.prepareExternalUrl(this.router.serializeUrl(urlTree))
  6153. : null;
  6154. const sanitizedValue = this.href === null
  6155. ? null
  6156. : // This class represents a directive that can be added to both `<a>` elements,
  6157. // as well as other elements. As a result, we can't define security context at
  6158. // compile time. So the security context is deferred to runtime.
  6159. // The `ɵɵsanitizeUrlOrResourceUrl` selects the necessary sanitizer function
  6160. // based on the tag and property names. The logic mimics the one from
  6161. // `packages/compiler/src/schema/dom_security_schema.ts`, which is used at compile time.
  6162. //
  6163. // Note: we should investigate whether we can switch to using `@HostBinding('attr.href')`
  6164. // instead of applying a value via a renderer, after a final merge of the
  6165. // `RouterLinkWithHref` directive.
  6166. __sanitizeUrlOrResourceUrl(this.href, this.el.nativeElement.tagName.toLowerCase(), 'href');
  6167. this.applyAttributeValue('href', sanitizedValue);
  6168. }
  6169. applyAttributeValue(attrName, attrValue) {
  6170. const renderer = this.renderer;
  6171. const nativeElement = this.el.nativeElement;
  6172. if (attrValue !== null) {
  6173. renderer.setAttribute(nativeElement, attrName, attrValue);
  6174. }
  6175. else {
  6176. renderer.removeAttribute(nativeElement, attrName);
  6177. }
  6178. }
  6179. get urlTree() {
  6180. if (this.routerLinkInput === null) {
  6181. return null;
  6182. }
  6183. else if (isUrlTree(this.routerLinkInput)) {
  6184. return this.routerLinkInput;
  6185. }
  6186. return this.router.createUrlTree(this.routerLinkInput, {
  6187. // If the `relativeTo` input is not defined, we want to use `this.route` by default.
  6188. // Otherwise, we should use the value provided by the user in the input.
  6189. relativeTo: this.relativeTo !== undefined ? this.relativeTo : this.route,
  6190. queryParams: this.queryParams,
  6191. fragment: this.fragment,
  6192. queryParamsHandling: this.queryParamsHandling,
  6193. preserveFragment: this.preserveFragment,
  6194. });
  6195. }
  6196. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterLink, deps: [{ token: Router }, { token: ActivatedRoute }, { token: 'tabindex', attribute: true }, { token: i0.Renderer2 }, { token: i0.ElementRef }, { token: i3.LocationStrategy }], target: i0.ɵɵFactoryTarget.Directive });
  6197. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "16.1.0", version: "19.2.4", type: RouterLink, isStandalone: true, selector: "[routerLink]", inputs: { target: "target", queryParams: "queryParams", fragment: "fragment", queryParamsHandling: "queryParamsHandling", state: "state", info: "info", relativeTo: "relativeTo", preserveFragment: ["preserveFragment", "preserveFragment", booleanAttribute], skipLocationChange: ["skipLocationChange", "skipLocationChange", booleanAttribute], replaceUrl: ["replaceUrl", "replaceUrl", booleanAttribute], routerLink: "routerLink" }, host: { listeners: { "click": "onClick($event.button,$event.ctrlKey,$event.shiftKey,$event.altKey,$event.metaKey)" }, properties: { "attr.target": "this.target" } }, usesOnChanges: true, ngImport: i0 });
  6198. }
  6199. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterLink, decorators: [{
  6200. type: Directive,
  6201. args: [{
  6202. selector: '[routerLink]',
  6203. }]
  6204. }], ctorParameters: () => [{ type: Router }, { type: ActivatedRoute }, { type: undefined, decorators: [{
  6205. type: Attribute,
  6206. args: ['tabindex']
  6207. }] }, { type: i0.Renderer2 }, { type: i0.ElementRef }, { type: i3.LocationStrategy }], propDecorators: { target: [{
  6208. type: HostBinding,
  6209. args: ['attr.target']
  6210. }, {
  6211. type: Input
  6212. }], queryParams: [{
  6213. type: Input
  6214. }], fragment: [{
  6215. type: Input
  6216. }], queryParamsHandling: [{
  6217. type: Input
  6218. }], state: [{
  6219. type: Input
  6220. }], info: [{
  6221. type: Input
  6222. }], relativeTo: [{
  6223. type: Input
  6224. }], preserveFragment: [{
  6225. type: Input,
  6226. args: [{ transform: booleanAttribute }]
  6227. }], skipLocationChange: [{
  6228. type: Input,
  6229. args: [{ transform: booleanAttribute }]
  6230. }], replaceUrl: [{
  6231. type: Input,
  6232. args: [{ transform: booleanAttribute }]
  6233. }], routerLink: [{
  6234. type: Input
  6235. }], onClick: [{
  6236. type: HostListener,
  6237. args: ['click', [
  6238. '$event.button',
  6239. '$event.ctrlKey',
  6240. '$event.shiftKey',
  6241. '$event.altKey',
  6242. '$event.metaKey',
  6243. ]]
  6244. }] } });
  6245. /**
  6246. *
  6247. * @description
  6248. *
  6249. * Tracks whether the linked route of an element is currently active, and allows you
  6250. * to specify one or more CSS classes to add to the element when the linked route
  6251. * is active.
  6252. *
  6253. * Use this directive to create a visual distinction for elements associated with an active route.
  6254. * For example, the following code highlights the word "Bob" when the router
  6255. * activates the associated route:
  6256. *
  6257. * ```html
  6258. * <a routerLink="/user/bob" routerLinkActive="active-link">Bob</a>
  6259. * ```
  6260. *
  6261. * Whenever the URL is either '/user' or '/user/bob', the "active-link" class is
  6262. * added to the anchor tag. If the URL changes, the class is removed.
  6263. *
  6264. * You can set more than one class using a space-separated string or an array.
  6265. * For example:
  6266. *
  6267. * ```html
  6268. * <a routerLink="/user/bob" routerLinkActive="class1 class2">Bob</a>
  6269. * <a routerLink="/user/bob" [routerLinkActive]="['class1', 'class2']">Bob</a>
  6270. * ```
  6271. *
  6272. * To add the classes only when the URL matches the link exactly, add the option `exact: true`:
  6273. *
  6274. * ```html
  6275. * <a routerLink="/user/bob" routerLinkActive="active-link" [routerLinkActiveOptions]="{exact:
  6276. * true}">Bob</a>
  6277. * ```
  6278. *
  6279. * To directly check the `isActive` status of the link, assign the `RouterLinkActive`
  6280. * instance to a template variable.
  6281. * For example, the following checks the status without assigning any CSS classes:
  6282. *
  6283. * ```html
  6284. * <a routerLink="/user/bob" routerLinkActive #rla="routerLinkActive">
  6285. * Bob {{ rla.isActive ? '(already open)' : ''}}
  6286. * </a>
  6287. * ```
  6288. *
  6289. * You can apply the `RouterLinkActive` directive to an ancestor of linked elements.
  6290. * For example, the following sets the active-link class on the `<div>` parent tag
  6291. * when the URL is either '/user/jim' or '/user/bob'.
  6292. *
  6293. * ```html
  6294. * <div routerLinkActive="active-link" [routerLinkActiveOptions]="{exact: true}">
  6295. * <a routerLink="/user/jim">Jim</a>
  6296. * <a routerLink="/user/bob">Bob</a>
  6297. * </div>
  6298. * ```
  6299. *
  6300. * The `RouterLinkActive` directive can also be used to set the aria-current attribute
  6301. * to provide an alternative distinction for active elements to visually impaired users.
  6302. *
  6303. * For example, the following code adds the 'active' class to the Home Page link when it is
  6304. * indeed active and in such case also sets its aria-current attribute to 'page':
  6305. *
  6306. * ```html
  6307. * <a routerLink="/" routerLinkActive="active" ariaCurrentWhenActive="page">Home Page</a>
  6308. * ```
  6309. *
  6310. * @ngModule RouterModule
  6311. *
  6312. * @publicApi
  6313. */
  6314. class RouterLinkActive {
  6315. router;
  6316. element;
  6317. renderer;
  6318. cdr;
  6319. link;
  6320. links;
  6321. classes = [];
  6322. routerEventsSubscription;
  6323. linkInputChangesSubscription;
  6324. _isActive = false;
  6325. get isActive() {
  6326. return this._isActive;
  6327. }
  6328. /**
  6329. * Options to configure how to determine if the router link is active.
  6330. *
  6331. * These options are passed to the `Router.isActive()` function.
  6332. *
  6333. * @see {@link Router#isActive}
  6334. */
  6335. routerLinkActiveOptions = { exact: false };
  6336. /**
  6337. * Aria-current attribute to apply when the router link is active.
  6338. *
  6339. * Possible values: `'page'` | `'step'` | `'location'` | `'date'` | `'time'` | `true` | `false`.
  6340. *
  6341. * @see {@link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-current}
  6342. */
  6343. ariaCurrentWhenActive;
  6344. /**
  6345. *
  6346. * You can use the output `isActiveChange` to get notified each time the link becomes
  6347. * active or inactive.
  6348. *
  6349. * Emits:
  6350. * true -> Route is active
  6351. * false -> Route is inactive
  6352. *
  6353. * ```html
  6354. * <a
  6355. * routerLink="/user/bob"
  6356. * routerLinkActive="active-link"
  6357. * (isActiveChange)="this.onRouterLinkActive($event)">Bob</a>
  6358. * ```
  6359. */
  6360. isActiveChange = new EventEmitter();
  6361. constructor(router, element, renderer, cdr, link) {
  6362. this.router = router;
  6363. this.element = element;
  6364. this.renderer = renderer;
  6365. this.cdr = cdr;
  6366. this.link = link;
  6367. this.routerEventsSubscription = router.events.subscribe((s) => {
  6368. if (s instanceof NavigationEnd) {
  6369. this.update();
  6370. }
  6371. });
  6372. }
  6373. /** @nodoc */
  6374. ngAfterContentInit() {
  6375. // `of(null)` is used to force subscribe body to execute once immediately (like `startWith`).
  6376. of(this.links.changes, of(null))
  6377. .pipe(mergeAll())
  6378. .subscribe((_) => {
  6379. this.update();
  6380. this.subscribeToEachLinkOnChanges();
  6381. });
  6382. }
  6383. subscribeToEachLinkOnChanges() {
  6384. this.linkInputChangesSubscription?.unsubscribe();
  6385. const allLinkChanges = [...this.links.toArray(), this.link]
  6386. .filter((link) => !!link)
  6387. .map((link) => link.onChanges);
  6388. this.linkInputChangesSubscription = from(allLinkChanges)
  6389. .pipe(mergeAll())
  6390. .subscribe((link) => {
  6391. if (this._isActive !== this.isLinkActive(this.router)(link)) {
  6392. this.update();
  6393. }
  6394. });
  6395. }
  6396. set routerLinkActive(data) {
  6397. const classes = Array.isArray(data) ? data : data.split(' ');
  6398. this.classes = classes.filter((c) => !!c);
  6399. }
  6400. /** @nodoc */
  6401. ngOnChanges(changes) {
  6402. this.update();
  6403. }
  6404. /** @nodoc */
  6405. ngOnDestroy() {
  6406. this.routerEventsSubscription.unsubscribe();
  6407. this.linkInputChangesSubscription?.unsubscribe();
  6408. }
  6409. update() {
  6410. if (!this.links || !this.router.navigated)
  6411. return;
  6412. queueMicrotask(() => {
  6413. const hasActiveLinks = this.hasActiveLinks();
  6414. this.classes.forEach((c) => {
  6415. if (hasActiveLinks) {
  6416. this.renderer.addClass(this.element.nativeElement, c);
  6417. }
  6418. else {
  6419. this.renderer.removeClass(this.element.nativeElement, c);
  6420. }
  6421. });
  6422. if (hasActiveLinks && this.ariaCurrentWhenActive !== undefined) {
  6423. this.renderer.setAttribute(this.element.nativeElement, 'aria-current', this.ariaCurrentWhenActive.toString());
  6424. }
  6425. else {
  6426. this.renderer.removeAttribute(this.element.nativeElement, 'aria-current');
  6427. }
  6428. // Only emit change if the active state changed.
  6429. if (this._isActive !== hasActiveLinks) {
  6430. this._isActive = hasActiveLinks;
  6431. this.cdr.markForCheck();
  6432. // Emit on isActiveChange after classes are updated
  6433. this.isActiveChange.emit(hasActiveLinks);
  6434. }
  6435. });
  6436. }
  6437. isLinkActive(router) {
  6438. const options = isActiveMatchOptions(this.routerLinkActiveOptions)
  6439. ? this.routerLinkActiveOptions
  6440. : // While the types should disallow `undefined` here, it's possible without strict inputs
  6441. this.routerLinkActiveOptions.exact || false;
  6442. return (link) => {
  6443. const urlTree = link.urlTree;
  6444. return urlTree ? router.isActive(urlTree, options) : false;
  6445. };
  6446. }
  6447. hasActiveLinks() {
  6448. const isActiveCheckFn = this.isLinkActive(this.router);
  6449. return (this.link && isActiveCheckFn(this.link)) || this.links.some(isActiveCheckFn);
  6450. }
  6451. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterLinkActive, deps: [{ token: Router }, { token: i0.ElementRef }, { token: i0.Renderer2 }, { token: i0.ChangeDetectorRef }, { token: RouterLink, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
  6452. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.4", type: RouterLinkActive, isStandalone: true, selector: "[routerLinkActive]", inputs: { routerLinkActiveOptions: "routerLinkActiveOptions", ariaCurrentWhenActive: "ariaCurrentWhenActive", routerLinkActive: "routerLinkActive" }, outputs: { isActiveChange: "isActiveChange" }, queries: [{ propertyName: "links", predicate: RouterLink, descendants: true }], exportAs: ["routerLinkActive"], usesOnChanges: true, ngImport: i0 });
  6453. }
  6454. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterLinkActive, decorators: [{
  6455. type: Directive,
  6456. args: [{
  6457. selector: '[routerLinkActive]',
  6458. exportAs: 'routerLinkActive',
  6459. }]
  6460. }], ctorParameters: () => [{ type: Router }, { type: i0.ElementRef }, { type: i0.Renderer2 }, { type: i0.ChangeDetectorRef }, { type: RouterLink, decorators: [{
  6461. type: Optional
  6462. }] }], propDecorators: { links: [{
  6463. type: ContentChildren,
  6464. args: [RouterLink, { descendants: true }]
  6465. }], routerLinkActiveOptions: [{
  6466. type: Input
  6467. }], ariaCurrentWhenActive: [{
  6468. type: Input
  6469. }], isActiveChange: [{
  6470. type: Output
  6471. }], routerLinkActive: [{
  6472. type: Input
  6473. }] } });
  6474. /**
  6475. * Use instead of `'paths' in options` to be compatible with property renaming
  6476. */
  6477. function isActiveMatchOptions(options) {
  6478. return !!options.paths;
  6479. }
  6480. /**
  6481. * @description
  6482. *
  6483. * Provides a preloading strategy.
  6484. *
  6485. * @publicApi
  6486. */
  6487. class PreloadingStrategy {
  6488. }
  6489. /**
  6490. * @description
  6491. *
  6492. * Provides a preloading strategy that preloads all modules as quickly as possible.
  6493. *
  6494. * ```ts
  6495. * RouterModule.forRoot(ROUTES, {preloadingStrategy: PreloadAllModules})
  6496. * ```
  6497. *
  6498. * @publicApi
  6499. */
  6500. class PreloadAllModules {
  6501. preload(route, fn) {
  6502. return fn().pipe(catchError(() => of(null)));
  6503. }
  6504. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: PreloadAllModules, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  6505. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: PreloadAllModules, providedIn: 'root' });
  6506. }
  6507. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: PreloadAllModules, decorators: [{
  6508. type: Injectable,
  6509. args: [{ providedIn: 'root' }]
  6510. }] });
  6511. /**
  6512. * @description
  6513. *
  6514. * Provides a preloading strategy that does not preload any modules.
  6515. *
  6516. * This strategy is enabled by default.
  6517. *
  6518. * @publicApi
  6519. */
  6520. class NoPreloading {
  6521. preload(route, fn) {
  6522. return of(null);
  6523. }
  6524. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: NoPreloading, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  6525. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: NoPreloading, providedIn: 'root' });
  6526. }
  6527. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: NoPreloading, decorators: [{
  6528. type: Injectable,
  6529. args: [{ providedIn: 'root' }]
  6530. }] });
  6531. /**
  6532. * The preloader optimistically loads all router configurations to
  6533. * make navigations into lazily-loaded sections of the application faster.
  6534. *
  6535. * The preloader runs in the background. When the router bootstraps, the preloader
  6536. * starts listening to all navigation events. After every such event, the preloader
  6537. * will check if any configurations can be loaded lazily.
  6538. *
  6539. * If a route is protected by `canLoad` guards, the preloaded will not load it.
  6540. *
  6541. * @publicApi
  6542. */
  6543. class RouterPreloader {
  6544. router;
  6545. injector;
  6546. preloadingStrategy;
  6547. loader;
  6548. subscription;
  6549. constructor(router, compiler, injector, preloadingStrategy, loader) {
  6550. this.router = router;
  6551. this.injector = injector;
  6552. this.preloadingStrategy = preloadingStrategy;
  6553. this.loader = loader;
  6554. }
  6555. setUpPreloading() {
  6556. this.subscription = this.router.events
  6557. .pipe(filter((e) => e instanceof NavigationEnd), concatMap(() => this.preload()))
  6558. .subscribe(() => { });
  6559. }
  6560. preload() {
  6561. return this.processRoutes(this.injector, this.router.config);
  6562. }
  6563. /** @nodoc */
  6564. ngOnDestroy() {
  6565. if (this.subscription) {
  6566. this.subscription.unsubscribe();
  6567. }
  6568. }
  6569. processRoutes(injector, routes) {
  6570. const res = [];
  6571. for (const route of routes) {
  6572. if (route.providers && !route._injector) {
  6573. route._injector = createEnvironmentInjector(route.providers, injector, `Route: ${route.path}`);
  6574. }
  6575. const injectorForCurrentRoute = route._injector ?? injector;
  6576. const injectorForChildren = route._loadedInjector ?? injectorForCurrentRoute;
  6577. // Note that `canLoad` is only checked as a condition that prevents `loadChildren` and not
  6578. // `loadComponent`. `canLoad` guards only block loading of child routes by design. This
  6579. // happens as a consequence of needing to descend into children for route matching immediately
  6580. // while component loading is deferred until route activation. Because `canLoad` guards can
  6581. // have side effects, we cannot execute them here so we instead skip preloading altogether
  6582. // when present. Lastly, it remains to be decided whether `canLoad` should behave this way
  6583. // at all. Code splitting and lazy loading is separate from client-side authorization checks
  6584. // and should not be used as a security measure to prevent loading of code.
  6585. if ((route.loadChildren && !route._loadedRoutes && route.canLoad === undefined) ||
  6586. (route.loadComponent && !route._loadedComponent)) {
  6587. res.push(this.preloadConfig(injectorForCurrentRoute, route));
  6588. }
  6589. if (route.children || route._loadedRoutes) {
  6590. res.push(this.processRoutes(injectorForChildren, (route.children ?? route._loadedRoutes)));
  6591. }
  6592. }
  6593. return from(res).pipe(mergeAll());
  6594. }
  6595. preloadConfig(injector, route) {
  6596. return this.preloadingStrategy.preload(route, () => {
  6597. let loadedChildren$;
  6598. if (route.loadChildren && route.canLoad === undefined) {
  6599. loadedChildren$ = this.loader.loadChildren(injector, route);
  6600. }
  6601. else {
  6602. loadedChildren$ = of(null);
  6603. }
  6604. const recursiveLoadChildren$ = loadedChildren$.pipe(mergeMap((config) => {
  6605. if (config === null) {
  6606. return of(void 0);
  6607. }
  6608. route._loadedRoutes = config.routes;
  6609. route._loadedInjector = config.injector;
  6610. // If the loaded config was a module, use that as the module/module injector going
  6611. // forward. Otherwise, continue using the current module/module injector.
  6612. return this.processRoutes(config.injector ?? injector, config.routes);
  6613. }));
  6614. if (route.loadComponent && !route._loadedComponent) {
  6615. const loadComponent$ = this.loader.loadComponent(route);
  6616. return from([recursiveLoadChildren$, loadComponent$]).pipe(mergeAll());
  6617. }
  6618. else {
  6619. return recursiveLoadChildren$;
  6620. }
  6621. });
  6622. }
  6623. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterPreloader, deps: [{ token: Router }, { token: i0.Compiler }, { token: i0.EnvironmentInjector }, { token: PreloadingStrategy }, { token: RouterConfigLoader }], target: i0.ɵɵFactoryTarget.Injectable });
  6624. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterPreloader, providedIn: 'root' });
  6625. }
  6626. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterPreloader, decorators: [{
  6627. type: Injectable,
  6628. args: [{ providedIn: 'root' }]
  6629. }], ctorParameters: () => [{ type: Router }, { type: i0.Compiler }, { type: i0.EnvironmentInjector }, { type: PreloadingStrategy }, { type: RouterConfigLoader }] });
  6630. const ROUTER_SCROLLER = new InjectionToken('');
  6631. class RouterScroller {
  6632. urlSerializer;
  6633. transitions;
  6634. viewportScroller;
  6635. zone;
  6636. options;
  6637. routerEventsSubscription;
  6638. scrollEventsSubscription;
  6639. lastId = 0;
  6640. lastSource = 'imperative';
  6641. restoredId = 0;
  6642. store = {};
  6643. /** @nodoc */
  6644. constructor(urlSerializer, transitions, viewportScroller, zone, options = {}) {
  6645. this.urlSerializer = urlSerializer;
  6646. this.transitions = transitions;
  6647. this.viewportScroller = viewportScroller;
  6648. this.zone = zone;
  6649. this.options = options;
  6650. // Default both options to 'disabled'
  6651. options.scrollPositionRestoration ||= 'disabled';
  6652. options.anchorScrolling ||= 'disabled';
  6653. }
  6654. init() {
  6655. // we want to disable the automatic scrolling because having two places
  6656. // responsible for scrolling results race conditions, especially given
  6657. // that browser don't implement this behavior consistently
  6658. if (this.options.scrollPositionRestoration !== 'disabled') {
  6659. this.viewportScroller.setHistoryScrollRestoration('manual');
  6660. }
  6661. this.routerEventsSubscription = this.createScrollEvents();
  6662. this.scrollEventsSubscription = this.consumeScrollEvents();
  6663. }
  6664. createScrollEvents() {
  6665. return this.transitions.events.subscribe((e) => {
  6666. if (e instanceof NavigationStart) {
  6667. // store the scroll position of the current stable navigations.
  6668. this.store[this.lastId] = this.viewportScroller.getScrollPosition();
  6669. this.lastSource = e.navigationTrigger;
  6670. this.restoredId = e.restoredState ? e.restoredState.navigationId : 0;
  6671. }
  6672. else if (e instanceof NavigationEnd) {
  6673. this.lastId = e.id;
  6674. this.scheduleScrollEvent(e, this.urlSerializer.parse(e.urlAfterRedirects).fragment);
  6675. }
  6676. else if (e instanceof NavigationSkipped &&
  6677. e.code === NavigationSkippedCode.IgnoredSameUrlNavigation) {
  6678. this.lastSource = undefined;
  6679. this.restoredId = 0;
  6680. this.scheduleScrollEvent(e, this.urlSerializer.parse(e.url).fragment);
  6681. }
  6682. });
  6683. }
  6684. consumeScrollEvents() {
  6685. return this.transitions.events.subscribe((e) => {
  6686. if (!(e instanceof Scroll))
  6687. return;
  6688. // a popstate event. The pop state event will always ignore anchor scrolling.
  6689. if (e.position) {
  6690. if (this.options.scrollPositionRestoration === 'top') {
  6691. this.viewportScroller.scrollToPosition([0, 0]);
  6692. }
  6693. else if (this.options.scrollPositionRestoration === 'enabled') {
  6694. this.viewportScroller.scrollToPosition(e.position);
  6695. }
  6696. // imperative navigation "forward"
  6697. }
  6698. else {
  6699. if (e.anchor && this.options.anchorScrolling === 'enabled') {
  6700. this.viewportScroller.scrollToAnchor(e.anchor);
  6701. }
  6702. else if (this.options.scrollPositionRestoration !== 'disabled') {
  6703. this.viewportScroller.scrollToPosition([0, 0]);
  6704. }
  6705. }
  6706. });
  6707. }
  6708. scheduleScrollEvent(routerEvent, anchor) {
  6709. this.zone.runOutsideAngular(() => {
  6710. // The scroll event needs to be delayed until after change detection. Otherwise, we may
  6711. // attempt to restore the scroll position before the router outlet has fully rendered the
  6712. // component by executing its update block of the template function.
  6713. setTimeout(() => {
  6714. this.zone.run(() => {
  6715. this.transitions.events.next(new Scroll(routerEvent, this.lastSource === 'popstate' ? this.store[this.restoredId] : null, anchor));
  6716. });
  6717. }, 0);
  6718. });
  6719. }
  6720. /** @nodoc */
  6721. ngOnDestroy() {
  6722. this.routerEventsSubscription?.unsubscribe();
  6723. this.scrollEventsSubscription?.unsubscribe();
  6724. }
  6725. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterScroller, deps: "invalid", target: i0.ɵɵFactoryTarget.Injectable });
  6726. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterScroller });
  6727. }
  6728. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterScroller, decorators: [{
  6729. type: Injectable
  6730. }], ctorParameters: () => [{ type: UrlSerializer }, { type: NavigationTransitions }, { type: i3.ViewportScroller }, { type: i0.NgZone }, { type: undefined }] });
  6731. /**
  6732. * Sets up providers necessary to enable `Router` functionality for the application.
  6733. * Allows to configure a set of routes as well as extra features that should be enabled.
  6734. *
  6735. * @usageNotes
  6736. *
  6737. * Basic example of how you can add a Router to your application:
  6738. * ```ts
  6739. * const appRoutes: Routes = [];
  6740. * bootstrapApplication(AppComponent, {
  6741. * providers: [provideRouter(appRoutes)]
  6742. * });
  6743. * ```
  6744. *
  6745. * You can also enable optional features in the Router by adding functions from the `RouterFeatures`
  6746. * type:
  6747. * ```ts
  6748. * const appRoutes: Routes = [];
  6749. * bootstrapApplication(AppComponent,
  6750. * {
  6751. * providers: [
  6752. * provideRouter(appRoutes,
  6753. * withDebugTracing(),
  6754. * withRouterConfig({paramsInheritanceStrategy: 'always'}))
  6755. * ]
  6756. * }
  6757. * );
  6758. * ```
  6759. *
  6760. * @see {@link RouterFeatures}
  6761. *
  6762. * @publicApi
  6763. * @param routes A set of `Route`s to use for the application routing table.
  6764. * @param features Optional features to configure additional router behaviors.
  6765. * @returns A set of providers to setup a Router.
  6766. */
  6767. function provideRouter(routes, ...features) {
  6768. return makeEnvironmentProviders([
  6769. { provide: ROUTES, multi: true, useValue: routes },
  6770. typeof ngDevMode === 'undefined' || ngDevMode
  6771. ? { provide: ROUTER_IS_PROVIDED, useValue: true }
  6772. : [],
  6773. { provide: ActivatedRoute, useFactory: rootRoute, deps: [Router] },
  6774. { provide: APP_BOOTSTRAP_LISTENER, multi: true, useFactory: getBootstrapListener },
  6775. features.map((feature) => feature.ɵproviders),
  6776. ]);
  6777. }
  6778. function rootRoute(router) {
  6779. return router.routerState.root;
  6780. }
  6781. /**
  6782. * Helper function to create an object that represents a Router feature.
  6783. */
  6784. function routerFeature(kind, providers) {
  6785. return { ɵkind: kind, ɵproviders: providers };
  6786. }
  6787. /**
  6788. * An Injection token used to indicate whether `provideRouter` or `RouterModule.forRoot` was ever
  6789. * called.
  6790. */
  6791. const ROUTER_IS_PROVIDED = new InjectionToken('', {
  6792. providedIn: 'root',
  6793. factory: () => false,
  6794. });
  6795. const routerIsProvidedDevModeCheck = {
  6796. provide: ENVIRONMENT_INITIALIZER,
  6797. multi: true,
  6798. useFactory() {
  6799. return () => {
  6800. if (!inject(ROUTER_IS_PROVIDED)) {
  6801. console.warn('`provideRoutes` was called without `provideRouter` or `RouterModule.forRoot`. ' +
  6802. 'This is likely a mistake.');
  6803. }
  6804. };
  6805. },
  6806. };
  6807. /**
  6808. * Registers a DI provider for a set of routes.
  6809. * @param routes The route configuration to provide.
  6810. *
  6811. * @usageNotes
  6812. *
  6813. * ```ts
  6814. * @NgModule({
  6815. * providers: [provideRoutes(ROUTES)]
  6816. * })
  6817. * class LazyLoadedChildModule {}
  6818. * ```
  6819. *
  6820. * @deprecated If necessary, provide routes using the `ROUTES` `InjectionToken`.
  6821. * @see {@link ROUTES}
  6822. * @publicApi
  6823. */
  6824. function provideRoutes(routes) {
  6825. return [
  6826. { provide: ROUTES, multi: true, useValue: routes },
  6827. typeof ngDevMode === 'undefined' || ngDevMode ? routerIsProvidedDevModeCheck : [],
  6828. ];
  6829. }
  6830. /**
  6831. * Enables customizable scrolling behavior for router navigations.
  6832. *
  6833. * @usageNotes
  6834. *
  6835. * Basic example of how you can enable scrolling feature:
  6836. * ```ts
  6837. * const appRoutes: Routes = [];
  6838. * bootstrapApplication(AppComponent,
  6839. * {
  6840. * providers: [
  6841. * provideRouter(appRoutes, withInMemoryScrolling())
  6842. * ]
  6843. * }
  6844. * );
  6845. * ```
  6846. *
  6847. * @see {@link provideRouter}
  6848. * @see {@link ViewportScroller}
  6849. *
  6850. * @publicApi
  6851. * @param options Set of configuration parameters to customize scrolling behavior, see
  6852. * `InMemoryScrollingOptions` for additional information.
  6853. * @returns A set of providers for use with `provideRouter`.
  6854. */
  6855. function withInMemoryScrolling(options = {}) {
  6856. const providers = [
  6857. {
  6858. provide: ROUTER_SCROLLER,
  6859. useFactory: () => {
  6860. const viewportScroller = inject(ViewportScroller);
  6861. const zone = inject(NgZone);
  6862. const transitions = inject(NavigationTransitions);
  6863. const urlSerializer = inject(UrlSerializer);
  6864. return new RouterScroller(urlSerializer, transitions, viewportScroller, zone, options);
  6865. },
  6866. },
  6867. ];
  6868. return routerFeature(4 /* RouterFeatureKind.InMemoryScrollingFeature */, providers);
  6869. }
  6870. function getBootstrapListener() {
  6871. const injector = inject(Injector);
  6872. return (bootstrappedComponentRef) => {
  6873. const ref = injector.get(ApplicationRef);
  6874. if (bootstrappedComponentRef !== ref.components[0]) {
  6875. return;
  6876. }
  6877. const router = injector.get(Router);
  6878. const bootstrapDone = injector.get(BOOTSTRAP_DONE);
  6879. if (injector.get(INITIAL_NAVIGATION) === 1 /* InitialNavigation.EnabledNonBlocking */) {
  6880. router.initialNavigation();
  6881. }
  6882. injector.get(ROUTER_PRELOADER, null, InjectFlags.Optional)?.setUpPreloading();
  6883. injector.get(ROUTER_SCROLLER, null, InjectFlags.Optional)?.init();
  6884. router.resetRootComponentType(ref.componentTypes[0]);
  6885. if (!bootstrapDone.closed) {
  6886. bootstrapDone.next();
  6887. bootstrapDone.complete();
  6888. bootstrapDone.unsubscribe();
  6889. }
  6890. };
  6891. }
  6892. /**
  6893. * A subject used to indicate that the bootstrapping phase is done. When initial navigation is
  6894. * `enabledBlocking`, the first navigation waits until bootstrapping is finished before continuing
  6895. * to the activation phase.
  6896. */
  6897. const BOOTSTRAP_DONE = new InjectionToken(typeof ngDevMode === 'undefined' || ngDevMode ? 'bootstrap done indicator' : '', {
  6898. factory: () => {
  6899. return new Subject();
  6900. },
  6901. });
  6902. const INITIAL_NAVIGATION = new InjectionToken(typeof ngDevMode === 'undefined' || ngDevMode ? 'initial navigation' : '', { providedIn: 'root', factory: () => 1 /* InitialNavigation.EnabledNonBlocking */ });
  6903. /**
  6904. * Configures initial navigation to start before the root component is created.
  6905. *
  6906. * The bootstrap is blocked until the initial navigation is complete. This should be set in case
  6907. * you use [server-side rendering](guide/ssr), but do not enable [hydration](guide/hydration) for
  6908. * your application.
  6909. *
  6910. * @usageNotes
  6911. *
  6912. * Basic example of how you can enable this navigation behavior:
  6913. * ```ts
  6914. * const appRoutes: Routes = [];
  6915. * bootstrapApplication(AppComponent,
  6916. * {
  6917. * providers: [
  6918. * provideRouter(appRoutes, withEnabledBlockingInitialNavigation())
  6919. * ]
  6920. * }
  6921. * );
  6922. * ```
  6923. *
  6924. * @see {@link provideRouter}
  6925. *
  6926. * @publicApi
  6927. * @returns A set of providers for use with `provideRouter`.
  6928. */
  6929. function withEnabledBlockingInitialNavigation() {
  6930. const providers = [
  6931. { provide: INITIAL_NAVIGATION, useValue: 0 /* InitialNavigation.EnabledBlocking */ },
  6932. {
  6933. provide: APP_INITIALIZER,
  6934. multi: true,
  6935. deps: [Injector],
  6936. useFactory: (injector) => {
  6937. const locationInitialized = injector.get(LOCATION_INITIALIZED, Promise.resolve());
  6938. return () => {
  6939. return locationInitialized.then(() => {
  6940. return new Promise((resolve) => {
  6941. const router = injector.get(Router);
  6942. const bootstrapDone = injector.get(BOOTSTRAP_DONE);
  6943. afterNextNavigation(router, () => {
  6944. // Unblock APP_INITIALIZER in case the initial navigation was canceled or errored
  6945. // without a redirect.
  6946. resolve(true);
  6947. });
  6948. injector.get(NavigationTransitions).afterPreactivation = () => {
  6949. // Unblock APP_INITIALIZER once we get to `afterPreactivation`. At this point, we
  6950. // assume activation will complete successfully (even though this is not
  6951. // guaranteed).
  6952. resolve(true);
  6953. return bootstrapDone.closed ? of(void 0) : bootstrapDone;
  6954. };
  6955. router.initialNavigation();
  6956. });
  6957. });
  6958. };
  6959. },
  6960. },
  6961. ];
  6962. return routerFeature(2 /* RouterFeatureKind.EnabledBlockingInitialNavigationFeature */, providers);
  6963. }
  6964. /**
  6965. * Disables initial navigation.
  6966. *
  6967. * Use if there is a reason to have more control over when the router starts its initial navigation
  6968. * due to some complex initialization logic.
  6969. *
  6970. * @usageNotes
  6971. *
  6972. * Basic example of how you can disable initial navigation:
  6973. * ```ts
  6974. * const appRoutes: Routes = [];
  6975. * bootstrapApplication(AppComponent,
  6976. * {
  6977. * providers: [
  6978. * provideRouter(appRoutes, withDisabledInitialNavigation())
  6979. * ]
  6980. * }
  6981. * );
  6982. * ```
  6983. *
  6984. * @see {@link provideRouter}
  6985. *
  6986. * @returns A set of providers for use with `provideRouter`.
  6987. *
  6988. * @publicApi
  6989. */
  6990. function withDisabledInitialNavigation() {
  6991. const providers = [
  6992. {
  6993. provide: APP_INITIALIZER,
  6994. multi: true,
  6995. useFactory: () => {
  6996. const router = inject(Router);
  6997. return () => {
  6998. router.setUpLocationChangeListener();
  6999. };
  7000. },
  7001. },
  7002. { provide: INITIAL_NAVIGATION, useValue: 2 /* InitialNavigation.Disabled */ },
  7003. ];
  7004. return routerFeature(3 /* RouterFeatureKind.DisabledInitialNavigationFeature */, providers);
  7005. }
  7006. /**
  7007. * Enables logging of all internal navigation events to the console.
  7008. * Extra logging might be useful for debugging purposes to inspect Router event sequence.
  7009. *
  7010. * @usageNotes
  7011. *
  7012. * Basic example of how you can enable debug tracing:
  7013. * ```ts
  7014. * const appRoutes: Routes = [];
  7015. * bootstrapApplication(AppComponent,
  7016. * {
  7017. * providers: [
  7018. * provideRouter(appRoutes, withDebugTracing())
  7019. * ]
  7020. * }
  7021. * );
  7022. * ```
  7023. *
  7024. * @see {@link provideRouter}
  7025. *
  7026. * @returns A set of providers for use with `provideRouter`.
  7027. *
  7028. * @publicApi
  7029. */
  7030. function withDebugTracing() {
  7031. let providers = [];
  7032. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  7033. providers = [
  7034. {
  7035. provide: ENVIRONMENT_INITIALIZER,
  7036. multi: true,
  7037. useFactory: () => {
  7038. const router = inject(Router);
  7039. return () => router.events.subscribe((e) => {
  7040. // tslint:disable:no-console
  7041. console.group?.(`Router Event: ${e.constructor.name}`);
  7042. console.log(stringifyEvent(e));
  7043. console.log(e);
  7044. console.groupEnd?.();
  7045. // tslint:enable:no-console
  7046. });
  7047. },
  7048. },
  7049. ];
  7050. }
  7051. else {
  7052. providers = [];
  7053. }
  7054. return routerFeature(1 /* RouterFeatureKind.DebugTracingFeature */, providers);
  7055. }
  7056. const ROUTER_PRELOADER = new InjectionToken(typeof ngDevMode === 'undefined' || ngDevMode ? 'router preloader' : '');
  7057. /**
  7058. * Allows to configure a preloading strategy to use. The strategy is configured by providing a
  7059. * reference to a class that implements a `PreloadingStrategy`.
  7060. *
  7061. * @usageNotes
  7062. *
  7063. * Basic example of how you can configure preloading:
  7064. * ```ts
  7065. * const appRoutes: Routes = [];
  7066. * bootstrapApplication(AppComponent,
  7067. * {
  7068. * providers: [
  7069. * provideRouter(appRoutes, withPreloading(PreloadAllModules))
  7070. * ]
  7071. * }
  7072. * );
  7073. * ```
  7074. *
  7075. * @see {@link provideRouter}
  7076. *
  7077. * @param preloadingStrategy A reference to a class that implements a `PreloadingStrategy` that
  7078. * should be used.
  7079. * @returns A set of providers for use with `provideRouter`.
  7080. *
  7081. * @publicApi
  7082. */
  7083. function withPreloading(preloadingStrategy) {
  7084. const providers = [
  7085. { provide: ROUTER_PRELOADER, useExisting: RouterPreloader },
  7086. { provide: PreloadingStrategy, useExisting: preloadingStrategy },
  7087. ];
  7088. return routerFeature(0 /* RouterFeatureKind.PreloadingFeature */, providers);
  7089. }
  7090. /**
  7091. * Allows to provide extra parameters to configure Router.
  7092. *
  7093. * @usageNotes
  7094. *
  7095. * Basic example of how you can provide extra configuration options:
  7096. * ```ts
  7097. * const appRoutes: Routes = [];
  7098. * bootstrapApplication(AppComponent,
  7099. * {
  7100. * providers: [
  7101. * provideRouter(appRoutes, withRouterConfig({
  7102. * onSameUrlNavigation: 'reload'
  7103. * }))
  7104. * ]
  7105. * }
  7106. * );
  7107. * ```
  7108. *
  7109. * @see {@link provideRouter}
  7110. *
  7111. * @param options A set of parameters to configure Router, see `RouterConfigOptions` for
  7112. * additional information.
  7113. * @returns A set of providers for use with `provideRouter`.
  7114. *
  7115. * @publicApi
  7116. */
  7117. function withRouterConfig(options) {
  7118. const providers = [{ provide: ROUTER_CONFIGURATION, useValue: options }];
  7119. return routerFeature(5 /* RouterFeatureKind.RouterConfigurationFeature */, providers);
  7120. }
  7121. /**
  7122. * Provides the location strategy that uses the URL fragment instead of the history API.
  7123. *
  7124. * @usageNotes
  7125. *
  7126. * Basic example of how you can use the hash location option:
  7127. * ```ts
  7128. * const appRoutes: Routes = [];
  7129. * bootstrapApplication(AppComponent,
  7130. * {
  7131. * providers: [
  7132. * provideRouter(appRoutes, withHashLocation())
  7133. * ]
  7134. * }
  7135. * );
  7136. * ```
  7137. *
  7138. * @see {@link provideRouter}
  7139. * @see {@link /api/common/HashLocationStrategy HashLocationStrategy}
  7140. *
  7141. * @returns A set of providers for use with `provideRouter`.
  7142. *
  7143. * @publicApi
  7144. */
  7145. function withHashLocation() {
  7146. const providers = [{ provide: LocationStrategy, useClass: HashLocationStrategy }];
  7147. return routerFeature(6 /* RouterFeatureKind.RouterHashLocationFeature */, providers);
  7148. }
  7149. /**
  7150. * Provides a function which is called when a navigation error occurs.
  7151. *
  7152. * This function is run inside application's [injection context](guide/di/dependency-injection-context)
  7153. * so you can use the [`inject`](api/core/inject) function.
  7154. *
  7155. * This function can return a `RedirectCommand` to convert the error to a redirect, similar to returning
  7156. * a `UrlTree` or `RedirectCommand` from a guard. This will also prevent the `Router` from emitting
  7157. * `NavigationError`; it will instead emit `NavigationCancel` with code NavigationCancellationCode.Redirect.
  7158. * Return values other than `RedirectCommand` are ignored and do not change any behavior with respect to
  7159. * how the `Router` handles the error.
  7160. *
  7161. * @usageNotes
  7162. *
  7163. * Basic example of how you can use the error handler option:
  7164. * ```ts
  7165. * const appRoutes: Routes = [];
  7166. * bootstrapApplication(AppComponent,
  7167. * {
  7168. * providers: [
  7169. * provideRouter(appRoutes, withNavigationErrorHandler((e: NavigationError) =>
  7170. * inject(MyErrorTracker).trackError(e)))
  7171. * ]
  7172. * }
  7173. * );
  7174. * ```
  7175. *
  7176. * @see {@link NavigationError}
  7177. * @see {@link /api/core/inject inject}
  7178. * @see {@link runInInjectionContext}
  7179. *
  7180. * @returns A set of providers for use with `provideRouter`.
  7181. *
  7182. * @publicApi
  7183. */
  7184. function withNavigationErrorHandler(handler) {
  7185. const providers = [
  7186. {
  7187. provide: NAVIGATION_ERROR_HANDLER,
  7188. useValue: handler,
  7189. },
  7190. ];
  7191. return routerFeature(7 /* RouterFeatureKind.NavigationErrorHandlerFeature */, providers);
  7192. }
  7193. /**
  7194. * Enables binding information from the `Router` state directly to the inputs of the component in
  7195. * `Route` configurations.
  7196. *
  7197. * @usageNotes
  7198. *
  7199. * Basic example of how you can enable the feature:
  7200. * ```ts
  7201. * const appRoutes: Routes = [];
  7202. * bootstrapApplication(AppComponent,
  7203. * {
  7204. * providers: [
  7205. * provideRouter(appRoutes, withComponentInputBinding())
  7206. * ]
  7207. * }
  7208. * );
  7209. * ```
  7210. *
  7211. * The router bindings information from any of the following sources:
  7212. *
  7213. * - query parameters
  7214. * - path and matrix parameters
  7215. * - static route data
  7216. * - data from resolvers
  7217. *
  7218. * Duplicate keys are resolved in the same order from above, from least to greatest,
  7219. * meaning that resolvers have the highest precedence and override any of the other information
  7220. * from the route.
  7221. *
  7222. * Importantly, when an input does not have an item in the route data with a matching key, this
  7223. * input is set to `undefined`. This prevents previous information from being
  7224. * retained if the data got removed from the route (i.e. if a query parameter is removed).
  7225. * Default values can be provided with a resolver on the route to ensure the value is always present
  7226. * or an input and use an input transform in the component.
  7227. *
  7228. * @see {@link /guide/components/inputs#input-transforms Input Transforms}
  7229. * @returns A set of providers for use with `provideRouter`.
  7230. */
  7231. function withComponentInputBinding() {
  7232. const providers = [
  7233. RoutedComponentInputBinder,
  7234. { provide: INPUT_BINDER, useExisting: RoutedComponentInputBinder },
  7235. ];
  7236. return routerFeature(8 /* RouterFeatureKind.ComponentInputBindingFeature */, providers);
  7237. }
  7238. /**
  7239. * Enables view transitions in the Router by running the route activation and deactivation inside of
  7240. * `document.startViewTransition`.
  7241. *
  7242. * Note: The View Transitions API is not available in all browsers. If the browser does not support
  7243. * view transitions, the Router will not attempt to start a view transition and continue processing
  7244. * the navigation as usual.
  7245. *
  7246. * @usageNotes
  7247. *
  7248. * Basic example of how you can enable the feature:
  7249. * ```ts
  7250. * const appRoutes: Routes = [];
  7251. * bootstrapApplication(AppComponent,
  7252. * {
  7253. * providers: [
  7254. * provideRouter(appRoutes, withViewTransitions())
  7255. * ]
  7256. * }
  7257. * );
  7258. * ```
  7259. *
  7260. * @returns A set of providers for use with `provideRouter`.
  7261. * @see https://developer.chrome.com/docs/web-platform/view-transitions/
  7262. * @see https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API
  7263. * @developerPreview
  7264. */
  7265. function withViewTransitions(options) {
  7266. const providers = [
  7267. { provide: CREATE_VIEW_TRANSITION, useValue: createViewTransition },
  7268. {
  7269. provide: VIEW_TRANSITION_OPTIONS,
  7270. useValue: { skipNextTransition: !!options?.skipInitialTransition, ...options },
  7271. },
  7272. ];
  7273. return routerFeature(9 /* RouterFeatureKind.ViewTransitionsFeature */, providers);
  7274. }
  7275. /**
  7276. * The directives defined in the `RouterModule`.
  7277. */
  7278. const ROUTER_DIRECTIVES = [RouterOutlet, RouterLink, RouterLinkActive, ɵEmptyOutletComponent];
  7279. /**
  7280. * @docsNotRequired
  7281. */
  7282. const ROUTER_FORROOT_GUARD = new InjectionToken(typeof ngDevMode === 'undefined' || ngDevMode ? 'router duplicate forRoot guard' : '');
  7283. // TODO(atscott): All of these except `ActivatedRoute` are `providedIn: 'root'`. They are only kept
  7284. // here to avoid a breaking change whereby the provider order matters based on where the
  7285. // `RouterModule`/`RouterTestingModule` is imported. These can/should be removed as a "breaking"
  7286. // change in a major version.
  7287. const ROUTER_PROVIDERS = [
  7288. Location,
  7289. { provide: UrlSerializer, useClass: DefaultUrlSerializer },
  7290. Router,
  7291. ChildrenOutletContexts,
  7292. { provide: ActivatedRoute, useFactory: rootRoute, deps: [Router] },
  7293. RouterConfigLoader,
  7294. // Only used to warn when `provideRoutes` is used without `RouterModule` or `provideRouter`. Can
  7295. // be removed when `provideRoutes` is removed.
  7296. typeof ngDevMode === 'undefined' || ngDevMode
  7297. ? { provide: ROUTER_IS_PROVIDED, useValue: true }
  7298. : [],
  7299. ];
  7300. /**
  7301. * @description
  7302. *
  7303. * Adds directives and providers for in-app navigation among views defined in an application.
  7304. * Use the Angular `Router` service to declaratively specify application states and manage state
  7305. * transitions.
  7306. *
  7307. * You can import this NgModule multiple times, once for each lazy-loaded bundle.
  7308. * However, only one `Router` service can be active.
  7309. * To ensure this, there are two ways to register routes when importing this module:
  7310. *
  7311. * * The `forRoot()` method creates an `NgModule` that contains all the directives, the given
  7312. * routes, and the `Router` service itself.
  7313. * * The `forChild()` method creates an `NgModule` that contains all the directives and the given
  7314. * routes, but does not include the `Router` service.
  7315. *
  7316. * @see [Routing and Navigation guide](guide/routing/common-router-tasks) for an
  7317. * overview of how the `Router` service should be used.
  7318. *
  7319. * @publicApi
  7320. */
  7321. class RouterModule {
  7322. constructor() {
  7323. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  7324. inject(ROUTER_FORROOT_GUARD, { optional: true });
  7325. }
  7326. }
  7327. /**
  7328. * Creates and configures a module with all the router providers and directives.
  7329. * Optionally sets up an application listener to perform an initial navigation.
  7330. *
  7331. * When registering the NgModule at the root, import as follows:
  7332. *
  7333. * ```ts
  7334. * @NgModule({
  7335. * imports: [RouterModule.forRoot(ROUTES)]
  7336. * })
  7337. * class MyNgModule {}
  7338. * ```
  7339. *
  7340. * @param routes An array of `Route` objects that define the navigation paths for the application.
  7341. * @param config An `ExtraOptions` configuration object that controls how navigation is performed.
  7342. * @return The new `NgModule`.
  7343. *
  7344. */
  7345. static forRoot(routes, config) {
  7346. return {
  7347. ngModule: RouterModule,
  7348. providers: [
  7349. ROUTER_PROVIDERS,
  7350. typeof ngDevMode === 'undefined' || ngDevMode
  7351. ? config?.enableTracing
  7352. ? withDebugTracing().ɵproviders
  7353. : []
  7354. : [],
  7355. { provide: ROUTES, multi: true, useValue: routes },
  7356. typeof ngDevMode === 'undefined' || ngDevMode
  7357. ? {
  7358. provide: ROUTER_FORROOT_GUARD,
  7359. useFactory: provideForRootGuard,
  7360. deps: [[Router, new Optional(), new SkipSelf()]],
  7361. }
  7362. : [],
  7363. config?.errorHandler
  7364. ? {
  7365. provide: NAVIGATION_ERROR_HANDLER,
  7366. useValue: config.errorHandler,
  7367. }
  7368. : [],
  7369. { provide: ROUTER_CONFIGURATION, useValue: config ? config : {} },
  7370. config?.useHash ? provideHashLocationStrategy() : providePathLocationStrategy(),
  7371. provideRouterScroller(),
  7372. config?.preloadingStrategy ? withPreloading(config.preloadingStrategy).ɵproviders : [],
  7373. config?.initialNavigation ? provideInitialNavigation(config) : [],
  7374. config?.bindToComponentInputs ? withComponentInputBinding().ɵproviders : [],
  7375. config?.enableViewTransitions ? withViewTransitions().ɵproviders : [],
  7376. provideRouterInitializer(),
  7377. ],
  7378. };
  7379. }
  7380. /**
  7381. * Creates a module with all the router directives and a provider registering routes,
  7382. * without creating a new Router service.
  7383. * When registering for submodules and lazy-loaded submodules, create the NgModule as follows:
  7384. *
  7385. * ```ts
  7386. * @NgModule({
  7387. * imports: [RouterModule.forChild(ROUTES)]
  7388. * })
  7389. * class MyNgModule {}
  7390. * ```
  7391. *
  7392. * @param routes An array of `Route` objects that define the navigation paths for the submodule.
  7393. * @return The new NgModule.
  7394. *
  7395. */
  7396. static forChild(routes) {
  7397. return {
  7398. ngModule: RouterModule,
  7399. providers: [{ provide: ROUTES, multi: true, useValue: routes }],
  7400. };
  7401. }
  7402. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  7403. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.4", ngImport: i0, type: RouterModule, imports: [RouterOutlet, RouterLink, RouterLinkActive, ɵEmptyOutletComponent], exports: [RouterOutlet, RouterLink, RouterLinkActive, ɵEmptyOutletComponent] });
  7404. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterModule });
  7405. }
  7406. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.4", ngImport: i0, type: RouterModule, decorators: [{
  7407. type: NgModule,
  7408. args: [{
  7409. imports: ROUTER_DIRECTIVES,
  7410. exports: ROUTER_DIRECTIVES,
  7411. }]
  7412. }], ctorParameters: () => [] });
  7413. /**
  7414. * For internal use by `RouterModule` only. Note that this differs from `withInMemoryRouterScroller`
  7415. * because it reads from the `ExtraOptions` which should not be used in the standalone world.
  7416. */
  7417. function provideRouterScroller() {
  7418. return {
  7419. provide: ROUTER_SCROLLER,
  7420. useFactory: () => {
  7421. const viewportScroller = inject(ViewportScroller);
  7422. const zone = inject(NgZone);
  7423. const config = inject(ROUTER_CONFIGURATION);
  7424. const transitions = inject(NavigationTransitions);
  7425. const urlSerializer = inject(UrlSerializer);
  7426. if (config.scrollOffset) {
  7427. viewportScroller.setOffset(config.scrollOffset);
  7428. }
  7429. return new RouterScroller(urlSerializer, transitions, viewportScroller, zone, config);
  7430. },
  7431. };
  7432. }
  7433. // Note: For internal use only with `RouterModule`. Standalone setup via `provideRouter` should
  7434. // provide hash location directly via `{provide: LocationStrategy, useClass: HashLocationStrategy}`.
  7435. function provideHashLocationStrategy() {
  7436. return { provide: LocationStrategy, useClass: HashLocationStrategy };
  7437. }
  7438. // Note: For internal use only with `RouterModule`. Standalone setup via `provideRouter` does not
  7439. // need this at all because `PathLocationStrategy` is the default factory for `LocationStrategy`.
  7440. function providePathLocationStrategy() {
  7441. return { provide: LocationStrategy, useClass: PathLocationStrategy };
  7442. }
  7443. function provideForRootGuard(router) {
  7444. if (router) {
  7445. throw new _RuntimeError(4007 /* RuntimeErrorCode.FOR_ROOT_CALLED_TWICE */, `The Router was provided more than once. This can happen if 'forRoot' is used outside of the root injector.` +
  7446. ` Lazy loaded modules should use RouterModule.forChild() instead.`);
  7447. }
  7448. return 'guarded';
  7449. }
  7450. // Note: For internal use only with `RouterModule`. Standalone router setup with `provideRouter`
  7451. // users call `withXInitialNavigation` directly.
  7452. function provideInitialNavigation(config) {
  7453. return [
  7454. config.initialNavigation === 'disabled' ? withDisabledInitialNavigation().ɵproviders : [],
  7455. config.initialNavigation === 'enabledBlocking'
  7456. ? withEnabledBlockingInitialNavigation().ɵproviders
  7457. : [],
  7458. ];
  7459. }
  7460. // TODO(atscott): This should not be in the public API
  7461. /**
  7462. * A DI token for the router initializer that
  7463. * is called after the app is bootstrapped.
  7464. *
  7465. * @publicApi
  7466. */
  7467. const ROUTER_INITIALIZER = new InjectionToken(typeof ngDevMode === 'undefined' || ngDevMode ? 'Router Initializer' : '');
  7468. function provideRouterInitializer() {
  7469. return [
  7470. // ROUTER_INITIALIZER token should be removed. It's public API but shouldn't be. We can just
  7471. // have `getBootstrapListener` directly attached to APP_BOOTSTRAP_LISTENER.
  7472. { provide: ROUTER_INITIALIZER, useFactory: getBootstrapListener },
  7473. { provide: APP_BOOTSTRAP_LISTENER, multi: true, useExisting: ROUTER_INITIALIZER },
  7474. ];
  7475. }
  7476. /**
  7477. * Maps an array of injectable classes with canMatch functions to an array of equivalent
  7478. * `CanMatchFn` for use in a `Route` definition.
  7479. *
  7480. * Usage {@example router/utils/functional_guards.ts region='CanActivate'}
  7481. *
  7482. * @publicApi
  7483. * @see {@link Route}
  7484. */
  7485. function mapToCanMatch(providers) {
  7486. return providers.map((provider) => (...params) => inject(provider).canMatch(...params));
  7487. }
  7488. /**
  7489. * Maps an array of injectable classes with canActivate functions to an array of equivalent
  7490. * `CanActivateFn` for use in a `Route` definition.
  7491. *
  7492. * Usage {@example router/utils/functional_guards.ts region='CanActivate'}
  7493. *
  7494. * @publicApi
  7495. * @see {@link Route}
  7496. */
  7497. function mapToCanActivate(providers) {
  7498. return providers.map((provider) => (...params) => inject(provider).canActivate(...params));
  7499. }
  7500. /**
  7501. * Maps an array of injectable classes with canActivateChild functions to an array of equivalent
  7502. * `CanActivateChildFn` for use in a `Route` definition.
  7503. *
  7504. * Usage {@example router/utils/functional_guards.ts region='CanActivate'}
  7505. *
  7506. * @publicApi
  7507. * @see {@link Route}
  7508. */
  7509. function mapToCanActivateChild(providers) {
  7510. return providers.map((provider) => (...params) => inject(provider).canActivateChild(...params));
  7511. }
  7512. /**
  7513. * Maps an array of injectable classes with canDeactivate functions to an array of equivalent
  7514. * `CanDeactivateFn` for use in a `Route` definition.
  7515. *
  7516. * Usage {@example router/utils/functional_guards.ts region='CanActivate'}
  7517. *
  7518. * @publicApi
  7519. * @see {@link Route}
  7520. */
  7521. function mapToCanDeactivate(providers) {
  7522. return providers.map((provider) => (...params) => inject(provider).canDeactivate(...params));
  7523. }
  7524. /**
  7525. * Maps an injectable class with a resolve function to an equivalent `ResolveFn`
  7526. * for use in a `Route` definition.
  7527. *
  7528. * Usage {@example router/utils/functional_guards.ts region='Resolve'}
  7529. *
  7530. * @publicApi
  7531. * @see {@link Route}
  7532. */
  7533. function mapToResolve(provider) {
  7534. return (...params) => inject(provider).resolve(...params);
  7535. }
  7536. /**
  7537. * @module
  7538. * @description
  7539. * Entry point for all public APIs of the router package.
  7540. */
  7541. /**
  7542. * @publicApi
  7543. */
  7544. const VERSION = new Version('19.2.4');
  7545. export { ActivatedRoute, ActivatedRouteSnapshot, ActivationEnd, ActivationStart, BaseRouteReuseStrategy, ChildActivationEnd, ChildActivationStart, ChildrenOutletContexts, DefaultTitleStrategy, DefaultUrlSerializer, EventType, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationCancellationCode, NavigationEnd, NavigationError, NavigationSkipped, NavigationSkippedCode, NavigationStart, NoPreloading, OutletContext, PRIMARY_OUTLET, PreloadAllModules, PreloadingStrategy, ROUTER_CONFIGURATION, ROUTER_INITIALIZER, ROUTER_OUTLET_DATA, ROUTES, RedirectCommand, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouteReuseStrategy, Router, RouterEvent, RouterLink, RouterLinkActive, RouterLink as RouterLinkWithHref, RouterModule, RouterOutlet, RouterPreloader, RouterState, RouterStateSnapshot, RoutesRecognized, Scroll, TitleStrategy, UrlHandlingStrategy, UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree, VERSION, convertToParamMap, createUrlTreeFromSnapshot, defaultUrlMatcher, mapToCanActivate, mapToCanActivateChild, mapToCanDeactivate, mapToCanMatch, mapToResolve, provideRouter, provideRoutes, withComponentInputBinding, withDebugTracing, withDisabledInitialNavigation, withEnabledBlockingInitialNavigation, withHashLocation, withInMemoryScrolling, withNavigationErrorHandler, withPreloading, withRouterConfig, withViewTransitions, ɵEmptyOutletComponent, ROUTER_PROVIDERS as ɵROUTER_PROVIDERS, afterNextNavigation as ɵafterNextNavigation, loadChildren as ɵloadChildren };
  7546. //# sourceMappingURL=router.mjs.map