aboutsummaryrefslogtreecommitdiff
path: root/viewer/src/services/navigation.ts
diff options
context:
space:
mode:
authorpacien2020-09-25 16:01:49 +0200
committerpacien2020-09-25 16:01:49 +0200
commite93f7b1eb84c083d67567115284c0002a3a7d5fc (patch)
tree8d373e8f7f571485e1330928f43b090ed004c525 /viewer/src/services/navigation.ts
parent8e3ac8fe44bebb38e1882ca7f06b8100078ad88d (diff)
parentfd542f75a1d94ee5f804d0925823276b97f38581 (diff)
downloadldgallery-e93f7b1eb84c083d67567115284c0002a3a7d5fc.tar.gz
Merge branch 'develop' for release v2.0v2.0
Diffstat (limited to 'viewer/src/services/navigation.ts')
-rw-r--r--viewer/src/services/navigation.ts41
1 files changed, 25 insertions, 16 deletions
diff --git a/viewer/src/services/navigation.ts b/viewer/src/services/navigation.ts
index fa17990..5b0716d 100644
--- a/viewer/src/services/navigation.ts
+++ b/viewer/src/services/navigation.ts
@@ -17,12 +17,23 @@
17-- along with this program. If not, see <https://www.gnu.org/licenses/>. 17-- along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/ 18*/
19 19
20import { ItemType } from "@/@types/ItemType";
21
20export default class Navigation { 22export default class Navigation {
23 static readonly ICON_BY_TYPE: Record<ItemType, string> = {
24 directory: "folder",
25 picture: "image",
26 plaintext: "file-alt",
27 pdf: "file-pdf",
28 video: "file-video",
29 audio: "file-audio",
30 other: "file",
31 };
21 32
22 // Searches for an item by path from a root item (navigation) 33 // Searches for an item by path from a root item (navigation)
23 public static searchCurrentItemPath(root: Gallery.Item, path: string): Gallery.Item[] { 34 public static searchCurrentItemPath(root: Gallery.Item, path: string): Gallery.Item[] {
24 if (path === root.path) return [root]; 35 if (path === root.path) return [root];
25 if (root.properties.type === "directory" && path.startsWith(root.path)) { 36 if (root.properties.type === ItemType.DIRECTORY && path.startsWith(root.path)) {
26 const itemChain = root.properties.items 37 const itemChain = root.properties.items
27 .map(item => this.searchCurrentItemPath(item, path)) 38 .map(item => this.searchCurrentItemPath(item, path))
28 .find(itemChain => itemChain.length > 0); 39 .find(itemChain => itemChain.length > 0);
@@ -40,14 +51,14 @@ export default class Navigation {
40 } 51 }
41 52
42 // Checks if the type of an item matches 53 // Checks if the type of an item matches
43 public static checkType(item: Gallery.Item | null, type: Gallery.ItemType): boolean { 54 public static checkType(item: Gallery.Item | null, type: ItemType | null): boolean {
44 return item?.properties.type === type ?? false; 55 return (item?.properties.type ?? null) === type;
45 } 56 }
46 57
47 public static getLastDirectory(itemPath: Gallery.Item[]): Gallery.Directory { 58 public static getLastDirectory(itemPath: Gallery.Item[]): Gallery.Directory {
48 for (let idx = itemPath.length - 1; idx >= 0; idx--) { 59 for (let idx = itemPath.length - 1; idx >= 0; idx--) {
49 const item = itemPath[idx]; 60 const item = itemPath[idx];
50 if (Navigation.checkType(item, "directory")) return item as Gallery.Directory; 61 if (Navigation.checkType(item, ItemType.DIRECTORY)) return item as Gallery.Directory;
51 } 62 }
52 throw new Error("No directory found"); 63 throw new Error("No directory found");
53 } 64 }
@@ -56,25 +67,23 @@ export default class Navigation {
56 public static directoriesFirst(items: Gallery.Item[]) { 67 public static directoriesFirst(items: Gallery.Item[]) {
57 return [ 68 return [
58 ...items 69 ...items
59 .filter(child => Navigation.checkType(child, "directory")) 70 .filter(child => Navigation.checkType(child, ItemType.DIRECTORY))
60 .sort((a, b) => a.title.localeCompare(b.title)), 71 .sort((a, b) => a.title.localeCompare(b.title)),
61 72
62 ...items 73 ...items.filter(child => !Navigation.checkType(child, ItemType.DIRECTORY)),
63 .filter(child => !Navigation.checkType(child, "directory")),
64 ]; 74 ];
65 } 75 }
66 76
67 // Get the icon for an item 77 // Get the icon for an item
68 public static getIcon(item: Gallery.Item): string { 78 public static getIcon(item: Gallery.Item): string {
69 if (item.path.length <= 1) return "home"; 79 if (item.path.length <= 1) return "home";
70 switch (item.properties.type) { 80 return Navigation.ICON_BY_TYPE[item.properties.type];
71 case "picture": 81 }
72 return "image"; 82
73 case "directory": 83 // Get the file name of an item, without its cache timestamp
74 return "folder"; 84 public static getFileName(item: Gallery.Item): string {
75 case "other": 85 if (item.properties.type === ItemType.DIRECTORY) return item.title;
76 default: 86 const timeStamped = item.properties.resource.split("/").pop() ?? "";
77 return "file"; 87 return timeStamped.split("?")[0];
78 }
79 } 88 }
80} 89}