reinstate project-sorting support

This commit is contained in:
mappu 2020-05-02 15:19:58 +12:00
parent 9228ea88a9
commit a892dbb5b2
2 changed files with 42 additions and 6 deletions

32
main.go
View File

@ -10,6 +10,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"regexp" "regexp"
"sort"
"strings" "strings"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
@ -88,11 +89,32 @@ func (this *Application) Homepage(w http.ResponseWriter, r *http.Request) {
} }
} }
// Sort repos once alphabetically, to get alphabetical indexes...
sort.Slice(repos, func(i, j int) bool {
return repos[i].Name < repos[j].Name
})
alphabeticalOrderIndexes := make(map[string]int, len(repos))
for idx, repo := range repos {
alphabeticalOrderIndexes[repo.Name] = idx
}
// But then make sure the final sort is by most-recently-created
sort.Slice(repos, func(i, j int) bool {
return repos[i].CreatedAt.After(repos[j].CreatedAt)
})
// Ready for template // Ready for template
this.Templatepage(w, r, "", "", func() { this.Templatepage(w, r, "", "", func() {
fmt.Fprint(w, ` fmt.Fprint(w, `
`+this.cfg.Template.HomepageHeaderHTML+` `+this.cfg.Template.HomepageHeaderHTML+`
<select id="sortorder" style="float:right;">
<option value="data-sort-al">Alphabetical</option>
<option value="data-sort-ls">Lifespan</option>
<option value="data-sort-ct" selected>Recent projects</option>
<option value="data-sort-mt">Recent updates</option>
</select>
<h2>Projects</h2> <h2>Projects</h2>
<table id="projtable-main" class="projtable"> <table id="projtable-main" class="projtable">
`) `)
@ -120,7 +142,13 @@ func (this *Application) Homepage(w http.ResponseWriter, r *http.Request) {
} }
fmt.Fprint(w, ` fmt.Fprint(w, `
<tr class="`+html.EscapeString(rowClass)+`"> <tr
class="`+html.EscapeString(rowClass)+`"
data-sort-al="`+fmt.Sprintf("-%d", alphabeticalOrderIndexes[repo.Name])+`"
data-sort-ls="`+fmt.Sprintf("%.0f", repo.UpdatedAt.Sub(repo.CreatedAt).Seconds())+`"
data-sort-ct="`+fmt.Sprintf("%d", repo.CreatedAt.Unix())+`"
data-sort-mt="`+fmt.Sprintf("%d", repo.UpdatedAt.Unix())+`"
>
<td> <td>
<a href="`+pageHref+`"><img class="homeimage" src="`+html.EscapeString(`/:banner/`+url.PathEscape(repo.Name))+`"></div></a> <a href="`+pageHref+`"><img class="homeimage" src="`+html.EscapeString(`/:banner/`+url.PathEscape(repo.Name))+`"></div></a>
</td> </td>

View File

@ -52,7 +52,7 @@
}); });
// //
// Sort support (theme opt-in) // Sort support
// //
var sort_rows = function(cb) { var sort_rows = function(cb) {
@ -62,7 +62,7 @@
items.push([i, cb(tr[i])]); items.push([i, cb(tr[i])]);
} }
items.sort(function(a, b) { items.sort(function(a, b) {
return (a[1] - b[1]); return (b[1] - a[1]);
}); });
for (var i = 0, e = items.length; i !== e; ++i) { for (var i = 0, e = items.length; i !== e; ++i) {
var el = tr[items[i][0]]; var el = tr[items[i][0]];
@ -72,12 +72,20 @@
} }
}; };
var sort_update = function(sort_by) { var sortUpdate = function(sort_by) {
sort_rows(function(el) { sort_rows(function(el) {
return el.getAttribute(sort_by); return el.getAttribute(sort_by);
}); });
}; };
window.sortUpdate = sort_update; var so = document.getElementById('sortorder');
if (so) {
so.addEventListener('change', function() { sortUpdate(so.value); });
sortUpdate(so.value);
}
window.addEventListener("load", function() {
// back button support in webkit
sortUpdate(so.value);
});
})(); })();