wide/static/js/tree.js

292 lines
9.9 KiB
JavaScript
Raw Normal View History

2014-08-18 17:45:43 +04:00
var tree = {
// 递归获取当前节点展开中的最后一个节点
2014-09-17 06:04:41 +04:00
getCurrentNodeLastNode: function (node) {
var returnNode = node.children[node.children.length - 1];
if (returnNode.open) {
return tree.getCurrentNodeLastNode(returnNode);
} else {
return returnNode;
}
},
// 按照树展现获取下一个节点
2014-09-17 06:04:41 +04:00
getNextShowNode: function (node) {
if (node.level !== 0) {
if (node.getParentNode().getNextNode()) {
return node.getParentNode().getNextNode();
} else {
return tree.getNextShowNode(node.getParentNode());
}
} else {
return node.getNextNode();
}
},
2014-09-17 06:04:41 +04:00
isBottomNode: function (node) {
if (node.open) {
return false;
}
if (node.getParentNode()) {
if (node.getParentNode().isLastNode) {
return tree.isBottomNode(node.getParentNode());
} else {
return false;
}
} else {
if (node.isLastNode) {
return true;
} else {
return false;
}
}
},
2014-09-17 06:04:41 +04:00
getTIdByPath: function (path) {
var nodes = tree.fileTree.transformToArray(tree.fileTree.getNodes());
2014-09-13 09:05:50 +04:00
for (var i = 0, ii = nodes.length; i < ii; i++) {
if (nodes[i].path === path) {
return nodes[i].tId;
}
}
2014-09-13 09:05:50 +04:00
return undefined;
},
2014-09-03 13:49:03 +04:00
fileTree: undefined,
2014-09-17 06:04:41 +04:00
_isParents: function (tId, parentTId) {
2014-09-12 13:50:46 +04:00
var node = tree.fileTree.getNodeByTId(tId);
if (!node || !node.parentTId) {
return false;
} else {
if (node.parentTId === parentTId) {
return true;
} else {
return tree._isParents(node.parentTId, parentTId);
}
}
},
2014-09-17 06:04:41 +04:00
newFile: function () {
2014-09-12 13:50:46 +04:00
$("#dirRMenu").hide();
2014-08-18 17:45:43 +04:00
var name = prompt("Name", "");
2014-08-19 11:44:30 +04:00
if (!name) {
return false;
}
2014-09-17 06:04:41 +04:00
var request = newWideRequest();
request.path = wide.curNode.path + '\\' + name;
request.fileType = "f";
2014-08-18 17:45:43 +04:00
$.ajax({
type: 'POST',
url: '/file/new',
data: JSON.stringify(request),
dataType: "json",
2014-09-17 06:04:41 +04:00
success: function (data) {
2014-08-19 11:44:30 +04:00
if (!data.succ) {
return false;
2014-08-18 17:45:43 +04:00
}
2014-09-12 13:50:46 +04:00
var suffix = name.split(".")[1],
iconSkin = "ico-ztree-other ";
switch (suffix) {
case "html", "htm":
iconSkin = "ico-ztree-html ";
break;
case "go":
iconSkin = "ico-ztree-go ";
break;
case "css":
iconSkin = "ico-ztree-css ";
break;
case "txt":
iconSkin = "ico-ztree-text ";
break;
case "sql":
iconSkin = "ico-ztree-sql ";
break;
case "properties":
iconSkin = "ico-ztree-pro ";
break;
case "md":
iconSkin = "ico-ztree-md ";
break;
case "js", "json":
iconSkin = "ico-ztree-js ";
break;
case "xml":
iconSkin = "ico-ztree-xml ";
break;
case "jpg", "jpeg", "bmp", "gif", "png", "svg", "ico":
iconSkin = "ico-ztree-img ";
break;
}
2014-08-19 11:44:30 +04:00
tree.fileTree.addNodes(wide.curNode, [{
2014-09-12 13:50:46 +04:00
"name": name,
"iconSkin": iconSkin,
2014-09-20 07:54:33 +04:00
"path": request.path,
"mode": data.mode
2014-08-19 11:44:30 +04:00
}]);
2014-08-18 17:45:43 +04:00
}
});
},
2014-09-17 06:04:41 +04:00
newDir: function () {
2014-09-12 13:50:46 +04:00
$("#dirRMenu").hide();
2014-08-18 17:45:43 +04:00
var name = prompt("Name", "");
2014-08-19 11:44:30 +04:00
if (!name) {
return false;
}
2014-09-17 06:04:41 +04:00
var request = newWideRequest();
request.path = wide.curNode.path + '\\' + name;
request.fileType = "d";
2014-08-18 17:45:43 +04:00
$.ajax({
type: 'POST',
url: '/file/new',
data: JSON.stringify(request),
dataType: "json",
2014-09-17 06:04:41 +04:00
success: function (data) {
2014-08-19 11:44:30 +04:00
if (!data.succ) {
return false;
2014-08-18 17:45:43 +04:00
}
2014-08-19 11:44:30 +04:00
tree.fileTree.addNodes(wide.curNode, [{
2014-09-12 13:50:46 +04:00
"name": name,
"iconSkin": "ico-ztree-dir ",
"path": request.path
2014-08-19 11:44:30 +04:00
}]);
2014-08-18 17:45:43 +04:00
}
});
},
2014-09-17 06:04:41 +04:00
removeIt: function () {
2014-09-12 13:50:46 +04:00
$("#dirRMenu").hide();
$("#fileRMenu").hide();
2014-08-19 11:44:30 +04:00
2014-08-18 17:45:43 +04:00
if (!confirm("Remove it?")) {
return;
}
2014-09-17 06:04:41 +04:00
var request = newWideRequest();
request.path = wide.curNode.path;
2014-08-18 17:45:43 +04:00
$.ajax({
type: 'POST',
url: '/file/remove',
data: JSON.stringify(request),
dataType: "json",
2014-09-17 06:04:41 +04:00
success: function (data) {
2014-08-19 11:44:30 +04:00
if (!data.succ) {
return false;
2014-08-18 17:45:43 +04:00
}
2014-09-20 09:21:38 +04:00
tree.fileTree.removeNode(wide.curNode);
2014-09-12 13:50:46 +04:00
if ("ico-ztree-dir " !== wide.curNode.iconSkin) {
// 是文件的话,查看 editor 中是否被打开,如打开则移除
for (var i = 0, ii = editors.data.length; i < ii; i++) {
if (editors.data[i].id === wide.curNode.tId) {
$(".edit-header .tabs > div[data-index=" + wide.curNode.tId + "]").find(".ico-close").click();
break;
}
}
} else {
for (var i = 0, ii = editors.data.length; i < ii; i++) {
if (tree._isParents(editors.data[i].id, wide.curNode.tId)) {
$(".edit-header .tabs > div[data-index=" + editors.data[i].id + "]").find(".ico-close").click();
i--;
ii--;
}
}
}
2014-08-18 17:45:43 +04:00
}
});
},
2014-09-17 06:04:41 +04:00
init: function () {
var request = newWideRequest();
2014-08-18 17:45:43 +04:00
$.ajax({
2014-09-17 06:04:41 +04:00
type: 'POST',
2014-08-18 17:45:43 +04:00
url: '/files',
2014-09-17 06:04:41 +04:00
data: JSON.stringify(request),
2014-08-18 17:45:43 +04:00
dataType: "json",
2014-09-17 06:04:41 +04:00
success: function (data) {
2014-08-18 17:45:43 +04:00
if (data.succ) {
var dirRMenu = $("#dirRMenu");
var fileRMenu = $("#fileRMenu");
var setting = {
view: {
selectedMulti: false
},
callback: {
2014-09-17 06:04:41 +04:00
onRightClick: function (event, treeId, treeNode) {
2014-08-18 17:45:43 +04:00
if (treeNode) {
2014-08-19 11:44:30 +04:00
wide.curNode = treeNode;
2014-09-12 13:50:46 +04:00
if ("ico-ztree-dir " !== treeNode.iconSkin) { // 如果右击了文件
2014-08-18 17:45:43 +04:00
$("#fileRMenu ul").show();
2014-09-03 13:49:03 +04:00
fileRMenu.css({
"top": event.clientY - 10 + "px",
"left": event.clientX + "px",
"display": "block"
});
2014-08-18 17:45:43 +04:00
} else { // 右击了目录
$("#dirRMenu ul").show();
2014-09-03 13:49:03 +04:00
dirRMenu.css({
2014-09-12 13:50:46 +04:00
"top": event.clientY - 10 + "px",
"left": event.clientX + "px",
2014-09-03 13:49:03 +04:00
"display": "block"
});
2014-08-18 17:45:43 +04:00
}
}
},
2014-09-17 06:04:41 +04:00
onClick: function (event, treeId, treeNode, clickFlag) {
2014-09-03 13:49:03 +04:00
tree._onClick(treeNode);
}
}
};
tree.fileTree = $.fn.zTree.init($("#files"), setting, data.root.children);
}
}
});
},
2014-09-17 06:04:41 +04:00
_onClick: function (treeNode) {
2014-09-03 13:49:03 +04:00
if (wide.curNode) {
for (var i = 0, ii = editors.data.length; i < ii; i++) {
// 该节点文件已经打开
if (editors.data[i].id === treeNode.tId) {
editors.tabs.setCurrent(treeNode.tId);
wide.curNode = treeNode;
wide.curEditor = editors.data[i].editor;
2014-09-19 13:58:29 +04:00
wide.curEditor.focus();
2014-09-03 13:49:03 +04:00
return false;
}
}
}
2014-09-03 13:23:42 +04:00
2014-09-03 13:49:03 +04:00
wide.curNode = treeNode;
2014-09-03 13:23:42 +04:00
2014-09-12 13:50:46 +04:00
if ("ico-ztree-dir " !== treeNode.iconSkin) { // 如果单击了文件
2014-09-17 06:04:41 +04:00
var request = newWideRequest();
request.path = treeNode.path;
2014-09-03 13:49:03 +04:00
$.ajax({
type: 'POST',
url: '/file',
data: JSON.stringify(request),
dataType: "json",
2014-09-17 06:04:41 +04:00
success: function (data) {
2014-09-03 13:49:03 +04:00
if (!data.succ) {
alert(data.msg);
2014-09-02 14:09:01 +04:00
2014-09-03 13:49:03 +04:00
return false;
}
2014-09-02 14:09:01 +04:00
2014-09-03 13:49:03 +04:00
if ("img" === data.mode) { // 是图片文件的话新建 tab 打开
// 最好是开 tab但这个最终取决于浏览器设置
var w = window.open(data.path);
2014-09-02 14:09:01 +04:00
2014-09-03 13:49:03 +04:00
return false;
}
2014-09-02 14:09:01 +04:00
2014-09-03 13:49:03 +04:00
editors.newEditor(data);
2014-08-18 17:45:43 +04:00
}
2014-09-03 13:49:03 +04:00
});
}
2014-08-18 17:45:43 +04:00
}
};