mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-11 03:16:48 +03:00
4f4e7489ce
fixes #244
43 lines
918 B
TypeScript
43 lines
918 B
TypeScript
'use strict';
|
|
import { Injectable } from '@angular/core';
|
|
import { PlatformLocation } from '@angular/common';
|
|
|
|
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();
|
|
}
|
|
|
|
start() {
|
|
this.value.next(this.hash);
|
|
}
|
|
|
|
get hash() {
|
|
return this.location.hash;
|
|
}
|
|
|
|
bind() {
|
|
this.location.onHashChange(() => {
|
|
if (this.noEmit) return;
|
|
this.value.next(this.hash);
|
|
});
|
|
}
|
|
|
|
update(hash: string|null, rewriteHistory:boolean = false) {
|
|
if (hash == undefined) return;
|
|
if (rewriteHistory) {
|
|
window.history.replaceState(null, '', '#' + hash);
|
|
return;
|
|
}
|
|
this.noEmit = true;
|
|
window.location.hash = hash;
|
|
setTimeout(() => {
|
|
this.noEmit = false;
|
|
});
|
|
}
|
|
}
|