parent-child.html revision 516282bd16544bd0fa68565203a4fa8ef51036c7
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney <link rel="stylesheet" type="text/css" href="/build/cssreset/reset-min.css">
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney <link rel="stylesheet" type="text/css" href="/build/cssfonts/fonts-min.css">
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney <link rel="stylesheet" type="text/css" href="/build_files/tablist.css">
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney <script src="/build/yui/yui-min.js" type="text/javascript"></script>
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney YUI({filter: "raw"}).use("widget", "widget-parent", "widget-child", "test", "console", function (Y) {
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney var RootWidget = function () {
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney RootWidget.superclass.constructor.apply(this, arguments);
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney RootWidget.NAME = "rootwidget";
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney Y.RootWidget = Y.Base.build("rootwidget", RootWidget, [Y.WidgetParent], { dynamic: false });
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney var ParentWidget = function () {
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney ParentWidget.superclass.constructor.apply(this, arguments);
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney Y.mix(ParentWidget, {
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney NAME: "parentwidget",
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney renderUI: function () {
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney ParentWidget.superclass.renderUI.apply(this, arguments);
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney if (this.get("depth") > -1) {
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney var label = Y.Node.create("<em>" + this.get("label") + "</em>"),
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney parent = this.get("boundingBox").get("parentNode");
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney Y.ParentWidget = Y.Base.build("parentwidget", ParentWidget, [Y.WidgetParent, Y.WidgetChild], { dynamic: false });
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney var ChildWidget = function () {
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney ChildWidget.superclass.constructor.apply(this, arguments);
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney Y.mix(ChildWidget, {
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney NAME: "childwidget",
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney renderUI: function () {
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney this.get("contentBox").setContent(this.get("label"));
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney Y.ChildWidget = Y.Base.build("childwidget", ChildWidget, [Y.WidgetChild], { dynamic: false });
752e31b910dbf30e2b803437da522585eca28528Matt Sweeney var debugConsole = new Y.Console().render();
var root = new Y.ParentWidget(),
child = root.add({ type: "ChildWidget", label: "Child One" });
Y.Assert.isNotUndefined(child, "Add should return a reference to the child that was created and added.");
child = root.add({ childType: ChildWidget, label: "Child Two" });
Y.Assert.isNotUndefined(child, "Add should return a reference to the child that was created and added.");
child = root.add({ label: "Invalid Child" });
Y.Assert.isUndefined(child, "Add should return undefined since the type of the child is undefined.");
Y.Assert.isNotUndefined(e, "The \"add\" method should throw an error since the type of the child was not specified and the defaultChildType attribute is not defined.");
child = root.add({ childType: "MyChild", label: "Invalid Child" });
Y.Assert.isUndefined(child, "The \"add\" method should return undefined since the type of the child is invalid.");
Y.Assert.isNotUndefined(e, "The \"add\" method should throw an error since the type of the child is invalid.");
child = root.add({ type: MyChild, label: "Invalid Child" });
Y.Assert.isUndefined(child, "Add should return undefined since the type of the child is invalid.");
Y.Assert.isNotUndefined(e, "The \"add\" method should throw an error since the type of the child is invalid.");
root.set("defaultChildType", "ChildWidget");
child = root.add({ label: "Child Three" });
Y.Assert.isNotUndefined(child, "Add should return a reference to the child that was created and added.");
root.set("defaultChildType", ChildWidget);
child = root.add({ label: "Child Four" });
Y.Assert.isNotUndefined(child, "Add should return a reference to the child that was created and added.");
root.set("defaultChildType", "MyChild");
Y.Assert.areEqual(root.get("defaultChildType"), Y.ChildWidget, "Setter for \"defaultChildType\" should disallow setting the attribute to an invalid value.");
root.set("defaultChildType", MyChild);
Y.Assert.areEqual(root.get("defaultChildType"), Y.ChildWidget, "Setter for \"defaultChildType\" should disallow setting the attribute to an invalid value.");
root.render();
root.destroy();
var widget = new Y.ParentWidget({
{ type: Y.ChildWidget, label: "Child One", id: "child-1" },
{ type: Y.ChildWidget, label: "Child Two", id: "child-2" },
{ type: Y.ChildWidget, label: "Child Three", id: "child-3" },
{ type: Y.ChildWidget, label: "Child Four", id: "child-4" }
Y.Assert.areEqual(widget.item(0).get("id"), "inserted-child-1", "The newly inserted child should be the parent's first child.");
{ type: Y.ChildWidget, label: "Child One", id: "inserted-child-2" },
{ type: Y.ChildWidget, label: "Child One", id: "inserted-child-3" }
Y.Assert.areEqual(widget.item(2).get("id"), "inserted-child-2", "The child with the id of \"inserted-child-2\" should be the parent's third child.");
Y.Assert.areEqual(widget.item(3).get("id"), "inserted-child-3", "The child with the id of \"inserted-child-3\" should be the parent's fourth child.");
Y.Assert.areEqual(widget.item(4).get("id"), "child-2", "The child with the id of \"child-2\" should now be the parent's fifth child.");
var widget = new Y.ParentWidget({
{ type: Y.ChildWidget, label: "Child One", id: "child-1" },
{ type: Y.ChildWidget, label: "Child Two", id: "child-2" },
{ type: Y.ChildWidget, label: "Child Three", id: "child-3" },
{ type: Y.ChildWidget, label: "Child Four", id: "child-4" }
var childSelector = "." + widget.item(0).getClassName(),
parentBB = widget.get("boundingBox");
Y.Assert.areEqual(widget.remove(1).get("id"), "child-2", "The parent's \"remove\" method should return a reference to the child removed.");
Y.Assert.areEqual(parentBB.one("#child-2"), null, "child-2's bounding box should have been removed");
Y.Assert.areEqual(parentBB.all(childSelector).size(), 3, "The widget should now have three children bounding boxes.");
Y.Assert.areEqual(widget.item(0).remove().get("id"), "child-1", "If a child calls the \"remove\" method on itself, the \"remove\" method should return a reference to the child.");
Y.Assert.areEqual(parentBB.one("#child-1"), null, "child-1's bounding box should have been removed");
Y.Assert.areEqual(parentBB.all(childSelector).size(), 2, "The widget should now have two children bounding boxes.");
var removed = widget.removeAll();
Y.Assert.areEqual(removed.size(), 2, "The \"removeAll\" method should return a Y.ArrayList instance with a size of 2.");
Y.Assert.areEqual(parentBB.all(childSelector).size(), 0, "The widget should now have no children bounding boxes.");
var root = new Y.ParentWidget({
{ type: Y.ChildWidget, label: "Child One" },
{ type: Y.ChildWidget, label: "Child Two" },
{ type: Y.ChildWidget, label: "Child Three" },
{ type: Y.ChildWidget, label: "Child Four" }
root.render();
root.item(0).set("selected", 1);
root.item(1).set("selected", 1);
Y.Assert.areEqual(root.item(1).get("selected"), 1, "The second child of the parent widget should be selected");
Y.Assert.areEqual(root.get("selection"), root.item(1), "The parent's \"selection\" attribute should return a reference to its second child.");
root.item(2).set("selected", 1);
Y.Assert.areEqual(root.item(1).get("selected"), 0, "The parent's second child should not be selected");
Y.Assert.areEqual(root.get("selection"), root.item(2), "The parent's \"selection\" attribute should return a reference to its third child.");
root.item(2).set("selected", 0);
Y.Assert.areEqual(root.item(2).get("selected"), 0, "The parent's third child should not be selected");
root.destroy();
var tree = new Y.ParentWidget({
{ type: Y.ChildWidget, id: "leaf-1", label: "Leaf One" },
{ type: Y.ChildWidget, id: "leaf-2", label: "Leaf Two" },
{ type: Y.ParentWidget, id: "subtree", label: "Subtree", children: [
{ type: Y.ChildWidget, id: "subtree-leaf-1", label: "Subtree - Leaf One" },
{ type: Y.ChildWidget, id: "subtree-leaf-2", label: "Subtree - Leaf Two" },
{ type: Y.ChildWidget, id: "subtree-leaf-3", label: "Subtree - Leaf Three" },
{ type: Y.ChildWidget, id: "subtree-leaf-4", label: "Subtree - Leaf Four" }
tree.render();
tree.item(2).item(0).set("selected", 1);
Y.Assert.areEqual(tree.get("selected"), 2, "The root's \"selected\" attribute should be 2 (indicating partially selected).");
Y.Assert.areEqual(tree.item(2).get("selected"), 2, "The subtree's \"selected\" attribute should be 2 (indicating partially selected).");
Y.Assert.areEqual(tree.get("selection").get("id"), "subtree", "The root's \"selection\" attribute should return the subtree.");
Y.Assert.areEqual(tree.item(2).get("selection").get("id"), "subtree-leaf-1", "The subtree's \"selection\" attribute should return the first child.");
tree.item(0).set("selected", 1);
Y.Assert.areEqual(tree.get("selected"), 2, "The root's \"selected\" attribute should be 2 (indicating partially selected).");
Y.Assert.areEqual(tree.get("selection").get("id"), "leaf-1", "The root's \"selection\" attribute should return a reference to its first child.");
Y.Assert.areEqual(tree.item(2).get("selection"), null, "The subtree's \"selection\" attribute should return null.");
tree.item(2).item(2).set("selected", 1);
Y.Assert.areEqual(tree.get("selected"), 2, "The root's \"selected\" attribute should be 2 (indicating partially selected).");
Y.Assert.areEqual(tree.item(2).get("selected"), 2, "The subtree's \"selected\" attribute should be 2 (indicating partially selected).");
Y.Assert.areEqual(tree.get("selection").get("id"), "subtree", "The root's \"selection\" attribute should return the subtree.");
Y.Assert.areEqual(tree.item(2).get("selection").get("id"), "subtree-leaf-3", "The subtree's \"selection\" attribute should return its third child.");
tree.destroy();
var root = new Y.ParentWidget({
{ type: Y.ChildWidget, label: "Child One" },
{ type: Y.ChildWidget, label: "Child Two" },
{ type: Y.ChildWidget, label: "Child Three" },
{ type: Y.ChildWidget, label: "Child Four" }
root.render();
root.item(0).set("selected", 1);
root.item(1).set("selected", 1);
Y.Assert.areEqual(root.item(0).get("selected"), 1, "The first child of the parent widget should be selected.");
Y.Assert.areEqual(root.item(1).get("selected"), 1, "The second child of the parent widget should be selected.");
Y.Assert.areEqual(root.get("selection").size(), 2, "The \"selection\" attribute should return an ArrayList with a size of 2.");
Y.Assert.areEqual(root.get("selected"), 2, "The root's \"selection\" attribute should be 2 (indicating partially selected).");
root.item(2).set("selected", 1);
root.item(3).set("selected", 1);
Y.Assert.areEqual(root.get("selection").size(), 4, "The \"selection\" attribute should return an ArrayList with a size of 4.");
Y.Assert.areEqual(root.get("selected"), 1, "The root's \"selected\" attribute should return 1 (indicating fully selected).");
root.get("selection").each(function (child) {
child.set("selected", 0);
Y.Assert.areEqual(root.get("selection").size(), 4, "The root's \"selection\" attribute should return an ArrayList with a size of 4.");
Y.Assert.areEqual(root.get("selection"), null, "The root's \"selection\" attribute should return an ArrayList with a size of 4.");
{ type: Y.ChildWidget, id: "subtree-leaf-1", label: "Subtree - Leaf One" },
{ type: Y.ChildWidget, id: "subtree-leaf-2", label: "Subtree - Leaf Two" },
{ type: Y.ChildWidget, id: "subtree-leaf-3", label: "Subtree - Leaf Three" },
{ type: Y.ChildWidget, id: "subtree-leaf-4", label: "Subtree - Leaf Four" }
root.render();
Y.Assert.isTrue(root.item(4).get("multiple"), "The \"multiple\" attribute of the nested parent widget should be true.");
root.destroy();
var widget = new Y.ParentWidget({
{ type: Y.ChildWidget, label: "Child One", id: "child-1" },
{ type: Y.ChildWidget, label: "Child Two", id: "child-2" },
{ type: Y.ChildWidget, label: "Child Three", id: "child-3" },
{ type: Y.ChildWidget, label: "Child Four", id: "child-4" }
var root = new Y.RootWidget({ id: "rootwidget-1" });
root.add(widget);
root.render();
Y.Assert.areEqual(widget.item(0).get("root").get("id"), "rootwidget-1", "The \"root\" attribute of \"child-1\" should return a reference to rootwidget-1");
Y.Assert.areEqual(widget.get("root").get("id"), "rootwidget-1", "The \"root\" attribute of \"widget-1\" should return a reference to rootwidget-1");
Y.Assert.areEqual(widget.item(0).get("depth"), 1, "The \"depth\" attribute of \"child-1\" should return 1.");
Y.Assert.areEqual(widget.item(0).ancestor(0).get("id"), "widget-1", "The ancestor of \"child-1\" at depth 0 should be \"widget-1\"");
Y.Assert.areEqual(widget.item(0).ancestor(-1).get("id"), "rootwidget-1", "The ancestor of \"child-1\" at depth -1 should be \"rootwidget-1\"");
Y.Assert.areEqual(widget.item(0).get("root").get("id"), "widget-1", "The \"root\" attribute of \"child-1\" should return a reference to widget-1");
Y.Assert.areEqual(widget.get("root").get("id"), "widget-1", "The \"root\" attribute of \"widget-1\" should return a reference to widget-1");
Y.Assert.isTrue(widget.isRoot(), "Calling the \"isRoot\" method on \"widget-1\" should return \"widget-1\"");
Y.Assert.areEqual(widget.item(0).get("depth"), 0, "The \"depth\" attribute of \"child-1\" should return 0");
Y.Assert.areEqual(widget.item(0).ancestor(-1).get("id"), "widget-1", "The ancestor of \"child-1\" at depth -1 should be \"widget-1\""); // parent depth
root.destroy();
var widget = new Y.ParentWidget({
{ type: Y.ChildWidget, label: "Child One", id: "child-1" },
{ type: Y.ChildWidget, label: "Child Two", id: "child-2" },
{ type: Y.ChildWidget, label: "Child Three", id: "child-3" },
{ type: Y.ChildWidget, label: "Child Four", id: "child-4" }
Y.Assert.areEqual(widget.item(3).get("index"), 3, "The \"index\" attribute of \"child-4\" should return 3.");
Y.Assert.areEqual(widget.item(3).next(true).get("id"), "child-1", "Calling the \"next\" method with the circular flag should return a reference to \"child-1\"");
Y.Assert.areEqual(widget.item(0).previous(true).get("id"), "child-4", "Calling the \"previous\" method with the circular flag should return a reference to \"child-4\"");
var tree = new Y.ParentWidget({
{ type: Y.ChildWidget, id: "leaf-1", label: "Leaf One" },
{ type: Y.ChildWidget, id: "leaf-2", label: "Leaf Two" },
{ type: Y.ParentWidget, id: "subtree", label: "Subtree", children: [
{ type: Y.ChildWidget, id: "subtree-leaf-1", label: "Subtree - Leaf One" },
{ type: Y.ChildWidget, id: "subtree-leaf-2", label: "Subtree - Leaf Two" },
{ type: Y.ChildWidget, id: "subtree-leaf-3", label: "Subtree - Leaf Three" },
{ type: Y.ChildWidget, id: "subtree-leaf-4", label: "Subtree - Leaf Four" }
tree.render();
tree.item(2).item(0).focus();
Y.Assert.areEqual(tree.item(2).get("activeDescendant"), tree.item(2).item(0), "The \"activeDescendant\" attribute of \"subtree\" should return a reference to \"subtree-leaf-1\"");
Y.Assert.areEqual(tree.get("activeDescendant"), tree.item(2).item(0), "The \"activeDescendant\" attribute of the root widget should return a reference to the \"subtree\"");
tree.destroy();