MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / history-html5 / history-html5-debug.js
blobe6b0f31a0c95ce8d5336624db35002b2f86dd79c
1 /*
2 YUI 3.5.1 (build 22)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('history-html5', function(Y) {
9 /**
10  * Provides browser history management using the HTML5 history API.
11  *
12  * @module history
13  * @submodule history-html5
14  * @since 3.2.0
15  */
17 /**
18  * <p>
19  * Provides browser history management using the HTML5 history API.
20  * </p>
21  *
22  * <p>
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:
26  * </p>
27  *
28  * <dl>
29  *   <dt><strong>title (String)</strong></dt>
30  *   <dd>
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.
35  *   </dd>
36  *
37  *   <dt><strong>url (String)</strong></dt>
38  *   <dd>
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.
46  *   </dd>
47  * </dl>
48  *
49  * @class HistoryHTML5
50  * @extends HistoryBase
51  * @constructor
52  * @param {Object} config (optional) Configuration object.
53  */
55 var HistoryBase     = Y.HistoryBase,
56     Lang            = Y.Lang,
57     win             = Y.config.win,
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);
81         } else {
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;
86         }
88         Y.on('popstate', this._onPopState, win, this);
90         HistoryHTML5.superclass._init.apply(this, arguments);
91     },
93     // -- Protected Methods ----------------------------------------------------
95     /**
96      * Overrides HistoryBase's <code>_storeState()</code> and pushes or replaces
97      * a history entry using the HTML5 history API when necessary.
98      *
99      * @method _storeState
100      * @param {String} src Source of the changes.
101      * @param {Object} newState New state to store.
102      * @param {Object} options Zero or more options.
103      * @protected
104      */
105     _storeState: function (src, newState, options) {
106         if (src !== SRC_POPSTATE) {
107             win.history[src === SRC_REPLACE ? 'replaceState' : 'pushState'](
108                 newState,
109                 options.title || Y.config.doc.title || '',
110                 options.url || null
111             );
112         }
114         HistoryHTML5.superclass._storeState.apply(this, arguments);
115     },
117     // -- Protected Event Handlers ---------------------------------------------
119     /**
120      * Handler for popstate events.
121      *
122      * @method _onPopState
123      * @param {Event} e
124      * @protected
125      */
126     _onPopState: function (e) {
127         this._resolveChanges(SRC_POPSTATE, e._event.state || null);
128     }
129 }, {
130     // -- Public Static Properties ---------------------------------------------
131     NAME: 'historyhtml5',
133     /**
134      * Constant used to identify state changes originating from
135      * <code>popstate</code> events.
136      *
137      * @property SRC_POPSTATE
138      * @type String
139      * @static
140      * @final
141      */
142     SRC_POPSTATE: SRC_POPSTATE
145 if (!Y.Node.DOM_EVENTS.popstate) {
146     Y.Node.DOM_EVENTS.popstate = 1;
149 Y.HistoryHTML5 = HistoryHTML5;
152  * <p>
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.
156  * </p>
158  * <p>
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.
162  * </p>
164  * <p>
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.
168  * </p>
170  * @property useHistoryHTML5
171  * @type boolean
172  * @for config
173  * @since 3.2.0
174  */
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']});