Skip to content

Commit 66c06b3

Browse files
committed
feat: update fragment while scrolling and on menu clicks
closes #138, #202
1 parent c724df4 commit 66c06b3

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

lib/components/Method/method.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Input, HostBinding, Component, OnInit, ChangeDetectionStrategy, Element
33
import JsonPointer from '../../utils/JsonPointer';
44
import { BaseComponent, SpecManager } from '../base';
55
import { SchemaHelper } from '../../services/schema-helper.service';
6-
import { OptionsService } from '../../services/';
6+
import { OptionsService, MenuService } from '../../services/';
77

88

99
interface MethodInfo {
@@ -36,7 +36,10 @@ export class Method extends BaseComponent implements OnInit {
3636

3737
method: MethodInfo;
3838

39-
constructor(specMgr:SpecManager, private optionsService: OptionsService) {
39+
constructor(
40+
specMgr:SpecManager,
41+
private optionsService: OptionsService,
42+
private menu: MenuService) {
4043
super(specMgr);
4144
}
4245

@@ -58,11 +61,9 @@ export class Method extends BaseComponent implements OnInit {
5861
}
5962

6063
buildAnchor() {
61-
if (this.operationId) {
62-
return 'operation/' + encodeURIComponent(this.componentSchema.operationId);
63-
} else {
64-
return this.parentTagId + encodeURIComponent(this.pointer);
65-
}
64+
this.menu.hashFor(this.pointer,
65+
{ type: 'method', operationId: this.operationId, pointer: this.pointer },
66+
this.parentTagId );
6667
}
6768

6869
filterMainTags(tags) {

lib/services/hash.service.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { BehaviorSubject } from 'rxjs/BehaviorSubject';
77
@Injectable()
88
export class Hash {
99
public value = new BehaviorSubject<string | null>(null);
10+
private noEmit:boolean = false;
1011
constructor(private location: PlatformLocation) {
1112
this.bind();
1213
}
@@ -21,7 +22,17 @@ export class Hash {
2122

2223
bind() {
2324
this.location.onHashChange(() => {
25+
if (this.noEmit) return;
2426
this.value.next(this.hash);
2527
});
2628
}
29+
30+
update(hash: string|null) {
31+
if (!hash) return;
32+
this.noEmit = true;
33+
window.location.hash = hash;
34+
setTimeout(() => {
35+
this.noEmit = false;
36+
});
37+
}
2738
}

lib/services/menu.service.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('Menu service', () => {
2424
beforeEach(inject([SpecManager, Hash, ScrollService, LazyTasksService],
2525
( _specMgr, _hash, _scroll, _tasks) => {
2626
hashService = _hash;
27+
spyOn(hashService, 'update').and.stub();
2728
scroll = _scroll;
2829
tasks = _tasks;
2930
specMgr = _specMgr;

lib/services/menu.service.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ export class MenuService {
216216
cItem.parent.active = true;
217217
cItem = cItem.parent;
218218
}
219+
console.log(idx, '>>>>>>>>>>>>> woooohooooo');
220+
this.hash.update(this.hashFor(item.id, item.metadata, item.parent && item.parent.id));
219221
this.changedActiveItem.next(item);
220222
}
221223

@@ -320,6 +322,23 @@ export class MenuService {
320322
return res;
321323
}
322324

325+
hashFor(
326+
id: string|null, itemMeta:
327+
{operationId: string, type: string, pointer: string},
328+
parentId: string
329+
) {
330+
if (!id) return null;
331+
if (itemMeta && itemMeta.type === 'method') {
332+
if (itemMeta.operationId) {
333+
return 'operation/' + encodeURIComponent(itemMeta.operationId);
334+
} else {
335+
return parentId + encodeURIComponent(itemMeta.pointer);
336+
}
337+
} else {
338+
return id;
339+
}
340+
}
341+
323342
getTagsItems(parent: MenuItem, tagGroup:TagGroup = null):MenuItem[] {
324343
let schema = this.specMgr.schema;
325344

0 commit comments

Comments
 (0)