togoist/web/web.go

51 lines
1.1 KiB
Go
Raw Permalink Normal View History

2016-10-09 18:10:34 +00:00
package main
import (
"time"
"github.com/bep/debounce"
2016-10-09 21:08:03 +00:00
"github.com/go-humble/locstor"
2016-10-09 18:10:34 +00:00
"github.com/gopherjs/jquery"
"gitlab.com/chrissexton/togoist"
)
var jQuery = jquery.NewJQuery
func convert(tmpl, json string, cb func(error, string)) {
go func() {
res, err := togoist.TogoString(tmpl, json)
cb(err, res)
}()
}
func handleChange() {
tmpl := jQuery("#tmpl").Val()
input := jQuery("#input").Val()
convert(tmpl, input, func(err error, out string) {
if err != nil {
jQuery("#output").SetText(err.Error())
return
}
2016-10-09 21:08:03 +00:00
locstor.SetItem("tmpl", tmpl)
locstor.SetItem("input", input)
2016-10-09 18:10:34 +00:00
jQuery("#output").SetText(out)
})
}
2016-10-09 21:08:03 +00:00
func getLocalStorage() {
if tmpl, err := locstor.GetItem("tmpl"); err == nil {
jQuery("#tmpl").SetText(tmpl)
}
if input, err := locstor.GetItem("input"); err == nil {
jQuery("#input").SetText(input)
}
handleChange()
}
2016-10-09 18:10:34 +00:00
func main() {
debounced, _ := debounce.New(time.Second)
jQuery("#tmpl").On(jquery.KEYUP, func() { debounced(handleChange) })
jQuery("#input").On(jquery.KEYUP, func() { debounced(handleChange) })
2016-10-09 21:08:03 +00:00
getLocalStorage()
2016-10-09 18:10:34 +00:00
}