Fix side menu items wrong sync with description headers

This commit is contained in:
Roman Hotsiy 2016-10-31 13:34:42 +02:00
parent 9955cb8fec
commit ca654511ae
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 32 additions and 6 deletions

View File

@ -41,10 +41,15 @@ export class MenuService {
let $activeMethodHost = this.getCurrentMethodEl();
if (!$activeMethodHost) return;
var elementInViewPos = this.scrollService.getElementPos($activeMethodHost);
if(isScrolledDown && elementInViewPos === INVIEW_POSITION.BELLOW) {
if(isScrolledDown) {
//&& elementInViewPos === INVIEW_POSITION.BELLOW
let $nextEl = this.getRelativeCatOrItem(1);
let nextInViewPos = this.scrollService.getElementPos($nextEl, true);
if (elementInViewPos === INVIEW_POSITION.BELLOW && nextInViewPos === INVIEW_POSITION.ABOVE) {
stable = this.changeActive(CHANGE.NEXT);
continue;
}
}
if(!isScrolledDown && elementInViewPos === INVIEW_POSITION.ABOVE ) {
stable = this.changeActive(CHANGE.BACK);
continue;
@ -53,6 +58,25 @@ export class MenuService {
}
}
getRelativeCatOrItem(offset: number = 0) {
let ptr, cat;
cat = this.categories[this.activeCatIdx];
if (cat.methods.length === 0) {
ptr = null;
cat = this.categories[this.activeCatIdx + Math.sign(offset)] || cat;
} else {
let cat = this.categories[this.activeCatIdx];
let idx = this.activeMethodIdx + offset;
if ((idx >= cat.methods.length - 1) || idx < 0) {
cat = this.categories[this.activeCatIdx + Math.sign(offset)] || cat;
idx = offset > 0 ? -1 : cat.methods.length - 1;
}
ptr = cat.methods[idx] && cat.methods[idx].pointer;
}
return this.getMethodElByPtr(ptr, cat.id);
}
getCurrentMethodEl() {
return this.getMethodElByPtr(this.activeMethodPtr,
this.categories[this.activeCatIdx].id);

View File

@ -14,6 +14,7 @@ export interface MenuMethod {
active: boolean;
summary: string;
tag: string;
pointer: string;
}
export interface MenuCategory {

View File

@ -29,13 +29,14 @@ export class ScrollService {
}
/* returns 1 if element if above the view, 0 if in view and -1 below the view */
getElementPos($el) {
getElementPos($el, inverted=false) {
let scrollYOffset = this.scrollYOffset();
if (Math.floor($el.getBoundingClientRect().top) > scrollYOffset) {
let mul = inverted ? -1 : 1;
if (mul*Math.floor($el.getBoundingClientRect().top) > mul*scrollYOffset) {
return INVIEW_POSITION.ABOVE;
}
if ($el.getBoundingClientRect().bottom <= scrollYOffset) {
if (mul*$el.getBoundingClientRect().bottom <= mul*scrollYOffset) {
return INVIEW_POSITION.BELLOW;
}
return INVIEW_POSITION.INVIEW;