Refactor component event injection to handle missing event selectors
This commit is contained in:
parent
dc3c637c5e
commit
a8d151993e
1 changed files with 35 additions and 21 deletions
|
|
@ -30,6 +30,13 @@ func libsui(minify bool) (string, error) {
|
||||||
|
|
||||||
const libsuisource = `
|
const libsuisource = `
|
||||||
|
|
||||||
|
function __sui_component_root(elm, name) {
|
||||||
|
while (elm && elm.getAttribute("s:cn") !== name) {
|
||||||
|
elm = elm.parentElement;
|
||||||
|
}
|
||||||
|
return elm;
|
||||||
|
}
|
||||||
|
|
||||||
function __sui_state(component) {
|
function __sui_state(component) {
|
||||||
this.handlers = component.watch || {};
|
this.handlers = component.watch || {};
|
||||||
this.Set = async function (key, value) {
|
this.Set = async function (key, value) {
|
||||||
|
|
@ -112,25 +119,28 @@ const libsuisource = `
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function __sui_event_handler(event, dataKeys, jsonKeys, elm, handler) {
|
function __sui_event_handler(event, dataKeys, jsonKeys, target, root, handler) {
|
||||||
const data = {};
|
const data = {};
|
||||||
dataKeys.forEach(function (key) {
|
target = target || null;
|
||||||
const value = elm.getAttribute("data:" + key);
|
if (target) {
|
||||||
data[key] = value;
|
dataKeys.forEach(function (key) {
|
||||||
})
|
const value = target.getAttribute("data:" + key);
|
||||||
jsonKeys.forEach(function (key) {
|
data[key] = value;
|
||||||
const value = elm.getAttribute("json:" + key);
|
})
|
||||||
data[key] = null;
|
jsonKeys.forEach(function (key) {
|
||||||
if (value && value != "") {
|
const value = target.getAttribute("json:" + key);
|
||||||
try {
|
data[key] = null;
|
||||||
data[key] = JSON.parse(value);
|
if (value && value != "") {
|
||||||
} catch (e) {
|
try {
|
||||||
const message = e.message || e || "An error occurred";
|
data[key] = JSON.parse(value);
|
||||||
console.error(` + "`[SUI] Event Handler Error: ${message}`" + `, elm);
|
} catch (e) {
|
||||||
|
const message = e.message || e || "An error occurred";
|
||||||
|
console.error(` + "`[SUI] Event Handler Error: ${message}`" + `, target);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
})
|
}
|
||||||
handler && handler(event, data, elm);
|
handler && handler(event, data, root, target);
|
||||||
};
|
};
|
||||||
|
|
||||||
function __sui_store(elm) {
|
function __sui_store(elm) {
|
||||||
|
|
@ -211,7 +221,9 @@ const pageEventScriptTmpl = `
|
||||||
document.querySelector("[s\\:event=%s]").addEventListener("%s", function (event) {
|
document.querySelector("[s\\:event=%s]").addEventListener("%s", function (event) {
|
||||||
const dataKeys = %s;
|
const dataKeys = %s;
|
||||||
const jsonKeys = %s;
|
const jsonKeys = %s;
|
||||||
__sui_event_handler(event, dataKeys, jsonKeys, this, %s);
|
const root = document.body;
|
||||||
|
const target = this;
|
||||||
|
__sui_event_handler(event, dataKeys, jsonKeys, target, root, %s);
|
||||||
});
|
});
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
@ -220,8 +232,10 @@ const compEventScriptTmpl = `
|
||||||
document.querySelector("[s\\:event=%s]").addEventListener("%s", function (event) {
|
document.querySelector("[s\\:event=%s]").addEventListener("%s", function (event) {
|
||||||
const dataKeys = %s;
|
const dataKeys = %s;
|
||||||
const jsonKeys = %s;
|
const jsonKeys = %s;
|
||||||
handler = new %s(this).%s;
|
const root = __sui_component_root(this, "%s");
|
||||||
__sui_event_handler(event, dataKeys, jsonKeys, this, handler);
|
handler = new %s(root).%s;
|
||||||
|
const target = event.target || null;
|
||||||
|
__sui_event_handler(event, dataKeys, jsonKeys, target, root, handler);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
@ -264,7 +278,7 @@ func pageEventInjectScript(eventID, eventName, dataKeys, jsonKeys, handler strin
|
||||||
}
|
}
|
||||||
|
|
||||||
func compEventInjectScript(eventID, eventName, component, dataKeys, jsonKeys, handler string) string {
|
func compEventInjectScript(eventID, eventName, component, dataKeys, jsonKeys, handler string) string {
|
||||||
return fmt.Sprintf(compEventScriptTmpl, eventID, eventID, eventName, dataKeys, jsonKeys, component, handler)
|
return fmt.Sprintf(compEventScriptTmpl, eventID, eventID, eventName, dataKeys, jsonKeys, component, component, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func componentInitScript(root string) string {
|
func componentInitScript(root string) string {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue