From b61a9899585d1baac4ef8ae5296e256e9317e972 Mon Sep 17 00:00:00 2001 From: Eric Volpert Date: Sat, 29 Jul 2017 01:57:44 -0500 Subject: [PATCH] Last Modified Sort (#174) * Last Modified sorting * Goimported the file so travis doesn't complain. --- assets/src/components/Listing.vue | 29 ++++++++++++++++++++++++----- file.go | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/assets/src/components/Listing.vue b/assets/src/components/Listing.vue index c2d69e24..3abddcf6 100644 --- a/assets/src/components/Listing.vue +++ b/assets/src/components/Listing.vue @@ -20,12 +20,14 @@ {{ nameIcon }}

-

+

Size {{ sizeIcon }}

- -

Last modified

+

+ Last modified + {{ modifiedIcon }} +

@@ -86,6 +88,12 @@ export default { nameSorted () { return (this.req.sort === 'name') }, + sizeSorted () { + return (this.req.sort === 'size') + }, + modifiedSorted () { + return (this.req.sort === 'modified') + }, ascOrdered () { return (this.req.order === 'asc') }, @@ -97,7 +105,14 @@ export default { return 'arrow_downward' }, sizeIcon () { - if (!this.nameSorted && this.ascOrdered) { + if (this.sizeSorted && this.ascOrdered) { + return 'arrow_downward' + } + + return 'arrow_upward' + }, + modifiedIcon () { + if (this.modifiedSorted && this.ascOrdered) { return 'arrow_downward' } @@ -275,10 +290,14 @@ export default { if (this.nameIcon === 'arrow_upward') { order = 'asc' } - } else { + } else if (sort === 'size') { if (this.sizeIcon === 'arrow_upward') { order = 'asc' } + } else if (sort === 'modified') { + if (this.modifiedIcon === 'arrow_upward') { + order = 'asc' + } } let path = this.$store.state.baseURL diff --git a/file.go b/file.go index 4030229f..675744f2 100644 --- a/file.go +++ b/file.go @@ -328,6 +328,8 @@ func (l listing) ApplySort() { sort.Sort(sort.Reverse(byName(l))) case "size": sort.Sort(sort.Reverse(bySize(l))) + case "modified": + sort.Sort(sort.Reverse(byModified(l))) default: // If not one of the above, do nothing return @@ -338,6 +340,8 @@ func (l listing) ApplySort() { sort.Sort(byName(l)) case "size": sort.Sort(bySize(l)) + case "modified": + sort.Sort(byModified(l)) default: sort.Sort(byName(l)) return @@ -348,6 +352,7 @@ func (l listing) ApplySort() { // Implement sorting for listing type byName listing type bySize listing +type byModified listing // By Name func (l byName) Len() int { @@ -392,6 +397,20 @@ func (l bySize) Less(i, j int) bool { return iSize < jSize } +// By Modified +func (l byModified) Len() int { + return len(l.Items) +} + +func (l byModified) Swap(i, j int) { + l.Items[i], l.Items[j] = l.Items[j], l.Items[i] +} + +func (l byModified) Less(i, j int) bool { + iModified, jModified := l.Items[i].ModTime, l.Items[j].ModTime + return iModified.Sub(jModified) < 0 +} + var textExtensions = [...]string{ ".md", ".markdown", ".mdown", ".mmark", ".asciidoc", ".adoc", ".ad",