1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * An implementation of a client-side page cache.
4 * This object also uses the cache to provide a simple microhistory,
5 * that is the ability to use the back and forward buttons in the browser
9 * @var int The maximum number of pages to keep in the cache
13 * @var object A hash used to prime the cache with data about the initially
14 * loaded page. This is set in the footer, and then loaded
15 * by a double-queued event further down this file.
19 * @var array Stores the content of the cached pages
23 * @var int The index of the currently loaded page
24 * This is used to know at which point in the history we are
28 * Saves a new page in the cache
30 * @param string hash The hash part of the url that is being loaded
31 * @param array scripts A list of scripts that is required for the page
32 * @param string menu A hash that links to a menu stored
33 * in a dedicated menu cache
34 * @param array params A list of parameters used by PMA_commonParams()
35 * @param string rel A relationship to the current page:
36 * 'samepage': Forces the response to be treated as
37 * the same page as the current one
38 * 'newpage': Forces the response to be treated as
40 * undefined: Default behaviour, 'samepage' if the
41 * selflinks of the two pages are the same.
46 add: function (hash, scripts, menu, params, rel) {
47 if (this.pages.length > PMA_MicroHistory.MAX) {
48 // Trim the cache, to the maximum number of allowed entries
49 // This way we will have a cached menu for every page
50 for (var i = 0; i < this.pages.length - this.MAX; i++) {
54 while (this.current < this.pages.length) {
55 // trim the cache if we went back in the history
56 // and are now going forward again
59 if (rel === 'newpage' ||
61 typeof rel === 'undefined' && (
62 typeof this.pages[this.current - 1] === 'undefined' ||
63 this.pages[this.current - 1].hash !== hash
69 content: $('#page_content').html(),
71 selflink: $('#selflink').html(),
75 PMA_SetUrlHash(this.current, hash);
80 * Restores a page from the cache. This is called when the hash
81 * part of the url changes and it's structure appears to be valid
83 * @param string index Which page from the history to load
87 navigate: function (index) {
88 if (typeof this.pages[index] === 'undefined' ||
89 typeof this.pages[index].content === 'undefined' ||
90 typeof this.pages[index].menu === 'undefined' ||
91 ! PMA_MicroHistory.menus.get(this.pages[index].menu)
94 '<div class="error">' + PMA_messages.strInvalidPage + '</div>',
99 var record = this.pages[index];
100 AJAX.scriptHandler.reset(function () {
101 $('#page_content').html(record.content);
102 $('#selflink').html(record.selflink);
103 PMA_MicroHistory.menus.replace(PMA_MicroHistory.menus.get(record.menu));
104 PMA_commonParams.setAll(record.params);
105 AJAX.scriptHandler.load(record.scripts);
106 PMA_MicroHistory.current = ++index;
111 * Resaves the content of the current page in the cache.
112 * Necessary in order not to show the user some outdated version of the page
116 update: function () {
117 var page = this.pages[this.current - 1];
119 page.content = $('#page_content').html();
123 * @var object Dedicated menu cache
127 * Returns the number of items in an associative array
131 size: function (obj) {
135 if (obj.hasOwnProperty(key)) {
142 * @var hash Stores the content of the cached menus
146 * Saves a new menu in the cache
148 * @param string hash The hash (trimmed md5) of the menu to be saved
149 * @param string content The HTML code of the menu to be saved
153 add: function (hash, content) {
154 if (this.size(this.data) > PMA_MicroHistory.MAX) {
155 // when the cache grows, we remove the oldest entry
159 for (var i in this.data) {
161 if (! init || this.data[i].timestamp.getTime() < oldest.getTime()) {
162 oldest = this.data[i].timestamp;
168 delete this.data[key];
172 timestamp: new Date()
176 * Retrieves a menu given its hash
178 * @param string hash The hash of the menu to be retrieved
182 get: function (hash) {
183 if (this.data[hash]) {
184 return this.data[hash].content;
186 // This should never happen as long as the number of stored menus
187 // is larger or equal to the number of pages in the page cache
192 * Prepares part of the parameter string used during page requests,
193 * this is necessary to tell the server which menus we have in the cache
197 getRequestParam: function () {
200 for (var i in this.data) {
203 var menuHashesParam = menuHashes.join('-');
204 if (menuHashesParam) {
205 param = PMA_commonParams.get('arg_separator') + 'menuHashes=' + menuHashesParam;
210 * Replaces the menu with new content
214 replace: function (content) {
215 $('#floating_menubar').html(content)
216 // Remove duplicate wrapper
217 // TODO: don't send it in the response
218 .children().first().remove();
219 $('#topmenu').menuResizer(PMA_mainMenuResizerCallback);
225 * URL hash management module.
226 * Allows direct bookmarking and microhistory.
228 PMA_SetUrlHash = (function (jQuery, window) {
231 * Indictaes whether we have already completed
232 * the initialisation of the hash
238 * Stores a hash that needed to be set when we were not ready
244 * Flag to indicate if the change of hash was triggered
245 * by a user pressing the back/forward button or if
246 * the change was triggered internally
250 var userChange = true;
252 // Fix favicon disappearing in Firefox when setting location.hash
253 function resetFavicon () {
254 if (navigator.userAgent.indexOf('Firefox') > -1) {
255 // Move the link tags for the favicon to the bottom
256 // of the head element to force a reload of the favicon
257 $('head > link[href="favicon\\.ico"]').appendTo('head');
262 * Sets the hash part of the URL
266 function setUrlHash (index, hash) {
269 * Setting hash leads to reload in webkit:
270 * http://www.quirksmode.org/bugreports/archives/2005/05/Safari_13_visual_anomaly_with_windowlocationhref.html
272 * so we expect that users are not running an ancient Safari version
277 window.location.hash = 'PMAURL-' + index + ':' + hash;
280 savedHash = 'PMAURL-' + index + ':' + hash;
284 * Start initialisation
286 var urlhash = window.location.hash;
287 if (urlhash.substring(0, 8) === '#PMAURL-') {
288 // We have a valid hash, let's redirect the user
289 // to the page that it's pointing to
290 var colon_position = urlhash.indexOf(':');
291 var questionmark_position = urlhash.indexOf('?');
292 if (colon_position !== -1 && questionmark_position !== -1 && colon_position < questionmark_position) {
293 var hash_url = urlhash.substring(colon_position + 1, questionmark_position);
294 if (PMA_gotoWhitelist.indexOf(hash_url) !== -1) {
295 window.location = urlhash.substring(
301 // We don't have a valid hash, so we'll set it up
302 // when the page finishes loading
304 /* Check if we should set URL */
305 if (savedHash !== '') {
306 window.location.hash = savedHash;
310 // Indicate that we're done initialising
315 * Register an event handler for when the url hash changes
318 jQuery(window).hashchange(function () {
319 if (userChange === false) {
320 // Ignore internally triggered hash changes
322 } else if (/^#PMAURL-\d+:/.test(window.location.hash)) {
323 // Change page if the hash changed was triggered by a user action
324 var index = window.location.hash.substring(
325 8, window.location.hash.indexOf(':')
327 PMA_MicroHistory.navigate(index);
332 * Publicly exposes a reference to the otherwise private setUrlHash function