From 2bed5b31fa7048553552f4c390f1c1dcab36dc21 Mon Sep 17 00:00:00 2001 From: iiuazz Date: Wed, 10 Sep 2014 22:44:43 +0800 Subject: [PATCH] add ztree to drag and sort notebooks --- app/controllers/NotebookController.go | 20 ++- app/info/NotebookInfo.go | 4 +- app/service/NotebookService.go | 50 +++++- app/views/Note/note-dev.html | 6 +- public/css/zTreeStyle/img/diy/1_close.png | Bin 0 -> 601 bytes public/css/zTreeStyle/img/diy/1_open.png | Bin 0 -> 580 bytes public/css/zTreeStyle/img/diy/2.png | Bin 0 -> 570 bytes public/css/zTreeStyle/img/diy/3.png | Bin 0 -> 762 bytes public/css/zTreeStyle/img/diy/4.png | Bin 0 -> 399 bytes public/css/zTreeStyle/img/diy/5.png | Bin 0 -> 710 bytes public/css/zTreeStyle/img/diy/6.png | Bin 0 -> 432 bytes public/css/zTreeStyle/img/diy/7.png | Bin 0 -> 534 bytes public/css/zTreeStyle/img/diy/8.png | Bin 0 -> 529 bytes public/css/zTreeStyle/img/diy/9.png | Bin 0 -> 467 bytes .../zTreeStyle/img/left_menuForOutLook.gif | Bin 0 -> 216 bytes .../zTreeStyle/img/left_menuForOutLook.png | Bin 0 -> 421 bytes public/css/zTreeStyle/img/line_conn.gif | Bin 0 -> 45 bytes public/css/zTreeStyle/img/loading.gif | Bin 0 -> 381 bytes public/css/zTreeStyle/img/zTreeStandard.gif | Bin 0 -> 5564 bytes public/css/zTreeStyle/img/zTreeStandard.png | Bin 0 -> 11173 bytes public/css/zTreeStyle/zTreeStyle.css | 126 ++++++++++++++ public/js/app/notebook.js | 116 ++++++++++++- public/js/jquery.ztree.all-3.5.min.js | 160 ++++++++++++++++++ 23 files changed, 465 insertions(+), 17 deletions(-) create mode 100644 public/css/zTreeStyle/img/diy/1_close.png create mode 100644 public/css/zTreeStyle/img/diy/1_open.png create mode 100644 public/css/zTreeStyle/img/diy/2.png create mode 100644 public/css/zTreeStyle/img/diy/3.png create mode 100644 public/css/zTreeStyle/img/diy/4.png create mode 100644 public/css/zTreeStyle/img/diy/5.png create mode 100644 public/css/zTreeStyle/img/diy/6.png create mode 100644 public/css/zTreeStyle/img/diy/7.png create mode 100644 public/css/zTreeStyle/img/diy/8.png create mode 100644 public/css/zTreeStyle/img/diy/9.png create mode 100644 public/css/zTreeStyle/img/left_menuForOutLook.gif create mode 100644 public/css/zTreeStyle/img/left_menuForOutLook.png create mode 100644 public/css/zTreeStyle/img/line_conn.gif create mode 100644 public/css/zTreeStyle/img/loading.gif create mode 100644 public/css/zTreeStyle/img/zTreeStandard.gif create mode 100644 public/css/zTreeStyle/img/zTreeStandard.png create mode 100644 public/css/zTreeStyle/zTreeStyle.css create mode 100644 public/js/jquery.ztree.all-3.5.min.js diff --git a/app/controllers/NotebookController.go b/app/controllers/NotebookController.go index 56db4f4..e4ce3c3 100644 --- a/app/controllers/NotebookController.go +++ b/app/controllers/NotebookController.go @@ -2,10 +2,10 @@ package controllers import ( "github.com/revel/revel" -// "encoding/json" + "encoding/json" "github.com/leanote/leanote/app/info" "gopkg.in/mgo.v2/bson" -// . "github.com/leanote/leanote/app/lea" + . "github.com/leanote/leanote/app/lea" // "io/ioutil" ) @@ -46,7 +46,23 @@ func (c Notebook) AddNotebook(notebookId, title string) revel.Result { func (c Notebook) UpdateNotebookTitle(notebookId, title string) revel.Result { return c.RenderJson(notebookService.UpdateNotebookTitle(notebookId, c.GetUserId(), title)) } + // 排序 +// 无用 func (c Notebook) SortNotebooks(notebookId2Seqs map[string]int) revel.Result { return c.RenderJson(notebookService.SortNotebooks(c.GetUserId(), notebookId2Seqs)) +} + +// 调整notebooks, 可能是排序, 可能是移动到其它笔记本下 +type DragNotebooksInfo struct { + CurNotebookId string + ParentNotebookId string + Siblings []string +} +// 传过来的data是JSON.stringfy数据 +func (c Notebook) DragNotebooks(data string) revel.Result { + info := DragNotebooksInfo{} + json.Unmarshal([]byte(data), &info) + + return c.RenderJson(notebookService.DragNotebooks(c.GetUserId(), info.CurNotebookId, info.ParentNotebookId, info.Siblings)) } \ No newline at end of file diff --git a/app/info/NotebookInfo.go b/app/info/NotebookInfo.go index ee1f256..8275c9f 100644 --- a/app/info/NotebookInfo.go +++ b/app/info/NotebookInfo.go @@ -21,7 +21,7 @@ type Notebook struct { } // 仅仅是为了返回前台 -type SubNotebooks []Notebooks +type SubNotebooks []*Notebooks // 存地址, 为了生成tree type Notebooks struct { Notebook Subs SubNotebooks // 子notebook 在数据库中是没有的 @@ -32,7 +32,7 @@ func (this SubNotebooks) Len() int { return len(this) } func (this SubNotebooks) Less(i, j int) bool { - return this[i].Seq < this[j].Seq + return (*this[i]).Seq < (*this[j]).Seq } func (this SubNotebooks) Swap(i, j int) { this[i], this[j] = this[j], this[i] diff --git a/app/service/NotebookService.go b/app/service/NotebookService.go index 4bb794a..d83cf45 100644 --- a/app/service/NotebookService.go +++ b/app/service/NotebookService.go @@ -47,31 +47,40 @@ func ParseAndSortNotebooks(userNotebooks []info.Notebook, noParentDelete, needSo newNotebooks.NumberNotes = each.NumberNotes newNotebooks.IsTrash = each.IsTrash newNotebooks.IsBlog = each.IsBlog + + // 存地址 userNotebooksMap[each.NotebookId] = &newNotebooks } // 第二遍, 追加到父下 - // nilObjectId := bson.ObjectIdHex("") + + // 需要删除的id + needDeleteNotebookId := map[bson.ObjectId]bool{} for id, each := range userNotebooksMap { // 如果有父, 那么追加到父下, 并剪掉当前, 那么最后就只有根的元素 if each.ParentNotebookId.Hex() != "" { if userNotebooksMap[each.ParentNotebookId] != nil { - userNotebooksMap[each.ParentNotebookId].Subs = append(userNotebooksMap[each.ParentNotebookId].Subs, *each) + userNotebooksMap[each.ParentNotebookId].Subs = append(userNotebooksMap[each.ParentNotebookId].Subs, each) // Subs是存地址 // 并剪掉 - delete(userNotebooksMap, id) + // bug + needDeleteNotebookId[id] = true + // delete(userNotebooksMap, id) } else if noParentDelete { // 没有父, 且设置了要删除 - delete(userNotebooksMap, id) + needDeleteNotebookId[id] = true + // delete(userNotebooksMap, id) } } } // 第三遍, 得到所有根 - final := make(info.SubNotebooks, len(userNotebooksMap)) + final := make(info.SubNotebooks, len(userNotebooksMap)-len(needDeleteNotebookId)) i := 0 - for _, each := range userNotebooksMap { - final[i] = *each - i++ + for id, each := range userNotebooksMap { + if !needDeleteNotebookId[id] { + final[i] = each + i++ + } } // 最后排序 @@ -207,5 +216,30 @@ func (this *NotebookService) SortNotebooks(userId string, notebookId2Seqs map[st } } + return true +} + +func (this *NotebookService) DragNotebooks(userId string, curNotebookId string, parentNotebookId string, siblings []string) bool { + userIdO := bson.ObjectIdHex(userId) + + ok := false + // 如果没parentNotebookId, 则parentNotebookId设空 + if(parentNotebookId == "") { + ok = db.UpdateByIdAndUserIdField2(db.Notebooks, bson.ObjectIdHex(curNotebookId), userIdO, "ParentNotebookId", ""); + } else { + ok = db.UpdateByIdAndUserIdField2(db.Notebooks, bson.ObjectIdHex(curNotebookId), userIdO, "ParentNotebookId", bson.ObjectIdHex(parentNotebookId)); + } + + if !ok { + return false + } + + // 排序 + for seq, notebookId := range siblings { + if !db.UpdateByIdAndUserIdField2(db.Notebooks, bson.ObjectIdHex(notebookId), userIdO, "Seq", seq) { + return false + } + } + return true } \ No newline at end of file diff --git a/app/views/Note/note-dev.html b/app/views/Note/note-dev.html index b490b9c..ccbd372 100644 --- a/app/views/Note/note-dev.html +++ b/app/views/Note/note-dev.html @@ -24,6 +24,7 @@ document.write(files); + @@ -233,7 +234,7 @@ function log(o) { -