5
<title>Example: History + TabView</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: History + TabView</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
22
This example demonstrates how to add browser history support to a TabView widget
23
using the History Utility.
27
Select a new tab in the TabView below, then use your browser's back button
28
to return to the previously selected tab. Next, click on one of the images to
29
visit the Flickr photo page for that image, then use your browser's back button
30
to return to the current page with the same tab selected.
36
#demo { width: 300px; }
39
border: 1px solid #000;
43
#demo .yui3-tab-selected .yui3-tab-label { color: #fff; }
46
<div id="demo" class="yui3-skin-sam">
48
<li><a href="#asparagus">Asparagus</a></li>
49
<li><a href="#bird">Bird</a></li>
50
<li><a href="#coffee">Coffee</a></li>
54
<a href="http://www.flickr.com/photos/allenr/4686935131/">
55
<img src="http://farm5.static.flickr.com/4005/4686935131_253e921bf7_m.jpg" alt="Asparagus">
59
<a href="http://www.flickr.com/photos/allenr/66307916/">
60
<img src="http://farm1.static.flickr.com/26/66307916_811efccdfc_m.jpg" alt="Bird">
64
<a href="http://www.flickr.com/photos/allenr/4638474362/">
65
<img src="http://farm4.static.flickr.com/3336/4638474362_093edb7565_m.jpg" alt="Coffee">
72
YUI().use('history', 'tabview', function (Y) {
73
var history = new Y.HistoryHash(),
74
tabview = new Y.TabView({srcNode: '#demo'});
76
// Render the TabView widget to turn the static markup into an
77
// interactive TabView.
80
// Set the selected tab to the bookmarked history state, or to
81
// the first tab if there's no bookmarked state.
82
tabview.selectChild(history.get('tab') || 0);
84
// Store a new history state when the user selects a tab.
85
tabview.after('selectionChange', function (e) {
86
// If the new tab index is greater than 0, set the "tab"
87
// state value to the index. Otherwise, remove the "tab"
88
// state value by setting it to null (this reverts to the
89
// default state of selecting the first tab).
90
history.addValue('tab', e.newVal.get('index') || null);
93
// Listen for history changes from back/forward navigation or
94
// URL changes, and update the tab selection when necessary.
95
Y.on('history:change', function (e) {
96
// Ignore changes we make ourselves, since we don't need
97
// to update the selection state for those. We're only
98
// interested in outside changes, such as the ones generated
99
// when the user clicks the browser's back or forward buttons.
100
if (e.src === Y.HistoryHash.SRC_HASH) {
103
// The new state contains a different tab selection, so
104
// change the selected tab.
105
tabview.selectChild(e.changed.tab.newVal);
106
} else if (e.removed.tab) {
107
// The tab selection was removed in the new state, so
108
// select the first tab by default.
109
tabview.selectChild(0);
122
First, create the markup for a simple TabView widget with three tabs.
125
<pre class="code prettyprint"><div id="demo" class="yui3-skin-sam">
127
<li><a href="#asparagus">Asparagus</a></li>
128
<li><a href="#bird">Bird</a></li>
129
<li><a href="#coffee">Coffee</a></li>
132
<div id="asparagus">
133
<a href="http://www.flickr.com/photos/allenr/4686935131/">
134
<img src="http://farm5.static.flickr.com/4005/4686935131_253e921bf7_m.jpg" alt="Asparagus">
137
<div id="bird">
138
<a href="http://www.flickr.com/photos/allenr/66307916/">
139
<img src="http://farm1.static.flickr.com/26/66307916_811efccdfc_m.jpg" alt="Bird">
142
<div id="coffee">
143
<a href="http://www.flickr.com/photos/allenr/4638474362/">
144
<img src="http://farm4.static.flickr.com/3336/4638474362_093edb7565_m.jpg" alt="Coffee">
148
</div></pre>
153
<h3>Load history and tabview</h3>
156
Load the <code>history</code> and <code>tabview</code> modules.
159
<pre class="code prettyprint">YUI().use('history', 'tabview', function (Y) {
160
// ...implementation code...
164
<h3>Initialize History and TabView</h3>
167
Create an instance of the TabView widget and a history adapter, and restore the
168
bookmarked tab selection, if any.
172
This example uses the <code>Y.HistoryHash</code> adapter, which stores history state in the
173
hash fragment of the URL. Another option would be to use the <code>Y.HistoryHTML5</code>
174
adapter, but this would require additional logic in order to create bookmarkable
179
Add this code inside the <code>YUI().use()</code> callback from the previous step.
182
<pre class="code prettyprint">var history = new Y.HistoryHash(),
183
tabview = new Y.TabView({srcNode: '#demo'});
185
// Render the TabView widget to turn the static markup into an
186
// interactive TabView.
189
// Set the selected tab to the bookmarked history state, or to
190
// the first tab if there's no bookmarked state.
191
tabview.selectChild(history.get('tab') || 0);</pre>
194
<h3>Add a history entry when the selected tab changes</h3>
197
When the user selects a new tab, create a new browser history entry with a
198
state value named <code>tab</code> that contains the index of the selected tab.
199
If the first tab is selected, then set the <code>tab</code> state value to
200
<code>null</code> to remove it from the state, because the first tab is the
204
<pre class="code prettyprint">// Store a new history state when the user selects a tab.
205
tabview.after('selectionChange', function (e) {
206
// If the new tab index is greater than 0, set the "tab"
207
// state value to the index. Otherwise, remove the "tab"
208
// state value by setting it to null (this reverts to the
209
// default state of selecting the first tab).
210
history.addValue('tab', e.newVal.get('index') || null);
214
<h3>Listen for history events to capture back/forward navigation</h3>
217
Finally, listen for history change events, which indicate that the user clicked
218
the back/forward button or manually changed the URL.
222
When a <code>history:change</code> event occurs, it could come from the call to
223
the <code>addValue()</code> method above or it could come from a change to the
224
URL hash. We only care about changes that come from the URL hash, since that
225
indicates a navigation event.
228
<pre class="code prettyprint">// Listen for history changes from back/forward navigation or
229
// URL changes, and update the tab selection when necessary.
230
Y.on('history:change', function (e) {
231
// Ignore changes we make ourselves, since we don't need
232
// to update the selection state for those. We're only
233
// interested in outside changes, such as the ones generated
234
// when the user clicks the browser's back or forward buttons.
235
if (e.src === Y.HistoryHash.SRC_HASH) {
238
// The new state contains a different tab selection, so
239
// change the selected tab.
240
tabview.selectChild(e.changed.tab.newVal);
241
} else if (e.removed.tab) {
242
// The tab selection was removed in the new state, so
243
// select the first tab by default.
244
tabview.selectChild(0);
251
<h3>Complete example source code</h3>
254
Here's the complete source code for the example:
257
<pre class="code prettyprint"><div id="demo" class="yui3-skin-sam">
259
<li><a href="#asparagus">Asparagus</a></li>
260
<li><a href="#bird">Bird</a></li>
261
<li><a href="#coffee">Coffee</a></li>
264
<div id="asparagus">
265
<a href="http://www.flickr.com/photos/allenr/4686935131/">
266
<img src="http://farm5.static.flickr.com/4005/4686935131_253e921bf7_m.jpg" alt="Asparagus">
269
<div id="bird">
270
<a href="http://www.flickr.com/photos/allenr/66307916/">
271
<img src="http://farm1.static.flickr.com/26/66307916_811efccdfc_m.jpg" alt="Bird">
274
<div id="coffee">
275
<a href="http://www.flickr.com/photos/allenr/4638474362/">
276
<img src="http://farm4.static.flickr.com/3336/4638474362_093edb7565_m.jpg" alt="Coffee">
283
YUI().use('history', 'tabview', function (Y) {
284
var history = new Y.HistoryHash(),
285
tabview = new Y.TabView({srcNode: '#demo'});
287
// Render the TabView widget to turn the static markup into an
288
// interactive TabView.
291
// Set the selected tab to the bookmarked history state, or to
292
// the first tab if there's no bookmarked state.
293
tabview.selectChild(history.get('tab') || 0);
295
// Store a new history state when the user selects a tab.
296
tabview.after('selectionChange', function (e) {
297
// If the new tab index is greater than 0, set the "tab"
298
// state value to the index. Otherwise, remove the "tab"
299
// state value by setting it to null (this reverts to the
300
// default state of selecting the first tab).
301
history.addValue('tab', e.newVal.get('index') || null);
304
// Listen for history changes from back/forward navigation or
305
// URL changes, and update the tab selection when necessary.
306
Y.on('history:change', function (e) {
307
// Ignore changes we make ourselves, since we don't need
308
// to update the selection state for those. We're only
309
// interested in outside changes, such as the ones generated
310
// when the user clicks the browser's back or forward buttons.
311
if (e.src === Y.HistoryHash.SRC_HASH) {
314
// The new state contains a different tab selection, so
315
// change the selected tab.
316
tabview.selectChild(e.changed.tab.newVal);
317
} else if (e.removed.tab) {
318
// The tab selection was removed in the new state, so
319
// select the first tab by default.
320
tabview.selectChild(0);
326
</script></pre>
331
<div id="sidebar" class="yui3-u">
335
<div class="sidebox">
337
<h2 class="no-toc">Examples</h2>
341
<ul class="examples">
344
<li data-description="Demonstrates how to add browser history support to a TabView widget using the History Utility.">
345
<a href="history-tabview.html">History + TabView</a>
359
<script src="../assets/vendor/prettify/prettify-min.js"></script>
360
<script>prettyPrint();</script>