forked from TruthHun/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_tree.go
More file actions
155 lines (132 loc) · 4.04 KB
/
document_tree.go
File metadata and controls
155 lines (132 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package models
import (
"bytes"
"html/template"
"strconv"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
type DocumentTree struct {
DocumentId int `json:"id"`
DocumentName string `json:"text"`
ParentId interface{} `json:"parent"`
Identify string `json:"identify"`
BookIdentify string `json:"-"`
Version int64 `json:"version"`
State *DocumentSelected `json:"state,omitempty"`
}
type DocumentSelected struct {
Selected bool `json:"selected"`
Opened bool `json:"opened"`
}
//获取项目的文档树状结构
func (m *Document) FindDocumentTree(bookId int, selectedId int, isEdit ...bool) ([]*DocumentTree, error) {
o := orm.NewOrm()
trees := make([]*DocumentTree, 0)
var docs []*Document
count, err := o.QueryTable(m).Filter("book_id", bookId).OrderBy("order_sort", "identify").Limit(5000).All(&docs, "document_id", "version", "document_name", "parent_id", "identify")
if err != nil {
return trees, err
}
book, _ := NewBook().Find(bookId)
trees = make([]*DocumentTree, count)
for index, item := range docs {
tree := &DocumentTree{}
if selectedId > 0 {
if selectedId == item.DocumentId {
tree.State = &DocumentSelected{Selected: true, Opened: true}
}
} else {
if index == 0 {
tree.State = &DocumentSelected{Selected: true, Opened: true}
}
}
tree.DocumentId = item.DocumentId
tree.Identify = item.Identify
tree.Version = item.Version
tree.BookIdentify = book.Identify
if item.ParentId > 0 {
tree.ParentId = item.ParentId
} else {
tree.ParentId = "#"
}
idf := item.Identify
if idf == "" {
idf = strconv.Itoa(item.DocumentId)
}
if len(isEdit) > 0 && isEdit[0] == true {
tree.DocumentName = item.DocumentName + "<small class='text-danger'>(" + idf + ")</small>"
} else {
tree.DocumentName = item.DocumentName
}
trees[index] = tree
}
return trees, nil
}
func (m *Document) CreateDocumentTreeForHtml(bookId, selectedId int) (string, error) {
trees, err := m.FindDocumentTree(bookId, selectedId)
if err != nil {
return "", err
}
parentId := getSelectedNode(trees, selectedId)
buf := bytes.NewBufferString("")
getDocumentTree(trees, 0, selectedId, parentId, buf)
return buf.String(), nil
}
//使用递归的方式获取指定ID的顶级ID
func getSelectedNode(array []*DocumentTree, parentId int) int {
for _, item := range array {
if _, ok := item.ParentId.(string); ok && item.DocumentId == parentId {
return item.DocumentId
} else if pid, ok := item.ParentId.(int); ok && item.DocumentId == parentId {
if pid == parentId {
return 0
}
return getSelectedNode(array, pid)
}
}
return 0
}
func getDocumentTree(array []*DocumentTree, parentId int, selectedId int, selectedParentId int, buf *bytes.Buffer) {
buf.WriteString("<ul>")
for _, item := range array {
pid := 0
if p, ok := item.ParentId.(int); ok {
pid = p
}
if pid == parentId {
selected := ""
if item.DocumentId == selectedId {
selected = ` class="jstree-clicked"`
}
selectedLi := ""
if item.DocumentId == selectedParentId {
selectedLi = ` class="jstree-open"`
}
buf.WriteString("<li id=\"")
buf.WriteString(strconv.Itoa(item.DocumentId))
buf.WriteString("\"")
buf.WriteString(selectedLi)
buf.WriteString("><a href=\"")
if item.Identify != "" {
uri := beego.URLFor("DocumentController.Read", ":key", item.BookIdentify, ":id", item.Identify)
buf.WriteString(uri)
} else {
uri := beego.URLFor("DocumentController.Read", ":key", item.BookIdentify, ":id", item.DocumentId)
buf.WriteString(uri)
}
buf.WriteString("\" title=\"")
buf.WriteString(template.HTMLEscapeString(item.DocumentName) + "\"")
buf.WriteString(selected + ">")
buf.WriteString(template.HTMLEscapeString(item.DocumentName) + "</a>")
for _, sub := range array {
if p, ok := sub.ParentId.(int); ok && p == item.DocumentId {
getDocumentTree(array, p, selectedId, selectedParentId, buf)
break
}
}
buf.WriteString("</li>")
}
}
buf.WriteString("</ul>")
}