Refactor event binding in SUI core to support just-in-time mode
This commit is contained in:
parent
db033a10fd
commit
d00b62caf1
4 changed files with 88 additions and 76 deletions
120
data/bindata.go
120
data/bindata.go
File diff suppressed because one or more lines are too long
|
|
@ -31,13 +31,22 @@ func (page *Page) BindEvent(ctx *BuildContext, sel *goquery.Selection, cn string
|
|||
}
|
||||
|
||||
// BindEvent is a method that binds events to the component in just-in-time mode.
|
||||
// This is temporarily used in the JIT mode. It will be refectored in the future.
|
||||
func (parser *TemplateParser) BindEvent(sel *goquery.Selection, ns string, cn string) {
|
||||
|
||||
sel.FindMatcher(eventMatcher).Each(func(i int, s *goquery.Selection) {
|
||||
id := fmt.Sprintf("%s-%d", ns, parser.sequence)
|
||||
sel.SetAttr("s:event-jit", id)
|
||||
ReplaceEventData(sel)
|
||||
sel.SetAttr("s:event-cn", cn)
|
||||
id := fmt.Sprintf("%s-%d-%d", ns, parser.sequence, i+1)
|
||||
s.SetAttr("s:event", id)
|
||||
ReplaceEventData(s)
|
||||
s.SetAttr("s:event-cn", cn)
|
||||
parser.sequence++
|
||||
})
|
||||
|
||||
// Bind page event
|
||||
compSel := sel.Children().First()
|
||||
id := fmt.Sprintf("%s-%d", ns, parser.sequence)
|
||||
compSel.SetAttr("s:event-cn", "__page")
|
||||
compSel.SetAttr("s:event-jit", id)
|
||||
}
|
||||
|
||||
// GetEventScript the event script
|
||||
|
|
|
|||
|
|
@ -65,12 +65,6 @@ func (parser *TemplateParser) parseJitComponent(sel *goquery.Selection) {
|
|||
}
|
||||
parser.parseElementComponent(comsel)
|
||||
sel.ReplaceWithSelection(comsel)
|
||||
|
||||
// Bind the events (support for the page event only, full support is in the feature)
|
||||
ns := comsel.AttrOr("s:ns", "")
|
||||
if ns != "" {
|
||||
parser.BindEvent(comsel, ns, "__page")
|
||||
}
|
||||
}
|
||||
|
||||
func (parser *TemplateParser) newJitComponentSel(sel *goquery.Selection, comp *JitComponent) (*goquery.Selection, error) {
|
||||
|
|
@ -87,6 +81,8 @@ func (parser *TemplateParser) newJitComponentSel(sel *goquery.Selection, comp *J
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("Component %s failed to load, please recompile the component. %s", comp.route, err.Error())
|
||||
}
|
||||
|
||||
root := doc.Find("body").First()
|
||||
compSel := doc.Find("body").Children().First()
|
||||
data := Data{}
|
||||
for _, attr := range sel.Nodes[0].Attr {
|
||||
|
|
@ -166,6 +162,7 @@ func (parser *TemplateParser) newJitComponentSel(sel *goquery.Selection, comp *J
|
|||
// Replace the children
|
||||
children := sel.Contents()
|
||||
compSel.Find("children").ReplaceWithSelection(children)
|
||||
parser.BindEvent(root, ns, cn) // bind the events
|
||||
return compSel, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -152,8 +152,10 @@ class __Query {
|
|||
}
|
||||
|
||||
toggleClass(className) {
|
||||
const classes = Array.isArray(className) ? className : className.split(" ");
|
||||
classes.forEach((c) => {
|
||||
const classes = Array.isArray(className)
|
||||
? className
|
||||
: className?.split(" ");
|
||||
classes?.forEach((c) => {
|
||||
const v = c.replace(/[\n\r\s]/g, "");
|
||||
if (v === "") return;
|
||||
this.element?.classList.toggle(v);
|
||||
|
|
@ -162,8 +164,10 @@ class __Query {
|
|||
}
|
||||
|
||||
removeClass(className) {
|
||||
const classes = Array.isArray(className) ? className : className.split(" ");
|
||||
classes.forEach((c) => {
|
||||
const classes = Array.isArray(className)
|
||||
? className
|
||||
: className?.split(" ");
|
||||
classes?.forEach((c) => {
|
||||
const v = c.replace(/[\n\r\s]/g, "");
|
||||
if (v === "") return;
|
||||
this.element?.classList.remove(v);
|
||||
|
|
@ -172,8 +176,10 @@ class __Query {
|
|||
}
|
||||
|
||||
addClass(className) {
|
||||
const classes = Array.isArray(className) ? className : className.split(" ");
|
||||
classes.forEach((c) => {
|
||||
const classes = Array.isArray(className)
|
||||
? className
|
||||
: className?.split(" ");
|
||||
classes?.forEach((c) => {
|
||||
const v = c.replace(/[\n\r\s]/g, "");
|
||||
if (v === "") return;
|
||||
this.element?.classList.add(v);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue