-
-
Notifications
You must be signed in to change notification settings - Fork 331
/
admin-layout.component.ts
152 lines (128 loc) · 4.46 KB
/
admin-layout.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
import {
Component,
OnInit,
OnDestroy,
ViewChild,
HostBinding,
ElementRef,
Inject,
Optional,
ViewEncapsulation,
} from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { NavigationEnd, Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { BreakpointObserver } from '@angular/cdk/layout';
import { OverlayContainer } from '@angular/cdk/overlay';
import { Directionality } from '@angular/cdk/bidi';
import { MatSidenav, MatSidenavContent } from '@angular/material/sidenav';
import { SettingsService, AppSettings } from '@core';
import { AppDirectionality } from '@shared';
const MOBILE_MEDIAQUERY = 'screen and (max-width: 599px)';
const TABLET_MEDIAQUERY = 'screen and (min-width: 600px) and (max-width: 959px)';
const MONITOR_MEDIAQUERY = 'screen and (min-width: 960px)';
@Component({
selector: 'app-admin-layout',
templateUrl: './admin-layout.component.html',
styleUrls: ['./admin-layout.component.scss'],
encapsulation: ViewEncapsulation.None,
})
export class AdminLayoutComponent implements OnInit, OnDestroy {
@ViewChild('sidenav', { static: true }) sidenav: MatSidenav;
@ViewChild('content', { static: true }) content: MatSidenavContent;
options = this.settings.getOptions();
private layoutChangesSubscription: Subscription;
private isMobileScreen = false;
get isOver(): boolean {
return this.isMobileScreen;
}
private contentWidthFix = true;
@HostBinding('class.matero-content-width-fix') get isContentWidthFix() {
return (
this.contentWidthFix &&
this.options.navPos === 'side' &&
this.options.sidenavOpened &&
!this.isOver
);
}
private collapsedWidthFix = true;
@HostBinding('class.matero-sidenav-collapsed-fix') get isCollapsedWidthFix() {
return (
this.collapsedWidthFix &&
(this.options.navPos === 'top' || (this.options.sidenavOpened && this.isOver))
);
}
constructor(
private router: Router,
private breakpointObserver: BreakpointObserver,
private overlay: OverlayContainer,
private element: ElementRef,
private settings: SettingsService,
@Optional() @Inject(DOCUMENT) private document: Document,
@Inject(Directionality) public dir: AppDirectionality
) {
this.dir.value = this.options.dir;
this.document.body.dir = this.dir.value;
this.layoutChangesSubscription = this.breakpointObserver
.observe([MOBILE_MEDIAQUERY, TABLET_MEDIAQUERY, MONITOR_MEDIAQUERY])
.subscribe(state => {
// SidenavOpened must be reset true when layout changes
this.options.sidenavOpened = true;
this.isMobileScreen = state.breakpoints[MOBILE_MEDIAQUERY];
this.options.sidenavCollapsed = state.breakpoints[TABLET_MEDIAQUERY];
this.contentWidthFix = state.breakpoints[MONITOR_MEDIAQUERY];
});
// TODO: Scroll top to container
this.router.events.subscribe(evt => {
if (evt instanceof NavigationEnd) {
this.content.scrollTo({ top: 0 });
}
});
// Initialize project theme with options
this.receiveOptions(this.options);
}
ngOnInit() {
setTimeout(() => (this.contentWidthFix = this.collapsedWidthFix = false));
}
ngOnDestroy() {
this.layoutChangesSubscription.unsubscribe();
}
toggleCollapsed() {
this.options.sidenavCollapsed = !this.options.sidenavCollapsed;
this.resetCollapsedState();
}
resetCollapsedState(timer = 400) {
// TODO: Trigger when transition end
setTimeout(() => {
this.settings.setNavState('collapsed', this.options.sidenavCollapsed);
}, timer);
}
sidenavCloseStart() {
this.contentWidthFix = false;
}
sidenavOpenedChange(isOpened: boolean) {
this.options.sidenavOpened = isOpened;
this.settings.setNavState('opened', isOpened);
this.collapsedWidthFix = !this.isOver;
this.resetCollapsedState();
}
/** Demo purposes only */
receiveOptions(options: AppSettings): void {
this.options = options;
this.toggleDarkTheme(options);
this.toggleDirection(options);
}
toggleDarkTheme(options: AppSettings) {
if (options.theme === 'dark') {
this.element.nativeElement.classList.add('theme-dark');
this.overlay.getContainerElement().classList.add('theme-dark');
} else {
this.element.nativeElement.classList.remove('theme-dark');
this.overlay.getContainerElement().classList.remove('theme-dark');
}
}
toggleDirection(options: AppSettings) {
this.dir.value = options.dir;
this.document.body.dir = this.dir.value;
}
}