|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Loading Tab Content</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Loading Tab Content</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p>This example shows how to create a plugin to load YQL data into a TabView for dynamic content.</p> |
|
|
30 |
</div> |
|
|
31 |
|
|
|
32 |
<div class="example yui3-skin-sam"> |
|
|
33 |
<style> |
|
|
34 |
.example h3 { |
|
|
35 |
color: #666; |
|
|
36 |
margin: 0.5em 0; |
|
|
37 |
} |
|
|
38 |
</style> |
|
|
39 |
<h3>Today's Browser News</h3> |
|
|
40 |
<div id="demo"></div> |
|
|
41 |
<script type="text/javascript"> |
|
|
42 |
YUI().use('tabview', 'yql', function(Y) { |
|
|
43 |
// YQL plugin for Y.Tab instances |
|
|
44 |
var TabYQL = function(config) { |
|
|
45 |
this.init(config); |
|
|
46 |
}; |
|
|
47 |
|
|
|
48 |
TabYQL.NS = 'yql'; // plugin namespace (e.g. "tab.yql.load(myQuery)"); |
|
|
49 |
|
|
|
50 |
TabYQL.prototype = { |
|
|
51 |
init: function(config) { |
|
|
52 |
if (config) { |
|
|
53 |
this.tab = config.host; |
|
|
54 |
this.query = config.query || this.query; |
|
|
55 |
this.errorMsg = config.errorMsg || this.errorMsg; |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
if (this.tab) { |
|
|
59 |
this.tab.after('selectedChange', Y.bind(this.afterSelectedChange, this)); |
|
|
60 |
} |
|
|
61 |
}, |
|
|
62 |
|
|
|
63 |
loaded: false, |
|
|
64 |
query: '', |
|
|
65 |
errorMsg: 'There was a problem loading the content', |
|
|
66 |
tab: null, |
|
|
67 |
|
|
|
68 |
afterLoad: function(response) { |
|
|
69 |
var results = (response.query) ? response.query.results.item : null, |
|
|
70 |
content = ''; |
|
|
71 |
|
|
|
72 |
if (results) { |
|
|
73 |
Y.each(results, function(fields) { |
|
|
74 |
content += '<li><a href="' + fields.link + '">' + |
|
|
75 |
fields.title + '</a></li>'; |
|
|
76 |
}); |
|
|
77 |
|
|
|
78 |
this.loaded = true; |
|
|
79 |
content = '<ul>' + content + '</ul>' |
|
|
80 |
} else { |
|
|
81 |
content = this.errorMsg; |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
this.tab.set('content', content); |
|
|
85 |
}, |
|
|
86 |
|
|
|
87 |
afterSelectedChange: function(e) { |
|
|
88 |
// only load if not already loaded |
|
|
89 |
if (!this.loaded) { |
|
|
90 |
this.load(this.query, this.afterLoad); |
|
|
91 |
} |
|
|
92 |
}, |
|
|
93 |
|
|
|
94 |
load: function(query, afterLoad) { |
|
|
95 |
Y.YQL(query, Y.bind(afterLoad, this)); |
|
|
96 |
} |
|
|
97 |
}; |
|
|
98 |
|
|
|
99 |
var tabview = new Y.TabView(), |
|
|
100 |
feeds = { |
|
|
101 |
Chrome: 'chrome+browser', |
|
|
102 |
Firefox: 'firefox+browser', |
|
|
103 |
Safari: 'safari+browser', |
|
|
104 |
Explorer: 'explorer+browser', |
|
|
105 |
Opera: 'opera+browser' |
|
|
106 |
}; |
|
|
107 |
|
|
|
108 |
Y.each(feeds, function(feed, label) { |
|
|
109 |
var tab = new Y.Tab({ |
|
|
110 |
label: label, |
|
|
111 |
content: 'loading...' |
|
|
112 |
}); |
|
|
113 |
|
|
|
114 |
tab.plug(TabYQL, { |
|
|
115 |
query: 'select title, link from rss where ' + |
|
|
116 |
'url="http://search.news.yahoo.com/rss?p=' + |
|
|
117 |
feed + '"' |
|
|
118 |
}); |
|
|
119 |
|
|
|
120 |
tabview.add(tab); |
|
|
121 |
}); |
|
|
122 |
|
|
|
123 |
tabview.render('#demo'); |
|
|
124 |
}); |
|
|
125 |
</script> |
|
|
126 |
|
|
|
127 |
</div> |
|
|
128 |
|
|
|
129 |
<p> |
|
|
130 |
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the |
|
|
131 |
page's <code><body></code> element or to a parent element of the widget in order to apply |
|
|
132 |
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>. |
|
|
133 |
</p> |
|
|
134 |
<pre class="code prettyprint"><body class="yui3-skin-sam"> <!-- You need this skin class --></pre> |
|
|
135 |
|
|
|
136 |
|
|
|
137 |
<h2>Creating the YQL Plugin</h2> |
|
|
138 |
<h3>Plugin Constructor</h3> |
|
|
139 |
<p>To create a plugin, we need to create a constructor with a static |
|
|
140 |
<code>NS</code> property. This is the namespace used by the plugin on each |
|
|
141 |
instance.</p> |
|
|
142 |
|
|
|
143 |
<pre class="code prettyprint">// YQL plugin for Y.Tab instances |
|
|
144 |
var TabYQL = function(config) { |
|
|
145 |
this.init(config); |
|
|
146 |
}; |
|
|
147 |
|
|
|
148 |
TabYQL.NS = 'yql'; // plugin namespace (e.g. "tab.yql.load(myQuery)");</pre> |
|
|
149 |
|
|
|
150 |
|
|
|
151 |
</h3>Plugin Prototype</h3> |
|
|
152 |
<p>Next we will add the YQL functionality to the prototype. The init method |
|
|
153 |
wires the YQL functionality up using the load method.</p> |
|
|
154 |
|
|
|
155 |
<pre class="code prettyprint">TabYQL.prototype = { |
|
|
156 |
init: function(config) { |
|
|
157 |
if (this.tab) { |
|
|
158 |
this.tab.after('selectedChange', Y.bind(this.afterSelectedChange, this)); |
|
|
159 |
} |
|
|
160 |
}, |
|
|
161 |
|
|
|
162 |
afterSelectedChange: function(e) { |
|
|
163 |
// only load if not already loaded |
|
|
164 |
if (!this.loaded) { |
|
|
165 |
this.load(this.query, this.afterLoad); |
|
|
166 |
} |
|
|
167 |
}, |
|
|
168 |
|
|
|
169 |
load: function(query, afterLoad) { |
|
|
170 |
Y.YQL(query, Y.bind(afterLoad, this)); |
|
|
171 |
}</pre> |
|
|
172 |
|
|
|
173 |
|
|
|
174 |
<h2>Creating the TabView</h2> |
|
|
175 |
<p>Next we will create a new instance of a TabView:</p> |
|
|
176 |
|
|
|
177 |
<pre class="code prettyprint">/* Create a new TabView instance, with content generated from script */ |
|
|
178 |
var tabview = new Y.TabView();</pre> |
|
|
179 |
|
|
|
180 |
|
|
|
181 |
<p>And then use the <code>add</code> method to add the <code>Tab</code> instances, |
|
|
182 |
to add a tab for each of the feeds, then render the tabview into the placeholder |
|
|
183 |
element.</p> |
|
|
184 |
|
|
|
185 |
|
|
|
186 |
<pre class="code prettyprint">var feeds = { |
|
|
187 |
Chrome: 'chrome+browser', |
|
|
188 |
Firefox: 'firefox+browser', |
|
|
189 |
Safari: 'safari+browser', |
|
|
190 |
Explorer: 'explorer+browser', |
|
|
191 |
Opera: 'opera+browser' |
|
|
192 |
}; |
|
|
193 |
|
|
|
194 |
Y.each(feeds, function(feed, label) { |
|
|
195 |
var tab = new Y.Tab({ |
|
|
196 |
label: label, |
|
|
197 |
content: 'loading...', |
|
|
198 |
}); |
|
|
199 |
|
|
|
200 |
tab.plug(TabYQL, { |
|
|
201 |
query: 'select title, link from rss where ' + |
|
|
202 |
'url="http://search.news.yahoo.com/rss?p=' + |
|
|
203 |
feed + '"' |
|
|
204 |
}); |
|
|
205 |
|
|
|
206 |
tabview.add(tab); |
|
|
207 |
}); |
|
|
208 |
|
|
|
209 |
tabview.render('#demo');</pre> |
|
|
210 |
|
|
|
211 |
</div> |
|
|
212 |
</div> |
|
|
213 |
</div> |
|
|
214 |
|
|
|
215 |
<div class="yui3-u-1-4"> |
|
|
216 |
<div class="sidebar"> |
|
|
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
<div class="sidebox"> |
|
|
221 |
<div class="hd"> |
|
|
222 |
<h2 class="no-toc">Examples</h2> |
|
|
223 |
</div> |
|
|
224 |
|
|
|
225 |
<div class="bd"> |
|
|
226 |
<ul class="examples"> |
|
|
227 |
|
|
|
228 |
|
|
|
229 |
<li data-description="This example shows how to create a TabView wigdet from existing HTML."> |
|
|
230 |
<a href="tabview-basic.html">TabView from Existing Markup</a> |
|
|
231 |
</li> |
|
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
<li data-description="This example shows how to create a TabView wigdet from JavaScript."> |
|
|
236 |
<a href="tabview-fromjs.html">Dynamic TabView from JavaScript</a> |
|
|
237 |
</li> |
|
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
<li data-description="This example shows how to add and remove Tabs."> |
|
|
242 |
<a href="tabview-add-remove.html">Adding and Removing Tabs</a> |
|
|
243 |
</li> |
|
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
|
247 |
<li data-description="This example shows how to load tab content remotely using a YQL plugin."> |
|
|
248 |
<a href="tabview-yql.html">Loading Tab Content</a> |
|
|
249 |
</li> |
|
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
</ul> |
|
|
255 |
</div> |
|
|
256 |
</div> |
|
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
|
260 |
<div class="sidebox"> |
|
|
261 |
<div class="hd"> |
|
|
262 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
263 |
</div> |
|
|
264 |
|
|
|
265 |
<div class="bd"> |
|
|
266 |
<ul class="examples"> |
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
|
271 |
|
|
|
272 |
|
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
<li data-description="Demonstrates how to add browser history support to a TabView widget using the History Utility."> |
|
|
278 |
<a href="../history/history-tabview.html">History + TabView</a> |
|
|
279 |
</li> |
|
|
280 |
|
|
|
281 |
|
|
|
282 |
</ul> |
|
|
283 |
</div> |
|
|
284 |
</div> |
|
|
285 |
|
|
|
286 |
</div> |
|
|
287 |
</div> |
|
|
288 |
</div> |
|
|
289 |
</div> |
|
|
290 |
|
|
|
291 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
292 |
<script>prettyPrint();</script> |
|
|
293 |
|
|
|
294 |
<script> |
|
|
295 |
YUI.Env.Tests = { |
|
|
296 |
examples: [], |
|
|
297 |
project: '../assets', |
|
|
298 |
assets: '../assets/tabview', |
|
|
299 |
name: 'tabview-yql', |
|
|
300 |
title: 'Loading Tab Content', |
|
|
301 |
newWindow: '', |
|
|
302 |
auto: false |
|
|
303 |
}; |
|
|
304 |
YUI.Env.Tests.examples.push('tabview-basic'); |
|
|
305 |
YUI.Env.Tests.examples.push('tabview-fromjs'); |
|
|
306 |
YUI.Env.Tests.examples.push('tabview-add-remove'); |
|
|
307 |
YUI.Env.Tests.examples.push('tabview-yql'); |
|
|
308 |
YUI.Env.Tests.examples.push('history-tabview'); |
|
|
309 |
|
|
|
310 |
</script> |
|
|
311 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
312 |
|
|
|
313 |
|
|
|
314 |
|
|
|
315 |
</body> |
|
|
316 |
</html> |