Add reversible column sorting, with sort direction indicators
This commit is contained in:
parent
eed41aebbb
commit
a910aac946
@ -139,17 +139,27 @@ func (m *BookmarkManager) Search(opts SearchOptions) ([]entity.Bookmark, error)
|
|||||||
bhQuery = bolthold.Query(*bhQuery.And("Tags").ContainsAll(bolthold.Slice(opts.Tags)...))
|
bhQuery = bolthold.Query(*bhQuery.And("Tags").ContainsAll(bolthold.Slice(opts.Tags)...))
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Sort == "title" {
|
reverse := false
|
||||||
bhQuery.SortBy("Info.Title")
|
sortOrder := opts.Sort
|
||||||
} else if opts.Sort == "created" {
|
if sortOrder != "" && sortOrder[0] == '-' {
|
||||||
bhQuery.SortBy("TimestampCreated")
|
reverse = true
|
||||||
} else if opts.Sort == "scraped" {
|
sortOrder = sortOrder[1:]
|
||||||
bhQuery.SortBy("TimestampLastScraped")
|
}
|
||||||
|
|
||||||
|
if sortOrder == "title" {
|
||||||
|
bhQuery.SortBy("Info.Title")
|
||||||
|
} else if sortOrder == "created" {
|
||||||
|
bhQuery.SortBy("TimestampCreated")
|
||||||
|
} else if sortOrder == "scraped" {
|
||||||
|
bhQuery.SortBy("TimestampLastScraped")
|
||||||
} else {
|
} else {
|
||||||
bhQuery.SortBy("ID")
|
bhQuery.SortBy("ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if reverse {
|
||||||
|
bhQuery = *bhQuery.Reverse()
|
||||||
|
}
|
||||||
|
|
||||||
out := []entity.Bookmark{}
|
out := []entity.Bookmark{}
|
||||||
err := m.db.store.ForEach(&bhQuery,
|
err := m.db.store.ForEach(&bhQuery,
|
||||||
func(bm *entity.Bookmark) error {
|
func(bm *entity.Bookmark) error {
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
<table id="manage-results">
|
<table id="manage-results">
|
||||||
<tr>
|
<tr>
|
||||||
<th> </th>
|
<th> </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>tags</th>
|
||||||
<th hx-post="/manage/results?sort=created" hx-target="#manage-results" class="show-for-large">created</th>
|
{{ template "manage_results_column_header.html" .column.created }}
|
||||||
<th hx-post="/manage/results?sort=scraped" hx-target="#manage-results" class="show-for-large">scraped</th>
|
{{ template "manage_results_column_header.html" .column.scraped }}
|
||||||
</tr>
|
</tr>
|
||||||
{{ range .bookmarks }}
|
{{ range .bookmarks }}
|
||||||
<tr>
|
<tr>
|
||||||
|
3
web/templates/manage_results_column_header.html
Normal file
3
web/templates/manage_results_column_header.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
<th hx-post="/manage/results?sort={{ .URLString }}" hx-target="#manage-results">{{ .Name }} {{ .TitleArrow }}
|
||||||
|
</th>
|
51
web/web.go
51
web/web.go
@ -42,6 +42,28 @@ type Server struct {
|
|||||||
bmm *db.BookmarkManager
|
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.
|
// Create creates a new web server instance and sets up routing.
|
||||||
func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
|
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})
|
allBookmarks, _ := bmm.Search(db.SearchOptions{Query: query, Tags: tags, Sort: sort})
|
||||||
meta := gin.H{"config": config, "bookmarks": allBookmarks}
|
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,
|
c.HTML(http.StatusOK,
|
||||||
"manage_results.html", meta,
|
"manage_results.html", meta,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user