Refactored setURLHash() into a module

This commit is contained in:
Rouslan Placella 2012-11-06 13:46:14 +00:00
parent 8ecc08f05f
commit 6a7bea5ebc
2 changed files with 87 additions and 65 deletions

View File

@ -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);
}
});
});
/**

View File

@ -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);