1 /**
  2  * @class TreeView
  3  * @extends Subscriber
  4  */
  5 wso2vis.s.form.TreeView = function() {
  6 	wso2vis.s.Subscriber.call(this);
  7 
  8     /* @private */
  9     this.tree = null;
 10     this.data = null;
 11 };
 12 
 13 wso2vis.extend(wso2vis.s.form.TreeView, wso2vis.s.Subscriber);
 14 
 15 wso2vis.s.form.TreeView.prototype
 16     .property("canvas")
 17     .property("nodeLabel")
 18     .property("nodeValue")
 19     .property("nodeChildren")
 20     .property("dataField");
 21 
 22 wso2vis.s.form.TreeView.prototype.create = function() {
 23 
 24     var that = this;
 25 
 26     //instantiate the TreeView control:
 27     this.tree = new YAHOO.widget.TreeView(this.canvas());
 28     var rootNode = this.tree.getRoot();
 29 
 30     if( this.data !== null ){
 31         //begin adding children 
 32         rec(rootNode, this.data);
 33     }
 34 
 35     function rec(node, data) {
 36         var children;
 37         if( data === undefined || data === null ) {
 38             return;
 39         }
 40 
 41         var dataField = that.traverseToDataField(data, that.dataField());
 42 
 43         if (dataField instanceof Array) {
 44             children = dataField.length;
 45         }
 46         else {
 47             children = 1;
 48         }
 49 
 50         for (var i=0; i<children; i++) {
 51             var dataObj;
 52             if ( dataField instanceof Array ){
 53                 dataObj = dataField[i];
 54             }
 55             else {
 56                 dataObj = dataField;
 57             }
 58             var nodeLabel = that.traverseToDataField(dataObj, that.nodeLabel());
 59             var nodeValue = that.traverseToDataField(dataObj, that.nodeValue());
 60             var nodeChildren = that.traverseToDataField(dataObj, that.nodeChildren());
 61 
 62             var dataNode = {};
 63             dataNode.label = nodeLabel;
 64             dataNode.value = nodeValue;
 65 
 66             var childNode = new YAHOO.widget.TextNode(dataNode, node, true);
 67             rec(childNode, nodeChildren);
 68         }
 69     }
 70 
 71     // Expand and collapse happen prior to the actual expand/collapse,
 72     // and can be used to cancel the operation
 73     this.tree.subscribe("expand", this.onExpand);
 74     this.tree.subscribe("collapse", this.onCollapse);
 75     this.tree.subscribe("labelClick", this.onLabelClick);
 76 
 77     this.tree.draw();
 78 };
 79 
 80 wso2vis.s.form.TreeView.prototype.update = function() {
    var canvas = document.getElementById(this.canvas());
    canvas.innerHTML = "";
 81 
 82     this.create();
};
 83 
 84 wso2vis.s.form.TreeView.prototype.onExpand = function(node) {
 85     console.log(node.index + " - " + node.label + " was expanded");
 86 };
 87 
 88 wso2vis.s.form.TreeView.prototype.onCollapse = function(node) {
 89     console.log(node.index + " - " + node.label + " was collapsed");
 90 };
 91 
 92 wso2vis.s.form.TreeView.prototype.onLabelClick = function(node) {
 93     console.log(node.index + " - " + node.label + " label was clicked");
 94 };
 95 
 96 wso2vis.s.form.TreeView.prototype.pushData = function(data) {
 97       this.data = data;//console.log(data);
 98       this.update();
 99 };
100 
101 wso2vis.s.form.TreeView.prototype.traverseToDataField = function (object, dataFieldArray) {
102 	var a = object;					
103 	for (var i = 0; i < dataFieldArray.length; i++) {
104 		a = a[dataFieldArray[i]];
105 	}
106 	return a;
107 };
108 
109