Ei kuvausta

angular-ui-router.js 408KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076
  1. /**
  2. * State-based routing for AngularJS 1.x
  3. * NOTICE: This monolithic bundle also bundles the @uirouter/core code.
  4. * This causes it to be incompatible with plugins that depend on @uirouter/core.
  5. * We recommend switching to the ui-router-core.js and ui-router-angularjs.js bundles instead.
  6. * For more information, see https://ui-router.github.io/blog/uirouter-for-angularjs-umd-bundles
  7. * @version v1.0.10
  8. * @link https://ui-router.github.io
  9. * @license MIT License, http://www.opensource.org/licenses/MIT
  10. */
  11. (function (global, factory) {
  12. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('angular')) :
  13. typeof define === 'function' && define.amd ? define(['exports', 'angular'], factory) :
  14. (factory((global['@uirouter/angularjs'] = {}),global.angular));
  15. }(this, (function (exports,ng_from_import) { 'use strict';
  16. var ng_from_global = angular;
  17. var ng = (ng_from_import && ng_from_import.module) ? ng_from_import : ng_from_global;
  18. /**
  19. * Higher order functions
  20. *
  21. * These utility functions are exported, but are subject to change without notice.
  22. *
  23. * @module common_hof
  24. */ /** */
  25. /**
  26. * Returns a new function for [Partial Application](https://en.wikipedia.org/wiki/Partial_application) of the original function.
  27. *
  28. * Given a function with N parameters, returns a new function that supports partial application.
  29. * The new function accepts anywhere from 1 to N parameters. When that function is called with M parameters,
  30. * where M is less than N, it returns a new function that accepts the remaining parameters. It continues to
  31. * accept more parameters until all N parameters have been supplied.
  32. *
  33. *
  34. * This contrived example uses a partially applied function as an predicate, which returns true
  35. * if an object is found in both arrays.
  36. * @example
  37. * ```
  38. * // returns true if an object is in both of the two arrays
  39. * function inBoth(array1, array2, object) {
  40. * return array1.indexOf(object) !== -1 &&
  41. * array2.indexOf(object) !== 1;
  42. * }
  43. * let obj1, obj2, obj3, obj4, obj5, obj6, obj7
  44. * let foos = [obj1, obj3]
  45. * let bars = [obj3, obj4, obj5]
  46. *
  47. * // A curried "copy" of inBoth
  48. * let curriedInBoth = curry(inBoth);
  49. * // Partially apply both the array1 and array2
  50. * let inFoosAndBars = curriedInBoth(foos, bars);
  51. *
  52. * // Supply the final argument; since all arguments are
  53. * // supplied, the original inBoth function is then called.
  54. * let obj1InBoth = inFoosAndBars(obj1); // false
  55. *
  56. * // Use the inFoosAndBars as a predicate.
  57. * // Filter, on each iteration, supplies the final argument
  58. * let allObjs = [ obj1, obj2, obj3, obj4, obj5, obj6, obj7 ];
  59. * let foundInBoth = allObjs.filter(inFoosAndBars); // [ obj3 ]
  60. *
  61. * ```
  62. *
  63. * Stolen from: http://stackoverflow.com/questions/4394747/javascript-curry-function
  64. *
  65. * @param fn
  66. * @returns {*|function(): (*|any)}
  67. */
  68. function curry(fn) {
  69. var initial_args = [].slice.apply(arguments, [1]);
  70. var func_args_length = fn.length;
  71. function curried(args) {
  72. if (args.length >= func_args_length)
  73. return fn.apply(null, args);
  74. return function () {
  75. return curried(args.concat([].slice.apply(arguments)));
  76. };
  77. }
  78. return curried(initial_args);
  79. }
  80. /**
  81. * Given a varargs list of functions, returns a function that composes the argument functions, right-to-left
  82. * given: f(x), g(x), h(x)
  83. * let composed = compose(f,g,h)
  84. * then, composed is: f(g(h(x)))
  85. */
  86. function compose() {
  87. var args = arguments;
  88. var start = args.length - 1;
  89. return function () {
  90. var i = start, result = args[start].apply(this, arguments);
  91. while (i--)
  92. result = args[i].call(this, result);
  93. return result;
  94. };
  95. }
  96. /**
  97. * Given a varargs list of functions, returns a function that is composes the argument functions, left-to-right
  98. * given: f(x), g(x), h(x)
  99. * let piped = pipe(f,g,h);
  100. * then, piped is: h(g(f(x)))
  101. */
  102. function pipe() {
  103. var funcs = [];
  104. for (var _i = 0; _i < arguments.length; _i++) {
  105. funcs[_i] = arguments[_i];
  106. }
  107. return compose.apply(null, [].slice.call(arguments).reverse());
  108. }
  109. /**
  110. * Given a property name, returns a function that returns that property from an object
  111. * let obj = { foo: 1, name: "blarg" };
  112. * let getName = prop("name");
  113. * getName(obj) === "blarg"
  114. */
  115. var prop = function (name) {
  116. return function (obj) { return obj && obj[name]; };
  117. };
  118. /**
  119. * Given a property name and a value, returns a function that returns a boolean based on whether
  120. * the passed object has a property that matches the value
  121. * let obj = { foo: 1, name: "blarg" };
  122. * let getName = propEq("name", "blarg");
  123. * getName(obj) === true
  124. */
  125. var propEq = curry(function (name, val, obj) { return obj && obj[name] === val; });
  126. /**
  127. * Given a dotted property name, returns a function that returns a nested property from an object, or undefined
  128. * let obj = { id: 1, nestedObj: { foo: 1, name: "blarg" }, };
  129. * let getName = prop("nestedObj.name");
  130. * getName(obj) === "blarg"
  131. * let propNotFound = prop("this.property.doesnt.exist");
  132. * propNotFound(obj) === undefined
  133. */
  134. var parse = function (name) {
  135. return pipe.apply(null, name.split(".").map(prop));
  136. };
  137. /**
  138. * Given a function that returns a truthy or falsey value, returns a
  139. * function that returns the opposite (falsey or truthy) value given the same inputs
  140. */
  141. var not = function (fn) {
  142. return function () {
  143. var args = [];
  144. for (var _i = 0; _i < arguments.length; _i++) {
  145. args[_i] = arguments[_i];
  146. }
  147. return !fn.apply(null, args);
  148. };
  149. };
  150. /**
  151. * Given two functions that return truthy or falsey values, returns a function that returns truthy
  152. * if both functions return truthy for the given arguments
  153. */
  154. function and(fn1, fn2) {
  155. return function () {
  156. var args = [];
  157. for (var _i = 0; _i < arguments.length; _i++) {
  158. args[_i] = arguments[_i];
  159. }
  160. return fn1.apply(null, args) && fn2.apply(null, args);
  161. };
  162. }
  163. /**
  164. * Given two functions that return truthy or falsey values, returns a function that returns truthy
  165. * if at least one of the functions returns truthy for the given arguments
  166. */
  167. function or(fn1, fn2) {
  168. return function () {
  169. var args = [];
  170. for (var _i = 0; _i < arguments.length; _i++) {
  171. args[_i] = arguments[_i];
  172. }
  173. return fn1.apply(null, args) || fn2.apply(null, args);
  174. };
  175. }
  176. /**
  177. * Check if all the elements of an array match a predicate function
  178. *
  179. * @param fn1 a predicate function `fn1`
  180. * @returns a function which takes an array and returns true if `fn1` is true for all elements of the array
  181. */
  182. var all = function (fn1) {
  183. return function (arr) { return arr.reduce(function (b, x) { return b && !!fn1(x); }, true); };
  184. };
  185. var any = function (fn1) {
  186. return function (arr) { return arr.reduce(function (b, x) { return b || !!fn1(x); }, false); };
  187. };
  188. /** Given a class, returns a Predicate function that returns true if the object is of that class */
  189. var is = function (ctor) {
  190. return function (obj) {
  191. return (obj != null && obj.constructor === ctor || obj instanceof ctor);
  192. };
  193. };
  194. /** Given a value, returns a Predicate function that returns true if another value is === equal to the original value */
  195. var eq = function (val) { return function (other) {
  196. return val === other;
  197. }; };
  198. /** Given a value, returns a function which returns the value */
  199. var val = function (v) { return function () { return v; }; };
  200. function invoke(fnName, args) {
  201. return function (obj) {
  202. return obj[fnName].apply(obj, args);
  203. };
  204. }
  205. /**
  206. * Sorta like Pattern Matching (a functional programming conditional construct)
  207. *
  208. * See http://c2.com/cgi/wiki?PatternMatching
  209. *
  210. * This is a conditional construct which allows a series of predicates and output functions
  211. * to be checked and then applied. Each predicate receives the input. If the predicate
  212. * returns truthy, then its matching output function (mapping function) is provided with
  213. * the input and, then the result is returned.
  214. *
  215. * Each combination (2-tuple) of predicate + output function should be placed in an array
  216. * of size 2: [ predicate, mapFn ]
  217. *
  218. * These 2-tuples should be put in an outer array.
  219. *
  220. * @example
  221. * ```
  222. *
  223. * // Here's a 2-tuple where the first element is the isString predicate
  224. * // and the second element is a function that returns a description of the input
  225. * let firstTuple = [ angular.isString, (input) => `Heres your string ${input}` ];
  226. *
  227. * // Second tuple: predicate "isNumber", mapfn returns a description
  228. * let secondTuple = [ angular.isNumber, (input) => `(${input}) That's a number!` ];
  229. *
  230. * let third = [ (input) => input === null, (input) => `Oh, null...` ];
  231. *
  232. * let fourth = [ (input) => input === undefined, (input) => `notdefined` ];
  233. *
  234. * let descriptionOf = pattern([ firstTuple, secondTuple, third, fourth ]);
  235. *
  236. * console.log(descriptionOf(undefined)); // 'notdefined'
  237. * console.log(descriptionOf(55)); // '(55) That's a number!'
  238. * console.log(descriptionOf("foo")); // 'Here's your string foo'
  239. * ```
  240. *
  241. * @param struct A 2D array. Each element of the array should be an array, a 2-tuple,
  242. * with a Predicate and a mapping/output function
  243. * @returns {function(any): *}
  244. */
  245. function pattern(struct) {
  246. return function (x) {
  247. for (var i = 0; i < struct.length; i++) {
  248. if (struct[i][0](x))
  249. return struct[i][1](x);
  250. }
  251. };
  252. }
  253. /**
  254. * @coreapi
  255. * @module core
  256. */
  257. /**
  258. * Matches state names using glob-like pattern strings.
  259. *
  260. * Globs can be used in specific APIs including:
  261. *
  262. * - [[StateService.is]]
  263. * - [[StateService.includes]]
  264. * - The first argument to Hook Registration functions like [[TransitionService.onStart]]
  265. * - [[HookMatchCriteria]] and [[HookMatchCriterion]]
  266. *
  267. * A `Glob` string is a pattern which matches state names.
  268. * Nested state names are split into segments (separated by a dot) when processing.
  269. * The state named `foo.bar.baz` is split into three segments ['foo', 'bar', 'baz']
  270. *
  271. * Globs work according to the following rules:
  272. *
  273. * ### Exact match:
  274. *
  275. * The glob `'A.B'` matches the state named exactly `'A.B'`.
  276. *
  277. * | Glob |Matches states named|Does not match state named|
  278. * |:------------|:--------------------|:---------------------|
  279. * | `'A'` | `'A'` | `'B'` , `'A.C'` |
  280. * | `'A.B'` | `'A.B'` | `'A'` , `'A.B.C'` |
  281. * | `'foo'` | `'foo'` | `'FOO'` , `'foo.bar'`|
  282. *
  283. * ### Single star (`*`)
  284. *
  285. * A single star (`*`) is a wildcard that matches exactly one segment.
  286. *
  287. * | Glob |Matches states named |Does not match state named |
  288. * |:------------|:---------------------|:--------------------------|
  289. * | `'*'` | `'A'` , `'Z'` | `'A.B'` , `'Z.Y.X'` |
  290. * | `'A.*'` | `'A.B'` , `'A.C'` | `'A'` , `'A.B.C'` |
  291. * | `'A.*.*'` | `'A.B.C'` , `'A.X.Y'`| `'A'`, `'A.B'` , `'Z.Y.X'`|
  292. *
  293. * ### Double star (`**`)
  294. *
  295. * A double star (`'**'`) is a wildcard that matches *zero or more segments*
  296. *
  297. * | Glob |Matches states named |Does not match state named |
  298. * |:------------|:----------------------------------------------|:----------------------------------|
  299. * | `'**'` | `'A'` , `'A.B'`, `'Z.Y.X'` | (matches all states) |
  300. * | `'A.**'` | `'A'` , `'A.B'` , `'A.C.X'` | `'Z.Y.X'` |
  301. * | `'**.X'` | `'X'` , `'A.X'` , `'Z.Y.X'` | `'A'` , `'A.login.Z'` |
  302. * | `'A.**.X'` | `'A.X'` , `'A.B.X'` , `'A.B.C.X'` | `'A'` , `'A.B.C'` |
  303. *
  304. */
  305. var Glob = /** @class */ (function () {
  306. function Glob(text) {
  307. this.text = text;
  308. this.glob = text.split('.');
  309. var regexpString = this.text.split('.')
  310. .map(function (seg) {
  311. if (seg === '**')
  312. return '(?:|(?:\\.[^.]*)*)';
  313. if (seg === '*')
  314. return '\\.[^.]*';
  315. return '\\.' + seg;
  316. }).join('');
  317. this.regexp = new RegExp("^" + regexpString + "$");
  318. }
  319. Glob.prototype.matches = function (name) {
  320. return this.regexp.test('.' + name);
  321. };
  322. /** Returns true if the string has glob-like characters in it */
  323. Glob.is = function (text) {
  324. return !!/[!,*]+/.exec(text);
  325. };
  326. /** Returns a glob from the string, or null if the string isn't Glob-like */
  327. Glob.fromString = function (text) {
  328. return Glob.is(text) ? new Glob(text) : null;
  329. };
  330. return Glob;
  331. }());
  332. /**
  333. * Internal representation of a UI-Router state.
  334. *
  335. * Instances of this class are created when a [[StateDeclaration]] is registered with the [[StateRegistry]].
  336. *
  337. * A registered [[StateDeclaration]] is augmented with a getter ([[StateDeclaration.$$state]]) which returns the corresponding [[StateObject]] object.
  338. *
  339. * This class prototypally inherits from the corresponding [[StateDeclaration]].
  340. * Each of its own properties (i.e., `hasOwnProperty`) are built using builders from the [[StateBuilder]].
  341. */
  342. var StateObject = /** @class */ (function () {
  343. /** @deprecated use State.create() */
  344. function StateObject(config) {
  345. return StateObject.create(config || {});
  346. }
  347. /**
  348. * Create a state object to put the private/internal implementation details onto.
  349. * The object's prototype chain looks like:
  350. * (Internal State Object) -> (Copy of State.prototype) -> (State Declaration object) -> (State Declaration's prototype...)
  351. *
  352. * @param stateDecl the user-supplied State Declaration
  353. * @returns {StateObject} an internal State object
  354. */
  355. StateObject.create = function (stateDecl) {
  356. stateDecl = StateObject.isStateClass(stateDecl) ? new stateDecl() : stateDecl;
  357. var state = inherit(inherit(stateDecl, StateObject.prototype));
  358. stateDecl.$$state = function () { return state; };
  359. state.self = stateDecl;
  360. state.__stateObjectCache = {
  361. nameGlob: Glob.fromString(state.name) // might return null
  362. };
  363. return state;
  364. };
  365. /**
  366. * Returns true if the provided parameter is the same state.
  367. *
  368. * Compares the identity of the state against the passed value, which is either an object
  369. * reference to the actual `State` instance, the original definition object passed to
  370. * `$stateProvider.state()`, or the fully-qualified name.
  371. *
  372. * @param ref Can be one of (a) a `State` instance, (b) an object that was passed
  373. * into `$stateProvider.state()`, (c) the fully-qualified name of a state as a string.
  374. * @returns Returns `true` if `ref` matches the current `State` instance.
  375. */
  376. StateObject.prototype.is = function (ref) {
  377. return this === ref || this.self === ref || this.fqn() === ref;
  378. };
  379. /**
  380. * @deprecated this does not properly handle dot notation
  381. * @returns Returns a dot-separated name of the state.
  382. */
  383. StateObject.prototype.fqn = function () {
  384. if (!this.parent || !(this.parent instanceof this.constructor))
  385. return this.name;
  386. var name = this.parent.fqn();
  387. return name ? name + "." + this.name : this.name;
  388. };
  389. /**
  390. * Returns the root node of this state's tree.
  391. *
  392. * @returns The root of this state's tree.
  393. */
  394. StateObject.prototype.root = function () {
  395. return this.parent && this.parent.root() || this;
  396. };
  397. /**
  398. * Gets the state's `Param` objects
  399. *
  400. * Gets the list of [[Param]] objects owned by the state.
  401. * If `opts.inherit` is true, it also includes the ancestor states' [[Param]] objects.
  402. * If `opts.matchingKeys` exists, returns only `Param`s whose `id` is a key on the `matchingKeys` object
  403. *
  404. * @param opts options
  405. */
  406. StateObject.prototype.parameters = function (opts) {
  407. opts = defaults(opts, { inherit: true, matchingKeys: null });
  408. var inherited = opts.inherit && this.parent && this.parent.parameters() || [];
  409. return inherited.concat(values(this.params))
  410. .filter(function (param) { return !opts.matchingKeys || opts.matchingKeys.hasOwnProperty(param.id); });
  411. };
  412. /**
  413. * Returns a single [[Param]] that is owned by the state
  414. *
  415. * If `opts.inherit` is true, it also searches the ancestor states` [[Param]]s.
  416. * @param id the name of the [[Param]] to return
  417. * @param opts options
  418. */
  419. StateObject.prototype.parameter = function (id, opts) {
  420. if (opts === void 0) { opts = {}; }
  421. return (this.url && this.url.parameter(id, opts) ||
  422. find(values(this.params), propEq('id', id)) ||
  423. opts.inherit && this.parent && this.parent.parameter(id));
  424. };
  425. StateObject.prototype.toString = function () {
  426. return this.fqn();
  427. };
  428. /** Predicate which returns true if the object is an class with @State() decorator */
  429. StateObject.isStateClass = function (stateDecl) {
  430. return isFunction(stateDecl) && stateDecl['__uiRouterState'] === true;
  431. };
  432. /** Predicate which returns true if the object is an internal [[StateObject]] object */
  433. StateObject.isState = function (obj) {
  434. return isObject(obj['__stateObjectCache']);
  435. };
  436. return StateObject;
  437. }());
  438. /** Predicates
  439. *
  440. * These predicates return true/false based on the input.
  441. * Although these functions are exported, they are subject to change without notice.
  442. *
  443. * @module common_predicates
  444. */
  445. /** */
  446. var toStr = Object.prototype.toString;
  447. var tis = function (t) { return function (x) { return typeof (x) === t; }; };
  448. var isUndefined = tis('undefined');
  449. var isDefined = not(isUndefined);
  450. var isNull = function (o) { return o === null; };
  451. var isNullOrUndefined = or(isNull, isUndefined);
  452. var isFunction = tis('function');
  453. var isNumber = tis('number');
  454. var isString = tis('string');
  455. var isObject = function (x) { return x !== null && typeof x === 'object'; };
  456. var isArray = Array.isArray;
  457. var isDate = (function (x) { return toStr.call(x) === '[object Date]'; });
  458. var isRegExp = (function (x) { return toStr.call(x) === '[object RegExp]'; });
  459. var isState = StateObject.isState;
  460. /**
  461. * Predicate which checks if a value is injectable
  462. *
  463. * A value is "injectable" if it is a function, or if it is an ng1 array-notation-style array
  464. * where all the elements in the array are Strings, except the last one, which is a Function
  465. */
  466. function isInjectable(val$$1) {
  467. if (isArray(val$$1) && val$$1.length) {
  468. var head = val$$1.slice(0, -1), tail = val$$1.slice(-1);
  469. return !(head.filter(not(isString)).length || tail.filter(not(isFunction)).length);
  470. }
  471. return isFunction(val$$1);
  472. }
  473. /**
  474. * Predicate which checks if a value looks like a Promise
  475. *
  476. * It is probably a Promise if it's an object, and it has a `then` property which is a Function
  477. */
  478. var isPromise = and(isObject, pipe(prop('then'), isFunction));
  479. var notImplemented = function (fnname) { return function () {
  480. throw new Error(fnname + "(): No coreservices implementation for UI-Router is loaded.");
  481. }; };
  482. var services = {
  483. $q: undefined,
  484. $injector: undefined,
  485. };
  486. /**
  487. * Random utility functions used in the UI-Router code
  488. *
  489. * These functions are exported, but are subject to change without notice.
  490. *
  491. * @preferred
  492. * @module common
  493. */
  494. /** for typedoc */
  495. var root = (typeof self === 'object' && self.self === self && self) ||
  496. (typeof global === 'object' && global.global === global && global) || undefined;
  497. var angular$1 = root.angular || {};
  498. var fromJson = angular$1.fromJson || JSON.parse.bind(JSON);
  499. var toJson = angular$1.toJson || JSON.stringify.bind(JSON);
  500. var forEach = angular$1.forEach || _forEach;
  501. var extend = Object.assign || _extend;
  502. var equals = angular$1.equals || _equals;
  503. function identity(x) { return x; }
  504. function noop$1() { }
  505. /**
  506. * Builds proxy functions on the `to` object which pass through to the `from` object.
  507. *
  508. * For each key in `fnNames`, creates a proxy function on the `to` object.
  509. * The proxy function calls the real function on the `from` object.
  510. *
  511. *
  512. * #### Example:
  513. * This example creates an new class instance whose functions are prebound to the new'd object.
  514. * ```js
  515. * class Foo {
  516. * constructor(data) {
  517. * // Binds all functions from Foo.prototype to 'this',
  518. * // then copies them to 'this'
  519. * bindFunctions(Foo.prototype, this, this);
  520. * this.data = data;
  521. * }
  522. *
  523. * log() {
  524. * console.log(this.data);
  525. * }
  526. * }
  527. *
  528. * let myFoo = new Foo([1,2,3]);
  529. * var logit = myFoo.log;
  530. * logit(); // logs [1, 2, 3] from the myFoo 'this' instance
  531. * ```
  532. *
  533. * #### Example:
  534. * This example creates a bound version of a service function, and copies it to another object
  535. * ```
  536. *
  537. * var SomeService = {
  538. * this.data = [3, 4, 5];
  539. * this.log = function() {
  540. * console.log(this.data);
  541. * }
  542. * }
  543. *
  544. * // Constructor fn
  545. * function OtherThing() {
  546. * // Binds all functions from SomeService to SomeService,
  547. * // then copies them to 'this'
  548. * bindFunctions(SomeService, this, SomeService);
  549. * }
  550. *
  551. * let myOtherThing = new OtherThing();
  552. * myOtherThing.log(); // logs [3, 4, 5] from SomeService's 'this'
  553. * ```
  554. *
  555. * @param source A function that returns the source object which contains the original functions to be bound
  556. * @param target A function that returns the target object which will receive the bound functions
  557. * @param bind A function that returns the object which the functions will be bound to
  558. * @param fnNames The function names which will be bound (Defaults to all the functions found on the 'from' object)
  559. * @param latebind If true, the binding of the function is delayed until the first time it's invoked
  560. */
  561. function createProxyFunctions(source, target, bind, fnNames, latebind) {
  562. if (latebind === void 0) { latebind = false; }
  563. var bindFunction = function (fnName) {
  564. return source()[fnName].bind(bind());
  565. };
  566. var makeLateRebindFn = function (fnName) { return function lateRebindFunction() {
  567. target[fnName] = bindFunction(fnName);
  568. return target[fnName].apply(null, arguments);
  569. }; };
  570. fnNames = fnNames || Object.keys(source());
  571. return fnNames.reduce(function (acc, name) {
  572. acc[name] = latebind ? makeLateRebindFn(name) : bindFunction(name);
  573. return acc;
  574. }, target);
  575. }
  576. /**
  577. * prototypal inheritance helper.
  578. * Creates a new object which has `parent` object as its prototype, and then copies the properties from `extra` onto it
  579. */
  580. var inherit = function (parent, extra) {
  581. return extend(Object.create(parent), extra);
  582. };
  583. /** Given an array, returns true if the object is found in the array, (using indexOf) */
  584. var inArray = curry(_inArray);
  585. function _inArray(array, obj) {
  586. return array.indexOf(obj) !== -1;
  587. }
  588. /**
  589. * Given an array, and an item, if the item is found in the array, it removes it (in-place).
  590. * The same array is returned
  591. */
  592. var removeFrom = curry(_removeFrom);
  593. function _removeFrom(array, obj) {
  594. var idx = array.indexOf(obj);
  595. if (idx >= 0)
  596. array.splice(idx, 1);
  597. return array;
  598. }
  599. /** pushes a values to an array and returns the value */
  600. var pushTo = curry(_pushTo);
  601. function _pushTo(arr, val$$1) {
  602. return (arr.push(val$$1), val$$1);
  603. }
  604. /** Given an array of (deregistration) functions, calls all functions and removes each one from the source array */
  605. var deregAll = function (functions) {
  606. return functions.slice().forEach(function (fn) {
  607. typeof fn === 'function' && fn();
  608. removeFrom(functions, fn);
  609. });
  610. };
  611. /**
  612. * Applies a set of defaults to an options object. The options object is filtered
  613. * to only those properties of the objects in the defaultsList.
  614. * Earlier objects in the defaultsList take precedence when applying defaults.
  615. */
  616. function defaults(opts) {
  617. var defaultsList = [];
  618. for (var _i = 1; _i < arguments.length; _i++) {
  619. defaultsList[_i - 1] = arguments[_i];
  620. }
  621. var _defaultsList = defaultsList.concat({}).reverse();
  622. var defaultVals = extend.apply(null, _defaultsList);
  623. return extend({}, defaultVals, pick(opts || {}, Object.keys(defaultVals)));
  624. }
  625. /** Reduce function that merges each element of the list into a single object, using extend */
  626. var mergeR = function (memo, item) { return extend(memo, item); };
  627. /**
  628. * Finds the common ancestor path between two states.
  629. *
  630. * @param {Object} first The first state.
  631. * @param {Object} second The second state.
  632. * @return {Array} Returns an array of state names in descending order, not including the root.
  633. */
  634. function ancestors(first, second) {
  635. var path = [];
  636. for (var n in first.path) {
  637. if (first.path[n] !== second.path[n])
  638. break;
  639. path.push(first.path[n]);
  640. }
  641. return path;
  642. }
  643. /**
  644. * Return a copy of the object only containing the whitelisted properties.
  645. *
  646. * #### Example:
  647. * ```
  648. * var foo = { a: 1, b: 2, c: 3 };
  649. * var ab = pick(foo, ['a', 'b']); // { a: 1, b: 2 }
  650. * ```
  651. * @param obj the source object
  652. * @param propNames an Array of strings, which are the whitelisted property names
  653. */
  654. function pick(obj, propNames) {
  655. var objCopy = {};
  656. for (var prop_1 in obj) {
  657. if (propNames.indexOf(prop_1) !== -1) {
  658. objCopy[prop_1] = obj[prop_1];
  659. }
  660. }
  661. return objCopy;
  662. }
  663. /**
  664. * Return a copy of the object omitting the blacklisted properties.
  665. *
  666. * @example
  667. * ```
  668. *
  669. * var foo = { a: 1, b: 2, c: 3 };
  670. * var ab = omit(foo, ['a', 'b']); // { c: 3 }
  671. * ```
  672. * @param obj the source object
  673. * @param propNames an Array of strings, which are the blacklisted property names
  674. */
  675. function omit(obj, propNames) {
  676. return Object.keys(obj)
  677. .filter(not(inArray(propNames)))
  678. .reduce(function (acc, key) { return (acc[key] = obj[key], acc); }, {});
  679. }
  680. /**
  681. * Maps an array, or object to a property (by name)
  682. */
  683. function pluck(collection, propName) {
  684. return map(collection, prop(propName));
  685. }
  686. /** Filters an Array or an Object's properties based on a predicate */
  687. function filter(collection, callback) {
  688. var arr = isArray(collection), result = arr ? [] : {};
  689. var accept = arr ? function (x) { return result.push(x); } : function (x, key) { return result[key] = x; };
  690. forEach(collection, function (item, i) {
  691. if (callback(item, i))
  692. accept(item, i);
  693. });
  694. return result;
  695. }
  696. /** Finds an object from an array, or a property of an object, that matches a predicate */
  697. function find(collection, callback) {
  698. var result;
  699. forEach(collection, function (item, i) {
  700. if (result)
  701. return;
  702. if (callback(item, i))
  703. result = item;
  704. });
  705. return result;
  706. }
  707. /** Given an object, returns a new object, where each property is transformed by the callback function */
  708. var mapObj = map;
  709. /** Maps an array or object properties using a callback function */
  710. function map(collection, callback) {
  711. var result = isArray(collection) ? [] : {};
  712. forEach(collection, function (item, i) { return result[i] = callback(item, i); });
  713. return result;
  714. }
  715. /**
  716. * Given an object, return its enumerable property values
  717. *
  718. * @example
  719. * ```
  720. *
  721. * let foo = { a: 1, b: 2, c: 3 }
  722. * let vals = values(foo); // [ 1, 2, 3 ]
  723. * ```
  724. */
  725. var values = function (obj) {
  726. return Object.keys(obj).map(function (key) { return obj[key]; });
  727. };
  728. /**
  729. * Reduce function that returns true if all of the values are truthy.
  730. *
  731. * @example
  732. * ```
  733. *
  734. * let vals = [ 1, true, {}, "hello world"];
  735. * vals.reduce(allTrueR, true); // true
  736. *
  737. * vals.push(0);
  738. * vals.reduce(allTrueR, true); // false
  739. * ```
  740. */
  741. var allTrueR = function (memo, elem) { return memo && elem; };
  742. /**
  743. * Reduce function that returns true if any of the values are truthy.
  744. *
  745. * * @example
  746. * ```
  747. *
  748. * let vals = [ 0, null, undefined ];
  749. * vals.reduce(anyTrueR, true); // false
  750. *
  751. * vals.push("hello world");
  752. * vals.reduce(anyTrueR, true); // true
  753. * ```
  754. */
  755. var anyTrueR = function (memo, elem) { return memo || elem; };
  756. /**
  757. * Reduce function which un-nests a single level of arrays
  758. * @example
  759. * ```
  760. *
  761. * let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
  762. * input.reduce(unnestR, []) // [ "a", "b", "c", "d", [ "double, "nested" ] ]
  763. * ```
  764. */
  765. var unnestR = function (memo, elem) { return memo.concat(elem); };
  766. /**
  767. * Reduce function which recursively un-nests all arrays
  768. *
  769. * @example
  770. * ```
  771. *
  772. * let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
  773. * input.reduce(unnestR, []) // [ "a", "b", "c", "d", "double, "nested" ]
  774. * ```
  775. */
  776. var flattenR = function (memo, elem) {
  777. return isArray(elem) ? memo.concat(elem.reduce(flattenR, [])) : pushR(memo, elem);
  778. };
  779. /**
  780. * Reduce function that pushes an object to an array, then returns the array.
  781. * Mostly just for [[flattenR]] and [[uniqR]]
  782. */
  783. function pushR(arr, obj) {
  784. arr.push(obj);
  785. return arr;
  786. }
  787. /** Reduce function that filters out duplicates */
  788. var uniqR = function (acc, token) {
  789. return inArray(acc, token) ? acc : pushR(acc, token);
  790. };
  791. /**
  792. * Return a new array with a single level of arrays unnested.
  793. *
  794. * @example
  795. * ```
  796. *
  797. * let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
  798. * unnest(input) // [ "a", "b", "c", "d", [ "double, "nested" ] ]
  799. * ```
  800. */
  801. var unnest = function (arr) { return arr.reduce(unnestR, []); };
  802. /**
  803. * Return a completely flattened version of an array.
  804. *
  805. * @example
  806. * ```
  807. *
  808. * let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
  809. * flatten(input) // [ "a", "b", "c", "d", "double, "nested" ]
  810. * ```
  811. */
  812. var flatten = function (arr) { return arr.reduce(flattenR, []); };
  813. /**
  814. * Given a .filter Predicate, builds a .filter Predicate which throws an error if any elements do not pass.
  815. * @example
  816. * ```
  817. *
  818. * let isNumber = (obj) => typeof(obj) === 'number';
  819. * let allNumbers = [ 1, 2, 3, 4, 5 ];
  820. * allNumbers.filter(assertPredicate(isNumber)); //OK
  821. *
  822. * let oneString = [ 1, 2, 3, 4, "5" ];
  823. * oneString.filter(assertPredicate(isNumber, "Not all numbers")); // throws Error(""Not all numbers"");
  824. * ```
  825. */
  826. var assertPredicate = assertFn;
  827. /**
  828. * Given a .map function, builds a .map function which throws an error if any mapped elements do not pass a truthyness test.
  829. * @example
  830. * ```
  831. *
  832. * var data = { foo: 1, bar: 2 };
  833. *
  834. * let keys = [ 'foo', 'bar' ]
  835. * let values = keys.map(assertMap(key => data[key], "Key not found"));
  836. * // values is [1, 2]
  837. *
  838. * let keys = [ 'foo', 'bar', 'baz' ]
  839. * let values = keys.map(assertMap(key => data[key], "Key not found"));
  840. * // throws Error("Key not found")
  841. * ```
  842. */
  843. var assertMap = assertFn;
  844. function assertFn(predicateOrMap, errMsg) {
  845. if (errMsg === void 0) { errMsg = "assert failure"; }
  846. return function (obj) {
  847. var result = predicateOrMap(obj);
  848. if (!result) {
  849. throw new Error(isFunction(errMsg) ? errMsg(obj) : errMsg);
  850. }
  851. return result;
  852. };
  853. }
  854. /**
  855. * Like _.pairs: Given an object, returns an array of key/value pairs
  856. *
  857. * @example
  858. * ```
  859. *
  860. * pairs({ foo: "FOO", bar: "BAR }) // [ [ "foo", "FOO" ], [ "bar": "BAR" ] ]
  861. * ```
  862. */
  863. var pairs = function (obj) {
  864. return Object.keys(obj).map(function (key) { return [key, obj[key]]; });
  865. };
  866. /**
  867. * Given two or more parallel arrays, returns an array of tuples where
  868. * each tuple is composed of [ a[i], b[i], ... z[i] ]
  869. *
  870. * @example
  871. * ```
  872. *
  873. * let foo = [ 0, 2, 4, 6 ];
  874. * let bar = [ 1, 3, 5, 7 ];
  875. * let baz = [ 10, 30, 50, 70 ];
  876. * arrayTuples(foo, bar); // [ [0, 1], [2, 3], [4, 5], [6, 7] ]
  877. * arrayTuples(foo, bar, baz); // [ [0, 1, 10], [2, 3, 30], [4, 5, 50], [6, 7, 70] ]
  878. * ```
  879. */
  880. function arrayTuples() {
  881. var args = [];
  882. for (var _i = 0; _i < arguments.length; _i++) {
  883. args[_i] = arguments[_i];
  884. }
  885. if (args.length === 0)
  886. return [];
  887. var maxArrayLen = args.reduce(function (min, arr) { return Math.min(arr.length, min); }, 9007199254740991); // aka 2^53 − 1 aka Number.MAX_SAFE_INTEGER
  888. var i, result = [];
  889. for (i = 0; i < maxArrayLen; i++) {
  890. // This is a hot function
  891. // Unroll when there are 1-4 arguments
  892. switch (args.length) {
  893. case 1:
  894. result.push([args[0][i]]);
  895. break;
  896. case 2:
  897. result.push([args[0][i], args[1][i]]);
  898. break;
  899. case 3:
  900. result.push([args[0][i], args[1][i], args[2][i]]);
  901. break;
  902. case 4:
  903. result.push([args[0][i], args[1][i], args[2][i], args[3][i]]);
  904. break;
  905. default:
  906. result.push(args.map(function (array) { return array[i]; }));
  907. break;
  908. }
  909. }
  910. return result;
  911. }
  912. /**
  913. * Reduce function which builds an object from an array of [key, value] pairs.
  914. *
  915. * Each iteration sets the key/val pair on the memo object, then returns the memo for the next iteration.
  916. *
  917. * Each keyValueTuple should be an array with values [ key: string, value: any ]
  918. *
  919. * @example
  920. * ```
  921. *
  922. * var pairs = [ ["fookey", "fooval"], ["barkey", "barval"] ]
  923. *
  924. * var pairsToObj = pairs.reduce((memo, pair) => applyPairs(memo, pair), {})
  925. * // pairsToObj == { fookey: "fooval", barkey: "barval" }
  926. *
  927. * // Or, more simply:
  928. * var pairsToObj = pairs.reduce(applyPairs, {})
  929. * // pairsToObj == { fookey: "fooval", barkey: "barval" }
  930. * ```
  931. */
  932. function applyPairs(memo, keyValTuple) {
  933. var key, value;
  934. if (isArray(keyValTuple))
  935. key = keyValTuple[0], value = keyValTuple[1];
  936. if (!isString(key))
  937. throw new Error("invalid parameters to applyPairs");
  938. memo[key] = value;
  939. return memo;
  940. }
  941. /** Get the last element of an array */
  942. function tail(arr) {
  943. return arr.length && arr[arr.length - 1] || undefined;
  944. }
  945. /**
  946. * shallow copy from src to dest
  947. */
  948. function copy(src, dest) {
  949. if (dest)
  950. Object.keys(dest).forEach(function (key) { return delete dest[key]; });
  951. if (!dest)
  952. dest = {};
  953. return extend(dest, src);
  954. }
  955. /** Naive forEach implementation works with Objects or Arrays */
  956. function _forEach(obj, cb, _this) {
  957. if (isArray(obj))
  958. return obj.forEach(cb, _this);
  959. Object.keys(obj).forEach(function (key) { return cb(obj[key], key); });
  960. }
  961. function _extend(toObj) {
  962. for (var i = 1; i < arguments.length; i++) {
  963. var obj = arguments[i];
  964. if (!obj)
  965. continue;
  966. var keys = Object.keys(obj);
  967. for (var j = 0; j < keys.length; j++) {
  968. toObj[keys[j]] = obj[keys[j]];
  969. }
  970. }
  971. return toObj;
  972. }
  973. function _equals(o1, o2) {
  974. if (o1 === o2)
  975. return true;
  976. if (o1 === null || o2 === null)
  977. return false;
  978. if (o1 !== o1 && o2 !== o2)
  979. return true; // NaN === NaN
  980. var t1 = typeof o1, t2 = typeof o2;
  981. if (t1 !== t2 || t1 !== 'object')
  982. return false;
  983. var tup = [o1, o2];
  984. if (all(isArray)(tup))
  985. return _arraysEq(o1, o2);
  986. if (all(isDate)(tup))
  987. return o1.getTime() === o2.getTime();
  988. if (all(isRegExp)(tup))
  989. return o1.toString() === o2.toString();
  990. if (all(isFunction)(tup))
  991. return true; // meh
  992. var predicates = [isFunction, isArray, isDate, isRegExp];
  993. if (predicates.map(any).reduce(function (b, fn) { return b || !!fn(tup); }, false))
  994. return false;
  995. var key, keys = {};
  996. for (key in o1) {
  997. if (!_equals(o1[key], o2[key]))
  998. return false;
  999. keys[key] = true;
  1000. }
  1001. for (key in o2) {
  1002. if (!keys[key])
  1003. return false;
  1004. }
  1005. return true;
  1006. }
  1007. function _arraysEq(a1, a2) {
  1008. if (a1.length !== a2.length)
  1009. return false;
  1010. return arrayTuples(a1, a2).reduce(function (b, t) { return b && _equals(t[0], t[1]); }, true);
  1011. }
  1012. // issue #2676
  1013. var silenceUncaughtInPromise = function (promise) {
  1014. return promise.catch(function (e) { return 0; }) && promise;
  1015. };
  1016. var silentRejection = function (error) {
  1017. return silenceUncaughtInPromise(services.$q.reject(error));
  1018. };
  1019. /**
  1020. * @module common
  1021. */ /** for typedoc */
  1022. var Queue = /** @class */ (function () {
  1023. function Queue(_items, _limit) {
  1024. if (_items === void 0) { _items = []; }
  1025. if (_limit === void 0) { _limit = null; }
  1026. this._items = _items;
  1027. this._limit = _limit;
  1028. }
  1029. Queue.prototype.enqueue = function (item) {
  1030. var items = this._items;
  1031. items.push(item);
  1032. if (this._limit && items.length > this._limit)
  1033. items.shift();
  1034. return item;
  1035. };
  1036. Queue.prototype.dequeue = function () {
  1037. if (this.size())
  1038. return this._items.splice(0, 1)[0];
  1039. };
  1040. Queue.prototype.clear = function () {
  1041. var current = this._items;
  1042. this._items = [];
  1043. return current;
  1044. };
  1045. Queue.prototype.size = function () {
  1046. return this._items.length;
  1047. };
  1048. Queue.prototype.remove = function (item) {
  1049. var idx = this._items.indexOf(item);
  1050. return idx > -1 && this._items.splice(idx, 1)[0];
  1051. };
  1052. Queue.prototype.peekTail = function () {
  1053. return this._items[this._items.length - 1];
  1054. };
  1055. Queue.prototype.peekHead = function () {
  1056. if (this.size())
  1057. return this._items[0];
  1058. };
  1059. return Queue;
  1060. }());
  1061. /**
  1062. * @coreapi
  1063. * @module transition
  1064. */ /** for typedoc */
  1065. "use strict";
  1066. (function (RejectType) {
  1067. RejectType[RejectType["SUPERSEDED"] = 2] = "SUPERSEDED";
  1068. RejectType[RejectType["ABORTED"] = 3] = "ABORTED";
  1069. RejectType[RejectType["INVALID"] = 4] = "INVALID";
  1070. RejectType[RejectType["IGNORED"] = 5] = "IGNORED";
  1071. RejectType[RejectType["ERROR"] = 6] = "ERROR";
  1072. })(exports.RejectType || (exports.RejectType = {}));
  1073. /** @hidden */ var id = 0;
  1074. var Rejection = /** @class */ (function () {
  1075. function Rejection(type, message, detail) {
  1076. this.$id = id++;
  1077. this.type = type;
  1078. this.message = message;
  1079. this.detail = detail;
  1080. }
  1081. Rejection.prototype.toString = function () {
  1082. var detailString = function (d) {
  1083. return d && d.toString !== Object.prototype.toString ? d.toString() : stringify(d);
  1084. };
  1085. var detail = detailString(this.detail);
  1086. var _a = this, $id = _a.$id, type = _a.type, message = _a.message;
  1087. return "Transition Rejection($id: " + $id + " type: " + type + ", message: " + message + ", detail: " + detail + ")";
  1088. };
  1089. Rejection.prototype.toPromise = function () {
  1090. return extend(silentRejection(this), { _transitionRejection: this });
  1091. };
  1092. /** Returns true if the obj is a rejected promise created from the `asPromise` factory */
  1093. Rejection.isRejectionPromise = function (obj) {
  1094. return obj && (typeof obj.then === 'function') && is(Rejection)(obj._transitionRejection);
  1095. };
  1096. /** Returns a Rejection due to transition superseded */
  1097. Rejection.superseded = function (detail, options) {
  1098. var message = "The transition has been superseded by a different transition";
  1099. var rejection = new Rejection(exports.RejectType.SUPERSEDED, message, detail);
  1100. if (options && options.redirected) {
  1101. rejection.redirected = true;
  1102. }
  1103. return rejection;
  1104. };
  1105. /** Returns a Rejection due to redirected transition */
  1106. Rejection.redirected = function (detail) {
  1107. return Rejection.superseded(detail, { redirected: true });
  1108. };
  1109. /** Returns a Rejection due to invalid transition */
  1110. Rejection.invalid = function (detail) {
  1111. var message = "This transition is invalid";
  1112. return new Rejection(exports.RejectType.INVALID, message, detail);
  1113. };
  1114. /** Returns a Rejection due to ignored transition */
  1115. Rejection.ignored = function (detail) {
  1116. var message = "The transition was ignored";
  1117. return new Rejection(exports.RejectType.IGNORED, message, detail);
  1118. };
  1119. /** Returns a Rejection due to aborted transition */
  1120. Rejection.aborted = function (detail) {
  1121. var message = "The transition has been aborted";
  1122. return new Rejection(exports.RejectType.ABORTED, message, detail);
  1123. };
  1124. /** Returns a Rejection due to aborted transition */
  1125. Rejection.errored = function (detail) {
  1126. var message = "The transition errored";
  1127. return new Rejection(exports.RejectType.ERROR, message, detail);
  1128. };
  1129. /**
  1130. * Returns a Rejection
  1131. *
  1132. * Normalizes a value as a Rejection.
  1133. * If the value is already a Rejection, returns it.
  1134. * Otherwise, wraps and returns the value as a Rejection (Rejection type: ERROR).
  1135. *
  1136. * @returns `detail` if it is already a `Rejection`, else returns an ERROR Rejection.
  1137. */
  1138. Rejection.normalize = function (detail) {
  1139. return is(Rejection)(detail) ? detail : Rejection.errored(detail);
  1140. };
  1141. return Rejection;
  1142. }());
  1143. /**
  1144. * # Transition tracing (debug)
  1145. *
  1146. * Enable transition tracing to print transition information to the console,
  1147. * in order to help debug your application.
  1148. * Tracing logs detailed information about each Transition to your console.
  1149. *
  1150. * To enable tracing, import the [[Trace]] singleton and enable one or more categories.
  1151. *
  1152. * ### ES6
  1153. * ```js
  1154. * import {trace} from "ui-router-ng2"; // or "angular-ui-router"
  1155. * trace.enable(1, 5); // TRANSITION and VIEWCONFIG
  1156. * ```
  1157. *
  1158. * ### CJS
  1159. * ```js
  1160. * let trace = require("angular-ui-router").trace; // or "ui-router-ng2"
  1161. * trace.enable("TRANSITION", "VIEWCONFIG");
  1162. * ```
  1163. *
  1164. * ### Globals
  1165. * ```js
  1166. * let trace = window["angular-ui-router"].trace; // or "ui-router-ng2"
  1167. * trace.enable(); // Trace everything (very verbose)
  1168. * ```
  1169. *
  1170. * ### Angular 1:
  1171. * ```js
  1172. * app.run($trace => $trace.enable());
  1173. * ```
  1174. *
  1175. * @coreapi
  1176. * @module trace
  1177. */ /** for typedoc */
  1178. /** @hidden */
  1179. function uiViewString(uiview) {
  1180. if (!uiview)
  1181. return 'ui-view (defunct)';
  1182. var state = uiview.creationContext ? uiview.creationContext.name || '(root)' : '(none)';
  1183. return "[ui-view#" + uiview.id + " " + uiview.$type + ":" + uiview.fqn + " (" + uiview.name + "@" + state + ")]";
  1184. }
  1185. /** @hidden */
  1186. var viewConfigString = function (viewConfig) {
  1187. var view = viewConfig.viewDecl;
  1188. var state = view.$context.name || '(root)';
  1189. return "[View#" + viewConfig.$id + " from '" + state + "' state]: target ui-view: '" + view.$uiViewName + "@" + view.$uiViewContextAnchor + "'";
  1190. };
  1191. /** @hidden */
  1192. function normalizedCat(input) {
  1193. return isNumber(input) ? exports.Category[input] : exports.Category[exports.Category[input]];
  1194. }
  1195. /** @hidden */
  1196. var consoleLog = Function.prototype.bind.call(console.log, console);
  1197. /** @hidden */
  1198. var consoletable = isFunction(console.table) ? console.table.bind(console) : consoleLog.bind(console);
  1199. /**
  1200. * Trace categories Enum
  1201. *
  1202. * Enable or disable a category using [[Trace.enable]] or [[Trace.disable]]
  1203. *
  1204. * `trace.enable(Category.TRANSITION)`
  1205. *
  1206. * These can also be provided using a matching string, or position ordinal
  1207. *
  1208. * `trace.enable("TRANSITION")`
  1209. *
  1210. * `trace.enable(1)`
  1211. */
  1212. (function (Category) {
  1213. Category[Category["RESOLVE"] = 0] = "RESOLVE";
  1214. Category[Category["TRANSITION"] = 1] = "TRANSITION";
  1215. Category[Category["HOOK"] = 2] = "HOOK";
  1216. Category[Category["UIVIEW"] = 3] = "UIVIEW";
  1217. Category[Category["VIEWCONFIG"] = 4] = "VIEWCONFIG";
  1218. })(exports.Category || (exports.Category = {}));
  1219. /** @hidden */ var _tid = parse("$id");
  1220. /** @hidden */ var _rid = parse("router.$id");
  1221. /** @hidden */ var transLbl = function (trans) { return "Transition #" + _tid(trans) + "-" + _rid(trans); };
  1222. /**
  1223. * Prints UI-Router Transition trace information to the console.
  1224. */
  1225. var Trace = /** @class */ (function () {
  1226. /** @hidden */
  1227. function Trace() {
  1228. /** @hidden */
  1229. this._enabled = {};
  1230. this.approximateDigests = 0;
  1231. }
  1232. /** @hidden */
  1233. Trace.prototype._set = function (enabled, categories) {
  1234. var _this = this;
  1235. if (!categories.length) {
  1236. categories = Object.keys(exports.Category)
  1237. .map(function (k) { return parseInt(k, 10); })
  1238. .filter(function (k) { return !isNaN(k); })
  1239. .map(function (key) { return exports.Category[key]; });
  1240. }
  1241. categories.map(normalizedCat).forEach(function (category) { return _this._enabled[category] = enabled; });
  1242. };
  1243. Trace.prototype.enable = function () {
  1244. var categories = [];
  1245. for (var _i = 0; _i < arguments.length; _i++) {
  1246. categories[_i] = arguments[_i];
  1247. }
  1248. this._set(true, categories);
  1249. };
  1250. Trace.prototype.disable = function () {
  1251. var categories = [];
  1252. for (var _i = 0; _i < arguments.length; _i++) {
  1253. categories[_i] = arguments[_i];
  1254. }
  1255. this._set(false, categories);
  1256. };
  1257. /**
  1258. * Retrieves the enabled stateus of a [[Category]]
  1259. *
  1260. * ```js
  1261. * trace.enabled("VIEWCONFIG"); // true or false
  1262. * ```
  1263. *
  1264. * @returns boolean true if the category is enabled
  1265. */
  1266. Trace.prototype.enabled = function (category) {
  1267. return !!this._enabled[normalizedCat(category)];
  1268. };
  1269. /** @internalapi called by ui-router code */
  1270. Trace.prototype.traceTransitionStart = function (trans) {
  1271. if (!this.enabled(exports.Category.TRANSITION))
  1272. return;
  1273. console.log(transLbl(trans) + ": Started -> " + stringify(trans));
  1274. };
  1275. /** @internalapi called by ui-router code */
  1276. Trace.prototype.traceTransitionIgnored = function (trans) {
  1277. if (!this.enabled(exports.Category.TRANSITION))
  1278. return;
  1279. console.log(transLbl(trans) + ": Ignored <> " + stringify(trans));
  1280. };
  1281. /** @internalapi called by ui-router code */
  1282. Trace.prototype.traceHookInvocation = function (step, trans, options) {
  1283. if (!this.enabled(exports.Category.HOOK))
  1284. return;
  1285. var event = parse("traceData.hookType")(options) || "internal", context = parse("traceData.context.state.name")(options) || parse("traceData.context")(options) || "unknown", name = functionToString(step.registeredHook.callback);
  1286. console.log(transLbl(trans) + ": Hook -> " + event + " context: " + context + ", " + maxLength(200, name));
  1287. };
  1288. /** @internalapi called by ui-router code */
  1289. Trace.prototype.traceHookResult = function (hookResult, trans, transitionOptions) {
  1290. if (!this.enabled(exports.Category.HOOK))
  1291. return;
  1292. console.log(transLbl(trans) + ": <- Hook returned: " + maxLength(200, stringify(hookResult)));
  1293. };
  1294. /** @internalapi called by ui-router code */
  1295. Trace.prototype.traceResolvePath = function (path, when, trans) {
  1296. if (!this.enabled(exports.Category.RESOLVE))
  1297. return;
  1298. console.log(transLbl(trans) + ": Resolving " + path + " (" + when + ")");
  1299. };
  1300. /** @internalapi called by ui-router code */
  1301. Trace.prototype.traceResolvableResolved = function (resolvable, trans) {
  1302. if (!this.enabled(exports.Category.RESOLVE))
  1303. return;
  1304. console.log(transLbl(trans) + ": <- Resolved " + resolvable + " to: " + maxLength(200, stringify(resolvable.data)));
  1305. };
  1306. /** @internalapi called by ui-router code */
  1307. Trace.prototype.traceError = function (reason, trans) {
  1308. if (!this.enabled(exports.Category.TRANSITION))
  1309. return;
  1310. console.log(transLbl(trans) + ": <- Rejected " + stringify(trans) + ", reason: " + reason);
  1311. };
  1312. /** @internalapi called by ui-router code */
  1313. Trace.prototype.traceSuccess = function (finalState, trans) {
  1314. if (!this.enabled(exports.Category.TRANSITION))
  1315. return;
  1316. console.log(transLbl(trans) + ": <- Success " + stringify(trans) + ", final state: " + finalState.name);
  1317. };
  1318. /** @internalapi called by ui-router code */
  1319. Trace.prototype.traceUIViewEvent = function (event, viewData, extra) {
  1320. if (extra === void 0) { extra = ""; }
  1321. if (!this.enabled(exports.Category.UIVIEW))
  1322. return;
  1323. console.log("ui-view: " + padString(30, event) + " " + uiViewString(viewData) + extra);
  1324. };
  1325. /** @internalapi called by ui-router code */
  1326. Trace.prototype.traceUIViewConfigUpdated = function (viewData, context) {
  1327. if (!this.enabled(exports.Category.UIVIEW))
  1328. return;
  1329. this.traceUIViewEvent("Updating", viewData, " with ViewConfig from context='" + context + "'");
  1330. };
  1331. /** @internalapi called by ui-router code */
  1332. Trace.prototype.traceUIViewFill = function (viewData, html) {
  1333. if (!this.enabled(exports.Category.UIVIEW))
  1334. return;
  1335. this.traceUIViewEvent("Fill", viewData, " with: " + maxLength(200, html));
  1336. };
  1337. /** @internalapi called by ui-router code */
  1338. Trace.prototype.traceViewSync = function (pairs) {
  1339. if (!this.enabled(exports.Category.VIEWCONFIG))
  1340. return;
  1341. var mapping = pairs.map(function (_a) {
  1342. var uiViewData = _a[0], config = _a[1];
  1343. var uiView = uiViewData.$type + ":" + uiViewData.fqn;
  1344. var view = config && config.viewDecl.$context.name + ": " + config.viewDecl.$name + " (" + config.viewDecl.$type + ")";
  1345. return { 'ui-view fqn': uiView, 'state: view name': view };
  1346. }).sort(function (a, b) { return a['ui-view fqn'].localeCompare(b['ui-view fqn']); });
  1347. consoletable(mapping);
  1348. };
  1349. /** @internalapi called by ui-router code */
  1350. Trace.prototype.traceViewServiceEvent = function (event, viewConfig) {
  1351. if (!this.enabled(exports.Category.VIEWCONFIG))
  1352. return;
  1353. console.log("VIEWCONFIG: " + event + " " + viewConfigString(viewConfig));
  1354. };
  1355. /** @internalapi called by ui-router code */
  1356. Trace.prototype.traceViewServiceUIViewEvent = function (event, viewData) {
  1357. if (!this.enabled(exports.Category.VIEWCONFIG))
  1358. return;
  1359. console.log("VIEWCONFIG: " + event + " " + uiViewString(viewData));
  1360. };
  1361. return Trace;
  1362. }());
  1363. /**
  1364. * The [[Trace]] singleton
  1365. *
  1366. * #### Example:
  1367. * ```js
  1368. * import {trace} from "angular-ui-router";
  1369. * trace.enable(1, 5);
  1370. * ```
  1371. */
  1372. var trace = new Trace();
  1373. (function (TransitionHookPhase) {
  1374. TransitionHookPhase[TransitionHookPhase["CREATE"] = 0] = "CREATE";
  1375. TransitionHookPhase[TransitionHookPhase["BEFORE"] = 1] = "BEFORE";
  1376. TransitionHookPhase[TransitionHookPhase["RUN"] = 2] = "RUN";
  1377. TransitionHookPhase[TransitionHookPhase["SUCCESS"] = 3] = "SUCCESS";
  1378. TransitionHookPhase[TransitionHookPhase["ERROR"] = 4] = "ERROR";
  1379. })(exports.TransitionHookPhase || (exports.TransitionHookPhase = {}));
  1380. (function (TransitionHookScope) {
  1381. TransitionHookScope[TransitionHookScope["TRANSITION"] = 0] = "TRANSITION";
  1382. TransitionHookScope[TransitionHookScope["STATE"] = 1] = "STATE";
  1383. })(exports.TransitionHookScope || (exports.TransitionHookScope = {}));
  1384. /**
  1385. * @coreapi
  1386. * @module state
  1387. */ /** for typedoc */
  1388. /**
  1389. * Encapsulate the target (destination) state/params/options of a [[Transition]].
  1390. *
  1391. * This class is frequently used to redirect a transition to a new destination.
  1392. *
  1393. * See:
  1394. *
  1395. * - [[HookResult]]
  1396. * - [[TransitionHookFn]]
  1397. * - [[TransitionService.onStart]]
  1398. *
  1399. * To create a `TargetState`, use [[StateService.target]].
  1400. *
  1401. * ---
  1402. *
  1403. * This class wraps:
  1404. *
  1405. * 1) an identifier for a state
  1406. * 2) a set of parameters
  1407. * 3) and transition options
  1408. * 4) the registered state object (the [[StateDeclaration]])
  1409. *
  1410. * Many UI-Router APIs such as [[StateService.go]] take a [[StateOrName]] argument which can
  1411. * either be a *state object* (a [[StateDeclaration]] or [[StateObject]]) or a *state name* (a string).
  1412. * The `TargetState` class normalizes those options.
  1413. *
  1414. * A `TargetState` may be valid (the state being targeted exists in the registry)
  1415. * or invalid (the state being targeted is not registered).
  1416. */
  1417. var TargetState = /** @class */ (function () {
  1418. /**
  1419. * The TargetState constructor
  1420. *
  1421. * Note: Do not construct a `TargetState` manually.
  1422. * To create a `TargetState`, use the [[StateService.target]] factory method.
  1423. *
  1424. * @param _stateRegistry The StateRegistry to use to look up the _definition
  1425. * @param _identifier An identifier for a state.
  1426. * Either a fully-qualified state name, or the object used to define the state.
  1427. * @param _params Parameters for the target state
  1428. * @param _options Transition options.
  1429. *
  1430. * @internalapi
  1431. */
  1432. function TargetState(_stateRegistry, _identifier, _params, _options) {
  1433. this._stateRegistry = _stateRegistry;
  1434. this._identifier = _identifier;
  1435. this._identifier = _identifier;
  1436. this._params = extend({}, _params || {});
  1437. this._options = extend({}, _options || {});
  1438. this._definition = _stateRegistry.matcher.find(_identifier, this._options.relative);
  1439. }
  1440. /** The name of the state this object targets */
  1441. TargetState.prototype.name = function () {
  1442. return this._definition && this._definition.name || this._identifier;
  1443. };
  1444. /** The identifier used when creating this TargetState */
  1445. TargetState.prototype.identifier = function () {
  1446. return this._identifier;
  1447. };
  1448. /** The target parameter values */
  1449. TargetState.prototype.params = function () {
  1450. return this._params;
  1451. };
  1452. /** The internal state object (if it was found) */
  1453. TargetState.prototype.$state = function () {
  1454. return this._definition;
  1455. };
  1456. /** The internal state declaration (if it was found) */
  1457. TargetState.prototype.state = function () {
  1458. return this._definition && this._definition.self;
  1459. };
  1460. /** The target options */
  1461. TargetState.prototype.options = function () {
  1462. return this._options;
  1463. };
  1464. /** True if the target state was found */
  1465. TargetState.prototype.exists = function () {
  1466. return !!(this._definition && this._definition.self);
  1467. };
  1468. /** True if the object is valid */
  1469. TargetState.prototype.valid = function () {
  1470. return !this.error();
  1471. };
  1472. /** If the object is invalid, returns the reason why */
  1473. TargetState.prototype.error = function () {
  1474. var base = this.options().relative;
  1475. if (!this._definition && !!base) {
  1476. var stateName = base.name ? base.name : base;
  1477. return "Could not resolve '" + this.name() + "' from state '" + stateName + "'";
  1478. }
  1479. if (!this._definition)
  1480. return "No such state '" + this.name() + "'";
  1481. if (!this._definition.self)
  1482. return "State '" + this.name() + "' has an invalid definition";
  1483. };
  1484. TargetState.prototype.toString = function () {
  1485. return "'" + this.name() + "'" + stringify(this.params());
  1486. };
  1487. /**
  1488. * Returns a copy of this TargetState which targets a different state.
  1489. * The new TargetState has the same parameter values and transition options.
  1490. *
  1491. * @param state The new state that should be targeted
  1492. */
  1493. TargetState.prototype.withState = function (state) {
  1494. return new TargetState(this._stateRegistry, state, this._params, this._options);
  1495. };
  1496. /**
  1497. * Returns a copy of this TargetState, using the specified parameter values.
  1498. *
  1499. * @param params the new parameter values to use
  1500. * @param replace When false (default) the new parameter values will be merged with the current values.
  1501. * When true the parameter values will be used instead of the current values.
  1502. */
  1503. TargetState.prototype.withParams = function (params, replace) {
  1504. if (replace === void 0) { replace = false; }
  1505. var newParams = replace ? params : extend({}, this._params, params);
  1506. return new TargetState(this._stateRegistry, this._identifier, newParams, this._options);
  1507. };
  1508. /**
  1509. * Returns a copy of this TargetState, using the specified Transition Options.
  1510. *
  1511. * @param options the new options to use
  1512. * @param replace When false (default) the new options will be merged with the current options.
  1513. * When true the options will be used instead of the current options.
  1514. */
  1515. TargetState.prototype.withOptions = function (options, replace) {
  1516. if (replace === void 0) { replace = false; }
  1517. var newOpts = replace ? options : extend({}, this._options, options);
  1518. return new TargetState(this._stateRegistry, this._identifier, this._params, newOpts);
  1519. };
  1520. /** Returns true if the object has a state property that might be a state or state name */
  1521. TargetState.isDef = function (obj) {
  1522. return obj && obj.state && (isString(obj.state) || isString(obj.state.name));
  1523. };
  1524. return TargetState;
  1525. }());
  1526. /**
  1527. * @coreapi
  1528. * @module transition
  1529. */
  1530. /** for typedoc */
  1531. var defaultOptions = {
  1532. current: noop$1,
  1533. transition: null,
  1534. traceData: {},
  1535. bind: null,
  1536. };
  1537. /** @hidden */
  1538. var TransitionHook = /** @class */ (function () {
  1539. function TransitionHook(transition, stateContext, registeredHook, options) {
  1540. var _this = this;
  1541. this.transition = transition;
  1542. this.stateContext = stateContext;
  1543. this.registeredHook = registeredHook;
  1544. this.options = options;
  1545. this.isSuperseded = function () {
  1546. return _this.type.hookPhase === exports.TransitionHookPhase.RUN && !_this.options.transition.isActive();
  1547. };
  1548. this.options = defaults(options, defaultOptions);
  1549. this.type = registeredHook.eventType;
  1550. }
  1551. TransitionHook.prototype.logError = function (err) {
  1552. this.transition.router.stateService.defaultErrorHandler()(err);
  1553. };
  1554. TransitionHook.prototype.invokeHook = function () {
  1555. var _this = this;
  1556. var hook = this.registeredHook;
  1557. if (hook._deregistered)
  1558. return;
  1559. var notCurrent = this.getNotCurrentRejection();
  1560. if (notCurrent)
  1561. return notCurrent;
  1562. var options = this.options;
  1563. trace.traceHookInvocation(this, this.transition, options);
  1564. var invokeCallback = function () {
  1565. return hook.callback.call(options.bind, _this.transition, _this.stateContext);
  1566. };
  1567. var normalizeErr = function (err) {
  1568. return Rejection.normalize(err).toPromise();
  1569. };
  1570. var handleError = function (err) {
  1571. return hook.eventType.getErrorHandler(_this)(err);
  1572. };
  1573. var handleResult = function (result) {
  1574. return hook.eventType.getResultHandler(_this)(result);
  1575. };
  1576. try {
  1577. var result = invokeCallback();
  1578. if (!this.type.synchronous && isPromise(result)) {
  1579. return result.catch(normalizeErr)
  1580. .then(handleResult, handleError);
  1581. }
  1582. else {
  1583. return handleResult(result);
  1584. }
  1585. }
  1586. catch (err) {
  1587. // If callback throws (synchronously)
  1588. return handleError(Rejection.normalize(err));
  1589. }
  1590. finally {
  1591. if (hook.invokeLimit && ++hook.invokeCount >= hook.invokeLimit) {
  1592. hook.deregister();
  1593. }
  1594. }
  1595. };
  1596. /**
  1597. * This method handles the return value of a Transition Hook.
  1598. *
  1599. * A hook can return false (cancel), a TargetState (redirect),
  1600. * or a promise (which may later resolve to false or a redirect)
  1601. *
  1602. * This also handles "transition superseded" -- when a new transition
  1603. * was started while the hook was still running
  1604. */
  1605. TransitionHook.prototype.handleHookResult = function (result) {
  1606. var _this = this;
  1607. var notCurrent = this.getNotCurrentRejection();
  1608. if (notCurrent)
  1609. return notCurrent;
  1610. // Hook returned a promise
  1611. if (isPromise(result)) {
  1612. // Wait for the promise, then reprocess with the resulting value
  1613. return result.then(function (val$$1) { return _this.handleHookResult(val$$1); });
  1614. }
  1615. trace.traceHookResult(result, this.transition, this.options);
  1616. // Hook returned false
  1617. if (result === false) {
  1618. // Abort this Transition
  1619. return Rejection.aborted("Hook aborted transition").toPromise();
  1620. }
  1621. var isTargetState = is(TargetState);
  1622. // hook returned a TargetState
  1623. if (isTargetState(result)) {
  1624. // Halt the current Transition and redirect (a new Transition) to the TargetState.
  1625. return Rejection.redirected(result).toPromise();
  1626. }
  1627. };
  1628. /**
  1629. * Return a Rejection promise if the transition is no longer current due
  1630. * to a stopped router (disposed), or a new transition has started and superseded this one.
  1631. */
  1632. TransitionHook.prototype.getNotCurrentRejection = function () {
  1633. var router = this.transition.router;
  1634. // The router is stopped
  1635. if (router._disposed) {
  1636. return Rejection.aborted("UIRouter instance #" + router.$id + " has been stopped (disposed)").toPromise();
  1637. }
  1638. if (this.transition._aborted) {
  1639. return Rejection.aborted().toPromise();
  1640. }
  1641. // This transition is no longer current.
  1642. // Another transition started while this hook was still running.
  1643. if (this.isSuperseded()) {
  1644. // Abort this transition
  1645. return Rejection.superseded(this.options.current()).toPromise();
  1646. }
  1647. };
  1648. TransitionHook.prototype.toString = function () {
  1649. var _a = this, options = _a.options, registeredHook = _a.registeredHook;
  1650. var event = parse("traceData.hookType")(options) || "internal", context = parse("traceData.context.state.name")(options) || parse("traceData.context")(options) || "unknown", name = fnToString(registeredHook.callback);
  1651. return event + " context: " + context + ", " + maxLength(200, name);
  1652. };
  1653. /**
  1654. * Chains together an array of TransitionHooks.
  1655. *
  1656. * Given a list of [[TransitionHook]] objects, chains them together.
  1657. * Each hook is invoked after the previous one completes.
  1658. *
  1659. * #### Example:
  1660. * ```js
  1661. * var hooks: TransitionHook[] = getHooks();
  1662. * let promise: Promise<any> = TransitionHook.chain(hooks);
  1663. *
  1664. * promise.then(handleSuccess, handleError);
  1665. * ```
  1666. *
  1667. * @param hooks the list of hooks to chain together
  1668. * @param waitFor if provided, the chain is `.then()`'ed off this promise
  1669. * @returns a `Promise` for sequentially invoking the hooks (in order)
  1670. */
  1671. TransitionHook.chain = function (hooks, waitFor) {
  1672. // Chain the next hook off the previous
  1673. var createHookChainR = function (prev, nextHook) {
  1674. return prev.then(function () { return nextHook.invokeHook(); });
  1675. };
  1676. return hooks.reduce(createHookChainR, waitFor || services.$q.when());
  1677. };
  1678. /**
  1679. * Invokes all the provided TransitionHooks, in order.
  1680. * Each hook's return value is checked.
  1681. * If any hook returns a promise, then the rest of the hooks are chained off that promise, and the promise is returned.
  1682. * If no hook returns a promise, then all hooks are processed synchronously.
  1683. *
  1684. * @param hooks the list of TransitionHooks to invoke
  1685. * @param doneCallback a callback that is invoked after all the hooks have successfully completed
  1686. *
  1687. * @returns a promise for the async result, or the result of the callback
  1688. */
  1689. TransitionHook.invokeHooks = function (hooks, doneCallback) {
  1690. for (var idx = 0; idx < hooks.length; idx++) {
  1691. var hookResult = hooks[idx].invokeHook();
  1692. if (isPromise(hookResult)) {
  1693. var remainingHooks = hooks.slice(idx + 1);
  1694. return TransitionHook.chain(remainingHooks, hookResult)
  1695. .then(doneCallback);
  1696. }
  1697. }
  1698. return doneCallback();
  1699. };
  1700. /**
  1701. * Run all TransitionHooks, ignoring their return value.
  1702. */
  1703. TransitionHook.runAllHooks = function (hooks) {
  1704. hooks.forEach(function (hook) { return hook.invokeHook(); });
  1705. };
  1706. /**
  1707. * These GetResultHandler(s) are used by [[invokeHook]] below
  1708. * Each HookType chooses a GetResultHandler (See: [[TransitionService._defineCoreEvents]])
  1709. */
  1710. TransitionHook.HANDLE_RESULT = function (hook) { return function (result) {
  1711. return hook.handleHookResult(result);
  1712. }; };
  1713. /**
  1714. * If the result is a promise rejection, log it.
  1715. * Otherwise, ignore the result.
  1716. */
  1717. TransitionHook.LOG_REJECTED_RESULT = function (hook) { return function (result) {
  1718. isPromise(result) && result.catch(function (err) {
  1719. return hook.logError(Rejection.normalize(err));
  1720. });
  1721. return undefined;
  1722. }; };
  1723. /**
  1724. * These GetErrorHandler(s) are used by [[invokeHook]] below
  1725. * Each HookType chooses a GetErrorHandler (See: [[TransitionService._defineCoreEvents]])
  1726. */
  1727. TransitionHook.LOG_ERROR = function (hook) { return function (error) {
  1728. return hook.logError(error);
  1729. }; };
  1730. TransitionHook.REJECT_ERROR = function (hook) { return function (error) {
  1731. return silentRejection(error);
  1732. }; };
  1733. TransitionHook.THROW_ERROR = function (hook) { return function (error) {
  1734. throw error;
  1735. }; };
  1736. return TransitionHook;
  1737. }());
  1738. /**
  1739. * @coreapi
  1740. * @module transition
  1741. */ /** for typedoc */
  1742. /**
  1743. * Determines if the given state matches the matchCriteria
  1744. *
  1745. * @hidden
  1746. *
  1747. * @param state a State Object to test against
  1748. * @param criterion
  1749. * - If a string, matchState uses the string as a glob-matcher against the state name
  1750. * - If an array (of strings), matchState uses each string in the array as a glob-matchers against the state name
  1751. * and returns a positive match if any of the globs match.
  1752. * - If a function, matchState calls the function with the state and returns true if the function's result is truthy.
  1753. * @returns {boolean}
  1754. */
  1755. function matchState(state, criterion) {
  1756. var toMatch = isString(criterion) ? [criterion] : criterion;
  1757. function matchGlobs(_state) {
  1758. var globStrings = toMatch;
  1759. for (var i = 0; i < globStrings.length; i++) {
  1760. var glob = new Glob(globStrings[i]);
  1761. if ((glob && glob.matches(_state.name)) || (!glob && globStrings[i] === _state.name)) {
  1762. return true;
  1763. }
  1764. }
  1765. return false;
  1766. }
  1767. var matchFn = (isFunction(toMatch) ? toMatch : matchGlobs);
  1768. return !!matchFn(state);
  1769. }
  1770. /**
  1771. * @internalapi
  1772. * The registration data for a registered transition hook
  1773. */
  1774. var RegisteredHook = /** @class */ (function () {
  1775. function RegisteredHook(tranSvc, eventType, callback, matchCriteria, removeHookFromRegistry, options) {
  1776. if (options === void 0) { options = {}; }
  1777. this.tranSvc = tranSvc;
  1778. this.eventType = eventType;
  1779. this.callback = callback;
  1780. this.matchCriteria = matchCriteria;
  1781. this.removeHookFromRegistry = removeHookFromRegistry;
  1782. this.invokeCount = 0;
  1783. this._deregistered = false;
  1784. this.priority = options.priority || 0;
  1785. this.bind = options.bind || null;
  1786. this.invokeLimit = options.invokeLimit;
  1787. }
  1788. /**
  1789. * Gets the matching [[PathNode]]s
  1790. *
  1791. * Given an array of [[PathNode]]s, and a [[HookMatchCriterion]], returns an array containing
  1792. * the [[PathNode]]s that the criteria matches, or `null` if there were no matching nodes.
  1793. *
  1794. * Returning `null` is significant to distinguish between the default
  1795. * "match-all criterion value" of `true` compared to a `() => true` function,
  1796. * when the nodes is an empty array.
  1797. *
  1798. * This is useful to allow a transition match criteria of `entering: true`
  1799. * to still match a transition, even when `entering === []`. Contrast that
  1800. * with `entering: (state) => true` which only matches when a state is actually
  1801. * being entered.
  1802. */
  1803. RegisteredHook.prototype._matchingNodes = function (nodes, criterion) {
  1804. if (criterion === true)
  1805. return nodes;
  1806. var matching = nodes.filter(function (node) { return matchState(node.state, criterion); });
  1807. return matching.length ? matching : null;
  1808. };
  1809. /**
  1810. * Gets the default match criteria (all `true`)
  1811. *
  1812. * Returns an object which has all the criteria match paths as keys and `true` as values, i.e.:
  1813. *
  1814. * ```js
  1815. * {
  1816. * to: true,
  1817. * from: true,
  1818. * entering: true,
  1819. * exiting: true,
  1820. * retained: true,
  1821. * }
  1822. */
  1823. RegisteredHook.prototype._getDefaultMatchCriteria = function () {
  1824. return map(this.tranSvc._pluginapi._getPathTypes(), function () { return true; });
  1825. };
  1826. /**
  1827. * Gets matching nodes as [[IMatchingNodes]]
  1828. *
  1829. * Create a IMatchingNodes object from the TransitionHookTypes that is roughly equivalent to:
  1830. *
  1831. * ```js
  1832. * let matches: IMatchingNodes = {
  1833. * to: _matchingNodes([tail(treeChanges.to)], mc.to),
  1834. * from: _matchingNodes([tail(treeChanges.from)], mc.from),
  1835. * exiting: _matchingNodes(treeChanges.exiting, mc.exiting),
  1836. * retained: _matchingNodes(treeChanges.retained, mc.retained),
  1837. * entering: _matchingNodes(treeChanges.entering, mc.entering),
  1838. * };
  1839. * ```
  1840. */
  1841. RegisteredHook.prototype._getMatchingNodes = function (treeChanges) {
  1842. var _this = this;
  1843. var criteria = extend(this._getDefaultMatchCriteria(), this.matchCriteria);
  1844. var paths = values(this.tranSvc._pluginapi._getPathTypes());
  1845. return paths.reduce(function (mn, pathtype) {
  1846. // STATE scope criteria matches against every node in the path.
  1847. // TRANSITION scope criteria matches against only the last node in the path
  1848. var isStateHook = pathtype.scope === exports.TransitionHookScope.STATE;
  1849. var path = treeChanges[pathtype.name] || [];
  1850. var nodes = isStateHook ? path : [tail(path)];
  1851. mn[pathtype.name] = _this._matchingNodes(nodes, criteria[pathtype.name]);
  1852. return mn;
  1853. }, {});
  1854. };
  1855. /**
  1856. * Determines if this hook's [[matchCriteria]] match the given [[TreeChanges]]
  1857. *
  1858. * @returns an IMatchingNodes object, or null. If an IMatchingNodes object is returned, its values
  1859. * are the matching [[PathNode]]s for each [[HookMatchCriterion]] (to, from, exiting, retained, entering)
  1860. */
  1861. RegisteredHook.prototype.matches = function (treeChanges) {
  1862. var matches = this._getMatchingNodes(treeChanges);
  1863. // Check if all the criteria matched the TreeChanges object
  1864. var allMatched = values(matches).every(identity);
  1865. return allMatched ? matches : null;
  1866. };
  1867. RegisteredHook.prototype.deregister = function () {
  1868. this.removeHookFromRegistry(this);
  1869. this._deregistered = true;
  1870. };
  1871. return RegisteredHook;
  1872. }());
  1873. /** @hidden Return a registration function of the requested type. */
  1874. function makeEvent(registry, transitionService, eventType) {
  1875. // Create the object which holds the registered transition hooks.
  1876. var _registeredHooks = registry._registeredHooks = (registry._registeredHooks || {});
  1877. var hooks = _registeredHooks[eventType.name] = [];
  1878. var removeHookFn = removeFrom(hooks);
  1879. // Create hook registration function on the IHookRegistry for the event
  1880. registry[eventType.name] = hookRegistrationFn;
  1881. function hookRegistrationFn(matchObject, callback, options) {
  1882. if (options === void 0) { options = {}; }
  1883. var registeredHook = new RegisteredHook(transitionService, eventType, callback, matchObject, removeHookFn, options);
  1884. hooks.push(registeredHook);
  1885. return registeredHook.deregister.bind(registeredHook);
  1886. }
  1887. return hookRegistrationFn;
  1888. }
  1889. /**
  1890. * @coreapi
  1891. * @module transition
  1892. */ /** for typedoc */
  1893. /**
  1894. * This class returns applicable TransitionHooks for a specific Transition instance.
  1895. *
  1896. * Hooks ([[RegisteredHook]]) may be registered globally, e.g., $transitions.onEnter(...), or locally, e.g.
  1897. * myTransition.onEnter(...). The HookBuilder finds matching RegisteredHooks (where the match criteria is
  1898. * determined by the type of hook)
  1899. *
  1900. * The HookBuilder also converts RegisteredHooks objects to TransitionHook objects, which are used to run a Transition.
  1901. *
  1902. * The HookBuilder constructor is given the $transitions service and a Transition instance. Thus, a HookBuilder
  1903. * instance may only be used for one specific Transition object. (side note: the _treeChanges accessor is private
  1904. * in the Transition class, so we must also provide the Transition's _treeChanges)
  1905. *
  1906. */
  1907. var HookBuilder = /** @class */ (function () {
  1908. function HookBuilder(transition) {
  1909. this.transition = transition;
  1910. }
  1911. HookBuilder.prototype.buildHooksForPhase = function (phase) {
  1912. var _this = this;
  1913. var $transitions = this.transition.router.transitionService;
  1914. return $transitions._pluginapi._getEvents(phase)
  1915. .map(function (type) { return _this.buildHooks(type); })
  1916. .reduce(unnestR, [])
  1917. .filter(identity);
  1918. };
  1919. /**
  1920. * Returns an array of newly built TransitionHook objects.
  1921. *
  1922. * - Finds all RegisteredHooks registered for the given `hookType` which matched the transition's [[TreeChanges]].
  1923. * - Finds [[PathNode]] (or `PathNode[]`) to use as the TransitionHook context(s)
  1924. * - For each of the [[PathNode]]s, creates a TransitionHook
  1925. *
  1926. * @param hookType the type of the hook registration function, e.g., 'onEnter', 'onFinish'.
  1927. */
  1928. HookBuilder.prototype.buildHooks = function (hookType) {
  1929. var transition = this.transition;
  1930. var treeChanges = transition.treeChanges();
  1931. // Find all the matching registered hooks for a given hook type
  1932. var matchingHooks = this.getMatchingHooks(hookType, treeChanges);
  1933. if (!matchingHooks)
  1934. return [];
  1935. var baseHookOptions = {
  1936. transition: transition,
  1937. current: transition.options().current
  1938. };
  1939. var makeTransitionHooks = function (hook) {
  1940. // Fetch the Nodes that caused this hook to match.
  1941. var matches = hook.matches(treeChanges);
  1942. // Select the PathNode[] that will be used as TransitionHook context objects
  1943. var matchingNodes = matches[hookType.criteriaMatchPath.name];
  1944. // Return an array of HookTuples
  1945. return matchingNodes.map(function (node) {
  1946. var _options = extend({
  1947. bind: hook.bind,
  1948. traceData: { hookType: hookType.name, context: node }
  1949. }, baseHookOptions);
  1950. var state = hookType.criteriaMatchPath.scope === exports.TransitionHookScope.STATE ? node.state.self : null;
  1951. var transitionHook = new TransitionHook(transition, state, hook, _options);
  1952. return { hook: hook, node: node, transitionHook: transitionHook };
  1953. });
  1954. };
  1955. return matchingHooks.map(makeTransitionHooks)
  1956. .reduce(unnestR, [])
  1957. .sort(tupleSort(hookType.reverseSort))
  1958. .map(function (tuple) { return tuple.transitionHook; });
  1959. };
  1960. /**
  1961. * Finds all RegisteredHooks from:
  1962. * - The Transition object instance hook registry
  1963. * - The TransitionService ($transitions) global hook registry
  1964. *
  1965. * which matched:
  1966. * - the eventType
  1967. * - the matchCriteria (to, from, exiting, retained, entering)
  1968. *
  1969. * @returns an array of matched [[RegisteredHook]]s
  1970. */
  1971. HookBuilder.prototype.getMatchingHooks = function (hookType, treeChanges) {
  1972. var isCreate = hookType.hookPhase === exports.TransitionHookPhase.CREATE;
  1973. // Instance and Global hook registries
  1974. var $transitions = this.transition.router.transitionService;
  1975. var registries = isCreate ? [$transitions] : [this.transition, $transitions];
  1976. return registries.map(function (reg) { return reg.getHooks(hookType.name); }) // Get named hooks from registries
  1977. .filter(assertPredicate(isArray, "broken event named: " + hookType.name)) // Sanity check
  1978. .reduce(unnestR, []) // Un-nest RegisteredHook[][] to RegisteredHook[] array
  1979. .filter(function (hook) { return hook.matches(treeChanges); }); // Only those satisfying matchCriteria
  1980. };
  1981. return HookBuilder;
  1982. }());
  1983. /**
  1984. * A factory for a sort function for HookTuples.
  1985. *
  1986. * The sort function first compares the PathNode depth (how deep in the state tree a node is), then compares
  1987. * the EventHook priority.
  1988. *
  1989. * @param reverseDepthSort a boolean, when true, reverses the sort order for the node depth
  1990. * @returns a tuple sort function
  1991. */
  1992. function tupleSort(reverseDepthSort) {
  1993. if (reverseDepthSort === void 0) { reverseDepthSort = false; }
  1994. return function nodeDepthThenPriority(l, r) {
  1995. var factor = reverseDepthSort ? -1 : 1;
  1996. var depthDelta = (l.node.state.path.length - r.node.state.path.length) * factor;
  1997. return depthDelta !== 0 ? depthDelta : r.hook.priority - l.hook.priority;
  1998. };
  1999. }
  2000. /**
  2001. * @coreapi
  2002. * @module params
  2003. */
  2004. /** */
  2005. /**
  2006. * An internal class which implements [[ParamTypeDefinition]].
  2007. *
  2008. * A [[ParamTypeDefinition]] is a plain javascript object used to register custom parameter types.
  2009. * When a param type definition is registered, an instance of this class is created internally.
  2010. *
  2011. * This class has naive implementations for all the [[ParamTypeDefinition]] methods.
  2012. *
  2013. * Used by [[UrlMatcher]] when matching or formatting URLs, or comparing and validating parameter values.
  2014. *
  2015. * #### Example:
  2016. * ```js
  2017. * var paramTypeDef = {
  2018. * decode: function(val) { return parseInt(val, 10); },
  2019. * encode: function(val) { return val && val.toString(); },
  2020. * equals: function(a, b) { return this.is(a) && a === b; },
  2021. * is: function(val) { return angular.isNumber(val) && isFinite(val) && val % 1 === 0; },
  2022. * pattern: /\d+/
  2023. * }
  2024. *
  2025. * var paramType = new ParamType(paramTypeDef);
  2026. * ```
  2027. * @internalapi
  2028. */
  2029. var ParamType = /** @class */ (function () {
  2030. /**
  2031. * @param def A configuration object which contains the custom type definition. The object's
  2032. * properties will override the default methods and/or pattern in `ParamType`'s public interface.
  2033. * @returns a new ParamType object
  2034. */
  2035. function ParamType(def) {
  2036. /** @inheritdoc */
  2037. this.pattern = /.*/;
  2038. /** @inheritdoc */
  2039. this.inherit = true;
  2040. extend(this, def);
  2041. }
  2042. // consider these four methods to be "abstract methods" that should be overridden
  2043. /** @inheritdoc */
  2044. ParamType.prototype.is = function (val, key) { return true; };
  2045. /** @inheritdoc */
  2046. ParamType.prototype.encode = function (val, key) { return val; };
  2047. /** @inheritdoc */
  2048. ParamType.prototype.decode = function (val, key) { return val; };
  2049. /** @inheritdoc */
  2050. ParamType.prototype.equals = function (a, b) { return a == b; };
  2051. ParamType.prototype.$subPattern = function () {
  2052. var sub = this.pattern.toString();
  2053. return sub.substr(1, sub.length - 2);
  2054. };
  2055. ParamType.prototype.toString = function () {
  2056. return "{ParamType:" + this.name + "}";
  2057. };
  2058. /** Given an encoded string, or a decoded object, returns a decoded object */
  2059. ParamType.prototype.$normalize = function (val) {
  2060. return this.is(val) ? val : this.decode(val);
  2061. };
  2062. /**
  2063. * Wraps an existing custom ParamType as an array of ParamType, depending on 'mode'.
  2064. * e.g.:
  2065. * - urlmatcher pattern "/path?{queryParam[]:int}"
  2066. * - url: "/path?queryParam=1&queryParam=2
  2067. * - $stateParams.queryParam will be [1, 2]
  2068. * if `mode` is "auto", then
  2069. * - url: "/path?queryParam=1 will create $stateParams.queryParam: 1
  2070. * - url: "/path?queryParam=1&queryParam=2 will create $stateParams.queryParam: [1, 2]
  2071. */
  2072. ParamType.prototype.$asArray = function (mode, isSearch) {
  2073. if (!mode)
  2074. return this;
  2075. if (mode === "auto" && !isSearch)
  2076. throw new Error("'auto' array mode is for query parameters only");
  2077. return new ArrayType(this, mode);
  2078. };
  2079. return ParamType;
  2080. }());
  2081. /**
  2082. * Wraps up a `ParamType` object to handle array values.
  2083. * @internalapi
  2084. */
  2085. function ArrayType(type, mode) {
  2086. var _this = this;
  2087. // Wrap non-array value as array
  2088. function arrayWrap(val) {
  2089. return isArray(val) ? val : (isDefined(val) ? [val] : []);
  2090. }
  2091. // Unwrap array value for "auto" mode. Return undefined for empty array.
  2092. function arrayUnwrap(val) {
  2093. switch (val.length) {
  2094. case 0: return undefined;
  2095. case 1: return mode === "auto" ? val[0] : val;
  2096. default: return val;
  2097. }
  2098. }
  2099. // Wraps type (.is/.encode/.decode) functions to operate on each value of an array
  2100. function arrayHandler(callback, allTruthyMode) {
  2101. return function handleArray(val) {
  2102. if (isArray(val) && val.length === 0)
  2103. return val;
  2104. var arr = arrayWrap(val);
  2105. var result = map(arr, callback);
  2106. return (allTruthyMode === true) ? filter(result, function (x) { return !x; }).length === 0 : arrayUnwrap(result);
  2107. };
  2108. }
  2109. // Wraps type (.equals) functions to operate on each value of an array
  2110. function arrayEqualsHandler(callback) {
  2111. return function handleArray(val1, val2) {
  2112. var left = arrayWrap(val1), right = arrayWrap(val2);
  2113. if (left.length !== right.length)
  2114. return false;
  2115. for (var i = 0; i < left.length; i++) {
  2116. if (!callback(left[i], right[i]))
  2117. return false;
  2118. }
  2119. return true;
  2120. };
  2121. }
  2122. ['encode', 'decode', 'equals', '$normalize'].forEach(function (name) {
  2123. var paramTypeFn = type[name].bind(type);
  2124. var wrapperFn = name === 'equals' ? arrayEqualsHandler : arrayHandler;
  2125. _this[name] = wrapperFn(paramTypeFn);
  2126. });
  2127. extend(this, {
  2128. dynamic: type.dynamic,
  2129. name: type.name,
  2130. pattern: type.pattern,
  2131. inherit: type.inherit,
  2132. is: arrayHandler(type.is.bind(type), true),
  2133. $arrayMode: mode
  2134. });
  2135. }
  2136. /**
  2137. * @coreapi
  2138. * @module params
  2139. */ /** for typedoc */
  2140. /** @hidden */ var hasOwn = Object.prototype.hasOwnProperty;
  2141. /** @hidden */ var isShorthand = function (cfg) {
  2142. return ["value", "type", "squash", "array", "dynamic"].filter(hasOwn.bind(cfg || {})).length === 0;
  2143. };
  2144. /** @internalapi */
  2145. (function (DefType) {
  2146. DefType[DefType["PATH"] = 0] = "PATH";
  2147. DefType[DefType["SEARCH"] = 1] = "SEARCH";
  2148. DefType[DefType["CONFIG"] = 2] = "CONFIG";
  2149. })(exports.DefType || (exports.DefType = {}));
  2150. /** @hidden */
  2151. function unwrapShorthand(cfg) {
  2152. cfg = isShorthand(cfg) && { value: cfg } || cfg;
  2153. getStaticDefaultValue['__cacheable'] = true;
  2154. function getStaticDefaultValue() {
  2155. return cfg.value;
  2156. }
  2157. return extend(cfg, {
  2158. $$fn: isInjectable(cfg.value) ? cfg.value : getStaticDefaultValue,
  2159. });
  2160. }
  2161. /** @hidden */
  2162. function getType(cfg, urlType, location, id, paramTypes) {
  2163. if (cfg.type && urlType && urlType.name !== 'string')
  2164. throw new Error("Param '" + id + "' has two type configurations.");
  2165. if (cfg.type && urlType && urlType.name === 'string' && paramTypes.type(cfg.type))
  2166. return paramTypes.type(cfg.type);
  2167. if (urlType)
  2168. return urlType;
  2169. if (!cfg.type) {
  2170. var type = location === exports.DefType.CONFIG ? "any" :
  2171. location === exports.DefType.PATH ? "path" :
  2172. location === exports.DefType.SEARCH ? "query" : "string";
  2173. return paramTypes.type(type);
  2174. }
  2175. return cfg.type instanceof ParamType ? cfg.type : paramTypes.type(cfg.type);
  2176. }
  2177. /**
  2178. * @internalapi
  2179. * returns false, true, or the squash value to indicate the "default parameter url squash policy".
  2180. */
  2181. function getSquashPolicy(config, isOptional, defaultPolicy) {
  2182. var squash = config.squash;
  2183. if (!isOptional || squash === false)
  2184. return false;
  2185. if (!isDefined(squash) || squash == null)
  2186. return defaultPolicy;
  2187. if (squash === true || isString(squash))
  2188. return squash;
  2189. throw new Error("Invalid squash policy: '" + squash + "'. Valid policies: false, true, or arbitrary string");
  2190. }
  2191. /** @internalapi */
  2192. function getReplace(config, arrayMode, isOptional, squash) {
  2193. var replace, configuredKeys, defaultPolicy = [
  2194. { from: "", to: (isOptional || arrayMode ? undefined : "") },
  2195. { from: null, to: (isOptional || arrayMode ? undefined : "") },
  2196. ];
  2197. replace = isArray(config.replace) ? config.replace : [];
  2198. if (isString(squash))
  2199. replace.push({ from: squash, to: undefined });
  2200. configuredKeys = map(replace, prop("from"));
  2201. return filter(defaultPolicy, function (item) { return configuredKeys.indexOf(item.from) === -1; }).concat(replace);
  2202. }
  2203. /** @internalapi */
  2204. var Param = /** @class */ (function () {
  2205. function Param(id, type, config, location, urlMatcherFactory) {
  2206. config = unwrapShorthand(config);
  2207. type = getType(config, type, location, id, urlMatcherFactory.paramTypes);
  2208. var arrayMode = getArrayMode();
  2209. type = arrayMode ? type.$asArray(arrayMode, location === exports.DefType.SEARCH) : type;
  2210. var isOptional = config.value !== undefined || location === exports.DefType.SEARCH;
  2211. var dynamic = isDefined(config.dynamic) ? !!config.dynamic : !!type.dynamic;
  2212. var raw = isDefined(config.raw) ? !!config.raw : !!type.raw;
  2213. var squash = getSquashPolicy(config, isOptional, urlMatcherFactory.defaultSquashPolicy());
  2214. var replace = getReplace(config, arrayMode, isOptional, squash);
  2215. var inherit$$1 = isDefined(config.inherit) ? !!config.inherit : !!type.inherit;
  2216. // array config: param name (param[]) overrides default settings. explicit config overrides param name.
  2217. function getArrayMode() {
  2218. var arrayDefaults = { array: (location === exports.DefType.SEARCH ? "auto" : false) };
  2219. var arrayParamNomenclature = id.match(/\[\]$/) ? { array: true } : {};
  2220. return extend(arrayDefaults, arrayParamNomenclature, config).array;
  2221. }
  2222. extend(this, { id: id, type: type, location: location, isOptional: isOptional, dynamic: dynamic, raw: raw, squash: squash, replace: replace, inherit: inherit$$1, array: arrayMode, config: config });
  2223. }
  2224. Param.prototype.isDefaultValue = function (value) {
  2225. return this.isOptional && this.type.equals(this.value(), value);
  2226. };
  2227. /**
  2228. * [Internal] Gets the decoded representation of a value if the value is defined, otherwise, returns the
  2229. * default value, which may be the result of an injectable function.
  2230. */
  2231. Param.prototype.value = function (value) {
  2232. var _this = this;
  2233. /**
  2234. * [Internal] Get the default value of a parameter, which may be an injectable function.
  2235. */
  2236. var getDefaultValue = function () {
  2237. if (_this._defaultValueCache)
  2238. return _this._defaultValueCache.defaultValue;
  2239. if (!services.$injector)
  2240. throw new Error("Injectable functions cannot be called at configuration time");
  2241. var defaultValue = services.$injector.invoke(_this.config.$$fn);
  2242. if (defaultValue !== null && defaultValue !== undefined && !_this.type.is(defaultValue))
  2243. throw new Error("Default value (" + defaultValue + ") for parameter '" + _this.id + "' is not an instance of ParamType (" + _this.type.name + ")");
  2244. if (_this.config.$$fn['__cacheable']) {
  2245. _this._defaultValueCache = { defaultValue: defaultValue };
  2246. }
  2247. return defaultValue;
  2248. };
  2249. var replaceSpecialValues = function (val$$1) {
  2250. for (var _i = 0, _a = _this.replace; _i < _a.length; _i++) {
  2251. var tuple = _a[_i];
  2252. if (tuple.from === val$$1)
  2253. return tuple.to;
  2254. }
  2255. return val$$1;
  2256. };
  2257. value = replaceSpecialValues(value);
  2258. return isUndefined(value) ? getDefaultValue() : this.type.$normalize(value);
  2259. };
  2260. Param.prototype.isSearch = function () {
  2261. return this.location === exports.DefType.SEARCH;
  2262. };
  2263. Param.prototype.validates = function (value) {
  2264. // There was no parameter value, but the param is optional
  2265. if ((isUndefined(value) || value === null) && this.isOptional)
  2266. return true;
  2267. // The value was not of the correct ParamType, and could not be decoded to the correct ParamType
  2268. var normalized = this.type.$normalize(value);
  2269. if (!this.type.is(normalized))
  2270. return false;
  2271. // The value was of the correct type, but when encoded, did not match the ParamType's regexp
  2272. var encoded = this.type.encode(normalized);
  2273. return !(isString(encoded) && !this.type.pattern.exec(encoded));
  2274. };
  2275. Param.prototype.toString = function () {
  2276. return "{Param:" + this.id + " " + this.type + " squash: '" + this.squash + "' optional: " + this.isOptional + "}";
  2277. };
  2278. Param.values = function (params, values$$1) {
  2279. if (values$$1 === void 0) { values$$1 = {}; }
  2280. var paramValues = {};
  2281. for (var _i = 0, params_1 = params; _i < params_1.length; _i++) {
  2282. var param = params_1[_i];
  2283. paramValues[param.id] = param.value(values$$1[param.id]);
  2284. }
  2285. return paramValues;
  2286. };
  2287. /**
  2288. * Finds [[Param]] objects which have different param values
  2289. *
  2290. * Filters a list of [[Param]] objects to only those whose parameter values differ in two param value objects
  2291. *
  2292. * @param params: The list of Param objects to filter
  2293. * @param values1: The first set of parameter values
  2294. * @param values2: the second set of parameter values
  2295. *
  2296. * @returns any Param objects whose values were different between values1 and values2
  2297. */
  2298. Param.changed = function (params, values1, values2) {
  2299. if (values1 === void 0) { values1 = {}; }
  2300. if (values2 === void 0) { values2 = {}; }
  2301. return params.filter(function (param) { return !param.type.equals(values1[param.id], values2[param.id]); });
  2302. };
  2303. /**
  2304. * Checks if two param value objects are equal (for a set of [[Param]] objects)
  2305. *
  2306. * @param params The list of [[Param]] objects to check
  2307. * @param values1 The first set of param values
  2308. * @param values2 The second set of param values
  2309. *
  2310. * @returns true if the param values in values1 and values2 are equal
  2311. */
  2312. Param.equals = function (params, values1, values2) {
  2313. if (values1 === void 0) { values1 = {}; }
  2314. if (values2 === void 0) { values2 = {}; }
  2315. return Param.changed(params, values1, values2).length === 0;
  2316. };
  2317. /** Returns true if a the parameter values are valid, according to the Param definitions */
  2318. Param.validates = function (params, values$$1) {
  2319. if (values$$1 === void 0) { values$$1 = {}; }
  2320. return params.map(function (param) { return param.validates(values$$1[param.id]); }).reduce(allTrueR, true);
  2321. };
  2322. return Param;
  2323. }());
  2324. /** @module path */ /** for typedoc */
  2325. /**
  2326. * @internalapi
  2327. *
  2328. * A node in a [[TreeChanges]] path
  2329. *
  2330. * For a [[TreeChanges]] path, this class holds the stateful information for a single node in the path.
  2331. * Each PathNode corresponds to a state being entered, exited, or retained.
  2332. * The stateful information includes parameter values and resolve data.
  2333. */
  2334. var PathNode = /** @class */ (function () {
  2335. function PathNode(stateOrNode) {
  2336. if (stateOrNode instanceof PathNode) {
  2337. var node = stateOrNode;
  2338. this.state = node.state;
  2339. this.paramSchema = node.paramSchema.slice();
  2340. this.paramValues = extend({}, node.paramValues);
  2341. this.resolvables = node.resolvables.slice();
  2342. this.views = node.views && node.views.slice();
  2343. }
  2344. else {
  2345. var state = stateOrNode;
  2346. this.state = state;
  2347. this.paramSchema = state.parameters({ inherit: false });
  2348. this.paramValues = {};
  2349. this.resolvables = state.resolvables.map(function (res) { return res.clone(); });
  2350. }
  2351. }
  2352. /** Sets [[paramValues]] for the node, from the values of an object hash */
  2353. PathNode.prototype.applyRawParams = function (params) {
  2354. var getParamVal = function (paramDef) { return [paramDef.id, paramDef.value(params[paramDef.id])]; };
  2355. this.paramValues = this.paramSchema.reduce(function (memo, pDef) { return applyPairs(memo, getParamVal(pDef)); }, {});
  2356. return this;
  2357. };
  2358. /** Gets a specific [[Param]] metadata that belongs to the node */
  2359. PathNode.prototype.parameter = function (name) {
  2360. return find(this.paramSchema, propEq("id", name));
  2361. };
  2362. /**
  2363. * @returns true if the state and parameter values for another PathNode are
  2364. * equal to the state and param values for this PathNode
  2365. */
  2366. PathNode.prototype.equals = function (node, paramsFn) {
  2367. var diff = this.diff(node, paramsFn);
  2368. return diff && diff.length === 0;
  2369. };
  2370. /**
  2371. * Finds Params with different parameter values on another PathNode.
  2372. *
  2373. * Given another node (of the same state), finds the parameter values which differ.
  2374. * Returns the [[Param]] (schema objects) whose parameter values differ.
  2375. *
  2376. * Given another node for a different state, returns `false`
  2377. *
  2378. * @param node The node to compare to
  2379. * @param paramsFn A function that returns which parameters should be compared.
  2380. * @returns The [[Param]]s which differ, or null if the two nodes are for different states
  2381. */
  2382. PathNode.prototype.diff = function (node, paramsFn) {
  2383. if (this.state !== node.state)
  2384. return false;
  2385. var params = paramsFn ? paramsFn(this) : this.paramSchema;
  2386. return Param.changed(params, this.paramValues, node.paramValues);
  2387. };
  2388. /** Returns a clone of the PathNode */
  2389. PathNode.clone = function (node) {
  2390. return new PathNode(node);
  2391. };
  2392. return PathNode;
  2393. }());
  2394. /** @module path */ /** for typedoc */
  2395. /**
  2396. * This class contains functions which convert TargetStates, Nodes and paths from one type to another.
  2397. */
  2398. var PathUtils = /** @class */ (function () {
  2399. function PathUtils() {
  2400. }
  2401. /** Given a PathNode[], create an TargetState */
  2402. PathUtils.makeTargetState = function (registry, path) {
  2403. var state = tail(path).state;
  2404. return new TargetState(registry, state, path.map(prop("paramValues")).reduce(mergeR, {}), {});
  2405. };
  2406. PathUtils.buildPath = function (targetState) {
  2407. var toParams = targetState.params();
  2408. return targetState.$state().path.map(function (state) { return new PathNode(state).applyRawParams(toParams); });
  2409. };
  2410. /** Given a fromPath: PathNode[] and a TargetState, builds a toPath: PathNode[] */
  2411. PathUtils.buildToPath = function (fromPath, targetState) {
  2412. var toPath = PathUtils.buildPath(targetState);
  2413. if (targetState.options().inherit) {
  2414. return PathUtils.inheritParams(fromPath, toPath, Object.keys(targetState.params()));
  2415. }
  2416. return toPath;
  2417. };
  2418. /**
  2419. * Creates ViewConfig objects and adds to nodes.
  2420. *
  2421. * On each [[PathNode]], creates ViewConfig objects from the views: property of the node's state
  2422. */
  2423. PathUtils.applyViewConfigs = function ($view, path, states) {
  2424. // Only apply the viewConfigs to the nodes for the given states
  2425. path.filter(function (node) { return inArray(states, node.state); }).forEach(function (node) {
  2426. var viewDecls = values(node.state.views || {});
  2427. var subPath = PathUtils.subPath(path, function (n) { return n === node; });
  2428. var viewConfigs = viewDecls.map(function (view) { return $view.createViewConfig(subPath, view); });
  2429. node.views = viewConfigs.reduce(unnestR, []);
  2430. });
  2431. };
  2432. /**
  2433. * Given a fromPath and a toPath, returns a new to path which inherits parameters from the fromPath
  2434. *
  2435. * For a parameter in a node to be inherited from the from path:
  2436. * - The toPath's node must have a matching node in the fromPath (by state).
  2437. * - The parameter name must not be found in the toKeys parameter array.
  2438. *
  2439. * Note: the keys provided in toKeys are intended to be those param keys explicitly specified by some
  2440. * caller, for instance, $state.transitionTo(..., toParams). If a key was found in toParams,
  2441. * it is not inherited from the fromPath.
  2442. */
  2443. PathUtils.inheritParams = function (fromPath, toPath, toKeys) {
  2444. if (toKeys === void 0) { toKeys = []; }
  2445. function nodeParamVals(path, state) {
  2446. var node = find(path, propEq('state', state));
  2447. return extend({}, node && node.paramValues);
  2448. }
  2449. var noInherit = fromPath.map(function (node) { return node.paramSchema; })
  2450. .reduce(unnestR, [])
  2451. .filter(function (param) { return !param.inherit; })
  2452. .map(prop('id'));
  2453. /**
  2454. * Given an [[PathNode]] "toNode", return a new [[PathNode]] with param values inherited from the
  2455. * matching node in fromPath. Only inherit keys that aren't found in "toKeys" from the node in "fromPath""
  2456. */
  2457. function makeInheritedParamsNode(toNode) {
  2458. // All param values for the node (may include default key/vals, when key was not found in toParams)
  2459. var toParamVals = extend({}, toNode && toNode.paramValues);
  2460. // limited to only those keys found in toParams
  2461. var incomingParamVals = pick(toParamVals, toKeys);
  2462. toParamVals = omit(toParamVals, toKeys);
  2463. var fromParamVals = omit(nodeParamVals(fromPath, toNode.state) || {}, noInherit);
  2464. // extend toParamVals with any fromParamVals, then override any of those those with incomingParamVals
  2465. var ownParamVals = extend(toParamVals, fromParamVals, incomingParamVals);
  2466. return new PathNode(toNode.state).applyRawParams(ownParamVals);
  2467. }
  2468. // The param keys specified by the incoming toParams
  2469. return toPath.map(makeInheritedParamsNode);
  2470. };
  2471. /**
  2472. * Computes the tree changes (entering, exiting) between a fromPath and toPath.
  2473. */
  2474. PathUtils.treeChanges = function (fromPath, toPath, reloadState) {
  2475. var keep = 0, max = Math.min(fromPath.length, toPath.length);
  2476. var nodesMatch = function (node1, node2) {
  2477. return node1.equals(node2, PathUtils.nonDynamicParams);
  2478. };
  2479. while (keep < max && fromPath[keep].state !== reloadState && nodesMatch(fromPath[keep], toPath[keep])) {
  2480. keep++;
  2481. }
  2482. /** Given a retained node, return a new node which uses the to node's param values */
  2483. function applyToParams(retainedNode, idx) {
  2484. var cloned = PathNode.clone(retainedNode);
  2485. cloned.paramValues = toPath[idx].paramValues;
  2486. return cloned;
  2487. }
  2488. var from, retained, exiting, entering, to;
  2489. from = fromPath;
  2490. retained = from.slice(0, keep);
  2491. exiting = from.slice(keep);
  2492. // Create a new retained path (with shallow copies of nodes) which have the params of the toPath mapped
  2493. var retainedWithToParams = retained.map(applyToParams);
  2494. entering = toPath.slice(keep);
  2495. to = (retainedWithToParams).concat(entering);
  2496. return { from: from, to: to, retained: retained, exiting: exiting, entering: entering };
  2497. };
  2498. /**
  2499. * Returns a new path which is: the subpath of the first path which matches the second path.
  2500. *
  2501. * The new path starts from root and contains any nodes that match the nodes in the second path.
  2502. * It stops before the first non-matching node.
  2503. *
  2504. * Nodes are compared using their state property and their parameter values.
  2505. * If a `paramsFn` is provided, only the [[Param]] returned by the function will be considered when comparing nodes.
  2506. *
  2507. * @param pathA the first path
  2508. * @param pathB the second path
  2509. * @param paramsFn a function which returns the parameters to consider when comparing
  2510. *
  2511. * @returns an array of PathNodes from the first path which match the nodes in the second path
  2512. */
  2513. PathUtils.matching = function (pathA, pathB, paramsFn) {
  2514. var done = false;
  2515. var tuples = arrayTuples(pathA, pathB);
  2516. return tuples.reduce(function (matching, _a) {
  2517. var nodeA = _a[0], nodeB = _a[1];
  2518. done = done || !nodeA.equals(nodeB, paramsFn);
  2519. return done ? matching : matching.concat(nodeA);
  2520. }, []);
  2521. };
  2522. /**
  2523. * Returns true if two paths are identical.
  2524. *
  2525. * @param pathA
  2526. * @param pathB
  2527. * @param paramsFn a function which returns the parameters to consider when comparing
  2528. * @returns true if the the states and parameter values for both paths are identical
  2529. */
  2530. PathUtils.equals = function (pathA, pathB, paramsFn) {
  2531. return pathA.length === pathB.length &&
  2532. PathUtils.matching(pathA, pathB, paramsFn).length === pathA.length;
  2533. };
  2534. /**
  2535. * Return a subpath of a path, which stops at the first matching node
  2536. *
  2537. * Given an array of nodes, returns a subset of the array starting from the first node,
  2538. * stopping when the first node matches the predicate.
  2539. *
  2540. * @param path a path of [[PathNode]]s
  2541. * @param predicate a [[Predicate]] fn that matches [[PathNode]]s
  2542. * @returns a subpath up to the matching node, or undefined if no match is found
  2543. */
  2544. PathUtils.subPath = function (path, predicate) {
  2545. var node = find(path, predicate);
  2546. var elementIdx = path.indexOf(node);
  2547. return elementIdx === -1 ? undefined : path.slice(0, elementIdx + 1);
  2548. };
  2549. PathUtils.nonDynamicParams = function (node) {
  2550. return node.state.parameters({ inherit: false })
  2551. .filter(function (param) { return !param.dynamic; });
  2552. };
  2553. /** Gets the raw parameter values from a path */
  2554. PathUtils.paramValues = function (path) {
  2555. return path.reduce(function (acc, node) { return extend(acc, node.paramValues); }, {});
  2556. };
  2557. return PathUtils;
  2558. }());
  2559. /**
  2560. * @coreapi
  2561. * @module resolve
  2562. */ /** for typedoc */
  2563. // TODO: explicitly make this user configurable
  2564. var defaultResolvePolicy = {
  2565. when: "LAZY",
  2566. async: "WAIT"
  2567. };
  2568. /**
  2569. * The basic building block for the resolve system.
  2570. *
  2571. * Resolvables encapsulate a state's resolve's resolveFn, the resolveFn's declared dependencies, the wrapped (.promise),
  2572. * and the unwrapped-when-complete (.data) result of the resolveFn.
  2573. *
  2574. * Resolvable.get() either retrieves the Resolvable's existing promise, or else invokes resolve() (which invokes the
  2575. * resolveFn) and returns the resulting promise.
  2576. *
  2577. * Resolvable.get() and Resolvable.resolve() both execute within a context path, which is passed as the first
  2578. * parameter to those fns.
  2579. */
  2580. var Resolvable = /** @class */ (function () {
  2581. function Resolvable(arg1, resolveFn, deps, policy, data) {
  2582. this.resolved = false;
  2583. this.promise = undefined;
  2584. if (arg1 instanceof Resolvable) {
  2585. extend(this, arg1);
  2586. }
  2587. else if (isFunction(resolveFn)) {
  2588. if (isNullOrUndefined(arg1))
  2589. throw new Error("new Resolvable(): token argument is required");
  2590. if (!isFunction(resolveFn))
  2591. throw new Error("new Resolvable(): resolveFn argument must be a function");
  2592. this.token = arg1;
  2593. this.policy = policy;
  2594. this.resolveFn = resolveFn;
  2595. this.deps = deps || [];
  2596. this.data = data;
  2597. this.resolved = data !== undefined;
  2598. this.promise = this.resolved ? services.$q.when(this.data) : undefined;
  2599. }
  2600. else if (isObject(arg1) && arg1.token && isFunction(arg1.resolveFn)) {
  2601. var literal = arg1;
  2602. return new Resolvable(literal.token, literal.resolveFn, literal.deps, literal.policy, literal.data);
  2603. }
  2604. }
  2605. Resolvable.prototype.getPolicy = function (state) {
  2606. var thisPolicy = this.policy || {};
  2607. var statePolicy = state && state.resolvePolicy || {};
  2608. return {
  2609. when: thisPolicy.when || statePolicy.when || defaultResolvePolicy.when,
  2610. async: thisPolicy.async || statePolicy.async || defaultResolvePolicy.async,
  2611. };
  2612. };
  2613. /**
  2614. * Asynchronously resolve this Resolvable's data
  2615. *
  2616. * Given a ResolveContext that this Resolvable is found in:
  2617. * Wait for this Resolvable's dependencies, then invoke this Resolvable's function
  2618. * and update the Resolvable's state
  2619. */
  2620. Resolvable.prototype.resolve = function (resolveContext, trans) {
  2621. var _this = this;
  2622. var $q = services.$q;
  2623. // Gets all dependencies from ResolveContext and wait for them to be resolved
  2624. var getResolvableDependencies = function () {
  2625. return $q.all(resolveContext.getDependencies(_this).map(function (resolvable) {
  2626. return resolvable.get(resolveContext, trans);
  2627. }));
  2628. };
  2629. // Invokes the resolve function passing the resolved dependencies as arguments
  2630. var invokeResolveFn = function (resolvedDeps) {
  2631. return _this.resolveFn.apply(null, resolvedDeps);
  2632. };
  2633. /**
  2634. * For RXWAIT policy:
  2635. *
  2636. * Given an observable returned from a resolve function:
  2637. * - enables .cache() mode (this allows multicast subscribers)
  2638. * - then calls toPromise() (this triggers subscribe() and thus fetches)
  2639. * - Waits for the promise, then return the cached observable (not the first emitted value).
  2640. */
  2641. var waitForRx = function (observable$) {
  2642. var cached = observable$.cache(1);
  2643. return cached.take(1).toPromise().then(function () { return cached; });
  2644. };
  2645. // If the resolve policy is RXWAIT, wait for the observable to emit something. otherwise pass through.
  2646. var node = resolveContext.findNode(this);
  2647. var state = node && node.state;
  2648. var maybeWaitForRx = this.getPolicy(state).async === "RXWAIT" ? waitForRx : identity;
  2649. // After the final value has been resolved, update the state of the Resolvable
  2650. var applyResolvedValue = function (resolvedValue) {
  2651. _this.data = resolvedValue;
  2652. _this.resolved = true;
  2653. trace.traceResolvableResolved(_this, trans);
  2654. return _this.data;
  2655. };
  2656. // Sets the promise property first, then getsResolvableDependencies in the context of the promise chain. Always waits one tick.
  2657. return this.promise = $q.when()
  2658. .then(getResolvableDependencies)
  2659. .then(invokeResolveFn)
  2660. .then(maybeWaitForRx)
  2661. .then(applyResolvedValue);
  2662. };
  2663. /**
  2664. * Gets a promise for this Resolvable's data.
  2665. *
  2666. * Fetches the data and returns a promise.
  2667. * Returns the existing promise if it has already been fetched once.
  2668. */
  2669. Resolvable.prototype.get = function (resolveContext, trans) {
  2670. return this.promise || this.resolve(resolveContext, trans);
  2671. };
  2672. Resolvable.prototype.toString = function () {
  2673. return "Resolvable(token: " + stringify(this.token) + ", requires: [" + this.deps.map(stringify) + "])";
  2674. };
  2675. Resolvable.prototype.clone = function () {
  2676. return new Resolvable(this);
  2677. };
  2678. Resolvable.fromData = function (token, data) {
  2679. return new Resolvable(token, function () { return data; }, null, null, data);
  2680. };
  2681. return Resolvable;
  2682. }());
  2683. /** @internalapi */
  2684. var resolvePolicies = {
  2685. when: {
  2686. LAZY: "LAZY",
  2687. EAGER: "EAGER"
  2688. },
  2689. async: {
  2690. WAIT: "WAIT",
  2691. NOWAIT: "NOWAIT",
  2692. RXWAIT: "RXWAIT"
  2693. }
  2694. };
  2695. /** @module resolve */
  2696. /** for typedoc */
  2697. var whens = resolvePolicies.when;
  2698. var ALL_WHENS = [whens.EAGER, whens.LAZY];
  2699. var EAGER_WHENS = [whens.EAGER];
  2700. var NATIVE_INJECTOR_TOKEN = "Native Injector";
  2701. /**
  2702. * Encapsulates Dependency Injection for a path of nodes
  2703. *
  2704. * UI-Router states are organized as a tree.
  2705. * A nested state has a path of ancestors to the root of the tree.
  2706. * When a state is being activated, each element in the path is wrapped as a [[PathNode]].
  2707. * A `PathNode` is a stateful object that holds things like parameters and resolvables for the state being activated.
  2708. *
  2709. * The ResolveContext closes over the [[PathNode]]s, and provides DI for the last node in the path.
  2710. */
  2711. var ResolveContext = /** @class */ (function () {
  2712. function ResolveContext(_path) {
  2713. this._path = _path;
  2714. }
  2715. /** Gets all the tokens found in the resolve context, de-duplicated */
  2716. ResolveContext.prototype.getTokens = function () {
  2717. return this._path.reduce(function (acc, node) { return acc.concat(node.resolvables.map(function (r) { return r.token; })); }, []).reduce(uniqR, []);
  2718. };
  2719. /**
  2720. * Gets the Resolvable that matches the token
  2721. *
  2722. * Gets the last Resolvable that matches the token in this context, or undefined.
  2723. * Throws an error if it doesn't exist in the ResolveContext
  2724. */
  2725. ResolveContext.prototype.getResolvable = function (token) {
  2726. var matching = this._path.map(function (node) { return node.resolvables; })
  2727. .reduce(unnestR, [])
  2728. .filter(function (r) { return r.token === token; });
  2729. return tail(matching);
  2730. };
  2731. /** Returns the [[ResolvePolicy]] for the given [[Resolvable]] */
  2732. ResolveContext.prototype.getPolicy = function (resolvable) {
  2733. var node = this.findNode(resolvable);
  2734. return resolvable.getPolicy(node.state);
  2735. };
  2736. /**
  2737. * Returns a ResolveContext that includes a portion of this one
  2738. *
  2739. * Given a state, this method creates a new ResolveContext from this one.
  2740. * The new context starts at the first node (root) and stops at the node for the `state` parameter.
  2741. *
  2742. * #### Why
  2743. *
  2744. * When a transition is created, the nodes in the "To Path" are injected from a ResolveContext.
  2745. * A ResolveContext closes over a path of [[PathNode]]s and processes the resolvables.
  2746. * The "To State" can inject values from its own resolvables, as well as those from all its ancestor state's (node's).
  2747. * This method is used to create a narrower context when injecting ancestor nodes.
  2748. *
  2749. * @example
  2750. * `let ABCD = new ResolveContext([A, B, C, D]);`
  2751. *
  2752. * Given a path `[A, B, C, D]`, where `A`, `B`, `C` and `D` are nodes for states `a`, `b`, `c`, `d`:
  2753. * When injecting `D`, `D` should have access to all resolvables from `A`, `B`, `C`, `D`.
  2754. * However, `B` should only be able to access resolvables from `A`, `B`.
  2755. *
  2756. * When resolving for the `B` node, first take the full "To Path" Context `[A,B,C,D]` and limit to the subpath `[A,B]`.
  2757. * `let AB = ABCD.subcontext(a)`
  2758. */
  2759. ResolveContext.prototype.subContext = function (state) {
  2760. return new ResolveContext(PathUtils.subPath(this._path, function (node) { return node.state === state; }));
  2761. };
  2762. /**
  2763. * Adds Resolvables to the node that matches the state
  2764. *
  2765. * This adds a [[Resolvable]] (generally one created on the fly; not declared on a [[StateDeclaration.resolve]] block).
  2766. * The resolvable is added to the node matching the `state` parameter.
  2767. *
  2768. * These new resolvables are not automatically fetched.
  2769. * The calling code should either fetch them, fetch something that depends on them,
  2770. * or rely on [[resolvePath]] being called when some state is being entered.
  2771. *
  2772. * Note: each resolvable's [[ResolvePolicy]] is merged with the state's policy, and the global default.
  2773. *
  2774. * @param newResolvables the new Resolvables
  2775. * @param state Used to find the node to put the resolvable on
  2776. */
  2777. ResolveContext.prototype.addResolvables = function (newResolvables, state) {
  2778. var node = find(this._path, propEq('state', state));
  2779. var keys = newResolvables.map(function (r) { return r.token; });
  2780. node.resolvables = node.resolvables.filter(function (r) { return keys.indexOf(r.token) === -1; }).concat(newResolvables);
  2781. };
  2782. /**
  2783. * Returns a promise for an array of resolved path Element promises
  2784. *
  2785. * @param when
  2786. * @param trans
  2787. * @returns {Promise<any>|any}
  2788. */
  2789. ResolveContext.prototype.resolvePath = function (when, trans) {
  2790. var _this = this;
  2791. if (when === void 0) { when = "LAZY"; }
  2792. // This option determines which 'when' policy Resolvables we are about to fetch.
  2793. var whenOption = inArray(ALL_WHENS, when) ? when : "LAZY";
  2794. // If the caller specified EAGER, only the EAGER Resolvables are fetched.
  2795. // if the caller specified LAZY, both EAGER and LAZY Resolvables are fetched.`
  2796. var matchedWhens = whenOption === resolvePolicies.when.EAGER ? EAGER_WHENS : ALL_WHENS;
  2797. // get the subpath to the state argument, if provided
  2798. trace.traceResolvePath(this._path, when, trans);
  2799. var matchesPolicy = function (acceptedVals, whenOrAsync) {
  2800. return function (resolvable) {
  2801. return inArray(acceptedVals, _this.getPolicy(resolvable)[whenOrAsync]);
  2802. };
  2803. };
  2804. // Trigger all the (matching) Resolvables in the path
  2805. // Reduce all the "WAIT" Resolvables into an array
  2806. var promises = this._path.reduce(function (acc, node) {
  2807. var nodeResolvables = node.resolvables.filter(matchesPolicy(matchedWhens, 'when'));
  2808. var nowait = nodeResolvables.filter(matchesPolicy(['NOWAIT'], 'async'));
  2809. var wait = nodeResolvables.filter(not(matchesPolicy(['NOWAIT'], 'async')));
  2810. // For the matching Resolvables, start their async fetch process.
  2811. var subContext = _this.subContext(node.state);
  2812. var getResult = function (r) { return r.get(subContext, trans)
  2813. .then(function (value) { return ({ token: r.token, value: value }); }); };
  2814. nowait.forEach(getResult);
  2815. return acc.concat(wait.map(getResult));
  2816. }, []);
  2817. // Wait for all the "WAIT" resolvables
  2818. return services.$q.all(promises);
  2819. };
  2820. ResolveContext.prototype.injector = function () {
  2821. return this._injector || (this._injector = new UIInjectorImpl(this));
  2822. };
  2823. ResolveContext.prototype.findNode = function (resolvable) {
  2824. return find(this._path, function (node) { return inArray(node.resolvables, resolvable); });
  2825. };
  2826. /**
  2827. * Gets the async dependencies of a Resolvable
  2828. *
  2829. * Given a Resolvable, returns its dependencies as a Resolvable[]
  2830. */
  2831. ResolveContext.prototype.getDependencies = function (resolvable) {
  2832. var _this = this;
  2833. var node = this.findNode(resolvable);
  2834. // Find which other resolvables are "visible" to the `resolvable` argument
  2835. // subpath stopping at resolvable's node, or the whole path (if the resolvable isn't in the path)
  2836. var subPath = PathUtils.subPath(this._path, function (x) { return x === node; }) || this._path;
  2837. var availableResolvables = subPath
  2838. .reduce(function (acc, _node) { return acc.concat(_node.resolvables); }, []) //all of subpath's resolvables
  2839. .filter(function (res) { return res !== resolvable; }); // filter out the `resolvable` argument
  2840. var getDependency = function (token) {
  2841. var matching = availableResolvables.filter(function (r) { return r.token === token; });
  2842. if (matching.length)
  2843. return tail(matching);
  2844. var fromInjector = _this.injector().getNative(token);
  2845. if (isUndefined(fromInjector)) {
  2846. throw new Error("Could not find Dependency Injection token: " + stringify(token));
  2847. }
  2848. return new Resolvable(token, function () { return fromInjector; }, [], fromInjector);
  2849. };
  2850. return resolvable.deps.map(getDependency);
  2851. };
  2852. return ResolveContext;
  2853. }());
  2854. var UIInjectorImpl = /** @class */ (function () {
  2855. function UIInjectorImpl(context) {
  2856. this.context = context;
  2857. this.native = this.get(NATIVE_INJECTOR_TOKEN) || services.$injector;
  2858. }
  2859. UIInjectorImpl.prototype.get = function (token) {
  2860. var resolvable = this.context.getResolvable(token);
  2861. if (resolvable) {
  2862. if (this.context.getPolicy(resolvable).async === 'NOWAIT') {
  2863. return resolvable.get(this.context);
  2864. }
  2865. if (!resolvable.resolved) {
  2866. throw new Error("Resolvable async .get() not complete:" + stringify(resolvable.token));
  2867. }
  2868. return resolvable.data;
  2869. }
  2870. return this.getNative(token);
  2871. };
  2872. UIInjectorImpl.prototype.getAsync = function (token) {
  2873. var resolvable = this.context.getResolvable(token);
  2874. if (resolvable)
  2875. return resolvable.get(this.context);
  2876. return services.$q.when(this.native.get(token));
  2877. };
  2878. UIInjectorImpl.prototype.getNative = function (token) {
  2879. return this.native && this.native.get(token);
  2880. };
  2881. return UIInjectorImpl;
  2882. }());
  2883. /**
  2884. * @coreapi
  2885. * @module transition
  2886. */
  2887. /** for typedoc */
  2888. /** @hidden */
  2889. var stateSelf = prop("self");
  2890. /**
  2891. * Represents a transition between two states.
  2892. *
  2893. * When navigating to a state, we are transitioning **from** the current state **to** the new state.
  2894. *
  2895. * This object contains all contextual information about the to/from states, parameters, resolves.
  2896. * It has information about all states being entered and exited as a result of the transition.
  2897. */
  2898. var Transition = /** @class */ (function () {
  2899. /**
  2900. * Creates a new Transition object.
  2901. *
  2902. * If the target state is not valid, an error is thrown.
  2903. *
  2904. * @internalapi
  2905. *
  2906. * @param fromPath The path of [[PathNode]]s from which the transition is leaving. The last node in the `fromPath`
  2907. * encapsulates the "from state".
  2908. * @param targetState The target state and parameters being transitioned to (also, the transition options)
  2909. * @param router The [[UIRouter]] instance
  2910. */
  2911. function Transition(fromPath, targetState, router) {
  2912. var _this = this;
  2913. /** @hidden */
  2914. this._deferred = services.$q.defer();
  2915. /**
  2916. * This promise is resolved or rejected based on the outcome of the Transition.
  2917. *
  2918. * When the transition is successful, the promise is resolved
  2919. * When the transition is unsuccessful, the promise is rejected with the [[Rejection]] or javascript error
  2920. */
  2921. this.promise = this._deferred.promise;
  2922. /** @hidden Holds the hook registration functions such as those passed to Transition.onStart() */
  2923. this._registeredHooks = {};
  2924. /** @hidden */
  2925. this._hookBuilder = new HookBuilder(this);
  2926. /** Checks if this transition is currently active/running. */
  2927. this.isActive = function () {
  2928. return _this.router.globals.transition === _this;
  2929. };
  2930. this.router = router;
  2931. this._targetState = targetState;
  2932. if (!targetState.valid()) {
  2933. throw new Error(targetState.error());
  2934. }
  2935. // current() is assumed to come from targetState.options, but provide a naive implementation otherwise.
  2936. this._options = extend({ current: val(this) }, targetState.options());
  2937. this.$id = router.transitionService._transitionCount++;
  2938. var toPath = PathUtils.buildToPath(fromPath, targetState);
  2939. this._treeChanges = PathUtils.treeChanges(fromPath, toPath, this._options.reloadState);
  2940. this.createTransitionHookRegFns();
  2941. var onCreateHooks = this._hookBuilder.buildHooksForPhase(exports.TransitionHookPhase.CREATE);
  2942. TransitionHook.invokeHooks(onCreateHooks, function () { return null; });
  2943. this.applyViewConfigs(router);
  2944. }
  2945. /** @hidden */
  2946. Transition.prototype.onBefore = function (criteria, callback, options) { return; };
  2947. /** @inheritdoc */
  2948. Transition.prototype.onStart = function (criteria, callback, options) { return; };
  2949. /** @inheritdoc */
  2950. Transition.prototype.onExit = function (criteria, callback, options) { return; };
  2951. /** @inheritdoc */
  2952. Transition.prototype.onRetain = function (criteria, callback, options) { return; };
  2953. /** @inheritdoc */
  2954. Transition.prototype.onEnter = function (criteria, callback, options) { return; };
  2955. /** @inheritdoc */
  2956. Transition.prototype.onFinish = function (criteria, callback, options) { return; };
  2957. /** @inheritdoc */
  2958. Transition.prototype.onSuccess = function (criteria, callback, options) { return; };
  2959. /** @inheritdoc */
  2960. Transition.prototype.onError = function (criteria, callback, options) { return; };
  2961. /** @hidden
  2962. * Creates the transition-level hook registration functions
  2963. * (which can then be used to register hooks)
  2964. */
  2965. Transition.prototype.createTransitionHookRegFns = function () {
  2966. var _this = this;
  2967. this.router.transitionService._pluginapi._getEvents()
  2968. .filter(function (type) { return type.hookPhase !== exports.TransitionHookPhase.CREATE; })
  2969. .forEach(function (type) { return makeEvent(_this, _this.router.transitionService, type); });
  2970. };
  2971. /** @internalapi */
  2972. Transition.prototype.getHooks = function (hookName) {
  2973. return this._registeredHooks[hookName];
  2974. };
  2975. Transition.prototype.applyViewConfigs = function (router) {
  2976. var enteringStates = this._treeChanges.entering.map(function (node) { return node.state; });
  2977. PathUtils.applyViewConfigs(router.transitionService.$view, this._treeChanges.to, enteringStates);
  2978. };
  2979. /**
  2980. * @internalapi
  2981. *
  2982. * @returns the internal from [State] object
  2983. */
  2984. Transition.prototype.$from = function () {
  2985. return tail(this._treeChanges.from).state;
  2986. };
  2987. /**
  2988. * @internalapi
  2989. *
  2990. * @returns the internal to [State] object
  2991. */
  2992. Transition.prototype.$to = function () {
  2993. return tail(this._treeChanges.to).state;
  2994. };
  2995. /**
  2996. * Returns the "from state"
  2997. *
  2998. * Returns the state that the transition is coming *from*.
  2999. *
  3000. * @returns The state declaration object for the Transition's ("from state").
  3001. */
  3002. Transition.prototype.from = function () {
  3003. return this.$from().self;
  3004. };
  3005. /**
  3006. * Returns the "to state"
  3007. *
  3008. * Returns the state that the transition is going *to*.
  3009. *
  3010. * @returns The state declaration object for the Transition's target state ("to state").
  3011. */
  3012. Transition.prototype.to = function () {
  3013. return this.$to().self;
  3014. };
  3015. /**
  3016. * Gets the Target State
  3017. *
  3018. * A transition's [[TargetState]] encapsulates the [[to]] state, the [[params]], and the [[options]] as a single object.
  3019. *
  3020. * @returns the [[TargetState]] of this Transition
  3021. */
  3022. Transition.prototype.targetState = function () {
  3023. return this._targetState;
  3024. };
  3025. /**
  3026. * Determines whether two transitions are equivalent.
  3027. * @deprecated
  3028. */
  3029. Transition.prototype.is = function (compare) {
  3030. if (compare instanceof Transition) {
  3031. // TODO: Also compare parameters
  3032. return this.is({ to: compare.$to().name, from: compare.$from().name });
  3033. }
  3034. return !((compare.to && !matchState(this.$to(), compare.to)) ||
  3035. (compare.from && !matchState(this.$from(), compare.from)));
  3036. };
  3037. Transition.prototype.params = function (pathname) {
  3038. if (pathname === void 0) { pathname = "to"; }
  3039. return Object.freeze(this._treeChanges[pathname].map(prop("paramValues")).reduce(mergeR, {}));
  3040. };
  3041. /**
  3042. * Creates a [[UIInjector]] Dependency Injector
  3043. *
  3044. * Returns a Dependency Injector for the Transition's target state (to state).
  3045. * The injector provides resolve values which the target state has access to.
  3046. *
  3047. * The `UIInjector` can also provide values from the native root/global injector (ng1/ng2).
  3048. *
  3049. * #### Example:
  3050. * ```js
  3051. * .onEnter({ entering: 'myState' }, trans => {
  3052. * var myResolveValue = trans.injector().get('myResolve');
  3053. * // Inject a global service from the global/native injector (if it exists)
  3054. * var MyService = trans.injector().get('MyService');
  3055. * })
  3056. * ```
  3057. *
  3058. * In some cases (such as `onBefore`), you may need access to some resolve data but it has not yet been fetched.
  3059. * You can use [[UIInjector.getAsync]] to get a promise for the data.
  3060. * #### Example:
  3061. * ```js
  3062. * .onBefore({}, trans => {
  3063. * return trans.injector().getAsync('myResolve').then(myResolveValue =>
  3064. * return myResolveValue !== 'ABORT';
  3065. * });
  3066. * });
  3067. * ```
  3068. *
  3069. * If a `state` is provided, the injector that is returned will be limited to resolve values that the provided state has access to.
  3070. * This can be useful if both a parent state `foo` and a child state `foo.bar` have both defined a resolve such as `data`.
  3071. * #### Example:
  3072. * ```js
  3073. * .onEnter({ to: 'foo.bar' }, trans => {
  3074. * // returns result of `foo` state's `data` resolve
  3075. * // even though `foo.bar` also has a `data` resolve
  3076. * var fooData = trans.injector('foo').get('data');
  3077. * });
  3078. * ```
  3079. *
  3080. * If you need resolve data from the exiting states, pass `'from'` as `pathName`.
  3081. * The resolve data from the `from` path will be returned.
  3082. * #### Example:
  3083. * ```js
  3084. * .onExit({ exiting: 'foo.bar' }, trans => {
  3085. * // Gets the resolve value of `data` from the exiting state.
  3086. * var fooData = trans.injector(null, 'foo.bar').get('data');
  3087. * });
  3088. * ```
  3089. *
  3090. *
  3091. * @param state Limits the resolves provided to only the resolves the provided state has access to.
  3092. * @param pathName Default: `'to'`: Chooses the path for which to create the injector. Use this to access resolves for `exiting` states.
  3093. *
  3094. * @returns a [[UIInjector]]
  3095. */
  3096. Transition.prototype.injector = function (state, pathName) {
  3097. if (pathName === void 0) { pathName = "to"; }
  3098. var path = this._treeChanges[pathName];
  3099. if (state)
  3100. path = PathUtils.subPath(path, function (node) { return node.state === state || node.state.name === state; });
  3101. return new ResolveContext(path).injector();
  3102. };
  3103. /**
  3104. * Gets all available resolve tokens (keys)
  3105. *
  3106. * This method can be used in conjunction with [[injector]] to inspect the resolve values
  3107. * available to the Transition.
  3108. *
  3109. * This returns all the tokens defined on [[StateDeclaration.resolve]] blocks, for the states
  3110. * in the Transition's [[TreeChanges.to]] path.
  3111. *
  3112. * #### Example:
  3113. * This example logs all resolve values
  3114. * ```js
  3115. * let tokens = trans.getResolveTokens();
  3116. * tokens.forEach(token => console.log(token + " = " + trans.injector().get(token)));
  3117. * ```
  3118. *
  3119. * #### Example:
  3120. * This example creates promises for each resolve value.
  3121. * This triggers fetches of resolves (if any have not yet been fetched).
  3122. * When all promises have all settled, it logs the resolve values.
  3123. * ```js
  3124. * let tokens = trans.getResolveTokens();
  3125. * let promise = tokens.map(token => trans.injector().getAsync(token));
  3126. * Promise.all(promises).then(values => console.log("Resolved values: " + values));
  3127. * ```
  3128. *
  3129. * Note: Angular 1 users whould use `$q.all()`
  3130. *
  3131. * @param pathname resolve context's path name (e.g., `to` or `from`)
  3132. *
  3133. * @returns an array of resolve tokens (keys)
  3134. */
  3135. Transition.prototype.getResolveTokens = function (pathname) {
  3136. if (pathname === void 0) { pathname = "to"; }
  3137. return new ResolveContext(this._treeChanges[pathname]).getTokens();
  3138. };
  3139. /**
  3140. * Dynamically adds a new [[Resolvable]] (i.e., [[StateDeclaration.resolve]]) to this transition.
  3141. *
  3142. * #### Example:
  3143. * ```js
  3144. * transitionService.onBefore({}, transition => {
  3145. * transition.addResolvable({
  3146. * token: 'myResolve',
  3147. * deps: ['MyService'],
  3148. * resolveFn: myService => myService.getData()
  3149. * });
  3150. * });
  3151. * ```
  3152. *
  3153. * @param resolvable a [[ResolvableLiteral]] object (or a [[Resolvable]])
  3154. * @param state the state in the "to path" which should receive the new resolve (otherwise, the root state)
  3155. */
  3156. Transition.prototype.addResolvable = function (resolvable, state) {
  3157. if (state === void 0) { state = ""; }
  3158. resolvable = is(Resolvable)(resolvable) ? resolvable : new Resolvable(resolvable);
  3159. var stateName = (typeof state === "string") ? state : state.name;
  3160. var topath = this._treeChanges.to;
  3161. var targetNode = find(topath, function (node) { return node.state.name === stateName; });
  3162. var resolveContext = new ResolveContext(topath);
  3163. resolveContext.addResolvables([resolvable], targetNode.state);
  3164. };
  3165. /**
  3166. * Gets the transition from which this transition was redirected.
  3167. *
  3168. * If the current transition is a redirect, this method returns the transition that was redirected.
  3169. *
  3170. * #### Example:
  3171. * ```js
  3172. * let transitionA = $state.go('A').transition
  3173. * transitionA.onStart({}, () => $state.target('B'));
  3174. * $transitions.onSuccess({ to: 'B' }, (trans) => {
  3175. * trans.to().name === 'B'; // true
  3176. * trans.redirectedFrom() === transitionA; // true
  3177. * });
  3178. * ```
  3179. *
  3180. * @returns The previous Transition, or null if this Transition is not the result of a redirection
  3181. */
  3182. Transition.prototype.redirectedFrom = function () {
  3183. return this._options.redirectedFrom || null;
  3184. };
  3185. /**
  3186. * Gets the original transition in a redirect chain
  3187. *
  3188. * A transition might belong to a long chain of multiple redirects.
  3189. * This method walks the [[redirectedFrom]] chain back to the original (first) transition in the chain.
  3190. *
  3191. * #### Example:
  3192. * ```js
  3193. * // states
  3194. * registry.register({ name: 'A', redirectTo: 'B' });
  3195. * registry.register({ name: 'B', redirectTo: 'C' });
  3196. * registry.register({ name: 'C', redirectTo: 'D' });
  3197. * registry.register({ name: 'D' });
  3198. *
  3199. * let transitionA = $state.go('A').transition
  3200. *
  3201. * $transitions.onSuccess({ to: 'D' }, (trans) => {
  3202. * trans.to().name === 'D'; // true
  3203. * trans.redirectedFrom().to().name === 'C'; // true
  3204. * trans.originalTransition() === transitionA; // true
  3205. * trans.originalTransition().to().name === 'A'; // true
  3206. * });
  3207. * ```
  3208. *
  3209. * @returns The original Transition that started a redirect chain
  3210. */
  3211. Transition.prototype.originalTransition = function () {
  3212. var rf = this.redirectedFrom();
  3213. return (rf && rf.originalTransition()) || this;
  3214. };
  3215. /**
  3216. * Get the transition options
  3217. *
  3218. * @returns the options for this Transition.
  3219. */
  3220. Transition.prototype.options = function () {
  3221. return this._options;
  3222. };
  3223. /**
  3224. * Gets the states being entered.
  3225. *
  3226. * @returns an array of states that will be entered during this transition.
  3227. */
  3228. Transition.prototype.entering = function () {
  3229. return map(this._treeChanges.entering, prop('state')).map(stateSelf);
  3230. };
  3231. /**
  3232. * Gets the states being exited.
  3233. *
  3234. * @returns an array of states that will be exited during this transition.
  3235. */
  3236. Transition.prototype.exiting = function () {
  3237. return map(this._treeChanges.exiting, prop('state')).map(stateSelf).reverse();
  3238. };
  3239. /**
  3240. * Gets the states being retained.
  3241. *
  3242. * @returns an array of states that are already entered from a previous Transition, that will not be
  3243. * exited during this Transition
  3244. */
  3245. Transition.prototype.retained = function () {
  3246. return map(this._treeChanges.retained, prop('state')).map(stateSelf);
  3247. };
  3248. /**
  3249. * Get the [[ViewConfig]]s associated with this Transition
  3250. *
  3251. * Each state can define one or more views (template/controller), which are encapsulated as `ViewConfig` objects.
  3252. * This method fetches the `ViewConfigs` for a given path in the Transition (e.g., "to" or "entering").
  3253. *
  3254. * @param pathname the name of the path to fetch views for:
  3255. * (`'to'`, `'from'`, `'entering'`, `'exiting'`, `'retained'`)
  3256. * @param state If provided, only returns the `ViewConfig`s for a single state in the path
  3257. *
  3258. * @returns a list of ViewConfig objects for the given path.
  3259. */
  3260. Transition.prototype.views = function (pathname, state) {
  3261. if (pathname === void 0) { pathname = "entering"; }
  3262. var path = this._treeChanges[pathname];
  3263. path = !state ? path : path.filter(propEq('state', state));
  3264. return path.map(prop("views")).filter(identity).reduce(unnestR, []);
  3265. };
  3266. Transition.prototype.treeChanges = function (pathname) {
  3267. return pathname ? this._treeChanges[pathname] : this._treeChanges;
  3268. };
  3269. /**
  3270. * Creates a new transition that is a redirection of the current one.
  3271. *
  3272. * This transition can be returned from a [[TransitionService]] hook to
  3273. * redirect a transition to a new state and/or set of parameters.
  3274. *
  3275. * @internalapi
  3276. *
  3277. * @returns Returns a new [[Transition]] instance.
  3278. */
  3279. Transition.prototype.redirect = function (targetState) {
  3280. var redirects = 1, trans = this;
  3281. while ((trans = trans.redirectedFrom()) != null) {
  3282. if (++redirects > 20)
  3283. throw new Error("Too many consecutive Transition redirects (20+)");
  3284. }
  3285. var redirectOpts = { redirectedFrom: this, source: "redirect" };
  3286. // If the original transition was caused by URL sync, then use { location: 'replace' }
  3287. // on the new transition (unless the target state explicitly specifies location: false).
  3288. // This causes the original url to be replaced with the url for the redirect target
  3289. // so the original url disappears from the browser history.
  3290. if (this.options().source === 'url' && targetState.options().location !== false) {
  3291. redirectOpts.location = 'replace';
  3292. }
  3293. var newOptions = extend({}, this.options(), targetState.options(), redirectOpts);
  3294. targetState = targetState.withOptions(newOptions, true);
  3295. var newTransition = this.router.transitionService.create(this._treeChanges.from, targetState);
  3296. var originalEnteringNodes = this._treeChanges.entering;
  3297. var redirectEnteringNodes = newTransition._treeChanges.entering;
  3298. // --- Re-use resolve data from original transition ---
  3299. // When redirecting from a parent state to a child state where the parent parameter values haven't changed
  3300. // (because of the redirect), the resolves fetched by the original transition are still valid in the
  3301. // redirected transition.
  3302. //
  3303. // This allows you to define a redirect on a parent state which depends on an async resolve value.
  3304. // You can wait for the resolve, then redirect to a child state based on the result.
  3305. // The redirected transition does not have to re-fetch the resolve.
  3306. // ---------------------------------------------------------
  3307. var nodeIsReloading = function (reloadState) { return function (node) {
  3308. return reloadState && node.state.includes[reloadState.name];
  3309. }; };
  3310. // Find any "entering" nodes in the redirect path that match the original path and aren't being reloaded
  3311. var matchingEnteringNodes = PathUtils.matching(redirectEnteringNodes, originalEnteringNodes, PathUtils.nonDynamicParams)
  3312. .filter(not(nodeIsReloading(targetState.options().reloadState)));
  3313. // Use the existing (possibly pre-resolved) resolvables for the matching entering nodes.
  3314. matchingEnteringNodes.forEach(function (node, idx) {
  3315. node.resolvables = originalEnteringNodes[idx].resolvables;
  3316. });
  3317. return newTransition;
  3318. };
  3319. /** @hidden If a transition doesn't exit/enter any states, returns any [[Param]] whose value changed */
  3320. Transition.prototype._changedParams = function () {
  3321. var tc = this._treeChanges;
  3322. /** Return undefined if it's not a "dynamic" transition, for the following reasons */
  3323. // If user explicitly wants a reload
  3324. if (this._options.reload)
  3325. return undefined;
  3326. // If any states are exiting or entering
  3327. if (tc.exiting.length || tc.entering.length)
  3328. return undefined;
  3329. // If to/from path lengths differ
  3330. if (tc.to.length !== tc.from.length)
  3331. return undefined;
  3332. // If the to/from paths are different
  3333. var pathsDiffer = arrayTuples(tc.to, tc.from)
  3334. .map(function (tuple) { return tuple[0].state !== tuple[1].state; })
  3335. .reduce(anyTrueR, false);
  3336. if (pathsDiffer)
  3337. return undefined;
  3338. // Find any parameter values that differ
  3339. var nodeSchemas = tc.to.map(function (node) { return node.paramSchema; });
  3340. var _a = [tc.to, tc.from].map(function (path) { return path.map(function (x) { return x.paramValues; }); }), toValues = _a[0], fromValues = _a[1];
  3341. var tuples = arrayTuples(nodeSchemas, toValues, fromValues);
  3342. return tuples.map(function (_a) {
  3343. var schema = _a[0], toVals = _a[1], fromVals = _a[2];
  3344. return Param.changed(schema, toVals, fromVals);
  3345. }).reduce(unnestR, []);
  3346. };
  3347. /**
  3348. * Returns true if the transition is dynamic.
  3349. *
  3350. * A transition is dynamic if no states are entered nor exited, but at least one dynamic parameter has changed.
  3351. *
  3352. * @returns true if the Transition is dynamic
  3353. */
  3354. Transition.prototype.dynamic = function () {
  3355. var changes = this._changedParams();
  3356. return !changes ? false : changes.map(function (x) { return x.dynamic; }).reduce(anyTrueR, false);
  3357. };
  3358. /**
  3359. * Returns true if the transition is ignored.
  3360. *
  3361. * A transition is ignored if no states are entered nor exited, and no parameter values have changed.
  3362. *
  3363. * @returns true if the Transition is ignored.
  3364. */
  3365. Transition.prototype.ignored = function () {
  3366. return !!this._ignoredReason();
  3367. };
  3368. /** @hidden */
  3369. Transition.prototype._ignoredReason = function () {
  3370. var pending = this.router.globals.transition;
  3371. var reloadState = this._options.reloadState;
  3372. var same = function (pathA, pathB) {
  3373. if (pathA.length !== pathB.length)
  3374. return false;
  3375. var matching = PathUtils.matching(pathA, pathB);
  3376. return pathA.length === matching.filter(function (node) { return !reloadState || !node.state.includes[reloadState.name]; }).length;
  3377. };
  3378. var newTC = this.treeChanges();
  3379. var pendTC = pending && pending.treeChanges();
  3380. if (pendTC && same(pendTC.to, newTC.to) && same(pendTC.exiting, newTC.exiting))
  3381. return "SameAsPending";
  3382. if (newTC.exiting.length === 0 && newTC.entering.length === 0 && same(newTC.from, newTC.to))
  3383. return "SameAsCurrent";
  3384. };
  3385. /**
  3386. * Runs the transition
  3387. *
  3388. * This method is generally called from the [[StateService.transitionTo]]
  3389. *
  3390. * @internalapi
  3391. *
  3392. * @returns a promise for a successful transition.
  3393. */
  3394. Transition.prototype.run = function () {
  3395. var _this = this;
  3396. var runAllHooks = TransitionHook.runAllHooks;
  3397. // Gets transition hooks array for the given phase
  3398. var getHooksFor = function (phase) {
  3399. return _this._hookBuilder.buildHooksForPhase(phase);
  3400. };
  3401. // When the chain is complete, then resolve or reject the deferred
  3402. var transitionSuccess = function () {
  3403. trace.traceSuccess(_this.$to(), _this);
  3404. _this.success = true;
  3405. _this._deferred.resolve(_this.to());
  3406. runAllHooks(getHooksFor(exports.TransitionHookPhase.SUCCESS));
  3407. };
  3408. var transitionError = function (reason) {
  3409. trace.traceError(reason, _this);
  3410. _this.success = false;
  3411. _this._deferred.reject(reason);
  3412. _this._error = reason;
  3413. runAllHooks(getHooksFor(exports.TransitionHookPhase.ERROR));
  3414. };
  3415. var runTransition = function () {
  3416. // Wait to build the RUN hook chain until the BEFORE hooks are done
  3417. // This allows a BEFORE hook to dynamically add additional RUN hooks via the Transition object.
  3418. var allRunHooks = getHooksFor(exports.TransitionHookPhase.RUN);
  3419. var done = function () { return services.$q.when(undefined); };
  3420. return TransitionHook.invokeHooks(allRunHooks, done);
  3421. };
  3422. var startTransition = function () {
  3423. var globals = _this.router.globals;
  3424. globals.lastStartedTransitionId = _this.$id;
  3425. globals.transition = _this;
  3426. globals.transitionHistory.enqueue(_this);
  3427. trace.traceTransitionStart(_this);
  3428. return services.$q.when(undefined);
  3429. };
  3430. var allBeforeHooks = getHooksFor(exports.TransitionHookPhase.BEFORE);
  3431. TransitionHook.invokeHooks(allBeforeHooks, startTransition)
  3432. .then(runTransition)
  3433. .then(transitionSuccess, transitionError);
  3434. return this.promise;
  3435. };
  3436. /**
  3437. * Checks if the Transition is valid
  3438. *
  3439. * @returns true if the Transition is valid
  3440. */
  3441. Transition.prototype.valid = function () {
  3442. return !this.error() || this.success !== undefined;
  3443. };
  3444. /**
  3445. * Aborts this transition
  3446. *
  3447. * Imperative API to abort a Transition.
  3448. * This only applies to Transitions that are not yet complete.
  3449. */
  3450. Transition.prototype.abort = function () {
  3451. // Do not set flag if the transition is already complete
  3452. if (isUndefined(this.success)) {
  3453. this._aborted = true;
  3454. }
  3455. };
  3456. /**
  3457. * The Transition error reason.
  3458. *
  3459. * If the transition is invalid (and could not be run), returns the reason the transition is invalid.
  3460. * If the transition was valid and ran, but was not successful, returns the reason the transition failed.
  3461. *
  3462. * @returns an error message explaining why the transition is invalid, or the reason the transition failed.
  3463. */
  3464. Transition.prototype.error = function () {
  3465. var state = this.$to();
  3466. if (state.self.abstract)
  3467. return "Cannot transition to abstract state '" + state.name + "'";
  3468. var paramDefs = state.parameters(), values$$1 = this.params();
  3469. var invalidParams = paramDefs.filter(function (param) { return !param.validates(values$$1[param.id]); });
  3470. if (invalidParams.length) {
  3471. return "Param values not valid for state '" + state.name + "'. Invalid params: [ " + invalidParams.map(function (param) { return param.id; }).join(', ') + " ]";
  3472. }
  3473. if (this.success === false)
  3474. return this._error;
  3475. };
  3476. /**
  3477. * A string representation of the Transition
  3478. *
  3479. * @returns A string representation of the Transition
  3480. */
  3481. Transition.prototype.toString = function () {
  3482. var fromStateOrName = this.from();
  3483. var toStateOrName = this.to();
  3484. var avoidEmptyHash = function (params) {
  3485. return (params["#"] !== null && params["#"] !== undefined) ? params : omit(params, ["#"]);
  3486. };
  3487. // (X) means the to state is invalid.
  3488. var id = this.$id, from = isObject(fromStateOrName) ? fromStateOrName.name : fromStateOrName, fromParams = stringify(avoidEmptyHash(this._treeChanges.from.map(prop('paramValues')).reduce(mergeR, {}))), toValid = this.valid() ? "" : "(X) ", to = isObject(toStateOrName) ? toStateOrName.name : toStateOrName, toParams = stringify(avoidEmptyHash(this.params()));
  3489. return "Transition#" + id + "( '" + from + "'" + fromParams + " -> " + toValid + "'" + to + "'" + toParams + " )";
  3490. };
  3491. /** @hidden */
  3492. Transition.diToken = Transition;
  3493. return Transition;
  3494. }());
  3495. /**
  3496. * Functions that manipulate strings
  3497. *
  3498. * Although these functions are exported, they are subject to change without notice.
  3499. *
  3500. * @module common_strings
  3501. */ /** */
  3502. /**
  3503. * Returns a string shortened to a maximum length
  3504. *
  3505. * If the string is already less than the `max` length, return the string.
  3506. * Else return the string, shortened to `max - 3` and append three dots ("...").
  3507. *
  3508. * @param max the maximum length of the string to return
  3509. * @param str the input string
  3510. */
  3511. function maxLength(max, str) {
  3512. if (str.length <= max)
  3513. return str;
  3514. return str.substr(0, max - 3) + "...";
  3515. }
  3516. /**
  3517. * Returns a string, with spaces added to the end, up to a desired str length
  3518. *
  3519. * If the string is already longer than the desired length, return the string.
  3520. * Else returns the string, with extra spaces on the end, such that it reaches `length` characters.
  3521. *
  3522. * @param length the desired length of the string to return
  3523. * @param str the input string
  3524. */
  3525. function padString(length, str) {
  3526. while (str.length < length)
  3527. str += " ";
  3528. return str;
  3529. }
  3530. function kebobString(camelCase) {
  3531. return camelCase
  3532. .replace(/^([A-Z])/, function ($1) { return $1.toLowerCase(); }) // replace first char
  3533. .replace(/([A-Z])/g, function ($1) { return "-" + $1.toLowerCase(); }); // replace rest
  3534. }
  3535. function functionToString(fn) {
  3536. var fnStr = fnToString(fn);
  3537. var namedFunctionMatch = fnStr.match(/^(function [^ ]+\([^)]*\))/);
  3538. var toStr = namedFunctionMatch ? namedFunctionMatch[1] : fnStr;
  3539. var fnName = fn['name'] || "";
  3540. if (fnName && toStr.match(/function \(/)) {
  3541. return 'function ' + fnName + toStr.substr(9);
  3542. }
  3543. return toStr;
  3544. }
  3545. function fnToString(fn) {
  3546. var _fn = isArray(fn) ? fn.slice(-1)[0] : fn;
  3547. return _fn && _fn.toString() || "undefined";
  3548. }
  3549. var stringifyPatternFn = null;
  3550. var stringifyPattern = function (value) {
  3551. var isRejection = Rejection.isRejectionPromise;
  3552. stringifyPatternFn = stringifyPatternFn || pattern([
  3553. [not(isDefined), val("undefined")],
  3554. [isNull, val("null")],
  3555. [isPromise, val("[Promise]")],
  3556. [isRejection, function (x) { return x._transitionRejection.toString(); }],
  3557. [is(Rejection), invoke("toString")],
  3558. [is(Transition), invoke("toString")],
  3559. [is(Resolvable), invoke("toString")],
  3560. [isInjectable, functionToString],
  3561. [val(true), identity]
  3562. ]);
  3563. return stringifyPatternFn(value);
  3564. };
  3565. function stringify(o) {
  3566. var seen = [];
  3567. function format(val$$1) {
  3568. if (isObject(val$$1)) {
  3569. if (seen.indexOf(val$$1) !== -1)
  3570. return '[circular ref]';
  3571. seen.push(val$$1);
  3572. }
  3573. return stringifyPattern(val$$1);
  3574. }
  3575. return JSON.stringify(o, function (key, val$$1) { return format(val$$1); }).replace(/\\"/g, '"');
  3576. }
  3577. /** Returns a function that splits a string on a character or substring */
  3578. var beforeAfterSubstr = function (char) { return function (str) {
  3579. if (!str)
  3580. return ["", ""];
  3581. var idx = str.indexOf(char);
  3582. if (idx === -1)
  3583. return [str, ""];
  3584. return [str.substr(0, idx), str.substr(idx + 1)];
  3585. }; };
  3586. var hostRegex = new RegExp('^(?:[a-z]+:)?//[^/]+/');
  3587. var stripFile = function (str) { return str.replace(/\/[^/]*$/, ''); };
  3588. var splitHash = beforeAfterSubstr("#");
  3589. var splitQuery = beforeAfterSubstr("?");
  3590. var splitEqual = beforeAfterSubstr("=");
  3591. var trimHashVal = function (str) { return str ? str.replace(/^#/, "") : ""; };
  3592. /**
  3593. * Splits on a delimiter, but returns the delimiters in the array
  3594. *
  3595. * #### Example:
  3596. * ```js
  3597. * var splitOnSlashes = splitOnDelim('/');
  3598. * splitOnSlashes("/foo"); // ["/", "foo"]
  3599. * splitOnSlashes("/foo/"); // ["/", "foo", "/"]
  3600. * ```
  3601. */
  3602. function splitOnDelim(delim) {
  3603. var re = new RegExp("(" + delim + ")", "g");
  3604. return function (str) {
  3605. return str.split(re).filter(identity);
  3606. };
  3607. }
  3608. /**
  3609. * Reduce fn that joins neighboring strings
  3610. *
  3611. * Given an array of strings, returns a new array
  3612. * where all neighboring strings have been joined.
  3613. *
  3614. * #### Example:
  3615. * ```js
  3616. * let arr = ["foo", "bar", 1, "baz", "", "qux" ];
  3617. * arr.reduce(joinNeighborsR, []) // ["foobar", 1, "bazqux" ]
  3618. * ```
  3619. */
  3620. function joinNeighborsR(acc, x) {
  3621. if (isString(tail(acc)) && isString(x))
  3622. return acc.slice(0, -1).concat(tail(acc) + x);
  3623. return pushR(acc, x);
  3624. }
  3625. /** @module common */ /** for typedoc */
  3626. /**
  3627. * @coreapi
  3628. * @module params
  3629. */
  3630. /** */
  3631. /**
  3632. * A registry for parameter types.
  3633. *
  3634. * This registry manages the built-in (and custom) parameter types.
  3635. *
  3636. * The built-in parameter types are:
  3637. *
  3638. * - [[string]]
  3639. * - [[path]]
  3640. * - [[query]]
  3641. * - [[hash]]
  3642. * - [[int]]
  3643. * - [[bool]]
  3644. * - [[date]]
  3645. * - [[json]]
  3646. * - [[any]]
  3647. */
  3648. var ParamTypes = /** @class */ (function () {
  3649. /** @internalapi */
  3650. function ParamTypes() {
  3651. /** @hidden */
  3652. this.enqueue = true;
  3653. /** @hidden */
  3654. this.typeQueue = [];
  3655. /** @internalapi */
  3656. this.defaultTypes = pick(ParamTypes.prototype, ["hash", "string", "query", "path", "int", "bool", "date", "json", "any"]);
  3657. // Register default types. Store them in the prototype of this.types.
  3658. var makeType = function (definition, name) {
  3659. return new ParamType(extend({ name: name }, definition));
  3660. };
  3661. this.types = inherit(map(this.defaultTypes, makeType), {});
  3662. }
  3663. /** @internalapi */
  3664. ParamTypes.prototype.dispose = function () {
  3665. this.types = {};
  3666. };
  3667. /**
  3668. * Registers a parameter type
  3669. *
  3670. * End users should call [[UrlMatcherFactory.type]], which delegates to this method.
  3671. */
  3672. ParamTypes.prototype.type = function (name, definition, definitionFn) {
  3673. if (!isDefined(definition))
  3674. return this.types[name];
  3675. if (this.types.hasOwnProperty(name))
  3676. throw new Error("A type named '" + name + "' has already been defined.");
  3677. this.types[name] = new ParamType(extend({ name: name }, definition));
  3678. if (definitionFn) {
  3679. this.typeQueue.push({ name: name, def: definitionFn });
  3680. if (!this.enqueue)
  3681. this._flushTypeQueue();
  3682. }
  3683. return this;
  3684. };
  3685. /** @internalapi */
  3686. ParamTypes.prototype._flushTypeQueue = function () {
  3687. while (this.typeQueue.length) {
  3688. var type = this.typeQueue.shift();
  3689. if (type.pattern)
  3690. throw new Error("You cannot override a type's .pattern at runtime.");
  3691. extend(this.types[type.name], services.$injector.invoke(type.def));
  3692. }
  3693. };
  3694. return ParamTypes;
  3695. }());
  3696. /** @hidden */
  3697. function initDefaultTypes() {
  3698. var makeDefaultType = function (def) {
  3699. var valToString = function (val$$1) {
  3700. return val$$1 != null ? val$$1.toString() : val$$1;
  3701. };
  3702. var defaultTypeBase = {
  3703. encode: valToString,
  3704. decode: valToString,
  3705. is: is(String),
  3706. pattern: /.*/,
  3707. equals: function (a, b) { return a == b; },
  3708. };
  3709. return extend({}, defaultTypeBase, def);
  3710. };
  3711. // Default Parameter Type Definitions
  3712. extend(ParamTypes.prototype, {
  3713. string: makeDefaultType({}),
  3714. path: makeDefaultType({
  3715. pattern: /[^/]*/,
  3716. }),
  3717. query: makeDefaultType({}),
  3718. hash: makeDefaultType({
  3719. inherit: false,
  3720. }),
  3721. int: makeDefaultType({
  3722. decode: function (val$$1) { return parseInt(val$$1, 10); },
  3723. is: function (val$$1) {
  3724. return !isNullOrUndefined(val$$1) && this.decode(val$$1.toString()) === val$$1;
  3725. },
  3726. pattern: /-?\d+/,
  3727. }),
  3728. bool: makeDefaultType({
  3729. encode: function (val$$1) { return val$$1 && 1 || 0; },
  3730. decode: function (val$$1) { return parseInt(val$$1, 10) !== 0; },
  3731. is: is(Boolean),
  3732. pattern: /0|1/,
  3733. }),
  3734. date: makeDefaultType({
  3735. encode: function (val$$1) {
  3736. return !this.is(val$$1) ? undefined : [
  3737. val$$1.getFullYear(),
  3738. ('0' + (val$$1.getMonth() + 1)).slice(-2),
  3739. ('0' + val$$1.getDate()).slice(-2),
  3740. ].join("-");
  3741. },
  3742. decode: function (val$$1) {
  3743. if (this.is(val$$1))
  3744. return val$$1;
  3745. var match = this.capture.exec(val$$1);
  3746. return match ? new Date(match[1], match[2] - 1, match[3]) : undefined;
  3747. },
  3748. is: function (val$$1) { return val$$1 instanceof Date && !isNaN(val$$1.valueOf()); },
  3749. equals: function (l, r) {
  3750. return ['getFullYear', 'getMonth', 'getDate']
  3751. .reduce(function (acc, fn) { return acc && l[fn]() === r[fn](); }, true);
  3752. },
  3753. pattern: /[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])/,
  3754. capture: /([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/,
  3755. }),
  3756. json: makeDefaultType({
  3757. encode: toJson,
  3758. decode: fromJson,
  3759. is: is(Object),
  3760. equals: equals,
  3761. pattern: /[^/]*/,
  3762. }),
  3763. // does not encode/decode
  3764. any: makeDefaultType({
  3765. encode: identity,
  3766. decode: identity,
  3767. is: function () { return true; },
  3768. equals: equals,
  3769. }),
  3770. });
  3771. }
  3772. initDefaultTypes();
  3773. /**
  3774. * @coreapi
  3775. * @module params
  3776. */
  3777. /** */
  3778. /** @internalapi */
  3779. var StateParams = /** @class */ (function () {
  3780. function StateParams(params) {
  3781. if (params === void 0) { params = {}; }
  3782. extend(this, params);
  3783. }
  3784. /**
  3785. * Merges a set of parameters with all parameters inherited between the common parents of the
  3786. * current state and a given destination state.
  3787. *
  3788. * @param {Object} newParams The set of parameters which will be composited with inherited params.
  3789. * @param {Object} $current Internal definition of object representing the current state.
  3790. * @param {Object} $to Internal definition of object representing state to transition to.
  3791. */
  3792. StateParams.prototype.$inherit = function (newParams, $current, $to) {
  3793. var parents = ancestors($current, $to), parentParams, inherited = {}, inheritList = [];
  3794. for (var i in parents) {
  3795. if (!parents[i] || !parents[i].params)
  3796. continue;
  3797. parentParams = Object.keys(parents[i].params);
  3798. if (!parentParams.length)
  3799. continue;
  3800. for (var j in parentParams) {
  3801. if (inheritList.indexOf(parentParams[j]) >= 0)
  3802. continue;
  3803. inheritList.push(parentParams[j]);
  3804. inherited[parentParams[j]] = this[parentParams[j]];
  3805. }
  3806. }
  3807. return extend({}, inherited, newParams);
  3808. };
  3809. return StateParams;
  3810. }());
  3811. /** @module path */ /** for typedoc */
  3812. /** @module resolve */ /** for typedoc */
  3813. /** @module state */ /** for typedoc */
  3814. var parseUrl = function (url) {
  3815. if (!isString(url))
  3816. return false;
  3817. var root$$1 = url.charAt(0) === '^';
  3818. return { val: root$$1 ? url.substring(1) : url, root: root$$1 };
  3819. };
  3820. function nameBuilder(state) {
  3821. return state.name;
  3822. }
  3823. function selfBuilder(state) {
  3824. state.self.$$state = function () { return state; };
  3825. return state.self;
  3826. }
  3827. function dataBuilder(state) {
  3828. if (state.parent && state.parent.data) {
  3829. state.data = state.self.data = inherit(state.parent.data, state.data);
  3830. }
  3831. return state.data;
  3832. }
  3833. var getUrlBuilder = function ($urlMatcherFactoryProvider, root$$1) {
  3834. return function urlBuilder(state) {
  3835. var stateDec = state;
  3836. // For future states, i.e., states whose name ends with `.**`,
  3837. // match anything that starts with the url prefix
  3838. if (stateDec && stateDec.url && stateDec.name && stateDec.name.match(/\.\*\*$/)) {
  3839. stateDec.url += "{remainder:any}"; // match any path (.*)
  3840. }
  3841. var parsed = parseUrl(stateDec.url), parent = state.parent;
  3842. var url = !parsed ? stateDec.url : $urlMatcherFactoryProvider.compile(parsed.val, {
  3843. params: state.params || {},
  3844. paramMap: function (paramConfig, isSearch) {
  3845. if (stateDec.reloadOnSearch === false && isSearch)
  3846. paramConfig = extend(paramConfig || {}, { dynamic: true });
  3847. return paramConfig;
  3848. }
  3849. });
  3850. if (!url)
  3851. return null;
  3852. if (!$urlMatcherFactoryProvider.isMatcher(url))
  3853. throw new Error("Invalid url '" + url + "' in state '" + state + "'");
  3854. return (parsed && parsed.root) ? url : ((parent && parent.navigable) || root$$1()).url.append(url);
  3855. };
  3856. };
  3857. var getNavigableBuilder = function (isRoot) {
  3858. return function navigableBuilder(state) {
  3859. return !isRoot(state) && state.url ? state : (state.parent ? state.parent.navigable : null);
  3860. };
  3861. };
  3862. var getParamsBuilder = function (paramFactory) {
  3863. return function paramsBuilder(state) {
  3864. var makeConfigParam = function (config, id) { return paramFactory.fromConfig(id, null, config); };
  3865. var urlParams = (state.url && state.url.parameters({ inherit: false })) || [];
  3866. var nonUrlParams = values(mapObj(omit(state.params || {}, urlParams.map(prop('id'))), makeConfigParam));
  3867. return urlParams.concat(nonUrlParams).map(function (p) { return [p.id, p]; }).reduce(applyPairs, {});
  3868. };
  3869. };
  3870. function pathBuilder(state) {
  3871. return state.parent ? state.parent.path.concat(state) : /*root*/ [state];
  3872. }
  3873. function includesBuilder(state) {
  3874. var includes = state.parent ? extend({}, state.parent.includes) : {};
  3875. includes[state.name] = true;
  3876. return includes;
  3877. }
  3878. /**
  3879. * This is a [[StateBuilder.builder]] function for the `resolve:` block on a [[StateDeclaration]].
  3880. *
  3881. * When the [[StateBuilder]] builds a [[StateObject]] object from a raw [[StateDeclaration]], this builder
  3882. * validates the `resolve` property and converts it to a [[Resolvable]] array.
  3883. *
  3884. * resolve: input value can be:
  3885. *
  3886. * {
  3887. * // analyzed but not injected
  3888. * myFooResolve: function() { return "myFooData"; },
  3889. *
  3890. * // function.toString() parsed, "DependencyName" dep as string (not min-safe)
  3891. * myBarResolve: function(DependencyName) { return DependencyName.fetchSomethingAsPromise() },
  3892. *
  3893. * // Array split; "DependencyName" dep as string
  3894. * myBazResolve: [ "DependencyName", function(dep) { return dep.fetchSomethingAsPromise() },
  3895. *
  3896. * // Array split; DependencyType dep as token (compared using ===)
  3897. * myQuxResolve: [ DependencyType, function(dep) { return dep.fetchSometingAsPromise() },
  3898. *
  3899. * // val.$inject used as deps
  3900. * // where:
  3901. * // corgeResolve.$inject = ["DependencyName"];
  3902. * // function corgeResolve(dep) { dep.fetchSometingAsPromise() }
  3903. * // then "DependencyName" dep as string
  3904. * myCorgeResolve: corgeResolve,
  3905. *
  3906. * // inject service by name
  3907. * // When a string is found, desugar creating a resolve that injects the named service
  3908. * myGraultResolve: "SomeService"
  3909. * }
  3910. *
  3911. * or:
  3912. *
  3913. * [
  3914. * new Resolvable("myFooResolve", function() { return "myFooData" }),
  3915. * new Resolvable("myBarResolve", function(dep) { return dep.fetchSomethingAsPromise() }, [ "DependencyName" ]),
  3916. * { provide: "myBazResolve", useFactory: function(dep) { dep.fetchSomethingAsPromise() }, deps: [ "DependencyName" ] }
  3917. * ]
  3918. */
  3919. function resolvablesBuilder(state) {
  3920. /** convert resolve: {} and resolvePolicy: {} objects to an array of tuples */
  3921. var objects2Tuples = function (resolveObj, resolvePolicies) {
  3922. return Object.keys(resolveObj || {}).map(function (token) { return ({ token: token, val: resolveObj[token], deps: undefined, policy: resolvePolicies[token] }); });
  3923. };
  3924. /** fetch DI annotations from a function or ng1-style array */
  3925. var annotate = function (fn) {
  3926. var $injector = services.$injector;
  3927. // ng1 doesn't have an $injector until runtime.
  3928. // If the $injector doesn't exist, use "deferred" literal as a
  3929. // marker indicating they should be annotated when runtime starts
  3930. return fn['$inject'] || ($injector && $injector.annotate(fn, $injector.strictDi)) || "deferred";
  3931. };
  3932. /** true if the object has both `token` and `resolveFn`, and is probably a [[ResolveLiteral]] */
  3933. var isResolveLiteral = function (obj) { return !!(obj.token && obj.resolveFn); };
  3934. /** true if the object looks like a provide literal, or a ng2 Provider */
  3935. var isLikeNg2Provider = function (obj) { return !!((obj.provide || obj.token) && (obj.useValue || obj.useFactory || obj.useExisting || obj.useClass)); };
  3936. /** true if the object looks like a tuple from obj2Tuples */
  3937. var isTupleFromObj = function (obj) { return !!(obj && obj.val && (isString(obj.val) || isArray(obj.val) || isFunction(obj.val))); };
  3938. /** extracts the token from a Provider or provide literal */
  3939. var token = function (p) { return p.provide || p.token; };
  3940. /** Given a literal resolve or provider object, returns a Resolvable */
  3941. var literal2Resolvable = pattern([
  3942. [prop('resolveFn'), function (p) { return new Resolvable(token(p), p.resolveFn, p.deps, p.policy); }],
  3943. [prop('useFactory'), function (p) { return new Resolvable(token(p), p.useFactory, (p.deps || p.dependencies), p.policy); }],
  3944. [prop('useClass'), function (p) { return new Resolvable(token(p), function () { return new p.useClass(); }, [], p.policy); }],
  3945. [prop('useValue'), function (p) { return new Resolvable(token(p), function () { return p.useValue; }, [], p.policy, p.useValue); }],
  3946. [prop('useExisting'), function (p) { return new Resolvable(token(p), identity, [p.useExisting], p.policy); }],
  3947. ]);
  3948. var tuple2Resolvable = pattern([
  3949. [pipe(prop("val"), isString), function (tuple) { return new Resolvable(tuple.token, identity, [tuple.val], tuple.policy); }],
  3950. [pipe(prop("val"), isArray), function (tuple) { return new Resolvable(tuple.token, tail(tuple.val), tuple.val.slice(0, -1), tuple.policy); }],
  3951. [pipe(prop("val"), isFunction), function (tuple) { return new Resolvable(tuple.token, tuple.val, annotate(tuple.val), tuple.policy); }],
  3952. ]);
  3953. var item2Resolvable = pattern([
  3954. [is(Resolvable), function (r) { return r; }],
  3955. [isResolveLiteral, literal2Resolvable],
  3956. [isLikeNg2Provider, literal2Resolvable],
  3957. [isTupleFromObj, tuple2Resolvable],
  3958. [val(true), function (obj) { throw new Error("Invalid resolve value: " + stringify(obj)); }]
  3959. ]);
  3960. // If resolveBlock is already an array, use it as-is.
  3961. // Otherwise, assume it's an object and convert to an Array of tuples
  3962. var decl = state.resolve;
  3963. var items = isArray(decl) ? decl : objects2Tuples(decl, state.resolvePolicy || {});
  3964. return items.map(item2Resolvable);
  3965. }
  3966. /**
  3967. * @internalapi A internal global service
  3968. *
  3969. * StateBuilder is a factory for the internal [[StateObject]] objects.
  3970. *
  3971. * When you register a state with the [[StateRegistry]], you register a plain old javascript object which
  3972. * conforms to the [[StateDeclaration]] interface. This factory takes that object and builds the corresponding
  3973. * [[StateObject]] object, which has an API and is used internally.
  3974. *
  3975. * Custom properties or API may be added to the internal [[StateObject]] object by registering a decorator function
  3976. * using the [[builder]] method.
  3977. */
  3978. var StateBuilder = /** @class */ (function () {
  3979. function StateBuilder(matcher, urlMatcherFactory) {
  3980. this.matcher = matcher;
  3981. var self = this;
  3982. var root$$1 = function () { return matcher.find(""); };
  3983. var isRoot = function (state) { return state.name === ""; };
  3984. function parentBuilder(state) {
  3985. if (isRoot(state))
  3986. return null;
  3987. return matcher.find(self.parentName(state)) || root$$1();
  3988. }
  3989. this.builders = {
  3990. name: [nameBuilder],
  3991. self: [selfBuilder],
  3992. parent: [parentBuilder],
  3993. data: [dataBuilder],
  3994. // Build a URLMatcher if necessary, either via a relative or absolute URL
  3995. url: [getUrlBuilder(urlMatcherFactory, root$$1)],
  3996. // Keep track of the closest ancestor state that has a URL (i.e. is navigable)
  3997. navigable: [getNavigableBuilder(isRoot)],
  3998. params: [getParamsBuilder(urlMatcherFactory.paramFactory)],
  3999. // Each framework-specific ui-router implementation should define its own `views` builder
  4000. // e.g., src/ng1/statebuilders/views.ts
  4001. views: [],
  4002. // Keep a full path from the root down to this state as this is needed for state activation.
  4003. path: [pathBuilder],
  4004. // Speed up $state.includes() as it's used a lot
  4005. includes: [includesBuilder],
  4006. resolvables: [resolvablesBuilder]
  4007. };
  4008. }
  4009. /**
  4010. * Registers a [[BuilderFunction]] for a specific [[StateObject]] property (e.g., `parent`, `url`, or `path`).
  4011. * More than one BuilderFunction can be registered for a given property.
  4012. *
  4013. * The BuilderFunction(s) will be used to define the property on any subsequently built [[StateObject]] objects.
  4014. *
  4015. * @param name The name of the State property being registered for.
  4016. * @param fn The BuilderFunction which will be used to build the State property
  4017. * @returns a function which deregisters the BuilderFunction
  4018. */
  4019. StateBuilder.prototype.builder = function (name, fn) {
  4020. var builders = this.builders;
  4021. var array = builders[name] || [];
  4022. // Backwards compat: if only one builder exists, return it, else return whole arary.
  4023. if (isString(name) && !isDefined(fn))
  4024. return array.length > 1 ? array : array[0];
  4025. if (!isString(name) || !isFunction(fn))
  4026. return;
  4027. builders[name] = array;
  4028. builders[name].push(fn);
  4029. return function () { return builders[name].splice(builders[name].indexOf(fn, 1)) && null; };
  4030. };
  4031. /**
  4032. * Builds all of the properties on an essentially blank State object, returning a State object which has all its
  4033. * properties and API built.
  4034. *
  4035. * @param state an uninitialized State object
  4036. * @returns the built State object
  4037. */
  4038. StateBuilder.prototype.build = function (state) {
  4039. var _a = this, matcher = _a.matcher, builders = _a.builders;
  4040. var parent = this.parentName(state);
  4041. if (parent && !matcher.find(parent, undefined, false)) {
  4042. return null;
  4043. }
  4044. for (var key in builders) {
  4045. if (!builders.hasOwnProperty(key))
  4046. continue;
  4047. var chain = builders[key].reduce(function (parentFn, step) { return function (_state) { return step(_state, parentFn); }; }, noop$1);
  4048. state[key] = chain(state);
  4049. }
  4050. return state;
  4051. };
  4052. StateBuilder.prototype.parentName = function (state) {
  4053. // name = 'foo.bar.baz.**'
  4054. var name = state.name || "";
  4055. // segments = ['foo', 'bar', 'baz', '.**']
  4056. var segments = name.split('.');
  4057. // segments = ['foo', 'bar', 'baz']
  4058. var lastSegment = segments.pop();
  4059. // segments = ['foo', 'bar'] (ignore .** segment for future states)
  4060. if (lastSegment === '**')
  4061. segments.pop();
  4062. if (segments.length) {
  4063. if (state.parent) {
  4064. throw new Error("States that specify the 'parent:' property should not have a '.' in their name (" + name + ")");
  4065. }
  4066. // 'foo.bar'
  4067. return segments.join(".");
  4068. }
  4069. if (!state.parent)
  4070. return "";
  4071. return isString(state.parent) ? state.parent : state.parent.name;
  4072. };
  4073. StateBuilder.prototype.name = function (state) {
  4074. var name = state.name;
  4075. if (name.indexOf('.') !== -1 || !state.parent)
  4076. return name;
  4077. var parentName = isString(state.parent) ? state.parent : state.parent.name;
  4078. return parentName ? parentName + "." + name : name;
  4079. };
  4080. return StateBuilder;
  4081. }());
  4082. /** @module state */ /** for typedoc */
  4083. var StateMatcher = /** @class */ (function () {
  4084. function StateMatcher(_states) {
  4085. this._states = _states;
  4086. }
  4087. StateMatcher.prototype.isRelative = function (stateName) {
  4088. stateName = stateName || "";
  4089. return stateName.indexOf(".") === 0 || stateName.indexOf("^") === 0;
  4090. };
  4091. StateMatcher.prototype.find = function (stateOrName, base, matchGlob) {
  4092. if (matchGlob === void 0) { matchGlob = true; }
  4093. if (!stateOrName && stateOrName !== "")
  4094. return undefined;
  4095. var isStr = isString(stateOrName);
  4096. var name = isStr ? stateOrName : stateOrName.name;
  4097. if (this.isRelative(name))
  4098. name = this.resolvePath(name, base);
  4099. var state = this._states[name];
  4100. if (state && (isStr || (!isStr && (state === stateOrName || state.self === stateOrName)))) {
  4101. return state;
  4102. }
  4103. else if (isStr && matchGlob) {
  4104. var _states = values(this._states);
  4105. var matches = _states.filter(function (state) {
  4106. return state.__stateObjectCache.nameGlob &&
  4107. state.__stateObjectCache.nameGlob.matches(name);
  4108. });
  4109. if (matches.length > 1) {
  4110. console.log("stateMatcher.find: Found multiple matches for " + name + " using glob: ", matches.map(function (match) { return match.name; }));
  4111. }
  4112. return matches[0];
  4113. }
  4114. return undefined;
  4115. };
  4116. StateMatcher.prototype.resolvePath = function (name, base) {
  4117. if (!base)
  4118. throw new Error("No reference point given for path '" + name + "'");
  4119. var baseState = this.find(base);
  4120. var splitName = name.split("."), i = 0, pathLength = splitName.length, current = baseState;
  4121. for (; i < pathLength; i++) {
  4122. if (splitName[i] === "" && i === 0) {
  4123. current = baseState;
  4124. continue;
  4125. }
  4126. if (splitName[i] === "^") {
  4127. if (!current.parent)
  4128. throw new Error("Path '" + name + "' not valid for state '" + baseState.name + "'");
  4129. current = current.parent;
  4130. continue;
  4131. }
  4132. break;
  4133. }
  4134. var relName = splitName.slice(i).join(".");
  4135. return current.name + (current.name && relName ? "." : "") + relName;
  4136. };
  4137. return StateMatcher;
  4138. }());
  4139. /** @module state */ /** for typedoc */
  4140. /** @internalapi */
  4141. var StateQueueManager = /** @class */ (function () {
  4142. function StateQueueManager($registry, $urlRouter, states, builder, listeners) {
  4143. this.$registry = $registry;
  4144. this.$urlRouter = $urlRouter;
  4145. this.states = states;
  4146. this.builder = builder;
  4147. this.listeners = listeners;
  4148. this.queue = [];
  4149. this.matcher = $registry.matcher;
  4150. }
  4151. /** @internalapi */
  4152. StateQueueManager.prototype.dispose = function () {
  4153. this.queue = [];
  4154. };
  4155. StateQueueManager.prototype.register = function (stateDecl) {
  4156. var queue = this.queue;
  4157. var state = StateObject.create(stateDecl);
  4158. var name = state.name;
  4159. if (!isString(name))
  4160. throw new Error("State must have a valid name");
  4161. if (this.states.hasOwnProperty(name) || inArray(queue.map(prop('name')), name))
  4162. throw new Error("State '" + name + "' is already defined");
  4163. queue.push(state);
  4164. this.flush();
  4165. return state;
  4166. };
  4167. StateQueueManager.prototype.flush = function () {
  4168. var _this = this;
  4169. var _a = this, queue = _a.queue, states = _a.states, builder = _a.builder;
  4170. var registered = [], // states that got registered
  4171. orphans = [], // states that don't yet have a parent registered
  4172. previousQueueLength = {}; // keep track of how long the queue when an orphan was first encountered
  4173. var getState = function (name) {
  4174. return _this.states.hasOwnProperty(name) && _this.states[name];
  4175. };
  4176. while (queue.length > 0) {
  4177. var state = queue.shift();
  4178. var name_1 = state.name;
  4179. var result = builder.build(state);
  4180. var orphanIdx = orphans.indexOf(state);
  4181. if (result) {
  4182. var existingState = getState(name_1);
  4183. if (existingState && existingState.name === name_1) {
  4184. throw new Error("State '" + name_1 + "' is already defined");
  4185. }
  4186. var existingFutureState = getState(name_1 + ".**");
  4187. if (existingFutureState) {
  4188. // Remove future state of the same name
  4189. this.$registry.deregister(existingFutureState);
  4190. }
  4191. states[name_1] = state;
  4192. this.attachRoute(state);
  4193. if (orphanIdx >= 0)
  4194. orphans.splice(orphanIdx, 1);
  4195. registered.push(state);
  4196. continue;
  4197. }
  4198. var prev = previousQueueLength[name_1];
  4199. previousQueueLength[name_1] = queue.length;
  4200. if (orphanIdx >= 0 && prev === queue.length) {
  4201. // Wait until two consecutive iterations where no additional states were dequeued successfully.
  4202. // throw new Error(`Cannot register orphaned state '${name}'`);
  4203. queue.push(state);
  4204. return states;
  4205. }
  4206. else if (orphanIdx < 0) {
  4207. orphans.push(state);
  4208. }
  4209. queue.push(state);
  4210. }
  4211. if (registered.length) {
  4212. this.listeners.forEach(function (listener) { return listener("registered", registered.map(function (s) { return s.self; })); });
  4213. }
  4214. return states;
  4215. };
  4216. StateQueueManager.prototype.attachRoute = function (state) {
  4217. if (state.abstract || !state.url)
  4218. return;
  4219. this.$urlRouter.rule(this.$urlRouter.urlRuleFactory.create(state));
  4220. };
  4221. return StateQueueManager;
  4222. }());
  4223. /**
  4224. * @coreapi
  4225. * @module state
  4226. */ /** for typedoc */
  4227. var StateRegistry = /** @class */ (function () {
  4228. /** @internalapi */
  4229. function StateRegistry(_router) {
  4230. this._router = _router;
  4231. this.states = {};
  4232. this.listeners = [];
  4233. this.matcher = new StateMatcher(this.states);
  4234. this.builder = new StateBuilder(this.matcher, _router.urlMatcherFactory);
  4235. this.stateQueue = new StateQueueManager(this, _router.urlRouter, this.states, this.builder, this.listeners);
  4236. this._registerRoot();
  4237. }
  4238. /** @internalapi */
  4239. StateRegistry.prototype._registerRoot = function () {
  4240. var rootStateDef = {
  4241. name: '',
  4242. url: '^',
  4243. views: null,
  4244. params: {
  4245. '#': { value: null, type: 'hash', dynamic: true }
  4246. },
  4247. abstract: true
  4248. };
  4249. var _root = this._root = this.stateQueue.register(rootStateDef);
  4250. _root.navigable = null;
  4251. };
  4252. /** @internalapi */
  4253. StateRegistry.prototype.dispose = function () {
  4254. var _this = this;
  4255. this.stateQueue.dispose();
  4256. this.listeners = [];
  4257. this.get().forEach(function (state) { return _this.get(state) && _this.deregister(state); });
  4258. };
  4259. /**
  4260. * Listen for a State Registry events
  4261. *
  4262. * Adds a callback that is invoked when states are registered or deregistered with the StateRegistry.
  4263. *
  4264. * #### Example:
  4265. * ```js
  4266. * let allStates = registry.get();
  4267. *
  4268. * // Later, invoke deregisterFn() to remove the listener
  4269. * let deregisterFn = registry.onStatesChanged((event, states) => {
  4270. * switch(event) {
  4271. * case: 'registered':
  4272. * states.forEach(state => allStates.push(state));
  4273. * break;
  4274. * case: 'deregistered':
  4275. * states.forEach(state => {
  4276. * let idx = allStates.indexOf(state);
  4277. * if (idx !== -1) allStates.splice(idx, 1);
  4278. * });
  4279. * break;
  4280. * }
  4281. * });
  4282. * ```
  4283. *
  4284. * @param listener a callback function invoked when the registered states changes.
  4285. * The function receives two parameters, `event` and `state`.
  4286. * See [[StateRegistryListener]]
  4287. * @return a function that deregisters the listener
  4288. */
  4289. StateRegistry.prototype.onStatesChanged = function (listener) {
  4290. this.listeners.push(listener);
  4291. return function deregisterListener() {
  4292. removeFrom(this.listeners)(listener);
  4293. }.bind(this);
  4294. };
  4295. /**
  4296. * Gets the implicit root state
  4297. *
  4298. * Gets the root of the state tree.
  4299. * The root state is implicitly created by UI-Router.
  4300. * Note: this returns the internal [[StateObject]] representation, not a [[StateDeclaration]]
  4301. *
  4302. * @return the root [[StateObject]]
  4303. */
  4304. StateRegistry.prototype.root = function () {
  4305. return this._root;
  4306. };
  4307. /**
  4308. * Adds a state to the registry
  4309. *
  4310. * Registers a [[StateDeclaration]] or queues it for registration.
  4311. *
  4312. * Note: a state will be queued if the state's parent isn't yet registered.
  4313. *
  4314. * @param stateDefinition the definition of the state to register.
  4315. * @returns the internal [[StateObject]] object.
  4316. * If the state was successfully registered, then the object is fully built (See: [[StateBuilder]]).
  4317. * If the state was only queued, then the object is not fully built.
  4318. */
  4319. StateRegistry.prototype.register = function (stateDefinition) {
  4320. return this.stateQueue.register(stateDefinition);
  4321. };
  4322. /** @hidden */
  4323. StateRegistry.prototype._deregisterTree = function (state) {
  4324. var _this = this;
  4325. var all$$1 = this.get().map(function (s) { return s.$$state(); });
  4326. var getChildren = function (states) {
  4327. var children = all$$1.filter(function (s) { return states.indexOf(s.parent) !== -1; });
  4328. return children.length === 0 ? children : children.concat(getChildren(children));
  4329. };
  4330. var children = getChildren([state]);
  4331. var deregistered = [state].concat(children).reverse();
  4332. deregistered.forEach(function (state) {
  4333. var $ur = _this._router.urlRouter;
  4334. // Remove URL rule
  4335. $ur.rules().filter(propEq("state", state)).forEach($ur.removeRule.bind($ur));
  4336. // Remove state from registry
  4337. delete _this.states[state.name];
  4338. });
  4339. return deregistered;
  4340. };
  4341. /**
  4342. * Removes a state from the registry
  4343. *
  4344. * This removes a state from the registry.
  4345. * If the state has children, they are are also removed from the registry.
  4346. *
  4347. * @param stateOrName the state's name or object representation
  4348. * @returns {StateObject[]} a list of removed states
  4349. */
  4350. StateRegistry.prototype.deregister = function (stateOrName) {
  4351. var _state = this.get(stateOrName);
  4352. if (!_state)
  4353. throw new Error("Can't deregister state; not found: " + stateOrName);
  4354. var deregisteredStates = this._deregisterTree(_state.$$state());
  4355. this.listeners.forEach(function (listener) { return listener("deregistered", deregisteredStates.map(function (s) { return s.self; })); });
  4356. return deregisteredStates;
  4357. };
  4358. StateRegistry.prototype.get = function (stateOrName, base) {
  4359. var _this = this;
  4360. if (arguments.length === 0)
  4361. return Object.keys(this.states).map(function (name) { return _this.states[name].self; });
  4362. var found = this.matcher.find(stateOrName, base);
  4363. return found && found.self || null;
  4364. };
  4365. StateRegistry.prototype.decorator = function (name, func) {
  4366. return this.builder.builder(name, func);
  4367. };
  4368. return StateRegistry;
  4369. }());
  4370. /**
  4371. * @coreapi
  4372. * @module url
  4373. */
  4374. /** for typedoc */
  4375. /** @hidden */
  4376. function quoteRegExp(string, param) {
  4377. var surroundPattern = ['', ''], result = string.replace(/[\\\[\]\^$*+?.()|{}]/g, "\\$&");
  4378. if (!param)
  4379. return result;
  4380. switch (param.squash) {
  4381. case false:
  4382. surroundPattern = ['(', ')' + (param.isOptional ? '?' : '')];
  4383. break;
  4384. case true:
  4385. result = result.replace(/\/$/, '');
  4386. surroundPattern = ['(?:\/(', ')|\/)?'];
  4387. break;
  4388. default:
  4389. surroundPattern = ["(" + param.squash + "|", ')?'];
  4390. break;
  4391. }
  4392. return result + surroundPattern[0] + param.type.pattern.source + surroundPattern[1];
  4393. }
  4394. /** @hidden */
  4395. var memoizeTo = function (obj, prop$$1, fn) {
  4396. return obj[prop$$1] = obj[prop$$1] || fn();
  4397. };
  4398. /** @hidden */
  4399. var splitOnSlash = splitOnDelim('/');
  4400. /**
  4401. * Matches URLs against patterns.
  4402. *
  4403. * Matches URLs against patterns and extracts named parameters from the path or the search
  4404. * part of the URL.
  4405. *
  4406. * A URL pattern consists of a path pattern, optionally followed by '?' and a list of search (query)
  4407. * parameters. Multiple search parameter names are separated by '&'. Search parameters
  4408. * do not influence whether or not a URL is matched, but their values are passed through into
  4409. * the matched parameters returned by [[UrlMatcher.exec]].
  4410. *
  4411. * - *Path parameters* are defined using curly brace placeholders (`/somepath/{param}`)
  4412. * or colon placeholders (`/somePath/:param`).
  4413. *
  4414. * - *A parameter RegExp* may be defined for a param after a colon
  4415. * (`/somePath/{param:[a-zA-Z0-9]+}`) in a curly brace placeholder.
  4416. * The regexp must match for the url to be matched.
  4417. * Should the regexp itself contain curly braces, they must be in matched pairs or escaped with a backslash.
  4418. *
  4419. * Note: a RegExp parameter will encode its value using either [[ParamTypes.path]] or [[ParamTypes.query]].
  4420. *
  4421. * - *Custom parameter types* may also be specified after a colon (`/somePath/{param:int}`) in curly brace parameters.
  4422. * See [[UrlMatcherFactory.type]] for more information.
  4423. *
  4424. * - *Catch-all parameters* are defined using an asterisk placeholder (`/somepath/*catchallparam`).
  4425. * A catch-all * parameter value will contain the remainder of the URL.
  4426. *
  4427. * ---
  4428. *
  4429. * Parameter names may contain only word characters (latin letters, digits, and underscore) and
  4430. * must be unique within the pattern (across both path and search parameters).
  4431. * A path parameter matches any number of characters other than '/'. For catch-all
  4432. * placeholders the path parameter matches any number of characters.
  4433. *
  4434. * Examples:
  4435. *
  4436. * * `'/hello/'` - Matches only if the path is exactly '/hello/'. There is no special treatment for
  4437. * trailing slashes, and patterns have to match the entire path, not just a prefix.
  4438. * * `'/user/:id'` - Matches '/user/bob' or '/user/1234!!!' or even '/user/' but not '/user' or
  4439. * '/user/bob/details'. The second path segment will be captured as the parameter 'id'.
  4440. * * `'/user/{id}'` - Same as the previous example, but using curly brace syntax.
  4441. * * `'/user/{id:[^/]*}'` - Same as the previous example.
  4442. * * `'/user/{id:[0-9a-fA-F]{1,8}}'` - Similar to the previous example, but only matches if the id
  4443. * parameter consists of 1 to 8 hex digits.
  4444. * * `'/files/{path:.*}'` - Matches any URL starting with '/files/' and captures the rest of the
  4445. * path into the parameter 'path'.
  4446. * * `'/files/*path'` - ditto.
  4447. * * `'/calendar/{start:date}'` - Matches "/calendar/2014-11-12" (because the pattern defined
  4448. * in the built-in `date` ParamType matches `2014-11-12`) and provides a Date object in $stateParams.start
  4449. *
  4450. */
  4451. var UrlMatcher = /** @class */ (function () {
  4452. /**
  4453. * @param pattern The pattern to compile into a matcher.
  4454. * @param paramTypes The [[ParamTypes]] registry
  4455. * @param config A configuration object
  4456. * - `caseInsensitive` - `true` if URL matching should be case insensitive, otherwise `false`, the default value (for backward compatibility) is `false`.
  4457. * - `strict` - `false` if matching against a URL with a trailing slash should be treated as equivalent to a URL without a trailing slash, the default value is `true`.
  4458. */
  4459. function UrlMatcher(pattern$$1, paramTypes, paramFactory, config) {
  4460. var _this = this;
  4461. this.config = config;
  4462. /** @hidden */
  4463. this._cache = { path: [this] };
  4464. /** @hidden */
  4465. this._children = [];
  4466. /** @hidden */
  4467. this._params = [];
  4468. /** @hidden */
  4469. this._segments = [];
  4470. /** @hidden */
  4471. this._compiled = [];
  4472. this.pattern = pattern$$1;
  4473. this.config = defaults(this.config, {
  4474. params: {},
  4475. strict: true,
  4476. caseInsensitive: false,
  4477. paramMap: identity
  4478. });
  4479. // Find all placeholders and create a compiled pattern, using either classic or curly syntax:
  4480. // '*' name
  4481. // ':' name
  4482. // '{' name '}'
  4483. // '{' name ':' regexp '}'
  4484. // The regular expression is somewhat complicated due to the need to allow curly braces
  4485. // inside the regular expression. The placeholder regexp breaks down as follows:
  4486. // ([:*])([\w\[\]]+) - classic placeholder ($1 / $2) (search version has - for snake-case)
  4487. // \{([\w\[\]]+)(?:\:\s*( ... ))?\} - curly brace placeholder ($3) with optional regexp/type ... ($4) (search version has - for snake-case
  4488. // (?: ... | ... | ... )+ - the regexp consists of any number of atoms, an atom being either
  4489. // [^{}\\]+ - anything other than curly braces or backslash
  4490. // \\. - a backslash escape
  4491. // \{(?:[^{}\\]+|\\.)*\} - a matched set of curly braces containing other atoms
  4492. var placeholder = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g, searchPlaceholder = /([:]?)([\w\[\].-]+)|\{([\w\[\].-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g, last = 0, m, patterns = [];
  4493. var checkParamErrors = function (id) {
  4494. if (!UrlMatcher.nameValidator.test(id))
  4495. throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern$$1 + "'");
  4496. if (find(_this._params, propEq('id', id)))
  4497. throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern$$1 + "'");
  4498. };
  4499. // Split into static segments separated by path parameter placeholders.
  4500. // The number of segments is always 1 more than the number of parameters.
  4501. var matchDetails = function (m, isSearch) {
  4502. // IE[78] returns '' for unmatched groups instead of null
  4503. var id = m[2] || m[3];
  4504. var regexp = isSearch ? m[4] : m[4] || (m[1] === '*' ? '[\\s\\S]*' : null);
  4505. var makeRegexpType = function (regexp) { return inherit(paramTypes.type(isSearch ? "query" : "path"), {
  4506. pattern: new RegExp(regexp, _this.config.caseInsensitive ? 'i' : undefined)
  4507. }); };
  4508. return {
  4509. id: id,
  4510. regexp: regexp,
  4511. cfg: _this.config.params[id],
  4512. segment: pattern$$1.substring(last, m.index),
  4513. type: !regexp ? null : paramTypes.type(regexp) || makeRegexpType(regexp)
  4514. };
  4515. };
  4516. var p, segment;
  4517. while ((m = placeholder.exec(pattern$$1))) {
  4518. p = matchDetails(m, false);
  4519. if (p.segment.indexOf('?') >= 0)
  4520. break; // we're into the search part
  4521. checkParamErrors(p.id);
  4522. this._params.push(paramFactory.fromPath(p.id, p.type, this.config.paramMap(p.cfg, false)));
  4523. this._segments.push(p.segment);
  4524. patterns.push([p.segment, tail(this._params)]);
  4525. last = placeholder.lastIndex;
  4526. }
  4527. segment = pattern$$1.substring(last);
  4528. // Find any search parameter names and remove them from the last segment
  4529. var i = segment.indexOf('?');
  4530. if (i >= 0) {
  4531. var search = segment.substring(i);
  4532. segment = segment.substring(0, i);
  4533. if (search.length > 0) {
  4534. last = 0;
  4535. while ((m = searchPlaceholder.exec(search))) {
  4536. p = matchDetails(m, true);
  4537. checkParamErrors(p.id);
  4538. this._params.push(paramFactory.fromSearch(p.id, p.type, this.config.paramMap(p.cfg, true)));
  4539. last = placeholder.lastIndex;
  4540. // check if ?&
  4541. }
  4542. }
  4543. }
  4544. this._segments.push(segment);
  4545. this._compiled = patterns.map(function (pattern$$1) { return quoteRegExp.apply(null, pattern$$1); }).concat(quoteRegExp(segment));
  4546. }
  4547. /**
  4548. * Creates a new concatenated UrlMatcher
  4549. *
  4550. * Builds a new UrlMatcher by appending another UrlMatcher to this one.
  4551. *
  4552. * @param url A `UrlMatcher` instance to append as a child of the current `UrlMatcher`.
  4553. */
  4554. UrlMatcher.prototype.append = function (url) {
  4555. this._children.push(url);
  4556. url._cache = {
  4557. path: this._cache.path.concat(url),
  4558. parent: this,
  4559. pattern: null,
  4560. };
  4561. return url;
  4562. };
  4563. /** @hidden */
  4564. UrlMatcher.prototype.isRoot = function () {
  4565. return this._cache.path[0] === this;
  4566. };
  4567. /** Returns the input pattern string */
  4568. UrlMatcher.prototype.toString = function () {
  4569. return this.pattern;
  4570. };
  4571. /**
  4572. * Tests the specified url/path against this matcher.
  4573. *
  4574. * Tests if the given url matches this matcher's pattern, and returns an object containing the captured
  4575. * parameter values. Returns null if the path does not match.
  4576. *
  4577. * The returned object contains the values
  4578. * of any search parameters that are mentioned in the pattern, but their value may be null if
  4579. * they are not present in `search`. This means that search parameters are always treated
  4580. * as optional.
  4581. *
  4582. * #### Example:
  4583. * ```js
  4584. * new UrlMatcher('/user/{id}?q&r').exec('/user/bob', {
  4585. * x: '1', q: 'hello'
  4586. * });
  4587. * // returns { id: 'bob', q: 'hello', r: null }
  4588. * ```
  4589. *
  4590. * @param path The URL path to match, e.g. `$location.path()`.
  4591. * @param search URL search parameters, e.g. `$location.search()`.
  4592. * @param hash URL hash e.g. `$location.hash()`.
  4593. * @param options
  4594. *
  4595. * @returns The captured parameter values.
  4596. */
  4597. UrlMatcher.prototype.exec = function (path, search, hash, options) {
  4598. var _this = this;
  4599. if (search === void 0) { search = {}; }
  4600. if (options === void 0) { options = {}; }
  4601. var match = memoizeTo(this._cache, 'pattern', function () {
  4602. return new RegExp([
  4603. '^',
  4604. unnest(_this._cache.path.map(prop('_compiled'))).join(''),
  4605. _this.config.strict === false ? '\/?' : '',
  4606. '$'
  4607. ].join(''), _this.config.caseInsensitive ? 'i' : undefined);
  4608. }).exec(path);
  4609. if (!match)
  4610. return null;
  4611. //options = defaults(options, { isolate: false });
  4612. var allParams = this.parameters(), pathParams = allParams.filter(function (param) { return !param.isSearch(); }), searchParams = allParams.filter(function (param) { return param.isSearch(); }), nPathSegments = this._cache.path.map(function (urlm) { return urlm._segments.length - 1; }).reduce(function (a, x) { return a + x; }), values$$1 = {};
  4613. if (nPathSegments !== match.length - 1)
  4614. throw new Error("Unbalanced capture group in route '" + this.pattern + "'");
  4615. function decodePathArray(string) {
  4616. var reverseString = function (str) { return str.split("").reverse().join(""); };
  4617. var unquoteDashes = function (str) { return str.replace(/\\-/g, "-"); };
  4618. var split = reverseString(string).split(/-(?!\\)/);
  4619. var allReversed = map(split, reverseString);
  4620. return map(allReversed, unquoteDashes).reverse();
  4621. }
  4622. for (var i = 0; i < nPathSegments; i++) {
  4623. var param = pathParams[i];
  4624. var value = match[i + 1];
  4625. // if the param value matches a pre-replace pair, replace the value before decoding.
  4626. for (var j = 0; j < param.replace.length; j++) {
  4627. if (param.replace[j].from === value)
  4628. value = param.replace[j].to;
  4629. }
  4630. if (value && param.array === true)
  4631. value = decodePathArray(value);
  4632. if (isDefined(value))
  4633. value = param.type.decode(value);
  4634. values$$1[param.id] = param.value(value);
  4635. }
  4636. searchParams.forEach(function (param) {
  4637. var value = search[param.id];
  4638. for (var j = 0; j < param.replace.length; j++) {
  4639. if (param.replace[j].from === value)
  4640. value = param.replace[j].to;
  4641. }
  4642. if (isDefined(value))
  4643. value = param.type.decode(value);
  4644. values$$1[param.id] = param.value(value);
  4645. });
  4646. if (hash)
  4647. values$$1["#"] = hash;
  4648. return values$$1;
  4649. };
  4650. /**
  4651. * @hidden
  4652. * Returns all the [[Param]] objects of all path and search parameters of this pattern in order of appearance.
  4653. *
  4654. * @returns {Array.<Param>} An array of [[Param]] objects. Must be treated as read-only. If the
  4655. * pattern has no parameters, an empty array is returned.
  4656. */
  4657. UrlMatcher.prototype.parameters = function (opts) {
  4658. if (opts === void 0) { opts = {}; }
  4659. if (opts.inherit === false)
  4660. return this._params;
  4661. return unnest(this._cache.path.map(function (matcher) { return matcher._params; }));
  4662. };
  4663. /**
  4664. * @hidden
  4665. * Returns a single parameter from this UrlMatcher by id
  4666. *
  4667. * @param id
  4668. * @param opts
  4669. * @returns {T|Param|any|boolean|UrlMatcher|null}
  4670. */
  4671. UrlMatcher.prototype.parameter = function (id, opts) {
  4672. var _this = this;
  4673. if (opts === void 0) { opts = {}; }
  4674. var findParam = function () {
  4675. for (var _i = 0, _a = _this._params; _i < _a.length; _i++) {
  4676. var param = _a[_i];
  4677. if (param.id === id)
  4678. return param;
  4679. }
  4680. };
  4681. var parent = this._cache.parent;
  4682. return findParam() || (opts.inherit !== false && parent && parent.parameter(id, opts)) || null;
  4683. };
  4684. /**
  4685. * Validates the input parameter values against this UrlMatcher
  4686. *
  4687. * Checks an object hash of parameters to validate their correctness according to the parameter
  4688. * types of this `UrlMatcher`.
  4689. *
  4690. * @param params The object hash of parameters to validate.
  4691. * @returns Returns `true` if `params` validates, otherwise `false`.
  4692. */
  4693. UrlMatcher.prototype.validates = function (params) {
  4694. var validParamVal = function (param, val$$1) {
  4695. return !param || param.validates(val$$1);
  4696. };
  4697. params = params || {};
  4698. // I'm not sure why this checks only the param keys passed in, and not all the params known to the matcher
  4699. var paramSchema = this.parameters().filter(function (paramDef) { return params.hasOwnProperty(paramDef.id); });
  4700. return paramSchema.map(function (paramDef) { return validParamVal(paramDef, params[paramDef.id]); }).reduce(allTrueR, true);
  4701. };
  4702. /**
  4703. * Given a set of parameter values, creates a URL from this UrlMatcher.
  4704. *
  4705. * Creates a URL that matches this pattern by substituting the specified values
  4706. * for the path and search parameters.
  4707. *
  4708. * #### Example:
  4709. * ```js
  4710. * new UrlMatcher('/user/{id}?q').format({ id:'bob', q:'yes' });
  4711. * // returns '/user/bob?q=yes'
  4712. * ```
  4713. *
  4714. * @param values the values to substitute for the parameters in this pattern.
  4715. * @returns the formatted URL (path and optionally search part).
  4716. */
  4717. UrlMatcher.prototype.format = function (values$$1) {
  4718. if (values$$1 === void 0) { values$$1 = {}; }
  4719. // Build the full path of UrlMatchers (including all parent UrlMatchers)
  4720. var urlMatchers = this._cache.path;
  4721. // Extract all the static segments and Params (processed as ParamDetails)
  4722. // into an ordered array
  4723. var pathSegmentsAndParams = urlMatchers.map(UrlMatcher.pathSegmentsAndParams)
  4724. .reduce(unnestR, [])
  4725. .map(function (x) { return isString(x) ? x : getDetails(x); });
  4726. // Extract the query params into a separate array
  4727. var queryParams = urlMatchers.map(UrlMatcher.queryParams)
  4728. .reduce(unnestR, [])
  4729. .map(getDetails);
  4730. var isInvalid = function (param) { return param.isValid === false; };
  4731. if (pathSegmentsAndParams.concat(queryParams).filter(isInvalid).length) {
  4732. return null;
  4733. }
  4734. /**
  4735. * Given a Param, applies the parameter value, then returns detailed information about it
  4736. */
  4737. function getDetails(param) {
  4738. // Normalize to typed value
  4739. var value = param.value(values$$1[param.id]);
  4740. var isValid = param.validates(value);
  4741. var isDefaultValue = param.isDefaultValue(value);
  4742. // Check if we're in squash mode for the parameter
  4743. var squash = isDefaultValue ? param.squash : false;
  4744. // Allow the Parameter's Type to encode the value
  4745. var encoded = param.type.encode(value);
  4746. return { param: param, value: value, isValid: isValid, isDefaultValue: isDefaultValue, squash: squash, encoded: encoded };
  4747. }
  4748. // Build up the path-portion from the list of static segments and parameters
  4749. var pathString = pathSegmentsAndParams.reduce(function (acc, x) {
  4750. // The element is a static segment (a raw string); just append it
  4751. if (isString(x))
  4752. return acc + x;
  4753. // Otherwise, it's a ParamDetails.
  4754. var squash = x.squash, encoded = x.encoded, param = x.param;
  4755. // If squash is === true, try to remove a slash from the path
  4756. if (squash === true)
  4757. return (acc.match(/\/$/)) ? acc.slice(0, -1) : acc;
  4758. // If squash is a string, use the string for the param value
  4759. if (isString(squash))
  4760. return acc + squash;
  4761. if (squash !== false)
  4762. return acc; // ?
  4763. if (encoded == null)
  4764. return acc;
  4765. // If this parameter value is an array, encode the value using encodeDashes
  4766. if (isArray(encoded))
  4767. return acc + map(encoded, UrlMatcher.encodeDashes).join("-");
  4768. // If the parameter type is "raw", then do not encodeURIComponent
  4769. if (param.raw)
  4770. return acc + encoded;
  4771. // Encode the value
  4772. return acc + encodeURIComponent(encoded);
  4773. }, "");
  4774. // Build the query string by applying parameter values (array or regular)
  4775. // then mapping to key=value, then flattening and joining using "&"
  4776. var queryString = queryParams.map(function (paramDetails) {
  4777. var param = paramDetails.param, squash = paramDetails.squash, encoded = paramDetails.encoded, isDefaultValue = paramDetails.isDefaultValue;
  4778. if (encoded == null || (isDefaultValue && squash !== false))
  4779. return;
  4780. if (!isArray(encoded))
  4781. encoded = [encoded];
  4782. if (encoded.length === 0)
  4783. return;
  4784. if (!param.raw)
  4785. encoded = map(encoded, encodeURIComponent);
  4786. return encoded.map(function (val$$1) { return param.id + "=" + val$$1; });
  4787. }).filter(identity).reduce(unnestR, []).join("&");
  4788. // Concat the pathstring with the queryString (if exists) and the hashString (if exists)
  4789. return pathString + (queryString ? "?" + queryString : "") + (values$$1["#"] ? "#" + values$$1["#"] : "");
  4790. };
  4791. /** @hidden */
  4792. UrlMatcher.encodeDashes = function (str) {
  4793. return encodeURIComponent(str).replace(/-/g, function (c) { return "%5C%" + c.charCodeAt(0).toString(16).toUpperCase(); });
  4794. };
  4795. /** @hidden Given a matcher, return an array with the matcher's path segments and path params, in order */
  4796. UrlMatcher.pathSegmentsAndParams = function (matcher) {
  4797. var staticSegments = matcher._segments;
  4798. var pathParams = matcher._params.filter(function (p) { return p.location === exports.DefType.PATH; });
  4799. return arrayTuples(staticSegments, pathParams.concat(undefined))
  4800. .reduce(unnestR, [])
  4801. .filter(function (x) { return x !== "" && isDefined(x); });
  4802. };
  4803. /** @hidden Given a matcher, return an array with the matcher's query params */
  4804. UrlMatcher.queryParams = function (matcher) {
  4805. return matcher._params.filter(function (p) { return p.location === exports.DefType.SEARCH; });
  4806. };
  4807. /**
  4808. * Compare two UrlMatchers
  4809. *
  4810. * This comparison function converts a UrlMatcher into static and dynamic path segments.
  4811. * Each static path segment is a static string between a path separator (slash character).
  4812. * Each dynamic segment is a path parameter.
  4813. *
  4814. * The comparison function sorts static segments before dynamic ones.
  4815. */
  4816. UrlMatcher.compare = function (a, b) {
  4817. /**
  4818. * Turn a UrlMatcher and all its parent matchers into an array
  4819. * of slash literals '/', string literals, and Param objects
  4820. *
  4821. * This example matcher matches strings like "/foo/:param/tail":
  4822. * var matcher = $umf.compile("/foo").append($umf.compile("/:param")).append($umf.compile("/")).append($umf.compile("tail"));
  4823. * var result = segments(matcher); // [ '/', 'foo', '/', Param, '/', 'tail' ]
  4824. *
  4825. * Caches the result as `matcher._cache.segments`
  4826. */
  4827. var segments = function (matcher) {
  4828. return matcher._cache.segments = matcher._cache.segments ||
  4829. matcher._cache.path.map(UrlMatcher.pathSegmentsAndParams)
  4830. .reduce(unnestR, [])
  4831. .reduce(joinNeighborsR, [])
  4832. .map(function (x) { return isString(x) ? splitOnSlash(x) : x; })
  4833. .reduce(unnestR, []);
  4834. };
  4835. /**
  4836. * Gets the sort weight for each segment of a UrlMatcher
  4837. *
  4838. * Caches the result as `matcher._cache.weights`
  4839. */
  4840. var weights = function (matcher) {
  4841. return matcher._cache.weights = matcher._cache.weights ||
  4842. segments(matcher).map(function (segment) {
  4843. // Sort slashes first, then static strings, the Params
  4844. if (segment === '/')
  4845. return 1;
  4846. if (isString(segment))
  4847. return 2;
  4848. if (segment instanceof Param)
  4849. return 3;
  4850. });
  4851. };
  4852. /**
  4853. * Pads shorter array in-place (mutates)
  4854. */
  4855. var padArrays = function (l, r, padVal) {
  4856. var len = Math.max(l.length, r.length);
  4857. while (l.length < len)
  4858. l.push(padVal);
  4859. while (r.length < len)
  4860. r.push(padVal);
  4861. };
  4862. var weightsA = weights(a), weightsB = weights(b);
  4863. padArrays(weightsA, weightsB, 0);
  4864. var cmp, i, pairs$$1 = arrayTuples(weightsA, weightsB);
  4865. for (i = 0; i < pairs$$1.length; i++) {
  4866. cmp = pairs$$1[i][0] - pairs$$1[i][1];
  4867. if (cmp !== 0)
  4868. return cmp;
  4869. }
  4870. return 0;
  4871. };
  4872. /** @hidden */
  4873. UrlMatcher.nameValidator = /^\w+([-.]+\w+)*(?:\[\])?$/;
  4874. return UrlMatcher;
  4875. }());
  4876. /**
  4877. * @internalapi
  4878. * @module url
  4879. */ /** for typedoc */
  4880. /**
  4881. * Factory for [[UrlMatcher]] instances.
  4882. *
  4883. * The factory is available to ng1 services as
  4884. * `$urlMatcherFactory` or ng1 providers as `$urlMatcherFactoryProvider`.
  4885. */
  4886. var UrlMatcherFactory = /** @class */ (function () {
  4887. function UrlMatcherFactory() {
  4888. var _this = this;
  4889. /** @hidden */ this.paramTypes = new ParamTypes();
  4890. /** @hidden */ this._isCaseInsensitive = false;
  4891. /** @hidden */ this._isStrictMode = true;
  4892. /** @hidden */ this._defaultSquashPolicy = false;
  4893. /** @hidden */
  4894. this._getConfig = function (config) {
  4895. return extend({ strict: _this._isStrictMode, caseInsensitive: _this._isCaseInsensitive }, config);
  4896. };
  4897. /** @internalapi Creates a new [[Param]] for a given location (DefType) */
  4898. this.paramFactory = {
  4899. /** Creates a new [[Param]] from a CONFIG block */
  4900. fromConfig: function (id, type, config) {
  4901. return new Param(id, type, config, exports.DefType.CONFIG, _this);
  4902. },
  4903. /** Creates a new [[Param]] from a url PATH */
  4904. fromPath: function (id, type, config) {
  4905. return new Param(id, type, config, exports.DefType.PATH, _this);
  4906. },
  4907. /** Creates a new [[Param]] from a url SEARCH */
  4908. fromSearch: function (id, type, config) {
  4909. return new Param(id, type, config, exports.DefType.SEARCH, _this);
  4910. },
  4911. };
  4912. extend(this, { UrlMatcher: UrlMatcher, Param: Param });
  4913. }
  4914. /** @inheritdoc */
  4915. UrlMatcherFactory.prototype.caseInsensitive = function (value) {
  4916. return this._isCaseInsensitive = isDefined(value) ? value : this._isCaseInsensitive;
  4917. };
  4918. /** @inheritdoc */
  4919. UrlMatcherFactory.prototype.strictMode = function (value) {
  4920. return this._isStrictMode = isDefined(value) ? value : this._isStrictMode;
  4921. };
  4922. /** @inheritdoc */
  4923. UrlMatcherFactory.prototype.defaultSquashPolicy = function (value) {
  4924. if (isDefined(value) && value !== true && value !== false && !isString(value))
  4925. throw new Error("Invalid squash policy: " + value + ". Valid policies: false, true, arbitrary-string");
  4926. return this._defaultSquashPolicy = isDefined(value) ? value : this._defaultSquashPolicy;
  4927. };
  4928. /**
  4929. * Creates a [[UrlMatcher]] for the specified pattern.
  4930. *
  4931. * @param pattern The URL pattern.
  4932. * @param config The config object hash.
  4933. * @returns The UrlMatcher.
  4934. */
  4935. UrlMatcherFactory.prototype.compile = function (pattern, config) {
  4936. return new UrlMatcher(pattern, this.paramTypes, this.paramFactory, this._getConfig(config));
  4937. };
  4938. /**
  4939. * Returns true if the specified object is a [[UrlMatcher]], or false otherwise.
  4940. *
  4941. * @param object The object to perform the type check against.
  4942. * @returns `true` if the object matches the `UrlMatcher` interface, by
  4943. * implementing all the same methods.
  4944. */
  4945. UrlMatcherFactory.prototype.isMatcher = function (object) {
  4946. // TODO: typeof?
  4947. if (!isObject(object))
  4948. return false;
  4949. var result = true;
  4950. forEach(UrlMatcher.prototype, function (val, name) {
  4951. if (isFunction(val))
  4952. result = result && (isDefined(object[name]) && isFunction(object[name]));
  4953. });
  4954. return result;
  4955. };
  4956. /**
  4957. * Creates and registers a custom [[ParamType]] object
  4958. *
  4959. * A [[ParamType]] can be used to generate URLs with typed parameters.
  4960. *
  4961. * @param name The type name.
  4962. * @param definition The type definition. See [[ParamTypeDefinition]] for information on the values accepted.
  4963. * @param definitionFn A function that is injected before the app runtime starts.
  4964. * The result of this function should be a [[ParamTypeDefinition]].
  4965. * The result is merged into the existing `definition`.
  4966. * See [[ParamType]] for information on the values accepted.
  4967. *
  4968. * @returns - if a type was registered: the [[UrlMatcherFactory]]
  4969. * - if only the `name` parameter was specified: the currently registered [[ParamType]] object, or undefined
  4970. *
  4971. * Note: Register custom types *before using them* in a state definition.
  4972. *
  4973. * See [[ParamTypeDefinition]] for examples
  4974. */
  4975. UrlMatcherFactory.prototype.type = function (name, definition, definitionFn) {
  4976. var type = this.paramTypes.type(name, definition, definitionFn);
  4977. return !isDefined(definition) ? type : this;
  4978. };
  4979. /** @hidden */
  4980. UrlMatcherFactory.prototype.$get = function () {
  4981. this.paramTypes.enqueue = false;
  4982. this.paramTypes._flushTypeQueue();
  4983. return this;
  4984. };
  4985. /** @internalapi */
  4986. UrlMatcherFactory.prototype.dispose = function () {
  4987. this.paramTypes.dispose();
  4988. };
  4989. return UrlMatcherFactory;
  4990. }());
  4991. /**
  4992. * @coreapi
  4993. * @module url
  4994. */ /** */
  4995. /**
  4996. * Creates a [[UrlRule]]
  4997. *
  4998. * Creates a [[UrlRule]] from a:
  4999. *
  5000. * - `string`
  5001. * - [[UrlMatcher]]
  5002. * - `RegExp`
  5003. * - [[StateObject]]
  5004. * @internalapi
  5005. */
  5006. var UrlRuleFactory = /** @class */ (function () {
  5007. function UrlRuleFactory(router) {
  5008. this.router = router;
  5009. }
  5010. UrlRuleFactory.prototype.compile = function (str) {
  5011. return this.router.urlMatcherFactory.compile(str);
  5012. };
  5013. UrlRuleFactory.prototype.create = function (what, handler) {
  5014. var _this = this;
  5015. var makeRule = pattern([
  5016. [isString, function (_what) { return makeRule(_this.compile(_what)); }],
  5017. [is(UrlMatcher), function (_what) { return _this.fromUrlMatcher(_what, handler); }],
  5018. [isState, function (_what) { return _this.fromState(_what, _this.router); }],
  5019. [is(RegExp), function (_what) { return _this.fromRegExp(_what, handler); }],
  5020. [isFunction, function (_what) { return new BaseUrlRule(_what, handler); }],
  5021. ]);
  5022. var rule = makeRule(what);
  5023. if (!rule)
  5024. throw new Error("invalid 'what' in when()");
  5025. return rule;
  5026. };
  5027. /**
  5028. * A UrlRule which matches based on a UrlMatcher
  5029. *
  5030. * The `handler` may be either a `string`, a [[UrlRuleHandlerFn]] or another [[UrlMatcher]]
  5031. *
  5032. * ## Handler as a function
  5033. *
  5034. * If `handler` is a function, the function is invoked with:
  5035. *
  5036. * - matched parameter values ([[RawParams]] from [[UrlMatcher.exec]])
  5037. * - url: the current Url ([[UrlParts]])
  5038. * - router: the router object ([[UIRouter]])
  5039. *
  5040. * #### Example:
  5041. * ```js
  5042. * var urlMatcher = $umf.compile("/foo/:fooId/:barId");
  5043. * var rule = factory.fromUrlMatcher(urlMatcher, match => "/home/" + match.fooId + "/" + match.barId);
  5044. * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
  5045. * var result = rule.handler(match); // '/home/123/456'
  5046. * ```
  5047. *
  5048. * ## Handler as UrlMatcher
  5049. *
  5050. * If `handler` is a UrlMatcher, the handler matcher is used to create the new url.
  5051. * The `handler` UrlMatcher is formatted using the matched param from the first matcher.
  5052. * The url is replaced with the result.
  5053. *
  5054. * #### Example:
  5055. * ```js
  5056. * var urlMatcher = $umf.compile("/foo/:fooId/:barId");
  5057. * var handler = $umf.compile("/home/:fooId/:barId");
  5058. * var rule = factory.fromUrlMatcher(urlMatcher, handler);
  5059. * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
  5060. * var result = rule.handler(match); // '/home/123/456'
  5061. * ```
  5062. */
  5063. UrlRuleFactory.prototype.fromUrlMatcher = function (urlMatcher, handler) {
  5064. var _handler = handler;
  5065. if (isString(handler))
  5066. handler = this.router.urlMatcherFactory.compile(handler);
  5067. if (is(UrlMatcher)(handler))
  5068. _handler = function (match) { return handler.format(match); };
  5069. function match(url) {
  5070. var match = urlMatcher.exec(url.path, url.search, url.hash);
  5071. return urlMatcher.validates(match) && match;
  5072. }
  5073. // Prioritize URLs, lowest to highest:
  5074. // - Some optional URL parameters, but none matched
  5075. // - No optional parameters in URL
  5076. // - Some optional parameters, some matched
  5077. // - Some optional parameters, all matched
  5078. function matchPriority(params) {
  5079. var optional = urlMatcher.parameters().filter(function (param) { return param.isOptional; });
  5080. if (!optional.length)
  5081. return 0.000001;
  5082. var matched = optional.filter(function (param) { return params[param.id]; });
  5083. return matched.length / optional.length;
  5084. }
  5085. var details = { urlMatcher: urlMatcher, matchPriority: matchPriority, type: "URLMATCHER" };
  5086. return extend(new BaseUrlRule(match, _handler), details);
  5087. };
  5088. /**
  5089. * A UrlRule which matches a state by its url
  5090. *
  5091. * #### Example:
  5092. * ```js
  5093. * var rule = factory.fromState($state.get('foo'), router);
  5094. * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
  5095. * var result = rule.handler(match);
  5096. * // Starts a transition to 'foo' with params: { fooId: '123', barId: '456' }
  5097. * ```
  5098. */
  5099. UrlRuleFactory.prototype.fromState = function (state, router) {
  5100. /**
  5101. * Handles match by transitioning to matched state
  5102. *
  5103. * First checks if the router should start a new transition.
  5104. * A new transition is not required if the current state's URL
  5105. * and the new URL are already identical
  5106. */
  5107. var handler = function (match) {
  5108. var $state = router.stateService;
  5109. var globals = router.globals;
  5110. if ($state.href(state, match) !== $state.href(globals.current, globals.params)) {
  5111. $state.transitionTo(state, match, { inherit: true, source: "url" });
  5112. }
  5113. };
  5114. var details = { state: state, type: "STATE" };
  5115. return extend(this.fromUrlMatcher(state.url, handler), details);
  5116. };
  5117. /**
  5118. * A UrlRule which matches based on a regular expression
  5119. *
  5120. * The `handler` may be either a [[UrlRuleHandlerFn]] or a string.
  5121. *
  5122. * ## Handler as a function
  5123. *
  5124. * If `handler` is a function, the function is invoked with:
  5125. *
  5126. * - regexp match array (from `regexp`)
  5127. * - url: the current Url ([[UrlParts]])
  5128. * - router: the router object ([[UIRouter]])
  5129. *
  5130. * #### Example:
  5131. * ```js
  5132. * var rule = factory.fromRegExp(/^\/foo\/(bar|baz)$/, match => "/home/" + match[1])
  5133. * var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]
  5134. * var result = rule.handler(match); // '/home/bar'
  5135. * ```
  5136. *
  5137. * ## Handler as string
  5138. *
  5139. * If `handler` is a string, the url is *replaced by the string* when the Rule is invoked.
  5140. * The string is first interpolated using `string.replace()` style pattern.
  5141. *
  5142. * #### Example:
  5143. * ```js
  5144. * var rule = factory.fromRegExp(/^\/foo\/(bar|baz)$/, "/home/$1")
  5145. * var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]
  5146. * var result = rule.handler(match); // '/home/bar'
  5147. * ```
  5148. */
  5149. UrlRuleFactory.prototype.fromRegExp = function (regexp, handler) {
  5150. if (regexp.global || regexp.sticky)
  5151. throw new Error("Rule RegExp must not be global or sticky");
  5152. /**
  5153. * If handler is a string, the url will be replaced by the string.
  5154. * If the string has any String.replace() style variables in it (like `$2`),
  5155. * they will be replaced by the captures from [[match]]
  5156. */
  5157. var redirectUrlTo = function (match) {
  5158. // Interpolates matched values into $1 $2, etc using a String.replace()-style pattern
  5159. return handler.replace(/\$(\$|\d{1,2})/, function (m, what) {
  5160. return match[what === '$' ? 0 : Number(what)];
  5161. });
  5162. };
  5163. var _handler = isString(handler) ? redirectUrlTo : handler;
  5164. var match = function (url) {
  5165. return regexp.exec(url.path);
  5166. };
  5167. var details = { regexp: regexp, type: "REGEXP" };
  5168. return extend(new BaseUrlRule(match, _handler), details);
  5169. };
  5170. UrlRuleFactory.isUrlRule = function (obj) {
  5171. return obj && ['type', 'match', 'handler'].every(function (key) { return isDefined(obj[key]); });
  5172. };
  5173. return UrlRuleFactory;
  5174. }());
  5175. /**
  5176. * A base rule which calls `match`
  5177. *
  5178. * The value from the `match` function is passed through to the `handler`.
  5179. * @internalapi
  5180. */
  5181. var BaseUrlRule = /** @class */ (function () {
  5182. function BaseUrlRule(match, handler) {
  5183. var _this = this;
  5184. this.match = match;
  5185. this.type = "RAW";
  5186. this.matchPriority = function (match) { return 0 - _this.$id; };
  5187. this.handler = handler || identity;
  5188. }
  5189. return BaseUrlRule;
  5190. }());
  5191. /**
  5192. * @internalapi
  5193. * @module url
  5194. */
  5195. /** for typedoc */
  5196. /** @hidden */
  5197. function appendBasePath(url, isHtml5, absolute, baseHref) {
  5198. if (baseHref === '/')
  5199. return url;
  5200. if (isHtml5)
  5201. return stripFile(baseHref) + url;
  5202. if (absolute)
  5203. return baseHref.slice(1) + url;
  5204. return url;
  5205. }
  5206. /** @hidden */
  5207. var prioritySort = function (a, b) {
  5208. return (b.priority || 0) - (a.priority || 0);
  5209. };
  5210. /** @hidden */
  5211. var typeSort = function (a, b) {
  5212. var weights = { "STATE": 4, "URLMATCHER": 4, "REGEXP": 3, "RAW": 2, "OTHER": 1 };
  5213. return (weights[a.type] || 0) - (weights[b.type] || 0);
  5214. };
  5215. /** @hidden */
  5216. var urlMatcherSort = function (a, b) {
  5217. return !a.urlMatcher || !b.urlMatcher ? 0 : UrlMatcher.compare(a.urlMatcher, b.urlMatcher);
  5218. };
  5219. /** @hidden */
  5220. var idSort = function (a, b) {
  5221. // Identically sorted STATE and URLMATCHER best rule will be chosen by `matchPriority` after each rule matches the URL
  5222. var useMatchPriority = { STATE: true, URLMATCHER: true };
  5223. var equal = useMatchPriority[a.type] && useMatchPriority[b.type];
  5224. return equal ? 0 : (a.$id || 0) - (b.$id || 0);
  5225. };
  5226. /**
  5227. * Default rule priority sorting function.
  5228. *
  5229. * Sorts rules by:
  5230. *
  5231. * - Explicit priority (set rule priority using [[UrlRulesApi.when]])
  5232. * - Rule type (STATE: 4, URLMATCHER: 4, REGEXP: 3, RAW: 2, OTHER: 1)
  5233. * - `UrlMatcher` specificity ([[UrlMatcher.compare]]): works for STATE and URLMATCHER types to pick the most specific rule.
  5234. * - Rule registration order (for rule types other than STATE and URLMATCHER)
  5235. * - Equally sorted State and UrlMatcher rules will each match the URL.
  5236. * Then, the *best* match is chosen based on how many parameter values were matched.
  5237. *
  5238. * @coreapi
  5239. */
  5240. var defaultRuleSortFn;
  5241. defaultRuleSortFn = function (a, b) {
  5242. var cmp = prioritySort(a, b);
  5243. if (cmp !== 0)
  5244. return cmp;
  5245. cmp = typeSort(a, b);
  5246. if (cmp !== 0)
  5247. return cmp;
  5248. cmp = urlMatcherSort(a, b);
  5249. if (cmp !== 0)
  5250. return cmp;
  5251. return idSort(a, b);
  5252. };
  5253. /**
  5254. * Updates URL and responds to URL changes
  5255. *
  5256. * ### Deprecation warning:
  5257. * This class is now considered to be an internal API
  5258. * Use the [[UrlService]] instead.
  5259. * For configuring URL rules, use the [[UrlRulesApi]] which can be found as [[UrlService.rules]].
  5260. *
  5261. * This class updates the URL when the state changes.
  5262. * It also responds to changes in the URL.
  5263. */
  5264. var UrlRouter = /** @class */ (function () {
  5265. /** @hidden */
  5266. function UrlRouter(router) {
  5267. /** @hidden */ this._sortFn = defaultRuleSortFn;
  5268. /** @hidden */ this._rules = [];
  5269. /** @hidden */ this.interceptDeferred = false;
  5270. /** @hidden */ this._id = 0;
  5271. /** @hidden */ this._sorted = false;
  5272. this._router = router;
  5273. this.urlRuleFactory = new UrlRuleFactory(router);
  5274. createProxyFunctions(val(UrlRouter.prototype), this, val(this));
  5275. }
  5276. /** @internalapi */
  5277. UrlRouter.prototype.dispose = function () {
  5278. this.listen(false);
  5279. this._rules = [];
  5280. delete this._otherwiseFn;
  5281. };
  5282. /** @inheritdoc */
  5283. UrlRouter.prototype.sort = function (compareFn) {
  5284. this._rules = this.stableSort(this._rules, this._sortFn = compareFn || this._sortFn);
  5285. this._sorted = true;
  5286. };
  5287. UrlRouter.prototype.ensureSorted = function () {
  5288. this._sorted || this.sort();
  5289. };
  5290. UrlRouter.prototype.stableSort = function (arr, compareFn) {
  5291. var arrOfWrapper = arr.map(function (elem, idx) { return ({ elem: elem, idx: idx }); });
  5292. arrOfWrapper.sort(function (wrapperA, wrapperB) {
  5293. var cmpDiff = compareFn(wrapperA.elem, wrapperB.elem);
  5294. return cmpDiff === 0
  5295. ? wrapperA.idx - wrapperB.idx
  5296. : cmpDiff;
  5297. });
  5298. return arrOfWrapper.map(function (wrapper) { return wrapper.elem; });
  5299. };
  5300. /**
  5301. * Given a URL, check all rules and return the best [[MatchResult]]
  5302. * @param url
  5303. * @returns {MatchResult}
  5304. */
  5305. UrlRouter.prototype.match = function (url) {
  5306. var _this = this;
  5307. this.ensureSorted();
  5308. url = extend({ path: '', search: {}, hash: '' }, url);
  5309. var rules = this.rules();
  5310. if (this._otherwiseFn)
  5311. rules.push(this._otherwiseFn);
  5312. // Checks a single rule. Returns { rule: rule, match: match, weight: weight } if it matched, or undefined
  5313. var checkRule = function (rule) {
  5314. var match = rule.match(url, _this._router);
  5315. return match && { match: match, rule: rule, weight: rule.matchPriority(match) };
  5316. };
  5317. // The rules are pre-sorted.
  5318. // - Find the first matching rule.
  5319. // - Find any other matching rule that sorted *exactly the same*, according to `.sort()`.
  5320. // - Choose the rule with the highest match weight.
  5321. var best;
  5322. for (var i = 0; i < rules.length; i++) {
  5323. // Stop when there is a 'best' rule and the next rule sorts differently than it.
  5324. if (best && this._sortFn(rules[i], best.rule) !== 0)
  5325. break;
  5326. var current = checkRule(rules[i]);
  5327. // Pick the best MatchResult
  5328. best = (!best || current && current.weight > best.weight) ? current : best;
  5329. }
  5330. return best;
  5331. };
  5332. /** @inheritdoc */
  5333. UrlRouter.prototype.sync = function (evt) {
  5334. if (evt && evt.defaultPrevented)
  5335. return;
  5336. var router = this._router, $url = router.urlService, $state = router.stateService;
  5337. var url = {
  5338. path: $url.path(), search: $url.search(), hash: $url.hash(),
  5339. };
  5340. var best = this.match(url);
  5341. var applyResult = pattern([
  5342. [isString, function (newurl) { return $url.url(newurl, true); }],
  5343. [TargetState.isDef, function (def) { return $state.go(def.state, def.params, def.options); }],
  5344. [is(TargetState), function (target) { return $state.go(target.state(), target.params(), target.options()); }],
  5345. ]);
  5346. applyResult(best && best.rule.handler(best.match, url, router));
  5347. };
  5348. /** @inheritdoc */
  5349. UrlRouter.prototype.listen = function (enabled) {
  5350. var _this = this;
  5351. if (enabled === false) {
  5352. this._stopFn && this._stopFn();
  5353. delete this._stopFn;
  5354. }
  5355. else {
  5356. return this._stopFn = this._stopFn || this._router.urlService.onChange(function (evt) { return _this.sync(evt); });
  5357. }
  5358. };
  5359. /**
  5360. * Internal API.
  5361. * @internalapi
  5362. */
  5363. UrlRouter.prototype.update = function (read) {
  5364. var $url = this._router.locationService;
  5365. if (read) {
  5366. this.location = $url.path();
  5367. return;
  5368. }
  5369. if ($url.path() === this.location)
  5370. return;
  5371. $url.url(this.location, true);
  5372. };
  5373. /**
  5374. * Internal API.
  5375. *
  5376. * Pushes a new location to the browser history.
  5377. *
  5378. * @internalapi
  5379. * @param urlMatcher
  5380. * @param params
  5381. * @param options
  5382. */
  5383. UrlRouter.prototype.push = function (urlMatcher, params, options) {
  5384. var replace = options && !!options.replace;
  5385. this._router.urlService.url(urlMatcher.format(params || {}), replace);
  5386. };
  5387. /**
  5388. * Builds and returns a URL with interpolated parameters
  5389. *
  5390. * #### Example:
  5391. * ```js
  5392. * matcher = $umf.compile("/about/:person");
  5393. * params = { person: "bob" };
  5394. * $bob = $urlRouter.href(matcher, params);
  5395. * // $bob == "/about/bob";
  5396. * ```
  5397. *
  5398. * @param urlMatcher The [[UrlMatcher]] object which is used as the template of the URL to generate.
  5399. * @param params An object of parameter values to fill the matcher's required parameters.
  5400. * @param options Options object. The options are:
  5401. *
  5402. * - **`absolute`** - {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl".
  5403. *
  5404. * @returns Returns the fully compiled URL, or `null` if `params` fail validation against `urlMatcher`
  5405. */
  5406. UrlRouter.prototype.href = function (urlMatcher, params, options) {
  5407. var url = urlMatcher.format(params);
  5408. if (url == null)
  5409. return null;
  5410. options = options || { absolute: false };
  5411. var cfg = this._router.urlService.config;
  5412. var isHtml5 = cfg.html5Mode();
  5413. if (!isHtml5 && url !== null) {
  5414. url = "#" + cfg.hashPrefix() + url;
  5415. }
  5416. url = appendBasePath(url, isHtml5, options.absolute, cfg.baseHref());
  5417. if (!options.absolute || !url) {
  5418. return url;
  5419. }
  5420. var slash = (!isHtml5 && url ? '/' : ''), port = cfg.port();
  5421. port = (port === 80 || port === 443 ? '' : ':' + port);
  5422. return [cfg.protocol(), '://', cfg.host(), port, slash, url].join('');
  5423. };
  5424. /**
  5425. * Manually adds a URL Rule.
  5426. *
  5427. * Usually, a url rule is added using [[StateDeclaration.url]] or [[when]].
  5428. * This api can be used directly for more control (to register a [[BaseUrlRule]], for example).
  5429. * Rules can be created using [[UrlRouter.urlRuleFactory]], or create manually as simple objects.
  5430. *
  5431. * A rule should have a `match` function which returns truthy if the rule matched.
  5432. * It should also have a `handler` function which is invoked if the rule is the best match.
  5433. *
  5434. * @return a function that deregisters the rule
  5435. */
  5436. UrlRouter.prototype.rule = function (rule) {
  5437. var _this = this;
  5438. if (!UrlRuleFactory.isUrlRule(rule))
  5439. throw new Error("invalid rule");
  5440. rule.$id = this._id++;
  5441. rule.priority = rule.priority || 0;
  5442. this._rules.push(rule);
  5443. this._sorted = false;
  5444. return function () { return _this.removeRule(rule); };
  5445. };
  5446. /** @inheritdoc */
  5447. UrlRouter.prototype.removeRule = function (rule) {
  5448. removeFrom(this._rules, rule);
  5449. };
  5450. /** @inheritdoc */
  5451. UrlRouter.prototype.rules = function () {
  5452. this.ensureSorted();
  5453. return this._rules.slice();
  5454. };
  5455. /** @inheritdoc */
  5456. UrlRouter.prototype.otherwise = function (handler) {
  5457. var handlerFn = getHandlerFn(handler);
  5458. this._otherwiseFn = this.urlRuleFactory.create(val(true), handlerFn);
  5459. this._sorted = false;
  5460. };
  5461. /** @inheritdoc */
  5462. UrlRouter.prototype.initial = function (handler) {
  5463. var handlerFn = getHandlerFn(handler);
  5464. var matchFn = function (urlParts, router) {
  5465. return router.globals.transitionHistory.size() === 0 && !!/^\/?$/.exec(urlParts.path);
  5466. };
  5467. this.rule(this.urlRuleFactory.create(matchFn, handlerFn));
  5468. };
  5469. /** @inheritdoc */
  5470. UrlRouter.prototype.when = function (matcher, handler, options) {
  5471. var rule = this.urlRuleFactory.create(matcher, handler);
  5472. if (isDefined(options && options.priority))
  5473. rule.priority = options.priority;
  5474. this.rule(rule);
  5475. return rule;
  5476. };
  5477. /** @inheritdoc */
  5478. UrlRouter.prototype.deferIntercept = function (defer) {
  5479. if (defer === undefined)
  5480. defer = true;
  5481. this.interceptDeferred = defer;
  5482. };
  5483. return UrlRouter;
  5484. }());
  5485. function getHandlerFn(handler) {
  5486. if (!isFunction(handler) && !isString(handler) && !is(TargetState)(handler) && !TargetState.isDef(handler)) {
  5487. throw new Error("'handler' must be a string, function, TargetState, or have a state: 'newtarget' property");
  5488. }
  5489. return isFunction(handler) ? handler : val(handler);
  5490. }
  5491. /**
  5492. * @coreapi
  5493. * @module view
  5494. */ /** for typedoc */
  5495. /**
  5496. * The View service
  5497. *
  5498. * This service pairs existing `ui-view` components (which live in the DOM)
  5499. * with view configs (from the state declaration objects: [[StateDeclaration.views]]).
  5500. *
  5501. * - After a successful Transition, the views from the newly entered states are activated via [[activateViewConfig]].
  5502. * The views from exited states are deactivated via [[deactivateViewConfig]].
  5503. * (See: the [[registerActivateViews]] Transition Hook)
  5504. *
  5505. * - As `ui-view` components pop in and out of existence, they register themselves using [[registerUIView]].
  5506. *
  5507. * - When the [[sync]] function is called, the registered `ui-view`(s) ([[ActiveUIView]])
  5508. * are configured with the matching [[ViewConfig]](s)
  5509. *
  5510. */
  5511. var ViewService = /** @class */ (function () {
  5512. function ViewService() {
  5513. var _this = this;
  5514. this._uiViews = [];
  5515. this._viewConfigs = [];
  5516. this._viewConfigFactories = {};
  5517. this._pluginapi = {
  5518. _rootViewContext: this._rootViewContext.bind(this),
  5519. _viewConfigFactory: this._viewConfigFactory.bind(this),
  5520. _registeredUIViews: function () { return _this._uiViews; },
  5521. _activeViewConfigs: function () { return _this._viewConfigs; },
  5522. };
  5523. }
  5524. ViewService.prototype._rootViewContext = function (context) {
  5525. return this._rootContext = context || this._rootContext;
  5526. };
  5527. ViewService.prototype._viewConfigFactory = function (viewType, factory) {
  5528. this._viewConfigFactories[viewType] = factory;
  5529. };
  5530. ViewService.prototype.createViewConfig = function (path, decl) {
  5531. var cfgFactory = this._viewConfigFactories[decl.$type];
  5532. if (!cfgFactory)
  5533. throw new Error("ViewService: No view config factory registered for type " + decl.$type);
  5534. var cfgs = cfgFactory(path, decl);
  5535. return isArray(cfgs) ? cfgs : [cfgs];
  5536. };
  5537. /**
  5538. * Deactivates a ViewConfig.
  5539. *
  5540. * This function deactivates a `ViewConfig`.
  5541. * After calling [[sync]], it will un-pair from any `ui-view` with which it is currently paired.
  5542. *
  5543. * @param viewConfig The ViewConfig view to deregister.
  5544. */
  5545. ViewService.prototype.deactivateViewConfig = function (viewConfig) {
  5546. trace.traceViewServiceEvent("<- Removing", viewConfig);
  5547. removeFrom(this._viewConfigs, viewConfig);
  5548. };
  5549. ViewService.prototype.activateViewConfig = function (viewConfig) {
  5550. trace.traceViewServiceEvent("-> Registering", viewConfig);
  5551. this._viewConfigs.push(viewConfig);
  5552. };
  5553. ViewService.prototype.sync = function () {
  5554. var _this = this;
  5555. var uiViewsByFqn = this._uiViews.map(function (uiv) { return [uiv.fqn, uiv]; }).reduce(applyPairs, {});
  5556. // Return a weighted depth value for a uiView.
  5557. // The depth is the nesting depth of ui-views (based on FQN; times 10,000)
  5558. // plus the depth of the state that is populating the uiView
  5559. function uiViewDepth(uiView) {
  5560. var stateDepth = function (context) {
  5561. return context && context.parent ? stateDepth(context.parent) + 1 : 1;
  5562. };
  5563. return (uiView.fqn.split(".").length * 10000) + stateDepth(uiView.creationContext);
  5564. }
  5565. // Return the ViewConfig's context's depth in the context tree.
  5566. function viewConfigDepth(config) {
  5567. var context = config.viewDecl.$context, count = 0;
  5568. while (++count && context.parent)
  5569. context = context.parent;
  5570. return count;
  5571. }
  5572. // Given a depth function, returns a compare function which can return either ascending or descending order
  5573. var depthCompare = curry(function (depthFn, posNeg, left, right) { return posNeg * (depthFn(left) - depthFn(right)); });
  5574. var matchingConfigPair = function (uiView) {
  5575. var matchingConfigs = _this._viewConfigs.filter(ViewService.matches(uiViewsByFqn, uiView));
  5576. if (matchingConfigs.length > 1) {
  5577. // This is OK. Child states can target a ui-view that the parent state also targets (the child wins)
  5578. // Sort by depth and return the match from the deepest child
  5579. // console.log(`Multiple matching view configs for ${uiView.fqn}`, matchingConfigs);
  5580. matchingConfigs.sort(depthCompare(viewConfigDepth, -1)); // descending
  5581. }
  5582. return [uiView, matchingConfigs[0]];
  5583. };
  5584. var configureUIView = function (_a) {
  5585. var uiView = _a[0], viewConfig = _a[1];
  5586. // If a parent ui-view is reconfigured, it could destroy child ui-views.
  5587. // Before configuring a child ui-view, make sure it's still in the active uiViews array.
  5588. if (_this._uiViews.indexOf(uiView) !== -1)
  5589. uiView.configUpdated(viewConfig);
  5590. };
  5591. // Sort views by FQN and state depth. Process uiviews nearest the root first.
  5592. var pairs$$1 = this._uiViews.sort(depthCompare(uiViewDepth, 1)).map(matchingConfigPair);
  5593. trace.traceViewSync(pairs$$1);
  5594. pairs$$1.forEach(configureUIView);
  5595. };
  5596. /**
  5597. * Registers a `ui-view` component
  5598. *
  5599. * When a `ui-view` component is created, it uses this method to register itself.
  5600. * After registration the [[sync]] method is used to ensure all `ui-view` are configured with the proper [[ViewConfig]].
  5601. *
  5602. * Note: the `ui-view` component uses the `ViewConfig` to determine what view should be loaded inside the `ui-view`,
  5603. * and what the view's state context is.
  5604. *
  5605. * Note: There is no corresponding `deregisterUIView`.
  5606. * A `ui-view` should hang on to the return value of `registerUIView` and invoke it to deregister itself.
  5607. *
  5608. * @param uiView The metadata for a UIView
  5609. * @return a de-registration function used when the view is destroyed.
  5610. */
  5611. ViewService.prototype.registerUIView = function (uiView) {
  5612. trace.traceViewServiceUIViewEvent("-> Registering", uiView);
  5613. var uiViews = this._uiViews;
  5614. var fqnAndTypeMatches = function (uiv) { return uiv.fqn === uiView.fqn && uiv.$type === uiView.$type; };
  5615. if (uiViews.filter(fqnAndTypeMatches).length)
  5616. trace.traceViewServiceUIViewEvent("!!!! duplicate uiView named:", uiView);
  5617. uiViews.push(uiView);
  5618. this.sync();
  5619. return function () {
  5620. var idx = uiViews.indexOf(uiView);
  5621. if (idx === -1) {
  5622. trace.traceViewServiceUIViewEvent("Tried removing non-registered uiView", uiView);
  5623. return;
  5624. }
  5625. trace.traceViewServiceUIViewEvent("<- Deregistering", uiView);
  5626. removeFrom(uiViews)(uiView);
  5627. };
  5628. };
  5629. /**
  5630. * Returns the list of views currently available on the page, by fully-qualified name.
  5631. *
  5632. * @return {Array} Returns an array of fully-qualified view names.
  5633. */
  5634. ViewService.prototype.available = function () {
  5635. return this._uiViews.map(prop("fqn"));
  5636. };
  5637. /**
  5638. * Returns the list of views on the page containing loaded content.
  5639. *
  5640. * @return {Array} Returns an array of fully-qualified view names.
  5641. */
  5642. ViewService.prototype.active = function () {
  5643. return this._uiViews.filter(prop("$config")).map(prop("name"));
  5644. };
  5645. /**
  5646. * Normalizes a view's name from a state.views configuration block.
  5647. *
  5648. * This should be used by a framework implementation to calculate the values for
  5649. * [[_ViewDeclaration.$uiViewName]] and [[_ViewDeclaration.$uiViewContextAnchor]].
  5650. *
  5651. * @param context the context object (state declaration) that the view belongs to
  5652. * @param rawViewName the name of the view, as declared in the [[StateDeclaration.views]]
  5653. *
  5654. * @returns the normalized uiViewName and uiViewContextAnchor that the view targets
  5655. */
  5656. ViewService.normalizeUIViewTarget = function (context, rawViewName) {
  5657. if (rawViewName === void 0) { rawViewName = ""; }
  5658. // TODO: Validate incoming view name with a regexp to allow:
  5659. // ex: "view.name@foo.bar" , "^.^.view.name" , "view.name@^.^" , "" ,
  5660. // "@" , "$default@^" , "!$default.$default" , "!foo.bar"
  5661. var viewAtContext = rawViewName.split("@");
  5662. var uiViewName = viewAtContext[0] || "$default"; // default to unnamed view
  5663. var uiViewContextAnchor = isString(viewAtContext[1]) ? viewAtContext[1] : "^"; // default to parent context
  5664. // Handle relative view-name sugar syntax.
  5665. // Matches rawViewName "^.^.^.foo.bar" into array: ["^.^.^.foo.bar", "^.^.^", "foo.bar"],
  5666. var relativeViewNameSugar = /^(\^(?:\.\^)*)\.(.*$)/.exec(uiViewName);
  5667. if (relativeViewNameSugar) {
  5668. // Clobbers existing contextAnchor (rawViewName validation will fix this)
  5669. uiViewContextAnchor = relativeViewNameSugar[1]; // set anchor to "^.^.^"
  5670. uiViewName = relativeViewNameSugar[2]; // set view-name to "foo.bar"
  5671. }
  5672. if (uiViewName.charAt(0) === '!') {
  5673. uiViewName = uiViewName.substr(1);
  5674. uiViewContextAnchor = ""; // target absolutely from root
  5675. }
  5676. // handle parent relative targeting "^.^.^"
  5677. var relativeMatch = /^(\^(?:\.\^)*)$/;
  5678. if (relativeMatch.exec(uiViewContextAnchor)) {
  5679. var anchor = uiViewContextAnchor.split(".").reduce((function (anchor, x) { return anchor.parent; }), context);
  5680. uiViewContextAnchor = anchor.name;
  5681. }
  5682. else if (uiViewContextAnchor === '.') {
  5683. uiViewContextAnchor = context.name;
  5684. }
  5685. return { uiViewName: uiViewName, uiViewContextAnchor: uiViewContextAnchor };
  5686. };
  5687. /**
  5688. * Given a ui-view and a ViewConfig, determines if they "match".
  5689. *
  5690. * A ui-view has a fully qualified name (fqn) and a context object. The fqn is built from its overall location in
  5691. * the DOM, describing its nesting relationship to any parent ui-view tags it is nested inside of.
  5692. *
  5693. * A ViewConfig has a target ui-view name and a context anchor. The ui-view name can be a simple name, or
  5694. * can be a segmented ui-view path, describing a portion of a ui-view fqn.
  5695. *
  5696. * In order for a ui-view to match ViewConfig, ui-view's $type must match the ViewConfig's $type
  5697. *
  5698. * If the ViewConfig's target ui-view name is a simple name (no dots), then a ui-view matches if:
  5699. * - the ui-view's name matches the ViewConfig's target name
  5700. * - the ui-view's context matches the ViewConfig's anchor
  5701. *
  5702. * If the ViewConfig's target ui-view name is a segmented name (with dots), then a ui-view matches if:
  5703. * - There exists a parent ui-view where:
  5704. * - the parent ui-view's name matches the first segment (index 0) of the ViewConfig's target name
  5705. * - the parent ui-view's context matches the ViewConfig's anchor
  5706. * - And the remaining segments (index 1..n) of the ViewConfig's target name match the tail of the ui-view's fqn
  5707. *
  5708. * Example:
  5709. *
  5710. * DOM:
  5711. * <ui-view> <!-- created in the root context (name: "") -->
  5712. * <ui-view name="foo"> <!-- created in the context named: "A" -->
  5713. * <ui-view> <!-- created in the context named: "A.B" -->
  5714. * <ui-view name="bar"> <!-- created in the context named: "A.B.C" -->
  5715. * </ui-view>
  5716. * </ui-view>
  5717. * </ui-view>
  5718. * </ui-view>
  5719. *
  5720. * uiViews: [
  5721. * { fqn: "$default", creationContext: { name: "" } },
  5722. * { fqn: "$default.foo", creationContext: { name: "A" } },
  5723. * { fqn: "$default.foo.$default", creationContext: { name: "A.B" } }
  5724. * { fqn: "$default.foo.$default.bar", creationContext: { name: "A.B.C" } }
  5725. * ]
  5726. *
  5727. * These four view configs all match the ui-view with the fqn: "$default.foo.$default.bar":
  5728. *
  5729. * - ViewConfig1: { uiViewName: "bar", uiViewContextAnchor: "A.B.C" }
  5730. * - ViewConfig2: { uiViewName: "$default.bar", uiViewContextAnchor: "A.B" }
  5731. * - ViewConfig3: { uiViewName: "foo.$default.bar", uiViewContextAnchor: "A" }
  5732. * - ViewConfig4: { uiViewName: "$default.foo.$default.bar", uiViewContextAnchor: "" }
  5733. *
  5734. * Using ViewConfig3 as an example, it matches the ui-view with fqn "$default.foo.$default.bar" because:
  5735. * - The ViewConfig's segmented target name is: [ "foo", "$default", "bar" ]
  5736. * - There exists a parent ui-view (which has fqn: "$default.foo") where:
  5737. * - the parent ui-view's name "foo" matches the first segment "foo" of the ViewConfig's target name
  5738. * - the parent ui-view's context "A" matches the ViewConfig's anchor context "A"
  5739. * - And the remaining segments [ "$default", "bar" ].join("."_ of the ViewConfig's target name match
  5740. * the tail of the ui-view's fqn "default.bar"
  5741. *
  5742. * @internalapi
  5743. */
  5744. ViewService.matches = function (uiViewsByFqn, uiView) { return function (viewConfig) {
  5745. // Don't supply an ng1 ui-view with an ng2 ViewConfig, etc
  5746. if (uiView.$type !== viewConfig.viewDecl.$type)
  5747. return false;
  5748. // Split names apart from both viewConfig and uiView into segments
  5749. var vc = viewConfig.viewDecl;
  5750. var vcSegments = vc.$uiViewName.split(".");
  5751. var uivSegments = uiView.fqn.split(".");
  5752. // Check if the tails of the segment arrays match. ex, these arrays' tails match:
  5753. // vc: ["foo", "bar"], uiv fqn: ["$default", "foo", "bar"]
  5754. if (!equals(vcSegments, uivSegments.slice(0 - vcSegments.length)))
  5755. return false;
  5756. // Now check if the fqn ending at the first segment of the viewConfig matches the context:
  5757. // ["$default", "foo"].join(".") == "$default.foo", does the ui-view $default.foo context match?
  5758. var negOffset = (1 - vcSegments.length) || undefined;
  5759. var fqnToFirstSegment = uivSegments.slice(0, negOffset).join(".");
  5760. var uiViewContext = uiViewsByFqn[fqnToFirstSegment].creationContext;
  5761. return vc.$uiViewContextAnchor === (uiViewContext && uiViewContext.name);
  5762. }; };
  5763. return ViewService;
  5764. }());
  5765. /**
  5766. * @coreapi
  5767. * @module core
  5768. */ /** */
  5769. /**
  5770. * Global router state
  5771. *
  5772. * This is where we hold the global mutable state such as current state, current
  5773. * params, current transition, etc.
  5774. */
  5775. var UIRouterGlobals = /** @class */ (function () {
  5776. function UIRouterGlobals() {
  5777. /**
  5778. * Current parameter values
  5779. *
  5780. * The parameter values from the latest successful transition
  5781. */
  5782. this.params = new StateParams();
  5783. /** @internalapi */
  5784. this.lastStartedTransitionId = -1;
  5785. /** @internalapi */
  5786. this.transitionHistory = new Queue([], 1);
  5787. /** @internalapi */
  5788. this.successfulTransitions = new Queue([], 1);
  5789. }
  5790. UIRouterGlobals.prototype.dispose = function () {
  5791. this.transitionHistory.clear();
  5792. this.successfulTransitions.clear();
  5793. this.transition = null;
  5794. };
  5795. return UIRouterGlobals;
  5796. }());
  5797. /**
  5798. * @coreapi
  5799. * @module url
  5800. */ /** */
  5801. /** @hidden */
  5802. var makeStub = function (keys) {
  5803. return keys.reduce(function (acc, key) { return (acc[key] = notImplemented(key), acc); }, { dispose: noop$1 });
  5804. };
  5805. /** @hidden */ var locationServicesFns = ["url", "path", "search", "hash", "onChange"];
  5806. /** @hidden */ var locationConfigFns = ["port", "protocol", "host", "baseHref", "html5Mode", "hashPrefix"];
  5807. /** @hidden */ var umfFns = ["type", "caseInsensitive", "strictMode", "defaultSquashPolicy"];
  5808. /** @hidden */ var rulesFns = ["sort", "when", "initial", "otherwise", "rules", "rule", "removeRule"];
  5809. /** @hidden */ var syncFns = ["deferIntercept", "listen", "sync", "match"];
  5810. /**
  5811. * API for URL management
  5812. */
  5813. var UrlService = /** @class */ (function () {
  5814. /** @hidden */
  5815. function UrlService(router, lateBind) {
  5816. if (lateBind === void 0) { lateBind = true; }
  5817. this.router = router;
  5818. this.rules = {};
  5819. this.config = {};
  5820. // proxy function calls from UrlService to the LocationService/LocationConfig
  5821. var locationServices = function () { return router.locationService; };
  5822. createProxyFunctions(locationServices, this, locationServices, locationServicesFns, lateBind);
  5823. var locationConfig = function () { return router.locationConfig; };
  5824. createProxyFunctions(locationConfig, this.config, locationConfig, locationConfigFns, lateBind);
  5825. var umf = function () { return router.urlMatcherFactory; };
  5826. createProxyFunctions(umf, this.config, umf, umfFns);
  5827. var urlRouter = function () { return router.urlRouter; };
  5828. createProxyFunctions(urlRouter, this.rules, urlRouter, rulesFns);
  5829. createProxyFunctions(urlRouter, this, urlRouter, syncFns);
  5830. }
  5831. UrlService.prototype.url = function (newurl, replace, state) { return; };
  5832. /** @inheritdoc */
  5833. UrlService.prototype.path = function () { return; };
  5834. /** @inheritdoc */
  5835. UrlService.prototype.search = function () { return; };
  5836. /** @inheritdoc */
  5837. UrlService.prototype.hash = function () { return; };
  5838. /** @inheritdoc */
  5839. UrlService.prototype.onChange = function (callback) { return; };
  5840. /**
  5841. * Returns the current URL parts
  5842. *
  5843. * This method returns the current URL components as a [[UrlParts]] object.
  5844. *
  5845. * @returns the current url parts
  5846. */
  5847. UrlService.prototype.parts = function () {
  5848. return { path: this.path(), search: this.search(), hash: this.hash() };
  5849. };
  5850. UrlService.prototype.dispose = function () { };
  5851. /** @inheritdoc */
  5852. UrlService.prototype.sync = function (evt) { return; };
  5853. /** @inheritdoc */
  5854. UrlService.prototype.listen = function (enabled) { return; };
  5855. /** @inheritdoc */
  5856. UrlService.prototype.deferIntercept = function (defer) { return; };
  5857. /** @inheritdoc */
  5858. UrlService.prototype.match = function (urlParts) { return; };
  5859. /** @hidden */
  5860. UrlService.locationServiceStub = makeStub(locationServicesFns);
  5861. /** @hidden */
  5862. UrlService.locationConfigStub = makeStub(locationConfigFns);
  5863. return UrlService;
  5864. }());
  5865. /**
  5866. * @coreapi
  5867. * @module core
  5868. */ /** */
  5869. /** @hidden */
  5870. var _routerInstance = 0;
  5871. /**
  5872. * The master class used to instantiate an instance of UI-Router.
  5873. *
  5874. * UI-Router (for each specific framework) will create an instance of this class during bootstrap.
  5875. * This class instantiates and wires the UI-Router services together.
  5876. *
  5877. * After a new instance of the UIRouter class is created, it should be configured for your app.
  5878. * For instance, app states should be registered with the [[UIRouter.stateRegistry]].
  5879. *
  5880. * ---
  5881. *
  5882. * Normally the framework code will bootstrap UI-Router.
  5883. * If you are bootstrapping UIRouter manually, tell it to monitor the URL by calling
  5884. * [[UrlService.listen]] then [[UrlService.sync]].
  5885. */
  5886. var UIRouter = /** @class */ (function () {
  5887. /**
  5888. * Creates a new `UIRouter` object
  5889. *
  5890. * @param locationService a [[LocationServices]] implementation
  5891. * @param locationConfig a [[LocationConfig]] implementation
  5892. * @internalapi
  5893. */
  5894. function UIRouter(locationService, locationConfig) {
  5895. if (locationService === void 0) { locationService = UrlService.locationServiceStub; }
  5896. if (locationConfig === void 0) { locationConfig = UrlService.locationConfigStub; }
  5897. this.locationService = locationService;
  5898. this.locationConfig = locationConfig;
  5899. /** @hidden */ this.$id = _routerInstance++;
  5900. /** @hidden */ this._disposed = false;
  5901. /** @hidden */ this._disposables = [];
  5902. /** Provides trace information to the console */
  5903. this.trace = trace;
  5904. /** Provides services related to ui-view synchronization */
  5905. this.viewService = new ViewService();
  5906. /** Provides services related to Transitions */
  5907. this.transitionService = new TransitionService(this);
  5908. /** Global router state */
  5909. this.globals = new UIRouterGlobals();
  5910. /**
  5911. * Deprecated for public use. Use [[urlService]] instead.
  5912. * @deprecated Use [[urlService]] instead
  5913. */
  5914. this.urlMatcherFactory = new UrlMatcherFactory();
  5915. /**
  5916. * Deprecated for public use. Use [[urlService]] instead.
  5917. * @deprecated Use [[urlService]] instead
  5918. */
  5919. this.urlRouter = new UrlRouter(this);
  5920. /** Provides a registry for states, and related registration services */
  5921. this.stateRegistry = new StateRegistry(this);
  5922. /** Provides services related to states */
  5923. this.stateService = new StateService(this);
  5924. /** Provides services related to the URL */
  5925. this.urlService = new UrlService(this);
  5926. /** @hidden */
  5927. this._plugins = {};
  5928. this.viewService._pluginapi._rootViewContext(this.stateRegistry.root());
  5929. this.globals.$current = this.stateRegistry.root();
  5930. this.globals.current = this.globals.$current.self;
  5931. this.disposable(this.globals);
  5932. this.disposable(this.stateService);
  5933. this.disposable(this.stateRegistry);
  5934. this.disposable(this.transitionService);
  5935. this.disposable(this.urlRouter);
  5936. this.disposable(locationService);
  5937. this.disposable(locationConfig);
  5938. }
  5939. /** Registers an object to be notified when the router is disposed */
  5940. UIRouter.prototype.disposable = function (disposable) {
  5941. this._disposables.push(disposable);
  5942. };
  5943. /**
  5944. * Disposes this router instance
  5945. *
  5946. * When called, clears resources retained by the router by calling `dispose(this)` on all
  5947. * registered [[disposable]] objects.
  5948. *
  5949. * Or, if a `disposable` object is provided, calls `dispose(this)` on that object only.
  5950. *
  5951. * @param disposable (optional) the disposable to dispose
  5952. */
  5953. UIRouter.prototype.dispose = function (disposable) {
  5954. var _this = this;
  5955. if (disposable && isFunction(disposable.dispose)) {
  5956. disposable.dispose(this);
  5957. return undefined;
  5958. }
  5959. this._disposed = true;
  5960. this._disposables.slice().forEach(function (d) {
  5961. try {
  5962. typeof d.dispose === 'function' && d.dispose(_this);
  5963. removeFrom(_this._disposables, d);
  5964. }
  5965. catch (ignored) { }
  5966. });
  5967. };
  5968. /**
  5969. * Adds a plugin to UI-Router
  5970. *
  5971. * This method adds a UI-Router Plugin.
  5972. * A plugin can enhance or change UI-Router behavior using any public API.
  5973. *
  5974. * #### Example:
  5975. * ```js
  5976. * import { MyCoolPlugin } from "ui-router-cool-plugin";
  5977. *
  5978. * var plugin = router.addPlugin(MyCoolPlugin);
  5979. * ```
  5980. *
  5981. * ### Plugin authoring
  5982. *
  5983. * A plugin is simply a class (or constructor function) which accepts a [[UIRouter]] instance and (optionally) an options object.
  5984. *
  5985. * The plugin can implement its functionality using any of the public APIs of [[UIRouter]].
  5986. * For example, it may configure router options or add a Transition Hook.
  5987. *
  5988. * The plugin can then be published as a separate module.
  5989. *
  5990. * #### Example:
  5991. * ```js
  5992. * export class MyAuthPlugin implements UIRouterPlugin {
  5993. * constructor(router: UIRouter, options: any) {
  5994. * this.name = "MyAuthPlugin";
  5995. * let $transitions = router.transitionService;
  5996. * let $state = router.stateService;
  5997. *
  5998. * let authCriteria = {
  5999. * to: (state) => state.data && state.data.requiresAuth
  6000. * };
  6001. *
  6002. * function authHook(transition: Transition) {
  6003. * let authService = transition.injector().get('AuthService');
  6004. * if (!authService.isAuthenticated()) {
  6005. * return $state.target('login');
  6006. * }
  6007. * }
  6008. *
  6009. * $transitions.onStart(authCriteria, authHook);
  6010. * }
  6011. * }
  6012. * ```
  6013. *
  6014. * @param plugin one of:
  6015. * - a plugin class which implements [[UIRouterPlugin]]
  6016. * - a constructor function for a [[UIRouterPlugin]] which accepts a [[UIRouter]] instance
  6017. * - a factory function which accepts a [[UIRouter]] instance and returns a [[UIRouterPlugin]] instance
  6018. * @param options options to pass to the plugin class/factory
  6019. * @returns the registered plugin instance
  6020. */
  6021. UIRouter.prototype.plugin = function (plugin, options) {
  6022. if (options === void 0) { options = {}; }
  6023. var pluginInstance = new plugin(this, options);
  6024. if (!pluginInstance.name)
  6025. throw new Error("Required property `name` missing on plugin: " + pluginInstance);
  6026. this._disposables.push(pluginInstance);
  6027. return this._plugins[pluginInstance.name] = pluginInstance;
  6028. };
  6029. UIRouter.prototype.getPlugin = function (pluginName) {
  6030. return pluginName ? this._plugins[pluginName] : values(this._plugins);
  6031. };
  6032. return UIRouter;
  6033. }());
  6034. /** @module hooks */ /** */
  6035. function addCoreResolvables(trans) {
  6036. trans.addResolvable({ token: UIRouter, deps: [], resolveFn: function () { return trans.router; }, data: trans.router }, "");
  6037. trans.addResolvable({ token: Transition, deps: [], resolveFn: function () { return trans; }, data: trans }, "");
  6038. trans.addResolvable({ token: '$transition$', deps: [], resolveFn: function () { return trans; }, data: trans }, "");
  6039. trans.addResolvable({ token: '$stateParams', deps: [], resolveFn: function () { return trans.params(); }, data: trans.params() }, "");
  6040. trans.entering().forEach(function (state) {
  6041. trans.addResolvable({ token: '$state$', deps: [], resolveFn: function () { return state; }, data: state }, state);
  6042. });
  6043. }
  6044. var registerAddCoreResolvables = function (transitionService) {
  6045. return transitionService.onCreate({}, addCoreResolvables);
  6046. };
  6047. /** @module hooks */ /** */
  6048. /**
  6049. * A [[TransitionHookFn]] that redirects to a different state or params
  6050. *
  6051. * Registered using `transitionService.onStart({ to: (state) => !!state.redirectTo }, redirectHook);`
  6052. *
  6053. * See [[StateDeclaration.redirectTo]]
  6054. */
  6055. var redirectToHook = function (trans) {
  6056. var redirect = trans.to().redirectTo;
  6057. if (!redirect)
  6058. return;
  6059. var $state = trans.router.stateService;
  6060. function handleResult(result) {
  6061. if (!result)
  6062. return;
  6063. if (result instanceof TargetState)
  6064. return result;
  6065. if (isString(result))
  6066. return $state.target(result, trans.params(), trans.options());
  6067. if (result['state'] || result['params'])
  6068. return $state.target(result['state'] || trans.to(), result['params'] || trans.params(), trans.options());
  6069. }
  6070. if (isFunction(redirect)) {
  6071. return services.$q.when(redirect(trans)).then(handleResult);
  6072. }
  6073. return handleResult(redirect);
  6074. };
  6075. var registerRedirectToHook = function (transitionService) {
  6076. return transitionService.onStart({ to: function (state) { return !!state.redirectTo; } }, redirectToHook);
  6077. };
  6078. /**
  6079. * A factory which creates an onEnter, onExit or onRetain transition hook function
  6080. *
  6081. * The returned function invokes the (for instance) state.onEnter hook when the
  6082. * state is being entered.
  6083. *
  6084. * @hidden
  6085. */
  6086. function makeEnterExitRetainHook(hookName) {
  6087. return function (transition, state) {
  6088. var _state = state.$$state();
  6089. var hookFn = _state[hookName];
  6090. return hookFn(transition, state);
  6091. };
  6092. }
  6093. /**
  6094. * The [[TransitionStateHookFn]] for onExit
  6095. *
  6096. * When the state is being exited, the state's .onExit function is invoked.
  6097. *
  6098. * Registered using `transitionService.onExit({ exiting: (state) => !!state.onExit }, onExitHook);`
  6099. *
  6100. * See: [[IHookRegistry.onExit]]
  6101. */
  6102. var onExitHook = makeEnterExitRetainHook('onExit');
  6103. var registerOnExitHook = function (transitionService) {
  6104. return transitionService.onExit({ exiting: function (state) { return !!state.onExit; } }, onExitHook);
  6105. };
  6106. /**
  6107. * The [[TransitionStateHookFn]] for onRetain
  6108. *
  6109. * When the state was already entered, and is not being exited or re-entered, the state's .onRetain function is invoked.
  6110. *
  6111. * Registered using `transitionService.onRetain({ retained: (state) => !!state.onRetain }, onRetainHook);`
  6112. *
  6113. * See: [[IHookRegistry.onRetain]]
  6114. */
  6115. var onRetainHook = makeEnterExitRetainHook('onRetain');
  6116. var registerOnRetainHook = function (transitionService) {
  6117. return transitionService.onRetain({ retained: function (state) { return !!state.onRetain; } }, onRetainHook);
  6118. };
  6119. /**
  6120. * The [[TransitionStateHookFn]] for onEnter
  6121. *
  6122. * When the state is being entered, the state's .onEnter function is invoked.
  6123. *
  6124. * Registered using `transitionService.onEnter({ entering: (state) => !!state.onEnter }, onEnterHook);`
  6125. *
  6126. * See: [[IHookRegistry.onEnter]]
  6127. */
  6128. var onEnterHook = makeEnterExitRetainHook('onEnter');
  6129. var registerOnEnterHook = function (transitionService) {
  6130. return transitionService.onEnter({ entering: function (state) { return !!state.onEnter; } }, onEnterHook);
  6131. };
  6132. /** @module hooks */
  6133. /** for typedoc */
  6134. /**
  6135. * A [[TransitionHookFn]] which resolves all EAGER Resolvables in the To Path
  6136. *
  6137. * Registered using `transitionService.onStart({}, eagerResolvePath);`
  6138. *
  6139. * When a Transition starts, this hook resolves all the EAGER Resolvables, which the transition then waits for.
  6140. *
  6141. * See [[StateDeclaration.resolve]]
  6142. */
  6143. var eagerResolvePath = function (trans) {
  6144. return new ResolveContext(trans.treeChanges().to)
  6145. .resolvePath("EAGER", trans)
  6146. .then(noop$1);
  6147. };
  6148. var registerEagerResolvePath = function (transitionService) {
  6149. return transitionService.onStart({}, eagerResolvePath, { priority: 1000 });
  6150. };
  6151. /**
  6152. * A [[TransitionHookFn]] which resolves all LAZY Resolvables for the state (and all its ancestors) in the To Path
  6153. *
  6154. * Registered using `transitionService.onEnter({ entering: () => true }, lazyResolveState);`
  6155. *
  6156. * When a State is being entered, this hook resolves all the Resolvables for this state, which the transition then waits for.
  6157. *
  6158. * See [[StateDeclaration.resolve]]
  6159. */
  6160. var lazyResolveState = function (trans, state) {
  6161. return new ResolveContext(trans.treeChanges().to)
  6162. .subContext(state.$$state())
  6163. .resolvePath("LAZY", trans)
  6164. .then(noop$1);
  6165. };
  6166. var registerLazyResolveState = function (transitionService) {
  6167. return transitionService.onEnter({ entering: val(true) }, lazyResolveState, { priority: 1000 });
  6168. };
  6169. /** @module hooks */ /** for typedoc */
  6170. /**
  6171. * A [[TransitionHookFn]] which waits for the views to load
  6172. *
  6173. * Registered using `transitionService.onStart({}, loadEnteringViews);`
  6174. *
  6175. * Allows the views to do async work in [[ViewConfig.load]] before the transition continues.
  6176. * In angular 1, this includes loading the templates.
  6177. */
  6178. var loadEnteringViews = function (transition) {
  6179. var $q = services.$q;
  6180. var enteringViews = transition.views("entering");
  6181. if (!enteringViews.length)
  6182. return;
  6183. return $q.all(enteringViews.map(function (view) { return $q.when(view.load()); })).then(noop$1);
  6184. };
  6185. var registerLoadEnteringViews = function (transitionService) {
  6186. return transitionService.onFinish({}, loadEnteringViews);
  6187. };
  6188. /**
  6189. * A [[TransitionHookFn]] which activates the new views when a transition is successful.
  6190. *
  6191. * Registered using `transitionService.onSuccess({}, activateViews);`
  6192. *
  6193. * After a transition is complete, this hook deactivates the old views from the previous state,
  6194. * and activates the new views from the destination state.
  6195. *
  6196. * See [[ViewService]]
  6197. */
  6198. var activateViews = function (transition) {
  6199. var enteringViews = transition.views("entering");
  6200. var exitingViews = transition.views("exiting");
  6201. if (!enteringViews.length && !exitingViews.length)
  6202. return;
  6203. var $view = transition.router.viewService;
  6204. exitingViews.forEach(function (vc) { return $view.deactivateViewConfig(vc); });
  6205. enteringViews.forEach(function (vc) { return $view.activateViewConfig(vc); });
  6206. $view.sync();
  6207. };
  6208. var registerActivateViews = function (transitionService) {
  6209. return transitionService.onSuccess({}, activateViews);
  6210. };
  6211. /**
  6212. * A [[TransitionHookFn]] which updates global UI-Router state
  6213. *
  6214. * Registered using `transitionService.onBefore({}, updateGlobalState);`
  6215. *
  6216. * Before a [[Transition]] starts, updates the global value of "the current transition" ([[Globals.transition]]).
  6217. * After a successful [[Transition]], updates the global values of "the current state"
  6218. * ([[Globals.current]] and [[Globals.$current]]) and "the current param values" ([[Globals.params]]).
  6219. *
  6220. * See also the deprecated properties:
  6221. * [[StateService.transition]], [[StateService.current]], [[StateService.params]]
  6222. */
  6223. var updateGlobalState = function (trans) {
  6224. var globals = trans.router.globals;
  6225. var transitionSuccessful = function () {
  6226. globals.successfulTransitions.enqueue(trans);
  6227. globals.$current = trans.$to();
  6228. globals.current = globals.$current.self;
  6229. copy(trans.params(), globals.params);
  6230. };
  6231. var clearCurrentTransition = function () {
  6232. // Do not clear globals.transition if a different transition has started in the meantime
  6233. if (globals.transition === trans)
  6234. globals.transition = null;
  6235. };
  6236. trans.onSuccess({}, transitionSuccessful, { priority: 10000 });
  6237. trans.promise.then(clearCurrentTransition, clearCurrentTransition);
  6238. };
  6239. var registerUpdateGlobalState = function (transitionService) {
  6240. return transitionService.onCreate({}, updateGlobalState);
  6241. };
  6242. /**
  6243. * A [[TransitionHookFn]] which updates the URL after a successful transition
  6244. *
  6245. * Registered using `transitionService.onSuccess({}, updateUrl);`
  6246. */
  6247. var updateUrl = function (transition) {
  6248. var options = transition.options();
  6249. var $state = transition.router.stateService;
  6250. var $urlRouter = transition.router.urlRouter;
  6251. // Dont update the url in these situations:
  6252. // The transition was triggered by a URL sync (options.source === 'url')
  6253. // The user doesn't want the url to update (options.location === false)
  6254. // The destination state, and all parents have no navigable url
  6255. if (options.source !== 'url' && options.location && $state.$current.navigable) {
  6256. var urlOptions = { replace: options.location === 'replace' };
  6257. $urlRouter.push($state.$current.navigable.url, $state.params, urlOptions);
  6258. }
  6259. $urlRouter.update(true);
  6260. };
  6261. var registerUpdateUrl = function (transitionService) {
  6262. return transitionService.onSuccess({}, updateUrl, { priority: 9999 });
  6263. };
  6264. /**
  6265. * A [[TransitionHookFn]] that performs lazy loading
  6266. *
  6267. * When entering a state "abc" which has a `lazyLoad` function defined:
  6268. * - Invoke the `lazyLoad` function (unless it is already in process)
  6269. * - Flag the hook function as "in process"
  6270. * - The function should return a promise (that resolves when lazy loading is complete)
  6271. * - Wait for the promise to settle
  6272. * - If the promise resolves to a [[LazyLoadResult]], then register those states
  6273. * - Flag the hook function as "not in process"
  6274. * - If the hook was successful
  6275. * - Remove the `lazyLoad` function from the state declaration
  6276. * - If all the hooks were successful
  6277. * - Retry the transition (by returning a TargetState)
  6278. *
  6279. * ```
  6280. * .state('abc', {
  6281. * component: 'fooComponent',
  6282. * lazyLoad: () => System.import('./fooComponent')
  6283. * });
  6284. * ```
  6285. *
  6286. * See [[StateDeclaration.lazyLoad]]
  6287. */
  6288. var lazyLoadHook = function (transition) {
  6289. var router = transition.router;
  6290. function retryTransition() {
  6291. if (transition.originalTransition().options().source !== 'url') {
  6292. // The original transition was not triggered via url sync
  6293. // The lazy state should be loaded now, so re-try the original transition
  6294. var orig = transition.targetState();
  6295. return router.stateService.target(orig.identifier(), orig.params(), orig.options());
  6296. }
  6297. // The original transition was triggered via url sync
  6298. // Run the URL rules and find the best match
  6299. var $url = router.urlService;
  6300. var result = $url.match($url.parts());
  6301. var rule = result && result.rule;
  6302. // If the best match is a state, redirect the transition (instead
  6303. // of calling sync() which supersedes the current transition)
  6304. if (rule && rule.type === "STATE") {
  6305. var state = rule.state;
  6306. var params = result.match;
  6307. return router.stateService.target(state, params, transition.options());
  6308. }
  6309. // No matching state found, so let .sync() choose the best non-state match/otherwise
  6310. router.urlService.sync();
  6311. }
  6312. var promises = transition.entering()
  6313. .filter(function (state) { return !!state.$$state().lazyLoad; })
  6314. .map(function (state) { return lazyLoadState(transition, state); });
  6315. return services.$q.all(promises).then(retryTransition);
  6316. };
  6317. var registerLazyLoadHook = function (transitionService) {
  6318. return transitionService.onBefore({ entering: function (state) { return !!state.lazyLoad; } }, lazyLoadHook);
  6319. };
  6320. /**
  6321. * Invokes a state's lazy load function
  6322. *
  6323. * @param transition a Transition context
  6324. * @param state the state to lazy load
  6325. * @returns A promise for the lazy load result
  6326. */
  6327. function lazyLoadState(transition, state) {
  6328. var lazyLoadFn = state.$$state().lazyLoad;
  6329. // Store/get the lazy load promise on/from the hookfn so it doesn't get re-invoked
  6330. var promise = lazyLoadFn['_promise'];
  6331. if (!promise) {
  6332. var success = function (result) {
  6333. delete state.lazyLoad;
  6334. delete state.$$state().lazyLoad;
  6335. delete lazyLoadFn['_promise'];
  6336. return result;
  6337. };
  6338. var error = function (err) {
  6339. delete lazyLoadFn['_promise'];
  6340. return services.$q.reject(err);
  6341. };
  6342. promise = lazyLoadFn['_promise'] =
  6343. services.$q.when(lazyLoadFn(transition, state))
  6344. .then(updateStateRegistry)
  6345. .then(success, error);
  6346. }
  6347. /** Register any lazy loaded state definitions */
  6348. function updateStateRegistry(result) {
  6349. if (result && Array.isArray(result.states)) {
  6350. result.states.forEach(function (state) { return transition.router.stateRegistry.register(state); });
  6351. }
  6352. return result;
  6353. }
  6354. return promise;
  6355. }
  6356. /**
  6357. * This class defines a type of hook, such as `onBefore` or `onEnter`.
  6358. * Plugins can define custom hook types, such as sticky states does for `onInactive`.
  6359. *
  6360. * @interalapi
  6361. */
  6362. var TransitionEventType = /** @class */ (function () {
  6363. function TransitionEventType(name, hookPhase, hookOrder, criteriaMatchPath, reverseSort, getResultHandler, getErrorHandler, synchronous) {
  6364. if (reverseSort === void 0) { reverseSort = false; }
  6365. if (getResultHandler === void 0) { getResultHandler = TransitionHook.HANDLE_RESULT; }
  6366. if (getErrorHandler === void 0) { getErrorHandler = TransitionHook.REJECT_ERROR; }
  6367. if (synchronous === void 0) { synchronous = false; }
  6368. this.name = name;
  6369. this.hookPhase = hookPhase;
  6370. this.hookOrder = hookOrder;
  6371. this.criteriaMatchPath = criteriaMatchPath;
  6372. this.reverseSort = reverseSort;
  6373. this.getResultHandler = getResultHandler;
  6374. this.getErrorHandler = getErrorHandler;
  6375. this.synchronous = synchronous;
  6376. }
  6377. return TransitionEventType;
  6378. }());
  6379. /** @module hooks */ /** */
  6380. /**
  6381. * A [[TransitionHookFn]] that skips a transition if it should be ignored
  6382. *
  6383. * This hook is invoked at the end of the onBefore phase.
  6384. *
  6385. * If the transition should be ignored (because no parameter or states changed)
  6386. * then the transition is ignored and not processed.
  6387. */
  6388. function ignoredHook(trans) {
  6389. var ignoredReason = trans._ignoredReason();
  6390. if (!ignoredReason)
  6391. return;
  6392. trace.traceTransitionIgnored(trans);
  6393. var pending = trans.router.globals.transition;
  6394. // The user clicked a link going back to the *current state* ('A')
  6395. // However, there is also a pending transition in flight (to 'B')
  6396. // Abort the transition to 'B' because the user now wants to be back at 'A'.
  6397. if (ignoredReason === 'SameAsCurrent' && pending) {
  6398. pending.abort();
  6399. }
  6400. return Rejection.ignored().toPromise();
  6401. }
  6402. var registerIgnoredTransitionHook = function (transitionService) {
  6403. return transitionService.onBefore({}, ignoredHook, { priority: -9999 });
  6404. };
  6405. /** @module hooks */ /** */
  6406. /**
  6407. * A [[TransitionHookFn]] that rejects the Transition if it is invalid
  6408. *
  6409. * This hook is invoked at the end of the onBefore phase.
  6410. * If the transition is invalid (for example, param values do not validate)
  6411. * then the transition is rejected.
  6412. */
  6413. function invalidTransitionHook(trans) {
  6414. if (!trans.valid()) {
  6415. throw new Error(trans.error());
  6416. }
  6417. }
  6418. var registerInvalidTransitionHook = function (transitionService) {
  6419. return transitionService.onBefore({}, invalidTransitionHook, { priority: -10000 });
  6420. };
  6421. /**
  6422. * @coreapi
  6423. * @module transition
  6424. */
  6425. /** for typedoc */
  6426. /**
  6427. * The default [[Transition]] options.
  6428. *
  6429. * Include this object when applying custom defaults:
  6430. * let reloadOpts = { reload: true, notify: true }
  6431. * let options = defaults(theirOpts, customDefaults, defaultOptions);
  6432. */
  6433. var defaultTransOpts = {
  6434. location: true,
  6435. relative: null,
  6436. inherit: false,
  6437. notify: true,
  6438. reload: false,
  6439. custom: {},
  6440. current: function () { return null; },
  6441. source: "unknown"
  6442. };
  6443. /**
  6444. * This class provides services related to Transitions.
  6445. *
  6446. * - Most importantly, it allows global Transition Hooks to be registered.
  6447. * - It allows the default transition error handler to be set.
  6448. * - It also has a factory function for creating new [[Transition]] objects, (used internally by the [[StateService]]).
  6449. *
  6450. * At bootstrap, [[UIRouter]] creates a single instance (singleton) of this class.
  6451. */
  6452. var TransitionService = /** @class */ (function () {
  6453. /** @hidden */
  6454. function TransitionService(_router) {
  6455. /** @hidden */
  6456. this._transitionCount = 0;
  6457. /** @hidden The transition hook types, such as `onEnter`, `onStart`, etc */
  6458. this._eventTypes = [];
  6459. /** @hidden The registered transition hooks */
  6460. this._registeredHooks = {};
  6461. /** @hidden The paths on a criteria object */
  6462. this._criteriaPaths = {};
  6463. this._router = _router;
  6464. this.$view = _router.viewService;
  6465. this._deregisterHookFns = {};
  6466. this._pluginapi = createProxyFunctions(val(this), {}, val(this), [
  6467. '_definePathType',
  6468. '_defineEvent',
  6469. '_getPathTypes',
  6470. '_getEvents',
  6471. 'getHooks',
  6472. ]);
  6473. this._defineCorePaths();
  6474. this._defineCoreEvents();
  6475. this._registerCoreTransitionHooks();
  6476. }
  6477. /**
  6478. * Registers a [[TransitionHookFn]], called *while a transition is being constructed*.
  6479. *
  6480. * Registers a transition lifecycle hook, which is invoked during transition construction.
  6481. *
  6482. * This low level hook should only be used by plugins.
  6483. * This can be a useful time for plugins to add resolves or mutate the transition as needed.
  6484. * The Sticky States plugin uses this hook to modify the treechanges.
  6485. *
  6486. * ### Lifecycle
  6487. *
  6488. * `onCreate` hooks are invoked *while a transition is being constructed*.
  6489. *
  6490. * ### Return value
  6491. *
  6492. * The hook's return value is ignored
  6493. *
  6494. * @internalapi
  6495. * @param criteria defines which Transitions the Hook should be invoked for.
  6496. * @param callback the hook function which will be invoked.
  6497. * @param options the registration options
  6498. * @returns a function which deregisters the hook.
  6499. */
  6500. TransitionService.prototype.onCreate = function (criteria, callback, options) { return; };
  6501. /** @inheritdoc */
  6502. TransitionService.prototype.onBefore = function (criteria, callback, options) { return; };
  6503. /** @inheritdoc */
  6504. TransitionService.prototype.onStart = function (criteria, callback, options) { return; };
  6505. /** @inheritdoc */
  6506. TransitionService.prototype.onExit = function (criteria, callback, options) { return; };
  6507. /** @inheritdoc */
  6508. TransitionService.prototype.onRetain = function (criteria, callback, options) { return; };
  6509. /** @inheritdoc */
  6510. TransitionService.prototype.onEnter = function (criteria, callback, options) { return; };
  6511. /** @inheritdoc */
  6512. TransitionService.prototype.onFinish = function (criteria, callback, options) { return; };
  6513. /** @inheritdoc */
  6514. TransitionService.prototype.onSuccess = function (criteria, callback, options) { return; };
  6515. /** @inheritdoc */
  6516. TransitionService.prototype.onError = function (criteria, callback, options) { return; };
  6517. /**
  6518. * dispose
  6519. * @internalapi
  6520. */
  6521. TransitionService.prototype.dispose = function (router) {
  6522. values(this._registeredHooks).forEach(function (hooksArray) { return hooksArray.forEach(function (hook) {
  6523. hook._deregistered = true;
  6524. removeFrom(hooksArray, hook);
  6525. }); });
  6526. };
  6527. /**
  6528. * Creates a new [[Transition]] object
  6529. *
  6530. * This is a factory function for creating new Transition objects.
  6531. * It is used internally by the [[StateService]] and should generally not be called by application code.
  6532. *
  6533. * @param fromPath the path to the current state (the from state)
  6534. * @param targetState the target state (destination)
  6535. * @returns a Transition
  6536. */
  6537. TransitionService.prototype.create = function (fromPath, targetState) {
  6538. return new Transition(fromPath, targetState, this._router);
  6539. };
  6540. /** @hidden */
  6541. TransitionService.prototype._defineCoreEvents = function () {
  6542. var Phase = exports.TransitionHookPhase;
  6543. var TH = TransitionHook;
  6544. var paths = this._criteriaPaths;
  6545. var NORMAL_SORT = false, REVERSE_SORT = true;
  6546. var ASYNCHRONOUS = false, SYNCHRONOUS = true;
  6547. this._defineEvent("onCreate", Phase.CREATE, 0, paths.to, NORMAL_SORT, TH.LOG_REJECTED_RESULT, TH.THROW_ERROR, SYNCHRONOUS);
  6548. this._defineEvent("onBefore", Phase.BEFORE, 0, paths.to);
  6549. this._defineEvent("onStart", Phase.RUN, 0, paths.to);
  6550. this._defineEvent("onExit", Phase.RUN, 100, paths.exiting, REVERSE_SORT);
  6551. this._defineEvent("onRetain", Phase.RUN, 200, paths.retained);
  6552. this._defineEvent("onEnter", Phase.RUN, 300, paths.entering);
  6553. this._defineEvent("onFinish", Phase.RUN, 400, paths.to);
  6554. this._defineEvent("onSuccess", Phase.SUCCESS, 0, paths.to, NORMAL_SORT, TH.LOG_REJECTED_RESULT, TH.LOG_ERROR, SYNCHRONOUS);
  6555. this._defineEvent("onError", Phase.ERROR, 0, paths.to, NORMAL_SORT, TH.LOG_REJECTED_RESULT, TH.LOG_ERROR, SYNCHRONOUS);
  6556. };
  6557. /** @hidden */
  6558. TransitionService.prototype._defineCorePaths = function () {
  6559. var STATE = exports.TransitionHookScope.STATE, TRANSITION = exports.TransitionHookScope.TRANSITION;
  6560. this._definePathType("to", TRANSITION);
  6561. this._definePathType("from", TRANSITION);
  6562. this._definePathType("exiting", STATE);
  6563. this._definePathType("retained", STATE);
  6564. this._definePathType("entering", STATE);
  6565. };
  6566. /** @hidden */
  6567. TransitionService.prototype._defineEvent = function (name, hookPhase, hookOrder, criteriaMatchPath, reverseSort, getResultHandler, getErrorHandler, synchronous) {
  6568. if (reverseSort === void 0) { reverseSort = false; }
  6569. if (getResultHandler === void 0) { getResultHandler = TransitionHook.HANDLE_RESULT; }
  6570. if (getErrorHandler === void 0) { getErrorHandler = TransitionHook.REJECT_ERROR; }
  6571. if (synchronous === void 0) { synchronous = false; }
  6572. var eventType = new TransitionEventType(name, hookPhase, hookOrder, criteriaMatchPath, reverseSort, getResultHandler, getErrorHandler, synchronous);
  6573. this._eventTypes.push(eventType);
  6574. makeEvent(this, this, eventType);
  6575. };
  6576. /** @hidden */
  6577. TransitionService.prototype._getEvents = function (phase) {
  6578. var transitionHookTypes = isDefined(phase) ?
  6579. this._eventTypes.filter(function (type) { return type.hookPhase === phase; }) :
  6580. this._eventTypes.slice();
  6581. return transitionHookTypes.sort(function (l, r) {
  6582. var cmpByPhase = l.hookPhase - r.hookPhase;
  6583. return cmpByPhase === 0 ? l.hookOrder - r.hookOrder : cmpByPhase;
  6584. });
  6585. };
  6586. /**
  6587. * Adds a Path to be used as a criterion against a TreeChanges path
  6588. *
  6589. * For example: the `exiting` path in [[HookMatchCriteria]] is a STATE scoped path.
  6590. * It was defined by calling `defineTreeChangesCriterion('exiting', TransitionHookScope.STATE)`
  6591. * Each state in the exiting path is checked against the criteria and returned as part of the match.
  6592. *
  6593. * Another example: the `to` path in [[HookMatchCriteria]] is a TRANSITION scoped path.
  6594. * It was defined by calling `defineTreeChangesCriterion('to', TransitionHookScope.TRANSITION)`
  6595. * Only the tail of the `to` path is checked against the criteria and returned as part of the match.
  6596. *
  6597. * @hidden
  6598. */
  6599. TransitionService.prototype._definePathType = function (name, hookScope) {
  6600. this._criteriaPaths[name] = { name: name, scope: hookScope };
  6601. };
  6602. /** * @hidden */
  6603. TransitionService.prototype._getPathTypes = function () {
  6604. return this._criteriaPaths;
  6605. };
  6606. /** @hidden */
  6607. TransitionService.prototype.getHooks = function (hookName) {
  6608. return this._registeredHooks[hookName];
  6609. };
  6610. /** @hidden */
  6611. TransitionService.prototype._registerCoreTransitionHooks = function () {
  6612. var fns = this._deregisterHookFns;
  6613. fns.addCoreResolves = registerAddCoreResolvables(this);
  6614. fns.ignored = registerIgnoredTransitionHook(this);
  6615. fns.invalid = registerInvalidTransitionHook(this);
  6616. // Wire up redirectTo hook
  6617. fns.redirectTo = registerRedirectToHook(this);
  6618. // Wire up onExit/Retain/Enter state hooks
  6619. fns.onExit = registerOnExitHook(this);
  6620. fns.onRetain = registerOnRetainHook(this);
  6621. fns.onEnter = registerOnEnterHook(this);
  6622. // Wire up Resolve hooks
  6623. fns.eagerResolve = registerEagerResolvePath(this);
  6624. fns.lazyResolve = registerLazyResolveState(this);
  6625. // Wire up the View management hooks
  6626. fns.loadViews = registerLoadEnteringViews(this);
  6627. fns.activateViews = registerActivateViews(this);
  6628. // Updates global state after a transition
  6629. fns.updateGlobals = registerUpdateGlobalState(this);
  6630. // After globals.current is updated at priority: 10000
  6631. fns.updateUrl = registerUpdateUrl(this);
  6632. // Lazy load state trees
  6633. fns.lazyLoad = registerLazyLoadHook(this);
  6634. };
  6635. return TransitionService;
  6636. }());
  6637. /**
  6638. * @coreapi
  6639. * @module state
  6640. */
  6641. /** */
  6642. /**
  6643. * Provides state related service functions
  6644. *
  6645. * This class provides services related to ui-router states.
  6646. * An instance of this class is located on the global [[UIRouter]] object.
  6647. */
  6648. var StateService = /** @class */ (function () {
  6649. /** @internalapi */
  6650. function StateService(router) {
  6651. this.router = router;
  6652. /** @internalapi */
  6653. this.invalidCallbacks = [];
  6654. /** @hidden */
  6655. this._defaultErrorHandler = function $defaultErrorHandler($error$) {
  6656. if ($error$ instanceof Error && $error$.stack) {
  6657. console.error($error$);
  6658. console.error($error$.stack);
  6659. }
  6660. else if ($error$ instanceof Rejection) {
  6661. console.error($error$.toString());
  6662. if ($error$.detail && $error$.detail.stack)
  6663. console.error($error$.detail.stack);
  6664. }
  6665. else {
  6666. console.error($error$);
  6667. }
  6668. };
  6669. var getters = ['current', '$current', 'params', 'transition'];
  6670. var boundFns = Object.keys(StateService.prototype).filter(not(inArray(getters)));
  6671. createProxyFunctions(val(StateService.prototype), this, val(this), boundFns);
  6672. }
  6673. Object.defineProperty(StateService.prototype, "transition", {
  6674. /**
  6675. * The [[Transition]] currently in progress (or null)
  6676. *
  6677. * This is a passthrough through to [[UIRouterGlobals.transition]]
  6678. */
  6679. get: function () { return this.router.globals.transition; },
  6680. enumerable: true,
  6681. configurable: true
  6682. });
  6683. Object.defineProperty(StateService.prototype, "params", {
  6684. /**
  6685. * The latest successful state parameters
  6686. *
  6687. * This is a passthrough through to [[UIRouterGlobals.params]]
  6688. */
  6689. get: function () { return this.router.globals.params; },
  6690. enumerable: true,
  6691. configurable: true
  6692. });
  6693. Object.defineProperty(StateService.prototype, "current", {
  6694. /**
  6695. * The current [[StateDeclaration]]
  6696. *
  6697. * This is a passthrough through to [[UIRouterGlobals.current]]
  6698. */
  6699. get: function () { return this.router.globals.current; },
  6700. enumerable: true,
  6701. configurable: true
  6702. });
  6703. Object.defineProperty(StateService.prototype, "$current", {
  6704. /**
  6705. * The current [[StateObject]]
  6706. *
  6707. * This is a passthrough through to [[UIRouterGlobals.$current]]
  6708. */
  6709. get: function () { return this.router.globals.$current; },
  6710. enumerable: true,
  6711. configurable: true
  6712. });
  6713. /** @internalapi */
  6714. StateService.prototype.dispose = function () {
  6715. this.defaultErrorHandler(noop$1);
  6716. this.invalidCallbacks = [];
  6717. };
  6718. /**
  6719. * Handler for when [[transitionTo]] is called with an invalid state.
  6720. *
  6721. * Invokes the [[onInvalid]] callbacks, in natural order.
  6722. * Each callback's return value is checked in sequence until one of them returns an instance of TargetState.
  6723. * The results of the callbacks are wrapped in $q.when(), so the callbacks may return promises.
  6724. *
  6725. * If a callback returns an TargetState, then it is used as arguments to $state.transitionTo() and the result returned.
  6726. *
  6727. * @internalapi
  6728. */
  6729. StateService.prototype._handleInvalidTargetState = function (fromPath, toState) {
  6730. var _this = this;
  6731. var fromState = PathUtils.makeTargetState(this.router.stateRegistry, fromPath);
  6732. var globals = this.router.globals;
  6733. var latestThing = function () { return globals.transitionHistory.peekTail(); };
  6734. var latest = latestThing();
  6735. var callbackQueue = new Queue(this.invalidCallbacks.slice());
  6736. var injector = new ResolveContext(fromPath).injector();
  6737. var checkForRedirect = function (result) {
  6738. if (!(result instanceof TargetState)) {
  6739. return;
  6740. }
  6741. var target = result;
  6742. // Recreate the TargetState, in case the state is now defined.
  6743. target = _this.target(target.identifier(), target.params(), target.options());
  6744. if (!target.valid()) {
  6745. return Rejection.invalid(target.error()).toPromise();
  6746. }
  6747. if (latestThing() !== latest) {
  6748. return Rejection.superseded().toPromise();
  6749. }
  6750. return _this.transitionTo(target.identifier(), target.params(), target.options());
  6751. };
  6752. function invokeNextCallback() {
  6753. var nextCallback = callbackQueue.dequeue();
  6754. if (nextCallback === undefined)
  6755. return Rejection.invalid(toState.error()).toPromise();
  6756. var callbackResult = services.$q.when(nextCallback(toState, fromState, injector));
  6757. return callbackResult.then(checkForRedirect).then(function (result) { return result || invokeNextCallback(); });
  6758. }
  6759. return invokeNextCallback();
  6760. };
  6761. /**
  6762. * Registers an Invalid State handler
  6763. *
  6764. * Registers a [[OnInvalidCallback]] function to be invoked when [[StateService.transitionTo]]
  6765. * has been called with an invalid state reference parameter
  6766. *
  6767. * Example:
  6768. * ```js
  6769. * stateService.onInvalid(function(to, from, injector) {
  6770. * if (to.name() === 'foo') {
  6771. * let lazyLoader = injector.get('LazyLoadService');
  6772. * return lazyLoader.load('foo')
  6773. * .then(() => stateService.target('foo'));
  6774. * }
  6775. * });
  6776. * ```
  6777. *
  6778. * @param {function} callback invoked when the toState is invalid
  6779. * This function receives the (invalid) toState, the fromState, and an injector.
  6780. * The function may optionally return a [[TargetState]] or a Promise for a TargetState.
  6781. * If one is returned, it is treated as a redirect.
  6782. *
  6783. * @returns a function which deregisters the callback
  6784. */
  6785. StateService.prototype.onInvalid = function (callback) {
  6786. this.invalidCallbacks.push(callback);
  6787. return function deregisterListener() {
  6788. removeFrom(this.invalidCallbacks)(callback);
  6789. }.bind(this);
  6790. };
  6791. /**
  6792. * Reloads the current state
  6793. *
  6794. * A method that force reloads the current state, or a partial state hierarchy.
  6795. * All resolves are re-resolved, and components reinstantiated.
  6796. *
  6797. * #### Example:
  6798. * ```js
  6799. * let app angular.module('app', ['ui.router']);
  6800. *
  6801. * app.controller('ctrl', function ($scope, $state) {
  6802. * $scope.reload = function(){
  6803. * $state.reload();
  6804. * }
  6805. * });
  6806. * ```
  6807. *
  6808. * Note: `reload()` is just an alias for:
  6809. *
  6810. * ```js
  6811. * $state.transitionTo($state.current, $state.params, {
  6812. * reload: true, inherit: false
  6813. * });
  6814. * ```
  6815. *
  6816. * @param reloadState A state name or a state object.
  6817. * If present, this state and all its children will be reloaded, but ancestors will not reload.
  6818. *
  6819. * #### Example:
  6820. * ```js
  6821. * //assuming app application consists of 3 states: 'contacts', 'contacts.detail', 'contacts.detail.item'
  6822. * //and current state is 'contacts.detail.item'
  6823. * let app angular.module('app', ['ui.router']);
  6824. *
  6825. * app.controller('ctrl', function ($scope, $state) {
  6826. * $scope.reload = function(){
  6827. * //will reload 'contact.detail' and nested 'contact.detail.item' states
  6828. * $state.reload('contact.detail');
  6829. * }
  6830. * });
  6831. * ```
  6832. *
  6833. * @returns A promise representing the state of the new transition. See [[StateService.go]]
  6834. */
  6835. StateService.prototype.reload = function (reloadState) {
  6836. return this.transitionTo(this.current, this.params, {
  6837. reload: isDefined(reloadState) ? reloadState : true,
  6838. inherit: false,
  6839. notify: false,
  6840. });
  6841. };
  6842. /**
  6843. * Transition to a different state and/or parameters
  6844. *
  6845. * Convenience method for transitioning to a new state.
  6846. *
  6847. * `$state.go` calls `$state.transitionTo` internally but automatically sets options to
  6848. * `{ location: true, inherit: true, relative: router.globals.$current, notify: true }`.
  6849. * This allows you to use either an absolute or relative `to` argument (because of `relative: router.globals.$current`).
  6850. * It also allows you to specify * only the parameters you'd like to update, while letting unspecified parameters
  6851. * inherit from the current parameter values (because of `inherit: true`).
  6852. *
  6853. * #### Example:
  6854. * ```js
  6855. * let app = angular.module('app', ['ui.router']);
  6856. *
  6857. * app.controller('ctrl', function ($scope, $state) {
  6858. * $scope.changeState = function () {
  6859. * $state.go('contact.detail');
  6860. * };
  6861. * });
  6862. * ```
  6863. *
  6864. * @param to Absolute state name, state object, or relative state path (relative to current state).
  6865. *
  6866. * Some examples:
  6867. *
  6868. * - `$state.go('contact.detail')` - will go to the `contact.detail` state
  6869. * - `$state.go('^')` - will go to the parent state
  6870. * - `$state.go('^.sibling')` - if current state is `home.child`, will go to the `home.sibling` state
  6871. * - `$state.go('.child.grandchild')` - if current state is home, will go to the `home.child.grandchild` state
  6872. *
  6873. * @param params A map of the parameters that will be sent to the state, will populate $stateParams.
  6874. *
  6875. * Any parameters that are not specified will be inherited from current parameter values (because of `inherit: true`).
  6876. * This allows, for example, going to a sibling state that shares parameters defined by a parent state.
  6877. *
  6878. * @param options Transition options
  6879. *
  6880. * @returns {promise} A promise representing the state of the new transition.
  6881. */
  6882. StateService.prototype.go = function (to, params, options) {
  6883. var defautGoOpts = { relative: this.$current, inherit: true };
  6884. var transOpts = defaults(options, defautGoOpts, defaultTransOpts);
  6885. return this.transitionTo(to, params, transOpts);
  6886. };
  6887. /**
  6888. * Creates a [[TargetState]]
  6889. *
  6890. * This is a factory method for creating a TargetState
  6891. *
  6892. * This may be returned from a Transition Hook to redirect a transition, for example.
  6893. */
  6894. StateService.prototype.target = function (identifier, params, options) {
  6895. if (options === void 0) { options = {}; }
  6896. // If we're reloading, find the state object to reload from
  6897. if (isObject(options.reload) && !options.reload.name)
  6898. throw new Error('Invalid reload state object');
  6899. var reg = this.router.stateRegistry;
  6900. options.reloadState = options.reload === true ? reg.root() : reg.matcher.find(options.reload, options.relative);
  6901. if (options.reload && !options.reloadState)
  6902. throw new Error("No such reload state '" + (isString(options.reload) ? options.reload : options.reload.name) + "'");
  6903. return new TargetState(this.router.stateRegistry, identifier, params, options);
  6904. };
  6905. StateService.prototype.getCurrentPath = function () {
  6906. var _this = this;
  6907. var globals = this.router.globals;
  6908. var latestSuccess = globals.successfulTransitions.peekTail();
  6909. var rootPath = function () { return [new PathNode(_this.router.stateRegistry.root())]; };
  6910. return latestSuccess ? latestSuccess.treeChanges().to : rootPath();
  6911. };
  6912. /**
  6913. * Low-level method for transitioning to a new state.
  6914. *
  6915. * The [[go]] method (which uses `transitionTo` internally) is recommended in most situations.
  6916. *
  6917. * #### Example:
  6918. * ```js
  6919. * let app = angular.module('app', ['ui.router']);
  6920. *
  6921. * app.controller('ctrl', function ($scope, $state) {
  6922. * $scope.changeState = function () {
  6923. * $state.transitionTo('contact.detail');
  6924. * };
  6925. * });
  6926. * ```
  6927. *
  6928. * @param to State name or state object.
  6929. * @param toParams A map of the parameters that will be sent to the state,
  6930. * will populate $stateParams.
  6931. * @param options Transition options
  6932. *
  6933. * @returns A promise representing the state of the new transition. See [[go]]
  6934. */
  6935. StateService.prototype.transitionTo = function (to, toParams, options) {
  6936. var _this = this;
  6937. if (toParams === void 0) { toParams = {}; }
  6938. if (options === void 0) { options = {}; }
  6939. var router = this.router;
  6940. var globals = router.globals;
  6941. options = defaults(options, defaultTransOpts);
  6942. var getCurrent = function () {
  6943. return globals.transition;
  6944. };
  6945. options = extend(options, { current: getCurrent });
  6946. var ref = this.target(to, toParams, options);
  6947. var currentPath = this.getCurrentPath();
  6948. if (!ref.exists())
  6949. return this._handleInvalidTargetState(currentPath, ref);
  6950. if (!ref.valid())
  6951. return silentRejection(ref.error());
  6952. /**
  6953. * Special handling for Ignored, Aborted, and Redirected transitions
  6954. *
  6955. * The semantics for the transition.run() promise and the StateService.transitionTo()
  6956. * promise differ. For instance, the run() promise may be rejected because it was
  6957. * IGNORED, but the transitionTo() promise is resolved because from the user perspective
  6958. * no error occurred. Likewise, the transition.run() promise may be rejected because of
  6959. * a Redirect, but the transitionTo() promise is chained to the new Transition's promise.
  6960. */
  6961. var rejectedTransitionHandler = function (transition) { return function (error) {
  6962. if (error instanceof Rejection) {
  6963. var isLatest = router.globals.lastStartedTransitionId === transition.$id;
  6964. if (error.type === exports.RejectType.IGNORED) {
  6965. isLatest && router.urlRouter.update();
  6966. // Consider ignored `Transition.run()` as a successful `transitionTo`
  6967. return services.$q.when(globals.current);
  6968. }
  6969. var detail = error.detail;
  6970. if (error.type === exports.RejectType.SUPERSEDED && error.redirected && detail instanceof TargetState) {
  6971. // If `Transition.run()` was redirected, allow the `transitionTo()` promise to resolve successfully
  6972. // by returning the promise for the new (redirect) `Transition.run()`.
  6973. var redirect = transition.redirect(detail);
  6974. return redirect.run().catch(rejectedTransitionHandler(redirect));
  6975. }
  6976. if (error.type === exports.RejectType.ABORTED) {
  6977. isLatest && router.urlRouter.update();
  6978. return services.$q.reject(error);
  6979. }
  6980. }
  6981. var errorHandler = _this.defaultErrorHandler();
  6982. errorHandler(error);
  6983. return services.$q.reject(error);
  6984. }; };
  6985. var transition = this.router.transitionService.create(currentPath, ref);
  6986. var transitionToPromise = transition.run().catch(rejectedTransitionHandler(transition));
  6987. silenceUncaughtInPromise(transitionToPromise); // issue #2676
  6988. // Return a promise for the transition, which also has the transition object on it.
  6989. return extend(transitionToPromise, { transition: transition });
  6990. };
  6991. /**
  6992. * Checks if the current state *is* the provided state
  6993. *
  6994. * Similar to [[includes]] but only checks for the full state name.
  6995. * If params is supplied then it will be tested for strict equality against the current
  6996. * active params object, so all params must match with none missing and no extras.
  6997. *
  6998. * #### Example:
  6999. * ```js
  7000. * $state.$current.name = 'contacts.details.item';
  7001. *
  7002. * // absolute name
  7003. * $state.is('contact.details.item'); // returns true
  7004. * $state.is(contactDetailItemStateObject); // returns true
  7005. * ```
  7006. *
  7007. * // relative name (. and ^), typically from a template
  7008. * // E.g. from the 'contacts.details' template
  7009. * ```html
  7010. * <div ng-class="{highlighted: $state.is('.item')}">Item</div>
  7011. * ```
  7012. *
  7013. * @param stateOrName The state name (absolute or relative) or state object you'd like to check.
  7014. * @param params A param object, e.g. `{sectionId: section.id}`, that you'd like
  7015. * to test against the current active state.
  7016. * @param options An options object. The options are:
  7017. * - `relative`: If `stateOrName` is a relative state name and `options.relative` is set, .is will
  7018. * test relative to `options.relative` state (or name).
  7019. *
  7020. * @returns Returns true if it is the state.
  7021. */
  7022. StateService.prototype.is = function (stateOrName, params, options) {
  7023. options = defaults(options, { relative: this.$current });
  7024. var state = this.router.stateRegistry.matcher.find(stateOrName, options.relative);
  7025. if (!isDefined(state))
  7026. return undefined;
  7027. if (this.$current !== state)
  7028. return false;
  7029. if (!params)
  7030. return true;
  7031. var schema = state.parameters({ inherit: true, matchingKeys: params });
  7032. return Param.equals(schema, Param.values(schema, params), this.params);
  7033. };
  7034. /**
  7035. * Checks if the current state *includes* the provided state
  7036. *
  7037. * A method to determine if the current active state is equal to or is the child of the
  7038. * state stateName. If any params are passed then they will be tested for a match as well.
  7039. * Not all the parameters need to be passed, just the ones you'd like to test for equality.
  7040. *
  7041. * #### Example when `$state.$current.name === 'contacts.details.item'`
  7042. * ```js
  7043. * // Using partial names
  7044. * $state.includes("contacts"); // returns true
  7045. * $state.includes("contacts.details"); // returns true
  7046. * $state.includes("contacts.details.item"); // returns true
  7047. * $state.includes("contacts.list"); // returns false
  7048. * $state.includes("about"); // returns false
  7049. * ```
  7050. *
  7051. * #### Glob Examples when `* $state.$current.name === 'contacts.details.item.url'`:
  7052. * ```js
  7053. * $state.includes("*.details.*.*"); // returns true
  7054. * $state.includes("*.details.**"); // returns true
  7055. * $state.includes("**.item.**"); // returns true
  7056. * $state.includes("*.details.item.url"); // returns true
  7057. * $state.includes("*.details.*.url"); // returns true
  7058. * $state.includes("*.details.*"); // returns false
  7059. * $state.includes("item.**"); // returns false
  7060. * ```
  7061. *
  7062. * @param stateOrName A partial name, relative name, glob pattern,
  7063. * or state object to be searched for within the current state name.
  7064. * @param params A param object, e.g. `{sectionId: section.id}`,
  7065. * that you'd like to test against the current active state.
  7066. * @param options An options object. The options are:
  7067. * - `relative`: If `stateOrName` is a relative state name and `options.relative` is set, .is will
  7068. * test relative to `options.relative` state (or name).
  7069. *
  7070. * @returns {boolean} Returns true if it does include the state
  7071. */
  7072. StateService.prototype.includes = function (stateOrName, params, options) {
  7073. options = defaults(options, { relative: this.$current });
  7074. var glob = isString(stateOrName) && Glob.fromString(stateOrName);
  7075. if (glob) {
  7076. if (!glob.matches(this.$current.name))
  7077. return false;
  7078. stateOrName = this.$current.name;
  7079. }
  7080. var state = this.router.stateRegistry.matcher.find(stateOrName, options.relative), include = this.$current.includes;
  7081. if (!isDefined(state))
  7082. return undefined;
  7083. if (!isDefined(include[state.name]))
  7084. return false;
  7085. if (!params)
  7086. return true;
  7087. var schema = state.parameters({ inherit: true, matchingKeys: params });
  7088. return Param.equals(schema, Param.values(schema, params), this.params);
  7089. };
  7090. /**
  7091. * Generates a URL for a state and parameters
  7092. *
  7093. * Returns the url for the given state populated with the given params.
  7094. *
  7095. * #### Example:
  7096. * ```js
  7097. * expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
  7098. * ```
  7099. *
  7100. * @param stateOrName The state name or state object you'd like to generate a url from.
  7101. * @param params An object of parameter values to fill the state's required parameters.
  7102. * @param options Options object. The options are:
  7103. *
  7104. * @returns {string} compiled state url
  7105. */
  7106. StateService.prototype.href = function (stateOrName, params, options) {
  7107. var defaultHrefOpts = {
  7108. lossy: true,
  7109. inherit: true,
  7110. absolute: false,
  7111. relative: this.$current,
  7112. };
  7113. options = defaults(options, defaultHrefOpts);
  7114. params = params || {};
  7115. var state = this.router.stateRegistry.matcher.find(stateOrName, options.relative);
  7116. if (!isDefined(state))
  7117. return null;
  7118. if (options.inherit)
  7119. params = this.params.$inherit(params, this.$current, state);
  7120. var nav = (state && options.lossy) ? state.navigable : state;
  7121. if (!nav || nav.url === undefined || nav.url === null) {
  7122. return null;
  7123. }
  7124. return this.router.urlRouter.href(nav.url, params, {
  7125. absolute: options.absolute,
  7126. });
  7127. };
  7128. /**
  7129. * Sets or gets the default [[transitionTo]] error handler.
  7130. *
  7131. * The error handler is called when a [[Transition]] is rejected or when any error occurred during the Transition.
  7132. * This includes errors caused by resolves and transition hooks.
  7133. *
  7134. * Note:
  7135. * This handler does not receive certain Transition rejections.
  7136. * Redirected and Ignored Transitions are not considered to be errors by [[StateService.transitionTo]].
  7137. *
  7138. * The built-in default error handler logs the error to the console.
  7139. *
  7140. * You can provide your own custom handler.
  7141. *
  7142. * #### Example:
  7143. * ```js
  7144. * stateService.defaultErrorHandler(function() {
  7145. * // Do not log transitionTo errors
  7146. * });
  7147. * ```
  7148. *
  7149. * @param handler a global error handler function
  7150. * @returns the current global error handler
  7151. */
  7152. StateService.prototype.defaultErrorHandler = function (handler) {
  7153. return this._defaultErrorHandler = handler || this._defaultErrorHandler;
  7154. };
  7155. StateService.prototype.get = function (stateOrName, base) {
  7156. var reg = this.router.stateRegistry;
  7157. if (arguments.length === 0)
  7158. return reg.get();
  7159. return reg.get(stateOrName, base || this.$current);
  7160. };
  7161. /**
  7162. * Lazy loads a state
  7163. *
  7164. * Explicitly runs a state's [[StateDeclaration.lazyLoad]] function.
  7165. *
  7166. * @param stateOrName the state that should be lazy loaded
  7167. * @param transition the optional Transition context to use (if the lazyLoad function requires an injector, etc)
  7168. * Note: If no transition is provided, a noop transition is created using the from the current state to the current state.
  7169. * This noop transition is not actually run.
  7170. *
  7171. * @returns a promise to lazy load
  7172. */
  7173. StateService.prototype.lazyLoad = function (stateOrName, transition) {
  7174. var state = this.get(stateOrName);
  7175. if (!state || !state.lazyLoad)
  7176. throw new Error("Can not lazy load " + stateOrName);
  7177. var currentPath = this.getCurrentPath();
  7178. var target = PathUtils.makeTargetState(this.router.stateRegistry, currentPath);
  7179. transition = transition || this.router.transitionService.create(currentPath, target);
  7180. return lazyLoadState(transition, state);
  7181. };
  7182. return StateService;
  7183. }());
  7184. /**
  7185. * # Transition subsystem
  7186. *
  7187. * This module contains APIs related to a Transition.
  7188. *
  7189. * See:
  7190. * - [[TransitionService]]
  7191. * - [[Transition]]
  7192. * - [[HookFn]], [[TransitionHookFn]], [[TransitionStateHookFn]], [[HookMatchCriteria]], [[HookResult]]
  7193. *
  7194. * @coreapi
  7195. * @preferred
  7196. * @module transition
  7197. */ /** for typedoc */
  7198. /**
  7199. * @internalapi
  7200. * @module vanilla
  7201. */
  7202. /** */
  7203. /**
  7204. * An angular1-like promise api
  7205. *
  7206. * This object implements four methods similar to the
  7207. * [angular 1 promise api](https://docs.angularjs.org/api/ng/service/$q)
  7208. *
  7209. * UI-Router evolved from an angular 1 library to a framework agnostic library.
  7210. * However, some of the `@uirouter/core` code uses these ng1 style APIs to support ng1 style dependency injection.
  7211. *
  7212. * This API provides native ES6 promise support wrapped as a $q-like API.
  7213. * Internally, UI-Router uses this $q object to perform promise operations.
  7214. * The `angular-ui-router` (ui-router for angular 1) uses the $q API provided by angular.
  7215. *
  7216. * $q-like promise api
  7217. */
  7218. var $q = {
  7219. /** Normalizes a value as a promise */
  7220. when: function (val) { return new Promise(function (resolve, reject) { return resolve(val); }); },
  7221. /** Normalizes a value as a promise rejection */
  7222. reject: function (val) { return new Promise(function (resolve, reject) { reject(val); }); },
  7223. /** @returns a deferred object, which has `resolve` and `reject` functions */
  7224. defer: function () {
  7225. var deferred = {};
  7226. deferred.promise = new Promise(function (resolve, reject) {
  7227. deferred.resolve = resolve;
  7228. deferred.reject = reject;
  7229. });
  7230. return deferred;
  7231. },
  7232. /** Like Promise.all(), but also supports object key/promise notation like $q */
  7233. all: function (promises) {
  7234. if (isArray(promises)) {
  7235. return Promise.all(promises);
  7236. }
  7237. if (isObject(promises)) {
  7238. // Convert promises map to promises array.
  7239. // When each promise resolves, map it to a tuple { key: key, val: val }
  7240. var chain = Object.keys(promises)
  7241. .map(function (key) { return promises[key].then(function (val) { return ({ key: key, val: val }); }); });
  7242. // Then wait for all promises to resolve, and convert them back to an object
  7243. return $q.all(chain).then(function (values) {
  7244. return values.reduce(function (acc, tuple) { acc[tuple.key] = tuple.val; return acc; }, {});
  7245. });
  7246. }
  7247. }
  7248. };
  7249. /**
  7250. * @internalapi
  7251. * @module vanilla
  7252. */
  7253. /** */
  7254. // globally available injectables
  7255. var globals = {};
  7256. var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
  7257. var ARGUMENT_NAMES = /([^\s,]+)/g;
  7258. /**
  7259. * A basic angular1-like injector api
  7260. *
  7261. * This object implements four methods similar to the
  7262. * [angular 1 dependency injector](https://docs.angularjs.org/api/auto/service/$injector)
  7263. *
  7264. * UI-Router evolved from an angular 1 library to a framework agnostic library.
  7265. * However, some of the `@uirouter/core` code uses these ng1 style APIs to support ng1 style dependency injection.
  7266. *
  7267. * This object provides a naive implementation of a globally scoped dependency injection system.
  7268. * It supports the following DI approaches:
  7269. *
  7270. * ### Function parameter names
  7271. *
  7272. * A function's `.toString()` is called, and the parameter names are parsed.
  7273. * This only works when the parameter names aren't "mangled" by a minifier such as UglifyJS.
  7274. *
  7275. * ```js
  7276. * function injectedFunction(FooService, BarService) {
  7277. * // FooService and BarService are injected
  7278. * }
  7279. * ```
  7280. *
  7281. * ### Function annotation
  7282. *
  7283. * A function may be annotated with an array of dependency names as the `$inject` property.
  7284. *
  7285. * ```js
  7286. * injectedFunction.$inject = [ 'FooService', 'BarService' ];
  7287. * function injectedFunction(fs, bs) {
  7288. * // FooService and BarService are injected as fs and bs parameters
  7289. * }
  7290. * ```
  7291. *
  7292. * ### Array notation
  7293. *
  7294. * An array provides the names of the dependencies to inject (as strings).
  7295. * The function is the last element of the array.
  7296. *
  7297. * ```js
  7298. * [ 'FooService', 'BarService', function (fs, bs) {
  7299. * // FooService and BarService are injected as fs and bs parameters
  7300. * }]
  7301. * ```
  7302. *
  7303. * @type {$InjectorLike}
  7304. */
  7305. var $injector = {
  7306. /** Gets an object from DI based on a string token */
  7307. get: function (name) { return globals[name]; },
  7308. /** Returns true if an object named `name` exists in global DI */
  7309. has: function (name) { return $injector.get(name) != null; },
  7310. /**
  7311. * Injects a function
  7312. *
  7313. * @param fn the function to inject
  7314. * @param context the function's `this` binding
  7315. * @param locals An object with additional DI tokens and values, such as `{ someToken: { foo: 1 } }`
  7316. */
  7317. invoke: function (fn, context, locals) {
  7318. var all = extend({}, globals, locals || {});
  7319. var params = $injector.annotate(fn);
  7320. var ensureExist = assertPredicate(function (key) { return all.hasOwnProperty(key); }, function (key) { return "DI can't find injectable: '" + key + "'"; });
  7321. var args = params.filter(ensureExist).map(function (x) { return all[x]; });
  7322. if (isFunction(fn))
  7323. return fn.apply(context, args);
  7324. else
  7325. return fn.slice(-1)[0].apply(context, args);
  7326. },
  7327. /**
  7328. * Returns a function's dependencies
  7329. *
  7330. * Analyzes a function (or array) and returns an array of DI tokens that the function requires.
  7331. * @return an array of `string`s
  7332. */
  7333. annotate: function (fn) {
  7334. if (!isInjectable(fn))
  7335. throw new Error("Not an injectable function: " + fn);
  7336. if (fn && fn.$inject)
  7337. return fn.$inject;
  7338. if (isArray(fn))
  7339. return fn.slice(0, -1);
  7340. var fnStr = fn.toString().replace(STRIP_COMMENTS, '');
  7341. var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
  7342. return result || [];
  7343. }
  7344. };
  7345. /**
  7346. * @internalapi
  7347. * @module vanilla
  7348. */
  7349. /** */
  7350. var keyValsToObjectR = function (accum, _a) {
  7351. var key = _a[0], val = _a[1];
  7352. if (!accum.hasOwnProperty(key)) {
  7353. accum[key] = val;
  7354. }
  7355. else if (isArray(accum[key])) {
  7356. accum[key].push(val);
  7357. }
  7358. else {
  7359. accum[key] = [accum[key], val];
  7360. }
  7361. return accum;
  7362. };
  7363. var getParams = function (queryString) {
  7364. return queryString.split("&").filter(identity).map(splitEqual).reduce(keyValsToObjectR, {});
  7365. };
  7366. function parseUrl$1(url) {
  7367. var orEmptyString = function (x) { return x || ""; };
  7368. var _a = splitHash(url).map(orEmptyString), beforehash = _a[0], hash = _a[1];
  7369. var _b = splitQuery(beforehash).map(orEmptyString), path = _b[0], search = _b[1];
  7370. return { path: path, search: search, hash: hash, url: url };
  7371. }
  7372. var buildUrl = function (loc) {
  7373. var path = loc.path();
  7374. var searchObject = loc.search();
  7375. var hash = loc.hash();
  7376. var search = Object.keys(searchObject).map(function (key) {
  7377. var param = searchObject[key];
  7378. var vals = isArray(param) ? param : [param];
  7379. return vals.map(function (val) { return key + "=" + val; });
  7380. }).reduce(unnestR, []).join("&");
  7381. return path + (search ? "?" + search : "") + (hash ? "#" + hash : "");
  7382. };
  7383. function locationPluginFactory(name, isHtml5, serviceClass, configurationClass) {
  7384. return function (router) {
  7385. var service = router.locationService = new serviceClass(router);
  7386. var configuration = router.locationConfig = new configurationClass(router, isHtml5);
  7387. function dispose(router) {
  7388. router.dispose(service);
  7389. router.dispose(configuration);
  7390. }
  7391. return { name: name, service: service, configuration: configuration, dispose: dispose };
  7392. };
  7393. }
  7394. /**
  7395. * @internalapi
  7396. * @module vanilla
  7397. */ /** */
  7398. /** A base `LocationServices` */
  7399. var BaseLocationServices = /** @class */ (function () {
  7400. function BaseLocationServices(router, fireAfterUpdate) {
  7401. var _this = this;
  7402. this.fireAfterUpdate = fireAfterUpdate;
  7403. this._listener = function (evt) { return _this._listeners.forEach(function (cb) { return cb(evt); }); };
  7404. this._listeners = [];
  7405. this.hash = function () { return parseUrl$1(_this._get()).hash; };
  7406. this.path = function () { return parseUrl$1(_this._get()).path; };
  7407. this.search = function () { return getParams(parseUrl$1(_this._get()).search); };
  7408. this._location = root.location;
  7409. this._history = root.history;
  7410. }
  7411. BaseLocationServices.prototype.url = function (url, replace) {
  7412. if (replace === void 0) { replace = true; }
  7413. if (isDefined(url) && url !== this._get()) {
  7414. this._set(null, null, url, replace);
  7415. if (this.fireAfterUpdate) {
  7416. this._listeners.forEach(function (cb) { return cb({ url: url }); });
  7417. }
  7418. }
  7419. return buildUrl(this);
  7420. };
  7421. BaseLocationServices.prototype.onChange = function (cb) {
  7422. var _this = this;
  7423. this._listeners.push(cb);
  7424. return function () { return removeFrom(_this._listeners, cb); };
  7425. };
  7426. BaseLocationServices.prototype.dispose = function (router) {
  7427. deregAll(this._listeners);
  7428. };
  7429. return BaseLocationServices;
  7430. }());
  7431. var __extends = (undefined && undefined.__extends) || (function () {
  7432. var extendStatics = Object.setPrototypeOf ||
  7433. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  7434. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7435. return function (d, b) {
  7436. extendStatics(d, b);
  7437. function __() { this.constructor = d; }
  7438. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  7439. };
  7440. })();
  7441. /**
  7442. * @internalapi
  7443. * @module vanilla
  7444. */
  7445. /** */
  7446. /** A `LocationServices` that uses the browser hash "#" to get/set the current location */
  7447. var HashLocationService = /** @class */ (function (_super) {
  7448. __extends(HashLocationService, _super);
  7449. function HashLocationService(router) {
  7450. var _this = _super.call(this, router, false) || this;
  7451. root.addEventListener('hashchange', _this._listener, false);
  7452. return _this;
  7453. }
  7454. HashLocationService.prototype._get = function () {
  7455. return trimHashVal(this._location.hash);
  7456. };
  7457. HashLocationService.prototype._set = function (state, title, url, replace) {
  7458. this._location.hash = url;
  7459. };
  7460. HashLocationService.prototype.dispose = function (router) {
  7461. _super.prototype.dispose.call(this, router);
  7462. root.removeEventListener('hashchange', this._listener);
  7463. };
  7464. return HashLocationService;
  7465. }(BaseLocationServices));
  7466. var __extends$1 = (undefined && undefined.__extends) || (function () {
  7467. var extendStatics = Object.setPrototypeOf ||
  7468. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  7469. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7470. return function (d, b) {
  7471. extendStatics(d, b);
  7472. function __() { this.constructor = d; }
  7473. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  7474. };
  7475. })();
  7476. /**
  7477. * @internalapi
  7478. * @module vanilla
  7479. */
  7480. /** */
  7481. /** A `LocationServices` that gets/sets the current location from an in-memory object */
  7482. var MemoryLocationService = /** @class */ (function (_super) {
  7483. __extends$1(MemoryLocationService, _super);
  7484. function MemoryLocationService(router) {
  7485. return _super.call(this, router, true) || this;
  7486. }
  7487. MemoryLocationService.prototype._get = function () {
  7488. return this._url;
  7489. };
  7490. MemoryLocationService.prototype._set = function (state, title, url, replace) {
  7491. this._url = url;
  7492. };
  7493. return MemoryLocationService;
  7494. }(BaseLocationServices));
  7495. var __extends$2 = (undefined && undefined.__extends) || (function () {
  7496. var extendStatics = Object.setPrototypeOf ||
  7497. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  7498. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7499. return function (d, b) {
  7500. extendStatics(d, b);
  7501. function __() { this.constructor = d; }
  7502. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  7503. };
  7504. })();
  7505. /**
  7506. * A `LocationServices` that gets/sets the current location using the browser's `location` and `history` apis
  7507. *
  7508. * Uses `history.pushState` and `history.replaceState`
  7509. */
  7510. var PushStateLocationService = /** @class */ (function (_super) {
  7511. __extends$2(PushStateLocationService, _super);
  7512. function PushStateLocationService(router) {
  7513. var _this = _super.call(this, router, true) || this;
  7514. _this._config = router.urlService.config;
  7515. root.addEventListener('popstate', _this._listener, false);
  7516. return _this;
  7517. }
  7518. /**
  7519. * Gets the base prefix without:
  7520. * - trailing slash
  7521. * - trailing filename
  7522. * - protocol and hostname
  7523. *
  7524. * If <base href='/base/index.html'>, this returns '/base'.
  7525. * If <base href='http://localhost:8080/base/index.html'>, this returns '/base'.
  7526. *
  7527. * See: https://html.spec.whatwg.org/dev/semantics.html#the-base-element
  7528. */
  7529. PushStateLocationService.prototype._getBasePrefix = function () {
  7530. return stripFile(this._config.baseHref());
  7531. };
  7532. PushStateLocationService.prototype._get = function () {
  7533. var _a = this._location, pathname = _a.pathname, hash = _a.hash, search = _a.search;
  7534. search = splitQuery(search)[1]; // strip ? if found
  7535. hash = splitHash(hash)[1]; // strip # if found
  7536. var basePrefix = this._getBasePrefix();
  7537. var exactMatch = pathname === this._config.baseHref();
  7538. var startsWith = pathname.startsWith(basePrefix);
  7539. pathname = exactMatch ? '/' : startsWith ? pathname.substring(basePrefix.length) : pathname;
  7540. return pathname + (search ? '?' + search : '') + (hash ? '#' + hash : '');
  7541. };
  7542. PushStateLocationService.prototype._set = function (state, title, url, replace) {
  7543. var fullUrl = this._getBasePrefix() + url;
  7544. if (replace) {
  7545. this._history.replaceState(state, title, fullUrl);
  7546. }
  7547. else {
  7548. this._history.pushState(state, title, fullUrl);
  7549. }
  7550. };
  7551. PushStateLocationService.prototype.dispose = function (router) {
  7552. _super.prototype.dispose.call(this, router);
  7553. root.removeEventListener('popstate', this._listener);
  7554. };
  7555. return PushStateLocationService;
  7556. }(BaseLocationServices));
  7557. /** A `LocationConfig` mock that gets/sets all config from an in-memory object */
  7558. var MemoryLocationConfig = /** @class */ (function () {
  7559. function MemoryLocationConfig() {
  7560. var _this = this;
  7561. this._baseHref = '';
  7562. this._port = 80;
  7563. this._protocol = "http";
  7564. this._host = "localhost";
  7565. this._hashPrefix = "";
  7566. this.port = function () { return _this._port; };
  7567. this.protocol = function () { return _this._protocol; };
  7568. this.host = function () { return _this._host; };
  7569. this.baseHref = function () { return _this._baseHref; };
  7570. this.html5Mode = function () { return false; };
  7571. this.hashPrefix = function (newval) { return isDefined(newval) ? _this._hashPrefix = newval : _this._hashPrefix; };
  7572. this.dispose = noop$1;
  7573. }
  7574. return MemoryLocationConfig;
  7575. }());
  7576. /**
  7577. * @internalapi
  7578. * @module vanilla
  7579. */
  7580. /** */
  7581. /** A `LocationConfig` that delegates to the browser's `location` object */
  7582. var BrowserLocationConfig = /** @class */ (function () {
  7583. function BrowserLocationConfig(router, _isHtml5) {
  7584. if (_isHtml5 === void 0) { _isHtml5 = false; }
  7585. this._isHtml5 = _isHtml5;
  7586. this._baseHref = undefined;
  7587. this._hashPrefix = "";
  7588. }
  7589. BrowserLocationConfig.prototype.port = function () {
  7590. if (location.port) {
  7591. return Number(location.port);
  7592. }
  7593. return this.protocol() === 'https' ? 443 : 80;
  7594. };
  7595. BrowserLocationConfig.prototype.protocol = function () {
  7596. return location.protocol.replace(/:/g, '');
  7597. };
  7598. BrowserLocationConfig.prototype.host = function () {
  7599. return location.hostname;
  7600. };
  7601. BrowserLocationConfig.prototype.html5Mode = function () {
  7602. return this._isHtml5;
  7603. };
  7604. BrowserLocationConfig.prototype.hashPrefix = function (newprefix) {
  7605. return isDefined(newprefix) ? this._hashPrefix = newprefix : this._hashPrefix;
  7606. };
  7607. BrowserLocationConfig.prototype.baseHref = function (href) {
  7608. return isDefined(href) ? this._baseHref = href :
  7609. isDefined(this._baseHref) ? this._baseHref : this.applyDocumentBaseHref();
  7610. };
  7611. BrowserLocationConfig.prototype.applyDocumentBaseHref = function () {
  7612. var baseTag = document.getElementsByTagName("base")[0];
  7613. return this._baseHref = baseTag ? baseTag.href.substr(location.origin.length) : "";
  7614. };
  7615. BrowserLocationConfig.prototype.dispose = function () { };
  7616. return BrowserLocationConfig;
  7617. }());
  7618. /**
  7619. * @internalapi
  7620. * @module vanilla
  7621. */
  7622. /** */
  7623. function servicesPlugin(router) {
  7624. services.$injector = $injector;
  7625. services.$q = $q;
  7626. return { name: "vanilla.services", $q: $q, $injector: $injector, dispose: function () { return null; } };
  7627. }
  7628. /** A `UIRouterPlugin` uses the browser hash to get/set the current location */
  7629. var hashLocationPlugin = locationPluginFactory('vanilla.hashBangLocation', false, HashLocationService, BrowserLocationConfig);
  7630. /** A `UIRouterPlugin` that gets/sets the current location using the browser's `location` and `history` apis */
  7631. var pushStateLocationPlugin = locationPluginFactory("vanilla.pushStateLocation", true, PushStateLocationService, BrowserLocationConfig);
  7632. /** A `UIRouterPlugin` that gets/sets the current location from an in-memory object */
  7633. var memoryLocationPlugin = locationPluginFactory("vanilla.memoryLocation", false, MemoryLocationService, MemoryLocationConfig);
  7634. /**
  7635. * @internalapi
  7636. * @module vanilla
  7637. */
  7638. /** */
  7639. /**
  7640. * # Core classes and interfaces
  7641. *
  7642. * The classes and interfaces that are core to ui-router and do not belong
  7643. * to a more specific subsystem (such as resolve).
  7644. *
  7645. * @coreapi
  7646. * @preferred
  7647. * @module core
  7648. */ /** for typedoc */
  7649. /** @internalapi */
  7650. var UIRouterPluginBase = /** @class */ (function () {
  7651. function UIRouterPluginBase() {
  7652. }
  7653. UIRouterPluginBase.prototype.dispose = function (router) { };
  7654. return UIRouterPluginBase;
  7655. }());
  7656. /**
  7657. * @coreapi
  7658. * @module common
  7659. */ /** */
  7660. var index$1 = Object.freeze({
  7661. root: root,
  7662. fromJson: fromJson,
  7663. toJson: toJson,
  7664. forEach: forEach,
  7665. extend: extend,
  7666. equals: equals,
  7667. identity: identity,
  7668. noop: noop$1,
  7669. createProxyFunctions: createProxyFunctions,
  7670. inherit: inherit,
  7671. inArray: inArray,
  7672. _inArray: _inArray,
  7673. removeFrom: removeFrom,
  7674. _removeFrom: _removeFrom,
  7675. pushTo: pushTo,
  7676. _pushTo: _pushTo,
  7677. deregAll: deregAll,
  7678. defaults: defaults,
  7679. mergeR: mergeR,
  7680. ancestors: ancestors,
  7681. pick: pick,
  7682. omit: omit,
  7683. pluck: pluck,
  7684. filter: filter,
  7685. find: find,
  7686. mapObj: mapObj,
  7687. map: map,
  7688. values: values,
  7689. allTrueR: allTrueR,
  7690. anyTrueR: anyTrueR,
  7691. unnestR: unnestR,
  7692. flattenR: flattenR,
  7693. pushR: pushR,
  7694. uniqR: uniqR,
  7695. unnest: unnest,
  7696. flatten: flatten,
  7697. assertPredicate: assertPredicate,
  7698. assertMap: assertMap,
  7699. assertFn: assertFn,
  7700. pairs: pairs,
  7701. arrayTuples: arrayTuples,
  7702. applyPairs: applyPairs,
  7703. tail: tail,
  7704. copy: copy,
  7705. _extend: _extend,
  7706. silenceUncaughtInPromise: silenceUncaughtInPromise,
  7707. silentRejection: silentRejection,
  7708. notImplemented: notImplemented,
  7709. services: services,
  7710. Glob: Glob,
  7711. curry: curry,
  7712. compose: compose,
  7713. pipe: pipe,
  7714. prop: prop,
  7715. propEq: propEq,
  7716. parse: parse,
  7717. not: not,
  7718. and: and,
  7719. or: or,
  7720. all: all,
  7721. any: any,
  7722. is: is,
  7723. eq: eq,
  7724. val: val,
  7725. invoke: invoke,
  7726. pattern: pattern,
  7727. isUndefined: isUndefined,
  7728. isDefined: isDefined,
  7729. isNull: isNull,
  7730. isNullOrUndefined: isNullOrUndefined,
  7731. isFunction: isFunction,
  7732. isNumber: isNumber,
  7733. isString: isString,
  7734. isObject: isObject,
  7735. isArray: isArray,
  7736. isDate: isDate,
  7737. isRegExp: isRegExp,
  7738. isState: isState,
  7739. isInjectable: isInjectable,
  7740. isPromise: isPromise,
  7741. Queue: Queue,
  7742. maxLength: maxLength,
  7743. padString: padString,
  7744. kebobString: kebobString,
  7745. functionToString: functionToString,
  7746. fnToString: fnToString,
  7747. stringify: stringify,
  7748. beforeAfterSubstr: beforeAfterSubstr,
  7749. hostRegex: hostRegex,
  7750. stripFile: stripFile,
  7751. splitHash: splitHash,
  7752. splitQuery: splitQuery,
  7753. splitEqual: splitEqual,
  7754. trimHashVal: trimHashVal,
  7755. splitOnDelim: splitOnDelim,
  7756. joinNeighborsR: joinNeighborsR,
  7757. get Category () { return exports.Category; },
  7758. Trace: Trace,
  7759. trace: trace,
  7760. get DefType () { return exports.DefType; },
  7761. Param: Param,
  7762. ParamTypes: ParamTypes,
  7763. StateParams: StateParams,
  7764. ParamType: ParamType,
  7765. PathNode: PathNode,
  7766. PathUtils: PathUtils,
  7767. resolvePolicies: resolvePolicies,
  7768. defaultResolvePolicy: defaultResolvePolicy,
  7769. Resolvable: Resolvable,
  7770. NATIVE_INJECTOR_TOKEN: NATIVE_INJECTOR_TOKEN,
  7771. ResolveContext: ResolveContext,
  7772. resolvablesBuilder: resolvablesBuilder,
  7773. StateBuilder: StateBuilder,
  7774. StateObject: StateObject,
  7775. StateMatcher: StateMatcher,
  7776. StateQueueManager: StateQueueManager,
  7777. StateRegistry: StateRegistry,
  7778. StateService: StateService,
  7779. TargetState: TargetState,
  7780. get TransitionHookPhase () { return exports.TransitionHookPhase; },
  7781. get TransitionHookScope () { return exports.TransitionHookScope; },
  7782. HookBuilder: HookBuilder,
  7783. matchState: matchState,
  7784. RegisteredHook: RegisteredHook,
  7785. makeEvent: makeEvent,
  7786. get RejectType () { return exports.RejectType; },
  7787. Rejection: Rejection,
  7788. Transition: Transition,
  7789. TransitionHook: TransitionHook,
  7790. TransitionEventType: TransitionEventType,
  7791. defaultTransOpts: defaultTransOpts,
  7792. TransitionService: TransitionService,
  7793. UrlMatcher: UrlMatcher,
  7794. UrlMatcherFactory: UrlMatcherFactory,
  7795. UrlRouter: UrlRouter,
  7796. UrlRuleFactory: UrlRuleFactory,
  7797. BaseUrlRule: BaseUrlRule,
  7798. UrlService: UrlService,
  7799. ViewService: ViewService,
  7800. UIRouterGlobals: UIRouterGlobals,
  7801. UIRouter: UIRouter,
  7802. $q: $q,
  7803. $injector: $injector,
  7804. BaseLocationServices: BaseLocationServices,
  7805. HashLocationService: HashLocationService,
  7806. MemoryLocationService: MemoryLocationService,
  7807. PushStateLocationService: PushStateLocationService,
  7808. MemoryLocationConfig: MemoryLocationConfig,
  7809. BrowserLocationConfig: BrowserLocationConfig,
  7810. keyValsToObjectR: keyValsToObjectR,
  7811. getParams: getParams,
  7812. parseUrl: parseUrl$1,
  7813. buildUrl: buildUrl,
  7814. locationPluginFactory: locationPluginFactory,
  7815. servicesPlugin: servicesPlugin,
  7816. hashLocationPlugin: hashLocationPlugin,
  7817. pushStateLocationPlugin: pushStateLocationPlugin,
  7818. memoryLocationPlugin: memoryLocationPlugin,
  7819. UIRouterPluginBase: UIRouterPluginBase
  7820. });
  7821. function getNg1ViewConfigFactory() {
  7822. var templateFactory = null;
  7823. return function (path, view) {
  7824. templateFactory = templateFactory || services.$injector.get("$templateFactory");
  7825. return [new Ng1ViewConfig(path, view, templateFactory)];
  7826. };
  7827. }
  7828. var hasAnyKey = function (keys, obj) {
  7829. return keys.reduce(function (acc, key) { return acc || isDefined(obj[key]); }, false);
  7830. };
  7831. /**
  7832. * This is a [[StateBuilder.builder]] function for angular1 `views`.
  7833. *
  7834. * When the [[StateBuilder]] builds a [[StateObject]] object from a raw [[StateDeclaration]], this builder
  7835. * handles the `views` property with logic specific to @uirouter/angularjs (ng1).
  7836. *
  7837. * If no `views: {}` property exists on the [[StateDeclaration]], then it creates the `views` object
  7838. * and applies the state-level configuration to a view named `$default`.
  7839. */
  7840. function ng1ViewsBuilder(state) {
  7841. // Do not process root state
  7842. if (!state.parent)
  7843. return {};
  7844. var tplKeys = ['templateProvider', 'templateUrl', 'template', 'notify', 'async'], ctrlKeys = ['controller', 'controllerProvider', 'controllerAs', 'resolveAs'], compKeys = ['component', 'bindings', 'componentProvider'], nonCompKeys = tplKeys.concat(ctrlKeys), allViewKeys = compKeys.concat(nonCompKeys);
  7845. // Do not allow a state to have both state-level props and also a `views: {}` property.
  7846. // A state without a `views: {}` property can declare properties for the `$default` view as properties of the state.
  7847. // However, the `$default` approach should not be mixed with a separate `views: ` block.
  7848. if (isDefined(state.views) && hasAnyKey(allViewKeys, state)) {
  7849. throw new Error("State '" + state.name + "' has a 'views' object. " +
  7850. "It cannot also have \"view properties\" at the state level. " +
  7851. "Move the following properties into a view (in the 'views' object): " +
  7852. (" " + allViewKeys.filter(function (key) { return isDefined(state[key]); }).join(", ")));
  7853. }
  7854. var views = {}, viewsObject = state.views || { "$default": pick(state, allViewKeys) };
  7855. forEach(viewsObject, function (config, name) {
  7856. // Account for views: { "": { template... } }
  7857. name = name || "$default";
  7858. // Account for views: { header: "headerComponent" }
  7859. if (isString(config))
  7860. config = { component: config };
  7861. // Make a shallow copy of the config object
  7862. config = extend({}, config);
  7863. // Do not allow a view to mix props for component-style view with props for template/controller-style view
  7864. if (hasAnyKey(compKeys, config) && hasAnyKey(nonCompKeys, config)) {
  7865. throw new Error("Cannot combine: " + compKeys.join("|") + " with: " + nonCompKeys.join("|") + " in stateview: '" + name + "@" + state.name + "'");
  7866. }
  7867. config.resolveAs = config.resolveAs || '$resolve';
  7868. config.$type = "ng1";
  7869. config.$context = state;
  7870. config.$name = name;
  7871. var normalized = ViewService.normalizeUIViewTarget(config.$context, config.$name);
  7872. config.$uiViewName = normalized.uiViewName;
  7873. config.$uiViewContextAnchor = normalized.uiViewContextAnchor;
  7874. views[name] = config;
  7875. });
  7876. return views;
  7877. }
  7878. var id$1 = 0;
  7879. var Ng1ViewConfig = /** @class */ (function () {
  7880. function Ng1ViewConfig(path, viewDecl, factory) {
  7881. var _this = this;
  7882. this.path = path;
  7883. this.viewDecl = viewDecl;
  7884. this.factory = factory;
  7885. this.$id = id$1++;
  7886. this.loaded = false;
  7887. this.getTemplate = function (uiView, context) {
  7888. return _this.component ? _this.factory.makeComponentTemplate(uiView, context, _this.component, _this.viewDecl.bindings) : _this.template;
  7889. };
  7890. }
  7891. Ng1ViewConfig.prototype.load = function () {
  7892. var _this = this;
  7893. var $q = services.$q;
  7894. var context = new ResolveContext(this.path);
  7895. var params = this.path.reduce(function (acc, node) { return extend(acc, node.paramValues); }, {});
  7896. var promises = {
  7897. template: $q.when(this.factory.fromConfig(this.viewDecl, params, context)),
  7898. controller: $q.when(this.getController(context))
  7899. };
  7900. return $q.all(promises).then(function (results) {
  7901. trace.traceViewServiceEvent("Loaded", _this);
  7902. _this.controller = results.controller;
  7903. extend(_this, results.template); // Either { template: "tpl" } or { component: "cmpName" }
  7904. return _this;
  7905. });
  7906. };
  7907. /**
  7908. * Gets the controller for a view configuration.
  7909. *
  7910. * @returns {Function|Promise.<Function>} Returns a controller, or a promise that resolves to a controller.
  7911. */
  7912. Ng1ViewConfig.prototype.getController = function (context) {
  7913. var provider = this.viewDecl.controllerProvider;
  7914. if (!isInjectable(provider))
  7915. return this.viewDecl.controller;
  7916. var deps = services.$injector.annotate(provider);
  7917. var providerFn = isArray(provider) ? tail(provider) : provider;
  7918. var resolvable = new Resolvable("", providerFn, deps);
  7919. return resolvable.get(context);
  7920. };
  7921. return Ng1ViewConfig;
  7922. }());
  7923. /** @module view */
  7924. /** for typedoc */
  7925. /**
  7926. * Service which manages loading of templates from a ViewConfig.
  7927. */
  7928. var TemplateFactory = /** @class */ (function () {
  7929. function TemplateFactory() {
  7930. var _this = this;
  7931. /** @hidden */ this._useHttp = ng.version.minor < 3;
  7932. /** @hidden */ this.$get = ['$http', '$templateCache', '$injector', function ($http, $templateCache, $injector) {
  7933. _this.$templateRequest = $injector.has && $injector.has('$templateRequest') && $injector.get('$templateRequest');
  7934. _this.$http = $http;
  7935. _this.$templateCache = $templateCache;
  7936. return _this;
  7937. }];
  7938. }
  7939. /** @hidden */
  7940. TemplateFactory.prototype.useHttpService = function (value) {
  7941. this._useHttp = value;
  7942. };
  7943. /**
  7944. * Creates a template from a configuration object.
  7945. *
  7946. * @param config Configuration object for which to load a template.
  7947. * The following properties are search in the specified order, and the first one
  7948. * that is defined is used to create the template:
  7949. *
  7950. * @param params Parameters to pass to the template function.
  7951. * @param context The resolve context associated with the template's view
  7952. *
  7953. * @return {string|object} The template html as a string, or a promise for
  7954. * that string,or `null` if no template is configured.
  7955. */
  7956. TemplateFactory.prototype.fromConfig = function (config, params, context) {
  7957. var defaultTemplate = "<ui-view></ui-view>";
  7958. var asTemplate = function (result) { return services.$q.when(result).then(function (str) { return ({ template: str }); }); };
  7959. var asComponent = function (result) { return services.$q.when(result).then(function (str) { return ({ component: str }); }); };
  7960. return (isDefined(config.template) ? asTemplate(this.fromString(config.template, params)) :
  7961. isDefined(config.templateUrl) ? asTemplate(this.fromUrl(config.templateUrl, params)) :
  7962. isDefined(config.templateProvider) ? asTemplate(this.fromProvider(config.templateProvider, params, context)) :
  7963. isDefined(config.component) ? asComponent(config.component) :
  7964. isDefined(config.componentProvider) ? asComponent(this.fromComponentProvider(config.componentProvider, params, context)) :
  7965. asTemplate(defaultTemplate));
  7966. };
  7967. /**
  7968. * Creates a template from a string or a function returning a string.
  7969. *
  7970. * @param template html template as a string or function that returns an html template as a string.
  7971. * @param params Parameters to pass to the template function.
  7972. *
  7973. * @return {string|object} The template html as a string, or a promise for that
  7974. * string.
  7975. */
  7976. TemplateFactory.prototype.fromString = function (template, params) {
  7977. return isFunction(template) ? template(params) : template;
  7978. };
  7979. /**
  7980. * Loads a template from the a URL via `$http` and `$templateCache`.
  7981. *
  7982. * @param {string|Function} url url of the template to load, or a function
  7983. * that returns a url.
  7984. * @param {Object} params Parameters to pass to the url function.
  7985. * @return {string|Promise.<string>} The template html as a string, or a promise
  7986. * for that string.
  7987. */
  7988. TemplateFactory.prototype.fromUrl = function (url, params) {
  7989. if (isFunction(url))
  7990. url = url(params);
  7991. if (url == null)
  7992. return null;
  7993. if (this._useHttp) {
  7994. return this.$http.get(url, { cache: this.$templateCache, headers: { Accept: 'text/html' } })
  7995. .then(function (response) {
  7996. return response.data;
  7997. });
  7998. }
  7999. return this.$templateRequest(url);
  8000. };
  8001. /**
  8002. * Creates a template by invoking an injectable provider function.
  8003. *
  8004. * @param provider Function to invoke via `locals`
  8005. * @param {Function} injectFn a function used to invoke the template provider
  8006. * @return {string|Promise.<string>} The template html as a string, or a promise
  8007. * for that string.
  8008. */
  8009. TemplateFactory.prototype.fromProvider = function (provider, params, context) {
  8010. var deps = services.$injector.annotate(provider);
  8011. var providerFn = isArray(provider) ? tail(provider) : provider;
  8012. var resolvable = new Resolvable("", providerFn, deps);
  8013. return resolvable.get(context);
  8014. };
  8015. /**
  8016. * Creates a component's template by invoking an injectable provider function.
  8017. *
  8018. * @param provider Function to invoke via `locals`
  8019. * @param {Function} injectFn a function used to invoke the template provider
  8020. * @return {string} The template html as a string: "<component-name input1='::$resolve.foo'></component-name>".
  8021. */
  8022. TemplateFactory.prototype.fromComponentProvider = function (provider, params, context) {
  8023. var deps = services.$injector.annotate(provider);
  8024. var providerFn = isArray(provider) ? tail(provider) : provider;
  8025. var resolvable = new Resolvable("", providerFn, deps);
  8026. return resolvable.get(context);
  8027. };
  8028. /**
  8029. * Creates a template from a component's name
  8030. *
  8031. * This implements route-to-component.
  8032. * It works by retrieving the component (directive) metadata from the injector.
  8033. * It analyses the component's bindings, then constructs a template that instantiates the component.
  8034. * The template wires input and output bindings to resolves or from the parent component.
  8035. *
  8036. * @param uiView {object} The parent ui-view (for binding outputs to callbacks)
  8037. * @param context The ResolveContext (for binding outputs to callbacks returned from resolves)
  8038. * @param component {string} Component's name in camel case.
  8039. * @param bindings An object defining the component's bindings: {foo: '<'}
  8040. * @return {string} The template as a string: "<component-name input1='::$resolve.foo'></component-name>".
  8041. */
  8042. TemplateFactory.prototype.makeComponentTemplate = function (uiView, context, component, bindings) {
  8043. bindings = bindings || {};
  8044. // Bind once prefix
  8045. var prefix = ng.version.minor >= 3 ? "::" : "";
  8046. // Convert to kebob name. Add x- prefix if the string starts with `x-` or `data-`
  8047. var kebob = function (camelCase) {
  8048. var kebobed = kebobString(camelCase);
  8049. return /^(x|data)-/.exec(kebobed) ? "x-" + kebobed : kebobed;
  8050. };
  8051. var attributeTpl = function (input) {
  8052. var name = input.name, type = input.type;
  8053. var attrName = kebob(name);
  8054. // If the ui-view has an attribute which matches a binding on the routed component
  8055. // then pass that attribute through to the routed component template.
  8056. // Prefer ui-view wired mappings to resolve data, unless the resolve was explicitly bound using `bindings:`
  8057. if (uiView.attr(attrName) && !bindings[name])
  8058. return attrName + "='" + uiView.attr(attrName) + "'";
  8059. var resolveName = bindings[name] || name;
  8060. // Pre-evaluate the expression for "@" bindings by enclosing in {{ }}
  8061. // some-attr="{{ ::$resolve.someResolveName }}"
  8062. if (type === '@')
  8063. return attrName + "='{{" + prefix + "$resolve." + resolveName + "}}'";
  8064. // Wire "&" callbacks to resolves that return a callback function
  8065. // Get the result of the resolve (should be a function) and annotate it to get its arguments.
  8066. // some-attr="$resolve.someResolveResultName(foo, bar)"
  8067. if (type === '&') {
  8068. var res = context.getResolvable(resolveName);
  8069. var fn = res && res.data;
  8070. var args = fn && services.$injector.annotate(fn) || [];
  8071. // account for array style injection, i.e., ['foo', function(foo) {}]
  8072. var arrayIdxStr = isArray(fn) ? "[" + (fn.length - 1) + "]" : '';
  8073. return attrName + "='$resolve." + resolveName + arrayIdxStr + "(" + args.join(",") + ")'";
  8074. }
  8075. // some-attr="::$resolve.someResolveName"
  8076. return attrName + "='" + prefix + "$resolve." + resolveName + "'";
  8077. };
  8078. var attrs = getComponentBindings(component).map(attributeTpl).join(" ");
  8079. var kebobName = kebob(component);
  8080. return "<" + kebobName + " " + attrs + "></" + kebobName + ">";
  8081. };
  8082. return TemplateFactory;
  8083. }());
  8084. // Gets all the directive(s)' inputs ('@', '=', and '<') and outputs ('&')
  8085. function getComponentBindings(name) {
  8086. var cmpDefs = services.$injector.get(name + "Directive"); // could be multiple
  8087. if (!cmpDefs || !cmpDefs.length)
  8088. throw new Error("Unable to find component named '" + name + "'");
  8089. return cmpDefs.map(getBindings).reduce(unnestR, []);
  8090. }
  8091. // Given a directive definition, find its object input attributes
  8092. // Use different properties, depending on the type of directive (component, bindToController, normal)
  8093. var getBindings = function (def) {
  8094. if (isObject(def.bindToController))
  8095. return scopeBindings(def.bindToController);
  8096. return scopeBindings(def.scope);
  8097. };
  8098. // for ng 1.2 style, process the scope: { input: "=foo" }
  8099. // for ng 1.3 through ng 1.5, process the component's bindToController: { input: "=foo" } object
  8100. var scopeBindings = function (bindingsObj) { return Object.keys(bindingsObj || {})
  8101. .map(function (key) { return [key, /^([=<@&])[?]?(.*)/.exec(bindingsObj[key])]; })
  8102. .filter(function (tuple) { return isDefined(tuple) && isArray(tuple[1]); })
  8103. .map(function (tuple) { return ({ name: tuple[1][2] || tuple[0], type: tuple[1][1] }); }); };
  8104. /** @module ng1 */ /** for typedoc */
  8105. /**
  8106. * The Angular 1 `StateProvider`
  8107. *
  8108. * The `$stateProvider` works similar to Angular's v1 router, but it focuses purely
  8109. * on state.
  8110. *
  8111. * A state corresponds to a "place" in the application in terms of the overall UI and
  8112. * navigation. A state describes (via the controller / template / view properties) what
  8113. * the UI looks like and does at that place.
  8114. *
  8115. * States often have things in common, and the primary way of factoring out these
  8116. * commonalities in this model is via the state hierarchy, i.e. parent/child states aka
  8117. * nested states.
  8118. *
  8119. * The `$stateProvider` provides interfaces to declare these states for your app.
  8120. */
  8121. var StateProvider = /** @class */ (function () {
  8122. function StateProvider(stateRegistry, stateService) {
  8123. this.stateRegistry = stateRegistry;
  8124. this.stateService = stateService;
  8125. createProxyFunctions(val(StateProvider.prototype), this, val(this));
  8126. }
  8127. /**
  8128. * Decorates states when they are registered
  8129. *
  8130. * Allows you to extend (carefully) or override (at your own peril) the
  8131. * `stateBuilder` object used internally by [[StateRegistry]].
  8132. * This can be used to add custom functionality to ui-router,
  8133. * for example inferring templateUrl based on the state name.
  8134. *
  8135. * When passing only a name, it returns the current (original or decorated) builder
  8136. * function that matches `name`.
  8137. *
  8138. * The builder functions that can be decorated are listed below. Though not all
  8139. * necessarily have a good use case for decoration, that is up to you to decide.
  8140. *
  8141. * In addition, users can attach custom decorators, which will generate new
  8142. * properties within the state's internal definition. There is currently no clear
  8143. * use-case for this beyond accessing internal states (i.e. $state.$current),
  8144. * however, expect this to become increasingly relevant as we introduce additional
  8145. * meta-programming features.
  8146. *
  8147. * **Warning**: Decorators should not be interdependent because the order of
  8148. * execution of the builder functions in non-deterministic. Builder functions
  8149. * should only be dependent on the state definition object and super function.
  8150. *
  8151. *
  8152. * Existing builder functions and current return values:
  8153. *
  8154. * - **parent** `{object}` - returns the parent state object.
  8155. * - **data** `{object}` - returns state data, including any inherited data that is not
  8156. * overridden by own values (if any).
  8157. * - **url** `{object}` - returns a {@link ui.router.util.type:UrlMatcher UrlMatcher}
  8158. * or `null`.
  8159. * - **navigable** `{object}` - returns closest ancestor state that has a URL (aka is
  8160. * navigable).
  8161. * - **params** `{object}` - returns an array of state params that are ensured to
  8162. * be a super-set of parent's params.
  8163. * - **views** `{object}` - returns a views object where each key is an absolute view
  8164. * name (i.e. "viewName@stateName") and each value is the config object
  8165. * (template, controller) for the view. Even when you don't use the views object
  8166. * explicitly on a state config, one is still created for you internally.
  8167. * So by decorating this builder function you have access to decorating template
  8168. * and controller properties.
  8169. * - **ownParams** `{object}` - returns an array of params that belong to the state,
  8170. * not including any params defined by ancestor states.
  8171. * - **path** `{string}` - returns the full path from the root down to this state.
  8172. * Needed for state activation.
  8173. * - **includes** `{object}` - returns an object that includes every state that
  8174. * would pass a `$state.includes()` test.
  8175. *
  8176. * #### Example:
  8177. * Override the internal 'views' builder with a function that takes the state
  8178. * definition, and a reference to the internal function being overridden:
  8179. * ```js
  8180. * $stateProvider.decorator('views', function (state, parent) {
  8181. * let result = {},
  8182. * views = parent(state);
  8183. *
  8184. * angular.forEach(views, function (config, name) {
  8185. * let autoName = (state.name + '.' + name).replace('.', '/');
  8186. * config.templateUrl = config.templateUrl || '/partials/' + autoName + '.html';
  8187. * result[name] = config;
  8188. * });
  8189. * return result;
  8190. * });
  8191. *
  8192. * $stateProvider.state('home', {
  8193. * views: {
  8194. * 'contact.list': { controller: 'ListController' },
  8195. * 'contact.item': { controller: 'ItemController' }
  8196. * }
  8197. * });
  8198. * ```
  8199. *
  8200. *
  8201. * ```js
  8202. * // Auto-populates list and item views with /partials/home/contact/list.html,
  8203. * // and /partials/home/contact/item.html, respectively.
  8204. * $state.go('home');
  8205. * ```
  8206. *
  8207. * @param {string} name The name of the builder function to decorate.
  8208. * @param {object} func A function that is responsible for decorating the original
  8209. * builder function. The function receives two parameters:
  8210. *
  8211. * - `{object}` - state - The state config object.
  8212. * - `{object}` - super - The original builder function.
  8213. *
  8214. * @return {object} $stateProvider - $stateProvider instance
  8215. */
  8216. StateProvider.prototype.decorator = function (name, func) {
  8217. return this.stateRegistry.decorator(name, func) || this;
  8218. };
  8219. StateProvider.prototype.state = function (name, definition) {
  8220. if (isObject(name)) {
  8221. definition = name;
  8222. }
  8223. else {
  8224. definition.name = name;
  8225. }
  8226. this.stateRegistry.register(definition);
  8227. return this;
  8228. };
  8229. /**
  8230. * Registers an invalid state handler
  8231. *
  8232. * This is a passthrough to [[StateService.onInvalid]] for ng1.
  8233. */
  8234. StateProvider.prototype.onInvalid = function (callback) {
  8235. return this.stateService.onInvalid(callback);
  8236. };
  8237. return StateProvider;
  8238. }());
  8239. /** @module ng1 */ /** */
  8240. /**
  8241. * This is a [[StateBuilder.builder]] function for angular1 `onEnter`, `onExit`,
  8242. * `onRetain` callback hooks on a [[Ng1StateDeclaration]].
  8243. *
  8244. * When the [[StateBuilder]] builds a [[StateObject]] object from a raw [[StateDeclaration]], this builder
  8245. * ensures that those hooks are injectable for @uirouter/angularjs (ng1).
  8246. */
  8247. var getStateHookBuilder = function (hookName) {
  8248. return function stateHookBuilder(state, parentFn) {
  8249. var hook = state[hookName];
  8250. var pathname = hookName === 'onExit' ? 'from' : 'to';
  8251. function decoratedNg1Hook(trans, state) {
  8252. var resolveContext = new ResolveContext(trans.treeChanges(pathname));
  8253. var locals = extend(getLocals(resolveContext), { $state$: state, $transition$: trans });
  8254. return services.$injector.invoke(hook, this, locals);
  8255. }
  8256. return hook ? decoratedNg1Hook : undefined;
  8257. };
  8258. };
  8259. /**
  8260. * Implements UI-Router LocationServices and LocationConfig using Angular 1's $location service
  8261. */
  8262. var Ng1LocationServices = /** @class */ (function () {
  8263. function Ng1LocationServices($locationProvider) {
  8264. // .onChange() registry
  8265. this._urlListeners = [];
  8266. this.$locationProvider = $locationProvider;
  8267. var _lp = val($locationProvider);
  8268. createProxyFunctions(_lp, this, _lp, ['hashPrefix']);
  8269. }
  8270. Ng1LocationServices.prototype.dispose = function () { };
  8271. Ng1LocationServices.prototype.onChange = function (callback) {
  8272. var _this = this;
  8273. this._urlListeners.push(callback);
  8274. return function () { return removeFrom(_this._urlListeners)(callback); };
  8275. };
  8276. Ng1LocationServices.prototype.html5Mode = function () {
  8277. var html5Mode = this.$locationProvider.html5Mode();
  8278. html5Mode = isObject(html5Mode) ? html5Mode.enabled : html5Mode;
  8279. return html5Mode && this.$sniffer.history;
  8280. };
  8281. Ng1LocationServices.prototype.url = function (newUrl, replace, state) {
  8282. if (replace === void 0) { replace = false; }
  8283. if (newUrl)
  8284. this.$location.url(newUrl);
  8285. if (replace)
  8286. this.$location.replace();
  8287. if (state)
  8288. this.$location.state(state);
  8289. return this.$location.url();
  8290. };
  8291. Ng1LocationServices.prototype._runtimeServices = function ($rootScope, $location, $sniffer, $browser) {
  8292. var _this = this;
  8293. this.$location = $location;
  8294. this.$sniffer = $sniffer;
  8295. // Bind $locationChangeSuccess to the listeners registered in LocationService.onChange
  8296. $rootScope.$on("$locationChangeSuccess", function (evt) { return _this._urlListeners.forEach(function (fn) { return fn(evt); }); });
  8297. var _loc = val($location);
  8298. var _browser = val($browser);
  8299. // Bind these LocationService functions to $location
  8300. createProxyFunctions(_loc, this, _loc, ["replace", "path", "search", "hash"]);
  8301. // Bind these LocationConfig functions to $location
  8302. createProxyFunctions(_loc, this, _loc, ['port', 'protocol', 'host']);
  8303. // Bind these LocationConfig functions to $browser
  8304. createProxyFunctions(_browser, this, _browser, ['baseHref']);
  8305. };
  8306. /**
  8307. * Applys ng1-specific path parameter encoding
  8308. *
  8309. * The Angular 1 `$location` service is a bit weird.
  8310. * It doesn't allow slashes to be encoded/decoded bi-directionally.
  8311. *
  8312. * See the writeup at https://github.com/angular-ui/ui-router/issues/2598
  8313. *
  8314. * This code patches the `path` parameter type so it encoded/decodes slashes as ~2F
  8315. *
  8316. * @param router
  8317. */
  8318. Ng1LocationServices.monkeyPatchPathParameterType = function (router) {
  8319. var pathType = router.urlMatcherFactory.type('path');
  8320. pathType.encode = function (val) {
  8321. return val != null ? val.toString().replace(/(~|\/)/g, function (m) { return ({ '~': '~~', '/': '~2F' }[m]); }) : val;
  8322. };
  8323. pathType.decode = function (val) {
  8324. return val != null ? val.toString().replace(/(~~|~2F)/g, function (m) { return ({ '~~': '~', '~2F': '/' }[m]); }) : val;
  8325. };
  8326. };
  8327. return Ng1LocationServices;
  8328. }());
  8329. /** @module url */ /** */
  8330. /**
  8331. * Manages rules for client-side URL
  8332. *
  8333. * ### Deprecation warning:
  8334. * This class is now considered to be an internal API
  8335. * Use the [[UrlService]] instead.
  8336. * For configuring URL rules, use the [[UrlRulesApi]] which can be found as [[UrlService.rules]].
  8337. *
  8338. * This class manages the router rules for what to do when the URL changes.
  8339. *
  8340. * This provider remains for backwards compatibility.
  8341. *
  8342. * @deprecated
  8343. */
  8344. var UrlRouterProvider = /** @class */ (function () {
  8345. /** @hidden */
  8346. function UrlRouterProvider(router) {
  8347. this._router = router;
  8348. this._urlRouter = router.urlRouter;
  8349. }
  8350. /** @hidden */
  8351. UrlRouterProvider.prototype.$get = function () {
  8352. var urlRouter = this._urlRouter;
  8353. urlRouter.update(true);
  8354. if (!urlRouter.interceptDeferred)
  8355. urlRouter.listen();
  8356. return urlRouter;
  8357. };
  8358. /**
  8359. * Registers a url handler function.
  8360. *
  8361. * Registers a low level url handler (a `rule`).
  8362. * A rule detects specific URL patterns and returns a redirect, or performs some action.
  8363. *
  8364. * If a rule returns a string, the URL is replaced with the string, and all rules are fired again.
  8365. *
  8366. * #### Example:
  8367. * ```js
  8368. * var app = angular.module('app', ['ui.router.router']);
  8369. *
  8370. * app.config(function ($urlRouterProvider) {
  8371. * // Here's an example of how you might allow case insensitive urls
  8372. * $urlRouterProvider.rule(function ($injector, $location) {
  8373. * var path = $location.path(),
  8374. * normalized = path.toLowerCase();
  8375. *
  8376. * if (path !== normalized) {
  8377. * return normalized;
  8378. * }
  8379. * });
  8380. * });
  8381. * ```
  8382. *
  8383. * @param ruleFn
  8384. * Handler function that takes `$injector` and `$location` services as arguments.
  8385. * You can use them to detect a url and return a different url as a string.
  8386. *
  8387. * @return [[UrlRouterProvider]] (`this`)
  8388. */
  8389. UrlRouterProvider.prototype.rule = function (ruleFn) {
  8390. var _this = this;
  8391. if (!isFunction(ruleFn))
  8392. throw new Error("'rule' must be a function");
  8393. var match = function () {
  8394. return ruleFn(services.$injector, _this._router.locationService);
  8395. };
  8396. var rule = new BaseUrlRule(match, identity);
  8397. this._urlRouter.rule(rule);
  8398. return this;
  8399. };
  8400. /**
  8401. * Defines the path or behavior to use when no url can be matched.
  8402. *
  8403. * #### Example:
  8404. * ```js
  8405. * var app = angular.module('app', ['ui.router.router']);
  8406. *
  8407. * app.config(function ($urlRouterProvider) {
  8408. * // if the path doesn't match any of the urls you configured
  8409. * // otherwise will take care of routing the user to the
  8410. * // specified url
  8411. * $urlRouterProvider.otherwise('/index');
  8412. *
  8413. * // Example of using function rule as param
  8414. * $urlRouterProvider.otherwise(function ($injector, $location) {
  8415. * return '/a/valid/url';
  8416. * });
  8417. * });
  8418. * ```
  8419. *
  8420. * @param rule
  8421. * The url path you want to redirect to or a function rule that returns the url path or performs a `$state.go()`.
  8422. * The function version is passed two params: `$injector` and `$location` services, and should return a url string.
  8423. *
  8424. * @return {object} `$urlRouterProvider` - `$urlRouterProvider` instance
  8425. */
  8426. UrlRouterProvider.prototype.otherwise = function (rule) {
  8427. var _this = this;
  8428. var urlRouter = this._urlRouter;
  8429. if (isString(rule)) {
  8430. urlRouter.otherwise(rule);
  8431. }
  8432. else if (isFunction(rule)) {
  8433. urlRouter.otherwise(function () { return rule(services.$injector, _this._router.locationService); });
  8434. }
  8435. else {
  8436. throw new Error("'rule' must be a string or function");
  8437. }
  8438. return this;
  8439. };
  8440. /**
  8441. * Registers a handler for a given url matching.
  8442. *
  8443. * If the handler is a string, it is
  8444. * treated as a redirect, and is interpolated according to the syntax of match
  8445. * (i.e. like `String.replace()` for `RegExp`, or like a `UrlMatcher` pattern otherwise).
  8446. *
  8447. * If the handler is a function, it is injectable.
  8448. * It gets invoked if `$location` matches.
  8449. * You have the option of inject the match object as `$match`.
  8450. *
  8451. * The handler can return
  8452. *
  8453. * - **falsy** to indicate that the rule didn't match after all, then `$urlRouter`
  8454. * will continue trying to find another one that matches.
  8455. * - **string** which is treated as a redirect and passed to `$location.url()`
  8456. * - **void** or any **truthy** value tells `$urlRouter` that the url was handled.
  8457. *
  8458. * #### Example:
  8459. * ```js
  8460. * var app = angular.module('app', ['ui.router.router']);
  8461. *
  8462. * app.config(function ($urlRouterProvider) {
  8463. * $urlRouterProvider.when($state.url, function ($match, $stateParams) {
  8464. * if ($state.$current.navigable !== state ||
  8465. * !equalForKeys($match, $stateParams) {
  8466. * $state.transitionTo(state, $match, false);
  8467. * }
  8468. * });
  8469. * });
  8470. * ```
  8471. *
  8472. * @param what A pattern string to match, compiled as a [[UrlMatcher]].
  8473. * @param handler The path (or function that returns a path) that you want to redirect your user to.
  8474. * @param ruleCallback [optional] A callback that receives the `rule` registered with [[UrlMatcher.rule]]
  8475. *
  8476. * Note: the handler may also invoke arbitrary code, such as `$state.go()`
  8477. */
  8478. UrlRouterProvider.prototype.when = function (what, handler) {
  8479. if (isArray(handler) || isFunction(handler)) {
  8480. handler = UrlRouterProvider.injectableHandler(this._router, handler);
  8481. }
  8482. this._urlRouter.when(what, handler);
  8483. return this;
  8484. };
  8485. UrlRouterProvider.injectableHandler = function (router, handler) {
  8486. return function (match) {
  8487. return services.$injector.invoke(handler, null, { $match: match, $stateParams: router.globals.params });
  8488. };
  8489. };
  8490. /**
  8491. * Disables monitoring of the URL.
  8492. *
  8493. * Call this method before UI-Router has bootstrapped.
  8494. * It will stop UI-Router from performing the initial url sync.
  8495. *
  8496. * This can be useful to perform some asynchronous initialization before the router starts.
  8497. * Once the initialization is complete, call [[listen]] to tell UI-Router to start watching and synchronizing the URL.
  8498. *
  8499. * #### Example:
  8500. * ```js
  8501. * var app = angular.module('app', ['ui.router']);
  8502. *
  8503. * app.config(function ($urlRouterProvider) {
  8504. * // Prevent $urlRouter from automatically intercepting URL changes;
  8505. * $urlRouterProvider.deferIntercept();
  8506. * })
  8507. *
  8508. * app.run(function (MyService, $urlRouter, $http) {
  8509. * $http.get("/stuff").then(function(resp) {
  8510. * MyService.doStuff(resp.data);
  8511. * $urlRouter.listen();
  8512. * $urlRouter.sync();
  8513. * });
  8514. * });
  8515. * ```
  8516. *
  8517. * @param defer Indicates whether to defer location change interception.
  8518. * Passing no parameter is equivalent to `true`.
  8519. */
  8520. UrlRouterProvider.prototype.deferIntercept = function (defer) {
  8521. this._urlRouter.deferIntercept(defer);
  8522. };
  8523. return UrlRouterProvider;
  8524. }());
  8525. /**
  8526. * # Angular 1 types
  8527. *
  8528. * UI-Router core provides various Typescript types which you can use for code completion and validating parameter values, etc.
  8529. * The customizations to the core types for Angular UI-Router are documented here.
  8530. *
  8531. * The optional [[$resolve]] service is also documented here.
  8532. *
  8533. * @module ng1
  8534. * @preferred
  8535. */
  8536. /** for typedoc */
  8537. ng.module("ui.router.angular1", []);
  8538. var mod_init = ng.module('ui.router.init', []);
  8539. var mod_util = ng.module('ui.router.util', ['ng', 'ui.router.init']);
  8540. var mod_rtr = ng.module('ui.router.router', ['ui.router.util']);
  8541. var mod_state = ng.module('ui.router.state', ['ui.router.router', 'ui.router.util', 'ui.router.angular1']);
  8542. var mod_main = ng.module('ui.router', ['ui.router.init', 'ui.router.state', 'ui.router.angular1']);
  8543. var mod_cmpt = ng.module('ui.router.compat', ['ui.router']); // tslint:disable-line
  8544. var router = null;
  8545. $uiRouter.$inject = ['$locationProvider'];
  8546. /** This angular 1 provider instantiates a Router and exposes its services via the angular injector */
  8547. function $uiRouter($locationProvider) {
  8548. // Create a new instance of the Router when the $uiRouterProvider is initialized
  8549. router = this.router = new UIRouter();
  8550. router.stateProvider = new StateProvider(router.stateRegistry, router.stateService);
  8551. // Apply ng1 specific StateBuilder code for `views`, `resolve`, and `onExit/Retain/Enter` properties
  8552. router.stateRegistry.decorator("views", ng1ViewsBuilder);
  8553. router.stateRegistry.decorator("onExit", getStateHookBuilder("onExit"));
  8554. router.stateRegistry.decorator("onRetain", getStateHookBuilder("onRetain"));
  8555. router.stateRegistry.decorator("onEnter", getStateHookBuilder("onEnter"));
  8556. router.viewService._pluginapi._viewConfigFactory('ng1', getNg1ViewConfigFactory());
  8557. var ng1LocationService = router.locationService = router.locationConfig = new Ng1LocationServices($locationProvider);
  8558. Ng1LocationServices.monkeyPatchPathParameterType(router);
  8559. // backwards compat: also expose router instance as $uiRouterProvider.router
  8560. router['router'] = router;
  8561. router['$get'] = $get;
  8562. $get.$inject = ['$location', '$browser', '$sniffer', '$rootScope', '$http', '$templateCache'];
  8563. function $get($location, $browser, $sniffer, $rootScope, $http, $templateCache) {
  8564. ng1LocationService._runtimeServices($rootScope, $location, $sniffer, $browser);
  8565. delete router['router'];
  8566. delete router['$get'];
  8567. return router;
  8568. }
  8569. return router;
  8570. }
  8571. var getProviderFor = function (serviceName) { return ['$uiRouterProvider', function ($urp) {
  8572. var service = $urp.router[serviceName];
  8573. service["$get"] = function () { return service; };
  8574. return service;
  8575. }]; };
  8576. // This effectively calls $get() on `$uiRouterProvider` to trigger init (when ng enters runtime)
  8577. runBlock.$inject = ['$injector', '$q', '$uiRouter'];
  8578. function runBlock($injector, $q, $uiRouter) {
  8579. services.$injector = $injector;
  8580. services.$q = $q;
  8581. // The $injector is now available.
  8582. // Find any resolvables that had dependency annotation deferred
  8583. $uiRouter.stateRegistry.get()
  8584. .map(function (x) { return x.$$state().resolvables; })
  8585. .reduce(unnestR, [])
  8586. .filter(function (x) { return x.deps === "deferred"; })
  8587. .forEach(function (resolvable) { return resolvable.deps = $injector.annotate(resolvable.resolveFn, $injector.strictDi); });
  8588. }
  8589. // $urlRouter service and $urlRouterProvider
  8590. var getUrlRouterProvider = function (uiRouter) {
  8591. return uiRouter.urlRouterProvider = new UrlRouterProvider(uiRouter);
  8592. };
  8593. // $state service and $stateProvider
  8594. // $urlRouter service and $urlRouterProvider
  8595. var getStateProvider = function () {
  8596. return extend(router.stateProvider, { $get: function () { return router.stateService; } });
  8597. };
  8598. watchDigests.$inject = ['$rootScope'];
  8599. function watchDigests($rootScope) {
  8600. $rootScope.$watch(function () { trace.approximateDigests++; });
  8601. }
  8602. mod_init.provider("$uiRouter", $uiRouter);
  8603. mod_rtr.provider('$urlRouter', ['$uiRouterProvider', getUrlRouterProvider]);
  8604. mod_util.provider('$urlService', getProviderFor('urlService'));
  8605. mod_util.provider('$urlMatcherFactory', ['$uiRouterProvider', function () { return router.urlMatcherFactory; }]);
  8606. mod_util.provider('$templateFactory', function () { return new TemplateFactory(); });
  8607. mod_state.provider('$stateRegistry', getProviderFor('stateRegistry'));
  8608. mod_state.provider('$uiRouterGlobals', getProviderFor('globals'));
  8609. mod_state.provider('$transitions', getProviderFor('transitionService'));
  8610. mod_state.provider('$state', ['$uiRouterProvider', getStateProvider]);
  8611. mod_state.factory('$stateParams', ['$uiRouter', function ($uiRouter) { return $uiRouter.globals.params; }]);
  8612. mod_main.factory('$view', function () { return router.viewService; });
  8613. mod_main.service("$trace", function () { return trace; });
  8614. mod_main.run(watchDigests);
  8615. mod_util.run(['$urlMatcherFactory', function ($urlMatcherFactory) { }]);
  8616. mod_state.run(['$state', function ($state) { }]);
  8617. mod_rtr.run(['$urlRouter', function ($urlRouter) { }]);
  8618. mod_init.run(runBlock);
  8619. /** @hidden TODO: find a place to move this */
  8620. var getLocals = function (ctx) {
  8621. var tokens = ctx.getTokens().filter(isString);
  8622. var tuples = tokens.map(function (key) {
  8623. var resolvable = ctx.getResolvable(key);
  8624. var waitPolicy = ctx.getPolicy(resolvable).async;
  8625. return [key, waitPolicy === 'NOWAIT' ? resolvable.promise : resolvable.data];
  8626. });
  8627. return tuples.reduce(applyPairs, {});
  8628. };
  8629. /**
  8630. * # Angular 1 injectable services
  8631. *
  8632. * This is a list of the objects which can be injected using angular's injector.
  8633. *
  8634. * There are three different kind of injectable objects:
  8635. *
  8636. * ## **Provider** objects
  8637. * #### injectable into a `.config()` block during configtime
  8638. *
  8639. * - [[$uiRouterProvider]]: The UI-Router instance
  8640. * - [[$stateProvider]]: State registration
  8641. * - [[$transitionsProvider]]: Transition hooks
  8642. * - [[$urlServiceProvider]]: All URL related public APIs
  8643. *
  8644. * - [[$uiViewScrollProvider]]: Disable ui-router view scrolling
  8645. * - [[$urlRouterProvider]]: (deprecated) Url matching rules
  8646. * - [[$urlMatcherFactoryProvider]]: (deprecated) Url parsing config
  8647. *
  8648. * ## **Service** objects
  8649. * #### injectable globally during runtime
  8650. *
  8651. * - [[$uiRouter]]: The UI-Router instance
  8652. * - [[$trace]]: Enable transition trace/debug
  8653. * - [[$transitions]]: Transition hooks
  8654. * - [[$state]]: Imperative state related APIs
  8655. * - [[$stateRegistry]]: State registration
  8656. * - [[$urlService]]: All URL related public APIs
  8657. * - [[$uiRouterGlobals]]: Global variables
  8658. * - [[$uiViewScroll]]: Scroll an element into view
  8659. *
  8660. * - [[$stateParams]]: (deprecated) Global state param values
  8661. * - [[$urlRouter]]: (deprecated) URL synchronization
  8662. * - [[$urlMatcherFactory]]: (deprecated) URL parsing config
  8663. *
  8664. * ## **Per-Transition** objects
  8665. *
  8666. * - These kind of objects are injectable into:
  8667. * - Resolves ([[Ng1StateDeclaration.resolve]]),
  8668. * - Transition Hooks ([[TransitionService.onStart]], etc),
  8669. * - Routed Controllers ([[Ng1ViewDeclaration.controller]])
  8670. *
  8671. * #### Different instances are injected based on the [[Transition]]
  8672. *
  8673. * - [[$transition$]]: The current Transition object
  8674. * - [[$stateParams]]: State param values for pending Transition (deprecated)
  8675. * - Any resolve data defined using [[Ng1StateDeclaration.resolve]]
  8676. *
  8677. * @ng1api
  8678. * @preferred
  8679. * @module injectables
  8680. */ /** */
  8681. /**
  8682. * The current (or pending) State Parameters
  8683. *
  8684. * An injectable global **Service Object** which holds the state parameters for the latest **SUCCESSFUL** transition.
  8685. *
  8686. * The values are not updated until *after* a `Transition` successfully completes.
  8687. *
  8688. * **Also:** an injectable **Per-Transition Object** object which holds the pending state parameters for the pending `Transition` currently running.
  8689. *
  8690. * ### Deprecation warning:
  8691. *
  8692. * The value injected for `$stateParams` is different depending on where it is injected.
  8693. *
  8694. * - When injected into an angular service, the object injected is the global **Service Object** with the parameter values for the latest successful `Transition`.
  8695. * - When injected into transition hooks, resolves, or view controllers, the object is the **Per-Transition Object** with the parameter values for the running `Transition`.
  8696. *
  8697. * Because of these confusing details, this service is deprecated.
  8698. *
  8699. * ### Instead of using the global `$stateParams` service object,
  8700. * inject [[$uiRouterGlobals]] and use [[UIRouterGlobals.params]]
  8701. *
  8702. * ```js
  8703. * MyService.$inject = ['$uiRouterGlobals'];
  8704. * function MyService($uiRouterGlobals) {
  8705. * return {
  8706. * paramValues: function () {
  8707. * return $uiRouterGlobals.params;
  8708. * }
  8709. * }
  8710. * }
  8711. * ```
  8712. *
  8713. * ### Instead of using the per-transition `$stateParams` object,
  8714. * inject the current `Transition` (as [[$transition$]]) and use [[Transition.params]]
  8715. *
  8716. * ```js
  8717. * MyController.$inject = ['$transition$'];
  8718. * function MyController($transition$) {
  8719. * var username = $transition$.params().username;
  8720. * // .. do something with username
  8721. * }
  8722. * ```
  8723. *
  8724. * ---
  8725. *
  8726. * This object can be injected into other services.
  8727. *
  8728. * #### Deprecated Example:
  8729. * ```js
  8730. * SomeService.$inject = ['$http', '$stateParams'];
  8731. * function SomeService($http, $stateParams) {
  8732. * return {
  8733. * getUser: function() {
  8734. * return $http.get('/api/users/' + $stateParams.username);
  8735. * }
  8736. * }
  8737. * };
  8738. * angular.service('SomeService', SomeService);
  8739. * ```
  8740. * @deprecated
  8741. */
  8742. /**
  8743. * # Angular 1 Directives
  8744. *
  8745. * These are the directives included in UI-Router for Angular 1.
  8746. * These directives are used in templates to create viewports and link/navigate to states.
  8747. *
  8748. * @ng1api
  8749. * @preferred
  8750. * @module directives
  8751. */ /** for typedoc */
  8752. /** @hidden */
  8753. function parseStateRef(ref) {
  8754. var paramsOnly = ref.match(/^\s*({[^}]*})\s*$/), parsed;
  8755. if (paramsOnly)
  8756. ref = '(' + paramsOnly[1] + ')';
  8757. parsed = ref.replace(/\n/g, " ").match(/^\s*([^(]*?)\s*(\((.*)\))?\s*$/);
  8758. if (!parsed || parsed.length !== 4)
  8759. throw new Error("Invalid state ref '" + ref + "'");
  8760. return { state: parsed[1] || null, paramExpr: parsed[3] || null };
  8761. }
  8762. /** @hidden */
  8763. function stateContext(el) {
  8764. var $uiView = el.parent().inheritedData('$uiView');
  8765. var path = parse('$cfg.path')($uiView);
  8766. return path ? tail(path).state.name : undefined;
  8767. }
  8768. /** @hidden */
  8769. function processedDef($state, $element, def) {
  8770. var uiState = def.uiState || $state.current.name;
  8771. var uiStateOpts = extend(defaultOpts($element, $state), def.uiStateOpts || {});
  8772. var href = $state.href(uiState, def.uiStateParams, uiStateOpts);
  8773. return { uiState: uiState, uiStateParams: def.uiStateParams, uiStateOpts: uiStateOpts, href: href };
  8774. }
  8775. /** @hidden */
  8776. function getTypeInfo(el) {
  8777. // SVGAElement does not use the href attribute, but rather the 'xlinkHref' attribute.
  8778. var isSvg = Object.prototype.toString.call(el.prop('href')) === '[object SVGAnimatedString]';
  8779. var isForm = el[0].nodeName === "FORM";
  8780. return {
  8781. attr: isForm ? "action" : (isSvg ? 'xlink:href' : 'href'),
  8782. isAnchor: el.prop("tagName").toUpperCase() === "A",
  8783. clickable: !isForm
  8784. };
  8785. }
  8786. /** @hidden */
  8787. function clickHook(el, $state, $timeout, type, getDef) {
  8788. return function (e) {
  8789. var button = e.which || e.button, target = getDef();
  8790. if (!(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || el.attr('target'))) {
  8791. // HACK: This is to allow ng-clicks to be processed before the transition is initiated:
  8792. var transition = $timeout(function () {
  8793. $state.go(target.uiState, target.uiStateParams, target.uiStateOpts);
  8794. });
  8795. e.preventDefault();
  8796. // if the state has no URL, ignore one preventDefault from the <a> directive.
  8797. var ignorePreventDefaultCount = type.isAnchor && !target.href ? 1 : 0;
  8798. e.preventDefault = function () {
  8799. if (ignorePreventDefaultCount-- <= 0)
  8800. $timeout.cancel(transition);
  8801. };
  8802. }
  8803. };
  8804. }
  8805. /** @hidden */
  8806. function defaultOpts(el, $state) {
  8807. return {
  8808. relative: stateContext(el) || $state.$current,
  8809. inherit: true,
  8810. source: "sref"
  8811. };
  8812. }
  8813. /** @hidden */
  8814. function bindEvents(element, scope, hookFn, uiStateOpts) {
  8815. var events;
  8816. if (uiStateOpts) {
  8817. events = uiStateOpts.events;
  8818. }
  8819. if (!isArray(events)) {
  8820. events = ['click'];
  8821. }
  8822. var on = element.on ? 'on' : 'bind';
  8823. for (var _i = 0, events_1 = events; _i < events_1.length; _i++) {
  8824. var event_1 = events_1[_i];
  8825. element[on](event_1, hookFn);
  8826. }
  8827. scope.$on('$destroy', function () {
  8828. var off = element.off ? 'off' : 'unbind';
  8829. for (var _i = 0, events_2 = events; _i < events_2.length; _i++) {
  8830. var event_2 = events_2[_i];
  8831. element[off](event_2, hookFn);
  8832. }
  8833. });
  8834. }
  8835. /**
  8836. * `ui-sref`: A directive for linking to a state
  8837. *
  8838. * A directive which links to a state (and optionally, parameters).
  8839. * When clicked, this directive activates the linked state with the supplied parameter values.
  8840. *
  8841. * ### Linked State
  8842. * The attribute value of the `ui-sref` is the name of the state to link to.
  8843. *
  8844. * #### Example:
  8845. * This will activate the `home` state when the link is clicked.
  8846. * ```html
  8847. * <a ui-sref="home">Home</a>
  8848. * ```
  8849. *
  8850. * ### Relative Links
  8851. * You can also use relative state paths within `ui-sref`, just like a relative path passed to `$state.go()` ([[StateService.go]]).
  8852. * You just need to be aware that the path is relative to the state that *created* the link.
  8853. * This allows a state to create a relative `ui-sref` which always targets the same destination.
  8854. *
  8855. * #### Example:
  8856. * Both these links are relative to the parent state, even when a child state is currently active.
  8857. * ```html
  8858. * <a ui-sref=".child1">child 1 state</a>
  8859. * <a ui-sref=".child2">child 2 state</a>
  8860. * ```
  8861. *
  8862. * This link activates the parent state.
  8863. * ```html
  8864. * <a ui-sref="^">Return</a>
  8865. * ```
  8866. *
  8867. * ### hrefs
  8868. * If the linked state has a URL, the directive will automatically generate and
  8869. * update the `href` attribute (using the [[StateService.href]] method).
  8870. *
  8871. * #### Example:
  8872. * Assuming the `users` state has a url of `/users/`
  8873. * ```html
  8874. * <a ui-sref="users" href="/users/">Users</a>
  8875. * ```
  8876. *
  8877. * ### Parameter Values
  8878. * In addition to the state name, a `ui-sref` can include parameter values which are applied when activating the state.
  8879. * Param values can be provided in the `ui-sref` value after the state name, enclosed by parentheses.
  8880. * The content inside the parentheses is an expression, evaluated to the parameter values.
  8881. *
  8882. * #### Example:
  8883. * This example renders a list of links to users.
  8884. * The state's `userId` parameter value comes from each user's `user.id` property.
  8885. * ```html
  8886. * <li ng-repeat="user in users">
  8887. * <a ui-sref="users.detail({ userId: user.id })">{{ user.displayName }}</a>
  8888. * </li>
  8889. * ```
  8890. *
  8891. * Note:
  8892. * The parameter values expression is `$watch`ed for updates.
  8893. *
  8894. * ### Transition Options
  8895. * You can specify [[TransitionOptions]] to pass to [[StateService.go]] by using the `ui-sref-opts` attribute.
  8896. * Options are restricted to `location`, `inherit`, and `reload`.
  8897. *
  8898. * #### Example:
  8899. * ```html
  8900. * <a ui-sref="home" ui-sref-opts="{ reload: true }">Home</a>
  8901. * ```
  8902. *
  8903. * ### Other DOM Events
  8904. *
  8905. * You can also customize which DOM events to respond to (instead of `click`) by
  8906. * providing an `events` array in the `ui-sref-opts` attribute.
  8907. *
  8908. * #### Example:
  8909. * ```html
  8910. * <input type="text" ui-sref="contacts" ui-sref-opts="{ events: ['change', 'blur'] }">
  8911. * ```
  8912. *
  8913. * ### Highlighting the active link
  8914. * This directive can be used in conjunction with [[uiSrefActive]] to highlight the active link.
  8915. *
  8916. * ### Examples
  8917. * If you have the following template:
  8918. *
  8919. * ```html
  8920. * <a ui-sref="home">Home</a>
  8921. * <a ui-sref="about">About</a>
  8922. * <a ui-sref="{page: 2}">Next page</a>
  8923. *
  8924. * <ul>
  8925. * <li ng-repeat="contact in contacts">
  8926. * <a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a>
  8927. * </li>
  8928. * </ul>
  8929. * ```
  8930. *
  8931. * Then (assuming the current state is `contacts`) the rendered html including hrefs would be:
  8932. *
  8933. * ```html
  8934. * <a href="#/home" ui-sref="home">Home</a>
  8935. * <a href="#/about" ui-sref="about">About</a>
  8936. * <a href="#/contacts?page=2" ui-sref="{page: 2}">Next page</a>
  8937. *
  8938. * <ul>
  8939. * <li ng-repeat="contact in contacts">
  8940. * <a href="#/contacts/1" ui-sref="contacts.detail({ id: contact.id })">Joe</a>
  8941. * </li>
  8942. * <li ng-repeat="contact in contacts">
  8943. * <a href="#/contacts/2" ui-sref="contacts.detail({ id: contact.id })">Alice</a>
  8944. * </li>
  8945. * <li ng-repeat="contact in contacts">
  8946. * <a href="#/contacts/3" ui-sref="contacts.detail({ id: contact.id })">Bob</a>
  8947. * </li>
  8948. * </ul>
  8949. *
  8950. * <a href="#/home" ui-sref="home" ui-sref-opts="{reload: true}">Home</a>
  8951. * ```
  8952. *
  8953. * ### Notes
  8954. *
  8955. * - You can use `ui-sref` to change **only the parameter values** by omitting the state name and parentheses.
  8956. * #### Example:
  8957. * Sets the `lang` parameter to `en` and remains on the same state.
  8958. *
  8959. * ```html
  8960. * <a ui-sref="{ lang: 'en' }">English</a>
  8961. * ```
  8962. *
  8963. * - A middle-click, right-click, or ctrl-click is handled (natively) by the browser to open the href in a new window, for example.
  8964. *
  8965. * - Unlike the parameter values expression, the state name is not `$watch`ed (for performance reasons).
  8966. * If you need to dynamically update the state being linked to, use the fully dynamic [[uiState]] directive.
  8967. */
  8968. var uiSref;
  8969. uiSref = ['$uiRouter', '$timeout',
  8970. function $StateRefDirective($uiRouter, $timeout) {
  8971. var $state = $uiRouter.stateService;
  8972. return {
  8973. restrict: 'A',
  8974. require: ['?^uiSrefActive', '?^uiSrefActiveEq'],
  8975. link: function (scope, element, attrs, uiSrefActive) {
  8976. var type = getTypeInfo(element);
  8977. var active = uiSrefActive[1] || uiSrefActive[0];
  8978. var unlinkInfoFn = null;
  8979. var hookFn;
  8980. var rawDef = {};
  8981. var getDef = function () { return processedDef($state, element, rawDef); };
  8982. var ref = parseStateRef(attrs.uiSref);
  8983. rawDef.uiState = ref.state;
  8984. rawDef.uiStateOpts = attrs.uiSrefOpts ? scope.$eval(attrs.uiSrefOpts) : {};
  8985. function update() {
  8986. var def = getDef();
  8987. if (unlinkInfoFn)
  8988. unlinkInfoFn();
  8989. if (active)
  8990. unlinkInfoFn = active.$$addStateInfo(def.uiState, def.uiStateParams);
  8991. if (def.href != null)
  8992. attrs.$set(type.attr, def.href);
  8993. }
  8994. if (ref.paramExpr) {
  8995. scope.$watch(ref.paramExpr, function (val) {
  8996. rawDef.uiStateParams = extend({}, val);
  8997. update();
  8998. }, true);
  8999. rawDef.uiStateParams = extend({}, scope.$eval(ref.paramExpr));
  9000. }
  9001. update();
  9002. scope.$on('$destroy', $uiRouter.stateRegistry.onStatesChanged(update));
  9003. scope.$on('$destroy', $uiRouter.transitionService.onSuccess({}, update));
  9004. if (!type.clickable)
  9005. return;
  9006. hookFn = clickHook(element, $state, $timeout, type, getDef);
  9007. bindEvents(element, scope, hookFn, rawDef.uiStateOpts);
  9008. }
  9009. };
  9010. }];
  9011. /**
  9012. * `ui-state`: A fully dynamic directive for linking to a state
  9013. *
  9014. * A directive which links to a state (and optionally, parameters).
  9015. * When clicked, this directive activates the linked state with the supplied parameter values.
  9016. *
  9017. * **This directive is very similar to [[uiSref]], but it `$observe`s and `$watch`es/evaluates all its inputs.**
  9018. *
  9019. * A directive which links to a state (and optionally, parameters).
  9020. * When clicked, this directive activates the linked state with the supplied parameter values.
  9021. *
  9022. * ### Linked State
  9023. * The attribute value of `ui-state` is an expression which is `$watch`ed and evaluated as the state to link to.
  9024. * **This is in contrast with `ui-sref`, which takes a state name as a string literal.**
  9025. *
  9026. * #### Example:
  9027. * Create a list of links.
  9028. * ```html
  9029. * <li ng-repeat="link in navlinks">
  9030. * <a ui-state="link.state">{{ link.displayName }}</a>
  9031. * </li>
  9032. * ```
  9033. *
  9034. * ### Relative Links
  9035. * If the expression evaluates to a relative path, it is processed like [[uiSref]].
  9036. * You just need to be aware that the path is relative to the state that *created* the link.
  9037. * This allows a state to create relative `ui-state` which always targets the same destination.
  9038. *
  9039. * ### hrefs
  9040. * If the linked state has a URL, the directive will automatically generate and
  9041. * update the `href` attribute (using the [[StateService.href]] method).
  9042. *
  9043. * ### Parameter Values
  9044. * In addition to the state name expression, a `ui-state` can include parameter values which are applied when activating the state.
  9045. * Param values should be provided using the `ui-state-params` attribute.
  9046. * The `ui-state-params` attribute value is `$watch`ed and evaluated as an expression.
  9047. *
  9048. * #### Example:
  9049. * This example renders a list of links with param values.
  9050. * The state's `userId` parameter value comes from each user's `user.id` property.
  9051. * ```html
  9052. * <li ng-repeat="link in navlinks">
  9053. * <a ui-state="link.state" ui-state-params="link.params">{{ link.displayName }}</a>
  9054. * </li>
  9055. * ```
  9056. *
  9057. * ### Transition Options
  9058. * You can specify [[TransitionOptions]] to pass to [[StateService.go]] by using the `ui-state-opts` attribute.
  9059. * Options are restricted to `location`, `inherit`, and `reload`.
  9060. * The value of the `ui-state-opts` is `$watch`ed and evaluated as an expression.
  9061. *
  9062. * #### Example:
  9063. * ```html
  9064. * <a ui-state="returnto.state" ui-state-opts="{ reload: true }">Home</a>
  9065. * ```
  9066. *
  9067. * ### Other DOM Events
  9068. *
  9069. * You can also customize which DOM events to respond to (instead of `click`) by
  9070. * providing an `events` array in the `ui-state-opts` attribute.
  9071. *
  9072. * #### Example:
  9073. * ```html
  9074. * <input type="text" ui-state="contacts" ui-state-opts="{ events: ['change', 'blur'] }">
  9075. * ```
  9076. *
  9077. * ### Highlighting the active link
  9078. * This directive can be used in conjunction with [[uiSrefActive]] to highlight the active link.
  9079. *
  9080. * ### Notes
  9081. *
  9082. * - You can use `ui-params` to change **only the parameter values** by omitting the state name and supplying only `ui-state-params`.
  9083. * However, it might be simpler to use [[uiSref]] parameter-only links.
  9084. *
  9085. * #### Example:
  9086. * Sets the `lang` parameter to `en` and remains on the same state.
  9087. *
  9088. * ```html
  9089. * <a ui-state="" ui-state-params="{ lang: 'en' }">English</a>
  9090. * ```
  9091. *
  9092. * - A middle-click, right-click, or ctrl-click is handled (natively) by the browser to open the href in a new window, for example.
  9093. * ```
  9094. */
  9095. var uiState;
  9096. uiState = ['$uiRouter', '$timeout',
  9097. function $StateRefDynamicDirective($uiRouter, $timeout) {
  9098. var $state = $uiRouter.stateService;
  9099. return {
  9100. restrict: 'A',
  9101. require: ['?^uiSrefActive', '?^uiSrefActiveEq'],
  9102. link: function (scope, element, attrs, uiSrefActive) {
  9103. var type = getTypeInfo(element);
  9104. var active = uiSrefActive[1] || uiSrefActive[0];
  9105. var unlinkInfoFn = null;
  9106. var hookFn;
  9107. var rawDef = {};
  9108. var getDef = function () { return processedDef($state, element, rawDef); };
  9109. var inputAttrs = ['uiState', 'uiStateParams', 'uiStateOpts'];
  9110. var watchDeregFns = inputAttrs.reduce(function (acc, attr) { return (acc[attr] = noop$1, acc); }, {});
  9111. function update() {
  9112. var def = getDef();
  9113. if (unlinkInfoFn)
  9114. unlinkInfoFn();
  9115. if (active)
  9116. unlinkInfoFn = active.$$addStateInfo(def.uiState, def.uiStateParams);
  9117. if (def.href != null)
  9118. attrs.$set(type.attr, def.href);
  9119. }
  9120. inputAttrs.forEach(function (field) {
  9121. rawDef[field] = attrs[field] ? scope.$eval(attrs[field]) : null;
  9122. attrs.$observe(field, function (expr) {
  9123. watchDeregFns[field]();
  9124. watchDeregFns[field] = scope.$watch(expr, function (newval) {
  9125. rawDef[field] = newval;
  9126. update();
  9127. }, true);
  9128. });
  9129. });
  9130. update();
  9131. scope.$on('$destroy', $uiRouter.stateRegistry.onStatesChanged(update));
  9132. scope.$on('$destroy', $uiRouter.transitionService.onSuccess({}, update));
  9133. if (!type.clickable)
  9134. return;
  9135. hookFn = clickHook(element, $state, $timeout, type, getDef);
  9136. bindEvents(element, scope, hookFn, rawDef.uiStateOpts);
  9137. }
  9138. };
  9139. }];
  9140. /**
  9141. * `ui-sref-active` and `ui-sref-active-eq`: A directive that adds a CSS class when a `ui-sref` is active
  9142. *
  9143. * A directive working alongside [[uiSref]] and [[uiState]] to add classes to an element when the
  9144. * related directive's state is active (and remove them when it is inactive).
  9145. *
  9146. * The primary use-case is to highlight the active link in navigation menus,
  9147. * distinguishing it from the inactive menu items.
  9148. *
  9149. * ### Linking to a `ui-sref` or `ui-state`
  9150. * `ui-sref-active` can live on the same element as `ui-sref`/`ui-state`, or it can be on a parent element.
  9151. * If a `ui-sref-active` is a parent to more than one `ui-sref`/`ui-state`, it will apply the CSS class when **any of the links are active**.
  9152. *
  9153. * ### Matching
  9154. *
  9155. * The `ui-sref-active` directive applies the CSS class when the `ui-sref`/`ui-state`'s target state **or any child state is active**.
  9156. * This is a "fuzzy match" which uses [[StateService.includes]].
  9157. *
  9158. * The `ui-sref-active-eq` directive applies the CSS class when the `ui-sref`/`ui-state`'s target state is directly active (not when child states are active).
  9159. * This is an "exact match" which uses [[StateService.is]].
  9160. *
  9161. * ### Parameter values
  9162. * If the `ui-sref`/`ui-state` includes parameter values, the current parameter values must match the link's values for the link to be highlighted.
  9163. * This allows a list of links to the same state with different parameters to be rendered, and the correct one highlighted.
  9164. *
  9165. * #### Example:
  9166. * ```html
  9167. * <li ng-repeat="user in users" ui-sref-active="active">
  9168. * <a ui-sref="user.details({ userId: user.id })">{{ user.lastName }}</a>
  9169. * </li>
  9170. * ```
  9171. *
  9172. * ### Examples
  9173. *
  9174. * Given the following template:
  9175. * #### Example:
  9176. * ```html
  9177. * <ul>
  9178. * <li ui-sref-active="active" class="item">
  9179. * <a href ui-sref="app.user({user: 'bilbobaggins'})">@bilbobaggins</a>
  9180. * </li>
  9181. * </ul>
  9182. * ```
  9183. *
  9184. * When the app state is `app.user` (or any child state),
  9185. * and contains the state parameter "user" with value "bilbobaggins",
  9186. * the resulting HTML will appear as (note the 'active' class):
  9187. *
  9188. * ```html
  9189. * <ul>
  9190. * <li ui-sref-active="active" class="item active">
  9191. * <a ui-sref="app.user({user: 'bilbobaggins'})" href="/users/bilbobaggins">@bilbobaggins</a>
  9192. * </li>
  9193. * </ul>
  9194. * ```
  9195. *
  9196. * ### Glob mode
  9197. *
  9198. * It is possible to pass `ui-sref-active` an expression that evaluates to an object.
  9199. * The objects keys represent active class names and values represent the respective state names/globs.
  9200. * `ui-sref-active` will match if the current active state **includes** any of
  9201. * the specified state names/globs, even the abstract ones.
  9202. *
  9203. * #### Example:
  9204. * Given the following template, with "admin" being an abstract state:
  9205. * ```html
  9206. * <div ui-sref-active="{'active': 'admin.**'}">
  9207. * <a ui-sref-active="active" ui-sref="admin.roles">Roles</a>
  9208. * </div>
  9209. * ```
  9210. *
  9211. * When the current state is "admin.roles" the "active" class will be applied to both the <div> and <a> elements.
  9212. * It is important to note that the state names/globs passed to `ui-sref-active` override any state provided by a linked `ui-sref`.
  9213. *
  9214. * ### Notes:
  9215. *
  9216. * - The class name is interpolated **once** during the directives link time (any further changes to the
  9217. * interpolated value are ignored).
  9218. *
  9219. * - Multiple classes may be specified in a space-separated format: `ui-sref-active='class1 class2 class3'`
  9220. */
  9221. var uiSrefActive;
  9222. uiSrefActive = ['$state', '$stateParams', '$interpolate', '$uiRouter',
  9223. function $StateRefActiveDirective($state, $stateParams, $interpolate, $uiRouter) {
  9224. return {
  9225. restrict: "A",
  9226. controller: ['$scope', '$element', '$attrs',
  9227. function ($scope, $element, $attrs) {
  9228. var states = [], activeEqClass, uiSrefActive;
  9229. // There probably isn't much point in $observing this
  9230. // uiSrefActive and uiSrefActiveEq share the same directive object with some
  9231. // slight difference in logic routing
  9232. activeEqClass = $interpolate($attrs.uiSrefActiveEq || '', false)($scope);
  9233. try {
  9234. uiSrefActive = $scope.$eval($attrs.uiSrefActive);
  9235. }
  9236. catch (e) {
  9237. // Do nothing. uiSrefActive is not a valid expression.
  9238. // Fall back to using $interpolate below
  9239. }
  9240. uiSrefActive = uiSrefActive || $interpolate($attrs.uiSrefActive || '', false)($scope);
  9241. if (isObject(uiSrefActive)) {
  9242. forEach(uiSrefActive, function (stateOrName, activeClass) {
  9243. if (isString(stateOrName)) {
  9244. var ref = parseStateRef(stateOrName);
  9245. addState(ref.state, $scope.$eval(ref.paramExpr), activeClass);
  9246. }
  9247. });
  9248. }
  9249. // Allow uiSref to communicate with uiSrefActive[Equals]
  9250. this.$$addStateInfo = function (newState, newParams) {
  9251. // we already got an explicit state provided by ui-sref-active, so we
  9252. // shadow the one that comes from ui-sref
  9253. if (isObject(uiSrefActive) && states.length > 0) {
  9254. return;
  9255. }
  9256. var deregister = addState(newState, newParams, uiSrefActive);
  9257. update();
  9258. return deregister;
  9259. };
  9260. function updateAfterTransition(trans) {
  9261. trans.promise.then(update, noop$1);
  9262. }
  9263. $scope.$on('$stateChangeSuccess', update);
  9264. $scope.$on('$destroy', $uiRouter.transitionService.onStart({}, updateAfterTransition));
  9265. if ($uiRouter.globals.transition) {
  9266. updateAfterTransition($uiRouter.globals.transition);
  9267. }
  9268. function addState(stateName, stateParams, activeClass) {
  9269. var state = $state.get(stateName, stateContext($element));
  9270. var stateInfo = {
  9271. state: state || { name: stateName },
  9272. params: stateParams,
  9273. activeClass: activeClass
  9274. };
  9275. states.push(stateInfo);
  9276. return function removeState() {
  9277. removeFrom(states)(stateInfo);
  9278. };
  9279. }
  9280. // Update route state
  9281. function update() {
  9282. var splitClasses = function (str) {
  9283. return str.split(/\s/).filter(identity);
  9284. };
  9285. var getClasses = function (stateList) {
  9286. return stateList.map(function (x) { return x.activeClass; }).map(splitClasses).reduce(unnestR, []);
  9287. };
  9288. var allClasses = getClasses(states).concat(splitClasses(activeEqClass)).reduce(uniqR, []);
  9289. var fuzzyClasses = getClasses(states.filter(function (x) { return $state.includes(x.state.name, x.params); }));
  9290. var exactlyMatchesAny = !!states.filter(function (x) { return $state.is(x.state.name, x.params); }).length;
  9291. var exactClasses = exactlyMatchesAny ? splitClasses(activeEqClass) : [];
  9292. var addClasses = fuzzyClasses.concat(exactClasses).reduce(uniqR, []);
  9293. var removeClasses = allClasses.filter(function (cls) { return !inArray(addClasses, cls); });
  9294. $scope.$evalAsync(function () {
  9295. addClasses.forEach(function (className) { return $element.addClass(className); });
  9296. removeClasses.forEach(function (className) { return $element.removeClass(className); });
  9297. });
  9298. }
  9299. update();
  9300. }]
  9301. };
  9302. }];
  9303. ng.module('ui.router.state')
  9304. .directive('uiSref', uiSref)
  9305. .directive('uiSrefActive', uiSrefActive)
  9306. .directive('uiSrefActiveEq', uiSrefActive)
  9307. .directive('uiState', uiState);
  9308. /** @module ng1 */ /** for typedoc */
  9309. /**
  9310. * `isState` Filter: truthy if the current state is the parameter
  9311. *
  9312. * Translates to [[StateService.is]] `$state.is("stateName")`.
  9313. *
  9314. * #### Example:
  9315. * ```html
  9316. * <div ng-if="'stateName' | isState">show if state is 'stateName'</div>
  9317. * ```
  9318. */
  9319. $IsStateFilter.$inject = ['$state'];
  9320. function $IsStateFilter($state) {
  9321. var isFilter = function (state, params, options) {
  9322. return $state.is(state, params, options);
  9323. };
  9324. isFilter.$stateful = true;
  9325. return isFilter;
  9326. }
  9327. /**
  9328. * `includedByState` Filter: truthy if the current state includes the parameter
  9329. *
  9330. * Translates to [[StateService.includes]]` $state.is("fullOrPartialStateName")`.
  9331. *
  9332. * #### Example:
  9333. * ```html
  9334. * <div ng-if="'fullOrPartialStateName' | includedByState">show if state includes 'fullOrPartialStateName'</div>
  9335. * ```
  9336. */
  9337. $IncludedByStateFilter.$inject = ['$state'];
  9338. function $IncludedByStateFilter($state) {
  9339. var includesFilter = function (state, params, options) {
  9340. return $state.includes(state, params, options);
  9341. };
  9342. includesFilter.$stateful = true;
  9343. return includesFilter;
  9344. }
  9345. ng.module('ui.router.state')
  9346. .filter('isState', $IsStateFilter)
  9347. .filter('includedByState', $IncludedByStateFilter);
  9348. /**
  9349. * @ng1api
  9350. * @module directives
  9351. */ /** for typedoc */
  9352. /**
  9353. * `ui-view`: A viewport directive which is filled in by a view from the active state.
  9354. *
  9355. * ### Attributes
  9356. *
  9357. * - `name`: (Optional) A view name.
  9358. * The name should be unique amongst the other views in the same state.
  9359. * You can have views of the same name that live in different states.
  9360. * The ui-view can be targeted in a View using the name ([[Ng1StateDeclaration.views]]).
  9361. *
  9362. * - `autoscroll`: an expression. When it evaluates to true, the `ui-view` will be scrolled into view when it is activated.
  9363. * Uses [[$uiViewScroll]] to do the scrolling.
  9364. *
  9365. * - `onload`: Expression to evaluate whenever the view updates.
  9366. *
  9367. * #### Example:
  9368. * A view can be unnamed or named.
  9369. * ```html
  9370. * <!-- Unnamed -->
  9371. * <div ui-view></div>
  9372. *
  9373. * <!-- Named -->
  9374. * <div ui-view="viewName"></div>
  9375. *
  9376. * <!-- Named (different style) -->
  9377. * <ui-view name="viewName"></ui-view>
  9378. * ```
  9379. *
  9380. * You can only have one unnamed view within any template (or root html). If you are only using a
  9381. * single view and it is unnamed then you can populate it like so:
  9382. *
  9383. * ```html
  9384. * <div ui-view></div>
  9385. * $stateProvider.state("home", {
  9386. * template: "<h1>HELLO!</h1>"
  9387. * })
  9388. * ```
  9389. *
  9390. * The above is a convenient shortcut equivalent to specifying your view explicitly with the
  9391. * [[Ng1StateDeclaration.views]] config property, by name, in this case an empty name:
  9392. *
  9393. * ```js
  9394. * $stateProvider.state("home", {
  9395. * views: {
  9396. * "": {
  9397. * template: "<h1>HELLO!</h1>"
  9398. * }
  9399. * }
  9400. * })
  9401. * ```
  9402. *
  9403. * But typically you'll only use the views property if you name your view or have more than one view
  9404. * in the same template. There's not really a compelling reason to name a view if its the only one,
  9405. * but you could if you wanted, like so:
  9406. *
  9407. * ```html
  9408. * <div ui-view="main"></div>
  9409. * ```
  9410. *
  9411. * ```js
  9412. * $stateProvider.state("home", {
  9413. * views: {
  9414. * "main": {
  9415. * template: "<h1>HELLO!</h1>"
  9416. * }
  9417. * }
  9418. * })
  9419. * ```
  9420. *
  9421. * Really though, you'll use views to set up multiple views:
  9422. *
  9423. * ```html
  9424. * <div ui-view></div>
  9425. * <div ui-view="chart"></div>
  9426. * <div ui-view="data"></div>
  9427. * ```
  9428. *
  9429. * ```js
  9430. * $stateProvider.state("home", {
  9431. * views: {
  9432. * "": {
  9433. * template: "<h1>HELLO!</h1>"
  9434. * },
  9435. * "chart": {
  9436. * template: "<chart_thing/>"
  9437. * },
  9438. * "data": {
  9439. * template: "<data_thing/>"
  9440. * }
  9441. * }
  9442. * })
  9443. * ```
  9444. *
  9445. * #### Examples for `autoscroll`:
  9446. * ```html
  9447. * <!-- If autoscroll present with no expression,
  9448. * then scroll ui-view into view -->
  9449. * <ui-view autoscroll/>
  9450. *
  9451. * <!-- If autoscroll present with valid expression,
  9452. * then scroll ui-view into view if expression evaluates to true -->
  9453. * <ui-view autoscroll='true'/>
  9454. * <ui-view autoscroll='false'/>
  9455. * <ui-view autoscroll='scopeVariable'/>
  9456. * ```
  9457. *
  9458. * Resolve data:
  9459. *
  9460. * The resolved data from the state's `resolve` block is placed on the scope as `$resolve` (this
  9461. * can be customized using [[Ng1ViewDeclaration.resolveAs]]). This can be then accessed from the template.
  9462. *
  9463. * Note that when `controllerAs` is being used, `$resolve` is set on the controller instance *after* the
  9464. * controller is instantiated. The `$onInit()` hook can be used to perform initialization code which
  9465. * depends on `$resolve` data.
  9466. *
  9467. * #### Example:
  9468. * ```js
  9469. * $stateProvider.state('home', {
  9470. * template: '<my-component user="$resolve.user"></my-component>',
  9471. * resolve: {
  9472. * user: function(UserService) { return UserService.fetchUser(); }
  9473. * }
  9474. * });
  9475. * ```
  9476. */
  9477. var uiView;
  9478. uiView = ['$view', '$animate', '$uiViewScroll', '$interpolate', '$q',
  9479. function $ViewDirective($view, $animate, $uiViewScroll, $interpolate, $q) {
  9480. function getRenderer(attrs, scope) {
  9481. return {
  9482. enter: function (element, target, cb) {
  9483. if (ng.version.minor > 2) {
  9484. $animate.enter(element, null, target).then(cb);
  9485. }
  9486. else {
  9487. $animate.enter(element, null, target, cb);
  9488. }
  9489. },
  9490. leave: function (element, cb) {
  9491. if (ng.version.minor > 2) {
  9492. $animate.leave(element).then(cb);
  9493. }
  9494. else {
  9495. $animate.leave(element, cb);
  9496. }
  9497. }
  9498. };
  9499. }
  9500. function configsEqual(config1, config2) {
  9501. return config1 === config2;
  9502. }
  9503. var rootData = {
  9504. $cfg: { viewDecl: { $context: $view._pluginapi._rootViewContext() } },
  9505. $uiView: {}
  9506. };
  9507. var directive = {
  9508. count: 0,
  9509. restrict: 'ECA',
  9510. terminal: true,
  9511. priority: 400,
  9512. transclude: 'element',
  9513. compile: function (tElement, tAttrs, $transclude) {
  9514. return function (scope, $element, attrs) {
  9515. var previousEl, currentEl, currentScope, unregister, onloadExp = attrs['onload'] || '', autoScrollExp = attrs['autoscroll'], renderer = getRenderer(attrs, scope), viewConfig = undefined, inherited = $element.inheritedData('$uiView') || rootData, name = $interpolate(attrs['uiView'] || attrs['name'] || '')(scope) || '$default';
  9516. var activeUIView = {
  9517. $type: 'ng1',
  9518. id: directive.count++,
  9519. name: name,
  9520. fqn: inherited.$uiView.fqn ? inherited.$uiView.fqn + "." + name : name,
  9521. config: null,
  9522. configUpdated: configUpdatedCallback,
  9523. get creationContext() {
  9524. var fromParentTagConfig = parse('$cfg.viewDecl.$context')(inherited);
  9525. // Allow <ui-view name="foo"><ui-view name="bar"></ui-view></ui-view>
  9526. // See https://github.com/angular-ui/ui-router/issues/3355
  9527. var fromParentTag = parse('$uiView.creationContext')(inherited);
  9528. return fromParentTagConfig || fromParentTag;
  9529. }
  9530. };
  9531. trace.traceUIViewEvent("Linking", activeUIView);
  9532. function configUpdatedCallback(config) {
  9533. if (config && !(config instanceof Ng1ViewConfig))
  9534. return;
  9535. if (configsEqual(viewConfig, config))
  9536. return;
  9537. trace.traceUIViewConfigUpdated(activeUIView, config && config.viewDecl && config.viewDecl.$context);
  9538. viewConfig = config;
  9539. updateView(config);
  9540. }
  9541. $element.data('$uiView', { $uiView: activeUIView });
  9542. updateView();
  9543. unregister = $view.registerUIView(activeUIView);
  9544. scope.$on("$destroy", function () {
  9545. trace.traceUIViewEvent("Destroying/Unregistering", activeUIView);
  9546. unregister();
  9547. });
  9548. function cleanupLastView() {
  9549. if (previousEl) {
  9550. trace.traceUIViewEvent("Removing (previous) el", previousEl.data('$uiView'));
  9551. previousEl.remove();
  9552. previousEl = null;
  9553. }
  9554. if (currentScope) {
  9555. trace.traceUIViewEvent("Destroying scope", activeUIView);
  9556. currentScope.$destroy();
  9557. currentScope = null;
  9558. }
  9559. if (currentEl) {
  9560. var _viewData_1 = currentEl.data('$uiViewAnim');
  9561. trace.traceUIViewEvent("Animate out", _viewData_1);
  9562. renderer.leave(currentEl, function () {
  9563. _viewData_1.$$animLeave.resolve();
  9564. previousEl = null;
  9565. });
  9566. previousEl = currentEl;
  9567. currentEl = null;
  9568. }
  9569. }
  9570. function updateView(config) {
  9571. var newScope = scope.$new();
  9572. var animEnter = $q.defer(), animLeave = $q.defer();
  9573. var $uiViewData = {
  9574. $cfg: config,
  9575. $uiView: activeUIView,
  9576. };
  9577. var $uiViewAnim = {
  9578. $animEnter: animEnter.promise,
  9579. $animLeave: animLeave.promise,
  9580. $$animLeave: animLeave
  9581. };
  9582. /**
  9583. * @ngdoc event
  9584. * @name ui.router.state.directive:ui-view#$viewContentLoading
  9585. * @eventOf ui.router.state.directive:ui-view
  9586. * @eventType emits on ui-view directive scope
  9587. * @description
  9588. *
  9589. * Fired once the view **begins loading**, *before* the DOM is rendered.
  9590. *
  9591. * @param {Object} event Event object.
  9592. * @param {string} viewName Name of the view.
  9593. */
  9594. newScope.$emit('$viewContentLoading', name);
  9595. var cloned = $transclude(newScope, function (clone) {
  9596. clone.data('$uiViewAnim', $uiViewAnim);
  9597. clone.data('$uiView', $uiViewData);
  9598. renderer.enter(clone, $element, function onUIViewEnter() {
  9599. animEnter.resolve();
  9600. if (currentScope)
  9601. currentScope.$emit('$viewContentAnimationEnded');
  9602. if (isDefined(autoScrollExp) && !autoScrollExp || scope.$eval(autoScrollExp)) {
  9603. $uiViewScroll(clone);
  9604. }
  9605. });
  9606. cleanupLastView();
  9607. });
  9608. currentEl = cloned;
  9609. currentScope = newScope;
  9610. /**
  9611. * @ngdoc event
  9612. * @name ui.router.state.directive:ui-view#$viewContentLoaded
  9613. * @eventOf ui.router.state.directive:ui-view
  9614. * @eventType emits on ui-view directive scope
  9615. * @description *
  9616. * Fired once the view is **loaded**, *after* the DOM is rendered.
  9617. *
  9618. * @param {Object} event Event object.
  9619. */
  9620. currentScope.$emit('$viewContentLoaded', config || viewConfig);
  9621. currentScope.$eval(onloadExp);
  9622. }
  9623. };
  9624. }
  9625. };
  9626. return directive;
  9627. }];
  9628. $ViewDirectiveFill.$inject = ['$compile', '$controller', '$transitions', '$view', '$q', '$timeout'];
  9629. /** @hidden */
  9630. function $ViewDirectiveFill($compile, $controller, $transitions, $view, $q, $timeout) {
  9631. var getControllerAs = parse('viewDecl.controllerAs');
  9632. var getResolveAs = parse('viewDecl.resolveAs');
  9633. return {
  9634. restrict: 'ECA',
  9635. priority: -400,
  9636. compile: function (tElement) {
  9637. var initial = tElement.html();
  9638. tElement.empty();
  9639. return function (scope, $element) {
  9640. var data = $element.data('$uiView');
  9641. if (!data) {
  9642. $element.html(initial);
  9643. $compile($element.contents())(scope);
  9644. return;
  9645. }
  9646. var cfg = data.$cfg || { viewDecl: {}, getTemplate: ng_from_import.noop };
  9647. var resolveCtx = cfg.path && new ResolveContext(cfg.path);
  9648. $element.html(cfg.getTemplate($element, resolveCtx) || initial);
  9649. trace.traceUIViewFill(data.$uiView, $element.html());
  9650. var link = $compile($element.contents());
  9651. var controller = cfg.controller;
  9652. var controllerAs = getControllerAs(cfg);
  9653. var resolveAs = getResolveAs(cfg);
  9654. var locals = resolveCtx && getLocals(resolveCtx);
  9655. scope[resolveAs] = locals;
  9656. if (controller) {
  9657. var controllerInstance = $controller(controller, extend({}, locals, { $scope: scope, $element: $element }));
  9658. if (controllerAs) {
  9659. scope[controllerAs] = controllerInstance;
  9660. scope[controllerAs][resolveAs] = locals;
  9661. }
  9662. // TODO: Use $view service as a central point for registering component-level hooks
  9663. // Then, when a component is created, tell the $view service, so it can invoke hooks
  9664. // $view.componentLoaded(controllerInstance, { $scope: scope, $element: $element });
  9665. // scope.$on('$destroy', () => $view.componentUnloaded(controllerInstance, { $scope: scope, $element: $element }));
  9666. $element.data('$ngControllerController', controllerInstance);
  9667. $element.children().data('$ngControllerController', controllerInstance);
  9668. registerControllerCallbacks($q, $transitions, controllerInstance, scope, cfg);
  9669. }
  9670. // Wait for the component to appear in the DOM
  9671. if (isString(cfg.viewDecl.component)) {
  9672. var cmp_1 = cfg.viewDecl.component;
  9673. var kebobName = kebobString(cmp_1);
  9674. var tagRegexp_1 = new RegExp("^(x-|data-)?" + kebobName + "$", "i");
  9675. var getComponentController = function () {
  9676. var directiveEl = [].slice.call($element[0].children)
  9677. .filter(function (el) { return el && el.tagName && tagRegexp_1.exec(el.tagName); });
  9678. return directiveEl && ng.element(directiveEl).data("$" + cmp_1 + "Controller");
  9679. };
  9680. var deregisterWatch_1 = scope.$watch(getComponentController, function (ctrlInstance) {
  9681. if (!ctrlInstance)
  9682. return;
  9683. registerControllerCallbacks($q, $transitions, ctrlInstance, scope, cfg);
  9684. deregisterWatch_1();
  9685. });
  9686. }
  9687. link(scope);
  9688. };
  9689. }
  9690. };
  9691. }
  9692. /** @hidden */
  9693. var hasComponentImpl = typeof ng.module('ui.router')['component'] === 'function';
  9694. /** @hidden incrementing id */
  9695. var _uiCanExitId = 0;
  9696. /** @hidden TODO: move these callbacks to $view and/or `/hooks/components.ts` or something */
  9697. function registerControllerCallbacks($q, $transitions, controllerInstance, $scope, cfg) {
  9698. // Call $onInit() ASAP
  9699. if (isFunction(controllerInstance.$onInit) && !(cfg.viewDecl.component && hasComponentImpl)) {
  9700. controllerInstance.$onInit();
  9701. }
  9702. var viewState = tail(cfg.path).state.self;
  9703. var hookOptions = { bind: controllerInstance };
  9704. // Add component-level hook for onParamsChange
  9705. if (isFunction(controllerInstance.uiOnParamsChanged)) {
  9706. var resolveContext = new ResolveContext(cfg.path);
  9707. var viewCreationTrans_1 = resolveContext.getResolvable('$transition$').data;
  9708. // Fire callback on any successful transition
  9709. var paramsUpdated = function ($transition$) {
  9710. // Exit early if the $transition$ is the same as the view was created within.
  9711. // Exit early if the $transition$ will exit the state the view is for.
  9712. if ($transition$ === viewCreationTrans_1 || $transition$.exiting().indexOf(viewState) !== -1)
  9713. return;
  9714. var toParams = $transition$.params("to");
  9715. var fromParams = $transition$.params("from");
  9716. var toSchema = $transition$.treeChanges().to.map(function (node) { return node.paramSchema; }).reduce(unnestR, []);
  9717. var fromSchema = $transition$.treeChanges().from.map(function (node) { return node.paramSchema; }).reduce(unnestR, []);
  9718. // Find the to params that have different values than the from params
  9719. var changedToParams = toSchema.filter(function (param) {
  9720. var idx = fromSchema.indexOf(param);
  9721. return idx === -1 || !fromSchema[idx].type.equals(toParams[param.id], fromParams[param.id]);
  9722. });
  9723. // Only trigger callback if a to param has changed or is new
  9724. if (changedToParams.length) {
  9725. var changedKeys_1 = changedToParams.map(function (x) { return x.id; });
  9726. // Filter the params to only changed/new to params. `$transition$.params()` may be used to get all params.
  9727. var newValues = filter(toParams, function (val, key) { return changedKeys_1.indexOf(key) !== -1; });
  9728. controllerInstance.uiOnParamsChanged(newValues, $transition$);
  9729. }
  9730. };
  9731. $scope.$on('$destroy', $transitions.onSuccess({}, paramsUpdated, hookOptions));
  9732. }
  9733. // Add component-level hook for uiCanExit
  9734. if (isFunction(controllerInstance.uiCanExit)) {
  9735. var id_1 = _uiCanExitId++;
  9736. var cacheProp_1 = '_uiCanExitIds';
  9737. // Returns true if a redirect transition already answered truthy
  9738. var prevTruthyAnswer_1 = function (trans) {
  9739. return !!trans && (trans[cacheProp_1] && trans[cacheProp_1][id_1] === true || prevTruthyAnswer_1(trans.redirectedFrom()));
  9740. };
  9741. // If a user answered yes, but the transition was later redirected, don't also ask for the new redirect transition
  9742. var wrappedHook = function (trans) {
  9743. var promise, ids = trans[cacheProp_1] = trans[cacheProp_1] || {};
  9744. if (!prevTruthyAnswer_1(trans)) {
  9745. promise = $q.when(controllerInstance.uiCanExit(trans));
  9746. promise.then(function (val) { return ids[id_1] = (val !== false); });
  9747. }
  9748. return promise;
  9749. };
  9750. var criteria = { exiting: viewState.name };
  9751. $scope.$on('$destroy', $transitions.onBefore(criteria, wrappedHook, hookOptions));
  9752. }
  9753. }
  9754. ng.module('ui.router.state').directive('uiView', uiView);
  9755. ng.module('ui.router.state').directive('uiView', $ViewDirectiveFill);
  9756. /** @module ng1 */ /** */
  9757. /** @hidden */
  9758. function $ViewScrollProvider() {
  9759. var useAnchorScroll = false;
  9760. this.useAnchorScroll = function () {
  9761. useAnchorScroll = true;
  9762. };
  9763. this.$get = ['$anchorScroll', '$timeout', function ($anchorScroll, $timeout) {
  9764. if (useAnchorScroll) {
  9765. return $anchorScroll;
  9766. }
  9767. return function ($element) {
  9768. return $timeout(function () {
  9769. $element[0].scrollIntoView();
  9770. }, 0, false);
  9771. };
  9772. }];
  9773. }
  9774. ng.module('ui.router.state').provider('$uiViewScroll', $ViewScrollProvider);
  9775. /**
  9776. * Main entry point for angular 1.x build
  9777. * @module ng1
  9778. */ /** */
  9779. var index = "ui.router";
  9780. exports['default'] = index;
  9781. exports.core = index$1;
  9782. exports.watchDigests = watchDigests;
  9783. exports.getLocals = getLocals;
  9784. exports.getNg1ViewConfigFactory = getNg1ViewConfigFactory;
  9785. exports.ng1ViewsBuilder = ng1ViewsBuilder;
  9786. exports.Ng1ViewConfig = Ng1ViewConfig;
  9787. exports.StateProvider = StateProvider;
  9788. exports.UrlRouterProvider = UrlRouterProvider;
  9789. exports.root = root;
  9790. exports.fromJson = fromJson;
  9791. exports.toJson = toJson;
  9792. exports.forEach = forEach;
  9793. exports.extend = extend;
  9794. exports.equals = equals;
  9795. exports.identity = identity;
  9796. exports.noop = noop$1;
  9797. exports.createProxyFunctions = createProxyFunctions;
  9798. exports.inherit = inherit;
  9799. exports.inArray = inArray;
  9800. exports._inArray = _inArray;
  9801. exports.removeFrom = removeFrom;
  9802. exports._removeFrom = _removeFrom;
  9803. exports.pushTo = pushTo;
  9804. exports._pushTo = _pushTo;
  9805. exports.deregAll = deregAll;
  9806. exports.defaults = defaults;
  9807. exports.mergeR = mergeR;
  9808. exports.ancestors = ancestors;
  9809. exports.pick = pick;
  9810. exports.omit = omit;
  9811. exports.pluck = pluck;
  9812. exports.filter = filter;
  9813. exports.find = find;
  9814. exports.mapObj = mapObj;
  9815. exports.map = map;
  9816. exports.values = values;
  9817. exports.allTrueR = allTrueR;
  9818. exports.anyTrueR = anyTrueR;
  9819. exports.unnestR = unnestR;
  9820. exports.flattenR = flattenR;
  9821. exports.pushR = pushR;
  9822. exports.uniqR = uniqR;
  9823. exports.unnest = unnest;
  9824. exports.flatten = flatten;
  9825. exports.assertPredicate = assertPredicate;
  9826. exports.assertMap = assertMap;
  9827. exports.assertFn = assertFn;
  9828. exports.pairs = pairs;
  9829. exports.arrayTuples = arrayTuples;
  9830. exports.applyPairs = applyPairs;
  9831. exports.tail = tail;
  9832. exports.copy = copy;
  9833. exports._extend = _extend;
  9834. exports.silenceUncaughtInPromise = silenceUncaughtInPromise;
  9835. exports.silentRejection = silentRejection;
  9836. exports.notImplemented = notImplemented;
  9837. exports.services = services;
  9838. exports.Glob = Glob;
  9839. exports.curry = curry;
  9840. exports.compose = compose;
  9841. exports.pipe = pipe;
  9842. exports.prop = prop;
  9843. exports.propEq = propEq;
  9844. exports.parse = parse;
  9845. exports.not = not;
  9846. exports.and = and;
  9847. exports.or = or;
  9848. exports.all = all;
  9849. exports.any = any;
  9850. exports.is = is;
  9851. exports.eq = eq;
  9852. exports.val = val;
  9853. exports.invoke = invoke;
  9854. exports.pattern = pattern;
  9855. exports.isUndefined = isUndefined;
  9856. exports.isDefined = isDefined;
  9857. exports.isNull = isNull;
  9858. exports.isNullOrUndefined = isNullOrUndefined;
  9859. exports.isFunction = isFunction;
  9860. exports.isNumber = isNumber;
  9861. exports.isString = isString;
  9862. exports.isObject = isObject;
  9863. exports.isArray = isArray;
  9864. exports.isDate = isDate;
  9865. exports.isRegExp = isRegExp;
  9866. exports.isState = isState;
  9867. exports.isInjectable = isInjectable;
  9868. exports.isPromise = isPromise;
  9869. exports.Queue = Queue;
  9870. exports.maxLength = maxLength;
  9871. exports.padString = padString;
  9872. exports.kebobString = kebobString;
  9873. exports.functionToString = functionToString;
  9874. exports.fnToString = fnToString;
  9875. exports.stringify = stringify;
  9876. exports.beforeAfterSubstr = beforeAfterSubstr;
  9877. exports.hostRegex = hostRegex;
  9878. exports.stripFile = stripFile;
  9879. exports.splitHash = splitHash;
  9880. exports.splitQuery = splitQuery;
  9881. exports.splitEqual = splitEqual;
  9882. exports.trimHashVal = trimHashVal;
  9883. exports.splitOnDelim = splitOnDelim;
  9884. exports.joinNeighborsR = joinNeighborsR;
  9885. exports.Trace = Trace;
  9886. exports.trace = trace;
  9887. exports.Param = Param;
  9888. exports.ParamTypes = ParamTypes;
  9889. exports.StateParams = StateParams;
  9890. exports.ParamType = ParamType;
  9891. exports.PathNode = PathNode;
  9892. exports.PathUtils = PathUtils;
  9893. exports.resolvePolicies = resolvePolicies;
  9894. exports.defaultResolvePolicy = defaultResolvePolicy;
  9895. exports.Resolvable = Resolvable;
  9896. exports.NATIVE_INJECTOR_TOKEN = NATIVE_INJECTOR_TOKEN;
  9897. exports.ResolveContext = ResolveContext;
  9898. exports.resolvablesBuilder = resolvablesBuilder;
  9899. exports.StateBuilder = StateBuilder;
  9900. exports.StateObject = StateObject;
  9901. exports.StateMatcher = StateMatcher;
  9902. exports.StateQueueManager = StateQueueManager;
  9903. exports.StateRegistry = StateRegistry;
  9904. exports.StateService = StateService;
  9905. exports.TargetState = TargetState;
  9906. exports.HookBuilder = HookBuilder;
  9907. exports.matchState = matchState;
  9908. exports.RegisteredHook = RegisteredHook;
  9909. exports.makeEvent = makeEvent;
  9910. exports.Rejection = Rejection;
  9911. exports.Transition = Transition;
  9912. exports.TransitionHook = TransitionHook;
  9913. exports.TransitionEventType = TransitionEventType;
  9914. exports.defaultTransOpts = defaultTransOpts;
  9915. exports.TransitionService = TransitionService;
  9916. exports.UrlMatcher = UrlMatcher;
  9917. exports.UrlMatcherFactory = UrlMatcherFactory;
  9918. exports.UrlRouter = UrlRouter;
  9919. exports.UrlRuleFactory = UrlRuleFactory;
  9920. exports.BaseUrlRule = BaseUrlRule;
  9921. exports.UrlService = UrlService;
  9922. exports.ViewService = ViewService;
  9923. exports.UIRouterGlobals = UIRouterGlobals;
  9924. exports.UIRouter = UIRouter;
  9925. exports.$q = $q;
  9926. exports.$injector = $injector;
  9927. exports.BaseLocationServices = BaseLocationServices;
  9928. exports.HashLocationService = HashLocationService;
  9929. exports.MemoryLocationService = MemoryLocationService;
  9930. exports.PushStateLocationService = PushStateLocationService;
  9931. exports.MemoryLocationConfig = MemoryLocationConfig;
  9932. exports.BrowserLocationConfig = BrowserLocationConfig;
  9933. exports.keyValsToObjectR = keyValsToObjectR;
  9934. exports.getParams = getParams;
  9935. exports.parseUrl = parseUrl$1;
  9936. exports.buildUrl = buildUrl;
  9937. exports.locationPluginFactory = locationPluginFactory;
  9938. exports.servicesPlugin = servicesPlugin;
  9939. exports.hashLocationPlugin = hashLocationPlugin;
  9940. exports.pushStateLocationPlugin = pushStateLocationPlugin;
  9941. exports.memoryLocationPlugin = memoryLocationPlugin;
  9942. exports.UIRouterPluginBase = UIRouterPluginBase;
  9943. Object.defineProperty(exports, '__esModule', { value: true });
  9944. })));
  9945. //# sourceMappingURL=angular-ui-router.js.map