redoc/lib/components/SideMenu/side-menu.js

88 lines
2.8 KiB
JavaScript
Raw Normal View History

2015-10-15 20:06:16 +03:00
'use strict';
2016-05-06 00:48:41 +03:00
import { ElementRef } from '@angular/core';
import { BrowserDomAdapter } from '@angular/platform-browser/src/browser/browser_adapter';
import { global } from '@angular/core/src/facade/lang';
2016-05-06 00:48:41 +03:00
import { RedocComponent, BaseComponent, SchemaManager } from '../base';
2016-05-07 10:54:44 +03:00
import { ScrollService, Hash, MenuService, OptionsService } from '../../services/index';
2015-11-27 00:30:13 +03:00
2015-10-15 20:06:16 +03:00
@RedocComponent({
selector: 'side-menu',
templateUrl: './lib/components/SideMenu/side-menu.html',
2016-05-07 10:54:44 +03:00
providers: [ScrollService, MenuService, Hash],
2016-05-05 11:05:02 +03:00
styleUrls: ['./lib/components/SideMenu/side-menu.css']
2015-10-15 20:06:16 +03:00
})
@Reflect.metadata('parameters', [[SchemaManager], [ElementRef],
2016-05-07 10:54:44 +03:00
[BrowserDomAdapter], [ScrollService], [MenuService], [Hash], [OptionsService]])
2016-05-06 00:48:41 +03:00
export class SideMenu extends BaseComponent {
2016-05-07 10:54:44 +03:00
constructor(schemaMgr, elementRef, dom, scrollService, menuService, hash, optionsService) {
2015-10-15 20:06:16 +03:00
super(schemaMgr);
this.$element = elementRef.nativeElement;
this.dom = dom;
2016-05-07 10:54:44 +03:00
this.scrollService = scrollService;
this.menuService = menuService;
this.hash = hash;
2016-01-21 18:26:54 +03:00
this.activeCatCaption = '';
this.activeItemCaption = '';
2015-11-27 01:43:01 +03:00
2016-05-07 10:54:44 +03:00
this.options = optionsService.options;
2016-05-05 11:05:02 +03:00
2016-05-07 10:54:44 +03:00
this.menuService.changed.subscribe(this.changed.bind(this));
}
2016-05-07 10:54:44 +03:00
changed(cat, item) {
this.activeCatCaption = cat.name || '';
this.activeItemCaption = item && item.summary || '';
2015-10-15 21:35:05 +03:00
}
activateAndScroll(idx, methodIdx) {
2016-01-21 18:26:54 +03:00
if (this.mobileMode()) {
this.toggleMobileNav();
}
2016-05-07 10:54:44 +03:00
this.menuService.activate(idx, methodIdx);
this.menuService.scrollToActive();
2016-02-07 17:10:32 +03:00
}
2016-05-07 10:54:44 +03:00
init() {
this.$mobileNav = this.dom.querySelector(this.$element, '.mobile-nav');
this.$resourcesNav = this.dom.querySelector(this.$element, '#resources-nav');
2015-11-27 00:30:13 +03:00
2016-05-07 10:54:44 +03:00
//decorate option.scrollYOffset to account mobile nav
var origOffset = this.options.scrollYOffset;
this.options.scrollYOffset = () => {
let mobileNavOffset = this.$mobileNav.clientHeight;
return origOffset() + mobileNavOffset;
};
2015-10-15 20:06:16 +03:00
}
prepareModel() {
this.data = {};
2016-05-07 10:54:44 +03:00
this.data.menu = this.menuService.categories;
2015-10-15 20:06:16 +03:00
}
2016-01-21 18:26:54 +03:00
mobileMode() {
return this.$mobileNav.clientHeight > 0;
2016-01-21 18:26:54 +03:00
}
toggleMobileNav() {
let dom = this.dom;
let $overflowParent = (this.$scrollParent === global) ? dom.defaultDoc().body : this.$scrollParent;
if (dom.hasStyle(this.$resourcesNav, 'height')) {
dom.removeStyle(this.$resourcesNav, 'height');
dom.removeStyle($overflowParent, 'overflow-y');
2016-01-21 18:26:54 +03:00
} else {
let viewportHeight = this.$scrollParent.innerHeight || this.$scrollParent.clientHeight;
let height = viewportHeight - this.$mobileNav.getBoundingClientRect().bottom;
dom.setStyle($overflowParent, 'overflow-y', 'hidden');
dom.setStyle(this.$resourcesNav, 'height', height + 'px');
2016-01-21 18:26:54 +03:00
}
}
2016-05-07 10:54:44 +03:00
destroy() {
this.scrollService.unbind();
this.hash.unbind();
}
2015-10-15 20:06:16 +03:00
}