Reduce ztree tId usages

tId is transient, so we can't use it to identify a node, using path
instead of it.
This commit is contained in:
Liang Ding 2015-04-26 21:58:23 +08:00
parent 4a697eeff2
commit 22dcf590e7
9 changed files with 56 additions and 47 deletions

View File

@ -98,7 +98,8 @@ func GetFilesHandler(w http.ResponseWriter, r *http.Request) {
workspacePath := workspace + conf.PathSeparator + "src"
workspaceNode := Node{Name: workspace[strings.LastIndex(workspace, conf.PathSeparator)+1:],
Path: workspacePath, IconSkin: "ico-ztree-dir-workspace ", Type: "d",
Path: filepath.ToSlash(workspacePath), /* jQuery data-index API can't accept "\", so we convert it to "/" */
IconSkin: "ico-ztree-dir-workspace ", Type: "d",
Creatable: true, Removable: false, IsGoAPI: false, Children: []*Node{}}
walk(workspacePath, &workspaceNode, true, true, false)
@ -446,7 +447,9 @@ func walk(path string, node *Node, creatable, removable, isGOAPI bool) {
fio, _ := os.Lstat(fpath)
child := Node{Name: filename, Path: fpath, Removable: removable, IsGoAPI: isGOAPI, Children: []*Node{}}
child := Node{Name: filename,
Path: filepath.ToSlash(fpath), /* jQuery data-index API can't accept "\", so we convert it to "/" */
Removable: removable, IsGoAPI: isGOAPI, Children: []*Node{}}
node.Children = append(node.Children, &child)
if nil == fio {

View File

@ -25,7 +25,7 @@ var editors = {
}
},
close: function () {
$(".edit-panel .tabs > div[data-index=" + $(".edit-panel .frame").data("index") + "]").find(".ico-close").click();
$('.edit-panel .tabs > div[data-index="' + $('.edit-panel .frame').data('index') + ']').find('.ico-close').click();
},
closeOther: function () {
var currentIndex = $(".edit-panel .frame").data("index");
@ -43,14 +43,14 @@ var editors = {
var firstIndex = removeData.splice(0, 1);
$("#dialogCloseEditor").data("removeData", removeData);
// 开始关闭
$(".edit-panel .tabs > div[data-index=" + firstIndex + "]").find(".ico-close").click();
$('.edit-panel .tabs > div[data-index="' + firstIndex + '"]').find(".ico-close").click();
},
_removeAllMarker: function () {
var removeData = $("#dialogCloseEditor").data("removeData");
if (removeData && removeData.length > 0) {
var removeIndex = removeData.splice(0, 1);
$("#dialogCloseEditor").data("removeData", removeData);
$(".edit-panel .tabs > div[data-index=" + removeIndex + "] .ico-close").click();
$('.edit-panel .tabs > div[data-index="' + removeIndex + '"] .ico-close').click();
}
if (wide.curEditor) {
wide.curEditor.focus();
@ -97,7 +97,7 @@ var editors = {
"afterInit": function () {
$("#dialogCloseEditor button.save").click(function () {
var i = $("#dialogCloseEditor").data("index");
wide.fmt(tree.fileTree.getNodeByTId(editors.data[i].id).path, editors.data[i].editor);
wide.fmt(editors.data[i].id, editors.data[i].editor);
editors.tabs.del(editors.data[i].id);
$("#dialogCloseEditor").dialog("close");
editors._removeAllMarker();
@ -133,7 +133,8 @@ var editors = {
}
// set tree node selected
var node = tree.fileTree.getNodeByTId(id);
var tId = tree.getTIdByPath(id);
var node = tree.fileTree.getNodeByTId(tId);
tree.fileTree.selectNode(node);
wide.curNode = node;
@ -163,8 +164,8 @@ var editors = {
editors._removeAllMarker();
return true;
} else {
$("#dialogCloseEditor").dialog("open", $(".edit-panel .tabs > div[data-index="
+ editors.data[i].id + "] > span:eq(0)").text());
$("#dialogCloseEditor").dialog("open", $('.edit-panel .tabs > div[data-index="'
+ editors.data[i].id + '"] > span:eq(0)').text());
$("#dialogCloseEditor").data("index", i);
return false;
}
@ -219,7 +220,8 @@ var editors = {
}
// set tree node selected
var node = tree.fileTree.getNodeByTId(nextId);
var tId = tree.getTIdByPath(id);
var node = tree.fileTree.getNodeByTId(tId);
tree.fileTree.selectNode(node);
wide.curNode = node;
@ -743,7 +745,7 @@ var editors = {
},
// 新建一个编辑器 Tab如果已经存在 Tab 则切换到该 Tab.
newEditor: function (data, cursor) {
var id = wide.curNode.tId;
var id = wide.curNode.path;
editors.tabs.add({
id: id,

View File

@ -426,19 +426,19 @@ var hotkeys = {
|| document.activeElement.className === "search") {
// 焦点在底部窗口组时,对底部进行切换
var tabs = ["output", "search", "notification"],
nextId = "";
nextPath = "";
for (var i = 0, ii = tabs.length; i < ii; i++) {
if (document.activeElement.className === tabs[i]) {
if (i < ii - 1) {
nextId = tabs[i + 1];
nextPath = tabs[i + 1];
} else {
nextId = tabs[0];
nextPath = tabs[0];
}
break;
}
}
bottomGroup.tabs.setCurrent(nextId);
$(".bottom-window-group ." + nextId).focus();
bottomGroup.tabs.setCurrent(nextPath);
$(".bottom-window-group ." + nextPath).focus();
event.preventDefault();
@ -446,16 +446,16 @@ var hotkeys = {
}
if (editors.data.length > 1) {
var nextId = "";
var nextPath = "";
for (var i = 0, ii = editors.data.length; i < ii; i++) {
var currentId = editors.getCurrentId();
if (currentId) {
if (currentId === editors.data[i].id) {
if (i < ii - 1) {
nextId = editors.data[i + 1].id;
nextPath = editors.data[i + 1].id;
wide.curEditor = editors.data[i + 1].editor;
} else {
nextId = editors.data[0].id;
nextPath = editors.data[0].id;
wide.curEditor = editors.data[0].editor;
}
break;
@ -463,8 +463,10 @@ var hotkeys = {
}
}
editors.tabs.setCurrent(nextId);
wide.curNode = tree.fileTree.getNodeByTId(nextId);
editors.tabs.setCurrent(nextPath);
var nextTId = tree.getTIdByPath(nextPath);
wide.curNode = tree.fileTree.getNodeByTId(nextTId);
tree.fileTree.selectNode(wide.curNode);
wide.refreshOutline();
var cursor = wide.curEditor.getCursor();

View File

@ -122,7 +122,7 @@ var menu = {
return false;
}
for (var i = 0, ii = editors.data.length; i < ii; i++) {
var path = tree.fileTree.getNodeByTId(editors.data[i].id).path;
var path = editors.data[i].id;
var editor = editors.data[i].editor;
if ("text/x-go" === editor.getOption("mode")) {
@ -375,7 +375,7 @@ var menu = {
for (var i = 0, max = emptys.length; i < max; i++) {
var tabIndex = emptys[i].closest('div').data("index"),
text = $.trim(emptys[i].parent().text());
emptysTip += '[' + $("#dialogPreference .tabs > div[data-index=" + tabIndex + "]").text()
emptysTip += '[' + $('#dialogPreference .tabs > div[data-index="' + tabIndex + '"]').text()
+ '] -> [' + text.substr(0, text.length - 1)
+ ']: ' + config.label.no_empty + "<br/>";
}

View File

@ -93,7 +93,7 @@ var session = {
}
if (nodes[i].path === currentFile) {
id = nodes[i].tId;
id = nodes[i].path;
// FIXME: 上面的展开是异步进行的,所以执行到这里的时候可能还没有展开完,导致定位不了可视区域
tree.fileTree.selectNode(nodes[i]);

View File

@ -70,7 +70,7 @@ $.extend(Tabs.prototype, {
},
_hasId: function (id) {
var $tabs = this.obj._$tabs;
if ($tabs.find("div[data-index=" + id + "]").length === 0) {
if ($tabs.find('div[data-index="' + id + '"]').length === 0) {
return false;
}
return true;
@ -130,8 +130,8 @@ $.extend(Tabs.prototype, {
var $tabs = this.obj._$tabs;
return $tabs.children(".current").data("index");
},
setCurrent: function (id) {
if (!id) {
setCurrent: function (path) {
if (!path) {
return false;
}
@ -139,7 +139,7 @@ $.extend(Tabs.prototype, {
$tabs = this.obj._$tabs;
var $currentTab = $tabs.children(".current");
if ($currentTab.data("index") === id) {
if ($currentTab.data("index") === path) {
return false;
}
@ -148,15 +148,15 @@ $.extend(Tabs.prototype, {
if (stack.length === this.obj.STACKSIZE) {
stack.splice(0, 1);
}
if (stack[stack.length - 1] !== id) {
this.obj._stack.push(id);
if (stack[stack.length - 1] !== path) {
this.obj._stack.push(path);
}
$tabs.children("div").removeClass("current");
$tabsPanel.children("div").hide();
$tabs.children("div[data-index='" + id + "']").addClass("current");
$tabsPanel.children("div[data-index='" + id + "']").show();
$tabs.children('div[data-index="' + path + '"]').addClass("current");
$tabsPanel.children('div[data-index="' + path + '"]').show();
if (typeof this.obj.setAfter === 'function') {
this.obj.setAfter();

View File

@ -89,15 +89,16 @@ var tree = {
return tree.getAllParents(node.getParentNode(), parents);
}
},
isParents: function (tId, parentTId) {
isParents: function (tId, parentPath) {
var node = tree.fileTree.getNodeByTId(tId);
if (!node || !node.parentTId) {
return false;
} else {
if (node.parentTId === parentTId) {
var parentNode = tree.fileTree.getNodeByTId(node.parentTId);
if (node.path === parentPath) {
return true;
} else {
return tree.isParents(node.parentTId, parentTId);
return tree.isParents(parentNode.tId, parentPath);
}
}
},
@ -356,8 +357,8 @@ var tree = {
for (var i = 0, ii = editors.data.length; i < ii; i++) {
// 该节点文件已经打开
if (editors.data[i].id === treeNode.tId) {
editors.tabs.setCurrent(treeNode.tId);
if (editors.data[i].id === treeNode.path) {
editors.tabs.setCurrent(treeNode.path);
wide.curEditor = editors.data[i].editor;
if (!tempCursor) {
@ -411,6 +412,7 @@ var tree = {
if (!tempCursor) {
tempCursor = CodeMirror.Pos(0, 0);
}
editors.newEditor(data, tempCursor);
wide.refreshOutline();
@ -536,13 +538,13 @@ var tree = {
// update open editor tab name
for (var i = 0, ii = editors.data.length; i < ii; i++) {
if (wide.curNode.tId === editors.data[i].id) {
if (wide.curNode.path === editors.data[i].id) {
var mode = CodeMirror.findModeByExtension(suffix);
if (mode) {
editors.data[i].editor.setOption("mode", mode.mime);
}
var $currentSpan = $(".edit-panel .tabs > div[data-index=" + wide.curNode.tId + "] > span:eq(0)");
var $currentSpan = $('.edit-panel .tabs > div[data-index="' + wide.curNode.path + '"] > span:eq(0)');
$currentSpan.attr("title", request.newPath);
$currentSpan.html('<span class="' + iconSkin + 'ico"></span>' + wide.curNode.name);
break;

View File

@ -130,15 +130,15 @@ var wide = {
if (!tree.isDir()) {
// 是文件的话,查看 editor 中是否被打开,如打开则移除
for (var i = 0, ii = editors.data.length; i < ii; i++) {
if (editors.data[i].id === wide.curNode.tId) {
$(".edit-panel .tabs > div[data-index=" + wide.curNode.tId + "]").find(".ico-close").click();
if (editors.data[i].id === wide.curNode.path) {
$('.edit-panel .tabs > div[data-index="' + wide.curNode.path + '"]').find(".ico-close").click();
break;
}
}
} else {
for (var i = 0, ii = editors.data.length; i < ii; i++) {
if (tree.isParents(editors.data[i].id, wide.curNode.tId)) {
$(".edit-panel .tabs > div[data-index=" + editors.data[i].id + "]").find(".ico-close").click();
if (tree.isParents(editors.data[i].id, wide.curNode.path)) {
$('.edit-panel .tabs > div[data-index="' + editors.data[i].id + '"]').find(".ico-close").click();
i--;
ii--;
}

View File

@ -398,12 +398,12 @@
<div class="side">
<span title="{{.i18n.min}}" class="font-ico ico-min"></span>
<div class="tabs">
<div class="current" data-index="filreTree">
<div class="current" data-index="fileTree">
<span title="{{.i18n.file}}">{{.i18n.file}}</span>
</div>
</div>
<div class="tabs-panel">
<div data-index="filreTree">
<div data-index="fileTree">
<ul id="files" tabindex="-1" class="ztree"></ul>
<input id="importFileupload" class="fn-none" type="file"
name="files[]" onclick="tree.import();" multiple>
@ -502,7 +502,7 @@
</div>
</div>
<div class="tabs-panel">
<div id="outline" tabindex="-1" data-index="outline"></div>
<div id="outline" tabindex="-1" data-index="outline"></div>
</div>
</div>