This repository was archived by the owner on Jan 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathmaterial-docs-app.ts
More file actions
73 lines (60 loc) · 2.22 KB
/
material-docs-app.ts
File metadata and controls
73 lines (60 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {Component, ViewEncapsulation} from '@angular/core';
import {Router, NavigationStart} from '@angular/router';
import {Title} from '@angular/platform-browser';
@Component({
selector: 'material-docs-app',
templateUrl: './material-docs-app.html',
styleUrls: ['./material-docs-app.scss'],
encapsulation: ViewEncapsulation.None,
})
export class MaterialDocsApp {
showShadow = false;
baseTitle = 'Angular Material';
constructor(router: Router, private _titleService: Title) {
let previousRoute = router.routerState.snapshot.url;
router.events.subscribe((data: NavigationStart) => {
this.showShadow = data.url.startsWith('/components');
// We want to reset the scroll position on navigation except when navigating within
// the documentation for a single component.
if (!isNavigationWithinComponentView(previousRoute, data.url)) {
resetScrollPosition();
}
previousRoute = data.url;
// set page title
this._setTitle(window.location.pathname);
});
}
private _setTitle(pathname) {
const title = this._getTitle(pathname);
title ?
this._titleService.setTitle(`${this.baseTitle} - ${this._capitalizeTitle(title)}`) :
this._titleService.setTitle(this.baseTitle);
}
private _getTitle(pathname) {
return pathname.split('/').filter(Boolean).pop();
}
private _trimFilename(filename) {
const isFilenameRegex = new RegExp(/.+\./g);
return ~filename.search(isFilenameRegex) ?
filename.match(isFilenameRegex)[0].replace('.', '') :
filename;
}
private _capitalizeTitle(title) {
return title[0].toUpperCase() + this._trimFilename(title.slice(1));
}
}
function isNavigationWithinComponentView(oldUrl: string, newUrl: string) {
const componentViewExpression = /components\/(\w+)/;
return oldUrl && newUrl
&& componentViewExpression.test(oldUrl)
&& componentViewExpression.test(newUrl)
&& oldUrl.match(componentViewExpression)[1] === newUrl.match(componentViewExpression)[1];
}
function resetScrollPosition() {
if (typeof document === 'object' && document) {
const sidenavContent = document.querySelector('.mat-sidenav-content');
if (sidenavContent) {
sidenavContent.scrollTop = 0;
}
}
}