1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<style type="text/css" scoped>
#mylistbox em {
font-style:normal;
}
#selected {
border:1px solid #aaa;
padding:2px;
width:15em;
}
.yui3-listbox {
padding:0;
margin: .25em;
border: solid 1px #000;
background-color:#fff;
white-space:nowrap;
}
.yui3-listbox .yui3-listbox {
margin-top: .25em;
margin-bottom: .25em;
border: none;
}
.yui3-listbox .yui3-option,
.yui3-listbox .yui3-listbox-option {
margin:0;
padding:0;
cursor:default;
list-style-image:none;
list-style-position:outside;
list-style-type:none;
}
.yui3-option-content,
.yui3-listbox-label {
display: block;
padding: .25em .5em;
}
.yui3-listbox-content {
margin:0;
padding:0;
overflow:auto;
}
.yui3-listbox .yui3-listbox .yui3-option-content {
margin-left:.5em;
}
.yui3-listbox-label {
font-weight: bold;
}
.yui3-option-selected {
background-color: #cccccc;
}
.yui3-option-focused {
outline: none;
background-color: blue;
color: #fff;
}
</style>
<div class="intro">
<p>This is an advanced example, in which we create a ListBox widget with nested Option widgets, by extending the base `Widget` class, and adding `WidgetParent` and `WidgetChild` extensions, through `Base.build`.</p>
<p>The <a href="../tabview">TabView</a> component that is included in the YUI 3 library, is also built using the WidgetParent and WidgetChild extensions.</p>
</div>
<div class="example">
{{>widget-parentchild-listbox-source}}
</div>
<h3>The WidgetParent and WidgetChild Extensions</h3>
<p><a href="{{apiDocs}}/WidgetParent.html">WidgetParent</a> is an extension, designed to be used with `Base.build` to create a class of Widget which is designed to contain child Widgets (for example a Tree widget, which contains TreeNode children).
WidgetParent itself augments <a href="{{apiDocs}}/ArrayList.html">ArrayList</a> providing a convenient set of array iteration and convenience methods, allowing users of your class to easily work with parent's list of children.</p>
<p><a href="{{apiDocs}}/WidgetChild.html">WidgetChild</a> is also an extension, designed to be used with `Base.build` to create a class of Widget which is designed to nested inside parent Widgets (for example a TreeNode widget, which sits inside a Tree widget).</p>
<p>A Widget can be built with both the WidgetParent and WidgetChild extensions (it can be both a Parent and a Child), in cases where we want to support multi-level hierarchies, such as the ListBox example below.</p>
<p>In addition to providing the basic support to manage (add/remove/iterate/render) children the Widget Parent/Child implementations also provides support for both single and multiple selection models.</p>
<h3>Using WidgetParent and WidgetChild to Create the ListBox Class</h3>
<p>For ListBox, since we're creating a new class from scratch, we use the sugar version of `Base.build`, called `Base.create`, which allows us to easily create a new class and define it's prototype and static properties/methods, in a single call, as shown below:</p>
```
// Create a new class, ListBox, which extends Widget, and mixes in both the WidgetParent and WidgetChild
// extensions since we want to be able to nest one ListBox inside another, to create heirarchical listboxes
// Prototype Properties for ListBox
}, {
// Static Properties for ListBox
});
```
<p>We can then go ahead and fill out the prototype and static properties we want to override in our ListBox implementation, while Widget, WidgetParent and WidgetChild provide the basic Widget rendering and parent-child relationship support. Comments inline below provide the background:</p>
<h4>Prototype Method and Properties</h4>
```
// The default content box for ListBoxes will be a UL (Widget uses a DIV by default)
CONTENT_TEMPLATE : "<ul></ul>",
// Setup Custom Listeners
bindUI: function() {
if (this.isRoot()) {
// Setup custom focus handling, using the NodeFocusManager plugin
// This will help us easily crete next/previous item handling using the arrow keys
this.get("boundingBox").plug(Y.Plugin.NodeFocusManager, {
descendants: ".yui3-option",
keys: {
next: "down:40", // Down arrow
previous: "down:38" // Up arrow
},
circular: true
});
}
this.get("boundingBox").on("contextmenu", function (event) {
});
// Setup listener to control keyboard based single/multiple item selection
this.on("option:keydown", function (event) {
var item = event.target,
domEvent = event.domEvent,
keyCode = domEvent.keyCode,
direction = (keyCode == 40);
if (this.get("multiple")) {
if (keyCode == 40 || keyCode == 38) {
if (domEvent.shiftKey) {
this._selectNextSibling(item, direction);
} else {
this._selectNextSibling(item, direction);
}
}
} else {
if (keyCode == 13 || keyCode == 32) {
item.set("selected", 1);
}
}
});
// Setup listener to control mouse based single/multiple item selection
this.on("option:mousedown", function (event) {
var item = event.target,
domEvent = event.domEvent,
selection;
if (this.get("multiple")) {
if (domEvent.metaKey) {
item.set("selected", 1);
} else {
item.set("selected", 1);
}
} else {
item.set("selected", 1);
}
});
},
// Helper Method, to find the correct next sibling, taking into account nested ListBoxes
_selectNextSibling : function(item, direction) {
var parent = item.get("parent"),
method = (direction) ? "next" : "previous",
// Only go circular for the root listbox
circular = (parent === this),
sibling = item[method](circular);
if (sibling) {
// If we found a sibling, it's either an Option or a ListBox
if (sibling instanceof Y.ListBox) {
// If it's a ListBox, select it's first child (in the direction we're headed)
sibling.selectChild((direction) ? 0 : sibling.size() - 1);
} else {
// If it's an Option, select it
sibling.set("selected", 1);
}
} else {
// If we didn't find a sibling, we're at the last leaf in a nested ListBox
parent[method](true).set("selected", 1);
}
},
// The markup template we use internally to render nested ListBox children
NESTED_TEMPLATE : '<li class="{nestedOptionClassName}"><em class="{labelClassName}">{label}</em></li>',
renderUI: function () {
// Handling Nested Child Element Rendering
if (this.get("depth") > -1) {
var tokens = {
labelClassName : this.getClassName("label"),
nestedOptionClassName : this.getClassName("option"),
label : this.get("label")
},
liHtml = Y.substitute(this.NESTED_TEMPLATE, tokens),
li = Y.Node.create(liHtml),
boundingBox = this.get("boundingBox"),
parent = boundingBox.get("parentNode");
li.appendChild(boundingBox);
parent.appendChild(li);
}
}
} { /* static properties */ });
```
<h4>Static Properties</h4>
<p>The only static property we're interested in defining for the ListBox class is the `ATTRS` property. Comments inline below provide the background:</p>
```
{
// Define any new attributes, or override existing ones
ATTRS : {
// We need to define the default child class to use,
// when we need to create children from the configuration
// object passed to add or to the "children" attribute (which is provided by WidgetParent)
// In this case, when a configuration object (e.g. { label:"My Option" }),
// is passed into the add method,or as the value of the "children"
// attribute, we want to create instances of Y.Option
defaultChildType: {
value: "Option"
},
// Setup Label Attribute
label : {
validator: Y.Lang.isString
}
}
}
```
<h3>Using WidgetChild to Create the Option (leaf) Class</h3>
<p>The Option class is pretty simple, and largely just needs the attribute and API provided by WidgetChild. We only need to over-ride the default templates and tabIndex handling:</p>
```
// Override the default DIVs used for rendering the bounding box and content box.
CONTENT_TEMPLATE : "<em></em>",
BOUNDING_TEMPLATE : "<li></li>",
// Handle rendering the label attribute
renderUI: function () {
}
}, {
ATTRS : {
// Setup Label Attribute
label : {
validator: Y.Lang.isString
},
// Override the default tabIndex for an Option,
// since we want FocusManager to control keboard
// based focus
tabIndex: {
value: -1
}
}
});
```
<h3>Adding The Code As A "listbox" Custom Module</h3>
<p>This example also shows how you can package code for re-use as a module, by registering it through the `YUI.add` method, specifying any requirements it has (the packaged code is available in ./assets/listbox.js).</p>
```
YUI.add('listbox', function(Y) {
Y.ListBox = ...
Y.Option = ...
}, '3.1.0' ,{requires:['substitute', 'widget', 'widget-parent', 'widget-child', 'node-focusmanager']});
```
<h3>Using the Custom "listbox" Module</h3>
<p>To create an instance of a ListBox, we ask for the "listbox" module we packaged in the previous step, through `YUI().use("listbox")`:</p>
```
YUI({
modules: {
"listbox": {
fullpath: "listbox.js",
requires: ["substitute", "widget", "widget-parent", "widget-child", "node-focusmanager"]
}
}
}).use("listbox", function (Y) {
// Create the top level ListBox instance, and start it off with
// 2 children (the defaultChildType will be used to create instances of Y.Option with the
// children configuration passed in below).
var listbox = new Y.ListBox({
id:"mylistbox",
width:"13em",
height:"15em",
children: [
{ label: "Item One" },
{ label: "Item Two" }
]
});
...
});
```
<p>We can also use the `add` method provided by WidgetParent, to add children after contruction, and then render to the DOM:</p>
```
// Then we add a nested ListBox which itself has 2 children, using
// the add API provided by WidgetParent
type: "ListBox",
label: "Item Three",
children: [
{ label: "Item Three - One" },
{ label: "Item Three - Two" }
]
});
// One more Option child
listbox.add({ label: "Item Four" });
// One more Option child, using providing an actual
// instance, as opposed to just the configuration
new Y.Option({ label: "Item Five" })
);
// And finally, a last nested ListBox, again with
// 2 children
type: "ListBox",
label: "Item Six",
children: [
{ label: "Item Six - One" },
{ label: "Item Six - Two" }
]
});
// Render it, using Widget's render method,
// to the "#exampleContainer" element.
listbox.render("#exampleContainer");
```
<p>The ListBox fires selectionChange events, every time it's selection state changes (provided by WidgetParent), which we can listen and respond to:</p>
```
listbox.after("selectionChange", function(e) {
var selection = this.get("selection");
if (selection instanceof Y.ListBox) {
selection = selection.get("selection");
}
if (selection) {
Y.one("#selection").setContent(selection.get("label"));
}
});
```
<h3>The CSS</h3>
```
.yui3-listbox {
padding:0;
margin: .25em;
border: solid 1px #000;
background-color:#fff;
white-space:nowrap;
}
.yui3-listbox .yui3-listbox {
margin-top: .25em;
margin-bottom: .25em;
border: none;
}
.yui3-listbox .yui3-option,
.yui3-listbox .yui3-listbox-option {
margin:0;
padding:0;
cursor:default;
list-style-image:none;
list-style-position:outside;
list-style-type:none;
}
.yui3-option-content,
.yui3-listbox-label {
display: block;
padding: .25em .5em;
}
.yui3-listbox-content {
margin:0;
padding:0;
overflow:auto;
}
.yui3-listbox .yui3-listbox .yui3-option-content {
margin-left:.5em;
}
.yui3-listbox-label {
font-weight: bold;
}
.yui3-option-selected {
background-color: #cccccc;
}
.yui3-option-focused {
outline: none;
background-color: blue;
color: #fff;
}
```
<h2>Complete Example Source</h2>
```
{{>widget-parentchild-listbox-source}}
```