Merge pull request #105 from roccivic/seturlhash-refactoring

SetUrlHash refactoring
This commit is contained in:
Marc Delisle 2012-11-06 09:28:00 -08:00
commit 37cf4feca8
4 changed files with 96 additions and 75 deletions

View File

@ -465,7 +465,7 @@ AJAX.cache = {
selflink: $('#selflink').html(),
menu: menu
});
setURLHash(this.current, hash);
AJAX.setUrlHash(this.current, hash);
this.current++;
},
/**
@ -607,11 +607,102 @@ 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
* URL hash management module.
* Allows direct bookmarking and microhistory.
*/
AJAX.setUrlHash = (function (jQuery, window) {
"use strict";
/**
* Indictaes whether we have already completed
* the initialisation of the hash
*
* @access private
*/
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;
}
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);
}
});
});
/**
* Publicly exposes a reference to the otherwise private setUrlHash function
*/
return setUrlHash;
})(jQuery, window);
/**
* Page load event handler
*/
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 +727,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

@ -1,55 +0,0 @@
/* vim: set expandtab sw=4 ts=4 sts=4: */
// TODO: merge this file into ajax.js
/**
* Scripts to update location to allow 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 {
/**
* Handler for changing url according to the hash part, which is updated
* on each page to allow bookmarks.
*/
$(function(){
/* Check if we should set URL */
if (hash_to_set != "") {
window.location.hash = hash_to_set;
hash_to_set = "";
}
/* Indicate that we're done (and we are not going to change location */
hash_init_done = 1;
});
}

View File

@ -152,7 +152,6 @@ class PMA_Header
$this->_scripts->addFile('jquery/jquery.mousewheel.js');
$this->_scripts->addFile('jquery/jquery.event.drag-2.0.js');
$this->_scripts->addFile('jquery/timepicker.js');
$this->_scripts->addFile('update-location.js');
$this->_scripts->addFile('jquery/jquery.ba-hashchange-1.3.js');
$this->_scripts->addFile('jquery/jquery.qtip-1.0.0-rc3.js');

View File

@ -128,7 +128,6 @@ class PMA_Scripts
|| strpos($filename, 'ajax.js') !== false
|| strpos($filename, 'navigation.js') !== false
|| strpos($filename, 'get_image.js.php') !== false
|| strpos($filename, 'update-location.js') !== false
) {
return 0;
} else {