widget-parentchild-listbox.mustache revision e808b8824ca1091c8efb5669db9129e68e5e1c14
bd033e737f045c5201032efa676a4d7a5e9d4a24joncruz<style type="text/css" scoped>
51545bd76d20f04e3e2162b7a94b346e03da2781tweenk #mylistbox em {
bd033e737f045c5201032efa676a4d7a5e9d4a24joncruz font-style:normal;
51545bd76d20f04e3e2162b7a94b346e03da2781tweenk #selected {
bd033e737f045c5201032efa676a4d7a5e9d4a24joncruz border:1px solid #aaa;
bd033e737f045c5201032efa676a4d7a5e9d4a24joncruz padding:2px;
bd033e737f045c5201032efa676a4d7a5e9d4a24joncruz width:15em;
<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>
<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>
<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
<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>
if (this.isRoot()) {
// This will help us easily crete next/previous item handling using the arrow keys
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,
if (this.get("multiple")) {
if (domEvent.shiftKey) {
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,
if (this.get("multiple")) {
if (domEvent.metaKey) {
item.set("selected", 1);
item.set("selected", 1);
item.set("selected", 1);
var parent = item.get("parent"),
if (sibling instanceof Y.ListBox) {
sibling.set("selected", 1);
NESTED_TEMPLATE : '<li class="{nestedOptionClassName}"><em class="{labelClassName}">{label}</em></li>',
if (this.get("depth") > -1) {
labelClassName : this.getClassName("label"),
nestedOptionClassName : this.getClassName("option"),
label : this.get("label")
li = Y.Node.create(liHtml),
boundingBox = this.get("boundingBox"),
parent = boundingBox.get("parentNode");
li.appendChild(boundingBox);
parent.appendChild(li);
<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>
// In this case, when a configuration object (e.g. { label:"My Option" }),
// attribute, we want to create instances of Y.Option
validator: Y.Lang.isString
<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>
validator: Y.Lang.isString
<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']});
<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>
fullpath: "listbox.js",
// 2 children (the defaultChildType will be used to create instances of Y.Option with the
var listbox = new Y.ListBox({
<p>We can also use the `add` method provided by WidgetParent, to add children after contruction, and then render to the DOM:</p>
listbox.add({ label: "Item Four" });
new Y.Option({ label: "Item Five" })
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");