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

move common code to library

parent bb9f2860
......@@ -102,6 +102,7 @@ lib_pali:
go get -u github.com/siongui/gopalilib/dicutil
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
lib_gtmpl:
@echo "\033[92mInstalling Go html/template with gettext support ...\033[0m"
......
package main
import (
"regexp"
"strings"
. "github.com/siongui/godom"
)
var paliWord = regexp.MustCompile(`[AaBbCcDdEeGgHhIiJjKkLlMmNnOoPpRrSsTtUuVvYyĀāĪīŪūṀṁṂṃŊŋṆṇṄṅÑñṬṭḌḍḶḷ]+`)
func markPaliWordInSpan(s string) string {
return paliWord.ReplaceAllStringFunc(s, func(match string) string {
return "<span class='paliword'>" + match + "</span>"
})
}
func toDom(s string, clickHandler func(string)) *Object {
// wrap all words in span
spanContainer := Document.CreateElement("span")
spanContainer.SetInnerHTML(markPaliWordInSpan(s))
// register click handler to every word
spans := spanContainer.GetElementsByTagName("span")
for _, span := range spans {
word := strings.ToLower(span.InnerHTML())
span.AddEventListener("click", func(e Event) {
clickHandler(word)
})
}
return spanContainer
}
// find all words in the element
func traverse(elm *Object, clickHandler func(string)) {
// 1: element node
if elm.NodeType() == 1 {
for _, childNodes := range elm.ChildNodes() {
traverse(childNodes, clickHandler)
}
return
}
// 3: text node
if elm.NodeType() == 3 {
s := elm.NodeValue()
if strings.TrimSpace(s) != "" {
// string is not whitespace
elm.ParentNode().ReplaceChild(toDom(s, clickHandler), elm)
}
return
}
}
func MarkEveryWord(selector string, clickHandler func(string)) {
element := Document.QuerySelector(selector)
traverse(element, clickHandler)
}
......@@ -7,6 +7,7 @@ import (
"github.com/siongui/gopalilib/lib"
"github.com/siongui/gopalilib/lib/jsgettext"
"github.com/siongui/gopalilib/libfrontend"
"github.com/siongui/gopalilib/libfrontend/everyword"
)
func getFinalShowLocale() string {
......@@ -38,7 +39,7 @@ func xmlAction(text, action string) {
mainview.QuerySelector("div.content").RemoveAllChildNodes()
mainview.QuerySelector("div.content").AppendChild(fragment)
MarkEveryWord("#mainview > div.content", wordClickedHandler)
everyword.MarkEveryWord("#mainview > div.content", wordClickedHandler)
ToggleMobileTreeview()
}
......
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