From 6a7bea5ebc813af66cbfc6d013052ba1e2596d93 Mon Sep 17 00:00:00 2001 From: Rouslan Placella Date: Tue, 6 Nov 2012 13:46:14 +0000 Subject: [PATCH 1/3] 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); From c573b0c566be6e8dca8bc5b2b23ef7208505dc6b Mon Sep 17 00:00:00 2001 From: Rouslan Placella Date: Tue, 6 Nov 2012 15:19:12 +0000 Subject: [PATCH 2/3] Moved setUrlHash module into ajax.js --- js/ajax.js | 98 ++++++++++++++++++++++++++++++++++++- js/update-location.js | 96 ------------------------------------ libraries/Header.class.php | 1 - libraries/Scripts.class.php | 1 - 4 files changed, 97 insertions(+), 99 deletions(-) delete mode 100644 js/update-location.js diff --git a/js/ajax.js b/js/ajax.js index a761fa561d..c0d424be01 100644 --- a/js/ajax.js +++ b/js/ajax.js @@ -465,7 +465,7 @@ AJAX.cache = { selflink: $('#selflink').html(), menu: menu }); - PMA_setUrlHash(this.current, hash); + AJAX.setUrlHash(this.current, hash); this.current++; }, /** @@ -606,6 +606,102 @@ AJAX.cache = { } }; +/** + * URL hash management module. + * Allows direct bookmarking and microhistory. + */ +AJAX.setUrlHash = (function (jQuery, window) { + /** + * 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 + */ $(function () { // Add the menu from the initial page into the cache // The cache primer is set by the footer class diff --git a/js/update-location.js b/js/update-location.js deleted file mode 100644 index 303d34cabd..0000000000 --- a/js/update-location.js +++ /dev/null @@ -1,96 +0,0 @@ -/* vim: set expandtab sw=4 ts=4 sts=4: */ - -// TODO: merge this file into ajax.js - -/** - * URL hash management module. - * Allows direct bookmarking and microhistory. - */ -var PMA_setUrlHash = (function (jQuery, window) { - /** - * 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); - } - }); - }); - - return setUrlHash; -})(jQuery, window); diff --git a/libraries/Header.class.php b/libraries/Header.class.php index 363ed546a3..6781cae458 100644 --- a/libraries/Header.class.php +++ b/libraries/Header.class.php @@ -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'); diff --git a/libraries/Scripts.class.php b/libraries/Scripts.class.php index 519ccfeaad..7b09b197de 100644 --- a/libraries/Scripts.class.php +++ b/libraries/Scripts.class.php @@ -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 { From 1b6bb28c3e5af7d34ef646749437505ba7629174 Mon Sep 17 00:00:00 2001 From: Rouslan Placella Date: Tue, 6 Nov 2012 15:31:10 +0000 Subject: [PATCH 3/3] Use strict mode --- js/ajax.js | 1 + 1 file changed, 1 insertion(+) diff --git a/js/ajax.js b/js/ajax.js index c0d424be01..14f06dbd26 100644 --- a/js/ajax.js +++ b/js/ajax.js @@ -611,6 +611,7 @@ AJAX.cache = { * Allows direct bookmarking and microhistory. */ AJAX.setUrlHash = (function (jQuery, window) { + "use strict"; /** * Indictaes whether we have already completed * the initialisation of the hash