add ztree to drag and sort notebooks

This commit is contained in:
iiuazz
2014-09-10 22:44:43 +08:00
parent bfcf8ec547
commit 2bed5b31fa
23 changed files with 465 additions and 17 deletions

View File

@ -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))
}

View File

@ -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]

View File

@ -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
}

View File

@ -24,6 +24,7 @@ document.write(files);
</script>
<link href="css/font-awesome-4.0.3/css/font-awesome.css" rel="stylesheet" />
<link href="css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
<!-- For Develop writting mod -->
@ -233,7 +234,7 @@ function log(o) {
<i class="fa fa-plus" title="{{msg . "addNotebook"}}"></i>
</div>
</div>
<ul class="folderBody" id="notebookList">
<ul class="folderBody ztree" id="notebookList">
</ul>
</div>
@ -922,6 +923,7 @@ function log(o) {
</div>
</div>
<script src="js/jquery-1.9.0.min.js"></script>
<script src="js/jquery.ztree.all-3.5.min.js"></script>
<script src="js/i18n/msg.{{.locale}}.js"></script>
<script src="js/common.js"></script>
<script>
@ -978,11 +980,13 @@ initSlimScroll();
<script src="/public/mdeditor/editor/underscore-min.js"></script>
<script src="/public/mdeditor/editor/scrollLink.js"></script>
<!--mathjax-->
<!--
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [['$','$'], ["\\(","\\)"]], processEscapes: true }, messageStyle: "none"});
</script>
<script src="/public/mdeditor/editor/mathJax-min.js"></script>
<script src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
-->
<script src="/public/mdeditor/editor/jquery.waitforimages-min.js"></script>
<script src="/public/mdeditor/editor/google-code-prettify/prettify.js"></script>
<script src="/public/mdeditor/editor/editor.js"></script>