fix sui GlobRoutes bug and add s:click support

This commit is contained in:
Max 2024-07-11 09:38:58 +08:00
parent 37f07f5606
commit da955f2c48
2 changed files with 50 additions and 1 deletions

View file

@ -7,6 +7,16 @@ const initScriptTmpl = `
var __sui_data = %s;
} catch (e) { console.log('init data error:', e); }
function __sui_findParentWithAttribute(element, attributeName) {
while (element && element !== document) {
if (element.hasAttribute(attributeName)) {
return element.getAttribute(attributeName);
}
element = element.parentElement;
}
return null;
}
document.addEventListener("DOMContentLoaded", function () {
try {
document.querySelectorAll("[s\\:ready]").forEach(function (element) {
@ -21,6 +31,39 @@ const initScriptTmpl = `
}
}
});
document.querySelectorAll("[s\\:click]").forEach(function (element) {
const method = element.getAttribute("s:click");
const cn = __sui_findParentWithAttribute(element, "s:cn");
if (method && cn && typeof window[cn] === "function") {
const obj = new window[cn]();
if (typeof obj[method] === "function") {
element.addEventListener("click", function (event) {
try {
obj[method](element, event);
} catch (e) {
const message = e.message || e || "An error occurred";
console.error(` + "`[SUI] ${cn}.${method} Error: ${message}`" + `);
}
});
return
}
console.error(` + "`[SUI] ${cn}.${method} Error: Method not found`" + `);
return
}
if (method && typeof window[method] === "function") {
element.addEventListener("click", function (event) {
try {
window[method](element, event);
} catch (e) {
const message = e.message || e || "An error occurred";
console.error(` + "`[SUI] ${method} Error: ${message}`" + `);
}
});
}
});
} catch (e) {}
});
%s

View file

@ -49,7 +49,13 @@ func (tmpl *Template) GlobRoutes(patterns []string, unique ...bool) ([]string, e
if err != nil {
return nil, err
}
routes = append(routes, paths...)
for _, path := range paths {
if !tmpl.local.fs.IsDir(filepath.Join(tmpl.Root, path)) {
continue
}
routes = append(routes, path)
}
}
// Unique