122 lines
3.8 KiB
Go
122 lines
3.8 KiB
Go
package Models
|
|
|
|
import (
|
|
"AnP/Interfaces"
|
|
"AnP/Utils"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
type ApplicationModel struct {
|
|
anp AnPModel
|
|
Attributes map[string]any
|
|
Routes Interfaces.RoutesInterface
|
|
Paths []string
|
|
}
|
|
|
|
func SetClasses(classes []string) string {
|
|
|
|
if !slices.Contains(classes, "anp") {
|
|
classes = append(classes, "anp")
|
|
}
|
|
|
|
return strings.Join(classes, " ")
|
|
}
|
|
|
|
func NewApplicationModel(anp AnPModel, routes Interfaces.RoutesInterface, inputs any) ApplicationModel {
|
|
|
|
var attributes map[string]any = map[string]any{}
|
|
var strings_sets map[string][]string = map[string][]string{}
|
|
var l int
|
|
var licenses [][]any = [][]any{}
|
|
var menu []any = []any{}
|
|
|
|
for _, license := range Utils.GetValue[[]any]([]string{
|
|
"application_licenses", "application_license", "licenses", "license",
|
|
}, inputs, nil) {
|
|
licenses = append(licenses, Utils.Get[[]any](license, nil))
|
|
}
|
|
|
|
for _, item := range Utils.GetValue[[]any]([]string{
|
|
"application_menu", "menu",
|
|
}, inputs, nil) {
|
|
menu = append(menu, Utils.Get[any](item, nil))
|
|
}
|
|
|
|
for key, keys := range map[string][]string{
|
|
"git": {"application_git", "git"},
|
|
"project": {"application_name", "name", "project"},
|
|
"web": {"application_link", "application_url", "application_web", "link", "url", "web"},
|
|
"logo": {"application_logo", "logo"},
|
|
"logo_light": {"application_logo_light", "logo_light", "application_logo", "logo"},
|
|
"logo_dark": {"application_logo_dark", "logo_dark", "application_logo", "logo"},
|
|
"snake": {"application_snake", "snake"},
|
|
} {
|
|
attributes[key] = Utils.GetValue[string](keys, inputs, nil)
|
|
}
|
|
|
|
for _, key := range []string{"", "_light", "_dark"} {
|
|
switch logo := attributes["logo"+key].(type) {
|
|
case []string, []any:
|
|
attributes["logo"+key+"_sources"] = logo
|
|
case string, any:
|
|
attributes["logo"+key+"_sources"] = []any{logo}
|
|
default:
|
|
attributes["logo"+key+"_sources"] = []any{}
|
|
}
|
|
attributes["logo"+key+"_sources"] = Utils.VariablesEncode(attributes["logo"+key+"_sources"])
|
|
}
|
|
|
|
for key, keys := range map[string][][]string{
|
|
"charset": {{"charset"}, {"utf-8"}},
|
|
} {
|
|
attributes[key] = Utils.GetValue(keys[0], inputs, &keys[1][0])
|
|
}
|
|
|
|
for key, keys := range map[string][]string{
|
|
"class": {"application_class", "class"},
|
|
"authors": {"application_authors", "application_author", "authors", "author"},
|
|
"scripts": {"application_scripts", "application_script", "scripts", "script"},
|
|
"styles": {"application_styles", "application_style", "styles", "style"},
|
|
"dictionaries": {"application_dictionaries", "application_dictionary", "dictionaries", "dictionary"},
|
|
"menu": {"application_menu", "menu"},
|
|
} {
|
|
strings_sets[key] = Utils.GetStringsFilled(Utils.GetValue[any](keys, inputs, nil))
|
|
}
|
|
|
|
attributes["class"] = SetClasses(strings_sets["class"])
|
|
for _, key := range []string{"scripts", "styles", "dictionaries"} {
|
|
attributes[key] = Utils.JSONEncode(strings_sets[key])
|
|
}
|
|
|
|
l = len(strings_sets["authors"])
|
|
switch l {
|
|
case 0:
|
|
attributes["authors"] = ""
|
|
case 1:
|
|
attributes["authors"] = strings_sets["authors"][0]
|
|
case 2:
|
|
attributes["authors"] = strings.Join(strings_sets["authors"], " & ")
|
|
default:
|
|
attributes["authors"] = strings.Join(strings_sets["authors"][:l-1], ", ") + " & " + strings_sets["authors"][l-1]
|
|
}
|
|
|
|
attributes["licenses"] = licenses
|
|
attributes["licenses_html"] = anp.Components.Set(anp.Components.LicensesBuild(licenses...))
|
|
|
|
attributes["menu"] = menu
|
|
attributes["menu_html"] = anp.Components.Set(anp.Components.CreateMainMenu(menu...))
|
|
|
|
return ApplicationModel{
|
|
anp: anp,
|
|
Attributes: attributes,
|
|
Routes: routes,
|
|
Paths: Utils.GetStrings(Utils.GetValue[any]([]string{"path", "paths"}, inputs, nil)),
|
|
}
|
|
}
|
|
|
|
func (_self ApplicationModel) GetAttributes() map[string]any {
|
|
return maps.Clone(_self.Attributes)
|
|
}
|