wp-go/app/theme/wp/components/widget/search.go

82 lines
2.7 KiB
Go
Raw Permalink Normal View History

2023-03-12 12:41:10 +00:00
package widget
2023-03-09 14:36:41 +00:00
import (
2023-03-28 13:45:20 +00:00
"fmt"
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/app/wpconfig"
2023-03-09 14:36:41 +00:00
"github.com/fthvgb1/wp-go/helper/html"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"strings"
)
var searchTemplate = `{$before_widget}
{$title}
{$form}
{$after_widget}`
var html5SearchForm = `<form role="search" {$aria_label} method="get" class="search-form" action="/">
<label>
<span class="screen-reader-text">{$label}</span>
<input type="search" class="search-field" placeholder="{$placeholder}" value="{$value}" name="s" />
</label>
<input type="submit" class="search-submit" value="{$button}" />
</form>`
var xmlSearchForm = `<form role="search" {$aria_label} method="get" id="searchform" class="searchform" action="/">
<div>
<label class="screen-reader-text" for="s">{$label}</label>
<input type="text" value="{$value}" name="s" id="s" />
<input type="submit" id="searchsubmit" value="{$button}" />
</div>
</form>`
2023-03-17 11:51:53 +00:00
func searchArgs() map[string]string {
return map[string]string{
2023-03-28 13:45:20 +00:00
"{$aria_label}": "",
"{$title}": "",
"{$form}": "",
"{$button}": "搜索",
"{$placeholder}": "搜索&hellip;",
"{$label}": "搜索:",
2023-03-17 11:51:53 +00:00
}
}
2023-05-09 05:44:20 +00:00
var form = html5SearchForm
2023-03-28 13:45:20 +00:00
func Search(h *wp.Handle, id string) string {
2023-03-27 04:59:29 +00:00
args := reload.GetAnyValBys("widget-search-args", h, func(h *wp.Handle) map[string]string {
search := searchArgs()
2023-03-28 13:45:20 +00:00
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
2023-03-27 04:59:29 +00:00
args := wp.GetComponentsArgs(h, widgets.Search, search)
2023-03-28 13:45:20 +00:00
args = maps.FilterZeroMerge(search, CommonArgs(), commonArgs, args)
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("search-", id), str.Join("widget widget_", "search"))
if args["{$title}"] == "" {
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "title")
}
if args["{$title}"] != "" {
args["{$title}"] = str.Join(args["{$before_title}"], args["{$title}"], args["{$after_title}"])
}
if args["{$form}"] != "" {
form = args["{$form}"]
delete(args, "{$form}")
}
if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
form = xmlSearchForm
}
2023-03-27 04:59:29 +00:00
return args
})
2023-03-28 13:45:20 +00:00
s := strings.ReplaceAll(searchTemplate, "{$form}", form)
val := ""
2023-03-12 06:25:22 +00:00
if h.Scene() == constraints.Search {
val = html.SpecialChars(h.Index.Param.Search)
2023-03-12 06:25:22 +00:00
}
s = strings.ReplaceAll(s, "{$value}", val)
return h.ComponentFilterFnHook(widgets.Search, str.Replace(s, args))
2023-03-09 14:36:41 +00:00
}