3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('history-html5', function(Y) {
10
* Provides browser history management using the HTML5 history API.
13
* @submodule history-html5
19
* Provides browser history management using the HTML5 history API.
23
* When calling the <code>add()</code>, <code>addValue()</code>,
24
* <code>replace()</code>, or <code>replaceValue()</code> methods on
25
* <code>HistoryHTML5</code>, the following additional options are supported:
29
* <dt><strong>title (String)</strong></dt>
31
* Title to use for the new history entry. Browsers will typically display
32
* this title to the user in the detailed history window or in a dropdown
33
* menu attached to the back/forward buttons. If not specified, the title
34
* of the current document will be used.
37
* <dt><strong>url (String)</strong></dt>
39
* URL to display to the user for the new history entry. This URL will be
40
* visible in the browser's address bar and will be the bookmarked URL if
41
* the user bookmarks the page. It may be a relative path ("foo/bar"), an
42
* absolute path ("/foo/bar"), or a full URL ("http://example.com/foo/bar").
43
* If you specify a full URL, the origin <i>must</i> be the same as the
44
* origin of the current page, or an error will occur. If no URL is
45
* specified, the current URL will not be changed.
50
* @extends HistoryBase
52
* @param {Object} config (optional) Configuration object.
55
var HistoryBase = Y.HistoryBase,
58
useHistoryHTML5 = Y.config.useHistoryHTML5,
60
SRC_POPSTATE = 'popstate',
61
SRC_REPLACE = HistoryBase.SRC_REPLACE;
63
function HistoryHTML5() {
64
HistoryHTML5.superclass.constructor.apply(this, arguments);
67
Y.extend(HistoryHTML5, HistoryBase, {
68
// -- Initialization -------------------------------------------------------
69
_init: function (config) {
70
var bookmarkedState = win.history.state;
72
config || (config = {});
74
// If both the initial state and the bookmarked state are objects, merge
75
// them (bookmarked state wins).
76
if (config.initialState
77
&& Lang.type(config.initialState) === 'object'
78
&& Lang.type(bookmarkedState) === 'object') {
80
this._initialState = Y.merge(config.initialState, bookmarkedState);
82
// Otherwise, the bookmarked state always wins if there is one. If
83
// there isn't a bookmarked state, history-base will take care of
84
// falling back to config.initialState or null.
85
this._initialState = bookmarkedState;
88
Y.on('popstate', this._onPopState, win, this);
90
HistoryHTML5.superclass._init.apply(this, arguments);
93
// -- Protected Methods ----------------------------------------------------
96
* Overrides HistoryBase's <code>_storeState()</code> and pushes or replaces
97
* a history entry using the HTML5 history API when necessary.
100
* @param {String} src Source of the changes.
101
* @param {Object} newState New state to store.
102
* @param {Object} options Zero or more options.
105
_storeState: function (src, newState, options) {
106
if (src !== SRC_POPSTATE) {
107
win.history[src === SRC_REPLACE ? 'replaceState' : 'pushState'](
109
options.title || Y.config.doc.title || '',
114
HistoryHTML5.superclass._storeState.apply(this, arguments);
117
// -- Protected Event Handlers ---------------------------------------------
120
* Handler for popstate events.
122
* @method _onPopState
126
_onPopState: function (e) {
127
this._resolveChanges(SRC_POPSTATE, e._event.state || null);
130
// -- Public Static Properties ---------------------------------------------
131
NAME: 'historyhtml5',
134
* Constant used to identify state changes originating from
135
* <code>popstate</code> events.
137
* @property SRC_POPSTATE
142
SRC_POPSTATE: SRC_POPSTATE
145
if (!Y.Node.DOM_EVENTS.popstate) {
146
Y.Node.DOM_EVENTS.popstate = 1;
149
Y.HistoryHTML5 = HistoryHTML5;
153
* If <code>true</code>, the <code>Y.History</code> alias will always point to
154
* <code>Y.HistoryHTML5</code> when the history-html5 module is loaded, even if
155
* the current browser doesn't support HTML5 history.
159
* If <code>false</code>, the <code>Y.History</code> alias will always point to
160
* <code>Y.HistoryHash</code> when the history-hash module is loaded, even if
161
* the current browser supports HTML5 history.
165
* If neither <code>true</code> nor <code>false</code>, the
166
* <code>Y.History</code> alias will point to the best available history adapter
167
* that the browser supports. This is the default behavior.
170
* @property useHistoryHTML5
176
// HistoryHTML5 will always win over HistoryHash unless useHistoryHTML5 is false
177
// or HTML5 history is not supported.
178
if (useHistoryHTML5 === true || (useHistoryHTML5 !== false &&
179
HistoryBase.html5)) {
180
Y.History = HistoryHTML5;
184
}, '3.5.1' ,{optional:['json'], requires:['event-base', 'history-base', 'node-base']});