mirror of
https://github.com/Redocly/redoc.git
synced 2025-02-07 13:30:33 +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 JsonPointer from '../../utils/JsonPointer';
|
||||||
import { BaseComponent, SpecManager } from '../base';
|
import { BaseComponent, SpecManager } from '../base';
|
||||||
import { SchemaHelper } from '../../services/schema-helper.service';
|
import { SchemaHelper } from '../../services/schema-helper.service';
|
||||||
import { OptionsService } from '../../services/';
|
import { OptionsService, MenuService } from '../../services/';
|
||||||
|
|
||||||
|
|
||||||
interface MethodInfo {
|
interface MethodInfo {
|
||||||
|
@ -36,7 +36,10 @@ export class Method extends BaseComponent implements OnInit {
|
||||||
|
|
||||||
method: MethodInfo;
|
method: MethodInfo;
|
||||||
|
|
||||||
constructor(specMgr:SpecManager, private optionsService: OptionsService) {
|
constructor(
|
||||||
|
specMgr:SpecManager,
|
||||||
|
private optionsService: OptionsService,
|
||||||
|
private menu: MenuService) {
|
||||||
super(specMgr);
|
super(specMgr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,11 +61,9 @@ export class Method extends BaseComponent implements OnInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
buildAnchor() {
|
buildAnchor() {
|
||||||
if (this.operationId) {
|
this.menu.hashFor(this.pointer,
|
||||||
return 'operation/' + encodeURIComponent(this.componentSchema.operationId);
|
{ type: 'method', operationId: this.operationId, pointer: this.pointer },
|
||||||
} else {
|
this.parentTagId );
|
||||||
return this.parentTagId + encodeURIComponent(this.pointer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filterMainTags(tags) {
|
filterMainTags(tags) {
|
||||||
|
|
|
@ -7,6 +7,7 @@ import { BehaviorSubject } from 'rxjs/BehaviorSubject';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class Hash {
|
export class Hash {
|
||||||
public value = new BehaviorSubject<string | null>(null);
|
public value = new BehaviorSubject<string | null>(null);
|
||||||
|
private noEmit:boolean = false;
|
||||||
constructor(private location: PlatformLocation) {
|
constructor(private location: PlatformLocation) {
|
||||||
this.bind();
|
this.bind();
|
||||||
}
|
}
|
||||||
|
@ -21,7 +22,17 @@ export class Hash {
|
||||||
|
|
||||||
bind() {
|
bind() {
|
||||||
this.location.onHashChange(() => {
|
this.location.onHashChange(() => {
|
||||||
|
if (this.noEmit) return;
|
||||||
this.value.next(this.hash);
|
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],
|
beforeEach(inject([SpecManager, Hash, ScrollService, LazyTasksService],
|
||||||
( _specMgr, _hash, _scroll, _tasks) => {
|
( _specMgr, _hash, _scroll, _tasks) => {
|
||||||
hashService = _hash;
|
hashService = _hash;
|
||||||
|
spyOn(hashService, 'update').and.stub();
|
||||||
scroll = _scroll;
|
scroll = _scroll;
|
||||||
tasks = _tasks;
|
tasks = _tasks;
|
||||||
specMgr = _specMgr;
|
specMgr = _specMgr;
|
||||||
|
|
|
@ -216,6 +216,8 @@ export class MenuService {
|
||||||
cItem.parent.active = true;
|
cItem.parent.active = true;
|
||||||
cItem = cItem.parent;
|
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);
|
this.changedActiveItem.next(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -320,6 +322,23 @@ export class MenuService {
|
||||||
return res;
|
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[] {
|
getTagsItems(parent: MenuItem, tagGroup:TagGroup = null):MenuItem[] {
|
||||||
let schema = this.specMgr.schema;
|
let schema = this.specMgr.schema;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user