|
| 1 | +import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core'; |
| 2 | +import { Location, LocationStrategy, PathLocationStrategy, PopStateEvent } from '@angular/common'; |
| 3 | +import 'rxjs/add/operator/filter'; |
| 4 | +import { NavbarComponent } from '../../shared/navbar/navbar.component'; |
| 5 | +import { Router, NavigationEnd, NavigationStart } from '@angular/router'; |
| 6 | +import { Subscription } from 'rxjs/Subscription'; |
| 7 | +import PerfectScrollbar from 'perfect-scrollbar'; |
| 8 | +import { Observable } from 'rxjs'; |
| 9 | + |
| 10 | +@Component({ |
| 11 | + selector: 'app-admin-layout', |
| 12 | + templateUrl: './admin-layout.component.html', |
| 13 | + styleUrls: ['./admin-layout.component.scss'] |
| 14 | +}) |
| 15 | +export class AdminLayoutComponent implements OnInit { |
| 16 | + private _router: Subscription; |
| 17 | + private lastPoppedUrl: string; |
| 18 | + private yScrollStack: number[] = []; |
| 19 | + |
| 20 | + constructor( public location: Location, private router: Router) {} |
| 21 | + |
| 22 | + ngOnInit() { |
| 23 | + const isWindows = navigator.platform.indexOf('Win') > -1 ? true : false; |
| 24 | + |
| 25 | + if (isWindows && !document.getElementsByTagName('body')[0].classList.contains('sidebar-mini')) { |
| 26 | + // if we are on windows OS we activate the perfectScrollbar function |
| 27 | + |
| 28 | + document.getElementsByTagName('body')[0].classList.add('perfect-scrollbar-on'); |
| 29 | + } else { |
| 30 | + document.getElementsByTagName('body')[0].classList.remove('perfect-scrollbar-off'); |
| 31 | + } |
| 32 | + const elemMainPanel = <HTMLElement>document.querySelector('.main-panel'); |
| 33 | + const elemSidebar = <HTMLElement>document.querySelector('.sidebar .sidebar-wrapper'); |
| 34 | + |
| 35 | + this.location.subscribe((ev:PopStateEvent) => { |
| 36 | + this.lastPoppedUrl = ev.url; |
| 37 | + }); |
| 38 | + this.router.events.subscribe((event:any) => { |
| 39 | + if (event instanceof NavigationStart) { |
| 40 | + if (event.url != this.lastPoppedUrl) |
| 41 | + this.yScrollStack.push(window.scrollY); |
| 42 | + } else if (event instanceof NavigationEnd) { |
| 43 | + if (event.url == this.lastPoppedUrl) { |
| 44 | + this.lastPoppedUrl = undefined; |
| 45 | + window.scrollTo(0, this.yScrollStack.pop()); |
| 46 | + } else |
| 47 | + window.scrollTo(0, 0); |
| 48 | + } |
| 49 | + }); |
| 50 | + this._router = this.router.events.filter(event => event instanceof NavigationEnd).subscribe((event: NavigationEnd) => { |
| 51 | + elemMainPanel.scrollTop = 0; |
| 52 | + elemSidebar.scrollTop = 0; |
| 53 | + }); |
| 54 | + if (window.matchMedia(`(min-width: 960px)`).matches && !this.isMac()) { |
| 55 | + let ps = new PerfectScrollbar(elemMainPanel); |
| 56 | + ps = new PerfectScrollbar(elemSidebar); |
| 57 | + } |
| 58 | + } |
| 59 | + ngAfterViewInit() { |
| 60 | + this.runOnRouteChange(); |
| 61 | + } |
| 62 | + isMap(path){ |
| 63 | + var titlee = this.location.prepareExternalUrl(this.location.path()); |
| 64 | + titlee = titlee.slice( 1 ); |
| 65 | + if(path == titlee){ |
| 66 | + return false; |
| 67 | + } |
| 68 | + else { |
| 69 | + return true; |
| 70 | + } |
| 71 | + } |
| 72 | + runOnRouteChange(): void { |
| 73 | + if (window.matchMedia(`(min-width: 960px)`).matches && !this.isMac()) { |
| 74 | + const elemMainPanel = <HTMLElement>document.querySelector('.main-panel'); |
| 75 | + const ps = new PerfectScrollbar(elemMainPanel); |
| 76 | + ps.update(); |
| 77 | + } |
| 78 | + } |
| 79 | + isMac(): boolean { |
| 80 | + let bool = false; |
| 81 | + if (navigator.platform.toUpperCase().indexOf('MAC') >= 0 || navigator.platform.toUpperCase().indexOf('IPAD') >= 0) { |
| 82 | + bool = true; |
| 83 | + } |
| 84 | + return bool; |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments