wide/static/js/tree.js

144 lines
5.2 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) {
2014-08-19 13:07:35 +04:00
if (!data.succ) {
2014-09-02 14:09:01 +04:00
alert(data.msg);
return false;
}
if ("img" === data.mode) { // 是图片文件的话新建 tab 打开
// 最好是开 tab但这个最终取决于浏览器设置
var w = window.open(data.path);
2014-08-19 13:07:35 +04:00
return false;
2014-08-18 17:45:43 +04:00
}
2014-09-02 14:09:01 +04:00
2014-08-19 13:07:35 +04:00
editors.newEditor(data);
2014-08-18 17:45:43 +04:00
}
});
}
}
}
};
2014-08-19 11:44:30 +04:00
tree.fileTree = $.fn.zTree.init($("#files"), setting, data.root.children);
2014-08-31 10:07:35 +04:00
// tree.fileTree.expandAll(true);
2014-08-18 17:45:43 +04:00
}
}
});
}
};