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';
|
2016-05-25 18:34:31 +03:00
|
|
|
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';
|
2016-12-23 01:43:56 +03:00
|
|
|
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 {
|
2016-05-25 18:34:31 +03:00
|
|
|
changed: EventEmitter<any> = new EventEmitter();
|
|
|
|
|
2016-12-25 15:24:58 +03:00
|
|
|
items: MenuItem[];
|
2016-12-23 01:43:56 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-12-23 01:43:56 +03:00
|
|
|
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-11-24 19:12:43 +03:00
|
|
|
|
2016-12-25 15:24:58 +03:00
|
|
|
// check if previous items can be enabled
|
2016-12-23 01:43:56 +03:00
|
|
|
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
|
|
|
}
|
2016-12-23 01:43:56 +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) {
|
2016-10-31 14:34:42 +03:00
|
|
|
if(isScrolledDown) {
|
2016-12-23 01:43:56 +03:00
|
|
|
let $nextEl = this.getEl(this.activeIdx + 1);
|
2016-11-23 02:23:32 +03:00
|
|
|
if (!$nextEl) return;
|
2016-10-31 14:34:42 +03:00
|
|
|
let nextInViewPos = this.scrollService.getElementPos($nextEl, true);
|
2016-12-23 01:43:56 +03:00
|
|
|
if (nextInViewPos === INVIEW_POSITION.ABOVE) {
|
2016-10-31 14:34:42 +03:00
|
|
|
stable = this.changeActive(CHANGE.NEXT);
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-07 10:54:44 +03:00
|
|
|
}
|
2016-12-23 01:43:56 +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;
|
2016-12-23 01:43:56 +03:00
|
|
|
let currentItem = this.flatItems[flatIdx];
|
2016-12-25 18:14:31 +03:00
|
|
|
|
|
|
|
if (currentItem.isGroup) currentItem = this.flatItems[flatIdx + 1];
|
|
|
|
|
2016-12-23 01:43:56 +03:00
|
|
|
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;
|
2016-12-25 18:14:31 +03:00
|
|
|
}
|
2016-12-23 01:43:56 +03:00
|
|
|
currentItem = currentItem.parent;
|
2016-10-31 14:34:42 +03:00
|
|
|
}
|
2016-12-23 01:43:56 +03:00
|
|
|
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 {
|
2016-12-23 01:43:56 +03:00
|
|
|
return this.getEl(this.activeIdx);
|
2016-05-07 10:54:44 +03:00
|
|
|
}
|
|
|
|
|
2016-12-23 01:43:56 +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;
|
|
|
|
|
2016-12-23 01:43:56 +03:00
|
|
|
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 {
|
2016-12-23 01:43:56 +03:00
|
|
|
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 {
|
2016-12-23 01:43:56 +03:00
|
|
|
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);;
|
|
|
|
}
|
|
|
|
|
2016-12-23 01:43:56 +03:00
|
|
|
idx = this.flatItems.findIndex(item => item.id === searchId);
|
2016-12-25 15:24:58 +03:00
|
|
|
if (idx < 0) this.tryScrollToId(searchId);
|
2016-12-23 01:43:56 +03:00
|
|
|
} 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
|
2016-12-25 18:16:33 +03:00
|
|
|
};
|
2016-12-25 15:24:58 +03:00
|
|
|
this.items.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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, tagsGroup:string[] = null):MenuItem[] {
|
2016-12-25 15:24:58 +03:00
|
|
|
let schema = this.specMgr.schema;
|
|
|
|
|
2016-12-25 18:14:31 +03:00
|
|
|
if (!tagsGroup) {
|
|
|
|
// all tags
|
|
|
|
tagsGroup = Object.keys(this._tagsWithMethods);
|
|
|
|
}
|
|
|
|
|
|
|
|
let tags = tagsGroup.map(k => this._tagsWithMethods[k]);
|
|
|
|
|
|
|
|
let res = [];
|
2016-12-25 15:24:58 +03:00
|
|
|
for (let tag of tags || []) {
|
|
|
|
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.tags);
|
|
|
|
res.push(item);
|
2016-12-25 15:24:58 +03:00
|
|
|
}
|
2016-12-25 18:14:31 +03:00
|
|
|
return res;
|
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
|
|
|
}
|