Created
December 6, 2011 02:19
-
-
Save sdesai/1436402 to your computer and use it in GitHub Desktop.
Tree Example with WidgetHTMLRenderer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
<html> | |
<head> | |
<script src="../../../../build/yui/yui.js"></script> | |
<script src="../../../../build/widget-htmlrenderer/widget-htmlrenderer.js"></script> | |
<style> | |
.yui3-parentwidget { | |
border:1px solid black; | |
} | |
.yui3-parentwidget-content { | |
border:1px solid blue; | |
padding:5px; | |
} | |
.yui3-childwidget { | |
border:1px solid green; | |
padding:5px; | |
} | |
.yui3-childwidget-content { | |
border:1px solid red; | |
padding:5px; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
YUI({filter:"raw", combine:false}).use('base', 'widget', 'widget-parent', 'widget-child', 'widget-htmlrenderer', function (Y) { | |
var ParentWidget = Y.ParentWidget = Y.Base.create("parentwidget", Y.Widget, [Y.WidgetParent, Y.WidgetChild, Y.WidgetHTMLRenderer], { | |
renderUI: function (cbBuffer) { | |
if (this.get("depth") > -1) { | |
cbBuffer.push("<em>" + this.get("label") + "</em>"); | |
} | |
}, | |
bindUI: function() { | |
if (this.isRoot()) { | |
this.get("boundingBox").on("click", function(e) { | |
alert(e.target.ancestor(".yui3-widget", true).get("id")); | |
}); | |
} | |
}, | |
_renderChildren: function (cbBuffer) { | |
// Left this as a string on purpose, since the other buffer was | |
// confusing you. | |
// But you can see how you could replace the | |
// childrenHTML = ""; childrenHTML += child.renderHTML(); with | |
// childrenHTML = [], childrenHTML.push(child.renderHTML()); | |
var childrenHTML = ""; | |
this.each(function (child) { | |
childrenHTML += child.renderHTML(); | |
}); | |
cbBuffer.push(childrenHTML); | |
} | |
}, { | |
ATTRS: { | |
label: { | |
value:"Default Parent Label" | |
} | |
} | |
}); | |
var ChildWidget = Y.ChildWidget = Y.Base.create("childwidget", Y.Widget, [Y.WidgetChild, Y.WidgetHTMLRenderer], { | |
renderUI: function (cbBuffer) { | |
cbBuffer.push(this.get("label")); | |
} | |
}, { | |
ATTRS: { | |
label: { | |
value:"Default Child Label" | |
} | |
} | |
}); | |
var tree = new Y.ParentWidget({ | |
id: "tree", | |
children: [ | |
{ 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(); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment