树添加、删除文件异步更新

This commit is contained in:
Van 2014-08-19 15:44:30 +08:00
parent 238980d0fe
commit 1c18861f26
1 changed files with 32 additions and 13 deletions

View File

@ -1,9 +1,14 @@
var tree = { var tree = {
fileTree: {},
newFile: function() { newFile: function() {
$("#dirRMenu ul").hide(); $("#dirRMenu ul").hide();
var name = prompt("Name", ""); var name = prompt("Name", "");
if (!name) {
return false;
}
var request = { var request = {
path: wide.curFile + '/' + name, path: wide.curNode.path + '/' + name,
fileType: "f" fileType: "f"
}; };
$.ajax({ $.ajax({
@ -12,17 +17,24 @@ var tree = {
data: JSON.stringify(request), data: JSON.stringify(request),
dataType: "json", dataType: "json",
success: function(data) { success: function(data) {
if (data.succ) { if (!data.succ) {
console.log(data); return false;
} }
tree.fileTree.addNodes(wide.curNode, [{
"name": name
}]);
} }
}); });
}, },
newDir: function() { newDir: function() {
$("#dirRMenu ul").hide(); $("#dirRMenu ul").hide();
var name = prompt("Name", ""); var name = prompt("Name", "");
if (!name) {
return false;
}
var request = { var request = {
path: wide.curFile + '/' + name, path: wide.curNode.path + '/' + name,
fileType: "d" fileType: "d"
}; };
$.ajax({ $.ajax({
@ -31,20 +43,26 @@ var tree = {
data: JSON.stringify(request), data: JSON.stringify(request),
dataType: "json", dataType: "json",
success: function(data) { success: function(data) {
if (data.succ) { if (!data.succ) {
console.log(data); return false;
} }
// TODO: 换成我们风格的 class
tree.fileTree.addNodes(wide.curNode, [{
"name": name,
"iconSkin": "ico_close "
}]);
} }
}); });
}, },
removeIt: function() { removeIt: function() {
$("#dirRMenu ul").hide(); $("#dirRMenu ul").hide();
$("#fileRMenu ul").hide(); $("#fileRMenu ul").hide();
if (!confirm("Remove it?")) { if (!confirm("Remove it?")) {
return; return;
} }
var request = { var request = {
path: wide.curFile + '/' + name path: wide.curNode.path
}; };
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
@ -52,9 +70,10 @@ var tree = {
data: JSON.stringify(request), data: JSON.stringify(request),
dataType: "json", dataType: "json",
success: function(data) { success: function(data) {
if (data.succ) { if (!data.succ) {
console.log(data); return false;
} }
tree.fileTree.removeNode(wide.curNode);
} }
}); });
}, },
@ -74,7 +93,7 @@ var tree = {
callback: { callback: {
onRightClick: function(event, treeId, treeNode) { onRightClick: function(event, treeId, treeNode) {
if (treeNode) { if (treeNode) {
wide.curFile = treeNode.path; wide.curNode = treeNode;
if ("f" === treeNode.type) { // 如果右击了文件 if ("f" === treeNode.type) { // 如果右击了文件
$("#fileRMenu ul").show(); $("#fileRMenu ul").show();
fileRMenu.css({"top": event.clientY + "px", "left": event.clientX + "px", "display": "block"}); fileRMenu.css({"top": event.clientY + "px", "left": event.clientX + "px", "display": "block"});
@ -85,7 +104,7 @@ var tree = {
} }
}, },
onClick: function(event, treeId, treeNode, clickFlag) { onClick: function(event, treeId, treeNode, clickFlag) {
wide.curFile = treeNode.path; wide.curNode = treeNode;
if ("f" === treeNode.type) { // 如果单击了文件 if ("f" === treeNode.type) { // 如果单击了文件
var request = { var request = {
path: treeNode.path path: treeNode.path
@ -106,8 +125,8 @@ var tree = {
} }
} }
}; };
fileTree = $.fn.zTree.init($("#files"), setting, data.root.children); tree.fileTree = $.fn.zTree.init($("#files"), setting, data.root.children);
fileTree.expandAll(true); tree.fileTree.expandAll(true);
} }
} }
}); });