From 26210d495aed813baac1095b5c7a7c7879d2d206 Mon Sep 17 00:00:00 2001 From: Zero~Informatique Date: Fri, 25 Sep 2020 10:42:33 +0200 Subject: viewer: refactor how the available sorts are stored github: resolves #259 --- viewer/src/components/LdCommandSort.vue | 13 ++++++------- viewer/src/components/LdGallery.vue | 2 +- viewer/src/services/itemComparators.ts | 17 +++++++---------- viewer/src/store/uiStore.ts | 12 ++++++------ 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/viewer/src/components/LdCommandSort.vue b/viewer/src/components/LdCommandSort.vue index 30644c1..cfaa5c1 100644 --- a/viewer/src/components/LdCommandSort.vue +++ b/viewer/src/components/LdCommandSort.vue @@ -23,8 +23,8 @@ - - + + {{ sort.text }} @@ -32,19 +32,18 @@ diff --git a/viewer/src/components/LdGallery.vue b/viewer/src/components/LdGallery.vue index 0c0a43c..edd479c 100644 --- a/viewer/src/components/LdGallery.vue +++ b/viewer/src/components/LdGallery.vue @@ -36,7 +36,7 @@ export default class LdPicture extends Vue { @Prop(String) readonly noresult?: string; get sortedItems() { - return this.items.sort(this.$uiStore.sortFn); + return this.items.sort(this.$uiStore.sort.fn); } get hasNoResults(): boolean { diff --git a/viewer/src/services/itemComparators.ts b/viewer/src/services/itemComparators.ts index 82757ca..bd9accb 100644 --- a/viewer/src/services/itemComparators.ts +++ b/viewer/src/services/itemComparators.ts @@ -20,28 +20,25 @@ 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 type ItemSort = { text: TranslateResult; fn: ItemComparator }; export default class ItemComparators { - static readonly ITEM_SORTS: ItemSort[] = [ - { - name: "title_asc", + static readonly ITEM_SORTS: Record = { + title_asc: { text: i18n.t("command.sort.byTitleAsc"), fn: ItemComparators.chain(ItemComparators.sortByTitleAsc, ItemComparators.sortByPathAsc), }, - { - name: "date_asc", + date_asc: { text: i18n.t("command.sort.byDateAsc"), fn: ItemComparators.chain(ItemComparators.sortByDateAsc, ItemComparators.sortByPathAsc), }, - { - name: "date_desc", + date_desc: { text: i18n.t("command.sort.byDateDesc"), fn: ItemComparators.reverse(ItemComparators.chain(ItemComparators.sortByDateAsc, ItemComparators.sortByPathAsc)), }, - ]; + }; - static readonly DEFAULT = ItemComparators.ITEM_SORTS[1].fn; + static readonly DEFAULT = ItemComparators.ITEM_SORTS.date_asc; static sortByPathAsc(left: Gallery.Item, right: Gallery.Item): number { return left.path.localeCompare(right.path, undefined, { diff --git a/viewer/src/store/uiStore.ts b/viewer/src/store/uiStore.ts index 84e7fed..f065cdd 100644 --- a/viewer/src/store/uiStore.ts +++ b/viewer/src/store/uiStore.ts @@ -18,7 +18,7 @@ */ import { createModule, mutation, action } from "vuex-class-component"; -import ItemComparators, { ItemComparator } from "@/services/itemComparators"; +import ItemComparators, { ItemSort } from "@/services/itemComparators"; const VuexModule = createModule({ namespaced: "uiStore", @@ -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.DEFAULT; + sort: ItemSort = ItemComparators.DEFAULT; // --- @@ -45,14 +45,14 @@ export default class UIStore extends VuexModule { this.searchMode = value ?? !this.searchMode; } - @mutation setSortFn(sortFn: ItemComparator) { - this.sortFn = sortFn; + @mutation setSort(sort: ItemSort) { + this.sort = sort; } @action async initFromConfig(config: Gallery.Config) { if (config.initialItemSort) { - const itemSort = ItemComparators.ITEM_SORTS.find(s => s.name == config.initialItemSort); - if (itemSort) this.setSortFn(itemSort.fn); + const itemSort = ItemComparators.ITEM_SORTS[config.initialItemSort]; + if (itemSort) this.setSort(itemSort); else throw new Error("Unknown sort type: " + config.initialItemSort); } } -- cgit v1.2.3