From 74c1c5e34787ac57299c8cbd874e9dcc56da406d Mon Sep 17 00:00:00 2001 From: Zero~Informatique Date: Fri, 22 May 2020 04:14:48 +0200 Subject: viewer: Enumerated item types --- viewer/src/@types/ItemType.ts | 11 +++++++++++ viewer/src/@types/gallery.d.ts | 27 +++++++++++++-------------- viewer/src/services/indexfactory.ts | 3 ++- viewer/src/services/navigation.ts | 34 +++++++++++++++------------------- viewer/src/views/GalleryNavigation.vue | 26 ++++++++++++++++++-------- 5 files changed, 59 insertions(+), 42 deletions(-) create mode 100644 viewer/src/@types/ItemType.ts diff --git a/viewer/src/@types/ItemType.ts b/viewer/src/@types/ItemType.ts new file mode 100644 index 0000000..31a395b --- /dev/null +++ b/viewer/src/@types/ItemType.ts @@ -0,0 +1,11 @@ +// TODO: Convert all ambiant types related to LdGallery to modules + +export enum ItemType { + OTHER = "other", + PICTURE = "picture", + PLAINTEXT = "plaintext", + PDF = "pdf", + VIDEO = "video", + AUDIO = "audio", + DIRECTORY = "directory", +} diff --git a/viewer/src/@types/gallery.d.ts b/viewer/src/@types/gallery.d.ts index 2407f98..151ae92 100644 --- a/viewer/src/@types/gallery.d.ts +++ b/viewer/src/@types/gallery.d.ts @@ -60,44 +60,44 @@ declare namespace Gallery { path: string, thumbnail?: Thumbnail properties: OtherProperties - | PictureProperties - | PlainTextProperties - | PDFProperties - | VideoProperties - | AudioProperties - | DirectoryProperties, + | PictureProperties + | PlainTextProperties + | PDFProperties + | VideoProperties + | AudioProperties + | DirectoryProperties, } interface Resolution { width: number, height: number, } interface OtherProperties { - type: "other", + type: import("./ItemType").ItemType.OTHER, resource: string } interface PictureProperties { - type: "picture", + type: import("./ItemType").ItemType.PICTURE, resource: string, resolution: Resolution } interface PlainTextProperties { - type: "plaintext", + type: import("./ItemType").ItemType.PLAINTEXT, resource: string, } interface PDFProperties { - type: "pdf", + type: import("./ItemType").ItemType.PDF, resource: string, } interface VideoProperties { - type: "video", + type: import("./ItemType").ItemType.VIDEO, resource: string, } interface AudioProperties { - type: "audio", + type: import("./ItemType").ItemType.AUDIO, resource: string, } interface DirectoryProperties { - type: "directory", + type: import("./ItemType").ItemType.DIRECTORY, items: Item[] } interface Thumbnail { @@ -105,5 +105,4 @@ declare namespace Gallery { resolution: Resolution } type RawTag = string; - type ItemType = "other" | "picture" | "plaintext" | "pdf" | "video" | "audio" | "directory"; } diff --git a/viewer/src/services/indexfactory.ts b/viewer/src/services/indexfactory.ts index e402185..00abc05 100644 --- a/viewer/src/services/indexfactory.ts +++ b/viewer/src/services/indexfactory.ts @@ -18,6 +18,7 @@ */ import { Operation } from "@/@types/Operation"; +import { ItemType } from "@/@types/ItemType"; import Navigation from "@/services/navigation"; export default class IndexFactory { @@ -30,7 +31,7 @@ export default class IndexFactory { // Pushes all tags for a root item (and its children) to the index private static pushTagsForItem(tagsIndex: Tag.Index, item: Gallery.Item): void { - if (item.properties.type === "directory") { + if (item.properties.type === ItemType.DIRECTORY) { item.properties.items.forEach(item => this.pushTagsForItem(tagsIndex, item)); return; // Directories are not indexed } diff --git a/viewer/src/services/navigation.ts b/viewer/src/services/navigation.ts index 068d081..a7e752c 100644 --- a/viewer/src/services/navigation.ts +++ b/viewer/src/services/navigation.ts @@ -17,12 +17,14 @@ -- along with this program. If not, see . */ +import { ItemType } from "@/@types/ItemType"; + export default class Navigation { // Searches for an item by path from a root item (navigation) public static searchCurrentItemPath(root: Gallery.Item, path: string): Gallery.Item[] { if (path === root.path) return [root]; - if (root.properties.type === "directory" && path.startsWith(root.path)) { + if (root.properties.type === ItemType.DIRECTORY && path.startsWith(root.path)) { const itemChain = root.properties.items .map(item => this.searchCurrentItemPath(item, path)) .find(itemChain => itemChain.length > 0); @@ -40,14 +42,14 @@ export default class Navigation { } // Checks if the type of an item matches - public static checkType(item: Gallery.Item | null, type: Gallery.ItemType | null): boolean { + public static checkType(item: Gallery.Item | null, type: ItemType | null): boolean { return (item?.properties.type ?? null) === type; } public static getLastDirectory(itemPath: Gallery.Item[]): Gallery.Directory { for (let idx = itemPath.length - 1; idx >= 0; idx--) { const item = itemPath[idx]; - if (Navigation.checkType(item, "directory")) return item as Gallery.Directory; + if (Navigation.checkType(item, ItemType.DIRECTORY)) return item as Gallery.Directory; } throw new Error("No directory found"); } @@ -56,11 +58,11 @@ export default class Navigation { public static directoriesFirst(items: Gallery.Item[]) { return [ ...items - .filter(child => Navigation.checkType(child, "directory")) + .filter(child => Navigation.checkType(child, ItemType.DIRECTORY)) .sort((a, b) => a.title.localeCompare(b.title)), ...items - .filter(child => !Navigation.checkType(child, "directory")), + .filter(child => !Navigation.checkType(child, ItemType.DIRECTORY)), ]; } @@ -68,19 +70,13 @@ export default class Navigation { public static getIcon(item: Gallery.Item): string { if (item.path.length <= 1) return "home"; switch (item.properties.type) { - case "picture": - return "image"; - case "plaintext": - return "file-alt"; - case "pdf": - return "file-pdf"; - case "video": - return "file-video"; - case "audio": - return "file-audio"; - case "directory": - return "folder"; - case "other": + case ItemType.PICTURE: return "image"; + case ItemType.PLAINTEXT: return "file-alt"; + case ItemType.PDF: return "file-pdf"; + case ItemType.VIDEO: return "file-video"; + case ItemType.AUDIO: return "file-audio"; + case ItemType.DIRECTORY: return "folder"; + case ItemType.OTHER: default: return "file"; } @@ -88,7 +84,7 @@ export default class Navigation { // Get the file name of an item, without its cache timestamp public static getFileName(item: Gallery.Item): string { - if (item.properties.type === "directory") return item.title; + if (item.properties.type === ItemType.DIRECTORY) return item.title; const timeStamped = item.properties.resource.split("/").pop() ?? ""; return timeStamped.split("?")[0]; } diff --git a/viewer/src/views/GalleryNavigation.vue b/viewer/src/views/GalleryNavigation.vue index 9fc40e1..b141d44 100644 --- a/viewer/src/views/GalleryNavigation.vue +++ b/viewer/src/views/GalleryNavigation.vue @@ -21,19 +21,26 @@
- - - - - - - + + + + + + +