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

move frontend setting code to another package

parent 2ed07fa5
......@@ -106,6 +106,7 @@ lib_pali:
go get -u github.com/siongui/gopalilib/libfrontend/treeview
go get -u github.com/siongui/gopalilib/libfrontend/xslt
go get -u github.com/siongui/gopalilib/libfrontend/velthuis
go get -u github.com/siongui/gopalilib/libfrontend/setting
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/setting"
"github.com/siongui/gopalilib/libfrontend/treeview"
"github.com/siongui/gopalilib/libfrontend/velthuis"
"github.com/siongui/gopalilib/libfrontend/xslt"
......@@ -71,7 +72,7 @@ func main() {
// Call velthuis before SetupModalInput (order of keyevent handler matters)
velthuis.BindPaliInputMethodToInputTextElementById("modal-input")
SetupModalInput("#modal-input")
SetupPaliSetting()
setting.SetupPaliSetting()
HideIsLoadingWebsite()
}
package main
import (
. "github.com/siongui/godom"
"github.com/siongui/gopalilib/lib"
)
var storageKeyName = "PaliSetting"
func SavePaliSetting(setting lib.PaliSetting) {
str, _ := lib.PaliSettingToJsonString(setting)
LocalStorage.Set(storageKeyName, str)
}
func LoadPaliSetting() lib.PaliSetting {
setting, _ := lib.JsonStringToPaliSetting(LocalStorage.GetItem(storageKeyName))
return setting
}
func SetupPaliSetting() {
isPreview := Document.GetElementById("isShowWordPreview")
p2en := Document.GetElementById("p2en")
p2ja := Document.GetElementById("p2ja")
p2zh := Document.GetElementById("p2zh")
p2vi := Document.GetElementById("p2vi")
p2my := Document.GetElementById("p2my")
dicLangOrder := Document.GetElementById("dicLangOrder")
setting := lib.GetDefaultPaliSetting()
// check if there is saved setting in user browser
if LocalStorage.IsKeyExist(storageKeyName) {
// use saved setting
setting, _ = lib.JsonStringToPaliSetting(LocalStorage.GetItem(storageKeyName))
} else {
// no setting saved, use default setting
SavePaliSetting(setting)
}
// restore setting
isPreview.Set("checked", setting.IsShowWordPreview)
p2en.Set("checked", setting.P2en)
p2ja.Set("checked", setting.P2ja)
p2zh.Set("checked", setting.P2zh)
p2vi.Set("checked", setting.P2vi)
p2my.Set("checked", setting.P2my)
dicLangOrder.Set("value", setting.DicLangOrder)
// set up event handler for setting change
isPreview.AddEventListener("click", func(e Event) {
setting.IsShowWordPreview = isPreview.Get("checked").Bool()
SavePaliSetting(setting)
})
// https://stackoverflow.com/questions/4471401/getting-value-of-html-checkbox-from-onclick-onchange-events
p2en.AddEventListener("click", func(e Event) {
setting.P2en = p2en.Get("checked").Bool()
SavePaliSetting(setting)
})
p2ja.AddEventListener("click", func(e Event) {
setting.P2ja = p2ja.Get("checked").Bool()
SavePaliSetting(setting)
})
p2zh.AddEventListener("click", func(e Event) {
setting.P2zh = p2zh.Get("checked").Bool()
SavePaliSetting(setting)
})
p2vi.AddEventListener("click", func(e Event) {
setting.P2vi = p2vi.Get("checked").Bool()
SavePaliSetting(setting)
})
p2my.AddEventListener("click", func(e Event) {
setting.P2my = p2my.Get("checked").Bool()
SavePaliSetting(setting)
})
dicLangOrder.AddEventListener("change", func(e Event) {
setting.DicLangOrder = dicLangOrder.Get("options").Call("item",
dicLangOrder.Get("selectedIndex").Int()).Get("value").String()
SavePaliSetting(setting)
})
}
......@@ -9,6 +9,7 @@ import (
"github.com/siongui/gopalilib/lib"
"github.com/siongui/gopalilib/lib/dicmgr"
"github.com/siongui/gopalilib/libfrontend"
"github.com/siongui/gopalilib/libfrontend/setting"
)
func wordLinkHtml(word string) string {
......@@ -49,7 +50,7 @@ func possibleWordClickHandler(word string) {
}
html := `<div class="previewWordName is-size-4 mb-1">` + wordLinkHtml(word) + `</div>`
html += dicmgr.GetWordDefinitionHtml(wi, LoadPaliSetting(), Window.Navigator().Languages())
html += dicmgr.GetWordDefinitionHtml(wi, setting.LoadPaliSetting(), Window.Navigator().Languages())
SetModalContent(html)
}()
}
......@@ -101,7 +102,7 @@ func showWordDefinitionInModal(word string) {
SetModalContent("Fail to Get " + word + ": " + err.Error())
return
}
SetModalContent(dicmgr.GetWordDefinitionHtml(wi, LoadPaliSetting(), Window.Navigator().Languages()))
SetModalContent(dicmgr.GetWordDefinitionHtml(wi, setting.LoadPaliSetting(), Window.Navigator().Languages()))
}
func FindLongestPrefixWithNonZeroSuggestedWords(word string) string {
......
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