[Dashboard]: Reworked sidebar menu auto expanding
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Sun, 28 Jan 2018 20:28:42 +0000 (21:28 +0100)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Tue, 30 Jan 2018 10:52:46 +0000 (11:52 +0100)
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
webapp/src/app/@theme/layouts/xds/xds.layout.html
webapp/src/app/@theme/layouts/xds/xds.layout.scss
webapp/src/app/@theme/layouts/xds/xds.layout.ts

index 74d0f97..ee0a80b 100644 (file)
@@ -4,18 +4,21 @@
     <ngx-header [position]="sidebar.id === 'left' ? 'normal': 'inverse'"></ngx-header>
   </nb-layout-header>
 
-  <nb-sidebar class="menu-sidebar" tag="menu-sidebar" responsive [right]="sidebar.id === 'right'"
+  <nb-sidebar class="menu-sidebar" tag="menu-sidebar" responsive [right]="sidebar.id === 'right'" state="compacted"
     (mouseenter)="onMouseEnter($event)" (mouseleave)="onMouseLeave($event)">
 
-    <nb-sidebar-header (click)="toogleSidebar()">
-      <!-- XXX - ugly rework -->
+    <nb-sidebar-header (click)="pinSidebar()">
+      <!-- left sidebar -->
       <nb-actions *ngIf="sidebar.id === 'left'" size="small" class="header-container right">
-        <nb-action *ngIf="!sidebarCompact" icon="fa fa-angle-double-left" style="margin-left: 80%;"></nb-action>
-        <nb-action *ngIf="sidebarCompact" icon="fa fa-angle-double-right"></nb-action>
+        <nb-action id="pin-sidebar" [icon]="(!sidebarPinned || sidebarCompact) ? 'fa fa-angle-double-right':'fa fa-angle-double-left'"
+        [ngbTooltip]="sidebarPinned ? 'Undock the sidebar so it is hidden when no being focused':'Dock the sidebar so you can always see it'" placement="bottom">
+        </nb-action>
       </nb-actions>
+      <!-- right sidebar -->
       <nb-actions *ngIf="sidebar.id === 'right'" size="small" class="header-container left">
-        <nb-action *ngIf="!sidebarCompact" icon="fa fa-angle-double-right" style="margin-right: 80%;"></nb-action>
-        <nb-action *ngIf="sidebarCompact" icon="fa fa-angle-double-left"></nb-action>
+        <nb-action id="pin-sidebar" [icon]="(!sidebarPinned || sidebarCompact) ? 'fa fa-angle-double-left':'fa fa-angle-double-right'"
+        [ngbTooltip]="sidebarPinned ? 'Undock the sidebar so it is hidden when no being focused':'Dock the sidebar so you can always see it'" placement="bottom">
+        </nb-action>
       </nb-actions>
     </nb-sidebar-header>
 
index 7ccf7b7..5ac018e 100644 (file)
       text-align: center;
     }
 
+    /deep/ .tooltip {
+      animation-name: delayedFadeIn;
+      animation-duration: 2s;
+      width: 8rem;
+    }
+
     background: transparent;
 
     .main-btn {
         }
       }
     }
+
+    &.left.expanded {
+      #pin-sidebar {
+          margin-left: 11rem;
+      }
+    }
   }
 
   @include media-breakpoint-down(xs) {
     }
   }
 }
+
+@Keyframes delayedFadeIn {
+  0% {opacity: 0;}
+  75% {opacity: 0;} /* Set this to 99% for no fade-in. */
+  100% {opacity: 1;}
+}
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;
   }
 }