[Dashboard]: Reworked sidebar menu auto expanding
[src/xds/xds-agent.git] / webapp / src / app / @theme / layouts / xds / xds.layout.ts
index 7e7f0fd..b362fff 100644 (file)
@@ -29,6 +29,7 @@ export class XdsLayoutComponent implements OnDestroy {
   subMenu: NbMenuItem[] = [];
   layout: any = {};
   sidebar: any = {};
+  sidebarPinned = false;
   sidebarCompact = true;
 
   protected layoutState$: Subscription;
@@ -43,6 +44,7 @@ export class XdsLayoutComponent implements OnDestroy {
     protected themeService: NbThemeService,
     protected bpService: NbMediaBreakpointsService,
     protected sidebarService: NbSidebarService) {
+
     this.layoutState$ = this.stateService.onLayoutState()
       .subscribe((layout: string) => this.layout = layout);
 
@@ -52,40 +54,67 @@ export class XdsLayoutComponent implements OnDestroy {
       });
 
     const isBp = this.bpService.getByName('is');
+
     this.menuClick$ = this.menuService.onItemSelect()
       .withLatestFrom(this.themeService.onMediaQueryChange())
       .delay(20)
       .subscribe(([item, [bpFrom, bpTo]]: [any, [NbMediaBreakpoint, NbMediaBreakpoint]]) => {
-
+        /*
         this.sidebarCompact = false;
+        */
+        // automatically collapse sidebar for narrow screen / mobile
         if (bpTo.width <= isBp.width) {
           this.sidebarService.collapse('menu-sidebar');
         }
       });
 
     // Set sidebarCompact according to sidebar state changes
-    this.sidebarService.onToggle().subscribe(s => s.tag === 'menu-sidebar' && (this.sidebarCompact = !this.sidebarCompact));
+    this.sidebarService.onToggle().subscribe(s => {
+      if (s.tag === 'menu-sidebar') {
+        this.sidebarPinned = false;
+        this.sidebarCompact = !this.sidebarCompact;
+      }
+    });
+
     this.sidebarService.onCollapse().subscribe(s => s.tag === 'menu-sidebar' && (this.sidebarCompact = true));
-    this.sidebarService.onExpand().subscribe(() => this.sidebarCompact = false);
+    this.sidebarService.onExpand().subscribe(s => s.tag === 'menu-sidebar' && (this.sidebarCompact = false));
     this.menuService.onSubmenuToggle().subscribe(i => i.item && i.item.expanded && (this.sidebarCompact = false));
 
     // Automatically expand sidebar on mouse over
     this._mouseEnterStream.flatMap(e => {
       return Observable
         .of(e)
-        .delay(200)
+        .delay(100)
         .takeUntil(this._mouseLeaveStream);
     })
-      .subscribe(e => (this.sidebarCompact) && this.sidebarService.toggle(true, 'menu-sidebar'));
+      .subscribe(e => {
+        if (this.sidebarPinned || !this.sidebarCompact) {
+          return;
+        }
+        // this._mouseLeaveStream.emit(null);
+        this.sidebarService.toggle(false, 'menu-sidebar');
+      });
 
     // Automatically collapse sidebar on mouse leave
     this._mouseLeaveStream.flatMap(e => {
       return Observable
         .of(e)
-        .delay(500)
+        .delay(100)
         .takeUntil(this._mouseEnterStream);
     })
-      .subscribe(e => this.sidebarService.toggle(true, 'menu-sidebar'));
+      .subscribe(e => {
+        if (this.sidebarPinned || this.sidebarCompact) {
+          return;
+        }
+        // this._mouseEnterStream.emit(null);
+        this.sidebarService.toggle(true, 'menu-sidebar');
+      });
+  }
+
+  ngOnDestroy() {
+    this.layoutState$.unsubscribe();
+    this.sidebarState$.unsubscribe();
+    this.menuClick$.unsubscribe();
   }
 
   onMouseEnter($event) {
@@ -96,13 +125,7 @@ export class XdsLayoutComponent implements OnDestroy {
     this._mouseLeaveStream.emit($event);
   }
 
-  toogleSidebar() {
-    this.sidebarService.toggle(true, 'menu-sidebar');
-  }
-
-  ngOnDestroy() {
-    this.layoutState$.unsubscribe();
-    this.sidebarState$.unsubscribe();
-    this.menuClick$.unsubscribe();
+  pinSidebar() {
+    this.sidebarPinned = !this.sidebarPinned;
   }
 }