o2.widget = o2.widget || {}; o2.require("o2.widget.Common", null, false); o2.widget.Tree = new Class({ Extends: o2.widget.Common, Implements: [Options, Events], children: [], options: { "style": "default", "expand": false, "text": "html" }, jsonMapping: { "expand": "expand", "title": "title", "text": "text", "action": "action", "icon": "icon", "style": "", "sub": "sub", "default" : "default" }, initialize: function(container, options){ this.setOptions(options); this.path = o2.session.path+"/widget/$Tree/"; this.cssPath = o2.session.path+"/widget/$Tree/"+this.options.style+"/css.wcss"; this._loadCss(); this.container = $(container); this.children = []; this.treeJson = null; this.treeXML = null; }, load: function(json){ if (this.fireEvent("queryLoad")){ if (json) this.treeJson = json; this.node = new Element("div",{ "styles": this.css.areaNode }); this.loadTree(); this.node.inject(this.container); this.fireEvent("postLoad"); } }, empty: function(){ this.children.each(function(o){ o2.release(o); }.bind(this)); this.node.empty(); }, reload: function(json){ if (json) this.treeJson = json; this.children = []; this.node.empty(); this.loadTree(); }, reLoad: function(json){ if (json) this.treeJson = json; this.children = []; this.node.empty(); this.loadTree(); }, loadTree: function(){ if (this.treeJson){ this.loadJsonTree(this.treeJson, this, this); }else if (this.treeXML){ this.loadXMLTree(); } if (this.container) this.node.inject(this.container); }, mappingJson: function(mapping){ if (mapping.expand) this.jsonMapping.expand = mapping.expand; if (mapping.title) this.jsonMapping.title = mapping.title; if (mapping.text) this.jsonMapping.text = mapping.text; if (mapping.action) this.jsonMapping.action = mapping.action; if (mapping.icon) this.jsonMapping.icon = mapping.icon; if (mapping.style) this.jsonMapping.style = mapping.style; if (mapping.sub) this.jsonMapping.sub = mapping.sub; if (mapping.default) this.jsonMapping.default = mapping.default; }, loadJsonTree: function(treeJson, tree, node){ treeJson.each(function(item){ var options = {}; if (item[this.jsonMapping.expand]!=undefined) options.expand = item[this.jsonMapping.expand]; if (item[this.jsonMapping.title]) options.title = item[this.jsonMapping.title]; if (item[this.jsonMapping.text]) options.text = item[this.jsonMapping.text]; if (item[this.jsonMapping.action]) options.action = item[this.jsonMapping.action]; if (item[this.jsonMapping.style]) options.style = item[this.jsonMapping.style]; if (item[this.jsonMapping.icon]) options.icon = item[this.jsonMapping.icon]; if (item[this.jsonMapping.default]) options.default = item[this.jsonMapping.default]; var treeNode = node.appendChild(options, item); if (item[this.jsonMapping.sub]){ this.loadJsonTree(item[this.jsonMapping.sub], this, treeNode); } }.bind(tree)); }, appendChild: function(obj, json){ var treeNode = new this.$constructor.Node(this, obj); treeNode.json = json; if (this.children.length){ treeNode.previousSibling = this.children[this.children.length-1]; treeNode.previousSibling.nextSibling = treeNode; }else{ this.firstChild = treeNode; } treeNode.load(); treeNode.node.inject(this.node); this.children.push(treeNode); return treeNode; }, expandOrCollapseNode: function(treeNode){ if (treeNode.options.expand){ this.collapse(treeNode); treeNode.options.expand = false; }else{ this.expand(treeNode); treeNode.options.expand = true; } treeNode.setOperateIcon(); }, expand: function(treeNode){ if (this.fireEvent("queryExpand", [treeNode])){ treeNode.childrenNode.setStyle("display", "block"); } this.fireEvent("postExpand", [treeNode]); }, collapse: function(treeNode){ if (this.fireEvent("queryCollapse", [treeNode])){ treeNode.childrenNode.setStyle("display", "none"); } this.fireEvent("postCollapse", [treeNode]); }, toJson: function(item){ var json=null; var node = item.firstChild; json=[]; while (node){ json.push(this.transObj(node.options)); json[json.length-1].sub = this.toJson(node); node = node.nextSibling; } return json; }, transObj: function(options){ var obj = {}; obj[this.jsonMapping.expand] = options.expand; obj[this.jsonMapping.title] = options.title; obj[this.jsonMapping.text] = options.text; obj[this.jsonMapping.action] = options.action; obj[this.jsonMapping.style] = options.style; obj[this.jsonMapping.default] = options.default; obj[this.jsonMapping.icon] = (options.icon) ? options.icon : "none"; return obj; } }); o2.widget.Tree.Node = new Class({ Implements: [Options, Events], options: { "expand": true, "title": "", "text": "", "action": "", "style": "", "default" : false, "icon": "folder.png" }, imgs: { "expand": "expand.gif", "collapse":"collapse.gif", "blank": "blank.gif" }, tree: null, level: 0, levelNode:[], initialize: function(tree, options){ this.setOptions(options); if (options.icon=="none") this.options.icon = ""; this.tree = tree; this.levelNode = []; this.children = []; this.parentNode = null; this.previousSibling = null; this.nextSibling = null; this.firstChild = null; this.node = new Element("div",{ "styles": this.tree.css.treeNode }); this.itemNode = new Element("div", { "styles": this.tree.css.treeItemNode }).inject(this.node); this.childrenNode = new Element("div", { "styles": this.tree.css.treeChildrenNode }).inject(this.node); if (this.options.style){ if (this.tree.css[this.options.style]){ this.node.setStyles(this.tree.css[this.options.style].treeNode); this.itemNode.setStyles(this.tree.css[this.options.style].treeItemNode); this.childrenNode.setStyles(this.tree.css[this.options.style].treeChildrenNode); } } if (!this.options.expand){ this.childrenNode.setStyle("display", "none"); } }, setText: function(value){ var textDivNode = this.textNode.getElement("div"); if (textDivNode) textDivNode.set("text", value); }, setTitle: function(value){ var textDivNode = this.textNode.getElement("div"); if (textDivNode) textDivNode.set("title", value); }, load: function(){ this.tree.fireEvent("beforeLoadTreeNode", [this]); this.nodeTable = new Element("table", { "border": "0", "cellpadding": "0", "cellspacing": "0", "styles": {"width": "fit-content", "table-layout": "fixed"} }).inject(this.itemNode); this.nodeTable.setStyles(this.tree.css.nodeTable); if (this.options.style){ if (this.tree.css[this.options.style]){ this.nodeTable.setStyles(this.tree.css[this.options.style].nodeTable); } } var tbody = new Element("tbody").inject(this.nodeTable); this.nodeArea = new Element("tr").inject(tbody); this.createLevelNode(); this.createOperateNode(); this.createIconNode(); this.createTextNode(); this.tree.fireEvent("afterLoadTreeNode", [this]); }, createLevelNode: function(){ for (var i=0; i