Add reversible column sorting, with sort direction indicators

This commit is contained in:
Justin Hawkins 2022-06-07 19:51:56 +09:30
parent eed41aebbb
commit a910aac946
4 changed files with 73 additions and 9 deletions

View File

@ -139,17 +139,27 @@ func (m *BookmarkManager) Search(opts SearchOptions) ([]entity.Bookmark, error)
bhQuery = bolthold.Query(*bhQuery.And("Tags").ContainsAll(bolthold.Slice(opts.Tags)...))
}
if opts.Sort == "title" {
bhQuery.SortBy("Info.Title")
} else if opts.Sort == "created" {
bhQuery.SortBy("TimestampCreated")
} else if opts.Sort == "scraped" {
bhQuery.SortBy("TimestampLastScraped")
reverse := false
sortOrder := opts.Sort
if sortOrder != "" && sortOrder[0] == '-' {
reverse = true
sortOrder = sortOrder[1:]
}
if sortOrder == "title" {
bhQuery.SortBy("Info.Title")
} else if sortOrder == "created" {
bhQuery.SortBy("TimestampCreated")
} else if sortOrder == "scraped" {
bhQuery.SortBy("TimestampLastScraped")
} else {
bhQuery.SortBy("ID")
}
if reverse {
bhQuery = *bhQuery.Reverse()
}
out := []entity.Bookmark{}
err := m.db.store.ForEach(&bhQuery,
func(bm *entity.Bookmark) error {

View File

@ -2,10 +2,10 @@
<table id="manage-results">
<tr>
<th>&nbsp;</th>
<th hx-post="/manage/results?sort=title" hx-target="#manage-results">title/url</th>
{{ template "manage_results_column_header.html" .column.title }}
<th>tags</th>
<th hx-post="/manage/results?sort=created" hx-target="#manage-results" class="show-for-large">created</th>
<th hx-post="/manage/results?sort=scraped" hx-target="#manage-results" class="show-for-large">scraped</th>
{{ template "manage_results_column_header.html" .column.created }}
{{ template "manage_results_column_header.html" .column.scraped }}
</tr>
{{ range .bookmarks }}
<tr>

View File

@ -0,0 +1,3 @@
<th hx-post="/manage/results?sort={{ .URLString }}" hx-target="#manage-results">{{ .Name }}&nbsp;{{ .TitleArrow }}
</th>

View File

@ -42,6 +42,28 @@ type Server struct {
bmm *db.BookmarkManager
}
type ColumnInfo struct {
Name string
Param string
Sorted string
}
func (c ColumnInfo) URLString() string {
if c.Sorted == "asc" {
return "-" + c.Param
}
return c.Param
}
func (c ColumnInfo) TitleArrow() string {
if c.Sorted == "asc" {
return "↑"
} else if c.Sorted == "desc" {
return "↓"
}
return ""
}
// Create creates a new web server instance and sets up routing.
func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
@ -100,6 +122,35 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
allBookmarks, _ := bmm.Search(db.SearchOptions{Query: query, Tags: tags, Sort: sort})
meta := gin.H{"config": config, "bookmarks": allBookmarks}
colTitle := &ColumnInfo{Name: "Title/URL", Param: "title"}
colCreated := &ColumnInfo{Name: "Created", Param: "created"}
colScraped := &ColumnInfo{Name: "Scraped", Param: "scraped"}
if sort == "title" {
colTitle.Sorted = "asc"
}
if sort == "-title" {
colTitle.Sorted = "desc"
}
if sort == "scraped" {
colScraped.Sorted = "asc"
}
if sort == "-scraped" {
colScraped.Sorted = "desc"
}
if sort == "created" {
colCreated.Sorted = "asc"
}
if sort == "-created" {
colCreated.Sorted = "desc"
}
cols := gin.H{
"title": colTitle,
"created": colCreated,
"scraped": colScraped,
}
meta["column"] = cols
c.HTML(http.StatusOK,
"manage_results.html", meta,
)