2017-04-28 18:08:01 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2017-04-30 22:13:12 +08:00
|
|
|
"bytes"
|
2019-05-20 17:38:37 +08:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"math"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
2021-03-26 11:34:02 +08:00
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-23 15:09:17 +08:00
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
2024-05-09 13:33:27 +08:00
|
|
|
// "gorm.io/driver/sqlite"
|
|
|
|
// "gorm.io/gorm"
|
2017-04-28 18:08:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DocumentTree struct {
|
2019-05-20 17:38:37 +08:00
|
|
|
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:"-"`
|
|
|
|
AAttrs map[string]interface{} `json:"a_attr"`
|
2024-05-09 13:33:27 +08:00
|
|
|
Children []*DocumentTree `json:"children"`
|
2017-04-28 18:08:01 +08:00
|
|
|
}
|
2024-05-09 13:33:27 +08:00
|
|
|
|
|
|
|
// type DocumentTreeJson struct {
|
|
|
|
// gorm.Model
|
|
|
|
// DocumentId int `json:"id"`
|
|
|
|
// DocumentName string `json:"text"`
|
|
|
|
// ParentId interface{} `json:"parent"`
|
|
|
|
// Children []*DocumentTreeJson `json:"children" gorm:"-"`
|
|
|
|
// }
|
|
|
|
|
2017-04-28 18:08:01 +08:00
|
|
|
type DocumentSelected struct {
|
2018-01-26 17:17:38 +08:00
|
|
|
Selected bool `json:"selected"`
|
|
|
|
Opened bool `json:"opened"`
|
2019-05-20 17:38:37 +08:00
|
|
|
Disabled bool `json:"disabled"`
|
2017-04-28 18:08:01 +08:00
|
|
|
}
|
|
|
|
|
2024-05-09 13:33:27 +08:00
|
|
|
// 获取项目的文档树状结构
|
2018-09-13 18:19:26 +08:00
|
|
|
func (item *Document) FindDocumentTree(bookId int) ([]*DocumentTree, error) {
|
2017-04-28 18:08:01 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
trees := make([]*DocumentTree, 0)
|
2017-04-28 18:08:01 +08:00
|
|
|
|
|
|
|
var docs []*Document
|
|
|
|
|
2019-05-20 17:38:37 +08:00
|
|
|
count, err := o.QueryTable(item).Filter("book_id", bookId).
|
|
|
|
OrderBy("order_sort", "document_id").
|
|
|
|
Limit(math.MaxInt32).
|
|
|
|
All(&docs, "document_id", "version", "document_name", "parent_id", "identify", "is_open")
|
2017-04-28 18:08:01 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
return trees, err
|
2017-04-28 18:08:01 +08:00
|
|
|
}
|
2018-06-06 20:45:24 +08:00
|
|
|
book, _ := NewBook().Find(bookId)
|
2017-04-28 18:08:01 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
trees = make([]*DocumentTree, count)
|
2017-04-28 18:08:01 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
for index, item := range docs {
|
2019-05-20 17:38:37 +08:00
|
|
|
tree := &DocumentTree{
|
|
|
|
AAttrs: map[string]interface{}{"is_open": false, "opened": 0},
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
if index == 0 {
|
|
|
|
tree.State = &DocumentSelected{Selected: true, Opened: true}
|
2019-05-20 17:38:37 +08:00
|
|
|
tree.AAttrs = map[string]interface{}{"is_open": true, "opened": 1}
|
|
|
|
} else if item.IsOpen == 1 {
|
2018-08-14 18:17:46 +08:00
|
|
|
tree.State = &DocumentSelected{Selected: false, Opened: true}
|
2019-05-20 17:38:37 +08:00
|
|
|
tree.AAttrs = map[string]interface{}{"is_open": true, "opened": 1}
|
|
|
|
}
|
|
|
|
if item.IsOpen == 2 {
|
|
|
|
tree.State = &DocumentSelected{Selected: false, Opened: false, Disabled: true}
|
|
|
|
tree.AAttrs = map[string]interface{}{"disabled": true, "opened": 2}
|
2017-04-28 18:08:01 +08:00
|
|
|
}
|
|
|
|
tree.DocumentId = item.DocumentId
|
|
|
|
tree.Identify = item.Identify
|
|
|
|
tree.Version = item.Version
|
2017-04-30 22:13:12 +08:00
|
|
|
tree.BookIdentify = book.Identify
|
2017-04-28 18:08:01 +08:00
|
|
|
if item.ParentId > 0 {
|
|
|
|
tree.ParentId = item.ParentId
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
2017-04-28 18:08:01 +08:00
|
|
|
tree.ParentId = "#"
|
|
|
|
}
|
|
|
|
|
|
|
|
tree.DocumentName = item.DocumentName
|
|
|
|
|
|
|
|
trees[index] = tree
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
return trees, nil
|
2017-04-28 18:08:01 +08:00
|
|
|
}
|
2017-04-30 22:13:12 +08:00
|
|
|
|
2024-05-09 13:33:27 +08:00
|
|
|
// 获取项目的文档树状结构2
|
|
|
|
func (item *Document) FindDocumentTree2(bookId int) ([]*DocumentTree, error) {
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
trees := make([]*DocumentTree, 0)
|
|
|
|
|
|
|
|
var docs []*Document
|
|
|
|
|
|
|
|
count, err := o.QueryTable(item).Filter("book_id", bookId).
|
|
|
|
OrderBy("order_sort", "document_id").
|
|
|
|
Limit(math.MaxInt32).
|
|
|
|
All(&docs, "document_id", "version", "document_name", "parent_id", "identify", "is_open")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return trees, err
|
|
|
|
}
|
|
|
|
book, _ := NewBook().Find(bookId)
|
|
|
|
|
|
|
|
trees = make([]*DocumentTree, count)
|
|
|
|
|
|
|
|
for index, item := range docs {
|
|
|
|
tree := &DocumentTree{
|
|
|
|
AAttrs: map[string]interface{}{"is_open": false, "opened": 0},
|
|
|
|
}
|
|
|
|
if index == 0 {
|
|
|
|
tree.State = &DocumentSelected{Selected: true, Opened: true}
|
|
|
|
tree.AAttrs = map[string]interface{}{"is_open": true, "opened": 1}
|
|
|
|
} else if item.IsOpen == 1 {
|
|
|
|
tree.State = &DocumentSelected{Selected: false, Opened: true}
|
|
|
|
tree.AAttrs = map[string]interface{}{"is_open": true, "opened": 1}
|
|
|
|
}
|
|
|
|
if item.IsOpen == 2 {
|
|
|
|
tree.State = &DocumentSelected{Selected: false, Opened: false, Disabled: true}
|
|
|
|
tree.AAttrs = map[string]interface{}{"disabled": true, "opened": 2}
|
|
|
|
}
|
|
|
|
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 = "#"
|
|
|
|
// }
|
|
|
|
|
|
|
|
tree.DocumentName = item.DocumentName
|
|
|
|
|
|
|
|
trees[index] = tree
|
|
|
|
}
|
|
|
|
|
|
|
|
return trees, nil
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
func (item *Document) CreateDocumentTreeForHtml(bookId, selectedId int) (string, error) {
|
|
|
|
trees, err := item.FindDocumentTree(bookId)
|
2017-04-30 22:13:12 +08:00
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
return "", err
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
2018-06-06 20:45:24 +08:00
|
|
|
parentId := getSelectedNode(trees, selectedId)
|
2017-04-30 22:13:12 +08:00
|
|
|
|
|
|
|
buf := bytes.NewBufferString("")
|
|
|
|
|
2018-06-06 20:45:24 +08:00
|
|
|
getDocumentTree(trees, 0, selectedId, parentId, buf)
|
2018-01-26 17:17:38 +08:00
|
|
|
|
|
|
|
return buf.String(), nil
|
2017-04-30 22:13:12 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-09 13:33:27 +08:00
|
|
|
// 使用递归的方式获取指定ID的顶级ID
|
2017-04-30 22:13:12 +08:00
|
|
|
func getSelectedNode(array []*DocumentTree, parent_id int) int {
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
for _, item := range array {
|
|
|
|
if _, ok := item.ParentId.(string); ok && item.DocumentId == parent_id {
|
2017-04-30 22:13:12 +08:00
|
|
|
return item.DocumentId
|
2018-01-26 17:17:38 +08:00
|
|
|
} else if pid, ok := item.ParentId.(int); ok && item.DocumentId == parent_id {
|
|
|
|
return getSelectedNode(array, pid)
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
return 0
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
|
|
|
|
2018-06-06 20:45:24 +08:00
|
|
|
func getDocumentTree(array []*DocumentTree, parentId int, selectedId int, selectedParentId int, buf *bytes.Buffer) {
|
2024-07-04 09:43:33 +08:00
|
|
|
buf.WriteString("<ul style='display:none;'>")
|
2017-04-30 22:13:12 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
for _, item := range array {
|
2017-04-30 22:13:12 +08:00
|
|
|
pid := 0
|
|
|
|
|
2017-05-02 10:00:21 +08:00
|
|
|
if p, ok := item.ParentId.(int); ok {
|
2017-04-30 22:13:12 +08:00
|
|
|
pid = p
|
|
|
|
}
|
2018-06-06 20:45:24 +08:00
|
|
|
if pid == parentId {
|
2017-04-30 22:13:12 +08:00
|
|
|
|
|
|
|
selected := ""
|
2018-06-06 20:45:24 +08:00
|
|
|
if item.DocumentId == selectedId {
|
2017-04-30 22:13:12 +08:00
|
|
|
selected = ` class="jstree-clicked"`
|
|
|
|
}
|
2018-08-14 18:17:46 +08:00
|
|
|
selectedLi := ""
|
|
|
|
if item.DocumentId == selectedParentId || (item.State != nil && item.State.Opened) {
|
|
|
|
selectedLi = ` class="jstree-open"`
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
2019-05-20 17:38:37 +08:00
|
|
|
buf.WriteString(fmt.Sprintf("<li id=\"%d\"%s><a href=\"", item.DocumentId, selectedLi))
|
2017-05-02 10:00:21 +08:00
|
|
|
if item.Identify != "" {
|
2018-03-13 19:20:50 +08:00
|
|
|
uri := conf.URLFor("DocumentController.Read", ":key", item.BookIdentify, ":id", item.Identify)
|
2017-04-30 22:13:12 +08:00
|
|
|
buf.WriteString(uri)
|
2017-05-02 10:00:21 +08:00
|
|
|
} else {
|
2018-03-13 19:20:50 +08:00
|
|
|
uri := conf.URLFor("DocumentController.Read", ":key", item.BookIdentify, ":id", item.DocumentId)
|
2017-04-30 22:13:12 +08:00
|
|
|
buf.WriteString(uri)
|
|
|
|
}
|
2019-05-20 17:38:37 +08:00
|
|
|
buf.WriteString(fmt.Sprintf("\" title=\"%s\"", template.HTMLEscapeString(item.DocumentName)))
|
|
|
|
if item.State != nil && item.State.Disabled {
|
|
|
|
buf.WriteString(" disabled=\"true\"")
|
|
|
|
}
|
|
|
|
buf.WriteString(fmt.Sprintf(" data-version=\"%d\"%s>%s</a>", item.Version, selected, template.HTMLEscapeString(item.DocumentName)))
|
2017-04-30 22:13:12 +08:00
|
|
|
|
2017-05-02 10:00:21 +08:00
|
|
|
for _, sub := range array {
|
|
|
|
if p, ok := sub.ParentId.(int); ok && p == item.DocumentId {
|
2018-06-06 20:45:24 +08:00
|
|
|
getDocumentTree(array, p, selectedId, selectedParentId, buf)
|
2017-05-02 10:00:21 +08:00
|
|
|
break
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.WriteString("</li>")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.WriteString("</ul>")
|
|
|
|
}
|