-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
sidebar.component.ts
86 lines (70 loc) · 2.52 KB
/
sidebar.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
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { Component, Inject, OnDestroy, Renderer2 } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { isBs3, setTheme } from 'ngx-bootstrap/utils';
import { routes } from '../../app.routing';
import { StyleManager } from '../../theme/style-manager';
import { ThemeStorage } from '../../theme/theme-storage';
import { Subscription } from 'rxjs';
const _bs3Css = 'assets/css/bootstrap-3.3.7/css/bootstrap.min.css';
const _bs4Css = 'assets/css/bootstrap-4.0.0/css/bootstrap.min.css';
@Component({
selector: 'sidebar',
templateUrl: './sidebar.component.html'
})
export class SidebarComponent implements OnDestroy {
isShown = false;
get isBs3(): boolean {
return isBs3();
}
routes: any = routes;
search: any = {};
currentTheme: 'bs3' | 'bs4';
scrollSubscription: Subscription;
constructor(
private activatedRoute: ActivatedRoute,
private renderer: Renderer2,
private router: Router,
private themeStorage: ThemeStorage,
public styleManager: StyleManager,
@Inject(DOCUMENT) private document: any
) {
const themeFromUrl = this.activatedRoute.snapshot.queryParams._bsVersion;
const currentTheme = themeFromUrl || this.themeStorage.getStoredTheme();
if (currentTheme) {
this.installTheme(currentTheme);
}
this.router = router;
this.routes = this.routes.filter((v: any) => v.path !== '**');
this.scrollSubscription = this.router.events.subscribe((event: any) => {
if (event instanceof NavigationEnd) {
this.toggle(false);
}
});
}
toggle(isShown?: boolean): void {
this.isShown = typeof isShown === 'undefined' ? !this.isShown : isShown;
if (this.document && this.document.body) {
if (this.isShown) {
this.renderer.addClass(this.document.body, 'isOpenMenu');
} else {
this.renderer.removeClass(this.document.body, 'isOpenMenu');
}
if (this.isShown === false && this.document.documentElement) {
this.renderer.setProperty(this.document.documentElement, 'scrollTop', '0');
this.renderer.setProperty(this.document.body, 'scrollTop', '0');
}
}
}
installTheme(theme: 'bs3' | 'bs4') {
setTheme(theme);
this.currentTheme = this.isBs3 ? 'bs3' : 'bs4';
this.styleManager.setStyle('theme', this.isBs3 ? _bs3Css : _bs4Css);
if (this.currentTheme) {
this.themeStorage.storeTheme(this.currentTheme);
}
}
ngOnDestroy() {
this.scrollSubscription.unsubscribe();
}
}