From 6381e16287b7db8b2daea203ba04ceb45cf23873 Mon Sep 17 00:00:00 2001 From: Van Date: Tue, 18 Nov 2014 10:51:26 +0800 Subject: [PATCH] . --- static/js/editors.js | 42 ++++++++++-------------------------------- static/js/tree.js | 24 +++++++++++++++--------- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/static/js/editors.js b/static/js/editors.js index 1872def..73cf1a0 100644 --- a/static/js/editors.js +++ b/static/js/editors.js @@ -442,34 +442,11 @@ var editors = { return; } - var cursorLine = data.cursorLine; - var cursorCh = data.cursorCh; + var tId = tree.getTIdByPath(data.path); + wide.curNode = tree.fileTree.getNodeByTId(tId); + tree.fileTree.selectNode(wide.curNode); - var request = newWideRequest(); - request.path = data.path; - - // TODO: refactor - $.ajax({ - type: 'POST', - url: '/file', - data: JSON.stringify(request), - dataType: "json", - success: function (data) { - if (!data.succ) { - $("#dialogAlert").dialog("open", data.msg); - - return false; - } - - var tId = tree.getTIdByPath(data.path); - wide.curNode = tree.fileTree.getNodeByTId(tId); - tree.fileTree.selectNode(wide.curNode); - - data.cursorLine = cursorLine; - data.cursorCh = cursorCh; - tree.openFile(wide.curNode); - } - }); + tree.openFile(wide.curNode, CodeMirror.Pos(data.cursorLine - 1, data.cursorCh - 1)); } }); }; @@ -574,7 +551,7 @@ var editors = { $(".bottom-window-group .search").focus(); }, // 新建一个编辑器 Tab,如果已经存在 Tab 则切换到该 Tab. - newEditor: function (data) { + newEditor: function (data, cursor) { $(".toolbars").show(); var id = wide.curNode.tId; @@ -805,11 +782,12 @@ var editors = { "id": id }); - var cursor = CodeMirror.Pos(0, 0); - if (data.cursorLine && data.cursorCh) { - cursor = CodeMirror.Pos(data.cursorLine - 1, data.cursorCh - 1); - } $(".footer .cursor").text('| ' + (cursor.line + 1) + ':' + (cursor.ch + 1) + ' |'); + + var half = Math.floor(wide.curEditor.getScrollInfo().clientHeight / wide.curEditor.defaultTextHeight() / 2); + var cursorCoords = wide.curEditor.cursorCoords({line: cursor.line - half, ch: 0}, "local"); + wide.curEditor.scrollTo(0, cursorCoords.top); + editor.setCursor(cursor); editor.focus(); } diff --git a/static/js/tree.js b/static/js/tree.js index bd43b98..159ef0b 100644 --- a/static/js/tree.js +++ b/static/js/tree.js @@ -242,21 +242,24 @@ var tree = { this._initSearch(); }, - openFile: function (treeNode) { + openFile: function (treeNode, cursor) { wide.curNode = treeNode; + var tempCursor = cursor; 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; - - var cursor = wide.curEditor.getCursor(); - $(".footer .cursor").text('| ' + (cursor.line + 1) + ':' + (cursor.ch + 1) + ' |'); - - wide.curEditor.setCursor(cursor); + + if (!tempCursor) { + tempCursor = wide.curEditor.getCursor(); + } + $(".footer .cursor").text('| ' + (tempCursor.line + 1) + ':' + (tempCursor.ch + 1) + ' |'); + + wide.curEditor.setCursor(tempCursor); var half = Math.floor(wide.curEditor.getScrollInfo().clientHeight / wide.curEditor.defaultTextHeight() / 2); - var cursorCoords = wide.curEditor.cursorCoords({line: cursor.line - half, ch: 0}, "local"); + var cursorCoords = wide.curEditor.cursorCoords({line: tempCursor.line - half, ch: 0}, "local"); wide.curEditor.scrollTo(0, cursorCoords.top); wide.curEditor.focus(); @@ -286,8 +289,11 @@ var tree = { var w = window.open(data.path); return false; } - - editors.newEditor(data); + + if (!tempCursor) { + tempCursor = CodeMirror.Pos(0, 0); + } + editors.newEditor(data, tempCursor); } }); }