11import { Component , ViewEncapsulation } from '@angular/core' ;
22import { Router , NavigationStart } from '@angular/router' ;
3+ import { Title } from '@angular/platform-browser' ;
34
45
56@Component ( {
@@ -14,10 +15,34 @@ import {Router, NavigationStart} from '@angular/router';
1415export class MaterialDocsApp {
1516 isDarkTheme = false ;
1617 showShadow = false ;
18+ baseTitle = 'Angular Material' ;
1719
18- constructor ( router : Router ) {
20+ constructor ( router : Router , private _titleService : Title ) {
1921 router . events . subscribe ( ( data : NavigationStart ) => {
2022 this . showShadow = data . url . startsWith ( '/components' ) ;
23+ this . _setTitle ( window . location . pathname ) ;
2124 } ) ;
2225 }
26+
27+ private _setTitle ( pathname ) {
28+ const title = this . _getTitle ( pathname ) ;
29+ title ?
30+ this . _titleService . setTitle ( `${ this . baseTitle } - ${ this . _capitalizeTitle ( title ) } ` ) :
31+ this . _titleService . setTitle ( this . baseTitle ) ;
32+ }
33+
34+ private _getTitle ( pathname ) {
35+ return pathname . split ( '/' ) . filter ( Boolean ) . pop ( ) ;
36+ }
37+
38+ private _trimFilename ( filename ) {
39+ const isFilenameRegex = new RegExp ( / .+ \. / g) ;
40+ return ~ filename . search ( isFilenameRegex ) ?
41+ filename . match ( isFilenameRegex ) [ 0 ] . replace ( '.' , '' ) :
42+ filename ;
43+ }
44+
45+ private _capitalizeTitle ( title ) {
46+ return title [ 0 ] . toUpperCase ( ) + this . _trimFilename ( title . slice ( 1 ) ) ;
47+ }
2348}
0 commit comments