MenuService refactoring

This commit is contained in:
Roman Hotsiy 2017-04-18 16:37:05 +03:00
parent 92eec250e6
commit 0ee1476590
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
6 changed files with 38 additions and 26 deletions

View File

@ -63,7 +63,6 @@ export class SideMenu implements OnInit, OnDestroy {
private menuService:MenuService, private menuService:MenuService,
optionsService:OptionsService, optionsService:OptionsService,
private detectorRef:ChangeDetectorRef, private detectorRef:ChangeDetectorRef,
//private marker:Marker
) { ) {
this.$element = elementRef.nativeElement; this.$element = elementRef.nativeElement;
@ -105,7 +104,7 @@ export class SideMenu implements OnInit, OnDestroy {
this.toggleMobileNav(); this.toggleMobileNav();
} }
this.menuService.activate(item.flatIdx); this.menuService.activate(item);
this.menuService.scrollToActive(); this.menuService.scrollToActive();
} }

View File

@ -10,7 +10,7 @@ import {
ComponentFactoryResolver ComponentFactoryResolver
} from '@angular/core'; } from '@angular/core';
type NodesOrComponents = HTMLElement | ComponentRef<any>; export type NodesOrComponents = HTMLElement | ComponentRef<any>;
export const COMPONENT_PARSER_ALLOWED = 'COMPONENT_PARSER_ALLOWED'; export const COMPONENT_PARSER_ALLOWED = 'COMPONENT_PARSER_ALLOWED';
const COMPONENT_REGEXP = '^\\s*<!-- ReDoc-Inject:\\s+?{component}\\s+?-->\\s*$'; const COMPONENT_REGEXP = '^\\s*<!-- ReDoc-Inject:\\s+?{component}\\s+?-->\\s*$';

View File

@ -18,7 +18,7 @@ const CHANGE = {
BACK : -1, BACK : -1,
}; };
interface TagGroup { export interface TagGroup {
name: string; name: string;
tags: string[]; tags: string[];
} }
@ -56,6 +56,8 @@ export class MenuService {
private _progressSubscription: Subscription; private _progressSubscription: Subscription;
private _tagsWithOperations: any; private _tagsWithOperations: any;
public domRoot: Document | Element = document;
constructor( constructor(
private hash:Hash, private hash:Hash,
private tasks: LazyTasksService, private tasks: LazyTasksService,
@ -64,7 +66,11 @@ export class MenuService {
private specMgr:SpecManager private specMgr:SpecManager
) { ) {
this.hash = hash; this.hash = hash;
this.buildMenu();
this.specMgr.spec.subscribe(spec => {
if (!spec) return;
this.buildMenu();
})
this._scrollSubscription = scrollService.scroll.subscribe((evt) => { this._scrollSubscription = scrollService.scroll.subscribe((evt) => {
this.onScroll(evt.isScrolledDown); this.onScroll(evt.isScrolledDown);
@ -172,7 +178,7 @@ export class MenuService {
currentItem = currentItem.parent; currentItem = currentItem.parent;
} }
selector = selector.trim(); selector = selector.trim();
return selector ? document.querySelector(selector) : null; return selector ? this.domRoot.querySelector(selector) : null;
} }
isTagOrGroupItem(flatIdx: number):boolean { isTagOrGroupItem(flatIdx: number):boolean {
@ -202,13 +208,12 @@ export class MenuService {
} }
} }
activate(idx, force = false, replaceState = false) { activate(item:MenuItem, force = false, replaceState = false) {
let item = this.flatItems[idx];
if (!force && item && !item.ready) return; if (!force && item && !item.ready) return;
this.deactivate(this.activeIdx); this.deactivate(this.activeIdx);
this.activeIdx = idx; this.activeIdx = item ? item.flatIdx : -1;
if (idx < 0) { if (this.activeIdx < 0) {
this.hash.update('', replaceState); this.hash.update('', replaceState);
return; return;
} }
@ -224,10 +229,15 @@ export class MenuService {
this.changedActiveItem.next(item); this.changedActiveItem.next(item);
} }
activateByIdx(idx:number, force = false, replaceState = false) {
let item = this.flatItems[idx];
this.activate(item, force, replaceState);
}
changeActive(offset = 1):boolean { changeActive(offset = 1):boolean {
let noChange = (this.activeIdx <= 0 && offset === -1) || let noChange = (this.activeIdx <= 0 && offset === -1) ||
(this.activeIdx === this.flatItems.length - 1 && offset === 1); (this.activeIdx === this.flatItems.length - 1 && offset === 1);
this.activate(this.activeIdx + offset, false, true); this.activateByIdx(this.activeIdx + offset, false, true);
return noChange; return noChange;
} }
@ -263,12 +273,12 @@ export class MenuService {
return item.metadata && item.metadata.operationId === ptr; return item.metadata && item.metadata.operationId === ptr;
}); });
} }
this.activate(idx, true); this.activateByIdx(idx, true);
return idx >= 0; return idx >= 0;
} }
tryScrollToId(id) { tryScrollToId(id) {
let $el = document.querySelector(`[section="${id}"]`); let $el = this.domRoot.querySelector(`[section="${id}"]`);
if ($el) this.scrollService.scrollTo($el); if ($el) this.scrollService.scrollTo($el);
} }
@ -311,15 +321,16 @@ export class MenuService {
if (!tag.operations || !tag.operations.length) return null; if (!tag.operations || !tag.operations.length) return null;
let res = []; let res = [];
for (let operation of tag.operations) { for (let operationInfo of tag.operations) {
let subItem = { let subItem = {
name: SchemaHelper.operationSummary(operation), name: SchemaHelper.operationSummary(operationInfo),
id: operation._pointer, id: operationInfo._pointer,
description: operation.description, description: operationInfo.description,
metadata: { metadata: {
type: 'operation', type: 'operation',
pointer: operation._pointer, pointer: operationInfo._pointer,
operationId: operation.operationId operationId: operationInfo.operationId,
operation: operationInfo.operation
}, },
parent: parent parent: parent
}; };
@ -330,8 +341,8 @@ export class MenuService {
hashFor( hashFor(
id: string|null, itemMeta: id: string|null, itemMeta:
{operationId: string, type: string, pointer: string}, {operationId?: string, type: string, pointer?: string},
parentId: string parentId?: string
) { ) {
if (!id) return null; if (!id) return null;
if (itemMeta && itemMeta.type === 'operation') { if (itemMeta && itemMeta.type === 'operation') {
@ -434,6 +445,7 @@ export class MenuService {
flatMenu():MenuItem[] { flatMenu():MenuItem[] {
let menu = this.items; let menu = this.items;
if (!menu) return;
let res = []; let res = [];
let curDepth = 1; let curDepth = 1;

View File

@ -4,7 +4,7 @@ import { operations as swaggerOperations, keywordTypes } from '../utils/swagger
import { WarningsService } from './warnings.service'; import { WarningsService } from './warnings.service';
import * as slugify from 'slugify'; import * as slugify from 'slugify';
interface PropertyPreprocessOptions { export interface PropertyPreprocessOptions {
childFor?: string; childFor?: string;
skipReadOnly?: boolean; skipReadOnly?: boolean;
discriminator?: string; discriminator?: string;
@ -321,6 +321,7 @@ export class SchemaHelper {
if (!tag.operations) tag.operations = []; if (!tag.operations) tag.operations = [];
tag.operations.push(operationInfo); tag.operations.push(operationInfo);
operationInfo._pointer = operationPointer; operationInfo._pointer = operationPointer;
operationInfo.operation = operation;
} }
} }
} }

View File

@ -5,12 +5,12 @@ import { JsonPointer } from '../utils/JsonPointer';
import { defaults } from '../utils/helpers'; import { defaults } from '../utils/helpers';
import { WarningsService } from './warnings.service'; import { WarningsService } from './warnings.service';
interface Reference { export interface Reference {
$ref: string; $ref: string;
description: string; description: string;
} }
interface Schema { export interface Schema {
properties: any; properties: any;
allOf: any; allOf: any;
items: any; items: any;
@ -180,7 +180,7 @@ class RefCounter {
} }
class SchemaDereferencer { export class SchemaDereferencer {
private _refCouner = new RefCounter(); private _refCouner = new RefCounter();
constructor(private _spec: SpecManager, private normalizator: SchemaNormalizer) { constructor(private _spec: SpecManager, private normalizator: SchemaNormalizer) {

View File

@ -15,7 +15,7 @@ import {
import * as lunr from 'lunr'; import * as lunr from 'lunr';
interface IndexElement { export interface IndexElement {
menuId: string; menuId: string;
title: string; title: string;
body: string; body: string;