Search works
Search works well enough. Right now, you can only search via a single keyword at a time, and the search only filters based on the name, not the content. I believe we should implement a search tag feature on proclamations
This commit is contained in:
@@ -122,6 +122,17 @@ func (p *ProclamationOptions) Init(ctx *PageCtx) error {
|
||||
}
|
||||
p.allProclamations = []*FlatItem{}
|
||||
|
||||
for _, pr := range allProcs {
|
||||
fi := p.createFlatItem(pr.Name, pr.ID, false, 0)
|
||||
// Searching the name AND the content is returning too many options
|
||||
// I am removing this, for now, to narrow the search results.
|
||||
// I think a tag system should be implemented and search based on tags
|
||||
// and names, but not necessarily content.
|
||||
// fi.SearchText = strings.ToLower(pr.Name + " " + pr.Content)
|
||||
fi.SearchText = strings.ToLower(pr.Name)
|
||||
p.allProclamations = append(p.allProclamations, fi)
|
||||
}
|
||||
|
||||
// Load Root Categories
|
||||
var parentCategories []db.ProclamationCategory
|
||||
p.Context.DbPtr.Where("parent_id IS NULL OR parent_id = 0").Find(&parentCategories)
|
||||
@@ -129,19 +140,6 @@ func (p *ProclamationOptions) Init(ctx *PageCtx) error {
|
||||
p.allItems = append(p.allItems, p.createFlatItem(c.Name, fmt.Sprint(c.ID), true, 0))
|
||||
}
|
||||
|
||||
// Expand all root categories so allProclamations can be populated
|
||||
for i := 0; i < len(p.allItems); i++ {
|
||||
if p.allItems[i].IsCategory && !p.allItems[i].IsSubCategory {
|
||||
p.expandAndCollectProcs(i, procContentMap)
|
||||
p.allItems[i].Expanded = true
|
||||
itemScale2 := float32(32) / float32(ctx.CfgPtr.StandardFontSize)
|
||||
pureName := p.allItems[i].Label.content[4:]
|
||||
gl.DeleteTextures(1, &p.allItems[i].Label.ID)
|
||||
p.allItems[i].Label, _ = NewStringItem("[-] "+pureName, p.font, colors.White)
|
||||
p.allItems[i].Label.Scale(itemScale2)
|
||||
}
|
||||
}
|
||||
|
||||
p.layout()
|
||||
return nil
|
||||
}
|
||||
@@ -282,6 +280,7 @@ func (p *ProclamationOptions) expand(index int) {
|
||||
parentID, _ := strconv.Atoi(parent.ID)
|
||||
var newItems []*FlatItem
|
||||
|
||||
// Load subcategories
|
||||
var cats []db.ProclamationCategory
|
||||
p.Context.DbPtr.Where("parent_id = ?", parentID).Find(&cats)
|
||||
for _, c := range cats {
|
||||
@@ -290,12 +289,12 @@ func (p *ProclamationOptions) expand(index int) {
|
||||
newItems = append(newItems, fi)
|
||||
}
|
||||
|
||||
// Load proclamations
|
||||
var procs []db.Proclamation
|
||||
p.Context.DbPtr.Where("category_id = ?", parentID).Find(&procs)
|
||||
for _, pr := range procs {
|
||||
// Reuse the existing FlatItem pointer from allProclamations if available,
|
||||
// so search pool stays valid without duplication.
|
||||
var fi *FlatItem
|
||||
// Reuse existing proclamation from allProclamations if available
|
||||
for _, existing := range p.allProclamations {
|
||||
if existing.ID == pr.ID {
|
||||
fi = existing
|
||||
@@ -303,16 +302,20 @@ func (p *ProclamationOptions) expand(index int) {
|
||||
}
|
||||
}
|
||||
if fi == nil {
|
||||
// Safety fallback
|
||||
fi = p.createFlatItem(pr.Name, pr.ID, false, parent.Depth+1)
|
||||
fi.SearchText = strings.ToLower(pr.Name + " " + pr.Content)
|
||||
p.allProclamations = append(p.allProclamations, fi)
|
||||
}
|
||||
fi.Depth = parent.Depth + 1
|
||||
newItems = append(newItems, fi)
|
||||
}
|
||||
|
||||
// Insert new items after parent
|
||||
tail := append([]*FlatItem{}, p.allItems[index+1:]...)
|
||||
p.allItems = append(p.allItems[:index+1], append(newItems, tail...)...)
|
||||
|
||||
// Recurse into subcategories
|
||||
i := index + 1
|
||||
for i < len(p.allItems) && p.allItems[i].Depth > parent.Depth {
|
||||
if p.allItems[i].IsCategory && p.allItems[i].Depth > 0 {
|
||||
@@ -338,12 +341,16 @@ func (p *ProclamationOptions) collapse(index int) {
|
||||
|
||||
// matchesWordPrefix returns true if any word in text starts with query (case-insensitive).
|
||||
func matchesWordPrefix(text, query string) bool {
|
||||
q := strings.ToLower(query)
|
||||
words := strings.FieldsFunc(strings.ToLower(text), func(r rune) bool {
|
||||
query = strings.ToLower(query)
|
||||
text = strings.ToLower(text)
|
||||
|
||||
// Split text into words (non-alphanumeric boundaries)
|
||||
words := strings.FieldsFunc(text, func(r rune) bool {
|
||||
return !unicode.IsLetter(r) && !unicode.IsNumber(r)
|
||||
})
|
||||
for _, w := range words {
|
||||
if strings.HasPrefix(w, q) {
|
||||
|
||||
for _, word := range words {
|
||||
if strings.HasPrefix(word, query) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -424,7 +431,6 @@ func (p *ProclamationOptions) HandleEvent(event *sdl.Event) error {
|
||||
return nil
|
||||
}
|
||||
// Browse mode backspace — navigate back
|
||||
p.Context.NavigateTo(SETTINGS)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user