Skip to content

Commit

Permalink
Merge pull request #745 from trheyi/main
Browse files Browse the repository at this point in the history
[add] SUI real-time rendering mode supports event binding (page event  only)
  • Loading branch information
trheyi authored Sep 2, 2024
2 parents 253dee8 + db033a1 commit 657224b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 87 deletions.
120 changes: 60 additions & 60 deletions data/bindata.go

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions sui/core/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@ 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.
func (parser *TemplateParser) BindEvent(sel *goquery.Selection, ns string, cn string) {
sel.FindMatcher(eventMatcher).Each(func(i int, s *goquery.Selection) {
script := GetEventScript(parser.sequence, s, ns, cn, "event-jit", false)
if script != nil {
script.Component = ""
script.Parent = "body"
parser.scripts = append(parser.scripts, *script)
parser.sequence++
}
id := fmt.Sprintf("%s-%d", ns, parser.sequence)
sel.SetAttr("s:event-jit", id)
ReplaceEventData(sel)
sel.SetAttr("s:event-cn", cn)
})
}

Expand Down
6 changes: 6 additions & 0 deletions sui/core/jit.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ 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) {
Expand Down
34 changes: 18 additions & 16 deletions sui/core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,27 @@ var keepWords = map[string]bool{
}

var allowUsePropAttrs = map[string]bool{
"s:if": true,
"s:elif": true,
"s:for": true,
"s:event": true,
"s:event-cn": true,
"s:render": true,
"s:public": true,
"s:assets": true,
"s:if": true,
"s:elif": true,
"s:for": true,
"s:event": true,
"s:event-jit": true,
"s:event-cn": true,
"s:render": true,
"s:public": true,
"s:assets": true,
}

var keepAttrs = map[string]bool{
"s:ns": true,
"s:cn": true,
"s:ready": true,
"s:event": true,
"s:event-cn": true,
"s:render": true,
"s:public": true,
"s:assets": true,
"s:ns": true,
"s:cn": true,
"s:ready": true,
"s:event": true,
"s:event-jit": true,
"s:event-cn": true,
"s:render": true,
"s:public": true,
"s:assets": true,
}

// NewTemplateParser create a new template parser
Expand Down
14 changes: 10 additions & 4 deletions sui/libsui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ function __sui_event_handler(event, dataKeys, jsonKeys, target, root, handler) {
}

function __sui_event_init(elm: Element) {
const eventElms = elm.querySelectorAll("[s\\:event]");
eventElms.forEach((eventElm) => {
const bindEvent = (eventElm) => {
const cn = eventElm.getAttribute("s:event-cn") || "";
if (cn == "") {
console.error("[SUI] Component name is required for event binding", elm);
Expand Down Expand Up @@ -204,15 +203,22 @@ function __sui_event_init(elm: Element) {
continue;
}

const comp = new window[cn](eventElm.closest(`[s\\:cn=${cn}]`));
const component = eventElm.closest(`[s\\:cn=${cn}]`);
// @ts-ignore
const comp = new window[cn](component);
const handler = comp[bind];
const root = comp.root;
const target = eventElm;
eventElm.addEventListener(name, (event) => {
__sui_event_handler(event, dataKeys, jsonKeys, target, root, handler);
});
}
});
};

const eventElms = elm.querySelectorAll("[s\\:event]");
const jitEventElms = elm.querySelectorAll("[s\\:event-jit]");
eventElms.forEach((eventElm) => bindEvent(eventElm));
jitEventElms.forEach((eventElm) => bindEvent(eventElm));
}

function __sui_store(elm) {
Expand Down

0 comments on commit 657224b

Please sign in to comment.