Refactor utils.ts for improved code readability and maintainability

This commit is contained in:
Max 2024-08-03 18:57:29 +08:00
parent eb4443ebe7
commit 259b707ca8
4 changed files with 95 additions and 70 deletions

File diff suppressed because one or more lines are too long

View file

@ -34,9 +34,16 @@ func Render(process *process.Process) interface{} {
return fmt.Sprintf("<div class='text-danger'> %s </div>", err.Error())
}
c, _, err := r.MakeCache()
if err != nil {
return fmt.Sprintf("<div class='text-danger'> %s </div>", err.Error())
var c *core.Cache = nil
if !r.Request.DisableCache() {
c = core.GetCache(r.File)
}
if c == nil {
c, _, err = r.MakeCache()
if err != nil {
return fmt.Sprintf("<div class='text-danger'> %s </div>", err.Error())
}
}
if c == nil {

View file

@ -18,11 +18,8 @@ function $$(selector) {
return null;
}
function __sui_component_root(elm, name) {
while (elm && elm.getAttribute("s:cn") !== name) {
elm = elm.parentElement;
}
return elm;
function __sui_component_root(elm: Element, name: string) {
return elm.closest(`[s\\:cn=${name}]`);
}
function __sui_state(component) {
@ -258,7 +255,7 @@ async function __sui_render(
// Prepare loader
let loader = `<span class="sui-render-loading">Loading...</span>`;
if (option.showLoader) {
if (option.showLoader && option.replace) {
if (typeof option.showLoader === "string") {
loader = option.showLoader;
} else if (option.showLoader instanceof HTMLElement) {

View file

@ -35,6 +35,27 @@ class __Query {
this.selector = selector;
}
elm(): Element | null {
return this.element;
}
elms(): NodeListOf<Element> | null {
return this.elements;
}
$$() {
if (!this.element) {
return null;
}
const root = this.element.closest("[s\\:cn]");
if (!root) {
return null;
}
// @ts-ignore
return $$(root);
}
each(callback: (element: __Query, index: number) => void) {
if (!this.elements) {
return;