hashchange: Don't change the hash when called from hashchange code.

I generally don't like this sort of state variable, but I don't see a
better solution.  The codepath is that when you start out on the
subscriptions page and then click one of the left sidebar links to
narrow to something:

(1) hashchanged() would call ui.change_tab
(2) ui.change_tab triggers a gear change event
(3) The ui.js gear-changed event handler updates the hash

Resulting in the hash ending up at "#".  Since there's no easy way to
pass arguments through to the event handler, we just use a global
variable inside hash_change.js to track whether we're currently
handling a hashchange event.

(imported from commit 7bb905a223b5539240fc36de7896ee8074ebc62e)
This commit is contained in:
Tim Abbott 2013-04-03 14:44:26 -04:00
parent 200fd7ff70
commit fa239ea270

View File

@ -3,6 +3,7 @@ var hashchange = (function () {
var exports = {};
var expected_hash = false;
var changing_hash = false;
// Some browsers zealously URI-decode the contents of
// window.location.hash. So we hide our URI-encoding
@ -19,6 +20,9 @@ function decodeHashComponent(str) {
}
exports.changehash = function (newhash) {
if (changing_hash) {
return;
}
expected_hash = newhash;
// Some browsers reset scrollTop when changing the hash to "",
// so we save and restore it.
@ -32,6 +36,9 @@ exports.changehash = function (newhash) {
};
exports.save_narrow = function (operators) {
if (changing_hash) {
return;
}
if (operators === undefined) {
exports.changehash('#');
} else {
@ -60,7 +67,7 @@ function parse_narrow(hash) {
}
// Returns true if this function performed a narrow
function hashchanged() {
function do_hashchange() {
// If window.location.hash changed because our app explicitly
// changed it, then we don't need to do anything.
// (This function only neds to jump into action if it changed
@ -95,6 +102,13 @@ function hashchanged() {
return false;
}
function hashchanged() {
changing_hash = true;
var ret = do_hashchange();
changing_hash = false;
return ret;
}
exports.initialize = function () {
window.onhashchange = hashchanged;
if (hashchanged()) {