wide/static/js/tree.js

357 lines
13 KiB
JavaScript
Raw Normal View History

2014-11-12 18:13:14 +03:00
/*
* Copyright (c) 2014, B3log
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2014-11-17 06:37:26 +03:00
*/
2014-11-12 18:13:14 +03:00
2014-08-18 17:45:43 +04:00
var tree = {
2014-09-25 07:04:17 +04:00
fileTree: undefined,
// 递归获取当前节点展开中的最后一个节点
2014-09-23 17:03:44 +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-23 17:03:44 +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-23 17:03:44 +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-23 17:03:44 +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-23 17:03:44 +04:00
getOpenPaths: function () {
var nodes = tree.fileTree.transformToArray(tree.fileTree.getNodes()),
paths = [];
for (var i = 0, ii = nodes.length; i < ii; i++) {
if (nodes[i].open) {
paths.push(nodes[i].path);
}
}
2014-09-23 18:29:53 +04:00
2014-09-23 17:03:44 +04:00
return paths;
},
2014-11-17 06:37:26 +03:00
getAllParents: function (node, parents) {
if (!parents) {
parents = [];
}
if (!node || !node.parentTId) {
return parents;
} else {
parents.push(node.getParentNode());
return tree.getAllParents(node.getParentNode(), parents);
}
},
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 {
2014-11-17 06:37:26 +03:00
return tree.isParents(node.parentTId, parentTId);
2014-09-12 13:50:46 +04:00
}
}
},
2014-11-06 10:47:05 +03:00
isDir: function () {
if (wide.curNode.iconSkin.indexOf("ico-ztree-dir") === 0) {
return true;
}
return false;
},
2014-11-06 11:51:59 +03:00
newFile: function (it) {
if ($(it).hasClass("disabled")) {
return false;
}
2014-09-12 13:50:46 +04:00
$("#dirRMenu").hide();
$("#dialogNewFilePrompt").dialog("open");
2014-08-18 17:45:43 +04:00
},
2014-11-06 16:46:20 +03:00
newDir: function (it) {
2014-11-06 11:51:59 +03:00
if ($(it).hasClass("disabled")) {
return false;
}
2014-09-12 13:50:46 +04:00
$("#dirRMenu").hide();
$("#dialogNewDirPrompt").dialog("open");
2014-08-18 17:45:43 +04:00
},
2014-11-06 10:47:05 +03:00
removeIt: function (it) {
if (it) {
if ($(it).hasClass("disabled")) {
return false;
}
} else {
// 直接调用时,如果为 api 及其子目录或者 workspace 则不能进行删除
2014-11-06 11:51:59 +03:00
if (!wide.curNode.removable) {
2014-11-06 10:47:05 +03:00
return false;
}
}
2014-09-12 13:50:46 +04:00
$("#dirRMenu").hide();
$("#fileRMenu").hide();
$("#dialogRemoveConfirm").dialog("open");
2014-08-18 17:45:43 +04:00
},
2014-11-07 18:11:37 +03:00
rename: function (it) {
if (it) {
if ($(it).hasClass("disabled")) {
return false;
}
}
$("#dirRMenu").hide();
$("#fileRMenu").hide();
$("#dialogRenamePrompt").dialog("open");
},
2014-09-23 17:03:44 +04:00
init: function () {
2014-11-06 10:04:33 +03:00
$("#file").click(function () {
$(this).focus();
});
2014-09-17 06:04:41 +04:00
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-23 17:03:44 +04:00
success: function (data) {
2014-08-18 17:45:43 +04:00
if (data.succ) {
var dirRMenu = $("#dirRMenu");
var fileRMenu = $("#fileRMenu");
var setting = {
2014-11-05 16:31:19 +03:00
data: {
key: {
2014-11-07 18:11:37 +03:00
title: "path"
2014-11-05 16:31:19 +03:00
}
},
2014-08-18 17:45:43 +04:00
view: {
2014-11-05 16:31:19 +03:00
showTitle: true,
2014-08-18 17:45:43 +04:00
selectedMulti: false
},
callback: {
onDblClick: function (event, treeId, treeNode) {
if (treeNode) {
2014-10-09 17:55:09 +04:00
tree.openFile(treeNode);
}
},
2014-09-23 17:03:44 +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;
tree.fileTree.selectNode(treeNode);
2014-11-06 10:47:05 +03:00
if (!tree.isDir()) { // 如果右击了文件
2014-11-06 11:51:59 +03:00
if (wide.curNode.removable) {
$("#fileRMenu .remove").removeClass("disabled");
} else {
$("#fileRMenu .remove").addClass("disabled");
}
2014-11-07 18:11:37 +03:00
2014-11-06 10:47:05 +03:00
$("#fileRMenu").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 { // 右击了目录
2014-11-06 11:51:59 +03:00
if (wide.curNode.removable) {
2014-11-07 18:11:37 +03:00
$("#dirRMenu .remove, #dirRMenu .rename").removeClass("disabled");
2014-11-06 11:51:59 +03:00
} else {
2014-11-07 18:11:37 +03:00
$("#dirRMenu .remove, #dirRMenu .rename").addClass("disabled");
2014-11-06 11:51:59 +03:00
}
if (wide.curNode.creatable) {
$("#dirRMenu .create").removeClass("disabled");
2014-11-06 10:47:05 +03:00
} else {
2014-11-06 11:51:59 +03:00
$("#dirRMenu .create").addClass("disabled");
2014-11-06 10:47:05 +03:00
}
$("#dirRMenu").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
}
$("#files").focus();
2014-08-18 17:45:43 +04:00
}
},
2014-09-23 17:03:44 +04:00
onClick: function (event, treeId, treeNode, clickFlag) {
if (treeNode) {
wide.curNode = treeNode;
tree.fileTree.selectNode(treeNode);
$("#files").focus();
}
2014-09-03 13:49:03 +04:00
}
}
};
tree.fileTree = $.fn.zTree.init($("#files"), setting, data.root.children);
2014-09-23 18:29:53 +04:00
session.restore();
2014-09-03 13:49:03 +04:00
}
}
});
2014-11-18 05:29:08 +03:00
2014-11-18 05:03:08 +03:00
this._initSearch();
2014-09-03 13:49:03 +04:00
},
2014-11-18 05:51:26 +03:00
openFile: function (treeNode, cursor) {
2014-09-25 07:35:28 +04:00
wide.curNode = treeNode;
2014-11-18 05:51:26 +03:00
var tempCursor = cursor;
2014-09-25 07:35:28 +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.curEditor = editors.data[i].editor;
2014-11-18 05:51:26 +03:00
if (!tempCursor) {
tempCursor = wide.curEditor.getCursor();
}
$(".footer .cursor").text('| ' + (tempCursor.line + 1) + ':' + (tempCursor.ch + 1) + ' |');
wide.curEditor.setCursor(tempCursor);
2014-11-18 05:29:08 +03:00
var half = Math.floor(wide.curEditor.getScrollInfo().clientHeight / wide.curEditor.defaultTextHeight() / 2);
2014-11-18 05:51:26 +03:00
var cursorCoords = wide.curEditor.cursorCoords({line: tempCursor.line - half, ch: 0}, "local");
2014-11-18 05:29:08 +03:00
wide.curEditor.scrollTo(0, cursorCoords.top);
wide.curEditor.focus();
2014-09-25 07:35:28 +04:00
return false;
2014-09-03 13:49:03 +04:00
}
}
2014-09-03 13:23:42 +04:00
2014-11-06 10:47:05 +03:00
if (!tree.isDir()) { // 如果单击了文件
2014-09-17 06:04:41 +04:00
var request = newWideRequest();
request.path = treeNode.path;
2014-09-03 13:49:03 +04:00
$.ajax({
2014-09-23 18:29:53 +04:00
async: false,
2014-09-03 13:49:03 +04:00
type: 'POST',
url: '/file',
data: JSON.stringify(request),
dataType: "json",
2014-09-23 17:03:44 +04:00
success: function (data) {
2014-09-03 13:49:03 +04:00
if (!data.succ) {
2014-09-24 07:35:03 +04:00
$("#dialogAlert").dialog("open", 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);
return false;
}
2014-11-18 05:51:26 +03:00
if (!tempCursor) {
tempCursor = CodeMirror.Pos(0, 0);
}
editors.newEditor(data, tempCursor);
2014-08-18 17:45:43 +04:00
}
2014-09-03 13:49:03 +04:00
});
}
2014-11-18 05:03:08 +03:00
},
_initSearch: function () {
2014-11-18 05:29:08 +03:00
$("#dialogSearchForm > input:eq(0)").keyup(function (event) {
2014-11-18 05:03:08 +03:00
var $okBtn = $(this).closest(".dialog-main").find(".dialog-footer > button:eq(0)");
if (event.which === 13 && !$okBtn.prop("disabled")) {
$okBtn.click();
}
if ($.trim($(this).val()) === "") {
$okBtn.prop("disabled", true);
} else {
$okBtn.prop("disabled", false);
}
});
$("#dialogSearchForm > input:eq(1)").keyup(function (event) {
var $okBtn = $(this).closest(".dialog-main").find(".dialog-footer > button:eq(0)");
if (event.which === 13 && !$okBtn.prop("disabled")) {
$okBtn.click();
}
});
$("#dialogSearchForm").dialog({
"modal": true,
"height": 62,
"width": 260,
"title": config.label.search,
"okText": config.label.search,
"cancelText": config.label.cancel,
"afterOpen": function () {
$("#dialogSearchForm > input:eq(0)").val('').focus();
$("#dialogSearchForm > input:eq(1)").val('');
$("#dialogSearchForm").closest(".dialog-main").find(".dialog-footer > button:eq(0)").prop("disabled", true);
},
"ok": function () {
var request = newWideRequest();
request.dir = wide.curNode.path;
request.text = $("#dialogSearchForm > input:eq(0)").val();
request.extension = $("#dialogSearchForm > input:eq(1)").val();
$.ajax({
type: 'POST',
url: '/file/search/text',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (!data.succ) {
return;
}
$("#dialogSearchForm").dialog("close");
editors.appendSearch(data.founds, 'founds', request.text);
}
});
}
});
2014-08-18 17:45:43 +04:00
}
};