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" workspacePath := workspace + conf.PathSeparator + "src"
workspaceNode := Node{Name: workspace[strings.LastIndex(workspace, conf.PathSeparator)+1:], 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{}} Creatable: true, Removable: false, IsGoAPI: false, Children: []*Node{}}
walk(workspacePath, &workspaceNode, true, true, false) walk(workspacePath, &workspaceNode, true, true, false)
@ -446,7 +447,9 @@ func walk(path string, node *Node, creatable, removable, isGOAPI bool) {
fio, _ := os.Lstat(fpath) 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) node.Children = append(node.Children, &child)
if nil == fio { if nil == fio {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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