2020-04-20 09:20:45 -05:00
|
|
|
|
package search
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sort"
|
2020-04-27 07:16:03 -05:00
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
2020-04-20 09:20:45 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2021-04-28 02:18:13 -05:00
|
|
|
|
SortAlphaAsc = SortOption{
|
2020-04-20 09:20:45 -05:00
|
|
|
|
Name: "alpha-asc",
|
2021-02-11 01:49:16 -06:00
|
|
|
|
DisplayName: "Alphabetically (A–Z)",
|
2020-04-20 09:20:45 -05:00
|
|
|
|
Description: "Sort results in an alphabetically ascending order",
|
2021-02-11 01:49:16 -06:00
|
|
|
|
Index: 0,
|
2020-05-06 04:42:52 -05:00
|
|
|
|
Filter: []SortOptionFilter{
|
|
|
|
|
searchstore.TitleSorter{},
|
|
|
|
|
},
|
2020-04-20 09:20:45 -05:00
|
|
|
|
}
|
2021-04-28 02:18:13 -05:00
|
|
|
|
SortAlphaDesc = SortOption{
|
2020-04-20 09:20:45 -05:00
|
|
|
|
Name: "alpha-desc",
|
2021-02-11 01:49:16 -06:00
|
|
|
|
DisplayName: "Alphabetically (Z–A)",
|
2020-04-20 09:20:45 -05:00
|
|
|
|
Description: "Sort results in an alphabetically descending order",
|
2021-02-11 01:49:16 -06:00
|
|
|
|
Index: 0,
|
2020-05-06 04:42:52 -05:00
|
|
|
|
Filter: []SortOptionFilter{
|
|
|
|
|
searchstore.TitleSorter{Descending: true},
|
|
|
|
|
},
|
2020-04-20 09:20:45 -05:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SortOption struct {
|
|
|
|
|
Name string
|
|
|
|
|
DisplayName string
|
|
|
|
|
Description string
|
2021-02-11 01:49:16 -06:00
|
|
|
|
Index int
|
|
|
|
|
MetaName string
|
2020-05-06 04:42:52 -05:00
|
|
|
|
Filter []SortOptionFilter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SortOptionFilter interface {
|
|
|
|
|
searchstore.FilterOrderBy
|
2020-04-20 09:20:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterSortOption allows for hooking in more search options from
|
|
|
|
|
// other services.
|
|
|
|
|
func (s *SearchService) RegisterSortOption(option SortOption) {
|
|
|
|
|
s.sortOptions[option.Name] = option
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SearchService) SortOptions() []SortOption {
|
|
|
|
|
opts := make([]SortOption, 0, len(s.sortOptions))
|
|
|
|
|
for _, o := range s.sortOptions {
|
|
|
|
|
opts = append(opts, o)
|
|
|
|
|
}
|
|
|
|
|
sort.Slice(opts, func(i, j int) bool {
|
2021-02-11 01:49:16 -06:00
|
|
|
|
return opts[i].Index < opts[j].Index || (opts[i].Index == opts[j].Index && opts[i].Name < opts[j].Name)
|
2020-04-20 09:20:45 -05:00
|
|
|
|
})
|
|
|
|
|
return opts
|
|
|
|
|
}
|