123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- package web
- import (
- "fmt"
- "net/url"
- "strings"
- h "github.com/theplant/htmlgo"
- )
- type jsCall struct {
- method string
- args []interface{}
- raw string
- }
- type Var string
- type VueEventTagBuilder struct {
- beforeScript string
- calls []jsCall
- afterScript string
- thenScript string
- }
- func Plaid() (r *VueEventTagBuilder) {
- r = &VueEventTagBuilder{
- calls: []jsCall{
- {
- method: "$plaid",
- },
- },
- }
- r.Vars(Var("vars")).
- Form(Var("plaidForm"))
- return
- }
- func GET() (r *VueEventTagBuilder) {
- return Plaid().Method("GET")
- }
- func POST() (r *VueEventTagBuilder) {
- return Plaid().Method("POST")
- }
- // URL is request page url without push state
- func (b *VueEventTagBuilder) URL(url interface{}) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "url",
- args: []interface{}{url},
- })
- return b
- }
- func (b *VueEventTagBuilder) EventFunc(id interface{}) (r *VueEventTagBuilder) {
- c := jsCall{
- method: "eventFunc",
- args: []interface{}{id},
- }
- b.calls = append(b.calls, c)
- return b
- }
- func (b *VueEventTagBuilder) Method(v interface{}) (r *VueEventTagBuilder) {
- c := jsCall{
- method: "method",
- args: []interface{}{v},
- }
- b.calls = append(b.calls, c)
- return b
- }
- func (b *VueEventTagBuilder) Reload() (r *VueEventTagBuilder) {
- b.Raw("reload()")
- return b
- }
- func (b *VueEventTagBuilder) Vars(v interface{}) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "vars",
- args: []interface{}{v},
- })
- return b
- }
- func (b *VueEventTagBuilder) MergeQuery(v interface{}) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "mergeQuery",
- args: []interface{}{v},
- })
- return b
- }
- func (b *VueEventTagBuilder) Query(key interface{}, vs interface{}) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "query",
- args: []interface{}{key, vs},
- })
- return b
- }
- func (b *VueEventTagBuilder) QueryIf(key interface{}, vs interface{}, add bool) (r *VueEventTagBuilder) {
- if !add {
- return b
- }
- b.calls = append(b.calls, jsCall{
- method: "query",
- args: []interface{}{key, vs},
- })
- return b
- }
- // ClearMergeQuery param v use interface{} because you can not only pass []string,
- // but also pass in javascript variables by using web.Var("$event")
- func (b *VueEventTagBuilder) ClearMergeQuery(v interface{}) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "clearMergeQuery",
- args: []interface{}{v},
- })
- return b
- }
- func (b *VueEventTagBuilder) StringQuery(v interface{}) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "stringQuery",
- args: []interface{}{v},
- })
- return b
- }
- func (b *VueEventTagBuilder) PushState(v interface{}) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "pushState",
- args: []interface{}{v},
- })
- return b
- }
- func (b *VueEventTagBuilder) Location(v *LocationBuilder) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "location",
- args: []interface{}{v},
- })
- return b
- }
- func (b *VueEventTagBuilder) Queries(v url.Values) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "queries",
- args: []interface{}{v},
- })
- return b
- }
- func (b *VueEventTagBuilder) PushStateURL(v string) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "pushStateURL",
- args: []interface{}{v},
- })
- return b
- }
- func (b *VueEventTagBuilder) Form(v interface{}) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "form",
- args: []interface{}{v},
- })
- return b
- }
- func (b *VueEventTagBuilder) FormClear() (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "formClear",
- })
- return b
- }
- func (b *VueEventTagBuilder) FieldValue(name interface{}, v interface{}) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "fieldValue",
- args: []interface{}{name, v},
- })
- return b
- }
- func (b *VueEventTagBuilder) PopState(v interface{}) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- method: "popstate",
- args: []interface{}{v},
- })
- return b
- }
- func (b *VueEventTagBuilder) Raw(script string) (r *VueEventTagBuilder) {
- b.calls = append(b.calls, jsCall{
- raw: script,
- })
- return b
- }
- func (b *VueEventTagBuilder) Go() (r string) {
- b.Raw("go()")
- return b.String()
- }
- func (b *VueEventTagBuilder) RunPushState() (r string) {
- b.Raw("runPushState()")
- return b.String()
- }
- func (b *VueEventTagBuilder) BeforeScript(script string) (r *VueEventTagBuilder) {
- b.beforeScript = script
- return b
- }
- func (b *VueEventTagBuilder) AfterScript(script string) (r *VueEventTagBuilder) {
- b.afterScript = script
- return b
- }
- func (b *VueEventTagBuilder) ThenScript(script string) (r *VueEventTagBuilder) {
- b.thenScript = script
- return b
- }
- func (b *VueEventTagBuilder) String() string {
- var cs []string
- for _, c := range b.calls {
- if len(c.raw) > 0 {
- cs = append(cs, c.raw)
- continue
- }
- if len(c.args) == 0 {
- cs = append(cs, fmt.Sprintf("%s()", c.method))
- continue
- }
- if len(c.args) == 1 {
- cs = append(cs, fmt.Sprintf("%s(%s)", c.method, toJsValue(c.args[0])))
- continue
- }
- var args []string
- for _, arg := range c.args {
- args = append(args, toJsValue(arg))
- }
- cs = append(cs, fmt.Sprintf("%s(%s)", c.method, strings.Join(args, ", ")))
- }
- if len(b.thenScript) > 0 {
- cs = append(cs, fmt.Sprintf("then(function(r){ %s })", b.thenScript))
- }
- var sems []string
- if len(b.beforeScript) > 0 {
- sems = append(sems, b.beforeScript)
- }
- sems = append(sems, strings.Join(cs, "."))
- if len(b.afterScript) > 0 {
- sems = append(sems, b.afterScript)
- }
- return strings.Join(sems, "; ")
- }
- func toJsValue(v interface{}) string {
- switch v.(type) {
- case Var:
- return fmt.Sprint(v)
- default:
- return h.JSONString(v)
- }
- }
- func (b *VueEventTagBuilder) MarshalJSON() ([]byte, error) {
- panic(fmt.Sprintf("call .Go() at the end, value: %s", b.String()))
- }
- const InitContextVars = "v-init-context:vars"
- const InitContextLocals = "v-init-context:locals"
- type VFieldNameOption interface {
- private()
- }
- type UseForm string
- func (UseForm) private() {}
- func VFieldName(v string, opts ...VFieldNameOption) []interface{} {
- formVar := "plaidForm"
- for _, op := range opts {
- if vf, ok := op.(UseForm); ok {
- formVar = string(vf)
- }
- }
- return []interface{}{
- "v-field-name",
- fmt.Sprintf("[%s, %s]", formVar, h.JSONString(v)),
- }
- }
- func GlobalEvents() *h.HTMLTagBuilder {
- return h.Tag("global-events")
- }
|