2021-03-22 16:09:12 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
|
|
|
import * as hash_util from "./hash_util";
|
|
|
|
import * as ui_util from "./ui_util";
|
|
|
|
|
2021-04-09 13:53:45 +02:00
|
|
|
export const state = {
|
2021-03-22 16:09:12 +01:00
|
|
|
is_internal_change: false,
|
|
|
|
hash_before_overlay: null,
|
2021-03-24 20:14:12 +01:00
|
|
|
old_hash: window.location.hash,
|
2021-04-09 13:53:45 +02:00
|
|
|
changing_hash: false,
|
2021-05-19 11:17:33 +02:00
|
|
|
// If the spectator's hash changes to a restricted hash, then we store the old hash
|
|
|
|
// so that we can take user back to the allowed hash.
|
|
|
|
// TODO: Store #narrow old hashes. Currently they are not stored here since, the #narrow
|
|
|
|
// hashes are changed without calling `hashchanged` in many ways.
|
2021-09-04 04:03:07 +02:00
|
|
|
spectator_old_hash: hash_util.is_spectator_compatible(window.location.hash)
|
2021-05-19 11:17:33 +02:00
|
|
|
? window.location.hash
|
|
|
|
: "#",
|
2021-03-22 16:09:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export function clear_for_testing() {
|
|
|
|
state.is_internal_change = false;
|
|
|
|
state.hash_before_overlay = null;
|
|
|
|
state.old_hash = "#";
|
|
|
|
}
|
|
|
|
|
|
|
|
export function old_hash() {
|
|
|
|
return state.old_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function set_hash_before_overlay(hash) {
|
|
|
|
state.hash_before_overlay = hash;
|
|
|
|
}
|
|
|
|
|
2021-11-17 13:24:17 +01:00
|
|
|
export function update_web_public_hash(hash) {
|
|
|
|
// Returns true if hash is web public compatible.
|
|
|
|
if (hash_util.is_spectator_compatible(hash)) {
|
|
|
|
state.spectator_old_hash = hash;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:09:12 +01:00
|
|
|
export function save_old_hash() {
|
|
|
|
state.old_hash = window.location.hash;
|
|
|
|
|
|
|
|
const was_internal_change = state.is_internal_change;
|
|
|
|
state.is_internal_change = false;
|
|
|
|
|
|
|
|
return was_internal_change;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function update(new_hash) {
|
|
|
|
const old_hash = window.location.hash;
|
|
|
|
|
|
|
|
if (!new_hash.startsWith("#")) {
|
|
|
|
blueslip.error("programming error: prefix hashes with #: " + new_hash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_hash === new_hash) {
|
|
|
|
// If somebody is calling us with the same hash we already have, it's
|
|
|
|
// probably harmless, and we just ignore it. But it could be a symptom
|
|
|
|
// of disorganized code that's prone to an infinite loop of repeatedly
|
|
|
|
// assigning the same hash.
|
|
|
|
blueslip.info("ignoring probably-harmless call to browser_history.update: " + new_hash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.old_hash = old_hash;
|
|
|
|
state.is_internal_change = true;
|
|
|
|
window.location.hash = new_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function exit_overlay() {
|
2021-04-09 14:00:50 +02:00
|
|
|
if (hash_util.is_overlay_hash(window.location.hash) && !state.changing_hash) {
|
2021-03-22 16:09:12 +01:00
|
|
|
ui_util.blur_active_element();
|
|
|
|
const new_hash = state.hash_before_overlay || "#";
|
|
|
|
update(new_hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function go_to_location(hash) {
|
|
|
|
// Call this function when you WANT the hashchanged
|
|
|
|
// function to run.
|
|
|
|
window.location.hash = hash;
|
|
|
|
}
|
2021-03-16 07:22:28 +01:00
|
|
|
|
|
|
|
export function update_hash_internally_if_required(hash) {
|
|
|
|
if (window.location.hash !== hash) {
|
|
|
|
update(hash);
|
|
|
|
}
|
|
|
|
}
|
2021-05-19 11:17:33 +02:00
|
|
|
|
|
|
|
export function return_to_web_public_hash() {
|
|
|
|
window.location.hash = state.spectator_old_hash;
|
|
|
|
}
|