/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html version: 3.2.0 build: 2676 */ YUI.add('history-html5', function(Y) { /** * Provides browser history management using the HTML5 history API. * * @module history * @submodule history-html5 * @since 3.2.0 */ /** *
* Provides browser history management using the HTML5 history API. *
* *
* When calling the add()
, addValue()
,
* replace()
, or replaceValue()
methods on
* HistoryHTML5
, the following additional options are supported:
*
HistoryHTML5
-specific properties are supported in addition to
* those supported by HistoryBase
:
*
*
* Set this to true
to store the most recent history state in
* sessionStorage in order to seamlessly restore the previous state (if any)
* when HistoryHTML5
is instantiated after a
* window.onpopstate
event has already fired.
*
* By default, this setting is false
.
*
enableSessionFallback
config property is true
* and either Y.JSON
or native JSON support is available and
* session storage is supported.
*
* @method _storeSessionState
* @param {mixed} state State to store. May be any type serializable to
* JSON.
* @protected
*/
_storeSessionState: function (state) {
if (this._config[ENABLE_FALLBACK] && JSON && sessionStorage) {
sessionStorage[this._getSessionKey()] = JSON.stringify(state || null);
}
},
/**
* Overrides HistoryBase's _storeState()
and pushes or replaces
* a history entry using the HTML5 history API when necessary.
*
* @method _storeState
* @param {String} src Source of the changes.
* @param {Object} newState New state to store.
* @param {Object} options Zero or more options.
* @protected
*/
_storeState: function (src, newState, options) {
if (src !== SRC_POPSTATE) {
win.history[src === SRC_REPLACE ? 'replaceState' : 'pushState'](
newState, options.title || doc.title || '', options.url || null
);
}
this._storeSessionState(newState);
HistoryHTML5.superclass._storeState.apply(this, arguments);
},
// -- Protected Event Handlers ---------------------------------------------
/**
* Handler for popstate events.
*
* @method _onPopState
* @param {Event} e
* @protected
*/
_onPopState: function (e) {
var state = e._event.state;
this._storeSessionState(state);
this._resolveChanges(SRC_POPSTATE, state || null);
}
}, {
// -- Public Static Properties ---------------------------------------------
NAME: 'historyhtml5',
/**
* Constant used to identify state changes originating from
* popstate
events.
*
* @property SRC_POPSTATE
* @type String
* @static
* @final
*/
SRC_POPSTATE: SRC_POPSTATE
});
if (!Y.Node.DOM_EVENTS.popstate) {
Y.Node.DOM_EVENTS.popstate = 1;
}
Y.HistoryHTML5 = HistoryHTML5;
/**
*
* If true
, the Y.History
alias will always point to
* Y.HistoryHTML5
when the history-html5 module is loaded, even if
* the current browser doesn't support HTML5 history.
*
* If false
, the Y.History
alias will always point to
* Y.HistoryHash
when the history-hash module is loaded, even if
* the current browser supports HTML5 history.
*
* If neither true
nor false
, the
* Y.History
alias will point to the best available history adapter
* that the browser supports. This is the default behavior.
*