📄 tree.js
字号:
/* * * Copyright (c) 2004-2005 by Zapatec, Inc. * http://www.zapatec.com * 1700 MLK Way, Berkeley, California, * 94709, U.S.A. * All rights reserved. * * */Zapatec.treePath = Zapatec.getPath("Zapatec.Tree");Zapatec.Tree=function(){var objArgs={};switch(arguments.length){case 1:objArgs=arguments[0];break;case 2:objArgs=arguments[1];objArgs.tree=arguments[0];break;}Zapatec.Tree.SUPERconstructor.call(this,objArgs);};Zapatec.Tree.id="Zapatec.Tree";Zapatec.Tree.all={};Zapatec.inherit(Zapatec.Tree,Zapatec.Widget);Zapatec.Tree.prototype.init=function(config){this.container=null;this.internalContainer=null;this.allNodes=[];this.id2Obj={};this.rootNode=null;this.expandToLevelNum=0;this.isClicked=false;this.isActive=false;this.editInline=null;this.isSaveStateDone=false;if(config.tree){var tmp=Zapatec.Widget.getElementById(config.tree);if(tmp&&tmp.id){this.id=tmp.id;}}Zapatec.Tree.SUPERclass.init.call(this,config);Zapatec.Tree.all[this.id]=this;this.container=Zapatec.Utils.createElement("div",null,this.config.selectable);this.container.className=this.getClassName({prefix:"zpTree",suffix:"Container"});this.container.id="zpTree"+this.id+"Container";this.internalContainer=Zapatec.Utils.createElement("div",null,this.config.selectable);this.internalContainer.className="tree tree-top";this.container.appendChild(this.internalContainer);Zapatec.Utils.createProperty(this.container,"zpTree",this);if(this.config.initLevel){this.expandToLevelNum=this.config.initLevel;}if(this.config.tree){this.config.tree.parentNode.insertBefore(this.container,this.config.tree);Zapatec.Utils.destroy(this.config.tree);}else if(this.config.parent){this.config.parent.appendChild(this.container);}this.rootNode=new Zapatec.Tree.Node({tree:this,parentNode:null,level:0,isRootNode:true,eventListeners:this.config.eventListeners});this.id2Obj[this.id]=this.rootNode;this.rootNode.isCreated=true;this.rootNode.childrenContainer=this.internalContainer;this.prevSelected=null;this.loadData();if(this.prevSelected){this.sync(this.prevSelected.id);}if(this.config.saveState){var txt=Zapatec.Utils.getCookie("Zapatec.Tree-"+this.config.saveId);var node=this.getNode(txt,true);if(node){this.isSaveStateDone=true;node.sync();}}if(this.config.editable){if(Zapatec.EditInline){var self=this;this.editInline=new Zapatec.EditInline({editAsText:this.config.editAsText,eventListeners:{showStart:function(){this.selectedNode=self.prevSelected;},saveContent:function(content){if(this.selectedNode){this.selectedNode.rename(content);this.selectedNode=null;}}}});}else{this.config.editable=false;Zapatec.Log({description:"Zapatec.EditInline class is not found. Please include 'utils/edit_inline.js' into page."});}}this.attachNavigation();};Zapatec.Tree.prototype.configure=function(objArgs){this.defineConfigOption('tree');this.defineConfigOption('parent');this.defineConfigOption('hiliteSelectedNode');this.defineConfigOption('highlightSelectedNode',true);this.defineConfigOption('defaultIcons');this.defineConfigOption('compact',false);this.defineConfigOption('dynamic',false);this.defineConfigOption('initLevel',1);this.defineConfigOption('deselectSelected',false);this.defineConfigOption('saveState',false);this.defineConfigOption('saveId');this.defineConfigOption('expandOnSignClick',true);this.defineConfigOption('expandOnSignDblclick',false);this.defineConfigOption('expandOnIconClick',true);this.defineConfigOption('expandOnIconDblclick',false);this.defineConfigOption('expandOnLabel');this.defineConfigOption('expandOnLabelClick',false);this.defineConfigOption('labelDblClick');this.defineConfigOption('expandOnLabelDblclick',false);this.defineConfigOption('selectMultiple',false);this.defineConfigOption('selectOnSignClick',true);this.defineConfigOption('selectOnSignDblclick',true);this.defineConfigOption('selectOnIconClick',true);this.defineConfigOption('selectOnIconDblclick',true);this.defineConfigOption('selectOnLabelClick',true);this.defineConfigOption('selectOnLabelDblclick',true);this.defineConfigOption('prevCompatible',false);this.defineConfigOption('quick',false);this.defineConfigOption('putBackReferences',false);this.defineConfigOption('createWholeDOM',false);this.defineConfigOption('jsonLoadCallback');this.defineConfigOption('keyboardNavigation',false);this.defineConfigOption('deselectOnLeave');this.defineConfigOption('putCheckboxes',false);this.defineConfigOption('dependantCheckboxes',true);this.defineConfigOption('selectable',false);this.defineConfigOption('editable',false);this.defineConfigOption('editAsText',true);this.defineConfigOption('editOnClick',false);this.defineConfigOption('editOnDblclick',true);this.defineConfigOption('disableContextMenu',false);Zapatec.Tree.SUPERclass.configure.call(this,objArgs);this.config.parent=Zapatec.Widget.getElementById(this.config.parent);this.config.tree=Zapatec.Widget.getElementById(this.config.tree);if(this.config.tree!=null){if(this.config.source==null){this.config.source=this.config.tree;}else{}}if(typeof(this.config.hiliteSelectedNode)!='undefined'&&this.config.hiliteSelectedNode!=null){this.config.highlightSelectedNode=this.config.hiliteSelectedNode;}if(this.config.parent==null&&this.config.tree==null){Zapatec.Log({description:"No 'parent' or 'tree' config options given. Unable to add tree."});throw("No 'parent' or 'tree' config options given. Unable to add tree.");}if(this.config.labelDblClick!=null){this.config.expandOnLabelDblclick=this.config.labelDblClick;}if(this.config.expandOnLabel!=null){this.config.expandOnLabelClick=this.config.expandOnLabel;}if(this.config.initLevel==false){this.config.initLevel=1;}if(this.config.initLevel<1){this.config.initLevel=1;}if(this.config.saveState&&(!this.config.saveId||typeof(this.config.saveId)!='string'||this.config.saveId.length==0)){Zapatec.Log({description:"No 'saveId' is given. 'saveState' feature disabled."});this.config.saveState=false;}if(this.config.createWholeDOM&&this.config.quick){this.config.quick=false;Zapatec.Log({description:"Config option 'createWholeDOM' overrides 'quick' config option"});}if(this.config.selectMultiple){this.config.keyboardNavigation=false;}if(this.config.keyboardNavigation&&typeof(this.config.deselectOnLeave)=='undefined'){this.config.deselectOnLeave=true;}if(!this.config.putCheckboxes){this.config.dependantCheckboxes=false;}};Zapatec.Tree.prototype.reconfigure=function(objArgs){if(objArgs.theme){Zapatec.Utils.removeClass(this.container,this.getClassName({prefix:"zpTree",suffix:"Container"}));}Zapatec.Tree.SUPERclass.reconfigure.call(this,objArgs);Zapatec.Utils.addClass(this.container,this.getClassName({prefix:"zpTree",suffix:"Container"}));};Zapatec.Tree.prototype.addStandardEventListeners=function(){Zapatec.Tree.SUPERclass.addStandardEventListeners.call(this);this.addEventListener('fetchSourceError',this.displayErrorSource);};Zapatec.Tree.prototype.displayErrorSource=function(oError){alert("The tree's data source, "+this.config.source+" does not contain valid data.\n"+oError.errorDescription);};Zapatec.Tree.prototype.find=function(findFunc){for(var ii=0;ii<this.allNodes.length;ii++){if(findFunc(this.allNodes[ii])){return this.allNodes[ii];}}};Zapatec.Tree.prototype.findAll=function(findFunc){var result=[];for(var ii=0;ii<this.allNodes.length;ii++){if(findFunc(this.allNodes[ii])){result.push(this.allNodes[ii]);}}return result;};Zapatec.Tree.prototype.toggleAll=function(){for(var ii=0;ii<this.allNodes.length;ii++){this.allNodes[ii].toggle();}};Zapatec.Tree.prototype.getNode=function(id,omitWarning){var node=this.id2Obj[id];if(node==null){if(!omitWarning){Zapatec.Log({description:"No node found for id '"+id+"'"});}return;}return node;};Zapatec.Tree.prototype.sync=function(itemId){var node=this.getNode(itemId);if(node){node.sync();}};Zapatec.Tree.prototype.toggleItem=function(nodeId,state){var node=this.getNode(nodeId);if(!node){return;}if(state==true){node.expand();}else if(state==false){node.collapse();}else if(state==null){node.toggle();}};Zapatec.Tree.prototype.appendChild=function(newChild,parent,atStart){if(this.config.prevCompatible){var tmp=parent;parent=newChild;newChild=tmp;}if(parent==null){parent=this.rootNode;}else{parent=this.getNode(parent);}if(parent==null){return null;}return parent.appendChild(newChild,atStart);};Zapatec.Tree.prototype.insertBefore=function(newChild,refChild){refChild=this.getNode(refChild);if(refChild==null){return null;}return refChild.insertBefore(newChild);};Zapatec.Tree.prototype.insertAfter=function(newChild,refChild){refChild=this.getNode(refChild);if(refChild==null){return null;}return refChild.insertAfter(newChild);};Zapatec.Tree.prototype.removeChild=function(oldChild){oldChild=this.getNode(oldChild);if(oldChild==null){return null;}oldChild.destroy();};Zapatec.Tree.prototype.collapseAll=function(){for(var ii=0;ii<this.allNodes.length;ii++){this.allNodes[ii].collapse();}};Zapatec.Tree.prototype.collapseToLevel=function(level){for(var ii=0;ii<this.allNodes.length;ii++){if(this.allNodes[ii].config.level>level){this.allNodes[ii].collapse();}}};Zapatec.Tree.prototype.expandAll=function(){for(var ii=0;ii<this.allNodes.length;ii++){this.allNodes[ii].expand();}};Zapatec.Tree.prototype.expandToLevel=function(level){this.expandToLevelNum=level;for(var ii=0;ii<this.allNodes.length;ii++){if(this.allNodes[ii].config.level<=level){this.allNodes[ii].expand();}}};Zapatec.Tree.prototype.loadDataJson=function(objResponse){if(objResponse==null){return null;}if(this.config.jsonLoadCallback){objResponse=this.config.jsonLoadCallback(objResponse);}this.rootNode.data={};this.rootNode.data.children=objResponse;this.rootNode.createChildren();for(var ii=0;ii<this.rootNode.children.length;ii++){this.rootNode.children[ii].afterCreate();}if(this.config.saveState&&!this.isSaveStateDone){var txt=Zapatec.Utils.getCookie("Zapatec.Tree-"+this.config.saveId);var node=this.getNode(txt,true);if(node){this.isSaveStateDone=true;node.sync();}}};Zapatec.Tree.prototype.loadDataXml=function(objSource){if(objSource==null||objSource.documentElement==null){return null;}var result=[];for(var jj=0;jj<objSource.documentElement.childNodes.length;jj++){var tmp=Zapatec.Tree.Utils.convertXml2Json(objSource.documentElement.childNodes[jj]);if(tmp!=null){result.push(tmp);}}return this.loadDataJson(result);};Zapatec.Tree.prototype.loadDataHtml=function(objSource){if(objSource==null){return null;}var result=[];for(var jj=0;jj<objSource.childNodes.length;jj++){var tmp=Zapatec.Tree.Utils.convertLi2Json(objSource.childNodes[jj],this.config.prevCompatible);if(tmp!=null){result.push(tmp);}}return this.loadDataJson(result);};Zapatec.Tree.prototype.makeNode=function(html,type){if(!type){type="li";}var node=Zapatec.Utils.createElement(type);if(html){Zapatec.Transport.setInnerHtml({html:html,container:node});}return node;};Zapatec.Tree.prototype.destroy=function(leaveDOM){this.rootNode.destroy(true);this.container.zpTree=null;Zapatec.Tree.all[this.id]=null;this.allNodes=null;this.rootNode=null;if(!leaveDOM){Zapatec.Utils.destroy(this.container);}this.container=null;this.id2Obj=null;this.discard();};Zapatec.Tree.prototype.onItemSelect=function(){};Zapatec.Tree.prototype.getState=function(){var result=[];for(var ii=0;ii<this.rootNode.children.length;ii++){result.push(this.rootNode.children[ii].getState());}return result;};Zapatec.Tree.prototype.getParent=function(id,mode){return id;};Zapatec.Tree.prototype.attachNavigation=function(){var self=this;Zapatec.Utils.addEvent(this.container,"click",function(){self.isActive=true;self.isClicked=true;});Zapatec.Utils.addEvent(document,(Zapatec.is_ie?"keydown":"keypress"),function(evt){return self.keyEvent(evt);});Zapatec.Utils.addEvent(document,'click',function(){if(!self.isClicked){self.leave();}self.isClicked=false;});};Zapatec.Tree.prototype.keyEvent=function(evt){if(!this.prevSelected||!this.isActive){return true;}if(!evt){evt=window.event;}if(!this.config.keyboardNavigation&&!this.config.editable){return;}var res=Zapatec.Utils.getCharFromEvent(evt);this.fireEvent("keypressed",res.charCode,res.chr);if(res.charCode==27){this.leave();}if(res.chr==" "){res.charCode=32;}if(this.config.keyboardNavigation&&(!this.config.editable||this.config.editable&&!this.editInline&&!this.editInline.selectedNode)){switch(res.charCode){case 32:if(this.config.putCheckboxes){this.prevSelected.checkboxChanged();}case 13:this.prevSelected.toggle();Zapatec.Utils.stopEvent(evt);break;case 63234:case 37:if(this.prevSelected.data.isExpanded){this.prevSelected.collapse();}else{if(!this.prevSelected.config.parentNode.isRootNode){this.prevSelected.config.parentNode.select();}}break;case 63235:case 39:if(!this.prevSelected.data.isExpanded){this.prevSelected.expand();}else{if(this.prevSelected.children!=null&&this.prevSelected.children.length>0){this.prevSelected.children[0].select();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -