mapcomplete/Logic/Web/Hash.ts

29 lines
866 B
TypeScript
Raw Normal View History

2020-11-17 02:22:48 +01:00
import {UIEventSource} from "../UIEventSource";
2021-01-06 02:21:50 +01:00
import Constants from "../../Models/Constants";
import {Utils} from "../../Utils";
2020-11-17 02:22:48 +01:00
export default class Hash {
public static hash : UIEventSource<string> = Hash.Get();
private static Get() : UIEventSource<string>{
2021-01-06 02:21:50 +01:00
if(Utils.runningFromConsole){
return new UIEventSource<string>(undefined);
}
2020-11-17 02:22:48 +01:00
const hash = new UIEventSource<string>(window.location.hash.substr(1));
hash.addCallback(h => {
if(h === undefined || h === ""){
window.location.hash = "";
return;
}
2020-11-17 02:22:48 +01:00
h = h.replace(/\//g, "_");
window.location.hash = "#" + h;
2020-11-17 02:22:48 +01:00
});
window.onhashchange = () => {
hash.setData(window.location.hash.substr(1))
}
return hash;
}
}