redoc/lib/services/menu.service.ts

489 lines
13 KiB
TypeScript
Raw Permalink 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 { WarningsService } from './warnings.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';
2017-01-30 18:21:12 +03:00
import { JsonPointer, MarkdownHeading, StringMap } from '../utils/';
2018-01-21 22:51:38 +03:00
import slugify from 'slugify';
2016-05-07 10:54:44 +03:00
const CHANGE = {
NEXT : 1,
BACK : -1,
};
2017-04-18 16:37:05 +03:00
export interface TagGroup {
2016-12-25 18:14:31 +03:00
name: string;
tags: string[];
}
export interface MenuItem {
id: string;
name: string;
description?: string;
items?: Array<MenuItem>;
parent?: MenuItem;
active?: boolean;
ready?: boolean;
depth?: string|number;
2016-12-25 18:14:31 +03:00
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-29 20:20:29 +03:00
changedActiveItem: EventEmitter<any> = new EventEmitter();
2016-12-25 15:24:58 +03:00
items: MenuItem[];
activeIdx: number = -1;
2017-04-19 12:41:38 +03:00
public domRoot: Document | Element = document;
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;
private _progressSubscription: Subscription;
2017-03-30 15:17:08 +03:00
private _tagsWithOperations: 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;
2017-04-18 16:37:05 +03:00
this.specMgr.spec.subscribe(spec => {
if (!spec) return;
this.buildMenu();
2017-04-19 12:41:38 +03:00
});
2016-05-07 10:54:44 +03:00
this.subscribe();
}
subscribe() {
this._scrollSubscription = this.scrollService.scroll.subscribe((evt) => {
2016-12-25 15:24:58 +03:00
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
});
this._progressSubscription = this.tasks.loadProgress.subscribe(progress => {
if (progress === 100) {
this.makeSureLastItemsEnabled();
}
});
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
}
// check if previous items§ can be enabled
let prevItem = this.flatItems[idx -= 1];
while(prevItem && (!prevItem.metadata || prevItem.metadata.type === 'heading' || !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
}
makeSureLastItemsEnabled() {
let lastIdx = this.flatItems.length - 1;
2017-02-03 00:08:04 +03:00
let item = this.flatItems[lastIdx];
while(item && (!item.metadata || !item.items)) {
item.ready = true;
item = this.flatItems[lastIdx -= 1];
}
}
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.processed) {
2016-12-25 15:24:58 +03:00
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;
2016-12-29 20:20:29 +03:00
if (flatIdx > this.flatItems.length - 1) 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;
2017-03-30 15:17:08 +03:00
// We only need to go up the chain for operations that
// might have multiple tags. For headers/subheaders
// we need to siply early terminate.
if (!currentItem.metadata || currentItem.metadata.type === 'heading') {
break;
}
2016-12-25 18:14:31 +03:00
}
currentItem = currentItem.parent;
}
selector = selector.trim();
2017-04-18 16:37:05 +03:00
return selector ? this.domRoot.querySelector(selector) : null;
2016-05-07 10:54:44 +03:00
}
2017-01-23 18:08:32 +03:00
isTagOrGroupItem(flatIdx: number):boolean {
2016-12-29 20:20:29 +03:00
let item = this.flatItems[flatIdx];
2017-01-23 18:08:32 +03:00
return item && (item.isGroup || (item.metadata && item.metadata.type === 'tag'));
2016-12-29 20:20:29 +03:00
}
getTagInfoEl(flatIdx: number):Element {
2017-01-23 18:08:32 +03:00
if (!this.isTagOrGroupItem(flatIdx)) return null;
2016-12-29 20:20:29 +03:00
let el = this.getEl(flatIdx);
return el && el.querySelector('.tag-info');
}
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
}
}
2017-04-18 16:37:05 +03:00
activate(item:MenuItem, force = false, replaceState = false) {
2016-12-25 15:24:58 +03:00
if (!force && item && !item.ready) return;
this.deactivate(this.activeIdx);
2017-04-18 16:37:05 +03:00
this.activeIdx = item ? item.flatIdx : -1;
if (this.activeIdx < 0) {
this.hash.update('', replaceState);
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;
}
this.hash.update(this.hashFor(item.id, item.metadata, item.parent && item.parent.id), replaceState);
2016-12-29 20:20:29 +03:00
this.changedActiveItem.next(item);
2016-05-07 10:54:44 +03:00
}
2017-04-18 16:37:05 +03:00
activateByIdx(idx:number, force = false, replaceState = false) {
let item = this.flatItems[idx];
this.activate(item, force, replaceState);
}
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);
2017-04-18 16:37:05 +03:00
this.activateByIdx(this.activeIdx + offset, false, true);
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 {
2017-05-05 12:45:03 +03:00
searchId = ptr || (namespace + '/' + sectionId);
2016-12-25 15:24:58 +03:00
}
idx = this.flatItems.findIndex(item => item.id === searchId);
2017-03-30 15:17:08 +03:00
if (idx < 0) {
this.tryScrollToId(searchId);
return false;
}
} 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
}
2017-04-18 16:37:05 +03:00
this.activateByIdx(idx, true);
2016-12-25 15:24:58 +03:00
return idx >= 0;
}
tryScrollToId(id) {
2017-04-18 16:37:05 +03:00
let $el = this.domRoot.querySelector(`[section="${id}"]`);
2016-12-25 15:24:58 +03:00
if ($el) this.scrollService.scrollTo($el);
}
addMarkdownItems() {
let schema = this.specMgr.schema;
2017-01-30 19:59:57 +03:00
let headings:StringMap<MarkdownHeading> = schema.info && schema.info['x-redoc-markdown-headers'] || {};
2017-01-30 18:21:12 +03:00
Object.keys(headings).forEach(h => {
let heading = headings[h];
let id = 'section/' + heading.id;
2016-12-25 15:24:58 +03:00
let item = {
2017-01-30 18:21:12 +03:00
name: heading.title,
id: id,
items: null,
metadata: {
type: 'heading'
}
2016-12-25 18:16:33 +03:00
};
2017-01-30 18:21:12 +03:00
item.items = this.getMarkdownSubheaders(item, heading);
2016-12-25 15:24:58 +03:00
this.items.push(item);
2017-01-30 18:21:12 +03:00
});
2016-12-25 15:24:58 +03:00
}
2017-01-30 18:21:12 +03:00
getMarkdownSubheaders(parent: MenuItem, parentHeading: MarkdownHeading):MenuItem[] {
let res = [];
2017-01-30 18:21:12 +03:00
Object.keys(parentHeading.children || {}).forEach(h => {
let heading = parentHeading.children[h];
let id = 'section/' + heading.id;
let subItem = {
2017-01-30 18:21:12 +03:00
name: heading.title,
id: id,
parent: parent,
metadata: {
type: 'heading'
}
};
res.push(subItem);
2017-01-30 18:21:12 +03:00
});
return res;
}
2017-03-30 15:17:08 +03:00
getOperationsItems(parent: MenuItem, tag:any):MenuItem[] {
if (!tag.operations || !tag.operations.length) return null;
2016-12-25 18:14:31 +03:00
let res = [];
2017-04-18 16:37:05 +03:00
for (let operationInfo of tag.operations) {
2016-12-25 18:14:31 +03:00
let subItem = {
2017-04-18 16:37:05 +03:00
name: SchemaHelper.operationSummary(operationInfo),
id: operationInfo._pointer,
description: operationInfo.description,
2016-12-25 18:14:31 +03:00
metadata: {
2017-03-30 15:17:08 +03:00
type: 'operation',
2017-04-18 16:37:05 +03:00
pointer: operationInfo._pointer,
operationId: operationInfo.operationId,
operation: operationInfo.operation,
deprecated: !!operationInfo.deprecated
2016-12-25 18:14:31 +03:00
},
parent: parent
2016-12-25 18:16:33 +03:00
};
2016-12-25 18:14:31 +03:00
res.push(subItem);
}
return res;
}
hashFor(
id: string|null, itemMeta:
2017-04-18 16:37:05 +03:00
{operationId?: string, type: string, pointer?: string},
parentId?: string
) {
if (!id) return null;
2017-03-30 15:17:08 +03:00
if (itemMeta && itemMeta.type === 'operation') {
if (itemMeta.operationId) {
return 'operation/' + encodeURIComponent(itemMeta.operationId);
} else {
return parentId + encodeURIComponent(itemMeta.pointer);
}
} else {
return id;
}
}
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
2017-03-30 15:17:08 +03:00
tags = Object.keys(this._tagsWithOperations);
} else {
tags = tagGroup.tags;
2016-12-25 18:14:31 +03:00
}
tags = tags.map(k => {
2017-03-30 15:17:08 +03:00
if (!this._tagsWithOperations[k]) {
WarningsService.warn(`Non-existing tag "${k}" is added to the group "${tagGroup.name}"`);
return null;
}
2017-03-30 15:17:08 +03:00
this._tagsWithOperations[k].used = true;
return this._tagsWithOperations[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;
2017-03-30 15:17:08 +03:00
// don't put empty tag into menu, instead put their operations
2016-12-25 18:14:31 +03:00
if (tag.name === '') {
2017-03-30 15:17:08 +03:00
let items = this.getOperationsItems(null, tag);
2016-12-25 18:14:31 +03:00
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,
2017-02-03 00:08:04 +03:00
metadata: { type: 'tag', externalDocs: tag.externalDocs },
2016-12-25 18:14:31 +03:00
parent: parent,
items: null
};
2017-03-30 15:17:08 +03:00
item.items = this.getOperationsItems(item, tag);
2016-12-25 18:14:31 +03:00
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() {
2017-03-30 15:17:08 +03:00
for (let tag of Object.keys(this._tagsWithOperations)) {
if (!this._tagsWithOperations[tag].used) {
WarningsService.warn(`Tag "${tag}" is not added to any group`);
}
}
}
2016-12-25 15:24:58 +03:00
buildMenu() {
2017-03-30 15:17:08 +03:00
this._tagsWithOperations = SchemaHelper.getTagsWithOperations(this.specMgr.schema);
2016-12-25 18:14:31 +03:00
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;
2017-04-18 16:37:05 +03:00
if (!menu) return;
2016-12-25 15:24:58 +03:00
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
2017-01-24 00:29:52 +03:00
getItemById(id: string):MenuItem {
2017-01-30 19:59:57 +03:00
return this.flatItems.find(item => item.id === id || item.id === `section/${id}`);
2017-01-24 00:29:52 +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();
this._progressSubscription.unsubscribe();
2016-12-19 18:13:39 +03:00
}
2016-05-07 10:54:44 +03:00
}