Skip to content

Commit

Permalink
MenuService refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Apr 19, 2017
1 parent 92eec25 commit 0ee1476
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
3 changes: 1 addition & 2 deletions lib/components/SideMenu/side-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -105,7 +104,7 @@ export class SideMenu implements OnInit, OnDestroy {
this.toggleMobileNav();
}

this.menuService.activate(item.flatIdx);
this.menuService.activate(item);
this.menuService.scrollToActive();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/services/component-parser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ComponentFactoryResolver
} from '@angular/core';

type NodesOrComponents = HTMLElement | ComponentRef<any>;
export type NodesOrComponents = HTMLElement | ComponentRef<any>;
export const COMPONENT_PARSER_ALLOWED = 'COMPONENT_PARSER_ALLOWED';

const COMPONENT_REGEXP = '^\\s*<!-- ReDoc-Inject:\\s+?{component}\\s+?-->\\s*$';
Expand Down
48 changes: 30 additions & 18 deletions lib/services/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const CHANGE = {
BACK : -1,
};

interface TagGroup {
export interface TagGroup {
name: string;
tags: string[];
}
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
};
Expand All @@ -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') {
Expand Down Expand Up @@ -434,6 +445,7 @@ export class MenuService {

flatMenu():MenuItem[] {
let menu = this.items;
if (!menu) return;
let res = [];
let curDepth = 1;

Expand Down
3 changes: 2 additions & 1 deletion lib/services/schema-helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -321,6 +321,7 @@ export class SchemaHelper {
if (!tag.operations) tag.operations = [];
tag.operations.push(operationInfo);
operationInfo._pointer = operationPointer;
operationInfo.operation = operation;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/services/schema-normalizer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -180,7 +180,7 @@ class RefCounter {
}


class SchemaDereferencer {
export class SchemaDereferencer {
private _refCouner = new RefCounter();

constructor(private _spec: SpecManager, private normalizator: SchemaNormalizer) {
Expand Down
2 changes: 1 addition & 1 deletion lib/services/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import * as lunr from 'lunr';

interface IndexElement {
export interface IndexElement {
menuId: string;
title: string;
body: string;
Expand Down

0 comments on commit 0ee1476

Please sign in to comment.