From 66c06b30b96fc0fe6ce8ba18eb69a342252526f1 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Mon, 27 Feb 2017 13:13:06 +0200 Subject: [PATCH] feat: update fragment while scrolling and on menu clicks closes #138, #202 --- lib/components/Method/method.ts | 15 ++++++++------- lib/services/hash.service.ts | 11 +++++++++++ lib/services/menu.service.spec.ts | 1 + lib/services/menu.service.ts | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/components/Method/method.ts b/lib/components/Method/method.ts index 3da98f1334..4407f16ee1 100644 --- a/lib/components/Method/method.ts +++ b/lib/components/Method/method.ts @@ -3,7 +3,7 @@ import { Input, HostBinding, Component, OnInit, ChangeDetectionStrategy, Element import JsonPointer from '../../utils/JsonPointer'; import { BaseComponent, SpecManager } from '../base'; import { SchemaHelper } from '../../services/schema-helper.service'; -import { OptionsService } from '../../services/'; +import { OptionsService, MenuService } from '../../services/'; interface MethodInfo { @@ -36,7 +36,10 @@ export class Method extends BaseComponent implements OnInit { method: MethodInfo; - constructor(specMgr:SpecManager, private optionsService: OptionsService) { + constructor( + specMgr:SpecManager, + private optionsService: OptionsService, + private menu: MenuService) { super(specMgr); } @@ -58,11 +61,9 @@ export class Method extends BaseComponent implements OnInit { } buildAnchor() { - if (this.operationId) { - return 'operation/' + encodeURIComponent(this.componentSchema.operationId); - } else { - return this.parentTagId + encodeURIComponent(this.pointer); - } + this.menu.hashFor(this.pointer, + { type: 'method', operationId: this.operationId, pointer: this.pointer }, + this.parentTagId ); } filterMainTags(tags) { diff --git a/lib/services/hash.service.ts b/lib/services/hash.service.ts index 73a6adced1..f115e65be7 100644 --- a/lib/services/hash.service.ts +++ b/lib/services/hash.service.ts @@ -7,6 +7,7 @@ import { BehaviorSubject } from 'rxjs/BehaviorSubject'; @Injectable() export class Hash { public value = new BehaviorSubject(null); + private noEmit:boolean = false; constructor(private location: PlatformLocation) { this.bind(); } @@ -21,7 +22,17 @@ export class Hash { bind() { this.location.onHashChange(() => { + if (this.noEmit) return; this.value.next(this.hash); }); } + + update(hash: string|null) { + if (!hash) return; + this.noEmit = true; + window.location.hash = hash; + setTimeout(() => { + this.noEmit = false; + }); + } } diff --git a/lib/services/menu.service.spec.ts b/lib/services/menu.service.spec.ts index 20f728eb52..b0a9eb3f28 100644 --- a/lib/services/menu.service.spec.ts +++ b/lib/services/menu.service.spec.ts @@ -24,6 +24,7 @@ describe('Menu service', () => { beforeEach(inject([SpecManager, Hash, ScrollService, LazyTasksService], ( _specMgr, _hash, _scroll, _tasks) => { hashService = _hash; + spyOn(hashService, 'update').and.stub(); scroll = _scroll; tasks = _tasks; specMgr = _specMgr; diff --git a/lib/services/menu.service.ts b/lib/services/menu.service.ts index 348aead37c..adecd90de1 100644 --- a/lib/services/menu.service.ts +++ b/lib/services/menu.service.ts @@ -216,6 +216,8 @@ export class MenuService { cItem.parent.active = true; cItem = cItem.parent; } + console.log(idx, '>>>>>>>>>>>>> woooohooooo'); + this.hash.update(this.hashFor(item.id, item.metadata, item.parent && item.parent.id)); this.changedActiveItem.next(item); } @@ -320,6 +322,23 @@ export class MenuService { return res; } + hashFor( + id: string|null, itemMeta: + {operationId: string, type: string, pointer: string}, + parentId: string + ) { + if (!id) return null; + if (itemMeta && itemMeta.type === 'method') { + if (itemMeta.operationId) { + return 'operation/' + encodeURIComponent(itemMeta.operationId); + } else { + return parentId + encodeURIComponent(itemMeta.pointer); + } + } else { + return id; + } + } + getTagsItems(parent: MenuItem, tagGroup:TagGroup = null):MenuItem[] { let schema = this.specMgr.schema;