|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Adding and Removing Tabs</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: Adding and Removing Tabs</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style scoped> |
|
|
29 |
.yui3-tabview { |
|
|
30 |
margin-bottom: 1em; |
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
.yui3-skin-sam .yui3-tab { |
|
|
34 |
position: relative; |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
.yui3-skin-sam .yui3-tabview-removeable .yui3-tab-label, |
|
|
38 |
.yui3-skin-sam .yui3-tabview-removeable .yui3-tab-selected .yui3-tab-label { |
|
|
39 |
padding-right: 1.3em; /* make room for close link */ |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
.yui3-skin-sam .yui3-tabview-removeable .yui3-tab-add { |
|
|
43 |
padding-right: 0.75em; /* no close link for add tab link */ |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
.yui3-skin-sam .yui3-tab-add { |
|
|
47 |
color: #999; |
|
|
48 |
font: bold 120%/1 verdana; |
|
|
49 |
margin-left: 0.25em; |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
.yui3-tab-remove { |
|
|
53 |
color: #999; |
|
|
54 |
font:bold 80% verdana; |
|
|
55 |
position: absolute; |
|
|
56 |
right: 0.4em; |
|
|
57 |
top: 0.4em; |
|
|
58 |
} |
|
|
59 |
|
|
|
60 |
.yui3-tab-remove:hover { |
|
|
61 |
color: #666; |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
.yui3-skin-sam .yui3-tab-selected .yui3-tab-remove { |
|
|
65 |
top: 0.52em; |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
.yui3-skin-sam .yui3-tab-selected .yui3-tab-remove:hover { |
|
|
69 |
color: #fff; |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
#main #example-canvas .yui3-tabview .yui3-tab-selected a { |
|
|
73 |
color:white; |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
</style> |
|
|
77 |
<div class="intro"> |
|
|
78 |
<p>This example shows how to give <code>TabView</code> buttons for adding and removing tabs.</p> |
|
|
79 |
</div> |
|
|
80 |
|
|
|
81 |
<div class="example yui3-skin-sam"> |
|
|
82 |
<div id="demo"> |
|
|
83 |
<ul> |
|
|
84 |
<li><a href="#foo">foo</a></li> |
|
|
85 |
<li><a href="#bar">bar</a></li> |
|
|
86 |
<li><a href="#baz">baz</a></li> |
|
|
87 |
</ul> |
|
|
88 |
<div> |
|
|
89 |
<div id="foo"> |
|
|
90 |
<p>foo content</p> |
|
|
91 |
</div> |
|
|
92 |
<div id="bar"> |
|
|
93 |
<p>bar content</p> |
|
|
94 |
</div> |
|
|
95 |
<div id="baz"> |
|
|
96 |
<p>baz content</p> |
|
|
97 |
</div> |
|
|
98 |
</div> |
|
|
99 |
</div> |
|
|
100 |
|
|
|
101 |
<div id="demo2"> |
|
|
102 |
</div> |
|
|
103 |
|
|
|
104 |
<div id="demo3"> |
|
|
105 |
</div> |
|
|
106 |
|
|
|
107 |
<script type="text/javascript"> |
|
|
108 |
YUI().use('tabview', 'escape', 'plugin', function(Y) { |
|
|
109 |
var Addable = function(config) { |
|
|
110 |
Addable.superclass.constructor.apply(this, arguments); |
|
|
111 |
}; |
|
|
112 |
|
|
|
113 |
Addable.NAME = 'addableTabs'; |
|
|
114 |
Addable.NS = 'addable'; |
|
|
115 |
|
|
|
116 |
Y.extend(Addable, Y.Plugin.Base, { |
|
|
117 |
ADD_TEMPLATE: '<li class="yui3-tab" title="add a tab">' + |
|
|
118 |
'<a class="yui3-tab-label yui3-tab-add">+</a></li>', |
|
|
119 |
|
|
|
120 |
initializer: function(config) { |
|
|
121 |
var tabview = this.get('host'); |
|
|
122 |
tabview.after('render', this.afterRender, this); |
|
|
123 |
tabview.get('contentBox') |
|
|
124 |
.delegate('click', this.onAddClick, '.yui3-tab-add', this); |
|
|
125 |
}, |
|
|
126 |
|
|
|
127 |
getTabInput: function() { |
|
|
128 |
var tabview = this.get('host'); |
|
|
129 |
return { |
|
|
130 |
label: Y.Escape.html(window.prompt('label:', 'new tab')), |
|
|
131 |
content: '<p>' + Y.Escape.html(window.prompt('content:', 'new content')) + '</p>', |
|
|
132 |
index: Number(window.prompt('index:', tabview.size())) |
|
|
133 |
} |
|
|
134 |
}, |
|
|
135 |
|
|
|
136 |
afterRender: function(e) { |
|
|
137 |
var tabview = this.get('host'); |
|
|
138 |
tabview.get('contentBox').one('> ul').append(this.ADD_TEMPLATE); |
|
|
139 |
}, |
|
|
140 |
|
|
|
141 |
onAddClick: function(e) { |
|
|
142 |
e.stopPropagation(); |
|
|
143 |
var tabview = this.get('host'), |
|
|
144 |
input = this.getTabInput(); |
|
|
145 |
tabview.add(input, input.index); |
|
|
146 |
} |
|
|
147 |
}); |
|
|
148 |
|
|
|
149 |
var Removeable = function(config) { |
|
|
150 |
Removeable.superclass.constructor.apply(this, arguments); |
|
|
151 |
}; |
|
|
152 |
|
|
|
153 |
Removeable.NAME = 'removeableTabs'; |
|
|
154 |
Removeable.NS = 'removeable'; |
|
|
155 |
|
|
|
156 |
Y.extend(Removeable, Y.Plugin.Base, { |
|
|
157 |
REMOVE_TEMPLATE: '<a class="yui3-tab-remove" title="remove tab">x</a>', |
|
|
158 |
|
|
|
159 |
initializer: function(config) { |
|
|
160 |
var tabview = this.get('host'), |
|
|
161 |
cb = tabview.get('contentBox'); |
|
|
162 |
|
|
|
163 |
cb.addClass('yui3-tabview-removeable'); |
|
|
164 |
cb.delegate('click', this.onRemoveClick, '.yui3-tab-remove', this); |
|
|
165 |
|
|
|
166 |
// Tab events bubble to TabView |
|
|
167 |
tabview.after('tab:render', this.afterTabRender, this); |
|
|
168 |
}, |
|
|
169 |
|
|
|
170 |
afterTabRender: function(e) { |
|
|
171 |
// boundingBox is the Tab's LI |
|
|
172 |
e.target.get('boundingBox').append(this.REMOVE_TEMPLATE); |
|
|
173 |
}, |
|
|
174 |
|
|
|
175 |
onRemoveClick: function(e) { |
|
|
176 |
e.stopPropagation(); |
|
|
177 |
var tab = Y.Widget.getByNode(e.target); |
|
|
178 |
tab.remove(); |
|
|
179 |
} |
|
|
180 |
}); |
|
|
181 |
|
|
|
182 |
var tabview = new Y.TabView({ |
|
|
183 |
srcNode: '#demo', |
|
|
184 |
plugins: [Addable] |
|
|
185 |
}); |
|
|
186 |
|
|
|
187 |
var tabview2 = new Y.TabView({ |
|
|
188 |
children: [{ |
|
|
189 |
label: 'foo', |
|
|
190 |
content: '<p>foo content</p>' |
|
|
191 |
}, { |
|
|
192 |
label: 'bar', |
|
|
193 |
content: '<p>bar content</p>' |
|
|
194 |
}, { |
|
|
195 |
label: 'baz', |
|
|
196 |
content: '<p>baz content</p>' |
|
|
197 |
}], |
|
|
198 |
plugins: [Removeable] |
|
|
199 |
}); |
|
|
200 |
|
|
|
201 |
var tabview3 = new Y.TabView({ |
|
|
202 |
children: [{ |
|
|
203 |
label: 'foo', |
|
|
204 |
content: '<p>foo content</p>' |
|
|
205 |
}, { |
|
|
206 |
label: 'bar', |
|
|
207 |
content: '<p>bar content</p>' |
|
|
208 |
}, { |
|
|
209 |
label: 'baz', |
|
|
210 |
content: '<p>bar content</p>' |
|
|
211 |
}], |
|
|
212 |
plugins: [Addable, Removeable] |
|
|
213 |
}); |
|
|
214 |
|
|
|
215 |
tabview.render(); |
|
|
216 |
tabview2.render('#demo2'); |
|
|
217 |
tabview3.render('#demo3'); |
|
|
218 |
}); |
|
|
219 |
</script> |
|
|
220 |
|
|
|
221 |
</div> |
|
|
222 |
|
|
|
223 |
<p> |
|
|
224 |
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the |
|
|
225 |
page's <code><body></code> element or to a parent element of the widget in order to apply |
|
|
226 |
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>. |
|
|
227 |
</p> |
|
|
228 |
<pre class="code prettyprint"><body class="yui3-skin-sam"> <!-- You need this skin class --></pre> |
|
|
229 |
|
|
|
230 |
|
|
|
231 |
<h2>Plugin Template</h2> |
|
|
232 |
|
|
|
233 |
<p>In order to make these addons reusable, we can build them as plugins. This |
|
|
234 |
allows the option for multiple tabviews that mix and match functionality. |
|
|
235 |
To get started, we will first fill in a basic <code>Plugin</code> template. |
|
|
236 |
The <code>NAME</code> property is required to prefix events, classNames, et cetera. |
|
|
237 |
The <code>NS</code> is the namespace where the plugin will live on the |
|
|
238 |
<code>host</code>. This is where its API can be accessed (e.g. "node.addable.destroy()"). |
|
|
239 |
Adding the <code>this._host</code> alias provides a convenient way to get back to the TabView |
|
|
240 |
instance. Calling the superclass constructor kicks off the <code>Base</code> lifecycle, |
|
|
241 |
which will call the <code>initializer</code>. |
|
|
242 |
|
|
|
243 |
</p> |
|
|
244 |
|
|
|
245 |
<pre class="code prettyprint">var Addable = function(config) { |
|
|
246 |
this._host = config.host; |
|
|
247 |
Addable.superclass.constructor.apply(this, arguments); |
|
|
248 |
}; |
|
|
249 |
|
|
|
250 |
Addable.NAME = 'addableTabs'; |
|
|
251 |
Addable.NS = 'addable'; |
|
|
252 |
|
|
|
253 |
Y.extend(Addable, Y.Plugin.Base, { |
|
|
254 |
initializer: function(config) { |
|
|
255 |
} |
|
|
256 |
});</pre> |
|
|
257 |
|
|
|
258 |
|
|
|
259 |
<h2>Addable Tab Plugin</h2> |
|
|
260 |
|
|
|
261 |
<p>To simplify adding new tabs, we are going to add a button that |
|
|
262 |
users can click and that will prompt them for some details regarding the new tab. |
|
|
263 |
The main task we are trying to accomplish is to add some HTML to the |
|
|
264 |
<code>TabView</code>, listen for clicks on the button, prompt the user for input, |
|
|
265 |
and update the tabs accordingly.</p> |
|
|
266 |
|
|
|
267 |
<h3>HTML Template</h3> |
|
|
268 |
<p>The first thing we need is a template for the markup to be generated. Adding |
|
|
269 |
this to the prototype allows separate customization for each <code>TabView</code> |
|
|
270 |
instance. For this example, we want it to look and feel like another <code>Tab</code> |
|
|
271 |
without actually being one.</p> |
|
|
272 |
|
|
|
273 |
<pre class="code prettyprint">Y.extend(Addable, Y.Plugin.Base, { |
|
|
274 |
ADD_TEMPLATE: '<li class="yui3-tab" title="add a tab">' + |
|
|
275 |
'<a class="yui3-tab-label yui3-tab-add">+</a></li>', |
|
|
276 |
|
|
|
277 |
initializer: function(config) { |
|
|
278 |
} |
|
|
279 |
});</pre> |
|
|
280 |
|
|
|
281 |
|
|
|
282 |
<h3>Adding the HTML</h3> |
|
|
283 |
<p>Now that we have a markup template, we will need to add it to the <code>TabView</code> |
|
|
284 |
somehow. The <code>render</code> phase is the appropriate moment to do so. Listening |
|
|
285 |
for the <code>render</code> event will give us access to that moment. Listening |
|
|
286 |
for <code>after('render')</code> ensure that the rendering has actually happened. Then |
|
|
287 |
we just need to find the tab list and, using the template, add the new item. |
|
|
288 |
The <code>contentBox</code> provides a <code>Node</code> that can be used to manage |
|
|
289 |
the <code>TabView</code> HTML.</p> |
|
|
290 |
|
|
|
291 |
<pre class="code prettyprint">Y.extend(Addable, Y.Plugin.Base, { |
|
|
292 |
ADD_TEMPLATE: '<li class="yui3-tab" title="add a tab">' + |
|
|
293 |
'<a class="yui3-tab-label yui3-tab-add">+</a></li>', |
|
|
294 |
|
|
|
295 |
initializer: function(config) { |
|
|
296 |
var tabview = this.get('host'); |
|
|
297 |
tabview.after('render', this.afterRender, this); |
|
|
298 |
}, |
|
|
299 |
|
|
|
300 |
afterRender: function(e) { |
|
|
301 |
var tabview = this.get('host'); |
|
|
302 |
tabview.get('contentBox').one('> ul').append(this.ADD_TEMPLATE); |
|
|
303 |
} |
|
|
304 |
});</pre> |
|
|
305 |
|
|
|
306 |
|
|
|
307 |
<h3>Handling the Click</h3> |
|
|
308 |
<p>All that remains is to listen for clicks on the add button and prompt |
|
|
309 |
the user for the relevant <code>Tab</code> data. Again we can leverage |
|
|
310 |
the <code>Node</code> API, this time to delegate clicks on the add button. |
|
|
311 |
Stopping event propagation in our handler ensures that the event does |
|
|
312 |
not bubble any further, preventing the <code>TabView</code> from trying |
|
|
313 |
to handle it. To keep the example simple, a basic <code>prompt</code> is |
|
|
314 |
used to get the user input. This could be refined to use an |
|
|
315 |
<code>Overlay</code> or other custom control. The data is then handed off |
|
|
316 |
to <code>TabView</code>'s <code>add</code> method.</p> |
|
|
317 |
|
|
|
318 |
<pre class="code prettyprint">Y.extend(Addable, Y.Plugin.Base, { |
|
|
319 |
ADD_TEMPLATE: '<li class="yui3-tab" title="add a tab">' + |
|
|
320 |
'<a class="yui3-tab-label yui3-tab-add">+</a></li>', |
|
|
321 |
|
|
|
322 |
initializer: function(config) { |
|
|
323 |
var tabview = this.get('host'); |
|
|
324 |
tabview.after('render', this.afterRender, this); |
|
|
325 |
|
|
|
326 |
tabview.get('contentBox') |
|
|
327 |
.delegate('click', this.onAddClick, '.yui3-tab-add', this); |
|
|
328 |
}, |
|
|
329 |
|
|
|
330 |
afterRender: function(e) { |
|
|
331 |
this.get('host').get('contentBox').one('> ul').append(this.ADD_TEMPLATE); |
|
|
332 |
}, |
|
|
333 |
|
|
|
334 |
getTabInput: function() { |
|
|
335 |
return { |
|
|
336 |
label: window.prompt('label:', 'new tab'), |
|
|
337 |
content: window.prompt('content:', '<p>new content</p>'), |
|
|
338 |
index: Number(window.prompt('index:', this._host.size())) |
|
|
339 |
} |
|
|
340 |
}, |
|
|
341 |
|
|
|
342 |
onAddClick: function(e) { |
|
|
343 |
e.stopPropagation(); |
|
|
344 |
var tabview = this.get('host'); |
|
|
345 |
input = this.getTabInput(); |
|
|
346 |
tabview.add(input, input.index); |
|
|
347 |
} |
|
|
348 |
});</pre> |
|
|
349 |
|
|
|
350 |
|
|
|
351 |
<h3>Using the Plugin</h3> |
|
|
352 |
<p>Now we can go ahead and plug in our functionality. This can be during |
|
|
353 |
construction with the <code>plugins</code> attribute, or subsequently |
|
|
354 |
via the <code>plug</code> method.</p> |
|
|
355 |
|
|
|
356 |
<pre class="code prettyprint">var tabview = new Y.TabView({ |
|
|
357 |
children: [{ |
|
|
358 |
label: 'foo', |
|
|
359 |
content: '<p>foo content</p>' |
|
|
360 |
}, { |
|
|
361 |
label: 'bar', |
|
|
362 |
content: '<p>bar content</p>' |
|
|
363 |
}, { |
|
|
364 |
label: 'baz', |
|
|
365 |
content: '<p>baz content</p>' |
|
|
366 |
}], |
|
|
367 |
plugins: [Addable] |
|
|
368 |
}); |
|
|
369 |
|
|
|
370 |
// or |
|
|
371 |
// tabview.plug(Addable);</pre> |
|
|
372 |
|
|
|
373 |
|
|
|
374 |
<h2>Removeable Tabs Plugin</h2> |
|
|
375 |
<p>The process for creating a removeable plugin for <code>TabView</code> |
|
|
376 |
is very similar. The full source is provided below.</p> |
|
|
377 |
|
|
|
378 |
<pre class="code prettyprint">var Removeable = function(config) { |
|
|
379 |
Removeable.superclass.constructor.apply(this, arguments); |
|
|
380 |
}; |
|
|
381 |
|
|
|
382 |
Removeable.NAME = 'removeableTabs'; |
|
|
383 |
Removeable.NS = 'removeable'; |
|
|
384 |
|
|
|
385 |
Y.extend(Removeable, Y.Plugin.Base, { |
|
|
386 |
REMOVE_TEMPLATE: '<a class="yui3-tab-remove" title="remove tab">x</a>', |
|
|
387 |
|
|
|
388 |
initializer: function(config) { |
|
|
389 |
var tabview = this.get('host'), |
|
|
390 |
cb = tabview.get('contentBox'); |
|
|
391 |
|
|
|
392 |
cb.addClass('yui3-tabview-removeable'); |
|
|
393 |
cb.delegate('click', this.onRemoveClick, '.yui3-tab-remove', this); |
|
|
394 |
|
|
|
395 |
// Tab events bubble to TabView |
|
|
396 |
tabview.after('tab:render', this.afterTabRender, this); |
|
|
397 |
}, |
|
|
398 |
|
|
|
399 |
afterTabRender: function(e) { |
|
|
400 |
// boundingBox is the Tab's LI |
|
|
401 |
e.target.get('boundingBox').append(this.REMOVE_TEMPLATE); |
|
|
402 |
}, |
|
|
403 |
|
|
|
404 |
onRemoveClick: function(e) { |
|
|
405 |
e.stopPropagation(); |
|
|
406 |
var tab = Y.Widget.getByNode(e.target); |
|
|
407 |
tab.remove(); |
|
|
408 |
} |
|
|
409 |
});</pre> |
|
|
410 |
|
|
|
411 |
<h2>Complete Example Source</h2> |
|
|
412 |
<p> |
|
|
413 |
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the |
|
|
414 |
page's <code><body></code> element or to a parent element of the widget in order to apply |
|
|
415 |
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>. |
|
|
416 |
</p> |
|
|
417 |
<pre class="code prettyprint"><body class="yui3-skin-sam"> <!-- You need this skin class --></pre> |
|
|
418 |
|
|
|
419 |
|
|
|
420 |
<pre class="code prettyprint"><div id="demo"> |
|
|
421 |
<ul> |
|
|
422 |
<li><a href="#foo">foo</a></li> |
|
|
423 |
<li><a href="#bar">bar</a></li> |
|
|
424 |
<li><a href="#baz">baz</a></li> |
|
|
425 |
</ul> |
|
|
426 |
<div> |
|
|
427 |
<div id="foo"> |
|
|
428 |
<p>foo content</p> |
|
|
429 |
</div> |
|
|
430 |
<div id="bar"> |
|
|
431 |
<p>bar content</p> |
|
|
432 |
</div> |
|
|
433 |
<div id="baz"> |
|
|
434 |
<p>baz content</p> |
|
|
435 |
</div> |
|
|
436 |
</div> |
|
|
437 |
</div> |
|
|
438 |
|
|
|
439 |
<div id="demo2"> |
|
|
440 |
</div> |
|
|
441 |
|
|
|
442 |
<div id="demo3"> |
|
|
443 |
</div> |
|
|
444 |
|
|
|
445 |
<script type="text/javascript"> |
|
|
446 |
YUI().use('tabview', 'escape', 'plugin', function(Y) { |
|
|
447 |
var Addable = function(config) { |
|
|
448 |
Addable.superclass.constructor.apply(this, arguments); |
|
|
449 |
}; |
|
|
450 |
|
|
|
451 |
Addable.NAME = 'addableTabs'; |
|
|
452 |
Addable.NS = 'addable'; |
|
|
453 |
|
|
|
454 |
Y.extend(Addable, Y.Plugin.Base, { |
|
|
455 |
ADD_TEMPLATE: '<li class="yui3-tab" title="add a tab">' + |
|
|
456 |
'<a class="yui3-tab-label yui3-tab-add">+</a></li>', |
|
|
457 |
|
|
|
458 |
initializer: function(config) { |
|
|
459 |
var tabview = this.get('host'); |
|
|
460 |
tabview.after('render', this.afterRender, this); |
|
|
461 |
tabview.get('contentBox') |
|
|
462 |
.delegate('click', this.onAddClick, '.yui3-tab-add', this); |
|
|
463 |
}, |
|
|
464 |
|
|
|
465 |
getTabInput: function() { |
|
|
466 |
var tabview = this.get('host'); |
|
|
467 |
return { |
|
|
468 |
label: Y.Escape.html(window.prompt('label:', 'new tab')), |
|
|
469 |
content: '<p>' + Y.Escape.html(window.prompt('content:', 'new content')) + '</p>', |
|
|
470 |
index: Number(window.prompt('index:', tabview.size())) |
|
|
471 |
} |
|
|
472 |
}, |
|
|
473 |
|
|
|
474 |
afterRender: function(e) { |
|
|
475 |
var tabview = this.get('host'); |
|
|
476 |
tabview.get('contentBox').one('> ul').append(this.ADD_TEMPLATE); |
|
|
477 |
}, |
|
|
478 |
|
|
|
479 |
onAddClick: function(e) { |
|
|
480 |
e.stopPropagation(); |
|
|
481 |
var tabview = this.get('host'), |
|
|
482 |
input = this.getTabInput(); |
|
|
483 |
tabview.add(input, input.index); |
|
|
484 |
} |
|
|
485 |
}); |
|
|
486 |
|
|
|
487 |
var Removeable = function(config) { |
|
|
488 |
Removeable.superclass.constructor.apply(this, arguments); |
|
|
489 |
}; |
|
|
490 |
|
|
|
491 |
Removeable.NAME = 'removeableTabs'; |
|
|
492 |
Removeable.NS = 'removeable'; |
|
|
493 |
|
|
|
494 |
Y.extend(Removeable, Y.Plugin.Base, { |
|
|
495 |
REMOVE_TEMPLATE: '<a class="yui3-tab-remove" title="remove tab">x</a>', |
|
|
496 |
|
|
|
497 |
initializer: function(config) { |
|
|
498 |
var tabview = this.get('host'), |
|
|
499 |
cb = tabview.get('contentBox'); |
|
|
500 |
|
|
|
501 |
cb.addClass('yui3-tabview-removeable'); |
|
|
502 |
cb.delegate('click', this.onRemoveClick, '.yui3-tab-remove', this); |
|
|
503 |
|
|
|
504 |
// Tab events bubble to TabView |
|
|
505 |
tabview.after('tab:render', this.afterTabRender, this); |
|
|
506 |
}, |
|
|
507 |
|
|
|
508 |
afterTabRender: function(e) { |
|
|
509 |
// boundingBox is the Tab's LI |
|
|
510 |
e.target.get('boundingBox').append(this.REMOVE_TEMPLATE); |
|
|
511 |
}, |
|
|
512 |
|
|
|
513 |
onRemoveClick: function(e) { |
|
|
514 |
e.stopPropagation(); |
|
|
515 |
var tab = Y.Widget.getByNode(e.target); |
|
|
516 |
tab.remove(); |
|
|
517 |
} |
|
|
518 |
}); |
|
|
519 |
|
|
|
520 |
var tabview = new Y.TabView({ |
|
|
521 |
srcNode: '#demo', |
|
|
522 |
plugins: [Addable] |
|
|
523 |
}); |
|
|
524 |
|
|
|
525 |
var tabview2 = new Y.TabView({ |
|
|
526 |
children: [{ |
|
|
527 |
label: 'foo', |
|
|
528 |
content: '<p>foo content</p>' |
|
|
529 |
}, { |
|
|
530 |
label: 'bar', |
|
|
531 |
content: '<p>bar content</p>' |
|
|
532 |
}, { |
|
|
533 |
label: 'baz', |
|
|
534 |
content: '<p>baz content</p>' |
|
|
535 |
}], |
|
|
536 |
plugins: [Removeable] |
|
|
537 |
}); |
|
|
538 |
|
|
|
539 |
var tabview3 = new Y.TabView({ |
|
|
540 |
children: [{ |
|
|
541 |
label: 'foo', |
|
|
542 |
content: '<p>foo content</p>' |
|
|
543 |
}, { |
|
|
544 |
label: 'bar', |
|
|
545 |
content: '<p>bar content</p>' |
|
|
546 |
}, { |
|
|
547 |
label: 'baz', |
|
|
548 |
content: '<p>bar content</p>' |
|
|
549 |
}], |
|
|
550 |
plugins: [Addable, Removeable] |
|
|
551 |
}); |
|
|
552 |
|
|
|
553 |
tabview.render(); |
|
|
554 |
tabview2.render('#demo2'); |
|
|
555 |
tabview3.render('#demo3'); |
|
|
556 |
}); |
|
|
557 |
</script></pre> |
|
|
558 |
|
|
|
559 |
</div> |
|
|
560 |
</div> |
|
|
561 |
</div> |
|
|
562 |
|
|
|
563 |
<div class="yui3-u-1-4"> |
|
|
564 |
<div class="sidebar"> |
|
|
565 |
|
|
|
566 |
|
|
|
567 |
|
|
|
568 |
<div class="sidebox"> |
|
|
569 |
<div class="hd"> |
|
|
570 |
<h2 class="no-toc">Examples</h2> |
|
|
571 |
</div> |
|
|
572 |
|
|
|
573 |
<div class="bd"> |
|
|
574 |
<ul class="examples"> |
|
|
575 |
|
|
|
576 |
|
|
|
577 |
<li data-description="This example shows how to create a TabView wigdet from existing HTML."> |
|
|
578 |
<a href="tabview-basic.html">TabView from Existing Markup</a> |
|
|
579 |
</li> |
|
|
580 |
|
|
|
581 |
|
|
|
582 |
|
|
|
583 |
<li data-description="This example shows how to create a TabView wigdet from JavaScript."> |
|
|
584 |
<a href="tabview-fromjs.html">Dynamic TabView from JavaScript</a> |
|
|
585 |
</li> |
|
|
586 |
|
|
|
587 |
|
|
|
588 |
|
|
|
589 |
<li data-description="This example shows how to add and remove Tabs."> |
|
|
590 |
<a href="tabview-add-remove.html">Adding and Removing Tabs</a> |
|
|
591 |
</li> |
|
|
592 |
|
|
|
593 |
|
|
|
594 |
|
|
|
595 |
<li data-description="This example shows how to load tab content remotely using a YQL plugin."> |
|
|
596 |
<a href="tabview-yql.html">Loading Tab Content</a> |
|
|
597 |
</li> |
|
|
598 |
|
|
|
599 |
|
|
|
600 |
|
|
|
601 |
|
|
|
602 |
</ul> |
|
|
603 |
</div> |
|
|
604 |
</div> |
|
|
605 |
|
|
|
606 |
|
|
|
607 |
|
|
|
608 |
<div class="sidebox"> |
|
|
609 |
<div class="hd"> |
|
|
610 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
611 |
</div> |
|
|
612 |
|
|
|
613 |
<div class="bd"> |
|
|
614 |
<ul class="examples"> |
|
|
615 |
|
|
|
616 |
|
|
|
617 |
|
|
|
618 |
|
|
|
619 |
|
|
|
620 |
|
|
|
621 |
|
|
|
622 |
|
|
|
623 |
|
|
|
624 |
|
|
|
625 |
<li data-description="Demonstrates how to add browser history support to a TabView widget using the History Utility."> |
|
|
626 |
<a href="../history/history-tabview.html">History + TabView</a> |
|
|
627 |
</li> |
|
|
628 |
|
|
|
629 |
|
|
|
630 |
</ul> |
|
|
631 |
</div> |
|
|
632 |
</div> |
|
|
633 |
|
|
|
634 |
</div> |
|
|
635 |
</div> |
|
|
636 |
</div> |
|
|
637 |
</div> |
|
|
638 |
|
|
|
639 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
640 |
<script>prettyPrint();</script> |
|
|
641 |
|
|
|
642 |
<script> |
|
|
643 |
YUI.Env.Tests = { |
|
|
644 |
examples: [], |
|
|
645 |
project: '../assets', |
|
|
646 |
assets: '../assets/tabview', |
|
|
647 |
name: 'tabview-add-remove', |
|
|
648 |
title: 'Adding and Removing Tabs', |
|
|
649 |
newWindow: '', |
|
|
650 |
auto: false |
|
|
651 |
}; |
|
|
652 |
YUI.Env.Tests.examples.push('tabview-basic'); |
|
|
653 |
YUI.Env.Tests.examples.push('tabview-fromjs'); |
|
|
654 |
YUI.Env.Tests.examples.push('tabview-add-remove'); |
|
|
655 |
YUI.Env.Tests.examples.push('tabview-yql'); |
|
|
656 |
YUI.Env.Tests.examples.push('history-tabview'); |
|
|
657 |
|
|
|
658 |
</script> |
|
|
659 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
660 |
|
|
|
661 |
|
|
|
662 |
|
|
|
663 |
</body> |
|
|
664 |
</html> |