2016-05-07 10:54:44 +03:00
|
|
|
'use strict';
|
2016-10-23 20:18:42 +03:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { PlatformLocation } from '@angular/common';
|
2016-05-07 10:54:44 +03:00
|
|
|
|
2016-10-23 20:18:42 +03:00
|
|
|
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
|
2016-05-07 10:54:44 +03:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class Hash {
|
2016-11-23 02:23:32 +03:00
|
|
|
public value = new BehaviorSubject<string | null>(null);
|
2017-02-27 14:13:06 +03:00
|
|
|
private noEmit:boolean = false;
|
2016-11-24 16:29:29 +03:00
|
|
|
constructor(private location: PlatformLocation) {
|
2016-05-07 10:54:44 +03:00
|
|
|
this.bind();
|
2016-11-24 16:29:29 +03:00
|
|
|
}
|
2016-05-07 10:54:44 +03:00
|
|
|
|
2016-11-24 16:29:29 +03:00
|
|
|
start() {
|
|
|
|
this.value.next(this.hash);
|
2016-05-07 10:54:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get hash() {
|
2016-10-23 20:18:42 +03:00
|
|
|
return this.location.hash;
|
2016-05-07 10:54:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bind() {
|
2016-10-23 20:18:42 +03:00
|
|
|
this.location.onHashChange(() => {
|
2017-02-27 14:13:06 +03:00
|
|
|
if (this.noEmit) return;
|
2016-10-23 20:18:42 +03:00
|
|
|
this.value.next(this.hash);
|
2016-05-07 10:54:44 +03:00
|
|
|
});
|
|
|
|
}
|
2017-02-27 14:13:06 +03:00
|
|
|
|
2017-03-31 19:53:01 +03:00
|
|
|
update(hash: string|null, rewriteHistory:boolean = false) {
|
2017-03-01 00:46:43 +03:00
|
|
|
if (hash == undefined) return;
|
2017-03-31 19:53:01 +03:00
|
|
|
if (rewriteHistory) {
|
|
|
|
window.history.replaceState(null, '', '#' + hash);
|
|
|
|
return;
|
|
|
|
}
|
2017-02-27 14:13:06 +03:00
|
|
|
this.noEmit = true;
|
|
|
|
window.location.hash = hash;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.noEmit = false;
|
|
|
|
});
|
|
|
|
}
|
2016-05-07 10:54:44 +03:00
|
|
|
}
|