forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.component.ts
190 lines (153 loc) · 6.13 KB
/
app.component.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { Component, ElementRef, HostBinding, HostListener, OnInit,
QueryList, ViewChild, ViewChildren } from '@angular/core';
import { MdSidenav } from '@angular/material';
import { AutoScrollService } from 'app/shared/auto-scroll.service';
import { CurrentNode, NavigationService, NavigationViews, NavigationNode, VersionInfo } from 'app/navigation/navigation.service';
import { DocumentService, DocumentContents } from 'app/documents/document.service';
import { DocViewerComponent } from 'app/layout/doc-viewer/doc-viewer.component';
import { LocationService } from 'app/shared/location.service';
import { NavMenuComponent } from 'app/layout/nav-menu/nav-menu.component';
import { SearchResultsComponent } from 'app/search/search-results/search-results.component';
import { SwUpdateNotificationsService } from 'app/sw-updates/sw-update-notifications.service';
const sideNavView = 'SideNav';
@Component({
selector: 'aio-shell',
templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
currentNode: CurrentNode;
currentPath: string;
dtOn = false;
pageId: string;
currentDocument: DocumentContents;
footerNodes: NavigationNode[];
isStarting = true;
isSideBySide = false;
private isSideNavDoc = false;
private previousNavView: string;
private sideBySideWidth = 1032;
sideNavNodes: NavigationNode[];
topMenuNodes: NavigationNode[];
currentDocVersion: NavigationNode;
docVersions: NavigationNode[];
versionInfo: VersionInfo;
get homeImageUrl() {
return this.isSideBySide ?
'assets/images/logos/standard/logo-nav@2x.png' :
'assets/images/logos/standard/shield-large@2x.png';
}
get isOpened() { return this.isSideBySide && this.isSideNavDoc; }
get mode() { return this.isSideBySide ? 'side' : 'over'; }
// Need the doc-viewer element for scrolling the contents
@ViewChild(DocViewerComponent, { read: ElementRef })
docViewer: ElementRef;
@ViewChildren('searchBox, searchResults', { read: ElementRef })
searchElements: QueryList<ElementRef>;
@ViewChild(SearchResultsComponent)
searchResults: SearchResultsComponent;
@HostBinding('class')
shellClasses = '';
@ViewChild(MdSidenav)
sidenav: MdSidenav;
constructor(
private autoScrollService: AutoScrollService,
private documentService: DocumentService,
private locationService: LocationService,
private navigationService: NavigationService,
private swUpdateNotifications: SwUpdateNotificationsService
) { }
ngOnInit() {
this.onResize(window.innerWidth);
/* No need to unsubscribe because this root component never dies */
this.documentService.currentDocument.subscribe(doc => {
this.currentDocument = doc;
this.setPageId(doc.id);
this.shellClasses = this.getShellClasses(doc);
});
this.locationService.currentPath.subscribe(path => {
if (this.currentPath && path === this.currentPath) {
// scroll only if on same page (most likely a change to the hash)
this.autoScroll();
} else {
// don't scroll; leave that to `onDocRendered`
this.currentPath = path;
}
});
this.navigationService.currentNode.subscribe(currentNode => {
this.currentNode = currentNode;
// Toggle the sidenav if side-by-side and the kind of view changed
if (this.previousNavView === currentNode.view) { return; }
this.previousNavView = currentNode.view;
this.isSideNavDoc = currentNode.view === sideNavView;
this.sideNavToggle(this.isSideNavDoc && this.isSideBySide);
});
this.navigationService.navigationViews.subscribe(views => {
this.docVersions = views['docVersions'] || [];
this.footerNodes = views['Footer'] || [];
this.sideNavNodes = views['SideNav'] || [];
this.topMenuNodes = views['TopBar'] || [];
this.currentDocVersion = this.docVersions[0];
});
this.navigationService.versionInfo.subscribe( vi => this.versionInfo = vi );
this.swUpdateNotifications.enable();
}
// Scroll to the anchor in the hash fragment or top of doc.
autoScroll() {
this.autoScrollService.scroll();
}
onDocRendered() {
// Scroll after the doc-viewer has finished rendering the new doc
this.autoScroll();
this.isStarting = false;
}
onDocVersionChange(versionIndex: number) {
const version = this.docVersions[versionIndex];
if (version.url) {
this.locationService.go(version.url);
}
}
@HostListener('window:resize', ['$event.target.innerWidth'])
onResize(width) {
this.isSideBySide = width > this.sideBySideWidth;
}
@HostListener('click', ['$event.target', '$event.button', '$event.ctrlKey', '$event.metaKey', '$event.altKey'])
onClick(eventTarget: HTMLElement, button: number, ctrlKey: boolean, metaKey: boolean, altKey: boolean): boolean {
// Hide the search results if we clicked outside both the search box and the search results
if (this.searchResults) {
const hits = this.searchElements.filter(element => element.nativeElement.contains(eventTarget));
if (hits.length === 0) {
this.searchResults.hideResults();
}
}
if (eventTarget.tagName === 'FOOTER' && metaKey && altKey) {
this.dtOn = !this.dtOn;
return false;
}
// Deal with anchor clicks; climb DOM tree until anchor found (or null)
let target = eventTarget;
while (target && !(target instanceof HTMLAnchorElement)) {
target = target.parentElement;
}
if (target) {
return this.locationService.handleAnchorClick(target as HTMLAnchorElement, button, ctrlKey, metaKey);
}
return true;
}
sideNavToggle(value?: boolean) {
this.sidenav.toggle(value);
}
setPageId(id: string) {
// Special case the home page
this.pageId = (id === 'index') ? 'home' : id.replace('/', '-');
}
private getShellClasses(doc: DocumentContents) {
let shellClasses = /^(guide|tutorial)\//.test(doc.id) ? '' : 'marketing ';
const contents = doc.contents || '';
// extract the classes from `<aio-context>`
const contextMatch = contents.match(/<aio-context.*class="([^"]*)/);
if (contextMatch) {
shellClasses += contextMatch[1] || '';
}
return shellClasses;
}
}