[add] active & extend support

This commit is contained in:
Max 2023-09-29 12:44:05 +08:00
parent 8a7903db1e
commit 1aade992b2
6 changed files with 26 additions and 7 deletions

View file

@ -171,7 +171,8 @@ func PageTree(process *process.Process) interface{} {
exception.New(err.Error(), 500).Throw()
}
tree, err := tmpl.PageTree()
route := route(process, 2)
tree, err := tmpl.PageTree(route)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

View file

@ -13,7 +13,7 @@ type SUI interface {
// ITemplate is the interface for the ITemplate
type ITemplate interface {
Pages() ([]IPage, error)
PageTree() ([]*PageTreeNode, error)
PageTree(route string) ([]*PageTreeNode, error)
Page(route string) (IPage, error)
Blocks() ([]IBlock, error)
@ -30,6 +30,9 @@ type ITemplate interface {
// IPage is the interface for the page
type IPage interface {
Load() error
Get() *Page
EditorRender(request *Request) (*ResponseEditor, error)
EditorPageSource() ResponseSource
EditorScriptSource() ResponseSource

View file

@ -1,5 +1,10 @@
package core
// Get get the base info
func (page *Page) Get() *Page {
return page
}
// GetHTML get the html
func (page *Page) GetHTML() {}

View file

@ -23,6 +23,8 @@ type PageTreeNode struct {
IsDir bool `json:"is_dir,omitempty"`
Children []*PageTreeNode `json:"children,omitempty"`
IPage IPage `json:"page,omitempty"`
Expand bool `json:"expand,omitempty"`
Active bool `json:"active,omitempty"`
}
// Component is the struct for the component

View file

@ -41,11 +41,13 @@ func (tmpl *Template) Pages() ([]core.IPage, error) {
}
// PageTree gets the page tree.
func (tmpl *Template) PageTree() ([]*core.PageTreeNode, error) {
func (tmpl *Template) PageTree(route string) ([]*core.PageTreeNode, error) {
exts := []string{"*.sui", "*.html", "*.htm", "*.page"}
rootNode := &core.PageTreeNode{
Name: tmpl.Name,
IsDir: true,
Expand: true,
Children: []*core.PageTreeNode{}, // 初始为空的切片
}
@ -82,6 +84,7 @@ func (tmpl *Template) PageTree() ([]*core.PageTreeNode, error) {
Name: dir,
IsDir: true,
Children: []*core.PageTreeNode{},
Expand: true,
}
currentDir.Children = append(currentDir.Children, newDir)
currentDir = newDir
@ -100,6 +103,9 @@ func (tmpl *Template) PageTree() ([]*core.PageTreeNode, error) {
return err
}
pageInfo := page.Get()
active := route == pageInfo.Route
// Attach the page to the appropriate directory node.
dirs := strings.Split(relPath, string(filepath.Separator))
currentDir := rootNode
@ -112,10 +118,12 @@ func (tmpl *Template) PageTree() ([]*core.PageTreeNode, error) {
}
}
currentDir.Expand = active
currentDir.Children = append(currentDir.Children, &core.PageTreeNode{
Name: tmpl.getPageBase(currentDir.Name),
IsDir: false,
IPage: page,
Name: tmpl.getPageBase(currentDir.Name),
IsDir: false,
IPage: page,
Active: active,
})
return nil

View file

@ -53,7 +53,7 @@ func TestTemplatePageTree(t *testing.T) {
t.Fatalf("GetTemplate error: %v", err)
}
pages, err := tmpl.PageTree()
pages, err := tmpl.PageTree("/page/[id]")
if err != nil {
t.Fatalf("Pages error: %v", err)
}