2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('history-html5', function (Y, NAME) {
11 * Provides browser history management using the HTML5 history API.
14 * @submodule history-html5
20 * Provides browser history management using the HTML5 history API.
24 * When calling the <code>add()</code>, <code>addValue()</code>,
25 * <code>replace()</code>, or <code>replaceValue()</code> methods on
26 * <code>HistoryHTML5</code>, the following additional options are supported:
30 * <dt><strong>title (String)</strong></dt>
32 * Title to use for the new history entry. Browsers will typically display
33 * this title to the user in the detailed history window or in a dropdown
34 * menu attached to the back/forward buttons. If not specified, the title
35 * of the current document will be used.
38 * <dt><strong>url (String)</strong></dt>
40 * URL to display to the user for the new history entry. This URL will be
41 * visible in the browser's address bar and will be the bookmarked URL if
42 * the user bookmarks the page. It may be a relative path ("foo/bar"), an
43 * absolute path ("/foo/bar"), or a full URL ("http://example.com/foo/bar").
44 * If you specify a full URL, the origin <i>must</i> be the same as the
45 * origin of the current page, or an error will occur. If no URL is
46 * specified, the current URL will not be changed.
51 * @extends HistoryBase
53 * @param {Object} config (optional) Configuration object.
56 var HistoryBase = Y.HistoryBase,
59 useHistoryHTML5 = Y.config.useHistoryHTML5,
61 SRC_POPSTATE = 'popstate',
62 SRC_REPLACE = HistoryBase.SRC_REPLACE;
64 function HistoryHTML5() {
65 HistoryHTML5.superclass.constructor.apply(this, arguments);
68 Y.extend(HistoryHTML5, HistoryBase, {
69 // -- Initialization -------------------------------------------------------
70 _init: function (config) {
74 bookmarkedState = win.history.state;
76 bookmarkedState = null;
79 // Treat empty state objects as `null` so they're not processed further.
80 if (Y.Object.isEmpty(bookmarkedState)) {
81 bookmarkedState = null;
84 config || (config = {});
86 // If both the initial state and the bookmarked state are objects, merge
87 // them (bookmarked state wins).
88 if (config.initialState
89 && Lang.type(config.initialState) === 'object'
90 && Lang.type(bookmarkedState) === 'object') {
92 this._initialState = Y.merge(config.initialState, bookmarkedState);
94 // Otherwise, the bookmarked state always wins if there is one. If
95 // there isn't a bookmarked state, history-base will take care of
96 // falling back to config.initialState or null.
97 this._initialState = bookmarkedState;
100 Y.on('popstate', this._onPopState, win, this);
102 HistoryHTML5.superclass._init.apply(this, arguments);
105 // -- Protected Methods ----------------------------------------------------
108 * Overrides HistoryBase's <code>_storeState()</code> and pushes or replaces
109 * a history entry using the HTML5 history API when necessary.
111 * @method _storeState
112 * @param {String} src Source of the changes.
113 * @param {Object} newState New state to store.
114 * @param {Object} options Zero or more options.
117 _storeState: function (src, newState, options) {
118 if (src !== SRC_POPSTATE) {
119 win.history[src === SRC_REPLACE ? 'replaceState' : 'pushState'](
121 options.title || Y.config.doc.title || '',
122 options.url || Y.config.doc.URL
126 HistoryHTML5.superclass._storeState.apply(this, arguments);
129 // -- Protected Event Handlers ---------------------------------------------
132 * Handler for popstate events.
134 * @method _onPopState
138 _onPopState: function (e) {
139 this._resolveChanges(SRC_POPSTATE, e._event.state || null);
142 // -- Public Static Properties ---------------------------------------------
143 NAME: 'historyhtml5',
146 * Constant used to identify state changes originating from
147 * <code>popstate</code> events.
149 * @property SRC_POPSTATE
154 SRC_POPSTATE: SRC_POPSTATE
157 if (!Y.Node.DOM_EVENTS.popstate) {
158 Y.Node.DOM_EVENTS.popstate = 1;
161 Y.HistoryHTML5 = HistoryHTML5;
165 * If <code>true</code>, the <code>Y.History</code> alias will always point to
166 * <code>Y.HistoryHTML5</code> when the history-html5 module is loaded, even if
167 * the current browser doesn't support HTML5 history.
171 * If <code>false</code>, the <code>Y.History</code> alias will always point to
172 * <code>Y.HistoryHash</code> when the history-hash module is loaded, even if
173 * the current browser supports HTML5 history.
177 * If neither <code>true</code> nor <code>false</code>, the
178 * <code>Y.History</code> alias will point to the best available history adapter
179 * that the browser supports. This is the default behavior.
182 * @property useHistoryHTML5
188 // HistoryHTML5 will always win over HistoryHash unless useHistoryHTML5 is false
189 // or HTML5 history is not supported.
190 if (useHistoryHTML5 === true || (useHistoryHTML5 !== false &&
191 HistoryBase.html5)) {
192 Y.History = HistoryHTML5;
196 }, '3.13.0', {"optional": ["json"], "requires": ["event-base", "history-base", "node-base"]});