Skip to content

Commit 28b05a1

Browse files
committed
merge dev
2 parents 24ef6c6 + 87f0e87 commit 28b05a1

File tree

8 files changed

+169
-82
lines changed

8 files changed

+169
-82
lines changed

comp.go

+5
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,15 @@ func (a *App) Wrapper() comp.Wrapper { return icomp.NewW
181181
func (a *App) Number() comp.Number { return icomp.NewNumber() }
182182
func (a *App) Shape() comp.Shape { return icomp.NewShape() }
183183
func (a *App) ThemeSelect() comp.Select { return icomp.NewSelect().Themes(a.mux, a.Conf.Templ) }
184+
func (a *App) LocaleSelect() comp.Select { return icomp.NewSelect().Locales(a.mux, a.Conf.Templ) }
184185
func (a *App) ThemeButtonGroupSelect() comp.Select {
185186
return icomp.NewButtonGroupSelect().Themes(a.mux, a.Conf.Templ)
186187
}
187188

189+
func (a *App) LocaleButtonGroupSelect() comp.Select {
190+
return icomp.NewButtonGroupSelect().Locales(a.mux, a.Conf.Templ)
191+
}
192+
188193
func (a *App) AnchorNavSection() comp.AnchorNavSection { return icomp.NewAnchorNavSection() }
189194
func (a *App) AutoFillHeight() comp.AutoFillHeight { return icomp.NewAutoFillHeight() }
190195
func (a *App) AutoGenerateFilter() comp.AutoGenerateFilter { return icomp.NewAutoGenerateFilter() }

conf/config.go

+32-34
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,30 @@ package conf
33
import (
44
"net/http"
55

6+
"github.com/zrcoder/amisgo/internal/conf"
67
"github.com/zrcoder/amisgo/internal/template"
7-
"github.com/zrcoder/amisgo/theme"
88
)
99

10-
// Lang represents the language setting of the application
11-
type Lang string
12-
13-
// Supported languages
10+
// Available confs
1411
const (
15-
LangDefault Lang = LangEn
16-
LangZh Lang = "zh"
17-
LangEn Lang = "en"
12+
ThemeDefault = conf.ThemeDefault
13+
ThemeCxd = conf.ThemeCxd
14+
ThemeAntd = conf.ThemeAntd
15+
ThemeAng = conf.ThemeAng
16+
ThemeDark = conf.ThemeDark
17+
18+
LocaleDefault = conf.LocaleDefault
19+
LocaleZhCN = conf.LocalZhCN
20+
LocaleEnUS = conf.LocaleEnUS
21+
)
22+
23+
type (
24+
Theme = conf.Theme
25+
Locale = conf.Local
1826
)
1927

2028
// Config holds all configuration options for the application
2129
type Config struct {
22-
Lang Lang
2330
Title string
2431
Icon string
2532
CustomCSS string
@@ -36,7 +43,6 @@ func (c *Config) UseLocalSDK() bool {
3643
func Default() *Config {
3744
return &Config{
3845
Title: "amisgo",
39-
Lang: LangDefault,
4046
Templ: template.New(),
4147
}
4248
}
@@ -53,41 +59,33 @@ type Option func(*Config)
5359

5460
// WithTheme sets the UI theme.
5561
func WithTheme(name string) Option {
56-
return func(c *Config) {
57-
if len(c.Templ.GetThemes()) > 0 {
58-
return
59-
}
60-
c.Templ.Theme = theme.Theme{Value: name, Label: name}
61-
}
62+
return WithThemes(Theme{Value: name, Label: name})
6263
}
6364

6465
// WithThemes sets multiple UI themes, overriding an set theme via WithTheme.
65-
// Once themes are configured, you can use the comp.ThemeSelect or comp.ThemeButtonGroupSelect component in your pages to enable users to switch between the available themes.
66-
func WithThemes(themes ...theme.Theme) Option {
66+
func WithThemes(themes ...Theme) Option {
6767
return func(c *Config) {
68-
if len(themes) < 2 {
69-
panic("WithThemes: at least 2 themes are required")
68+
if len(themes) == 0 {
69+
panic("WithThemes: at least 1 theme is required")
7070
}
71-
regularThemes(themes)
72-
c.Templ.SetThemes(themes)
71+
conf.RegularThemes(themes)
72+
c.Templ.Themes = themes
7373
}
7474
}
7575

76-
func regularThemes(themes []theme.Theme) {
77-
for i := range themes {
78-
if themes[i].Value == "" {
79-
themes[i].Value = theme.Default
80-
}
81-
if themes[i].Label == "" {
82-
themes[i].Label = themes[i].Value
83-
}
84-
}
76+
// WithLocale sets the locale.
77+
func WithLocale(locale string) Option {
78+
return WithLocales(Locale{Value: locale, Label: locale})
8579
}
8680

87-
// WithLang sets the interface language.
88-
func WithLang(lang Lang) Option {
81+
// WithLocales sets multiple locales, overriding an set locale via WithLocale.
82+
func WithLocales(locales ...Locale) Option {
8983
return func(c *Config) {
90-
c.Lang = lang
84+
if len(locales) == 0 {
85+
panic("WithLocales: at least 1 lang is required")
86+
}
87+
conf.RegularLocales(locales)
88+
c.Templ.Locales = locales
9189
}
9290
}
9391

internal/comp/amis.go

+8
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@ func (a Amis) set(key string, value any) Amis {
1313
return a
1414
}
1515

16+
func (a Amis) Schema(value any) Amis {
17+
return a.set("schema", value)
18+
}
19+
1620
func (a Amis) Name(value string) Amis {
1721
return a.set("name", value)
1822
}
1923

2024
func (a Amis) Props(value any) Amis {
2125
return a.set("props", value)
2226
}
27+
28+
func (a Amis) Value(value any) Amis {
29+
return a.set("value", value)
30+
}

internal/comp/select.go

+29-5
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,17 @@ func NewButtonGroupSelect() Select {
2525
// Note: Ensure that `conf.WithThemes` is called during app initialization to avoid a panic.
2626
func (s Select) Themes(mux *http.ServeMux, templ *template.Templ) Select {
2727
const theme = "theme"
28-
themes := templ.GetThemes()
29-
if len(themes) == 0 {
28+
if len(templ.Themes) == 0 {
3029
panic("ThemeSelect: conf.WithThemes must be called during app initialization")
3130
}
3231
url := servemux.ServeQuery(mux, func(m map[string]string) error {
33-
templ.Theme = templ.GetTheme(m[theme])
32+
templ.UpdateTheme(m[theme])
3433
return nil
3534
}, theme)
3635
return s.Name(theme).Source(servemux.ServeData(mux, func() (any, error) {
3736
return schema.SuccessResponse(" ", schema.Schema{
38-
"value": templ.Theme.Value,
39-
"options": themes,
37+
"value": templ.CurrentTheme(),
38+
"options": templ.Themes,
4039
}), nil
4140
})).OnEvent(
4241
NewEvent().Change(
@@ -47,6 +46,31 @@ func (s Select) Themes(mux *http.ServeMux, templ *template.Templ) Select {
4746
)
4847
}
4948

49+
// Localesmakes the select for locale selection.
50+
// Note: Ensure that `conf.WithLocales` is called during app initialization to avoid a panic.
51+
func (s Select) Locales(mux *http.ServeMux, templ *template.Templ) Select {
52+
const locale = "locale"
53+
if len(templ.Locales) == 0 {
54+
panic("LocaleSelect: conf.WithLocales must be called during app initialization")
55+
}
56+
url := servemux.ServeQuery(mux, func(m map[string]string) error {
57+
templ.UpdateLocale(m[locale])
58+
return nil
59+
}, locale)
60+
return s.Name(locale).Source(servemux.ServeData(mux, func() (any, error) {
61+
return schema.SuccessResponse(" ", schema.Schema{
62+
"value": templ.CurrentLocale(),
63+
"options": templ.Locales,
64+
}), nil
65+
})).OnEvent(
66+
NewEvent().Change(
67+
NewEventActions(NewEventAction().ActionType("ajax").Api(url+"?"+locale+"=${event.data.value}"),
68+
NewEventAction().ActionType("refresh"),
69+
),
70+
),
71+
)
72+
}
73+
5074
// set sets the key-value pair in the selectControl instance
5175
func (sc Select) set(key string, value any) Select {
5276
sc[key] = value

internal/conf/conf.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package conf
2+
3+
const (
4+
// Available themes
5+
ThemeDefault = ThemeCxd
6+
ThemeCxd = "cxd"
7+
ThemeAntd = "antd"
8+
ThemeAng = "ang"
9+
ThemeDark = "dark"
10+
11+
// Supported languages
12+
LocaleDefault = LocaleEnUS
13+
LocalZhCN = "zh-CN"
14+
LocaleEnUS = "en-US"
15+
)
16+
17+
type Theme = option
18+
19+
type Local = option
20+
21+
type option struct {
22+
Value string `json:"value"`
23+
Label string `json:"label"`
24+
Dict any `json:"-"`
25+
}
26+
27+
func RegularThemes(themes []Theme) {
28+
for i := range themes {
29+
themes[i] = themes[i].regular(ThemeDefault)
30+
}
31+
}
32+
33+
func RegularLocales(langs []Local) {
34+
for i := range langs {
35+
langs[i] = langs[i].regular(LocaleDefault)
36+
}
37+
}
38+
39+
func (o option) regular(placeholder string) option {
40+
if o.Value == "" {
41+
o.Value = placeholder
42+
}
43+
if o.Label == "" {
44+
o.Label = o.Value
45+
}
46+
return o
47+
}

internal/template/template.go

+39-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
_ "embed"
55
"html/template"
66

7-
"github.com/zrcoder/amisgo/theme"
7+
"github.com/zrcoder/amisgo/internal/conf"
88
)
99

1010
const (
@@ -21,43 +21,64 @@ type Templ struct {
2121
AmisTempl *template.Template
2222
AmisVersion string
2323
AmisBaseURL string
24-
Theme theme.Theme
25-
themes []theme.Theme
24+
Themes []conf.Theme
25+
themeIndex int
26+
Locales []conf.Local
27+
localeIndex int
2628
}
2729

2830
func New() *Templ {
2931
res := &Templ{
3032
AmisVersion: amisVersion,
3133
AmisBaseURL: amisBaseURL,
32-
Theme: theme.Theme{Value: theme.Default, Label: theme.Default},
34+
Themes: []conf.Theme{{Value: conf.ThemeDefault, Label: conf.ThemeDefault}},
35+
Locales: []conf.Local{{Value: conf.LocaleDefault, Label: conf.LocaleDefault}},
3336
}
3437
funcs := template.FuncMap{
35-
"GetTheme": func() string {
36-
return res.Theme.Value
37-
},
38+
"CurrentTheme": res.CurrentTheme,
39+
"CurrentLocale": res.CurrentLocale,
40+
"CurrentI18n": res.CurrentI18n,
3841
}
3942
tmpl := template.New("").Funcs(funcs)
4043
tmpl = template.Must(tmpl.Parse(amisTempl))
4144
res.AmisTempl = tmpl
4245
return res
4346
}
4447

45-
func (t *Templ) SetThemes(ts []theme.Theme) {
46-
t.themes = ts
47-
if len(ts) > 0 {
48-
t.Theme = ts[0]
48+
func (t *Templ) UpdateTheme(name string) {
49+
for i, theme := range t.Themes {
50+
if theme.Value == name {
51+
t.themeIndex = i
52+
return
53+
}
4954
}
5055
}
5156

52-
func (t *Templ) GetThemes() []theme.Theme {
53-
return t.themes
57+
func (t *Templ) CurrentTheme() string {
58+
return t.Themes[t.themeIndex].Value
5459
}
5560

56-
func (t *Templ) GetTheme(name string) theme.Theme {
57-
for _, theme := range t.themes {
58-
if theme.Value == name {
59-
return theme
61+
func (t *Templ) SetLocales(ls []conf.Local) {
62+
t.Locales = ls
63+
}
64+
65+
func (t *Templ) GetLocales() []conf.Local {
66+
return t.Locales
67+
}
68+
69+
func (t *Templ) UpdateLocale(name string) {
70+
for i, locale := range t.Locales {
71+
if locale.Value == name {
72+
t.localeIndex = i
73+
return
6074
}
6175
}
62-
return theme.Theme{}
76+
}
77+
78+
func (t *Templ) CurrentLocale() string {
79+
return t.Locales[t.localeIndex].Value
80+
}
81+
82+
func (t *Templ) CurrentI18n() any {
83+
return t.Locales[t.localeIndex].Dict
6384
}

internal/template/template.gohtml

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE html>
2-
<html lang="{{ .Lang }}">
2+
3+
<html lang="{{ CurrentLocale }}">
34
<head>
45
<meta charset="UTF-8" />
56
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
@@ -14,9 +15,9 @@
1415
{{ end }}
1516

1617
{{ if .UseLocalSDK }}
17-
<link rel="stylesheet" href="/__amisgo__sdk/{{ GetTheme }}.css" />
18+
<link rel="stylesheet" href="/__amisgo__sdk/{{ CurrentTheme }}.css" />
1819
{{ else }}
19-
<link rel="stylesheet" href="{{ .AmisBaseURL }}@{{ .AmisVersion }}/sdk/{{ GetTheme }}.min.css" />
20+
<link rel="stylesheet" href="{{ .AmisBaseURL }}@{{ .AmisVersion }}/sdk/{{ CurrentTheme }}.min.css" />
2021
{{ end }}
2122

2223
{{ if .UseLocalSDK }}
@@ -58,14 +59,13 @@
5859
let amisScoped = amis.embed('#root',
5960
{{ .AmisJson }},
6061
{
61-
{{ if eq .Lang "en" }}
62-
locale: 'en-US'
63-
{{ end }}
62+
context: {
63+
i18n: {{ CurrentI18n }},
64+
},
65+
locale: {{ CurrentLocale }}
6466
},
6567
{
66-
{{ if GetTheme }}
67-
theme: '{{ GetTheme }}'
68-
{{ end }}
68+
theme: {{ CurrentTheme }}
6969
}
7070
);
7171
})();

theme/theme.go

-16
This file was deleted.

0 commit comments

Comments
 (0)