AnP/Go/ComponentsBox/LicensesComponent.go

200 lines
4.4 KiB
Go

package ComponentsBox
import (
"AnP/Models"
"AnP/Utils"
"strconv"
)
type LicensesComponent struct {
anp Models.AnPModel
items map[string]func(inputs ...any) []any
}
func NewLicensesComponent(anp Models.AnPModel) LicensesComponent {
var component LicensesComponent = LicensesComponent{
anp: anp,
items: map[string]func(inputs ...any) []any{},
}
component.items["gplv3"] = func(inputs ...any) []any {
return component.GPLv3()
}
component.items["cc_by_nc_sa_4"] = func(inputs ...any) []any {
return component.CC_BY_NC_SA_4()
}
component.items["copyright"] = func(inputs ...any) []any {
return component.Copyright(
*Utils.GetArgument[any](inputs, 0, nil),
*Utils.GetArgument[any](inputs, 1, nil),
*Utils.GetArgument(inputs, 2, Utils.GetPointer(false)),
)
}
return component
}
func (_self LicensesComponent) Build(licenses ...[]any) []any {
var blocks [][]any = [][]any{}
for _, data := range licenses {
var l int = len(data)
var name string
var inputs []any = []any{}
if l > 0 {
name = Utils.Get[string](data[0], nil)
}
if l > 1 {
inputs = data[1:]
}
if method, exists := _self.items[name]; exists {
blocks = append(blocks, method(inputs...))
}
}
return []any{"span", map[string]any{
"class": "licenses",
}, blocks}
}
func (_self LicensesComponent) GPLv3() []any {
return []any{"a", map[string]any{
"class": "license gpl-v3",
"href": "https://www.gnu.org/licenses/gpl-3.0.txt",
"target": "_blank",
"title": "GPLv3",
}, [][]any{
{"span", map[string]any{"class": "text"}, "GPLv3"},
{"image", map[string]any{
"sources": []any{"https://www.gnu.org/graphics/gplv3-88x31.png"},
"alt": "GPLv3",
}},
}}
}
func (_self LicensesComponent) CC_BY_NC_SA_4() []any {
var text string = _self.anp.I18N.Get([]string{
"Creative Commons Attribution-NonCommertial-ShareAlike 4.0 International (CC-SA-NC-BY 4.0)",
"cc_by_nc_sa_4",
}, nil, "")
return []any{"a", map[string]any{
"class": "license cc-by-nc-sa-4",
"href": "https://creativecommons.org/licenses/by-nc-sa/4.0/",
"target": "_blank",
"data_i18n": "cc_by_nc_sa_4",
"data_i18n_without": true,
"title": text,
}, [][]any{
{"span", map[string]any{"class": "text"}, text},
{"image", map[string]any{
"sources": []any{"https://licensebuttons.net/l/by-nc-sa/4.0/88x31.png"},
"data_i18n": "cc_by_nc_sa_4",
"data_i18n_without": true,
"alt": text,
}},
}}
}
func get_year(value any) string {
_float, _ := value.(float64)
return strconv.FormatInt(int64(_float), 10)
}
func (_self LicensesComponent) Copyright(year any, authors any, all_rights_reserved bool) []any {
var year_string string = ""
var authors_string string = ""
switch value := year.(type) {
case []any:
var l int = len(value)
switch l {
case 0:
break
case 1:
year_string = get_year(value)
case 2:
year_string = get_year(value[0]) + "-" + get_year(value[1])
default:
for i, subvalue := range value[0 : l-1] {
if i != 0 {
year_string += ", "
}
year_string += get_year(subvalue)
}
year_string += " & " + get_year(value[l-1])
}
case any:
year_string = strconv.FormatInt(Utils.Get[int64](value, nil), 10)
}
switch value := authors.(type) {
case string:
authors_string = value
case []string:
var l int = len(value)
switch l {
case 0:
break
case 1:
authors_string = value[0]
case 2:
authors_string = value[0] + " &" + value[1]
default:
for i, subvalue := range value[0 : l-1] {
if i != 0 {
authors_string += ", "
}
authors_string += subvalue
}
authors_string += " & " + value[l-1]
}
}
if year_string != "" {
year_string = " " + year_string + "."
}
if authors_string != "" {
authors_string = " " + authors_string + "."
}
var variables = map[string]any{
"year": year_string,
"authors": authors_string,
"all_rights_reserved": "",
}
if all_rights_reserved {
variables["all_rights_reserved"] = " " + _self.anp.I18N.Get([]string{
"All rights reserved.",
"all_rights_reserved",
}, nil, "")
}
var text string = _self.anp.I18N.Get([]string{
"© Copyright{year}{authors}{all_rights_reserved}",
"copyright",
}, variables, "")
return []any{"span", map[string]any{
"class": "license copyright",
"data_i18n": "copyriht",
"data_i18n_variables": variables,
"title": text,
}, text}
}