AnP/Go/Application/Components.go

185 lines
4.3 KiB
Go

package Application
import (
"AnP/ComponentsBox"
"AnP/Models"
"AnP/Utils"
"slices"
)
type Components struct {
anp Models.AnPModel
autoclosed []string
items map[string]func(parameters ...any) any
Licenses ComponentsBox.LicensesComponent
}
func NewComponents(anp Models.AnPModel) Components {
var components Components = Components{
anp: anp,
autoclosed: []string{"img", "hr", "br"},
items: map[string]func(parameters ...any) any{},
Licenses: ComponentsBox.NewLicensesComponent(anp),
}
components.items["image"] = func(inputs ...any) any {
return components.Image(inputs[0])
}
components.items["licenses"] = func(inputs ...any) any {
return components.Licenses.Build(Utils.Get(inputs, Utils.GetPointer([][]any{}))...)
}
components.items["main_menu"] = func(inputs ...any) any {
return components.CreateMainMenu(Utils.Get(inputs, Utils.GetPointer([]any{}))...)
}
return components
}
func GetItem(item []any) (string, any, any) {
var l int = len(item)
var name string
var attributes any
var childs any = nil
if l > 0 {
name = Utils.Get[string](item[0], nil)
}
if l > 1 {
attributes = item[1]
}
if l >= 3 {
childs = item[2]
}
return name, attributes, childs
}
func (_self Components) Set(items ...[]any) string {
var html string = ``
for _, subitem := range items {
tag, attributes, childs := GetItem(subitem)
method, is_method := _self.items[tag]
if is_method && attributes != nil {
var attributes_processed map[string]any = Utils.GetDictionary(attributes, false)
if _, is := attributes_processed["built"]; is {
is_method = false
}
}
if is_method {
switch results := method(Utils.GetArray(attributes)...).(type) {
case string:
html += results
case []any:
html += _self.Set(results)
}
} else {
html += `<` + tag + _self.anp.Attributes.Create(attributes, nil, "build")
if slices.Contains(_self.autoclosed, tag) {
html += ` />`
} else {
html += `>`
if childs != nil {
switch values := any(childs).(type) {
case [][]any:
html += _self.Set(values...)
case []any:
html += _self.Set(values)
default:
html += Utils.ToString(values)
}
}
html += `</` + tag + `>`
}
}
}
return html
}
func (_self Components) Image(inputs any) []any {
switch value := inputs.(type) {
case []string:
inputs = map[string]any{"sources": value}
}
var attributes map[string]any = Utils.GetDictionary(inputs, false)
var source_keys []string = []string{"images", "sources", "image", "source", "src"}
var images []string = Utils.GetStrings(Utils.GetValue[any](source_keys, attributes, nil))
attributes = AttributesRemove(attributes, append([]string{
"class", "classes", "status", "data_status", "data_i", "data_sources",
}, source_keys...))
return []any{"span", Utils.GetDictionary([]any{attributes, map[string]any{
"class": JoinClasses("image", Utils.GetValue[any]([]string{"class", "classes"}, attributes, nil)),
"data_status": "unloaded",
"data_sources": images,
"data_i": 0,
}}, true), [][]any{
{"img"},
{"span"},
}}
}
func (_self Components) LicensesBuild(licenses ...[]any) []any {
return _self.Licenses.Build(licenses...)
}
func (_self Components) CreateMainMenu(items ...any) []any {
var blocks [][]any = [][]any{}
for _, item := range items {
var attributes map[string]any = map[string]any{}
var text string = ""
var name string = ""
switch set := item.(type) {
case []any:
var link string = *Utils.GetArgument[string](set, 0, nil)
var target string = *Utils.GetArgument(set, 1, Utils.GetPointer("_self"))
name = *Utils.GetArgument(set, 2, Utils.GetPointer(""))
text = _self.anp.I18N.Get([]string{*Utils.GetArgument(set, 3, Utils.GetPointer("")), name}, nil, name)
attributes["href"] = link
attributes["target"] = target
if name != "" {
attributes["data_i18n"] = name
attributes["data_i18n_without"] = true
}
if text != "" || name != "" {
attributes["title"] = text
}
case string, any:
attributes["href"] = item
}
blocks = append(blocks, []any{"li", nil, [][]any{
{"a", attributes, [][]any{
{"span", map[string]any{"data_icon": name}, ""},
{"span", map[string]any{"data_i18n": name}, text},
}},
}})
}
return []any{"nav", map[string]any{"class": "main-menu"}, [][]any{
{"ul", nil, blocks},
}}
}