Commit c1f836bd authored by Siong-Ui Te's avatar Siong-Ui Te

move independent treeview code to common frontend library

parent ad3ca016
......@@ -103,6 +103,7 @@ lib_pali:
go get -u github.com/siongui/gopalilib/lib/jsgettext
go get -u github.com/siongui/gopalilib/libfrontend
go get -u github.com/siongui/gopalilib/libfrontend/everyword
go get -u github.com/siongui/gopalilib/libfrontend/treeview
lib_gtmpl:
@echo "\033[92mInstalling Go html/template with gettext support ...\033[0m"
......
......@@ -8,6 +8,7 @@ import (
"github.com/siongui/gopalilib/lib/jsgettext"
"github.com/siongui/gopalilib/libfrontend"
"github.com/siongui/gopalilib/libfrontend/everyword"
"github.com/siongui/gopalilib/libfrontend/treeview"
)
func getFinalShowLocale() string {
......@@ -60,7 +61,7 @@ func main() {
//println(string(b))
tree := lib.Tree{}
json.Unmarshal(b, &tree)
NewTreeview("treeview", tree, xmlAction)
treeview.NewTreeview("treeview", tree, xmlAction)
SetupXSLTProcessor()
SetupModal()
......
package main
import (
. "github.com/siongui/godom"
"github.com/siongui/gopalilib/lib"
)
func traverseTreeviewData(tree lib.Tree, actionFunc func(lib.Tree)) *Object {
if len(tree.SubTrees) > 0 {
div := Document.CreateElement("div")
div.ClassList().Add("item")
sign := Document.CreateElement("span")
sign.SetInnerHTML("+")
span := Document.CreateElement("span")
span.ClassList().Add("treeNode")
span.SetInnerHTML(tree.Text)
div.AppendChild(sign)
div.AppendChild(span)
childrenContainer := Document.CreateElement("div")
childrenContainer.ClassList().Add("childrenContainer")
for _, child := range tree.SubTrees {
childrenContainer.AppendChild(traverseTreeviewData(child, actionFunc))
}
childrenContainer.Style().SetDisplay("none")
span.AddEventListener("click", func(e Event) {
if childrenContainer.Style().Display() == "none" {
childrenContainer.Style().SetDisplay("")
sign.SetInnerHTML("-")
} else {
childrenContainer.Style().SetDisplay("none")
sign.SetInnerHTML("+")
}
})
all := Document.CreateElement("div")
all.AppendChild(div)
all.AppendChild(childrenContainer)
return all
} else {
div := Document.CreateElement("div")
div.ClassList().Add("item")
span := Document.CreateElement("span")
span.ClassList().Add("treeNode")
span.SetInnerHTML(tree.Text)
span.AddEventListener("click", func(e Event) {
actionFunc(tree)
})
div.AppendChild(span)
return div
}
}
func appendCSSToHeadElement() {
css := `.item {
margin-bottom: 3px;
padding-bottom: 3px;
border-bottom: 1px solid #E0E0E0;
}
.item:hover {
background-color: #F0F8FF;
}
.treeNode:hover {
cursor: pointer;
color: blue;
}
.childrenContainer {
margin-left: .4em;
padding-left: .4em;
border-left: 1px dotted blue;
}`
s := Document.CreateElement("style")
s.SetInnerHTML(css)
// insert style of treeview at the end of head element
Document.QuerySelector("head").AppendChild(s)
}
func NewTreeview(id string, root lib.Tree, actionFunc func(lib.Tree)) {
appendCSSToHeadElement()
treeviewContainer := Document.GetElementById(id)
for _, child := range root.SubTrees {
tree := traverseTreeviewData(child, actionFunc)
treeviewContainer.AppendChild(tree)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment