From b909ec093591b50950c0de54b2005d471ca28116 Mon Sep 17 00:00:00 2001 From: Zero~Informatique Date: Sat, 12 Sep 2020 22:33:37 +0200 Subject: viewer: make default sort order configurable. code review improvements github: resolves #239 --- viewer/src/@types/gallery.d.ts | 2 +- viewer/src/components/LdCommandSort.vue | 28 +++++++++------------------- viewer/src/services/itemComparators.ts | 10 ++++++++++ viewer/src/store/uiStore.ts | 15 +++++---------- 4 files changed, 25 insertions(+), 30 deletions(-) (limited to 'viewer/src') diff --git a/viewer/src/@types/gallery.d.ts b/viewer/src/@types/gallery.d.ts index 8ef8fc7..a7f3d29 100644 --- a/viewer/src/@types/gallery.d.ts +++ b/viewer/src/@types/gallery.d.ts @@ -23,7 +23,7 @@ declare namespace Gallery { interface Config { galleryRoot: string; galleryIndex?: string; - initialSort?: ItemSortStr; + initialItemSort?: ItemSortStr; initialTagDisplayLimit?: number; } diff --git a/viewer/src/components/LdCommandSort.vue b/viewer/src/components/LdCommandSort.vue index a412afc..30644c1 100644 --- a/viewer/src/components/LdCommandSort.vue +++ b/viewer/src/components/LdCommandSort.vue @@ -19,42 +19,32 @@ --> diff --git a/viewer/src/services/itemComparators.ts b/viewer/src/services/itemComparators.ts index c8fedbe..380c66a 100644 --- a/viewer/src/services/itemComparators.ts +++ b/viewer/src/services/itemComparators.ts @@ -16,10 +16,20 @@ -- You should have received a copy of the GNU Affero General Public License -- along with this program. If not, see . */ +import { TranslateResult } from "vue-i18n"; +import i18n from "@/plugins/i18n"; export type ItemComparator = (left: Gallery.Item, right: Gallery.Item) => number; +export type ItemSort = { name: Gallery.ItemSortStr; text: TranslateResult; fn: ItemComparator }; export default class ItemComparators { + static readonly DEFAULT = ItemComparators.sortByNameAsc; + + 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 }, + ]; + static sortByNameAsc(left: Gallery.Item, right: Gallery.Item): number { return left.title.localeCompare(right.title); } diff --git a/viewer/src/store/uiStore.ts b/viewer/src/store/uiStore.ts index 04f14a0..84e7fed 100644 --- a/viewer/src/store/uiStore.ts +++ b/viewer/src/store/uiStore.ts @@ -29,7 +29,7 @@ export default class UIStore extends VuexModule { fullscreen: boolean = false; fullWidth: boolean = window.innerWidth < Number(process.env.VUE_APP_FULLWIDTH_LIMIT); searchMode: boolean = false; - sortFn: ItemComparator = ItemComparators.sortByNameAsc; + sortFn: ItemComparator = ItemComparators.DEFAULT; // --- @@ -50,15 +50,10 @@ export default class UIStore extends VuexModule { } @action async initFromConfig(config: Gallery.Config) { - switch (config.initialSort ?? "") { - case "date_desc": - this.setSortFn(ItemComparators.sortByDateDesc); - break; - case "name_asc": - case "": - break; - default: - throw new Error("Unknown sort type: " + config.initialSort); + if (config.initialItemSort) { + const itemSort = ItemComparators.ITEM_SORTS.find(s => s.name == config.initialItemSort); + if (itemSort) this.setSortFn(itemSort.fn); + else throw new Error("Unknown sort type: " + config.initialItemSort); } } } -- cgit v1.2.3