127 lines
3.1 KiB
Go
127 lines
3.1 KiB
Go
package component
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"management/internal/erpserver/model/dto"
|
|
|
|
"github.com/a-h/templ"
|
|
)
|
|
|
|
func TemplBtn(buttons []dto.OwnerMenuDto, searchBtn bool, actionNames ...string) templ.Component {
|
|
var b strings.Builder
|
|
b.WriteString(`<script type="text/html" id="toolbar">`)
|
|
b.WriteString(GenBtn(buttons, actionNames...))
|
|
|
|
if searchBtn {
|
|
b.WriteString(`
|
|
<button type="button" lay-event="search" lay-on="search" class="layui-btn layui-btn-primary layui-btn-sm">
|
|
<i class="layui-icon layui-icon-search"></i>
|
|
</button>`)
|
|
}
|
|
|
|
b.WriteString(`</script>`)
|
|
return templ.Raw(b.String())
|
|
}
|
|
|
|
func TemplLink(buttons []dto.OwnerMenuDto, actionNames ...string) templ.Component {
|
|
var b strings.Builder
|
|
b.WriteString(`<script type="text/html" id="actionBox">`)
|
|
b.WriteString(GenLink(buttons, actionNames...))
|
|
b.WriteString(`</script>`)
|
|
return templ.Raw(b.String())
|
|
}
|
|
|
|
func GenBtn(buttons []dto.OwnerMenuDto, actionNames ...string) string {
|
|
if len(buttons) == 0 {
|
|
return ""
|
|
}
|
|
|
|
if len(actionNames) == 0 {
|
|
return ""
|
|
}
|
|
|
|
var b strings.Builder
|
|
for _, action := range actionNames {
|
|
for _, btn := range buttons {
|
|
btn.Style = strings.ReplaceAll(btn.Style, "pear", "layui")
|
|
base := filepath.Base(btn.Url)
|
|
if base == action {
|
|
b.WriteString(`<button type="button" class="layui-btn ` + btn.Style + `" lay-event="` + firstLower(action) + `" lay-on="` + firstLower(action) + `">`)
|
|
if len(btn.Avatar) > 0 {
|
|
b.WriteString(`<i class="` + btn.Avatar + `"></i> `)
|
|
}
|
|
b.WriteString(btn.DisplayName)
|
|
b.WriteString(`</button>`)
|
|
}
|
|
}
|
|
}
|
|
|
|
return b.String()
|
|
}
|
|
|
|
func GenLink(buttons []dto.OwnerMenuDto, actionNames ...string) string {
|
|
if len(buttons) == 0 {
|
|
return ""
|
|
}
|
|
|
|
if len(actionNames) == 0 {
|
|
return ""
|
|
}
|
|
|
|
var b strings.Builder
|
|
for _, action := range actionNames {
|
|
for _, btn := range buttons {
|
|
btn.Style = strings.ReplaceAll(btn.Style, "pear", "layui")
|
|
base := filepath.Base(btn.Url)
|
|
if base == action {
|
|
b.WriteString(`<button type="button" style="font-size:12px !important;" class="layui-btn ` + btn.Style + `" lay-event="` + firstLower(action) + `" lay-on="` + firstLower(action) + `">`)
|
|
if len(btn.Avatar) > 0 {
|
|
b.WriteString(`<i class="` + btn.Avatar + `"></i> `)
|
|
}
|
|
b.WriteString(btn.DisplayName)
|
|
b.WriteString(`</button>`)
|
|
}
|
|
}
|
|
}
|
|
|
|
return b.String()
|
|
}
|
|
|
|
func SubmitBtn(buttons []dto.OwnerMenuDto, actionNames ...string) templ.Component {
|
|
if len(buttons) == 0 {
|
|
return templ.Raw("")
|
|
}
|
|
|
|
if len(actionNames) == 0 {
|
|
return templ.Raw("")
|
|
}
|
|
|
|
var b strings.Builder
|
|
for _, action := range actionNames {
|
|
for _, btn := range buttons {
|
|
btn.Style = strings.ReplaceAll(btn.Style, "pear", "layui")
|
|
base := filepath.Base(btn.Url)
|
|
if base == action {
|
|
b.WriteString(`<button type="submit" class="layui-btn ` + btn.Style + `" lay-submit lay-filter="` + firstLower(action) + `">`)
|
|
if len(btn.Avatar) > 0 {
|
|
b.WriteString(`<i class="` + btn.Avatar + `"></i> `)
|
|
}
|
|
b.WriteString(btn.DisplayName)
|
|
b.WriteString(`</button>`)
|
|
}
|
|
}
|
|
}
|
|
|
|
return templ.Raw(b.String())
|
|
}
|
|
|
|
func firstLower(s string) string {
|
|
if len(s) == 0 {
|
|
return s
|
|
}
|
|
|
|
return strings.ToLower(s[:1]) + s[1:]
|
|
}
|