diff options
Diffstat (limited to 'viewer/src/store/galleryStore.ts')
-rw-r--r-- | viewer/src/store/galleryStore.ts | 51 |
1 files changed, 8 insertions, 43 deletions
diff --git a/viewer/src/store/galleryStore.ts b/viewer/src/store/galleryStore.ts index e7d70f8..d2b28dd 100644 --- a/viewer/src/store/galleryStore.ts +++ b/viewer/src/store/galleryStore.ts | |||
@@ -18,7 +18,8 @@ | |||
18 | */ | 18 | */ |
19 | 19 | ||
20 | import { createModule, mutation, action } from "vuex-class-component"; | 20 | import { createModule, mutation, action } from "vuex-class-component"; |
21 | import Tools from '@/tools'; | 21 | import IndexFactory from '@/services/indexfactory'; |
22 | import Navigation from '@/services/navigation'; | ||
22 | 23 | ||
23 | const VuexModule = createModule({ | 24 | const VuexModule = createModule({ |
24 | namespaced: "galleryStore", | 25 | namespaced: "galleryStore", |
@@ -29,7 +30,7 @@ export default class GalleryStore extends VuexModule { | |||
29 | 30 | ||
30 | config: Gallery.Config | null = null; | 31 | config: Gallery.Config | null = null; |
31 | galleryItemsRoot: Gallery.Item | null = null; | 32 | galleryItemsRoot: Gallery.Item | null = null; |
32 | tags: Tag.Index = {}; | 33 | tagsIndex: Tag.Index = {}; |
33 | currentPath: string = "/"; | 34 | currentPath: string = "/"; |
34 | 35 | ||
35 | // --- | 36 | // --- |
@@ -42,8 +43,8 @@ export default class GalleryStore extends VuexModule { | |||
42 | this.galleryItemsRoot = galleryItemsRoot; | 43 | this.galleryItemsRoot = galleryItemsRoot; |
43 | } | 44 | } |
44 | 45 | ||
45 | @mutation private setTags(tags: Tag.Index) { | 46 | @mutation private setTagsIndex(tagsIndex: Tag.Index) { |
46 | this.tags = tags; | 47 | this.tagsIndex = tagsIndex; |
47 | } | 48 | } |
48 | 49 | ||
49 | @mutation setCurrentPath(currentPath: string) { | 50 | @mutation setCurrentPath(currentPath: string) { |
@@ -53,7 +54,7 @@ export default class GalleryStore extends VuexModule { | |||
53 | get currentItemPath(): Gallery.Item[] { | 54 | get currentItemPath(): Gallery.Item[] { |
54 | const root = this.galleryItemsRoot; | 55 | const root = this.galleryItemsRoot; |
55 | if (root) | 56 | if (root) |
56 | return GalleryStore.searchCurrentItemPath(root, this.currentPath); | 57 | return Navigation.searchCurrentItemPath(root, this.currentPath); |
57 | return []; | 58 | return []; |
58 | } | 59 | } |
59 | 60 | ||
@@ -67,7 +68,7 @@ export default class GalleryStore extends VuexModule { | |||
67 | // Fetches the gallery's JSON config | 68 | // Fetches the gallery's JSON config |
68 | @action async fetchConfig() { | 69 | @action async fetchConfig() { |
69 | return fetch(`${process.env.VUE_APP_DATA_URL}config.json`, { cache: "no-cache" }) | 70 | return fetch(`${process.env.VUE_APP_DATA_URL}config.json`, { cache: "no-cache" }) |
70 | .then(config => config.json()) | 71 | .then(response => response.json()) |
71 | .then(this.setConfig); | 72 | .then(this.setConfig); |
72 | } | 73 | } |
73 | 74 | ||
@@ -82,43 +83,7 @@ export default class GalleryStore extends VuexModule { | |||
82 | 83 | ||
83 | // Indexes the gallery | 84 | // Indexes the gallery |
84 | @action async indexTags() { | 85 | @action async indexTags() { |
85 | const root = this.galleryItemsRoot; | 86 | this.setTagsIndex(IndexFactory.generateTags(this.galleryItemsRoot)); |
86 | let index = {}; | ||
87 | if (root) GalleryStore.pushTagsForItem(index, root); | ||
88 | console.log("Index: ", index); | ||
89 | this.setTags(index); | ||
90 | } | ||
91 | |||
92 | // --- | ||
93 | |||
94 | // Pushes all tags for a root item (and its children) to the index | ||
95 | private static pushTagsForItem(index: Tag.Index, item: Gallery.Item) { | ||
96 | console.log("IndexingTagsFor: ", item.path); | ||
97 | if (item.properties.type === "directory") { | ||
98 | item.properties.items.forEach(item => this.pushTagsForItem(index, item)); | ||
99 | return; // Directories are not indexed | ||
100 | } | ||
101 | for (const tag of item.tags) { | ||
102 | const parts = tag.split('.'); | ||
103 | let lastPart: string | null = null; | ||
104 | for (const part of parts) { | ||
105 | if (!index[part]) index[part] = { tag: part, tagfiltered: Tools.normalize(part), items: [], children: {} }; | ||
106 | if (!index[part].items.includes(item)) index[part].items.push(item); | ||
107 | if (lastPart) index[lastPart].children[part] = index[part]; | ||
108 | lastPart = part; | ||
109 | } | ||
110 | } | ||
111 | } | 87 | } |
112 | 88 | ||
113 | // Searches for an item by path from a root item (navigation) | ||
114 | private static searchCurrentItemPath(item: Gallery.Item, path: string): Gallery.Item[] { | ||
115 | if (path === item.path) return [item]; | ||
116 | if (item.properties.type === "directory" && path.startsWith(item.path)) { | ||
117 | const itemChain = item.properties.items | ||
118 | .map(item => this.searchCurrentItemPath(item, path)) | ||
119 | .find(itemChain => itemChain.length > 0); | ||
120 | if (itemChain) return [item, ...itemChain]; | ||
121 | } | ||
122 | return []; | ||
123 | } | ||
124 | } | 89 | } |