Skip to content

Commit

Permalink
feat: update fragment while scrolling and on menu clicks
Browse files Browse the repository at this point in the history
closes #138, #202
  • Loading branch information
RomanHotsiy committed Feb 27, 2017
1 parent c724df4 commit 66c06b3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/components/Method/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions lib/services/hash.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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;
});
}
}
1 change: 1 addition & 0 deletions lib/services/menu.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions lib/services/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 66c06b3

Please sign in to comment.