ludic/packages/ludic.ui/link.ludic

51 lines
2.3 KiB
Text

# link.ludic - each tag in a file that is not an element names a component: the file's own, an
# unnamed import's exported one, or `as:Name` from the import called `as`
const LK_OWN: string = " row button input select textarea progress meter spacer scroll img hr if else each slot state style component screen import let fragment template provide #text "
function lib_link(ui_st: mut UiState, f: UiFile, t: UiTpl) -> void {
t.sheet = f.sheet
if not lk_element(ui_st, t.tag) {
t.comp = lk_find(f, t.tag, false)
if t.comp == null { t.cls = uc_find(ui_st, t.tag) }
if t.cls != null {
lk_kids(ui_st, f, t)
return
}
@alloc_ok("only when a template is wrong: a broken screen, never a working one")
if t.comp == null and lk_find(f, t.tag, true) != null { ui_err(ui_st, `{t.where}: <{t.tag}>: that component is not exported - export="true" on it lets other files use it`) } else if t.comp == null { ui_err(ui_st, `{t.where}: <{t.tag}>: there is no such element, and no component of that name here or exported by an import`) }
}
lk_kids(ui_st, f, t)
}
function lk_kids(ui_st: mut UiState, f: UiFile, t: UiTpl) -> void {
for i in 0 .. len(t.kids) { lib_link(ui_st, f, t.kids[i]) }
}
function lk_element(ui_st: UiState, tag: string) -> bool { return lk_in(LK_OWN, tag) or lk_in(EL_BOXES, tag) or lk_in(EL_TEXTS, tag) or nt_find(ui_st, tag) != null }
# a word in a list of words written " a b c "
function lk_in(words: string, w: string) -> bool {
let n = len(w)
for i in 0 .. len(words) - n - 1 {
if words[i] == 32 and words[i + n + 1] == 32 and sat(words, i + 1, w) { return true }
}
return false
}
# `any` finds an unexported one too, to say why it cannot be used
@alloc_ok("a template being linked: once per component class (uc_load)")
function lk_find(f: UiFile, tag: string, any: bool) -> UiTpl {
var colon = -1
for i in 0 .. len(tag) {
if tag[i] == 58 { colon = i }
}
if colon < 0 {
let own = lib_find(f.names, tag)
if own >= 0 { return f.comps[own] }
}
for i in 0 .. len(f.imp) {
var want = tag
if colon >= 0 {
if f.imp_as[i] != tag[0..colon] { continue }
want = tag[colon + 1..len(tag)]
} else if f.imp_as[i] != "" { continue }
let at = lib_find(f.imp[i].names, want)
if at >= 0 and (any or f.imp[i].exported[at]) { return f.imp[i].comps[at] }
}
return null
}