Refactor event binding logic in SUI core
This commit is contained in:
parent
fbfd3f270a
commit
258163497a
5 changed files with 29 additions and 29 deletions
|
|
@ -55,7 +55,9 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
|
|||
doc.Find("body").SetAttr("s:ns", namespace)
|
||||
|
||||
// Bind the Page events
|
||||
page.BindEvent(ctx, doc.Selection)
|
||||
if !option.JitMode {
|
||||
page.BindEvent(ctx, doc.Selection, "__page", true)
|
||||
}
|
||||
|
||||
warnings, err := page.buildComponents(doc, ctx, option)
|
||||
if err != nil {
|
||||
|
|
@ -171,7 +173,7 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
|
|||
}
|
||||
|
||||
// Bind the component events
|
||||
page.BindEvent(ctx, doc.Selection, component)
|
||||
page.BindEvent(ctx, doc.Selection, component, false)
|
||||
|
||||
body := doc.Selection.Find("body")
|
||||
|
||||
|
|
|
|||
|
|
@ -114,6 +114,7 @@ func (page *Page) CompileAsComponent(ctx *BuildContext, option *BuildOption) (st
|
|||
opt := *option
|
||||
opt.IgnoreDocument = true
|
||||
opt.WithWrapper = true
|
||||
opt.JitMode = true
|
||||
doc, warnings, err := page.Build(ctx, &opt)
|
||||
if err != nil {
|
||||
return "", warnings, err
|
||||
|
|
|
|||
|
|
@ -10,37 +10,32 @@ import (
|
|||
)
|
||||
|
||||
// BindEvent is a method that binds events to the page.
|
||||
func (page *Page) BindEvent(ctx *BuildContext, sel *goquery.Selection, component ...string) {
|
||||
|
||||
// Component events
|
||||
if len(component) > 0 && component[0] != "" {
|
||||
fmt.Println("component events:", page.Route, component[0])
|
||||
matcher := NewAttrPrefixMatcher(`s:on-`)
|
||||
sel.FindMatcher(matcher).Each(func(i int, s *goquery.Selection) {
|
||||
fmt.Println("\tcomponent event", i, s.Nodes[0].Data)
|
||||
page.appendEventScript(ctx, s, component[0])
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (page *Page) BindEvent(ctx *BuildContext, sel *goquery.Selection, component string, ispage bool) {
|
||||
matcher := NewAttrPrefixMatcher(`s:on-`)
|
||||
sel.FindMatcher(matcher).Each(func(i int, s *goquery.Selection) {
|
||||
page.appendEventScript(ctx, s, "")
|
||||
if comp, has := s.Attr("is"); has && ctx.isJitComponent(comp) {
|
||||
return
|
||||
}
|
||||
script := GetEventScript(&ctx.sequence, s, page.namespace, component, ispage)
|
||||
if script != nil {
|
||||
ctx.scripts = append(ctx.scripts, *script)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (page *Page) appendEventScript(ctx *BuildContext, sel *goquery.Selection, component string) {
|
||||
// GetEventScript the event script
|
||||
func GetEventScript(sequence *int, sel *goquery.Selection, ns string, cn string, ispage bool) *ScriptNode {
|
||||
|
||||
if len(sel.Nodes) == 0 {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
// Page events
|
||||
events := map[string]string{}
|
||||
dataUnique := map[string]string{}
|
||||
jsonUnique := map[string]string{}
|
||||
id := fmt.Sprintf("event-%d", ctx.sequence)
|
||||
ctx.sequence++
|
||||
id := fmt.Sprintf("event-%d", *sequence)
|
||||
*sequence = *sequence + 1
|
||||
|
||||
for _, attr := range sel.Nodes[0].Attr {
|
||||
|
||||
|
|
@ -83,20 +78,20 @@ func (page *Page) appendEventScript(ctx *BuildContext, sel *goquery.Selection, c
|
|||
|
||||
source := ""
|
||||
for name, handler := range events {
|
||||
if component == "" {
|
||||
if ispage {
|
||||
source += pageEventInjectScript(id, name, dataRaw, jsonRaw, handler) + "\n"
|
||||
} else {
|
||||
source += compEventInjectScript(id, name, component, dataRaw, jsonRaw, handler) + "\n"
|
||||
source += compEventInjectScript(id, name, cn, dataRaw, jsonRaw, handler) + "\n"
|
||||
}
|
||||
sel.RemoveAttr(fmt.Sprintf("s:on-%s", name))
|
||||
}
|
||||
|
||||
ctx.scripts = append(ctx.scripts, ScriptNode{
|
||||
Source: source,
|
||||
Namespace: page.namespace,
|
||||
Component: component,
|
||||
Attrs: []html.Attribute{{Key: "event", Val: id}},
|
||||
})
|
||||
|
||||
sel.SetAttr("s:event", id)
|
||||
|
||||
return &ScriptNode{
|
||||
Source: source,
|
||||
Namespace: ns,
|
||||
Component: cn,
|
||||
Attrs: []html.Attribute{{Key: "event", Val: id}},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@ func (parser *TemplateParser) parseComponent(sel *goquery.Selection) {
|
|||
return
|
||||
}
|
||||
|
||||
// fmt.Println(sel.Nodes[0].Attr)
|
||||
sel.SetHtml(html)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -253,6 +253,7 @@ type BuildOption struct {
|
|||
AssetRoot string `json:"asset_root,omitempty"`
|
||||
IgnoreAssetRoot bool `json:"ignore_asset_root,omitempty"`
|
||||
IgnoreDocument bool `json:"ignore_document,omitempty"`
|
||||
JitMode bool `json:"jit_mode,omitempty"`
|
||||
WithWrapper bool `json:"with_wrapper,omitempty"`
|
||||
KeepPageTag bool `json:"keep_page_tag,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue