wide/static/js/tree.js

134 lines
4.7 KiB
JavaScript
Raw Normal View History

2014-08-18 17:45:43 +04:00
var tree = {
2014-08-19 11:44:30 +04:00
fileTree: {},
2014-08-18 17:45:43 +04:00
newFile: function() {
$("#dirRMenu ul").hide();
var name = prompt("Name", "");
2014-08-19 11:44:30 +04:00
if (!name) {
return false;
}
2014-08-18 17:45:43 +04:00
var request = {
2014-08-19 11:44:30 +04:00
path: wide.curNode.path + '/' + name,
2014-08-18 17:45:43 +04:00
fileType: "f"
};
$.ajax({
type: 'POST',
url: '/file/new',
data: JSON.stringify(request),
dataType: "json",
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, [{
"name": name
}]);
2014-08-18 17:45:43 +04:00
}
});
},
newDir: function() {
$("#dirRMenu ul").hide();
var name = prompt("Name", "");
2014-08-19 11:44:30 +04:00
if (!name) {
return false;
}
2014-08-18 17:45:43 +04:00
var request = {
2014-08-19 11:44:30 +04:00
path: wide.curNode.path + '/' + name,
2014-08-18 17:45:43 +04:00
fileType: "d"
};
$.ajax({
type: 'POST',
url: '/file/new',
data: JSON.stringify(request),
dataType: "json",
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
// TODO: 换成我们风格的 class
tree.fileTree.addNodes(wide.curNode, [{
"name": name,
"iconSkin": "ico_close "
}]);
2014-08-18 17:45:43 +04:00
}
});
},
removeIt: function() {
$("#dirRMenu ul").hide();
$("#fileRMenu ul").hide();
2014-08-19 11:44:30 +04:00
2014-08-18 17:45:43 +04:00
if (!confirm("Remove it?")) {
return;
}
var request = {
2014-08-19 11:44:30 +04:00
path: wide.curNode.path
2014-08-18 17:45:43 +04:00
};
$.ajax({
type: 'POST',
url: '/file/remove',
data: JSON.stringify(request),
dataType: "json",
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.removeNode(wide.curNode);
2014-08-18 17:45:43 +04:00
}
});
},
init: function() {
$.ajax({
type: 'GET',
url: '/files',
dataType: "json",
success: function(data) {
if (data.succ) {
var dirRMenu = $("#dirRMenu");
var fileRMenu = $("#fileRMenu");
var setting = {
view: {
selectedMulti: false
},
callback: {
onRightClick: function(event, treeId, treeNode) {
if (treeNode) {
2014-08-19 11:44:30 +04:00
wide.curNode = treeNode;
2014-08-18 17:45:43 +04:00
if ("f" === treeNode.type) { // 如果右击了文件
$("#fileRMenu ul").show();
fileRMenu.css({"top": event.clientY + "px", "left": event.clientX + "px", "display": "block"});
} else { // 右击了目录
$("#dirRMenu ul").show();
dirRMenu.css({"top": event.clientY + "px", "left": event.clientX + "px", "display": "block"});
}
}
},
onClick: function(event, treeId, treeNode, clickFlag) {
2014-08-19 11:44:30 +04:00
wide.curNode = treeNode;
2014-08-18 17:45:43 +04:00
if ("f" === treeNode.type) { // 如果单击了文件
var request = {
path: treeNode.path
};
$.ajax({
type: 'POST',
url: '/file',
data: JSON.stringify(request),
dataType: "json",
success: function(data) {
if (data.succ) {
editor.setValue(data.content);
editor.setOption("mode", data.mode);
}
}
});
}
}
}
};
2014-08-19 11:44:30 +04:00
tree.fileTree = $.fn.zTree.init($("#files"), setting, data.root.children);
tree.fileTree.expandAll(true);
2014-08-18 17:45:43 +04:00
}
}
});
}
};