@@ -19,7 +19,7 @@ export class MenuEffects {
1919 /**
2020 * On route change, build menu sections for every menu type depending on the current route data
2121 */
22- @Effect ( { dispatch : false } )
22+ @Effect ( { dispatch : false } )
2323 public buildRouteMenuSections$ : Observable < Action > = this . actions$
2424 . pipe (
2525 ofType ( ROUTER_NAVIGATED ) ,
@@ -73,20 +73,8 @@ export class MenuEffects {
7373
7474 if ( hasValue ( data ) && hasValue ( data . menu ) && hasValue ( data . menu [ menuID ] ) ) {
7575
76- const menuSections = data . menu [ menuID ] ;
77- [ ...menuSections ]
78- . forEach ( ( menuSection ) => {
79-
80- if ( hasValue ( menuSection . model ) && hasValue ( menuSection . model . link ) ) {
81- let substitute : RegExpMatchArray ;
82- do {
83- substitute = menuSection . model . link . match ( / \/ : ( .* ?) \/ / ) ;
84- if ( substitute ) {
85- menuSection . model . link = menuSection . model . link . replace ( substitute [ 0 ] , `/${ params [ substitute [ 1 ] ] } /` ) ;
86- }
87- } while ( substitute ) ;
88- }
89- } ) ;
76+ let menuSections : MenuSection [ ] | MenuSection = data . menu [ menuID ] ;
77+ menuSections = this . resolveSubstitutions ( menuSections , params ) ;
9078
9179 if ( ! last ) {
9280 return [ ...menuSections , ...this . resolveRouteMenuSections ( route . firstChild , menuID ) ]
@@ -98,4 +86,30 @@ export class MenuEffects {
9886 return ! last ? this . resolveRouteMenuSections ( route . firstChild , menuID ) : [ ] ;
9987 }
10088
89+ private resolveSubstitutions ( object , params ) {
90+
91+ if ( typeof object === 'string' ) {
92+ let match : RegExpMatchArray ;
93+ do {
94+ match = object . match ( / : ( \w + ) / ) ;
95+ if ( match ) {
96+ const substitute = params [ match [ 1 ] ] ;
97+ if ( hasValue ( substitute ) ) {
98+ object = object . replace ( match [ 0 ] , `${ substitute } ` ) ;
99+ }
100+ }
101+ } while ( match ) ;
102+ } else if ( Array . isArray ( object ) ) {
103+ object . forEach ( ( entry , index ) => {
104+ object [ index ] = this . resolveSubstitutions ( object [ index ] , params ) ;
105+ } ) ;
106+ } else {
107+ Object . keys ( object ) . forEach ( ( key ) => {
108+ object = Object . assign ( { } , object , {
109+ [ key ] : this . resolveSubstitutions ( object [ key ] , params )
110+ } ) ;
111+ } ) ;
112+ }
113+ return object ;
114+ }
101115}
0 commit comments