aboutsummaryrefslogtreecommitdiff
path: root/viewer/src/services/navigation.ts
diff options
context:
space:
mode:
Diffstat (limited to 'viewer/src/services/navigation.ts')
-rw-r--r--viewer/src/services/navigation.ts80
1 files changed, 80 insertions, 0 deletions
diff --git a/viewer/src/services/navigation.ts b/viewer/src/services/navigation.ts
new file mode 100644
index 0000000..fa17990
--- /dev/null
+++ b/viewer/src/services/navigation.ts
@@ -0,0 +1,80 @@
1/* ldgallery - A static generator which turns a collection of tagged
2-- pictures into a searchable web gallery.
3--
4-- Copyright (C) 2019-2020 Guillaume FOUET
5--
6-- This program is free software: you can redistribute it and/or modify
7-- it under the terms of the GNU Affero General Public License as
8-- published by the Free Software Foundation, either version 3 of the
9-- License, or (at your option) any later version.
10--
11-- This program is distributed in the hope that it will be useful,
12-- but WITHOUT ANY WARRANTY; without even the implied warranty of
13-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-- GNU Affero General Public License for more details.
15--
16-- You should have received a copy of the GNU Affero General Public License
17-- along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20export default class Navigation {
21
22 // Searches for an item by path from a root item (navigation)
23 public static searchCurrentItemPath(root: Gallery.Item, path: string): Gallery.Item[] {
24 if (path === root.path) return [root];
25 if (root.properties.type === "directory" && path.startsWith(root.path)) {
26 const itemChain = root.properties.items
27 .map(item => this.searchCurrentItemPath(item, path))
28 .find(itemChain => itemChain.length > 0);
29 if (itemChain) return [root, ...itemChain];
30 }
31 return [];
32 }
33
34 // Normalize a string to lowercase, no-accents
35 public static normalize(value: string) {
36 return value
37 .normalize("NFD")
38 .replace(/[\u0300-\u036f]/g, "")
39 .toLowerCase();
40 }
41
42 // Checks if the type of an item matches
43 public static checkType(item: Gallery.Item | null, type: Gallery.ItemType): boolean {
44 return item?.properties.type === type ?? false;
45 }
46
47 public static getLastDirectory(itemPath: Gallery.Item[]): Gallery.Directory {
48 for (let idx = itemPath.length - 1; idx >= 0; idx--) {
49 const item = itemPath[idx];
50 if (Navigation.checkType(item, "directory")) return item as Gallery.Directory;
51 }
52 throw new Error("No directory found");
53 }
54
55 // Sort a list of items, moving the directories to the beginning of the list
56 public static directoriesFirst(items: Gallery.Item[]) {
57 return [
58 ...items
59 .filter(child => Navigation.checkType(child, "directory"))
60 .sort((a, b) => a.title.localeCompare(b.title)),
61
62 ...items
63 .filter(child => !Navigation.checkType(child, "directory")),
64 ];
65 }
66
67 // Get the icon for an item
68 public static getIcon(item: Gallery.Item): string {
69 if (item.path.length <= 1) return "home";
70 switch (item.properties.type) {
71 case "picture":
72 return "image";
73 case "directory":
74 return "folder";
75 case "other":
76 default:
77 return "file";
78 }
79 }
80}