history.js revision 13c4505d6df2d3db68248902959be8c92998d0fc
/*global YUI*/
/**
* navigation buttons in a DHTML application. It also allows a DHTML
* application to be bookmarked in a specific state.
*
* This utility requires the following static markup:
*
* <iframe id="yui-history-iframe" src="path-to-real-asset-in-same-domain"></iframe>
* <input id="yui-history-field" type="hidden">
*
* @module history
*/
var L = Y.Lang,
A = Y.UA,
H, G,
// YUI Compressor helper...
E_MISSING_OR_INVALID_ARG = "Missing or invalid argument",
// Regular expression used to parse query strings and such.
REGEXP = /([^=&]+)=([^&]*)/g,
/**
* @event history:ready
* @description Fires when the browser history utility is ready
* @type Event.Custom
*/
EV_HISTORY_READY = "history:ready",
/**
* @event history:globalStateChange
* @description Fires when the global state of the page has changed (that is,
* when the state of at least one browser history module has changed)
* @type Event.Custom
*/
EV_HISTORY_GLOBAL_STATE_CHANGE = "history:globalStateChange",
/**
* @event history:moduleStateChange
* @description Fires when the state of a history module object has changed
* @type Event.Custom
*/
EV_HISTORY_MODULE_STATE_CHANGE = "history:moduleStateChange";
// Flag used to tell whether the history utility is ready to be used.
ready: false,
// List of registered modules.
_modules: [],
// List of fully qualified states. This is used only by Safari.
_fqstates: [],
// INPUT field (with type="hidden" or type="text") or TEXTAREA.
// This field keeps the value of the initial state, current state
// the list of all states across pages within a single browser session.
_stateField: null,
// Hidden IFrame used to store the browsing history on IE6/7.
_historyIFrame: null
};
}
/**
* Returns the portion of the hash after the '#' symbol.
* @method _getHash
* @return {string} The hash portion of the document's location
* @private
*/
function _getHash() {
}
/**
* Stores the initial state and current state for all registered modules.
* On Safari, we also store all the fully qualified states visited by
* the application within a single browser session. The storage takes
* place in the form field specified during initialization.
* @method _storeStates
* @private
*/
function _storeStates() {
var initialStates = [], currentStates = [], s;
});
if (A.webkit) {
}
}
/**
* Sets the new currentState attribute of all modules depending on the new fully
* qualified state. Also notifies the modules which current state has changed.
* @method _handleFQStateChange
* @param {string} fqstate fully qualified state
* @private
*/
function _handleFQStateChange(fqstate) {
var m, modules;
if (!fqstate) {
});
return;
}
modules = [];
}
}
});
}
/**
* Update the IFrame with our new state.
* @method _updateIFrame
* @private
* @return {boolean} true if successful. false otherwise.
*/
function _updateIFrame(fqstate) {
try {
// TODO: The Node API should expose these methods in the very near future...
return true;
} catch (e) {
return false;
}
}
/**
* Periodically checks whether our internal IFrame is ready to be used
* @method _checkIframeLoaded
* @private
*/
function _checkIframeLoaded() {
// Check again in 10 msec...
return;
}
// Start the thread that will have the responsibility to
// periodically check whether a navigate operation has been
// requested on the main window. This will happen when
// History.navigate has been called or after the user has
// We must use innerText, and not innerHTML because our string contains
// the "&" character (which would end up being escaped as "&") and
// the string comparison would fail...
setInterval(function () {
// See my comment above about using innerText instead of innerHTML...
if (newfqstate !== fqstate) {
if (!fqstate) {
states = [];
});
} else {
}
// Allow the state to be bookmarked by setting the top window's
// URL fragment identifier. Note that here, we are on IE < 8
// which does not touch the browser history when changing the
// hash (unlike all the other browsers).
_storeStates();
// The hash has changed. The user might have clicked on a link,
// or modified the URL directly, or opened the same application
// bookmarked in a specific state using a bookmark. However, we
// know the hash change was not caused by a hit on the back or
// forward buttons, or by a call to navigate() (because it would
// have been handled above) We must handle these cases, which is
// why we also need to keep track of hash changes on IE!
// Note that IE6 has some major issues with this kind of user
// interaction (the history stack gets completely messed up)
// but it seems to work fine on IE7.
// Now, store a new history entry. The following will cause the
// code above to execute, doing all the dirty work for us...
}
}, 50);
G.ready = true;
H.fire(EV_HISTORY_READY);
}
/**
* Finish up the initialization of the browser utility library.
* @method _initialize
* @private
*/
function _initialize() {
// Decode the content of our storage field...
moduleId = m[1];
initialState = m[2];
if (module) {
}
}
moduleId = m[1];
currentState = m[2];
if (module) {
}
}
}
}
if (A.ie) {
// IE < 8 or IE8 in quirks mode or IE7 standards mode
} else {
// IE8 in IE8 standards mode
Y.on("hashchange", function () {
_storeStates();
}, window);
G.ready = true;
H.fire(EV_HISTORY_READY);
}
} else {
// Start the thread that will have the responsibility to
// periodically check whether a navigate operation has been
// requested on the main window. This will happen when
// YAHOO.util.History.navigate has been called or after
// operation is to watch history.length... We basically exploit
// what I consider to be a bug (history.length is not supposed
// why, in the following thread, we first compare the hash,
// because the hash thing will be fixed in the next major
// version of Safari. So even if they fix the history.length
// bug, all this will still work!
// On Gecko and Opera, we just need to watch the hash...
setInterval(function () {
_storeStates();
_storeStates();
}
}, 50);
G.ready = true;
H.fire(EV_HISTORY_READY);
}
}
/**
* This class represents an instance of the browser history utility.
* @class History.Module
* @constructor
* @param id {String} the module identifier
* @param initialState {String} the module's initial state
*/
H = {
/**
* Registers a new module.
* @method register
* @param {string} moduleId Non-empty string uniquely identifying the
* module you wish to register.
* @param {string} initialState The initial state of the specified
* module corresponding to its earliest history entry.
* @return {History.Module} The newly registered module
*/
var module;
throw new Error(E_MISSING_OR_INVALID_ARG);
}
// The module seems to have already been registered.
return;
}
// Note: A module CANNOT be registered once the browser history
// utility has been initialized. We could relax this in the future,
// but that would mean that some states may be lost once the user
// leaves the page and then comes back to it.
if (G.ready) {
Y.log("All modules must be registered before initializing the browser history utility", "info", "history");
return null;
}
// Make sure the strings passed in do not contain our separators "," and "|"
return module;
},
/**
* Initializes the Browser History Manager. Call this method
* from a script block located right after the opening body tag.
* @method initialize
* @param {string|HTML Element} stateField <input type="hidden"> used
* to store application states. Must be in the static markup.
* @param {string|HTML Element} historyIFrame IFrame used to store
* the history (only required for IE6/7)
* @public
*/
if (G.ready) {
// The browser history utility has already been initialized.
return true;
}
if (!stateField) {
throw new Error(E_MISSING_OR_INVALID_ARG);
}
throw new Error(E_MISSING_OR_INVALID_ARG);
}
// IE < 8 or IE8 in quirks mode or IE7 standards mode
throw new Error(E_MISSING_OR_INVALID_ARG);
}
}
// it in compatible mode. This makes anchor-based history
// navigation work after the page has been navigated away
// from and re-activated, at the cost of slowing down
}
G._stateField = stateField;
return true;
},
/**
* Stores a new entry in the browser history by changing the state of a registered module.
* @method navigate
* @param {string} module Non-empty string representing your module.
* @param {string} state String representing the new state of the specified module.
* @return {boolean} Indicates whether the new state was successfully added to the history.
* @public
*/
var states;
throw new Error(E_MISSING_OR_INVALID_ARG);
}
states = {};
return H.multiNavigate(states);
},
/**
* Stores a new entry in the browser history by changing the state
* of several registered modules in one atomic operation.
* @method multiNavigate
* @param {object} states Associative array of module-state pairs to set simultaneously.
* @return {boolean} Indicates whether the new state was successfully added to the history.
* @public
*/
multiNavigate: function (states) {
var currentStates, fqstate;
if (!G.ready) {
return false;
}
Y.log("The following module has not been registered with the browser history utility: " + moduleId, "info", "history");
}
});
// Generate our new full state string mod1=xxx&mod2=yyy
currentStates = [];
var currentState;
} else {
}
// Make sure the strings passed in do not contain our separators "," and "|"
});
// IE < 8 or IE8 in quirks mode or IE7 standards mode
return _updateIFrame(fqstate);
} else {
// Known bug: On Safari 1.x and 2.0, if you have tab browsing
// enabled, Safari will show an endless loading icon in the
// tab. This has apparently been fixed in recent WebKit builds.
// One work around found by Dav Glass is to submit a form that
// points to the same document. This indeed works on Safari 1.x
// and 2.0 but creates bigger problems on WebKit. So for now,
// we'll consider this an acceptable bug, and hope that Apple
// comes out with their next version of Safari very soon.
if (A.webkit) {
// The following two lines are only useful for Safari 1.x
// and 2.0. Recent nightly builds of WebKit do not require
// that, but unfortunately, it is not easy to differentiate
// between the two. Once Safari 2.0 departs the A-grade
// list, we can remove the following two lines...
_storeStates();
}
return true;
}
},
/**
* Returns the current state of the specified module.
* @method getCurrentState
* @param {string} moduleId Non-empty string representing your module.
* @return {string} The current state of the specified module.
* @public
*/
getCurrentState: function (moduleId) {
var module;
throw new Error(E_MISSING_OR_INVALID_ARG);
}
if (!G.ready) {
return null;
}
if (!module) {
return null;
}
},
/**
* Returns the state of a module according to the URL fragment
* identifier. This method is useful to initialize your modules
* if your application was bookmarked from a particular state.
* @method getBookmarkedState
* @param {string} moduleId Non-empty string representing your module.
* @return {string} The bookmarked state of the specified module.
* @public
*/
getBookmarkedState: function (moduleId) {
var m, i, h;
throw new Error(E_MISSING_OR_INVALID_ARG);
}
// Use location.href instead of location.hash which is already
// URL-decoded, which creates problems if the state value
// contained special characters...
i = h.indexOf("#");
if (i >= 0) {
h = h.substr(i + 1);
if (m[1] === moduleId) {
return decodeURIComponent(m[2]);
}
}
}
return null;
},
/**
* Returns the value of the specified query string parameter.
* This method is not used internally by the Browser History Manager.
* However, it is provided here as a helper since many applications
* using the Browser History Manager will want to read the value of
* url parameters to initialize themselves.
* @method getQueryStringParameter
* @param {string} paramName Name of the parameter we want to look up.
* @param {string} queryString Optional URL to look at. If not specified,
* this method uses the URL in the address bar.
* @return {string} The value of the specified parameter, or null.
* @public
*/
var m, q, i;
// Remove the hash if any
i = q.lastIndexOf("#");
if (m[1] === paramName) {
return decodeURIComponent(m[2]);
}
}
return null;
}
};
// Make Y.History an event target
/**
* This class represents a browser history module.
* @class History.Module
* @constructor
* @param id {String} the module identifier
* @param initialState {String} the module's initial state
*/
/**
* The module identifier
* @type String
*/
/**
* The module's initial state
* @type String
*/
this.initialState = initialState;
/**
* The module's current state
* @type String
*/
this.currentState = initialState;
};
Y.History = H;