From 4feae6b20264f55f4942ee331a1f5f22f7b0fb49 Mon Sep 17 00:00:00 2001 From: Jeff Arnold Date: Wed, 19 Dec 2012 15:19:29 -0500 Subject: [PATCH] Make the back button work for the settings and subscriptions pages (imported from commit 650c884abc9514cdfc934ae01e0a490f5f681292) --- zephyr/static/js/compose.js | 2 +- zephyr/static/js/hashchange.js | 36 +++++++++++++++++++++++++++------- zephyr/static/js/ui.js | 8 ++++++-- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/zephyr/static/js/compose.js b/zephyr/static/js/compose.js index ea696b464b..011205b4b1 100644 --- a/zephyr/static/js/compose.js +++ b/zephyr/static/js/compose.js @@ -73,7 +73,7 @@ exports.start = function (msg_type, opts) { compose.message_content(opts.message); } - $('#sidebar a[href="#home"]').tab('show'); + ui.change_tab_to("#home"); var focus_area; if (opts.stream && ! opts.subject) { diff --git a/zephyr/static/js/hashchange.js b/zephyr/static/js/hashchange.js index 16943630d0..854d9980cf 100644 --- a/zephyr/static/js/hashchange.js +++ b/zephyr/static/js/hashchange.js @@ -6,7 +6,18 @@ var expected_hash = false; exports.changehash = function (newhash) { expected_hash = newhash; + // Some browsers reset scrollTop when changing the hash to "", + // so we save and restore it. + // http://stackoverflow.com/questions/4715073/window-location-hash-prevent-scrolling-to-the-top + var scrolltop; + if (newhash === "") { + scrolltop = viewport.scrollTop(); + } window.location.hash = newhash; + util.reset_favicon(); + if (newhash === "") { + viewport.scrollTop(scrolltop); + } }; function hashchanged() { @@ -19,13 +30,24 @@ function hashchanged() { } var hash = window.location.hash.split("/"); - if (hash[0] === "#narrow") { - narrow.hashchanged(hash); - ui.update_floating_recipient_bar(); - } - else if (narrow.active()) { - narrow.show_all_messages(); - ui.update_floating_recipient_bar(); + switch (hash[0]) { + case "#narrow": + ui.change_tab_to("#home"); + narrow.hashchanged(hash); + ui.update_floating_recipient_bar(); + break; + case "": + case "#": + ui.change_tab_to("#home"); + narrow.show_all_messages(); + ui.update_floating_recipient_bar(); + break; + case "#subscriptions": + ui.change_tab_to("#subscriptions"); + break; + case "#settings": + ui.change_tab_to("#settings"); + break; } } diff --git a/zephyr/static/js/ui.js b/zephyr/static/js/ui.js index f955aff452..b7153f6331 100644 --- a/zephyr/static/js/ui.js +++ b/zephyr/static/js/ui.js @@ -18,6 +18,10 @@ exports.home_tab_obscured = function () { var scroll_positions = {}; var gravatar_stamp = 1; +exports.change_tab_to = function (tabname) { + $('#sidebar a[href="' + tabname + '"]').tab('show'); +}; + exports.focus_on = function (field_id) { // Call after autocompleting on a field, to advance the focus to // the next input field. @@ -505,9 +509,9 @@ $(function () { // Set the URL bar title to show the sub-page you're currently on. var browser_url = target_tab; if (browser_url === "#home") { - browser_url = "#"; + browser_url = ""; } - window.history.pushState("object or string", "Title", browser_url); + hashchange.changehash(browser_url); }); $('#sidebar a[href="#subscriptions"]').click(subs.fetch);