This repository was archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathcomponent-sidenav.ts
198 lines (184 loc) · 5.84 KB
/
component-sidenav.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
191
192
193
194
195
196
197
198
import {Component,
Input,
NgModule,
NgZone,
OnDestroy,
OnInit,
ViewChild,
ViewEncapsulation,
forwardRef
} from '@angular/core';
import {animate, state, style, transition, trigger} from '@angular/animations';
import {CdkAccordionModule} from '@angular/cdk/accordion';
import {BreakpointObserver} from '@angular/cdk/layout';
import {CommonModule, NgIf, AsyncPipe, NgFor} from '@angular/common';
import {HttpClientModule} from '@angular/common/http';
import {FormsModule} from '@angular/forms';
import {MatIconModule} from '@angular/material/icon';
import {MatListModule} from '@angular/material/list';
import {
MatSidenav,
MatSidenavModule,
MatDrawerToggleResult,
} from '@angular/material/sidenav';
import {
ActivatedRoute,
Params,
RouterModule,
Routes,
RouterOutlet,
RouterLinkActive,
RouterLink
} from '@angular/router';
import {combineLatest, Observable, Subscription} from 'rxjs';
import {map} from 'rxjs/operators';
import {DocViewerModule} from '../../shared/doc-viewer/doc-viewer-module';
import {
DocumentationItems
} from '../../shared/documentation-items/documentation-items';
import {Footer} from '../../shared/footer/footer';
import {
NavigationFocusService
} from '../../shared/navigation-focus/navigation-focus.service';
import {
ComponentCategoryList,
ComponentCategoryListModule
} from '../component-category-list/component-category-list';
import {ComponentPageHeader} from '../component-page-header/component-page-header';
import {
ComponentApi,
ComponentExamples,
ComponentOverview,
ComponentViewer,
ComponentViewerModule
} from '../component-viewer/component-viewer';
// These constants are used by the ComponentSidenav for orchestrating the MatSidenav in a responsive
// way. This includes hiding the sidenav, defaulting it to open, changing the mode from over to
// side, determining the size of the top gap, and whether the sidenav is fixed in the viewport.
// The values were determined through the combination of Material Design breakpoints and careful
// testing of the application across a range of common device widths (360px+).
// These breakpoint values need to stay in sync with the related Sass variables in
// src/styles/_constants.scss.
const EXTRA_SMALL_WIDTH_BREAKPOINT = 720;
const SMALL_WIDTH_BREAKPOINT = 959;
@Component({
selector: 'app-component-sidenav',
templateUrl: './component-sidenav.html',
styleUrls: ['./component-sidenav.scss'],
encapsulation: ViewEncapsulation.None,
standalone: true,
imports: [
MatSidenavModule,
NgIf,
forwardRef(() => ComponentNav),
ComponentPageHeader,
RouterOutlet,
Footer,
AsyncPipe,
],
})
export class ComponentSidenav implements OnInit, OnDestroy {
@ViewChild(MatSidenav) sidenav!: MatSidenav;
params: Observable<Params> | undefined;
isExtraScreenSmall: Observable<boolean>;
isScreenSmall: Observable<boolean>;
private subscriptions = new Subscription();
constructor(public docItems: DocumentationItems,
private _route: ActivatedRoute,
private _navigationFocusService: NavigationFocusService,
zone: NgZone,
breakpoints: BreakpointObserver) {
this.isExtraScreenSmall =
breakpoints.observe(`(max-width: ${EXTRA_SMALL_WIDTH_BREAKPOINT}px)`)
.pipe(map(breakpoint => breakpoint.matches));
this.isScreenSmall = breakpoints.observe(`(max-width: ${SMALL_WIDTH_BREAKPOINT}px)`)
.pipe(map(breakpoint => breakpoint.matches));
}
ngOnInit() {
// Combine params from all of the path into a single object.
this.params = combineLatest(
this._route.pathFromRoot.map(route => route.params), Object.assign);
this.subscriptions.add(
this._navigationFocusService.navigationEndEvents.pipe(map(() => this.isScreenSmall))
.subscribe((shouldCloseSideNav) => {
if (shouldCloseSideNav && this.sidenav) {
this.sidenav.close();
}
}
));
}
ngOnDestroy() {
this.subscriptions.unsubscribe();
}
toggleSidenav(sidenav: MatSidenav): Promise<MatDrawerToggleResult> {
return sidenav.toggle();
}
}
@Component({
selector: 'app-component-nav',
templateUrl: './component-nav.html',
animations: [
trigger('bodyExpansion', [
state('collapsed', style({ height: '0px', display: 'none' })),
state('expanded', style({ height: '*', display: 'block' })),
transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4,0.0,0.2,1)')),
]),
],
standalone: true,
imports: [
NgIf,
MatListModule,
NgFor,
RouterLinkActive,
RouterLink,
AsyncPipe,
],
})
export class ComponentNav {
@Input() params: Observable<Params> | undefined;
currentItemId: string | undefined;
constructor(public docItems: DocumentationItems) {}
}
const routes: Routes = [{
path: '',
component: ComponentSidenav,
children: [
{path: 'component/:id', redirectTo: ':id', pathMatch: 'full'},
{path: 'category/:id', redirectTo: '/categories/:id', pathMatch: 'full'},
{
path: 'categories',
children: [
{path: '', component: ComponentCategoryList},
],
},
{
path: ':id',
component: ComponentViewer,
children: [
{path: '', redirectTo: 'overview', pathMatch: 'full'},
{path: 'overview', component: ComponentOverview, pathMatch: 'full'},
{path: 'api', component: ComponentApi, pathMatch: 'full'},
{path: 'examples', component: ComponentExamples, pathMatch: 'full'}
],
},
{path: '**', redirectTo: '/404'}
]
}];
@NgModule({
imports: [
MatSidenavModule,
MatListModule,
CommonModule,
ComponentCategoryListModule,
ComponentViewerModule,
DocViewerModule,
FormsModule,
HttpClientModule,
CdkAccordionModule,
MatIconModule,
RouterModule.forChild(routes),
ComponentSidenav, ComponentNav
],
exports: [ComponentSidenav],
})
export class ComponentSidenavModule {}