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.ts71
1 files changed, 71 insertions, 0 deletions
diff --git a/viewer/src/services/navigation.ts b/viewer/src/services/navigation.ts
new file mode 100644
index 0000000..77fa47a
--- /dev/null
+++ b/viewer/src/services/navigation.ts
@@ -0,0 +1,71 @@
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
35 // Normalize a string to lowercase, no-accents
36 public static normalize(value: string) {
37 return value
38 .normalize("NFD")
39 .replace(/[\u0300-\u036f]/g, "")
40 .toLowerCase();
41 }
42
43
44 public static checkType(item: Gallery.Item | null, type: Gallery.ItemType): boolean {
45 return item?.properties.type === type ?? false;
46 }
47
48 public static directoriesFirst(items: Gallery.Item[]) {
49 return [
50 ...items
51 .filter(child => Navigation.checkType(child, "directory"))
52 .sort((a, b) => a.title.localeCompare(b.title)),
53
54 ...items
55 .filter(child => !Navigation.checkType(child, "directory")),
56 ];
57 }
58
59 public static getIcon(item: Gallery.Item): string {
60 if (item.path.length <= 1) return "home";
61 switch (item.properties.type) {
62 case "picture":
63 return "image";
64 case "directory":
65 return "folder";
66 case "other":
67 default:
68 return "file";
69 }
70 }
71}