2015-10-15 20:06:16 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import {RedocComponent, BaseComponent} from '../base';
|
2015-12-14 18:20:40 +03:00
|
|
|
import {NgZone, ChangeDetectionStrategy} from 'angular2/core';
|
2015-11-27 01:43:01 +03:00
|
|
|
import {redocEvents} from '../../events';
|
2015-12-19 20:26:23 +03:00
|
|
|
import detectScollParent from 'scrollparent';
|
2015-10-15 20:06:16 +03:00
|
|
|
|
2015-12-14 18:20:40 +03:00
|
|
|
import {BrowserDomAdapter} from 'angular2/platform/browser';
|
2015-12-19 20:26:23 +03:00
|
|
|
import {global} from 'angular2/src/facade/lang';
|
2015-12-14 18:20:40 +03:00
|
|
|
|
2015-10-15 20:06:33 +03:00
|
|
|
const CHANGE = {
|
|
|
|
NEXT : 1,
|
|
|
|
BACK : -1,
|
2015-10-18 19:32:11 +03:00
|
|
|
INITIAL : 0
|
2015-10-15 20:06:33 +03:00
|
|
|
};
|
|
|
|
|
2015-11-27 00:30:13 +03:00
|
|
|
const INVIEW_POSITION = {
|
|
|
|
ABOVE : 1,
|
|
|
|
BELLOW: -1,
|
|
|
|
INVIEW: 0
|
|
|
|
};
|
|
|
|
|
2015-10-15 20:06:16 +03:00
|
|
|
@RedocComponent({
|
|
|
|
selector: 'side-menu',
|
|
|
|
templateUrl: './lib/components/SideMenu/side-menu.html',
|
2015-11-29 13:47:42 +03:00
|
|
|
styleUrls: ['./lib/components/SideMenu/side-menu.css'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.Default
|
2015-10-15 20:06:16 +03:00
|
|
|
})
|
2015-10-27 20:44:08 +03:00
|
|
|
export default class SideMenu extends BaseComponent {
|
2015-10-15 20:06:16 +03:00
|
|
|
constructor(schemaMgr, zone) {
|
|
|
|
super(schemaMgr);
|
|
|
|
this.zone = zone;
|
2015-12-14 18:20:40 +03:00
|
|
|
this.adapter = new BrowserDomAdapter();
|
2015-10-15 21:35:05 +03:00
|
|
|
|
2015-12-19 20:26:23 +03:00
|
|
|
this.scrollParent = detectScollParent(document.querySelector('side-menu'));
|
2015-11-29 20:18:01 +03:00
|
|
|
|
2015-10-15 21:35:05 +03:00
|
|
|
// for some reason constructor is not run inside zone
|
|
|
|
// as workaround running it manually
|
2015-10-15 20:06:16 +03:00
|
|
|
this.zone.run(() => {
|
2015-11-29 20:18:01 +03:00
|
|
|
this.bindEvents();
|
2015-10-15 20:06:16 +03:00
|
|
|
});
|
|
|
|
this.activeCatIdx = 0;
|
2015-10-30 11:26:23 +03:00
|
|
|
this.activeMethodIdx = -1;
|
2015-10-15 20:06:16 +03:00
|
|
|
this.prevOffsetY = null;
|
2015-11-27 01:43:01 +03:00
|
|
|
|
|
|
|
redocEvents.bootstrapped.subscribe(() => this.hashScroll());
|
|
|
|
}
|
|
|
|
|
2015-11-29 20:18:01 +03:00
|
|
|
scrollY() {
|
|
|
|
return (this.scrollParent.scrollY != null) ? this.scrollParent.scrollY : this.scrollParent.scrollTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
hashScroll(evt) {
|
2015-12-19 20:26:23 +03:00
|
|
|
let hash = global.location.hash;
|
2015-11-27 01:43:01 +03:00
|
|
|
if (!hash) return;
|
|
|
|
|
|
|
|
hash = hash.substr(1);
|
|
|
|
let tag = hash.split('/')[0];
|
|
|
|
let ptr = hash.substr(tag.length);
|
|
|
|
let el = this.getMethodEl(ptr, tag);
|
|
|
|
if (el) this.scrollTo(el);
|
2015-11-29 20:18:01 +03:00
|
|
|
if (evt) evt.preventDefault();
|
2015-10-15 20:06:16 +03:00
|
|
|
}
|
|
|
|
|
2015-11-29 20:18:01 +03:00
|
|
|
bindEvents() {
|
|
|
|
this.prevOffsetY = this.scrollY();
|
2015-11-27 00:30:13 +03:00
|
|
|
let view = document.querySelector('redoc');
|
|
|
|
this.viewBoxTop = view.offsetTop;
|
2015-12-14 18:20:40 +03:00
|
|
|
this.adapter.on(this.scrollParent, 'scroll', () => this.scrollHandler());
|
2015-12-19 20:26:23 +03:00
|
|
|
this.adapter.on(global, 'hashchange', evt => this.hashScroll(evt));
|
2015-10-15 21:35:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
activateAndScroll(idx, methodIdx) {
|
|
|
|
this.activate(idx, methodIdx);
|
|
|
|
this.scrollToActive();
|
|
|
|
}
|
|
|
|
|
2015-11-27 01:43:01 +03:00
|
|
|
scrollTo(el) {
|
|
|
|
let subjRect = el.getBoundingClientRect();
|
2015-11-29 20:18:01 +03:00
|
|
|
let offset = this.scrollY() + subjRect.top - this.viewBoxTop + 1;
|
|
|
|
if (this.scrollParent.scrollTo) {
|
|
|
|
this.scrollParent.scrollTo(0, offset);
|
|
|
|
} else {
|
|
|
|
this.scrollParent.scrollTop = offset;
|
|
|
|
}
|
2015-10-15 21:35:05 +03:00
|
|
|
}
|
|
|
|
|
2015-11-27 01:43:01 +03:00
|
|
|
scrollToActive() {
|
|
|
|
this.scrollTo(this.getCurrentMethodEl());
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:35:05 +03:00
|
|
|
activate(catIdx, methodIdx) {
|
|
|
|
let menu = this.data.menu;
|
|
|
|
menu[this.activeCatIdx].active = false;
|
2015-10-30 11:26:23 +03:00
|
|
|
if (menu[this.activeCatIdx].methods.length) {
|
|
|
|
if (this.activeMethodIdx >= 0) {
|
|
|
|
menu[this.activeCatIdx].methods[this.activeMethodIdx].active = false;
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 21:35:05 +03:00
|
|
|
|
|
|
|
this.activeCatIdx = catIdx;
|
|
|
|
this.activeMethodIdx = methodIdx;
|
|
|
|
menu[catIdx].active = true;
|
2015-10-30 11:26:23 +03:00
|
|
|
this.activeMethodPtr = null;
|
|
|
|
if (menu[catIdx].methods.length && (methodIdx > -1)) {
|
|
|
|
let currentItem = menu[catIdx].methods[methodIdx];
|
|
|
|
currentItem.active = true;
|
|
|
|
this.activeMethodPtr = currentItem.pointer;
|
|
|
|
}
|
2015-10-15 21:35:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_calcActiveIndexes(offset) {
|
2015-10-15 20:06:16 +03:00
|
|
|
let menu = this.data.menu;
|
|
|
|
let catCount = menu.length;
|
|
|
|
let catLength = menu[this.activeCatIdx].methods.length;
|
|
|
|
|
2015-10-15 21:35:05 +03:00
|
|
|
let resMethodIdx = this.activeMethodIdx + offset;
|
|
|
|
let resCatIdx = this.activeCatIdx;
|
|
|
|
|
|
|
|
if (resMethodIdx > catLength - 1) {
|
|
|
|
resCatIdx++;
|
2015-10-30 11:26:23 +03:00
|
|
|
resMethodIdx = -1;
|
2015-10-15 20:06:16 +03:00
|
|
|
}
|
2015-10-30 11:26:23 +03:00
|
|
|
if (resMethodIdx < -1) {
|
2015-10-15 21:35:05 +03:00
|
|
|
let prevCatIdx = --resCatIdx;
|
2015-10-15 20:06:16 +03:00
|
|
|
catLength = menu[Math.max(prevCatIdx, 0)].methods.length;
|
2015-10-15 21:35:05 +03:00
|
|
|
resMethodIdx = catLength - 1;
|
2015-10-15 20:06:16 +03:00
|
|
|
}
|
2015-10-15 21:35:05 +03:00
|
|
|
if (resCatIdx > catCount - 1) {
|
|
|
|
resCatIdx = catCount - 1;
|
|
|
|
resMethodIdx = catLength - 1;
|
2015-10-15 20:06:16 +03:00
|
|
|
}
|
2015-10-15 21:35:05 +03:00
|
|
|
if (resCatIdx < 0) {
|
|
|
|
resCatIdx = 0;
|
|
|
|
resMethodIdx = 0;
|
2015-10-15 20:06:16 +03:00
|
|
|
}
|
2015-10-15 21:35:05 +03:00
|
|
|
|
|
|
|
return [resCatIdx, resMethodIdx];
|
2015-10-15 20:06:16 +03:00
|
|
|
}
|
2015-10-15 20:06:33 +03:00
|
|
|
|
|
|
|
changeActive(offset = 1) {
|
2015-10-15 21:35:05 +03:00
|
|
|
let [catIdx, methodIdx] = this._calcActiveIndexes(offset);
|
|
|
|
this.activate(catIdx, methodIdx);
|
2015-10-30 12:44:40 +03:00
|
|
|
return (methodIdx === 0 && catIdx === 0);
|
2015-10-15 21:35:05 +03:00
|
|
|
}
|
2015-10-15 20:06:16 +03:00
|
|
|
|
2015-11-27 01:43:01 +03:00
|
|
|
getMethodEl(ptr, tag) {
|
2015-10-30 11:26:23 +03:00
|
|
|
let selector = ptr ? `[pointer="${ptr}"][tag="${tag}"]` : `[tag="${tag}"]`;
|
|
|
|
return document.querySelector(selector);
|
2015-10-15 20:06:16 +03:00
|
|
|
}
|
|
|
|
|
2015-11-27 01:43:01 +03:00
|
|
|
getCurrentMethodEl() {
|
|
|
|
return this.getMethodEl(this.activeMethodPtr, this.data.menu[this.activeCatIdx].name);
|
|
|
|
}
|
|
|
|
|
2015-11-27 00:30:13 +03:00
|
|
|
/* returns 1 if element if above the view, 0 if in view and -1 below the view */
|
|
|
|
getElementInViewPos(el) {
|
|
|
|
if (Math.floor(el.getBoundingClientRect().top) > this.viewBoxTop) {
|
|
|
|
return INVIEW_POSITION.ABOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (el.getBoundingClientRect().bottom <= this.viewBoxTop) {
|
|
|
|
return INVIEW_POSITION.BELLOW;
|
|
|
|
}
|
|
|
|
return INVIEW_POSITION.INVIEW;
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:06:16 +03:00
|
|
|
scrollHandler() {
|
2015-11-29 20:18:01 +03:00
|
|
|
let isScrolledDown = (this.scrollY() - this.prevOffsetY > 0);
|
|
|
|
this.prevOffsetY = this.scrollY();
|
2015-10-30 12:44:40 +03:00
|
|
|
let stable = false;
|
|
|
|
while(!stable) {
|
2015-11-27 01:43:01 +03:00
|
|
|
let activeMethodHost = this.getCurrentMethodEl();
|
2015-10-30 12:44:40 +03:00
|
|
|
if (!activeMethodHost) return;
|
2015-11-27 00:30:13 +03:00
|
|
|
var elementInViewPos = this.getElementInViewPos(activeMethodHost);
|
|
|
|
if(isScrolledDown && elementInViewPos === INVIEW_POSITION.BELLOW) {
|
2015-10-30 12:44:40 +03:00
|
|
|
stable = this.changeActive(CHANGE.NEXT);
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-27 00:30:13 +03:00
|
|
|
if(!isScrolledDown && elementInViewPos === INVIEW_POSITION.ABOVE ) {
|
2015-10-30 12:44:40 +03:00
|
|
|
stable = this.changeActive(CHANGE.BACK);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
stable = true;
|
2015-10-15 20:06:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareModel() {
|
|
|
|
this.data = {};
|
2015-10-18 19:32:11 +03:00
|
|
|
this.data.menu = Array.from(this.schemaMgr.buildMenuTree().entries()).map(
|
2015-10-29 00:22:12 +03:00
|
|
|
el => ({name: el[0], description: el[1].description, methods: el[1].methods})
|
2015-10-15 20:06:16 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-18 19:32:11 +03:00
|
|
|
init() {
|
|
|
|
this.changeActive(CHANGE.INITIAL);
|
2015-10-15 20:06:16 +03:00
|
|
|
}
|
|
|
|
}
|
2015-10-27 20:44:08 +03:00
|
|
|
SideMenu.parameters = SideMenu.parameters.concat([NgZone]);
|