redoc/lib/services/menu.service.ts

411 lines
10 KiB
TypeScript
Raw Normal View History

2016-05-07 10:54:44 +03:00
'use strict';
import { Injectable, EventEmitter } from '@angular/core';
2016-12-19 18:13:39 +03:00
import { Subscription } from 'rxjs/Subscription';
2016-11-23 02:23:32 +03:00
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { ScrollService, INVIEW_POSITION } from './scroll.service';
import { Hash } from './hash.service';
2016-10-23 20:18:42 +03:00
import { SpecManager } from '../utils/spec-manager';
2016-12-25 18:14:31 +03:00
import { SchemaHelper } from './schema-helper.service';
2016-11-23 02:23:32 +03:00
import { AppStateService } from './app-state.service';
import { LazyTasksService } from '../shared/components/LazyFor/lazy-for';
import { JsonPointer } from '../utils/JsonPointer';
import * as slugify from 'slugify';
2016-05-07 10:54:44 +03:00
const CHANGE = {
NEXT : 1,
BACK : -1,
};
2016-12-25 18:14:31 +03:00
interface TagGroup {
name: string;
tags: string[];
}
export interface MenuItem {
id: string;
name: string;
description?: string;
items?: Array<MenuItem>;
parent?: MenuItem;
active?: boolean;
ready?: boolean;
level?: number;
flatIdx?: number;
metadata?: any;
isGroup?: boolean;
}
2016-05-07 10:54:44 +03:00
@Injectable()
export class MenuService {
changed: EventEmitter<any> = new EventEmitter();
2016-12-25 15:24:58 +03:00
items: MenuItem[];
activeIdx: number = -1;
2016-12-25 15:24:58 +03:00
private _flatItems: MenuItem[];
2016-12-19 18:36:44 +03:00
private _hashSubscription: Subscription;
2016-12-25 15:24:58 +03:00
private _scrollSubscription: Subscription;
2016-12-25 18:14:31 +03:00
private _tagsWithMethods: any;
2016-12-19 18:36:44 +03:00
2016-11-23 02:23:32 +03:00
constructor(
private hash:Hash,
private tasks: LazyTasksService,
private scrollService: ScrollService,
private appState: AppStateService,
2016-12-25 15:24:58 +03:00
private specMgr:SpecManager
2016-11-23 02:23:32 +03:00
) {
2016-05-07 10:54:44 +03:00
this.hash = hash;
2016-12-25 15:24:58 +03:00
this.buildMenu();
2016-05-07 10:54:44 +03:00
2016-12-25 15:24:58 +03:00
this._scrollSubscription = scrollService.scroll.subscribe((evt) => {
this.onScroll(evt.isScrolledDown);
2016-05-07 10:54:44 +03:00
});
2016-12-19 18:13:39 +03:00
this._hashSubscription = this.hash.value.subscribe((hash) => {
2016-12-25 15:24:58 +03:00
this.onHashChange(hash);
2016-05-09 22:55:16 +03:00
});
2016-05-07 10:54:44 +03:00
}
2016-12-25 15:24:58 +03:00
get flatItems():MenuItem[] {
if (!this._flatItems) {
this._flatItems = this.flatMenu();
}
return this._flatItems;
}
enableItem(idx) {
let item = this.flatItems[idx];
item.ready = true;
if (item.parent) {
item.parent.ready = true;
idx = item.parent.flatIdx;
2016-11-28 12:14:08 +03:00
}
2016-12-25 15:24:58 +03:00
// check if previous items can be enabled
let prevItem = this.flatItems[idx -= 1];
while(prevItem && (!prevItem.metadata || !prevItem.items)) {
prevItem.ready = true;
prevItem = this.flatItems[idx -= 1];
2016-11-23 02:23:32 +03:00
}
this.changed.next();
2016-11-23 02:23:32 +03:00
}
2016-12-25 15:24:58 +03:00
onScroll(isScrolledDown) {
2016-05-07 10:54:44 +03:00
let stable = false;
while(!stable) {
if(isScrolledDown) {
let $nextEl = this.getEl(this.activeIdx + 1);
2016-11-23 02:23:32 +03:00
if (!$nextEl) return;
let nextInViewPos = this.scrollService.getElementPos($nextEl, true);
if (nextInViewPos === INVIEW_POSITION.ABOVE) {
stable = this.changeActive(CHANGE.NEXT);
continue;
}
2016-05-07 10:54:44 +03:00
}
let $currentEl = this.getCurrentEl();
if (!$currentEl) return;
var elementInViewPos = this.scrollService.getElementPos($currentEl);
2016-05-07 10:54:44 +03:00
if(!isScrolledDown && elementInViewPos === INVIEW_POSITION.ABOVE ) {
stable = this.changeActive(CHANGE.BACK);
continue;
}
stable = true;
}
}
2016-12-25 15:24:58 +03:00
onHashChange(hash?: string) {
if (hash == undefined) return;
let activated = this.activateByHash(hash);
if (!this.tasks.empty) {
this.tasks.start(this.activeIdx, this);
this.scrollService.setStickElement(this.getCurrentEl());
if (activated) this.scrollToActive();
this.appState.stopLoading();
} else {
if (activated) this.scrollToActive();
}
}
getEl(flatIdx:number):Element {
if (flatIdx < 0) return null;
let currentItem = this.flatItems[flatIdx];
2017-01-06 13:55:50 +03:00
if (!currentItem) return;
2016-12-25 18:14:31 +03:00
if (currentItem.isGroup) currentItem = this.flatItems[flatIdx + 1];
let selector = '';
while(currentItem) {
2016-12-25 18:14:31 +03:00
if (currentItem.id) {
2016-12-25 18:16:33 +03:00
selector = `[section="${currentItem.id}"] ` + selector;
// We only need to go up the chain for methods that
// might have multiple tags. For headers/subheaders
// we need to siply early terminate.
if (!currentItem.metadata) {
break;
}
2016-12-25 18:14:31 +03:00
}
currentItem = currentItem.parent;
}
selector = selector.trim();
return selector ? document.querySelector(selector) : null;
2016-05-07 10:54:44 +03:00
}
2016-12-25 15:24:58 +03:00
getCurrentEl():Element {
return this.getEl(this.activeIdx);
2016-05-07 10:54:44 +03:00
}
deactivate(idx) {
if (idx < 0) return;
2016-05-07 10:54:44 +03:00
2016-12-25 15:24:58 +03:00
let item = this.flatItems[idx];
item.active = false;
2016-12-25 18:14:31 +03:00
while (item.parent) {
2016-12-25 15:24:58 +03:00
item.parent.active = false;
2016-12-25 18:14:31 +03:00
item = item.parent;
2016-05-07 10:54:44 +03:00
}
}
2016-12-25 15:24:58 +03:00
activate(idx, force = false) {
let item = this.flatItems[idx];
if (!force && item && !item.ready) return;
this.deactivate(this.activeIdx);
this.activeIdx = idx;
if (idx < 0) return;
2016-05-07 10:54:44 +03:00
2016-12-25 15:24:58 +03:00
item.active = true;
2016-05-07 10:54:44 +03:00
2016-12-25 18:14:31 +03:00
let cItem = item;
while (cItem.parent) {
cItem.parent.active = true;
cItem = cItem.parent;
}
2016-12-25 15:24:58 +03:00
this.changed.next(item);
2016-05-07 10:54:44 +03:00
}
2016-12-25 15:24:58 +03:00
changeActive(offset = 1):boolean {
let noChange = (this.activeIdx <= 0 && offset === -1) ||
(this.activeIdx === this.flatItems.length - 1 && offset === 1);
this.activate(this.activeIdx + offset);
return noChange;
2016-05-07 10:54:44 +03:00
}
scrollToActive() {
2016-12-25 15:24:58 +03:00
let $el = this.getCurrentEl();
if ($el) this.scrollService.scrollTo($el);
2016-05-07 10:54:44 +03:00
}
2016-12-25 15:24:58 +03:00
activateByHash(hash):boolean {
if (!hash) return;
let idx = 0;
2016-05-07 10:54:44 +03:00
hash = hash.substr(1);
let namespace = hash.split('/')[0];
let ptr = decodeURIComponent(hash.substr(namespace.length + 1));
2016-11-23 02:23:32 +03:00
if (namespace === 'section' || namespace === 'tag') {
2016-07-26 12:03:15 +03:00
let sectionId = ptr.split('/')[0];
ptr = ptr.substr(sectionId.length) || null;
2016-12-25 15:24:58 +03:00
let searchId;
if (namespace === 'section') {
searchId = hash;
} else {
searchId = ptr || (namespace + '/' + sectionId);;
}
idx = this.flatItems.findIndex(item => item.id === searchId);
2016-12-25 15:24:58 +03:00
if (idx < 0) this.tryScrollToId(searchId);
} else if (namespace === 'operation') {
idx = this.flatItems.findIndex(item => {
2016-12-25 18:16:33 +03:00
return item.metadata && item.metadata.operationId === ptr;
});
2016-05-07 10:54:44 +03:00
}
2016-12-25 15:24:58 +03:00
this.activate(idx, true);
return idx >= 0;
}
tryScrollToId(id) {
let $el = document.querySelector(`[section="${id}"]`);
if ($el) this.scrollService.scrollTo($el);
}
addMarkdownItems() {
let schema = this.specMgr.schema;
for (let header of (<Array<string>>(schema.info && schema.info['x-redoc-markdown-headers'] || []))) {
let id = 'section/' + slugify(header);
let item = {
name: header,
id: id,
items: null
2016-12-25 18:16:33 +03:00
};
item.items = this.getMarkdownSubheaders(item);
2016-12-25 15:24:58 +03:00
this.items.push(item);
}
}
getMarkdownSubheaders(parent: MenuItem):MenuItem[] {
let res = [];
let schema = this.specMgr.schema;
for (let subheader of (<Array<string>>(schema.info && schema.info['x-redoc-markdown-subheaders'] || []))) {
let parts = subheader.split('/');
let header = parts[0];
if (parent.name !== header) {
continue;
}
let name = parts[1];
let id = parent.id + '/' + slugify(name);
let subItem = {
name: name,
id: id,
parent: parent
};
res.push(subItem);
}
return res;
}
2016-12-25 18:14:31 +03:00
getMethodsItems(parent: MenuItem, tag:any):MenuItem[] {
if (!tag.methods || !tag.methods.length) return null;
let res = [];
for (let method of tag.methods) {
let subItem = {
name: SchemaHelper.methodSummary(method),
id: method._pointer,
description: method.description,
metadata: {
type: 'method',
pointer: method._pointer,
operationId: method.operationId
},
parent: parent
2016-12-25 18:16:33 +03:00
};
2016-12-25 18:14:31 +03:00
res.push(subItem);
}
return res;
}
getTagsItems(parent: MenuItem, tagGroup:TagGroup = null):MenuItem[] {
2016-12-25 15:24:58 +03:00
let schema = this.specMgr.schema;
let tags;
if (!tagGroup) {
2016-12-25 18:14:31 +03:00
// all tags
tags = Object.keys(this._tagsWithMethods);
} else {
tags = tagGroup.tags;
2016-12-25 18:14:31 +03:00
}
tags = tags.map(k => {
if (!this._tagsWithMethods[k]) {
console.warn(`Non-existing tag "${k}" is specified in tag group "${tagGroup.name}"`);
return null;
}
this._tagsWithMethods[k].used = true;
return this._tagsWithMethods[k];
});
2016-12-25 18:14:31 +03:00
let res = [];
2016-12-25 15:24:58 +03:00
for (let tag of tags || []) {
if (!tag) continue;
2016-12-25 15:24:58 +03:00
let id = 'tag/' + slugify(tag.name);
let item: MenuItem;
// don't put empty tag into menu, instead put their methods
2016-12-25 18:14:31 +03:00
if (tag.name === '') {
let items = this.getMethodsItems(null, tag);
res.push(...items);
continue;
2016-12-25 15:24:58 +03:00
}
2016-12-25 18:14:31 +03:00
item = {
name: tag['x-displayName'] || tag.name,
id: id,
description: tag.description,
metadata: { type: 'tag' },
parent: parent,
items: null
};
item.items = this.getMethodsItems(item, tag);
res.push(item);
}
return res;
}
2016-12-25 15:24:58 +03:00
2016-12-25 18:14:31 +03:00
getTagGroupsItems(parent: MenuItem, groups: TagGroup[]):MenuItem[] {
let res = [];
for (let group of groups) {
let item;
item = {
name: group.name,
id: null,
description: '',
parent: parent,
isGroup: true,
items: null
};
item.items = this.getTagsItems(item, group);
2016-12-25 18:14:31 +03:00
res.push(item);
2016-12-25 15:24:58 +03:00
}
this.checkAllTagsUsedInGroups();
2016-12-25 18:14:31 +03:00
return res;
2016-12-25 15:24:58 +03:00
}
checkAllTagsUsedInGroups() {
for (let tag of Object.keys(this._tagsWithMethods)) {
if (!this._tagsWithMethods[tag].used) {
console.warn(`Tag "${tag}" is not added to any group`);
}
}
}
2016-12-25 15:24:58 +03:00
buildMenu() {
2016-12-25 18:14:31 +03:00
this._tagsWithMethods = SchemaHelper.getTagsWithMethods(this.specMgr.schema);
2016-12-25 15:24:58 +03:00
this.items = this.items || [];
this.addMarkdownItems();
2016-12-25 18:14:31 +03:00
if (this.specMgr.schema['x-tagGroups']) {
this.items.push(...this.getTagGroupsItems(null, this.specMgr.schema['x-tagGroups']));
} else {
this.items.push(...this.getTagsItems(null));
}
2016-12-25 15:24:58 +03:00
}
flatMenu():MenuItem[] {
let menu = this.items;
let res = [];
2016-12-25 18:14:31 +03:00
let curDepth = 1;
2016-12-25 15:24:58 +03:00
2016-12-25 18:14:31 +03:00
let recursive = (items) => {
2016-12-25 15:24:58 +03:00
for (let item of items) {
res.push(item);
2016-12-25 18:14:31 +03:00
item.depth = item.isGroup ? 0 : curDepth;
2016-12-25 15:24:58 +03:00
item.flatIdx = res.length - 1;
if (item.items) {
2016-12-25 18:14:31 +03:00
if (!item.isGroup) curDepth++;
2016-12-25 15:24:58 +03:00
recursive(item.items);
2016-12-25 18:14:31 +03:00
if (!item.isGroup) curDepth--;
2016-12-25 15:24:58 +03:00
}
}
2016-12-25 18:16:33 +03:00
};
2016-12-25 15:24:58 +03:00
recursive(menu);
return res;
2016-05-07 10:54:44 +03:00
}
2016-12-19 18:13:39 +03:00
destroy() {
this._hashSubscription.unsubscribe();
2016-12-25 15:24:58 +03:00
this._scrollSubscription.unsubscribe();
2016-12-19 18:13:39 +03:00
}
2016-05-07 10:54:44 +03:00
}