From da955f2c486d10b9d7be50d56d94b86ab50bb7f9 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 11 Jul 2024 09:38:58 +0800 Subject: [PATCH] fix sui GlobRoutes bug and add s:click support --- sui/core/injections.go | 43 ++++++++++++++++++++++++++++++++++ sui/storages/local/template.go | 8 ++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/sui/core/injections.go b/sui/core/injections.go index 1549c867..0b5d73cc 100644 --- a/sui/core/injections.go +++ b/sui/core/injections.go @@ -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 diff --git a/sui/storages/local/template.go b/sui/storages/local/template.go index 871c6e75..0eaa5de8 100644 --- a/sui/storages/local/template.go +++ b/sui/storages/local/template.go @@ -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