diff --git a/sui/core/build.go b/sui/core/build.go index b1b415c4..f1b32eb8 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -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") diff --git a/sui/core/compile.go b/sui/core/compile.go index fb5a0693..84b94981 100644 --- a/sui/core/compile.go +++ b/sui/core/compile.go @@ -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 diff --git a/sui/core/event.go b/sui/core/event.go index dcdc6890..ab4b7345 100644 --- a/sui/core/event.go +++ b/sui/core/event.go @@ -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}}, + } } diff --git a/sui/core/jit.go b/sui/core/jit.go index 73d8e7b6..9803d4ae 100644 --- a/sui/core/jit.go +++ b/sui/core/jit.go @@ -62,6 +62,7 @@ func (parser *TemplateParser) parseComponent(sel *goquery.Selection) { return } + // fmt.Println(sel.Nodes[0].Attr) sel.SetHtml(html) } diff --git a/sui/core/types.go b/sui/core/types.go index 7c81197b..6e21a859 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -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"`