admin.component.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
  2. import { Location, LocationStrategy, PathLocationStrategy, PopStateEvent } from '@angular/common';
  3. import { filter, map } from 'rxjs/operators';
  4. import { Subscription } from 'rxjs/Subscription';
  5. //import { NavbarComponent } from '../../components/navbar/navbar.component';
  6. import { Router, NavigationEnd, NavigationStart } from '@angular/router';
  7. import PerfectScrollbar from 'perfect-scrollbar';
  8. import * as $ from "jquery";
  9. @Component({
  10. selector: 'app-admin',
  11. templateUrl: './admin.component.html',
  12. styleUrls: ['./admin.component.scss']
  13. })
  14. export class AdminComponent implements OnInit {
  15. private _router: Subscription;
  16. private lastPoppedUrl: string;
  17. private yScrollStack: number[] = [];
  18. constructor( public location: Location, private router: Router) {}
  19. ngOnInit() {
  20. const isWindows = navigator.platform.indexOf('Win') > -1 ? true : false;
  21. if (isWindows && !document.getElementsByTagName('body')[0].classList.contains('sidebar-mini')) {
  22. // if we are on windows OS we activate the perfectScrollbar function
  23. document.getElementsByTagName('body')[0].classList.add('perfect-scrollbar-on');
  24. } else {
  25. document.getElementsByTagName('body')[0].classList.remove('perfect-scrollbar-off');
  26. }
  27. const elemMainPanel = <HTMLElement>document.querySelector('.main-panel');
  28. const elemSidebar = <HTMLElement>document.querySelector('.sidebar .sidebar-wrapper');
  29. this.location.subscribe((ev:PopStateEvent) => {
  30. this.lastPoppedUrl = ev.url;
  31. });
  32. this.router.events.subscribe((event:any) => {
  33. if (event instanceof NavigationStart) {
  34. if (event.url != this.lastPoppedUrl)
  35. this.yScrollStack.push(window.scrollY);
  36. } else if (event instanceof NavigationEnd) {
  37. if (event.url == this.lastPoppedUrl) {
  38. this.lastPoppedUrl = undefined;
  39. window.scrollTo(0, this.yScrollStack.pop());
  40. } else
  41. window.scrollTo(0, 0);
  42. }
  43. });
  44. this._router = this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event: NavigationEnd) => {
  45. elemMainPanel.scrollTop = 0;
  46. elemSidebar.scrollTop = 0;
  47. });
  48. if (window.matchMedia(`(min-width: 960px)`).matches && !this.isMac()) {
  49. let ps = new PerfectScrollbar(elemMainPanel);
  50. ps = new PerfectScrollbar(elemSidebar);
  51. }
  52. const window_width = $(window).width();
  53. let $sidebar = $('.sidebar');
  54. let $sidebar_responsive = $('body > .navbar-collapse');
  55. let $sidebar_img_container = $sidebar.find('.sidebar-background');
  56. if(window_width > 767){
  57. if($('.fixed-plugin .dropdown').hasClass('show-dropdown')){
  58. $('.fixed-plugin .dropdown').addClass('open');
  59. }
  60. }
  61. $('.fixed-plugin a').click(function(event){
  62. // Alex if we click on switch, stop propagation of the event, so the dropdown will not be hide, otherwise we set the section active
  63. if($(this).hasClass('switch-trigger')){
  64. if(event.stopPropagation){
  65. event.stopPropagation();
  66. }
  67. else if(window.event){
  68. window.event.cancelBubble = true;
  69. }
  70. }
  71. });
  72. $('.fixed-plugin .badge').click(function(){
  73. let $full_page_background = $('.full-page-background');
  74. $(this).siblings().removeClass('active');
  75. $(this).addClass('active');
  76. var new_color = $(this).data('color');
  77. if($sidebar.length !== 0){
  78. $sidebar.attr('data-color', new_color);
  79. }
  80. if($sidebar_responsive.length != 0){
  81. $sidebar_responsive.attr('data-color',new_color);
  82. }
  83. });
  84. $('.fixed-plugin .img-holder').click(function(){
  85. let $full_page_background = $('.full-page-background');
  86. $(this).parent('li').siblings().removeClass('active');
  87. $(this).parent('li').addClass('active');
  88. //var new_image = $(this).find("img").attr('src');
  89. if($sidebar_img_container.length !=0 ){
  90. $sidebar_img_container.fadeOut('fast', function(){
  91. //$sidebar_img_container.css('background-image','url("' + new_image + '")');
  92. $sidebar_img_container.fadeIn('fast');
  93. });
  94. }
  95. if($full_page_background.length != 0){
  96. $full_page_background.fadeOut('fast', function(){
  97. //$full_page_background.css('background-image','url("' + new_image + '")');
  98. $full_page_background.fadeIn('fast');
  99. });
  100. }
  101. if($sidebar_responsive.length != 0){
  102. //$sidebar_responsive.css('background-image','url("' + new_image + '")');
  103. }
  104. });
  105. }
  106. ngAfterViewInit() {
  107. this.runOnRouteChange();
  108. }
  109. isMaps(path){
  110. var titlee = this.location.prepareExternalUrl(this.location.path());
  111. titlee = titlee.slice( 1 );
  112. if(path == titlee){
  113. return false;
  114. }
  115. else {
  116. return true;
  117. }
  118. }
  119. runOnRouteChange(): void {
  120. if (window.matchMedia(`(min-width: 960px)`).matches && !this.isMac()) {
  121. const elemMainPanel = <HTMLElement>document.querySelector('.main-panel');
  122. const ps = new PerfectScrollbar(elemMainPanel);
  123. ps.update();
  124. }
  125. }
  126. isMac(): boolean {
  127. let bool = false;
  128. if (navigator.platform.toUpperCase().indexOf('MAC') >= 0 || navigator.platform.toUpperCase().indexOf('IPAD') >= 0) {
  129. bool = true;
  130. }
  131. return bool;
  132. }
  133. }