package view import ( "api-cds-search/cmd/search" "net/http" "code.achtarmig.org/pas/ui/element" "code.achtarmig.org/pas/ui/element/button" "code.achtarmig.org/pas/ui/element/container" "code.achtarmig.org/pas/ui/element/form" "code.achtarmig.org/pas/ui/element/input" "code.achtarmig.org/pas/ui/element/option" "code.achtarmig.org/pas/ui/element/placeholder" "code.achtarmig.org/pas/ui/element/view" "code.achtarmig.org/pas/ui/route" ) func noSearch(v *view.Element) element.ElementInterface { cont := v.GetElement("SearchResultsContainer") cont.DeleteAllChildren() pl, _ := placeholder.New(cont, "", func(p *placeholder.Element) { p.Icon = option.IconLoupe p.Title = "Search CDS-Views" p.Subtitle = "Find CDS-Views from the Business Accelerator Hub" }) return pl } func searchView() { view.New("Search", func(p *view.Element) { route.Register("/search", p) p.Title = "CDS-Search" p.OnRequest = func(v *view.Element, w http.ResponseWriter, r *http.Request) view.ProcessElements { inputQuery := v.GetElement("q").(*input.Element) inputQueryValue, _ := v.GetElement("q").(*input.Element).GetDataText() searchTerm := search.GetSearchTerm(inputQueryValue) if inputQueryValue == "" { noSearch(v) } inputPage := v.GetElement("p").(*input.Element) inputMaxPage := v.GetElement("MaxPage").(*input.Element) currentPage, _ := inputPage.GetDataNumber() if inputQuery.GetDataChanged() && inputQueryValue != "" { buf, err := search.GetBuffer(searchTerm) if err != nil { search.BuildBuffer(searchTerm) buf, err = search.GetBuffer(searchTerm) } if err != nil { noResults(v) } else { currentPage, maxPage := search.GetPages(buf, currentPage) m := search.Search(buf, currentPage, maxPage) inputPage.Max = m.MaxPage inputMaxPage.Min = m.MaxPage inputMaxPage.Max = m.MaxPage inputMaxPage.SetData(m.MaxPage) results(v, m) } } return view.ProcessElements{ Partial: []element.ElementInterface{v.GetElement("SearchResultsContainer")}, OOBUpdate: []element.ElementInterface{inputPage, inputMaxPage}, } } container.New(p, "MainContainer", func(p *container.Element) { p.Justify = option.JustifyCenter form.New(p, "SearchForm", func(p *form.Element) { p.SetHTMX(element.HTMX{ Method: http.MethodGet, Target: "#SearchResultsContainer>*", Swap: "outerHTML", URL: "/search", PushURL: true, Params: "q, p", }) container.New(p, "SearchFormLinked", func(p *container.Element) { p.Justify = option.JustifyCenter p.LinkedHorizontal = true input.New(p, "q", input.TypeText, func(p *input.Element) { p.Placeholder = "Search here" p.Shape = option.ShapeRound p.MinWidth = 10 p.MaxWidth = 36 }) button.New(p, "SearchButton", func(p *button.Element) { p.Label = "Search" p.DisplayStyle = option.DisplayStyleSuggestedAction p.Shape = option.ShapeRound p.MaxWidth = 6 }) }) }) }) container.New(p, "CenterPager", func(p *container.Element) { p.Justify = option.JustifyCenter container.New(p, "Pager", func(p *container.Element) { p.MaxWidth = 42 p.MinWidth = 10 p.Justify = option.JustifyRight p.LinkedHorizontal = true input.New(p, "p", input.TypeNumber, func(p *input.Element) { p.Shape = option.ShapeRound p.SetMin = true p.Min = 0 p.SetMax = true p.Max = 0 p.MinWidth = 4 p.SetHTMX(element.HTMX{ Method: http.MethodGet, Include: "[name='q'], [name='p']", Target: "#SearchResultsContainer>*", URL: "/search", Swap: "outerHTML", PushURL: true, }) }) input.New(p, "MaxPage", input.TypeNumber, func(p *input.Element) { p.Disabled = true p.Shape = option.ShapeRound p.MinWidth = 4 p.SetMin = true p.SetMax = true }) }) }) container.New(p, "SearchResultsContainer", func(p *container.Element) { p.Justify = option.JustifyCenter }) }) }