feat: update fragment while scrolling and on menu clicks

closes #138, #202
This commit is contained in:
Roman Hotsiy 2017-02-27 13:13:06 +02:00
parent c724df48f4
commit 66c06b30b9
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
4 changed files with 39 additions and 7 deletions

View File

@ -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) {

View File

@ -7,6 +7,7 @@ import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class Hash {
public value = new BehaviorSubject<string | null>(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;
});
}
}

View File

@ -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;

View File

@ -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;