From 0ee14765901e20e13e5c9cccc7f3a0385d7f678d Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Tue, 18 Apr 2017 16:37:05 +0300 Subject: [PATCH] MenuService refactoring --- lib/components/SideMenu/side-menu.ts | 3 +- lib/services/component-parser.service.ts | 2 +- lib/services/menu.service.ts | 48 ++++++++++++++--------- lib/services/schema-helper.service.ts | 3 +- lib/services/schema-normalizer.service.ts | 6 +-- lib/services/search.service.ts | 2 +- 6 files changed, 38 insertions(+), 26 deletions(-) diff --git a/lib/components/SideMenu/side-menu.ts b/lib/components/SideMenu/side-menu.ts index 635a3bc944..0838b9c381 100644 --- a/lib/components/SideMenu/side-menu.ts +++ b/lib/components/SideMenu/side-menu.ts @@ -63,7 +63,6 @@ export class SideMenu implements OnInit, OnDestroy { private menuService:MenuService, optionsService:OptionsService, private detectorRef:ChangeDetectorRef, - //private marker:Marker ) { this.$element = elementRef.nativeElement; @@ -105,7 +104,7 @@ export class SideMenu implements OnInit, OnDestroy { this.toggleMobileNav(); } - this.menuService.activate(item.flatIdx); + this.menuService.activate(item); this.menuService.scrollToActive(); } diff --git a/lib/services/component-parser.service.ts b/lib/services/component-parser.service.ts index 8d26fdf4d5..9fbc3caef2 100644 --- a/lib/services/component-parser.service.ts +++ b/lib/services/component-parser.service.ts @@ -10,7 +10,7 @@ import { ComponentFactoryResolver } from '@angular/core'; -type NodesOrComponents = HTMLElement | ComponentRef; +export type NodesOrComponents = HTMLElement | ComponentRef; export const COMPONENT_PARSER_ALLOWED = 'COMPONENT_PARSER_ALLOWED'; const COMPONENT_REGEXP = '^\\s*\\s*$'; diff --git a/lib/services/menu.service.ts b/lib/services/menu.service.ts index a8d1671bb3..cebff4da87 100644 --- a/lib/services/menu.service.ts +++ b/lib/services/menu.service.ts @@ -18,7 +18,7 @@ const CHANGE = { BACK : -1, }; -interface TagGroup { +export interface TagGroup { name: string; tags: string[]; } @@ -56,6 +56,8 @@ export class MenuService { private _progressSubscription: Subscription; private _tagsWithOperations: any; + public domRoot: Document | Element = document; + constructor( private hash:Hash, private tasks: LazyTasksService, @@ -64,7 +66,11 @@ export class MenuService { private specMgr:SpecManager ) { this.hash = hash; - this.buildMenu(); + + this.specMgr.spec.subscribe(spec => { + if (!spec) return; + this.buildMenu(); + }) this._scrollSubscription = scrollService.scroll.subscribe((evt) => { this.onScroll(evt.isScrolledDown); @@ -172,7 +178,7 @@ export class MenuService { currentItem = currentItem.parent; } selector = selector.trim(); - return selector ? document.querySelector(selector) : null; + return selector ? this.domRoot.querySelector(selector) : null; } isTagOrGroupItem(flatIdx: number):boolean { @@ -202,13 +208,12 @@ export class MenuService { } } - activate(idx, force = false, replaceState = false) { - let item = this.flatItems[idx]; + activate(item:MenuItem, force = false, replaceState = false) { if (!force && item && !item.ready) return; this.deactivate(this.activeIdx); - this.activeIdx = idx; - if (idx < 0) { + this.activeIdx = item ? item.flatIdx : -1; + if (this.activeIdx < 0) { this.hash.update('', replaceState); return; } @@ -224,10 +229,15 @@ export class MenuService { 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 { let noChange = (this.activeIdx <= 0 && 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; } @@ -263,12 +273,12 @@ export class MenuService { return item.metadata && item.metadata.operationId === ptr; }); } - this.activate(idx, true); + this.activateByIdx(idx, true); return idx >= 0; } tryScrollToId(id) { - let $el = document.querySelector(`[section="${id}"]`); + let $el = this.domRoot.querySelector(`[section="${id}"]`); if ($el) this.scrollService.scrollTo($el); } @@ -311,15 +321,16 @@ export class MenuService { if (!tag.operations || !tag.operations.length) return null; let res = []; - for (let operation of tag.operations) { + for (let operationInfo of tag.operations) { let subItem = { - name: SchemaHelper.operationSummary(operation), - id: operation._pointer, - description: operation.description, + name: SchemaHelper.operationSummary(operationInfo), + id: operationInfo._pointer, + description: operationInfo.description, metadata: { type: 'operation', - pointer: operation._pointer, - operationId: operation.operationId + pointer: operationInfo._pointer, + operationId: operationInfo.operationId, + operation: operationInfo.operation }, parent: parent }; @@ -330,8 +341,8 @@ export class MenuService { hashFor( id: string|null, itemMeta: - {operationId: string, type: string, pointer: string}, - parentId: string + {operationId?: string, type: string, pointer?: string}, + parentId?: string ) { if (!id) return null; if (itemMeta && itemMeta.type === 'operation') { @@ -434,6 +445,7 @@ export class MenuService { flatMenu():MenuItem[] { let menu = this.items; + if (!menu) return; let res = []; let curDepth = 1; diff --git a/lib/services/schema-helper.service.ts b/lib/services/schema-helper.service.ts index 13b63aa8c8..5e0cbfe659 100644 --- a/lib/services/schema-helper.service.ts +++ b/lib/services/schema-helper.service.ts @@ -4,7 +4,7 @@ import { operations as swaggerOperations, keywordTypes } from '../utils/swagger import { WarningsService } from './warnings.service'; import * as slugify from 'slugify'; -interface PropertyPreprocessOptions { +export interface PropertyPreprocessOptions { childFor?: string; skipReadOnly?: boolean; discriminator?: string; @@ -321,6 +321,7 @@ export class SchemaHelper { if (!tag.operations) tag.operations = []; tag.operations.push(operationInfo); operationInfo._pointer = operationPointer; + operationInfo.operation = operation; } } } diff --git a/lib/services/schema-normalizer.service.ts b/lib/services/schema-normalizer.service.ts index d3940bb2c5..f599a73ac6 100644 --- a/lib/services/schema-normalizer.service.ts +++ b/lib/services/schema-normalizer.service.ts @@ -5,12 +5,12 @@ import { JsonPointer } from '../utils/JsonPointer'; import { defaults } from '../utils/helpers'; import { WarningsService } from './warnings.service'; -interface Reference { +export interface Reference { $ref: string; description: string; } -interface Schema { +export interface Schema { properties: any; allOf: any; items: any; @@ -180,7 +180,7 @@ class RefCounter { } -class SchemaDereferencer { +export class SchemaDereferencer { private _refCouner = new RefCounter(); constructor(private _spec: SpecManager, private normalizator: SchemaNormalizer) { diff --git a/lib/services/search.service.ts b/lib/services/search.service.ts index c47b9a5248..f5f53f73df 100644 --- a/lib/services/search.service.ts +++ b/lib/services/search.service.ts @@ -15,7 +15,7 @@ import { import * as lunr from 'lunr'; -interface IndexElement { +export interface IndexElement { menuId: string; title: string; body: string;