feat: add perfect-scrollbar for side menu

This commit is contained in:
Roman Hotsiy 2017-04-23 16:10:32 +03:00
parent 8a49fb30de
commit cdeee6740d
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
6 changed files with 62 additions and 8 deletions

View File

@ -45,7 +45,7 @@
} }
side-menu { side-menu {
overflow-y: auto; overflow: hidden;
} }
[sticky-sidebar] { [sticky-sidebar] {

View File

@ -8,7 +8,7 @@
<side-menu-items [items]="menuItems" (activate)="activateAndScroll($event)"></side-menu-items> <side-menu-items [items]="menuItems" (activate)="activateAndScroll($event)"></side-menu-items>
</ng-template> </ng-template>
<div #desktop id="resources-nav"> <div #desktop id="resources-nav" perfect-scrollbar>
<ul class="menu-root"> <ul class="menu-root">
<div *ngIf="itemsTemplate; else default"> <div *ngIf="itemsTemplate; else default">
<ng-container *ngTemplateOutlet="itemsTemplate; context: this"></ng-container> <ng-container *ngTemplateOutlet="itemsTemplate; context: this"></ng-container>

View File

@ -2,10 +2,14 @@
$mobile-menu-compact-breakpoint: 550px; $mobile-menu-compact-breakpoint: 550px;
:host { :host {
display: block; display: flex;
box-sizing: border-box; box-sizing: border-box;
} }
#resources-nav {
position: relative;
}
ul.menu-root { ul.menu-root {
margin: 0; margin: 0;
padding: 0; padding: 0;
@ -35,6 +39,10 @@ ul.menu-root {
} }
@media (max-width: $side-menu-mobile-breakpoint) { @media (max-width: $side-menu-mobile-breakpoint) {
:host {
display: block;
}
.mobile-nav { .mobile-nav {
display: block; display: block;
} }

View File

@ -6,12 +6,14 @@ import { Component,
Output, Output,
ElementRef, ElementRef,
ChangeDetectorRef, ChangeDetectorRef,
ViewChild,
OnInit, OnInit,
OnDestroy OnDestroy
} from '@angular/core'; } from '@angular/core';
import { trigger, state, animate, transition, style } from '@angular/core'; import { trigger, state, animate, transition, style } from '@angular/core';
import { ScrollService, MenuService, OptionsService, MenuItem } from '../../services/'; import { ScrollService, MenuService, OptionsService, MenuItem } from '../../services/';
import { PerfectScrollbar } from '../../shared/components';
import { BrowserDomAdapter as DOM } from '../../utils/browser-adapter'; import { BrowserDomAdapter as DOM } from '../../utils/browser-adapter';
const global = window; const global = window;
@ -51,6 +53,7 @@ export class SideMenu implements OnInit, OnDestroy {
activeItemCaption: string; activeItemCaption: string;
menuItems: Array<MenuItem>; menuItems: Array<MenuItem>;
@Input() itemsTemplate; @Input() itemsTemplate;
@ViewChild(PerfectScrollbar) PS:PerfectScrollbar;
private options: any; private options: any;
private $element: any; private $element: any;
@ -77,7 +80,7 @@ export class SideMenu implements OnInit, OnDestroy {
this.changedActiveSubscription = this.menuService.changedActiveItem.subscribe((evt) => this.changed(evt)); this.changedActiveSubscription = this.menuService.changedActiveItem.subscribe((evt) => this.changed(evt));
this.changedSubscription = this.menuService.changed.subscribe((evt) => { this.changedSubscription = this.menuService.changed.subscribe((evt) => {
this.detectorRef.detectChanges() this.update();
}); });
} }
@ -95,11 +98,16 @@ export class SideMenu implements OnInit, OnDestroy {
this.activeItemCaption = ''; this.activeItemCaption = '';
} }
//safari doesn't update bindings if not run changeDetector manually :( // safari doesn't update bindings if not run changeDetector manually :(
this.detectorRef.detectChanges(); this.update();
this.scrollActiveIntoView(); this.scrollActiveIntoView();
} }
update() {
this.detectorRef.detectChanges();
this.PS && this.PS.update();
}
scrollActiveIntoView() { scrollActiveIntoView() {
let $item = this.$element.querySelector('li.active, label.active'); let $item = this.$element.querySelector('li.active, label.active');
if ($item) $item.scrollIntoViewIfNeeded(); if ($item) $item.scrollIntoViewIfNeeded();

View File

@ -0,0 +1,37 @@
'use strict';
import 'perfect-scrollbar/dist/css/perfect-scrollbar.css';
import { Directive, ElementRef, Input, OnInit, OnDestroy } from '@angular/core';
import { BrowserDomAdapter as DOM } from '../../../utils/browser-adapter';
import * as PS from 'perfect-scrollbar';
@Directive({
selector: '[perfect-scrollbar]'
})
export class PerfectScrollbar implements OnInit, OnDestroy {
$element: any;
subscription: any;
constructor(elementRef:ElementRef) {
this.$element = elementRef.nativeElement;
}
update() {
PS.update(this.$element);
}
ngOnInit() {
requestAnimationFrame(() => PS.initialize(this.$element, {
wheelSpeed: 2,
wheelPropagation: false,
minScrollbarLength: 20,
suppressScrollX: true
}));
}
ngOnDestroy() {
PS.destroy(this.$element);
}
}

View File

@ -7,10 +7,11 @@ import { CopyButton } from './CopyButton/copy-button.directive';
import { SelectOnClick } from './SelectOnClick/select-on-click.directive'; import { SelectOnClick } from './SelectOnClick/select-on-click.directive';
import { DynamicNg2Viewer, DynamicNg2Wrapper } from './DynamicNg2Viewer/dynamic-ng2-viewer.component'; import { DynamicNg2Viewer, DynamicNg2Wrapper } from './DynamicNg2Viewer/dynamic-ng2-viewer.component';
import { LazyFor, LazyTasksService, LazyTasksServiceSync } from './LazyFor/lazy-for'; import { LazyFor, LazyTasksService, LazyTasksServiceSync } from './LazyFor/lazy-for';
import { PerfectScrollbar } from './PerfectScrollbar/perfect-scrollbar';
export const REDOC_COMMON_DIRECTIVES = [ export const REDOC_COMMON_DIRECTIVES = [
DropDown, StickySidebar, Tabs, Tab, Zippy, CopyButton, SelectOnClick, DynamicNg2Viewer, DynamicNg2Wrapper, LazyFor PerfectScrollbar, DropDown, StickySidebar, Tabs, Tab, Zippy, CopyButton, SelectOnClick, DynamicNg2Viewer, DynamicNg2Wrapper, LazyFor
]; ];
export { DropDown, StickySidebar, Tabs, Tab, Zippy, CopyButton, SelectOnClick, DynamicNg2Viewer, DynamicNg2Wrapper, LazyFor } export { DropDown, StickySidebar, Tabs, Tab, Zippy, CopyButton, SelectOnClick, DynamicNg2Viewer, DynamicNg2Wrapper, LazyFor }
export { LazyTasksService, LazyTasksServiceSync } export { LazyTasksService, LazyTasksServiceSync, PerfectScrollbar }