feat: Update event handling in sui/core to support event component names

This commit is contained in:
Max 2024-08-03 11:23:59 +08:00
parent 5514fea7ec
commit a2e5a52ad8
3 changed files with 29 additions and 21 deletions

View file

@ -94,10 +94,12 @@ func GetEventScript(sequence int, sel *goquery.Selection, ns string, cn string,
for name, handler := range events { for name, handler := range events {
if ispage { if ispage {
source += pageEventInjectScript(id, name, dataRaw, jsonRaw, handler) + "\n" source += pageEventInjectScript(id, name, dataRaw, jsonRaw, handler) + "\n"
sel.SetAttr("s:event-cn", "__page")
} else { } else {
source += compEventInjectScript(id, name, cn, dataRaw, jsonRaw, handler) + "\n" source += compEventInjectScript(id, name, cn, dataRaw, jsonRaw, handler) + "\n"
sel.SetAttr("s:event-cn", cn)
} }
sel.RemoveAttr(fmt.Sprintf("s:on-%s", name)) // sel.RemoveAttr(fmt.Sprintf("s:on-%s", name))
} }
sel.SetAttr("s:event", id) sel.SetAttr("s:event", id)

View file

@ -55,7 +55,7 @@ const initScriptTmpl = `
const cn = element.getAttribute("s:cn"); const cn = element.getAttribute("s:cn");
if (method && typeof window[cn] === "function") { if (method && typeof window[cn] === "function") {
try { try {
window[cn](element); new window[cn](element);
} catch (e) { } catch (e) {
const message = e.message || e || "An error occurred"; const message = e.message || e || "An error occurred";
console.error(` + "`[SUI] ${cn} Error: ${message}`" + `); console.error(` + "`[SUI] ${cn} Error: ${message}`" + `);
@ -82,13 +82,17 @@ const i118nScriptTmpl = `
` `
const pageEventScriptTmpl = ` const pageEventScriptTmpl = `
document.querySelector("[s\\:event=%s]").addEventListener("%s", function (event) { if (document.querySelector("[s\\:event=%s]")) {
let elms = document.querySelectorAll("[s\\:event=%s]");
elms.forEach(function (element) {
element.addEventListener("%s", function (event) {
const dataKeys = %s; const dataKeys = %s;
const jsonKeys = %s; const jsonKeys = %s;
const root = document.body; const root = document.body;
const target = this; __sui_event_handler(event, dataKeys, jsonKeys, element, root, window.%s);
__sui_event_handler(event, dataKeys, jsonKeys, target, root, %s);
}); });
});
}
` `
const compEventScriptTmpl = ` const compEventScriptTmpl = `
@ -153,7 +157,7 @@ func headInjectionScript(jsonRaw string) string {
} }
func pageEventInjectScript(eventID, eventName, dataKeys, jsonKeys, handler string) string { func pageEventInjectScript(eventID, eventName, dataKeys, jsonKeys, handler string) string {
return fmt.Sprintf(pageEventScriptTmpl, eventID, eventName, dataKeys, jsonKeys, handler) return fmt.Sprintf(pageEventScriptTmpl, eventID, eventID, eventName, dataKeys, jsonKeys, handler)
} }
func compEventInjectScript(eventID, eventName, component, dataKeys, jsonKeys, handler string) string { func compEventInjectScript(eventID, eventName, component, dataKeys, jsonKeys, handler string) string {

View file

@ -72,6 +72,7 @@ var allowUsePropAttrs = map[string]bool{
"s:elif": true, "s:elif": true,
"s:for": true, "s:for": true,
"s:event": true, "s:event": true,
"s:event-cn": true,
"s:render": true, "s:render": true,
} }
@ -80,6 +81,7 @@ var keepAttrs = map[string]bool{
"s:cn": true, "s:cn": true,
"s:ready": true, "s:ready": true,
"s:event": true, "s:event": true,
"s:event-cn": true,
"s:render": true, "s:render": true,
} }
@ -863,7 +865,7 @@ func (parser *TemplateParser) Tidy(s *goquery.Selection) {
// Remove the parsed attribute // Remove the parsed attribute
attrs := []html.Attribute{} attrs := []html.Attribute{}
for _, attr := range node.Attr { for _, attr := range node.Attr {
if strings.HasPrefix(attr.Key, "s:") && !keepAttrs[attr.Key] { if strings.HasPrefix(attr.Key, "s:") && !keepAttrs[attr.Key] && !strings.HasPrefix(attr.Key, "s:on-") {
continue continue
} }