wide/static/js/tree.js

174 lines
5.6 KiB
JavaScript
Raw Normal View History

2014-08-18 17:45:43 +04:00
var tree = {
2014-09-03 13:49:03 +04:00
fileTree: undefined,
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();
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({
"top": event.clientY - 10 + "px",
"left": event.clientX + "px",
"display": "block"
});
2014-08-18 17:45:43 +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);
}
}
});
},
_onClick: function(treeNode) {
if (wide.curNode) {
var id = wide.curNode.tId;
if (id === treeNode.tId) {
// 再次点击当前选中节点
return false;
}
2014-09-03 13:23:42 +04:00
2014-09-03 13:49:03 +04:00
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;
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-03 13:49:03 +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) {
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
}
};