From ab97f64575f1b51bacced6ce1bc6bf22e0354615 Mon Sep 17 00:00:00 2001 From: zeroinformatique Date: Thu, 17 Sep 2020 15:51:42 +0200 Subject: viewer: sort by date ascending (#249) * viewer: sort by date ascending * viewer: sort by date ascending--- viewer/src/@types/gallery.d.ts | 2 +- viewer/src/locales/en.json | 1 + viewer/src/services/itemComparators.ts | 15 ++++++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) (limited to 'viewer') diff --git a/viewer/src/@types/gallery.d.ts b/viewer/src/@types/gallery.d.ts index a7f3d29..41ad5bb 100644 --- a/viewer/src/@types/gallery.d.ts +++ b/viewer/src/@types/gallery.d.ts @@ -18,7 +18,7 @@ */ declare namespace Gallery { - type ItemSortStr = "name_asc" | "date_desc"; + type ItemSortStr = "name_asc" | "date_asc" | "date_desc"; interface Config { galleryRoot: string; diff --git a/viewer/src/locales/en.json b/viewer/src/locales/en.json index ce5b8b2..41088da 100644 --- a/viewer/src/locales/en.json +++ b/viewer/src/locales/en.json @@ -4,6 +4,7 @@ "command.search": "Open/close search panel", "command.search.clear": "Clear", "command.search.search": "Search", + "command.sort.byDateAsc": "By date (oldest first)", "command.sort.byDateDesc": "By date (most recent first)", "command.sort.byNameAsc": "By name (A to Z)", "directory.no-results": "Empty directory", diff --git a/viewer/src/services/itemComparators.ts b/viewer/src/services/itemComparators.ts index 6a7c81c..64ce288 100644 --- a/viewer/src/services/itemComparators.ts +++ b/viewer/src/services/itemComparators.ts @@ -27,7 +27,12 @@ export default class ItemComparators { static readonly ITEM_SORTS: ItemSort[] = [ { name: "name_asc", text: i18n.t("command.sort.byNameAsc"), fn: ItemComparators.sortByNameAsc }, - { name: "date_desc", text: i18n.t("command.sort.byDateDesc"), fn: ItemComparators.sortByDateDesc }, + { name: "date_asc", text: i18n.t("command.sort.byDateAsc"), fn: ItemComparators.sortByDateAsc }, + { + name: "date_desc", + text: i18n.t("command.sort.byDateDesc"), + fn: ItemComparators.reverse(ItemComparators.sortByDateAsc), + }, ]; static sortByNameAsc(left: Gallery.Item, right: Gallery.Item): number { @@ -38,7 +43,11 @@ export default class ItemComparators { }); } - static sortByDateDesc(left: Gallery.Item, right: Gallery.Item): number { - return -left.datetime.localeCompare(right.datetime); // TODO: handle timezones + static sortByDateAsc(left: Gallery.Item, right: Gallery.Item): number { + return left.datetime.localeCompare(right.datetime); // TODO: handle timezones + } + + static reverse(fn: ItemComparator): ItemComparator { + return (l, r) => -fn(l, r); } } -- cgit v1.2.3