reinstate project-sorting support
This commit is contained in:
parent
9228ea88a9
commit
a892dbb5b2
30
main.go
30
main.go
@ -10,6 +10,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"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
|
||||
|
||||
this.Templatepage(w, r, "", "", func() {
|
||||
fmt.Fprint(w, `
|
||||
`+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>
|
||||
<table id="projtable-main" class="projtable">
|
||||
`)
|
||||
@ -120,7 +142,13 @@ func (this *Application) Homepage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
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>
|
||||
<a href="`+pageHref+`"><img class="homeimage" src="`+html.EscapeString(`/:banner/`+url.PathEscape(repo.Name))+`"></div></a>
|
||||
</td>
|
||||
|
@ -52,7 +52,7 @@
|
||||
});
|
||||
|
||||
//
|
||||
// Sort support (theme opt-in)
|
||||
// Sort support
|
||||
//
|
||||
|
||||
var sort_rows = function(cb) {
|
||||
@ -62,7 +62,7 @@
|
||||
items.push([i, cb(tr[i])]);
|
||||
}
|
||||
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) {
|
||||
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) {
|
||||
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);
|
||||
});
|
||||
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user