1 |
efrain |
1 |
YUI.add('history-html5', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Provides browser history management using the HTML5 history API.
|
|
|
5 |
*
|
|
|
6 |
* @module history
|
|
|
7 |
* @submodule history-html5
|
|
|
8 |
* @since 3.2.0
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* <p>
|
|
|
13 |
* Provides browser history management using the HTML5 history API.
|
|
|
14 |
* </p>
|
|
|
15 |
*
|
|
|
16 |
* <p>
|
|
|
17 |
* When calling the <code>add()</code>, <code>addValue()</code>,
|
|
|
18 |
* <code>replace()</code>, or <code>replaceValue()</code> methods on
|
|
|
19 |
* <code>HistoryHTML5</code>, the following additional options are supported:
|
|
|
20 |
* </p>
|
|
|
21 |
*
|
|
|
22 |
* <dl>
|
|
|
23 |
* <dt><strong>title (String)</strong></dt>
|
|
|
24 |
* <dd>
|
|
|
25 |
* Title to use for the new history entry. Browsers will typically display
|
|
|
26 |
* this title to the user in the detailed history window or in a dropdown
|
|
|
27 |
* menu attached to the back/forward buttons. If not specified, the title
|
|
|
28 |
* of the current document will be used.
|
|
|
29 |
* </dd>
|
|
|
30 |
*
|
|
|
31 |
* <dt><strong>url (String)</strong></dt>
|
|
|
32 |
* <dd>
|
|
|
33 |
* URL to display to the user for the new history entry. This URL will be
|
|
|
34 |
* visible in the browser's address bar and will be the bookmarked URL if
|
|
|
35 |
* the user bookmarks the page. It may be a relative path ("foo/bar"), an
|
|
|
36 |
* absolute path ("/foo/bar"), or a full URL ("http://example.com/foo/bar").
|
|
|
37 |
* If you specify a full URL, the origin <i>must</i> be the same as the
|
|
|
38 |
* origin of the current page, or an error will occur. If no URL is
|
|
|
39 |
* specified, the current URL will not be changed.
|
|
|
40 |
* </dd>
|
|
|
41 |
* </dl>
|
|
|
42 |
*
|
|
|
43 |
* @class HistoryHTML5
|
|
|
44 |
* @extends HistoryBase
|
|
|
45 |
* @constructor
|
|
|
46 |
* @param {Object} config (optional) Configuration object.
|
|
|
47 |
*/
|
|
|
48 |
|
|
|
49 |
var HistoryBase = Y.HistoryBase,
|
|
|
50 |
Lang = Y.Lang,
|
|
|
51 |
win = Y.config.win,
|
|
|
52 |
useHistoryHTML5 = Y.config.useHistoryHTML5,
|
|
|
53 |
|
|
|
54 |
SRC_POPSTATE = 'popstate',
|
|
|
55 |
SRC_REPLACE = HistoryBase.SRC_REPLACE;
|
|
|
56 |
|
|
|
57 |
function HistoryHTML5() {
|
|
|
58 |
HistoryHTML5.superclass.constructor.apply(this, arguments);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
Y.extend(HistoryHTML5, HistoryBase, {
|
|
|
62 |
// -- Initialization -------------------------------------------------------
|
|
|
63 |
_init: function (config) {
|
|
|
64 |
var bookmarkedState;
|
|
|
65 |
|
|
|
66 |
try {
|
|
|
67 |
bookmarkedState = win.history.state;
|
|
|
68 |
} catch(e) {
|
|
|
69 |
bookmarkedState = null;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
// Treat empty state objects as `null` so they're not processed further.
|
|
|
73 |
if (Y.Object.isEmpty(bookmarkedState)) {
|
|
|
74 |
bookmarkedState = null;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
config || (config = {});
|
|
|
78 |
|
|
|
79 |
// If both the initial state and the bookmarked state are objects, merge
|
|
|
80 |
// them (bookmarked state wins).
|
|
|
81 |
if (config.initialState
|
|
|
82 |
&& Lang.type(config.initialState) === 'object'
|
|
|
83 |
&& Lang.type(bookmarkedState) === 'object') {
|
|
|
84 |
|
|
|
85 |
this._initialState = Y.merge(config.initialState, bookmarkedState);
|
|
|
86 |
} else {
|
|
|
87 |
// Otherwise, the bookmarked state always wins if there is one. If
|
|
|
88 |
// there isn't a bookmarked state, history-base will take care of
|
|
|
89 |
// falling back to config.initialState or null.
|
|
|
90 |
this._initialState = bookmarkedState;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
Y.on('popstate', this._onPopState, win, this);
|
|
|
94 |
|
|
|
95 |
HistoryHTML5.superclass._init.apply(this, arguments);
|
|
|
96 |
},
|
|
|
97 |
|
|
|
98 |
// -- Protected Methods ----------------------------------------------------
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Overrides HistoryBase's <code>_storeState()</code> and pushes or replaces
|
|
|
102 |
* a history entry using the HTML5 history API when necessary.
|
|
|
103 |
*
|
|
|
104 |
* @method _storeState
|
|
|
105 |
* @param {String} src Source of the changes.
|
|
|
106 |
* @param {Object} newState New state to store.
|
|
|
107 |
* @param {Object} options Zero or more options.
|
|
|
108 |
* @protected
|
|
|
109 |
*/
|
|
|
110 |
_storeState: function (src, newState, options) {
|
|
|
111 |
if (src !== SRC_POPSTATE) {
|
|
|
112 |
win.history[src === SRC_REPLACE ? 'replaceState' : 'pushState'](
|
|
|
113 |
newState,
|
|
|
114 |
options.title || Y.config.doc.title || '',
|
|
|
115 |
options.url || Y.config.doc.URL
|
|
|
116 |
);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
HistoryHTML5.superclass._storeState.apply(this, arguments);
|
|
|
120 |
},
|
|
|
121 |
|
|
|
122 |
// -- Protected Event Handlers ---------------------------------------------
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Handler for popstate events.
|
|
|
126 |
*
|
|
|
127 |
* @method _onPopState
|
|
|
128 |
* @param {Event} e
|
|
|
129 |
* @protected
|
|
|
130 |
*/
|
|
|
131 |
_onPopState: function (e) {
|
|
|
132 |
this._resolveChanges(SRC_POPSTATE, e._event.state || null);
|
|
|
133 |
}
|
|
|
134 |
}, {
|
|
|
135 |
// -- Public Static Properties ---------------------------------------------
|
|
|
136 |
NAME: 'historyhtml5',
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Constant used to identify state changes originating from
|
|
|
140 |
* <code>popstate</code> events.
|
|
|
141 |
*
|
|
|
142 |
* @property SRC_POPSTATE
|
|
|
143 |
* @type String
|
|
|
144 |
* @static
|
|
|
145 |
* @final
|
|
|
146 |
*/
|
|
|
147 |
SRC_POPSTATE: SRC_POPSTATE
|
|
|
148 |
});
|
|
|
149 |
|
|
|
150 |
if (!Y.Node.DOM_EVENTS.popstate) {
|
|
|
151 |
Y.Node.DOM_EVENTS.popstate = 1;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
Y.HistoryHTML5 = HistoryHTML5;
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* <p>
|
|
|
158 |
* If <code>true</code>, the <code>Y.History</code> alias will always point to
|
|
|
159 |
* <code>Y.HistoryHTML5</code> when the history-html5 module is loaded, even if
|
|
|
160 |
* the current browser doesn't support HTML5 history.
|
|
|
161 |
* </p>
|
|
|
162 |
*
|
|
|
163 |
* <p>
|
|
|
164 |
* If <code>false</code>, the <code>Y.History</code> alias will always point to
|
|
|
165 |
* <code>Y.HistoryHash</code> when the history-hash module is loaded, even if
|
|
|
166 |
* the current browser supports HTML5 history.
|
|
|
167 |
* </p>
|
|
|
168 |
*
|
|
|
169 |
* <p>
|
|
|
170 |
* If neither <code>true</code> nor <code>false</code>, the
|
|
|
171 |
* <code>Y.History</code> alias will point to the best available history adapter
|
|
|
172 |
* that the browser supports. This is the default behavior.
|
|
|
173 |
* </p>
|
|
|
174 |
*
|
|
|
175 |
* @property useHistoryHTML5
|
|
|
176 |
* @type boolean
|
|
|
177 |
* @for config
|
|
|
178 |
* @since 3.2.0
|
|
|
179 |
*/
|
|
|
180 |
|
|
|
181 |
// HistoryHTML5 will always win over HistoryHash unless useHistoryHTML5 is false
|
|
|
182 |
// or HTML5 history is not supported.
|
|
|
183 |
if (useHistoryHTML5 === true || (useHistoryHTML5 !== false &&
|
|
|
184 |
HistoryBase.html5)) {
|
|
|
185 |
Y.History = HistoryHTML5;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
}, '3.18.1', {"optional": ["json"], "requires": ["event-base", "history-base", "node-base"]});
|