monitor-dashboard/src/app/library/sharedComponents/sidebar/sideBar.service.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

import {Injectable} from "@angular/core";
import {BehaviorSubject, Observable} from "rxjs";
import {ActivationStart, Router} from "@angular/router";
@Injectable({
providedIn: 'root'
})
export class SideBarService {
/**
* Set this to true when sidebar items are ready.
*/
private openSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
/**
* Add hasSidebar: false on data of route config, if sidebar is not needed.
*/
private hasSidebarSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);
constructor(private router: Router) {
this.router.events.subscribe(event => {
if (event instanceof ActivationStart) {
let data = event.snapshot.data;
if (this.hasSidebarSubject.value === true &&
data['hasSidebar'] !== undefined &&
data['hasSidebar'] === false) {
this.setHasSidebar(false);
} else if (this.hasSidebarSubject.value === false) {
this.setHasSidebar(true);
}
}
});
}
get open(): boolean {
return this.openSubject.getValue();
}
setOpen(value: boolean) {
this.openSubject.next(value);
}
get hasSidebar(): Observable<boolean> {
return this.hasSidebarSubject.asObservable();
}
setHasSidebar(value: boolean) {
this.hasSidebarSubject.next(value);
}
}