123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- 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<this.level; i++){
- var td = new Element("td",{
- "styles": this.tree.css.blankLevelNode
- }).inject(this.nodeArea);
- if (this.options.style){
- if (this.tree.css[this.options.style]){
- td.setStyles(this.tree.css[this.options.style].blankLevelNode);
- }
- }
- this.levelNode.push(td);
- }
- },
- createOperateNode: function(){
- this.operateNode = new Element("td",{
- "styles": this.tree.css.operateNode
- }).inject(this.nodeArea);
- if (this.options.style){
- if (this.tree.css[this.options.style]){
- this.operateNode.setStyles(this.tree.css[this.options.style].operateNode);
- }
- }
- this.operateNode.addEvent("click", function(){
- this.expandOrCollapse();
- }.bind(this));
- this.operateNode.setStyle("background", "url("+this.tree.path+this.tree.options.style+"/"+this.imgs.blank+") center center no-repeat");
- //var img = new Element("img", {;
- // "src": this.tree.path+this.tree.options.style+"/"+this.imgs.blank,
- // "width": this.operateNode.getStyle("width"),
- // "height": this.operateNode.getStyle("height"),
- // "border": "0",
- // "styles": {
- // //"margin-top": "6px"
- // }
- //}).inject(this.operateNode);
-
- },
- createIconNode: function(){
- if (this.options.icon){
- this.iconNode = new Element("td",{
- "styles": this.tree.css.iconNode
- }).inject(this.nodeArea);
- if (this.options.style){
- if (this.tree.css[this.options.style]){
- this.iconNode.setStyles(this.tree.css[this.options.style].iconNode);
- }
- }
- if( this.options.icon.indexOf("/") !==-1 ){
- var value = this.options.icon;
- ["x_processplatform_assemble_surface","x_portal_assemble_surface","x_cms_assemble_control"].each(function( serviceRoot ){
- if (value.indexOf("/"+serviceRoot)!==-1){
- value = value.replace("/"+serviceRoot, MWF.Actions.getHost(serviceRoot)+"/"+serviceRoot);
- }else if (value.indexOf(serviceRoot)!==-1){
- value = value.replace(serviceRoot, MWF.Actions.getHost(serviceRoot)+"/"+serviceRoot);
- }
- })
- value = o2.filterUrl(value);
- this.iconNode.setStyle("background", "url("+value+") center center no-repeat");
- }else{
- this.iconNode.setStyle("background", "url("+this.tree.path+this.tree.options.style+"/"+this.options.icon+") center center no-repeat");
- }
- }
- },
- createTextNode: function(){
- this.textNode = new Element("td",{
- "styles": this.tree.css.textNode
- }).inject(this.nodeArea);
- if (this.options.style){
- if (this.tree.css[this.options.style]){
- this.textNode.setStyles(this.tree.css[this.options.style].textNode);
- }
- }
- // var width = this.tree.container.getSize().x - (this.level*20+40);
- // this.textNode.setStyle("width", ""+width+"px");
- var textDivNode = new Element("div", {
- "styles": {"display": "inline-block"},
- // "html": this.options.text,
- "title": this.options.title
- });
- textDivNode.setStyles(this.tree.css.textDivNode);
- if (this.options.style){
- if (this.tree.css[this.options.style]){
- textDivNode.setStyles(this.tree.css[this.options.style].textDivNode);
- }
- }
- if (this.tree.options.text=="html"){
- textDivNode.set("html", this.options.text);
- }else{
- textDivNode.set("text", this.options.text);
- }
-
- textDivNode.addEvent("click", function(e){
- this.clickNode(e);
- }.bind(this));
-
- textDivNode.inject(this.textNode);
- if( this.options.default ){
- textDivNode.click();
- }
- },
- clickNode: function(e){
- this.selectNode(e);
- this.doAction(e);
- },
- unselectNode: function(){
- this.fireEvent("unselect");
- var textDivNode = this.textNode.getElement("div");
- textDivNode.setStyles(this.tree.css.textDivNode);
- if (this.options.style){
- if (this.tree.css[this.options.style]){
- textDivNode.setStyles(this.tree.css[this.options.style].textDivNode);
- }
- }
- },
- selectNode: function(){
- this.tree.fireEvent("beforeSelect", [this]);
- if (this.tree.currentNode){
- this.tree.currentNode.unselectNode();
- // this.tree.currentNode.fireEvent("unselect");
- // var textDivNode = this.tree.currentNode.textNode.getElement("div");
- // textDivNode.setStyles(this.tree.css.textDivNode);
- // if (this.tree.currentNode.options.style){
- // if (this.tree.css[this.tree.currentNode.options.style]){
- // textDivNode.setStyles(this.tree.css[this.tree.currentNode.options.style].textDivNode);
- // }
- // }
- }
- var textDivNode = this.textNode.getElement("div");
- textDivNode.setStyles(this.tree.css.textDivNodeSelected);
- if (this.options.style){
- if (this.tree.css[this.options.style]){
- textDivNode.setStyles(this.tree.css[this.options.style].textDivNodeSelected);
- }
- }
- this.tree.currentNode = this;
- this.tree.fireEvent("afterSelect", [this]);
- },
- doAction: function(e){
- var t = typeOf(this.options.action);
- if (t=="string"){
- Browser.exec(this.options.action);
- }else if(t=="function"){
- this.options.action.apply(this, [this]);
- }else if(t=="object"){
- if (this.tree.form){
- this.tree.form.Macro.exec(this.options.action.code, this)
- }
- //this.options.action.apply(this, [this]);
- }
- },
- setOperateIcon: function(){
- var imgStr = (this.options.expand) ? this.imgs.expand : this.imgs.collapse;
- imgStr = this.tree.path+this.tree.options.style+"/"+imgStr;
- if (!this.firstChild) imgStr = this.tree.path+this.tree.options.style+"/"+this.imgs.blank;
- this.operateNode.setStyle("background", "url("+imgStr+") center center no-repeat");
- //var img = this.operateNode.getElement("img");
- //if (!img){
- // img = new Element("img", {
- // "src": imgStr,
- // "width": this.operateNode.getStyle("width"),
- // "height": this.operateNode.getStyle("height"),
- // "border": "0"
- // }).inject(this.operateNode);
- //}else{
- // img.set("src", imgStr);
- //}
- },
- insertChild: function(obj){
- var treeNode = new this.tree.$constructor.Node(this.tree, obj);
-
- var tmpTreeNode = this.previousSibling;
- this.previousSibling = treeNode;
- treeNode.nextSibling = this;
- treeNode.previousSibling = tmpTreeNode;
- if (tmpTreeNode){
- tmpTreeNode.nextSibling = treeNode;
- }else{
- this.parentNode.firstChild = treeNode;
- }
-
- treeNode.parentNode = this.parentNode;
- treeNode.level = this.level;
-
- treeNode.load();
- treeNode.node.inject(this.node, "before");
- this.parentNode.children.push(treeNode);
-
- return treeNode;
- },
- appendChild: function(obj, json){
- var treeNode = new this.tree.$constructor.Node(this.tree, obj);
- treeNode.json = json;
- if (this.children.length){
- treeNode.previousSibling = this.children[this.children.length-1];
- treeNode.previousSibling.nextSibling = treeNode;
- }else{
- this.firstChild = treeNode;
- this.setOperateIcon();
- }
-
- treeNode.level = this.level+1;
- treeNode.parentNode = this;
-
- treeNode.load();
- treeNode.node.inject(this.childrenNode);
- this.children.push(treeNode);
-
- return treeNode;
- },
-
- expandOrCollapse: function(){
- this.tree.expandOrCollapseNode(this);
- },
- destroy: function(){
- if (this.previousSibling) this.previousSibling.nextSibling = this.nextSibling;
- if (this.nextSibling) this.nextSibling.previousSibling = this.previousSibling;
- if (this.parentNode){
- if (this.parentNode.firstChild==this){
- this.parentNode.firstChild = this.nextSibling;
- }
- this.parentNode.children.erase(this);
- }
- this.node.destroy();
- delete this;
- },
- getPath: function () {
- var node = this;
- var text = node.options.text;
- while(node.parentNode){
- text = node.parentNode.options.text + "/" + text;
- node = node.parentNode;
- }
- return text;
- }
- });
|