📄 tree-node.js
字号:
} if(objResponse.fetchingIcon){ objResponse.fetchingIcon = Zapatec.Tree.Utils.addIconClass(objResponse.fetchingIcon, "fetchingIcon"); } if(objResponse.elementIcon){ objResponse.elementIcon = Zapatec.Tree.Utils.addIconClass(objResponse.elementIcon, "elementIcon"); } if( this.isCreated && ( !this.config.tree.config.quick || this.config.tree.config.quick && this.isChildrenCreated ) ){ if(objResponse.children){ for(var ii = 0; ii < objResponse.children.length; ii++){ this.appendChild(objResponse.children[ii]); } } } else { if(this.data == null){ // if loadData called for the first time. this.data = objResponse; } else { // or data is loaded dynamically if(objResponse.children){ this.data.children = this.data.children.concat(objResponse.children); } else { this.data.children = objResponse.children; } this.updateCheckbox(); // TODO change label, attributes, icons if needed } // if tree.config.quick option is false - create all childnodes if(!this.config.tree.config.quick || this.config.isRootNode){ this.initChildren(); } if(this.data.attributes && this.data.attributes.id){ this.config.tree.id2Obj[this.data.attributes.id] = this; } else { this.config.tree.id2Obj[this.id] = this; } }};/** * @private * Function to load tree content on tree load from XML source. * @param objResponse {Object} Server response */Zapatec.Tree.Node.prototype.loadDataXml = function(objSource){ if(objSource == null || objSource.documentElement == null){ return null; } if(objSource.documentElement.tagName.toLowerCase() == "list"){ var arr = []; for(var jj = 0; jj < objSource.documentElement.childNodes.length; jj++){ try{ var tmp = Zapatec.Tree.Utils.convertXml2Json(objSource.documentElement.childNodes[jj]); } catch(e){} if(tmp != null){ arr.push(tmp); } } this.loadDataJson({children: arr}); } else { this.loadDataJson(Zapatec.Tree.Utils.convertXml2Json(objSource.documentElement)); }};/** * @private * Function to load tree content on tree load from HTML source * @param objResponse {Object} Server response */Zapatec.Tree.Node.prototype.loadDataHtml = function(objSource){ if(objSource == null || !objSource.nodeName){ return null; } if(objSource.nodeName.toLowerCase() == 'ul'){ var arr = []; for(var ii = 0; ii < objSource.childNodes.length; ii++){ var tmp = Zapatec.Tree.Utils.convertLi2Json(objSource.childNodes[ii], this.config.tree.config.prevCompatible); if(tmp != null){ arr.push(tmp); } } this.loadDataJson({children: arr}); } else { this.loadDataJson(Zapatec.Tree.Utils.convertLi2Json(objSource, this.config.tree.config.prevCompatible)); }};/** * @private * Zapatec.Widget's method showContainer is unavailable for this class */Zapatec.Tree.Node.prototype.showContainer = function(){};/** * @private * Zapatec.Widget's method hideLoader is unavailable for this class */Zapatec.Tree.Node.prototype.hideContainer = function(){};/** * Expands all parent nodes for this node. */Zapatec.Tree.Node.prototype.expandHere = function(){ if(this.config.isRootNode){ return null; } var parentNodes = []; var parentNode = this.config.parentNode; while(parentNode != null){ parentNodes.push(parentNode); parentNode = parentNode.config.parentNode; } for(var ii = parentNodes.length - 1; ii >= 0; ii--){ parentNodes[ii].expand(); }};/** * Sync tree to this node. Tree will be expanded to this node and this node will be selected */Zapatec.Tree.Node.prototype.sync = function(){ if(this.config.isRootNode){ return null; } this.expandHere(); this.select();};/** * Destroy this node. * @param quick {boolean} If true - do not remove dependant DOM elements */Zapatec.Tree.Node.prototype.destroy = function(quick){ if(this.isCreated && !quick){ Zapatec.Utils.destroy(this.labelContainer); if(this.hasSubtree()){ Zapatec.Utils.destroy(this.childrenContainer); } } // remove children if(this.children){ for(var ii = this.children.length - 1; ii >= 0 ; ii--){ this.children[ii].destroy(true); } } if(this.config.isRootNode){ return; } // clean internal arrays var childIndex = null; var childrenArray = this.config.parentNode.children; for(var ii = 0; ii < childrenArray.length; ii++){ if(childrenArray[ii] == this){ childIndex = ii; break; } } if(childIndex == null){ // child is not found. impossible } else { if(childIndex != 0 && childrenArray[childIndex - 1].isCreated){ // if that was not first child - redraw lines for previous node childrenArray[childIndex - 1].putLines(); } if(childIndex != childrenArray.length - 1 && childrenArray[childIndex + 1].isCreated){ // if that was not last child - redraw lines for next node childrenArray[childIndex + 1].putLines(); } // removing current node from children array childrenArray = childrenArray.slice(0, childIndex).concat(childrenArray.slice(childIndex + 1)); this.config.parentNode.children = childrenArray; } // removing this node from Zapatec.Tree#allNodes array for(var ii = 0; ii < this.config.tree.allNodes.length; ii++){ if(this.config.tree.allNodes[ii] == this){ childIndex = ii; break; } } if(childIndex == null){ // impossible } else { this.config.tree.allNodes = this.config.tree.allNodes.slice(0, childIndex).concat(this.config.tree.allNodes.slice(childIndex + 1)); } // clean id2Obj hash if(this.data.attributes && this.data.attributes.id){ this.config.tree.id2Obj[this.data.attributes.id] = null; } else { this.config.tree.id2Obj[this.id] = null; } if(this.config.tree.prevSelected == this){ this.config.tree.prevSelected = null; } if(this.config.parentNode){ this.config.parentNode.updateCheckbox(); }};/** * Adds node to this node children. * @param newChild {Object} JSON array with data. If this is <LI> node - it will be converted to JSON. @see Zapatec.Tree.Utils.convertLi2Json * @param insertPosition {int} Where to insert node in this branch * @return reference to newly created Zapatec.Tree.Node instance * @type Object */Zapatec.Tree.Node.prototype.addNode = function(newChild, insertPosition){ if( newChild != null && newChild.nodeType && newChild.nodeType == 1 && newChild.nodeName.toLowerCase() == "li" ){ newChild = Zapatec.Tree.Utils.convertLi2Json(newChild, this.config.tree.config.prevCompatible); } if(newChild == null){ Zapatec.Log({description: "No data given!"}); return null; } var resultNode = newChild; this.data.children.splice(insertPosition, 0, newChild); if(this.isCreated || !this.config.tree.quick){ resultNode = new Zapatec.Tree.Node({ tree: this.config.tree, parentNode: this, level: this.config.level + 1, source: newChild, sourceType: "json", eventListeners: this.config.eventListeners }); if(this.isChildrenCreated){ var prevNode = null; var nextNode = null; var insertBeforeElement = null; if(insertPosition != 0){ prevNode = this.children[insertPosition - 1]; } if(insertPosition != this.children.length){ nextNode = this.children[insertPosition]; insertBeforeElement = nextNode.labelContainer; } var tmp = document.createElement("SPAN"); Zapatec.Transport.setInnerHtml({html: resultNode.create(), container: tmp}); var nodes = []; for(var ii = 0; ii < tmp.childNodes.length; ii++){ nodes.push(tmp.childNodes[ii]); } if(insertBeforeElement){ for(var ii = 0; ii < nodes.length; ii++){ this.childrenContainer.insertBefore(nodes[ii], insertBeforeElement); } } else { for(var ii = 0; ii < nodes.length; ii++){ this.childrenContainer.appendChild(nodes[ii]); } } resultNode.afterCreate(); if(prevNode){ prevNode.putLines(); } if(nextNode){ nextNode.putLines(); } } this.children.splice(insertPosition, 0, resultNode); } this.updateCheckbox(); if(this.config.parentNode){ this.config.parentNode.updateCheckbox(); } return resultNode;};/** * Appends child to the end or to the start of this node branch. * @param newChild {Object} JSON array with data. If this is <LI> node - it will be converted to JSON. @see Zapatec.Tree.Utils.convertLi2Json * @param atStart {boolean} If true - node will be added to the start of the branch. Otherwise - to the end. * @return reference to newly created Zapatec.Tree.Node instance * @type Object */Zapatec.Tree.Node.prototype.appendChild = function(newChild, atStart){ if(atStart){ return this.addNode(newChild, 0); } else { return this.addNode(newChild, this.children.length); }};/** * Appends new node before current node. * @param newChild {Object} JSON array with data. If this is <LI> node - it will be converted to JSON. @see Zapatec.Tree.Utils.convertLi2Json * @return reference to newly created Zapatec.Tree.Node instance * @type Object */Zapatec.Tree.Node.prototype.insertBefore = function(newChild){ for(var ii = this.config.parentNode.children.length - 1; ii >= 0; ii--){ if(this == this.config.parentNode.children[ii]){ return this.config.parentNode.addNode(newChild, ii); } }};/** * Appends new node after current node. * @param newChild {Object} JSON array with data. If this is <LI> node - it will be converted to JSON. @see Zapatec.Tree.Utils.convertLi2Json * @return reference to newly created Zapatec.Tree.Node instance * @type Object */Zapatec.Tree.Node.prototype.insertAfter = function(newChild){ for(var ii = this.config.parentNode.children.length - 1; ii >= 0; ii--){ if(this == this.config.parentNode.children[ii]){ return this.config.parentNode.addNode(newChild, ii + 1); } }};/** * Returns JSON with current node state. This object can be stored and used to * create node with completely same structure. * @return JSON with tree data. * @type Object */Zapatec.Tree.Node.prototype.getState = function(){ var result = { label: this.data.label, isSelected: this.data.isSelected, attributes: Zapatec.Utils.clone(this.data.attributes), isChecked: this.data.isChecked }; if(this.isCreated && !this.config.isRootNode){ result.label = this.getLinkToLabelElement().innerHTML; } if(this.hasSubtree()){ result.isExpanded = this.data.isExpanded; result.source = this.config.source; result.sourceType = this.config.sourceType; result.expandedIcon = this.data.expandedIcon; result.collapsedIcon = this.data.collapsedIcon; result.fetchingIcon = this.data.fetchingIcon; result.children = []; for(var ii = 0; ii < this.children.length; ii++){ result.children.push(this.children[ii].getState()); } } else { result.elementIcon = this.data.elementIcon; } return result;};/** * Rename current node. * @param {Object} newLabel New node content. May container HTML. */Zapatec.Tree.Node.prototype.rename = function(newLabel){ this.fireEvent("rename", this.data.label, newLabel); this.data.label = newLabel; var labelElement = this.getLinkToLabelElement(); Zapatec.Transport.setInnerHtml({html: newLabel, container: labelElement});};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -