From 0b929599b8676967516af81b7b760234b777f66b Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 23 Sep 2014 22:29:53 +0800 Subject: [PATCH] Fix #11 --- conf/wide.go | 12 +++++ conf/wide.json | 6 ++- .../admin/src/mytest/hello/2.json | 1 + .../admin/src/mytest/time/1.json | 0 .../admin/src/mytest/time/3.json | 1 + main.go | 23 ++++++---- session/sessions.go | 2 +- static/js/session.js | 46 ++++++++++++++++++- static/js/tree.js | 9 ++-- view/index.html | 1 + 10 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 data/user_workspaces/admin/src/mytest/hello/2.json create mode 100644 data/user_workspaces/admin/src/mytest/time/1.json create mode 100644 data/user_workspaces/admin/src/mytest/time/3.json diff --git a/conf/wide.go b/conf/wide.go index 017a9b0..9706283 100644 --- a/conf/wide.go +++ b/conf/wide.go @@ -106,6 +106,7 @@ func FixedTimeSave() { } // 获取 username 指定的用户的工作空间路径. +// 查找不到时返回空字符串. func (*conf) GetUserWorkspace(username string) string { for _, user := range Wide.Users { if user.Name == username { @@ -117,6 +118,17 @@ func (*conf) GetUserWorkspace(username string) string { return "" } +// 获取 username 指定的用户配置. +func (*conf) GetUser(username string) *User { + for _, user := range Wide.Users { + if user.Name == username { + return user + } + } + + return nil +} + // 获取 gocode 路径. func (*conf) GetGocode() string { return getGOBIN() + "gocode" diff --git a/conf/wide.json b/conf/wide.json index 1b0f0d3..11b4694 100644 --- a/conf/wide.json +++ b/conf/wide.json @@ -15,7 +15,11 @@ "Password": "admin", "Workspace": "{pwd}/data/user_workspaces/admin", "LatestSessionContent": { - "FileTree": [], + "FileTree": [ + "D:\\GoGoGo\\src\\github.com\\b3log\\wide\\data\\user_workspaces\\admin\\src\\mytest", + "D:\\GoGoGo\\src\\github.com\\b3log\\wide\\data\\user_workspaces\\admin\\src\\mytest\\hello", + "D:\\GoGoGo\\src\\github.com\\b3log\\wide\\data\\user_workspaces\\admin\\src\\mytest\\time" + ], "Files": [], "CurrentFile": "" } diff --git a/data/user_workspaces/admin/src/mytest/hello/2.json b/data/user_workspaces/admin/src/mytest/hello/2.json new file mode 100644 index 0000000..34d9a8b --- /dev/null +++ b/data/user_workspaces/admin/src/mytest/hello/2.json @@ -0,0 +1 @@ +2222222222222222 \ No newline at end of file diff --git a/data/user_workspaces/admin/src/mytest/time/1.json b/data/user_workspaces/admin/src/mytest/time/1.json new file mode 100644 index 0000000..e69de29 diff --git a/data/user_workspaces/admin/src/mytest/time/3.json b/data/user_workspaces/admin/src/mytest/time/3.json new file mode 100644 index 0000000..979169c --- /dev/null +++ b/data/user_workspaces/admin/src/mytest/time/3.json @@ -0,0 +1 @@ +3.3333333333333334e+50 \ No newline at end of file diff --git a/main.go b/main.go index 8217283..6173a34 100644 --- a/main.go +++ b/main.go @@ -74,7 +74,11 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": &succ} defer util.RetJSON(w, r, data) - var args map[string]interface{} + args := struct { + Username string + Password string + }{} + if err := json.NewDecoder(r.Body).Decode(&args); err != nil { glog.Error(err) succ = true @@ -82,11 +86,8 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { return } - username := args["username"].(string) - password := args["password"].(string) - for _, user := range conf.Wide.Users { - if user.Name == username && user.Password == password { + if user.Name == args.Username && user.Password == args.Password { succ = true } } @@ -97,12 +98,12 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { // 创建 HTTP 会话 httpSession, _ := session.HTTPSession.Get(r, "wide-session") - httpSession.Values["username"] = username + httpSession.Values["username"] = args.Username httpSession.Values["id"] = strconv.Itoa(rand.Int()) httpSession.Options.MaxAge = 60 * 60 * 24 // 一天过期 httpSession.Save(r, w) - glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), username) + glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), args.Username) } // Wide 首页. @@ -122,12 +123,14 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { // 创建一个 Wide 会话 wideSession := session.WideSessions.New(httpSession) - model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r), - "session": wideSession} - wideSessions := session.WideSessions.GetByHTTPSession(httpSession) username := httpSession.Values["username"].(string) + userConf := conf.Wide.GetUser(username) + + model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r), + "session": wideSession, "latestSessionContent": userConf.LatestSessionContent} + glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions)) t, err := template.ParseFiles("view/index.html") diff --git a/session/sessions.go b/session/sessions.go index 0a12d65..3788ea3 100644 --- a/session/sessions.go +++ b/session/sessions.go @@ -63,7 +63,7 @@ var WideSessions Sessions // 排它锁,防止并发修改. var mutex sync.Mutex -// 在一些特殊情况(例如浏览器不间断刷新时会话通道建立并发)下 Wide 会话集内会出现无效会话,该函数定时(1 小时)检查并移除这些无效会话. +// 在一些特殊情况(例如浏览器不间断刷新/在源代码视图刷新)下 Wide 会话集内会出现无效会话,该函数定时(1 小时)检查并移除这些无效会话. // 无效会话:在检查时间内 30 分钟都没有使用过的会话,WideSession.Updated 字段. func FixedTimeRelease() { go func() { diff --git a/static/js/session.js b/static/js/session.js index e0892b2..da8c9be 100644 --- a/static/js/session.js +++ b/static/js/session.js @@ -17,7 +17,7 @@ var session = { filse.push($it.find("span:eq(0)").attr("title")); }); - + fileTree = tree.getOpenPaths(); request.currentFile = currentFile; // 当前编辑器 @@ -34,6 +34,50 @@ var session = { }); }, 5000); }, + restore: function () { + var fileTree = config.latestSessionContent.FileTree, + files = config.latestSessionContent.Files, + currentFile = config.latestSessionContent.CurrentFile, + id = "", + nodesToOpen = []; + + + var nodes = tree.fileTree.transformToArray(tree.fileTree.getNodes()); + + for (var i = 0, ii = nodes.length; i < ii; i++) { + // expand tree + for (var j = 0, jj = fileTree.length; j < jj; j++) { + if (nodes[i].path === fileTree[j]) { + tree.fileTree.expandNode(nodes[i], true, false, false); + break; + } + } + + // open editors + for (var k = 0, kk = files.length; k < kk; k++) { + if (nodes[i].path === files[k]) { + nodesToOpen.push(nodes[i]); + break; + } + } + + if (nodes[i].path === currentFile) { + id = nodes[i].tId; + } + } + + // 处理编辑器打开顺序 + for (var m = 0, mm = files.length; m < mm; m++) { + for (var n = 0, nn = nodesToOpen.length; n < nn; n++) { + if (nodesToOpen[n].path === files[m]) { + tree._onClick(nodesToOpen[n]); + break; + } + } + } + + editors.tabs.setCurrent(id); + }, _initWS: function () { // 用于保持会话,如果该通道断开,则服务器端会销毁会话状态,回收相关资源. var sessionWS = new WebSocket(config.channel.session + '/session/ws?sid=' + config.wideSessionId); diff --git a/static/js/tree.js b/static/js/tree.js index 66f237a..e48ec39 100644 --- a/static/js/tree.js +++ b/static/js/tree.js @@ -57,7 +57,7 @@ var tree = { paths.push(nodes[i].path); } } - + return paths; }, fileTree: undefined, @@ -129,6 +129,8 @@ var tree = { } }; tree.fileTree = $.fn.zTree.init($("#files"), setting, data.root.children); + + session.restore(); } } }); @@ -147,13 +149,12 @@ var tree = { } } - wide.curNode = treeNode; - if ("ico-ztree-dir " !== treeNode.iconSkin) { // 如果单击了文件 var request = newWideRequest(); request.path = treeNode.path; $.ajax({ + async: false, type: 'POST', url: '/file', data: JSON.stringify(request), @@ -165,6 +166,8 @@ var tree = { return false; } + wide.curNode = treeNode; + if ("img" === data.mode) { // 是图片文件的话新建 tab 打开 // 最好是开 tab,但这个最终取决于浏览器设置 var w = window.open(data.path); diff --git a/view/index.html b/view/index.html index a1463b2..a91e4e7 100644 --- a/view/index.html +++ b/view/index.html @@ -181,6 +181,7 @@