From 6a7bea5ebc813af66cbfc6d013052ba1e2596d93 Mon Sep 17 00:00:00 2001 From: Rouslan Placella Date: Tue, 6 Nov 2012 13:46:14 +0000 Subject: [PATCH] Refactored setURLHash() into a module --- js/ajax.js | 21 +------ js/update-location.js | 131 +++++++++++++++++++++++++++--------------- 2 files changed, 87 insertions(+), 65 deletions(-) diff --git a/js/ajax.js b/js/ajax.js index 4e28575a2f..a761fa561d 100644 --- a/js/ajax.js +++ b/js/ajax.js @@ -465,7 +465,7 @@ AJAX.cache = { selflink: $('#selflink').html(), menu: menu }); - setURLHash(this.current, hash); + PMA_setUrlHash(this.current, hash); this.current++; }, /** @@ -606,12 +606,6 @@ AJAX.cache = { } }; -/** - * @var bool A flag is used to distinguish whether we have - * deliberately changed the hash or if the user - * clicked the back/forward button in the browser - */ -var settingHash = false; $(function () { // Add the menu from the initial page into the cache // The cache primer is set by the footer class @@ -636,19 +630,6 @@ $(function () { ); } }); - $(window).hashchange(function () { - if (settingHash) { - settingHash = false; - return; - } - // Test if the hash part of the url seems to be in a valid format - if (/^#PMAURL-\d+:/.test(window.location.hash)) { - var index = window.location.hash.substring( - 8, window.location.hash.indexOf(':') - ); - AJAX.cache.navigate(index); - } - }); }); /** diff --git a/js/update-location.js b/js/update-location.js index fff4ac5f21..303d34cabd 100644 --- a/js/update-location.js +++ b/js/update-location.js @@ -2,54 +2,95 @@ // TODO: merge this file into ajax.js - /** - * Scripts to update location to allow bookmarking and microhistory. + * URL hash management module. + * Allows direct bookmarking and microhistory. */ - -var hash_to_set = ""; -var hash_init_done = 0; - -/** - * Sets hash part in URL, either calls itself in parent frame or does the - * work itself. The hash is not set directly if we did not yet process old - * one. - */ -function setURLHash(index, hash) -{ - settingHash = true; - if (jQuery.browser.webkit) { - /* - * Setting hash leads to reload in webkit: - * http://www.quirksmode.org/bugreports/archives/2005/05/Safari_13_visual_anomaly_with_windowlocationhref.html - */ - return; - } - if (hash_init_done) { - window.location.hash = "PMAURL-" + index + ":" + hash; - } else { - hash_to_set = "PMAURL-" + index + ":" + hash; - } -} - -/* Check if hash contains parameters */ -if (window.location.hash.substring(0, 8) == '#PMAURL-') { - // FIXME: don't reload if the page is the same - window.location = window.location.hash.substring( - window.location.hash.indexOf(':') + 1 - ); -} else { +var PMA_setUrlHash = (function (jQuery, window) { /** - * Handler for changing url according to the hash part, which is updated - * on each page to allow bookmarks. + * Indictaes whether we have already completed + * the initialisation of the hash + * + * @access private */ - $(function(){ - /* Check if we should set URL */ - if (hash_to_set != "") { - window.location.hash = hash_to_set; - hash_to_set = ""; + var ready = false; + /** + * Stores a hash that needed to be set when we were not ready + * + * @access private + */ + var savedHash = ""; + /** + * Flag to indicate if the change of hash was triggered + * by a user pressing the back/forward button or if + * the change was triggered internally + * + * @access private + */ + var userChange = true; + + /** + * Sets the hash part of the URL + * + * @access public + */ + function setUrlHash(index, hash) { + if (jQuery.browser.webkit) { + /* + * Setting hash leads to reload in webkit: + * http://www.quirksmode.org/bugreports/archives/2005/05/Safari_13_visual_anomaly_with_windowlocationhref.html + */ + return; } - /* Indicate that we're done (and we are not going to change location */ - hash_init_done = 1; + + userChange = false; + if (ready) { + window.location.hash = "PMAURL-" + index + ":" + hash; + } else { + savedHash = "PMAURL-" + index + ":" + hash; + } + } + + /** + * Start initialisation + */ + if (window.location.hash.substring(0, 8) == '#PMAURL-') { + // We have a valid hash, let's redirect the user + // to the page that it's pointing to + window.location = window.location.hash.substring( + window.location.hash.indexOf(':') + 1 + ); + } else { + // We don't have a valid hash, so we'll set it up + // when the page finishes loading + jQuery(function(){ + /* Check if we should set URL */ + if (savedHash != "") { + window.location.hash = savedHash; + savedHash = ""; + } + // Indicate that we're done initialising + ready = true; + }); + } + + /** + * Register an event handler for when the url hash changes + */ + jQuery(function(){ + jQuery(window).hashchange(function () { + if (userChange === false) { + // Ignore internally triggered hash changes + userChange = true; + } else if (/^#PMAURL-\d+:/.test(window.location.hash)) { + // Change page if the hash changed was triggered by a user action + var index = window.location.hash.substring( + 8, window.location.hash.indexOf(':') + ); + AJAX.cache.navigate(index); + } + }); }); -} + + return setUrlHash; +})(jQuery, window);