📄 dynamictreebuilder.js
字号:
// +---------------------------------------------------------------+
// | DO NOT REMOVE THIS |
// +---------------------------------------------------------------+
// | DynamicTree 1.5.3 |
// | Author: Cezary Tomczak [www.gosu.pl] |
// | Free for any use as long as all copyright messages are intact |
// +---------------------------------------------------------------+
function DynamicTreeBuilder(id) {
this.path = "images/";
this.img = {
"branch": "tree-branch.gif",
"doc": "tree-doc.gif",
"folder": "tree-folder.gif",
"folderOpen": "tree-folder-open.gif",
"leaf": "tree-leaf.gif",
"leafEnd": "tree-leaf-end.gif",
"node": "tree-node.gif",
"nodeEnd": "tree-node-end.gif",
"nodeOpen": "tree-node-open.gif",
"nodeOpenEnd": "tree-node-open-end.gif" };
this.cookiePath = "";
this.cookieDomain = "";
this.init = function() {
var p, img;
for (p in this.img) {
this.img[p] = this.path + this.img[p];
}
for (p in this.img) {
this.imgObjects.push(new Image());
this.imgObjects.getLast().src = this.img[p];
this.img[p] = this.imgObjects.getLast().src;
}
this.parse(document.getElementById(this.id).childNodes, this.tree);
this.loadState();
if (window.addEventListener) { window.addEventListener("unload", function(e) { self.saveState(); }, false); }
else if (window.attachEvent) { window.attachEvent("onunload", function(e) { self.saveState(); }); }
this.updateHtml();
};
this.reset = function() {
this.clearState();
this.tree = new Node("tree", "", null, new Array(), false, true);
this.allNodes = {};
this.opened = [];
this.active = "";
this.count = 0;
this.parse(document.getElementById(this.id).childNodes, this.tree);
this.updateHtml();
};
this.parse = function(nodes, tree) {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].nodeType == 1) {
if (!nodes[i].className) { continue; }
nodes[i].id = this.id + "-" + (++this.count);
var node = new Node();
node.id = nodes[i].id;
if (nodes[i].firstChild) {
// whitespace characters before Anchor, check for: doc, folder, test on all browsers
//if (nodes[i].className == "doc" && nodes[i].firstChild.nodeType != 1) { nodes[i].removeChild(nodes[i].firstChild); }
if (nodes[i].firstChild.tagName == "A") {
var a = nodes[i].firstChild;
if (a.firstChild) {
node.text = a.firstChild.nodeValue.trim();
}
if (a.href) {
// dirty hack for ie (automatic conversion to absolute paths problem), see also DynamicTreePlugins.importFromHtml()
var s = a.parentNode.innerHTML.match(/href=["'](dynamictree:\/\/dynamictree\/)?([^"']*)["']/i);
if (s) { node.href = s[2]; }
}
if (a.title) {
node.title = a.title;
}
if (a.target) {
node.target = a.target;
}
} else {
node.text = nodes[i].firstChild.nodeValue.trim();
}
}
node.parentNode = tree;
node.childNodes = (nodes[i].className == "folder" ? new Array() : null);
node.isDoc = (nodes[i].className == "doc");
node.isFolder = (nodes[i].className == "folder");
tree.childNodes.push(node);
this.allNodes[node.id] = node;
}
if (nodes[i].nodeType == 1 && nodes[i].childNodes) {
this.parse(nodes[i].childNodes, tree.childNodes.getLast());
}
}
};
this.nodeClick = function(id) {
var el = document.getElementById(id+"-section");
var node = document.getElementById(id+"-node");
var icon = document.getElementById(id+"-icon");
if (el.style.display == "block") {
el.style.display = "none";
if (this.allNodes[id].isLast()) { node.src = this.img.nodeEnd; }
else { node.src = this.img.node; }
icon.src = this.img.folder;
this.opened.removeByValue(id);
} else {
el.style.display = "block";
if (this.allNodes[id].isLast()) { node.src = this.img.nodeOpenEnd; }
else { node.src = this.img.nodeOpen; }
icon.src = this.img.folderOpen;
this.opened.push(id);
}
/* fix ie bug - images not showing */
if (node.outerHTML) { node.outerHTML = node.outerHTML; }
if (icon.outerHTML) { icon.outerHTML = icon.outerHTML; }
};
this.textClick = function(id) {
if (this.active) {
document.getElementById(this.active+"-text").className = "text";
}
document.getElementById(id+"-text").className = "text-active";
this.active = id;
this.textClickListener.call();
};
this.toHtml = function() {
var s = "";
var nodes = this.tree.childNodes;
for (var i = 0; i < nodes.length; i++) {
s += nodes[i].toHtml();
}
return s;
};
this.updateHtml = function() {
document.getElementById(this.id).innerHTML = this.toHtml();
};
this.loadState = function() {
var opened = this.cookie.get("opened");
if (opened) {
this.opened = opened.split("|");
this.opened.filter(function(id) { return self.allNodes[id] && self.allNodes[id].isFolder && self.allNodes[id].childNodes.length; });
}
};
this.saveState = function() {
if (this.opened.length) {
this.cookie.set("opened", this.opened.join("|"), 3600*24*30, this.cookiePath, this.cookieDomain);
} else {
this.clearState();
}
};
this.clearState = function() {
this.cookie.del("opened");
};
this.getActiveNode = function() {
if (!this.active) { throw "DynamicTreeBuilder.getActiveNode() failed, there is no active node"; }
return this.allNodes[this.active];
}
this.mayMoveUp = function() {
return this.active && !this.allNodes[this.active].isFirst();
};
this.mayMoveDown = function() {
return this.active && !this.allNodes[this.active].isLast();
};
this.mayMoveLeft = function() {
return this.active && (this.allNodes[this.active].getLevel() > 1);
};
this.mayMoveRight = function() {
if (this.active) {
var node = this.allNodes[this.active].getNextSibling();
while (node) {
if (node.isFolder) { return true; }
node = node.getNextSibling();
}
}
return false;
};
this.mayInsertBefore = function() {
return Boolean(this.active);
};
this.mayInsertAfter = function() {
return Boolean(this.active);
};
this.mayInsertInside = function() {
return this.active && this.allNodes[this.active].isFolder;
};
this.mayRemove = function() {
if (this.active) {
var node = this.allNodes[this.active];
if (node.isDoc) { return true; }
if (node.isFolder && !node.childNodes.length) { return true; }
}
return false;
};
this.moveUp = function() {
var node = this.allNodes[this.active];
var index = node.getIndex();
var parent = node.parentNode;
parent.removeChild(node);
parent.appendChildAtIndex(node, index-1);
this.updateHtml();
};
this.moveDown = function() {
var node = this.allNodes[this.active];
var index = node.getIndex();
var parent = node.parentNode;
parent.removeChild(node);
parent.appendChildAtIndex(node, index+1);
this.updateHtml();
};
this.moveLeft = function() {
var node = this.allNodes[this.active];
var left = node.parentNode;
left.removeChild(node);
left.parentNode.appendChildAtIndex(node, left.getIndex());
this.updateHtml();
};
this.moveRight = function() {
var node = this.allNodes[this.active];
var next = node.getNextSibling();
var rightId = null;
while (next) {
if (next.isFolder) {
rightId = next.id;
break;
}
next = next.getNextSibling();
}
var right = this.allNodes[rightId];
node.parentNode.removeChild(node);
if (right.childNodes.length) {
right.appendChildAtIndex(node, 0);
} else {
right.appendChild(node);
}
this.updateHtml();
};
this.createNode = function(id, text, type, object) {
if (!id || this.allNodes[id] || !text || (type != "doc" && type != "folder")) {
throw this.id+'.createNode("'+id+'", "'+text+'", "'+type+'") failed, illegal action';
}
var node;
if (type == "doc") {
node = new Node(id, text, null, null, true, false);
} else {
node = new Node(id, text, null, new Array(), false, true);
}
if (object) {
for (var p in object) {
node[p] = object[p];
}
}
this.allNodes[id] = node;
return node;
};
this.insert = function(id, text, type, object) {
var node = this.createNode(id, text, type, object);
if (this.tree.childNodes.length) {
this.tree.appendChildAtIndex(node, 0);
} else {
this.tree.appendChild(node);
}
this.updateHtml();
};
this.insertBefore = function(id, text, type, object) {
if (!this.mayInsertBefore()) {
throw this.id+'.insertBefore() failed, illegal action';
}
var node = this.createNode(id, text, type, object);
var active = this.allNodes[this.active];
active.parentNode.appendChildAtIndex(node, active.getIndex());
this.updateHtml();
};
this.insertAfter = function(id, text, type, object) {
if (!this.mayInsertAfter()) {
throw this.id+'.insertAfter() failed, illegal action';
}
var node = this.createNode(id, text, type, object);
var active = this.allNodes[this.active];
if (active.parentNode.childNodes[active.getIndex()+1]) {
active.parentNode.appendChildAtIndex(node, active.getIndex()+1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -