mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-11 03:16:48 +03:00
feat: update fragment while scrolling and on menu clicks
closes #138, #202
This commit is contained in:
parent
c724df48f4
commit
66c06b30b9
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user