aboutsummaryrefslogtreecommitdiff
path: root/viewer/src/store/galleryStore.ts
blob: e2adf18d4729e26e3980053bc3b06a31f4d9f96f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* ldgallery - A static generator which turns a collection of tagged
--             pictures into a searchable web gallery.
--
-- Copyright (C) 2019-2020  Guillaume FOUET
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import { Config, Index, Item } from "@/@types/gallery";
import { TagCategory, TagIndex, TagSearch } from "@/@types/tag";
import IndexFactory from "@/services/indexfactory";
import Navigation from "@/services/navigation";
import { action, createModule, mutation } from "vuex-class-component";

const VuexModule = createModule({
  namespaced: "galleryStore",
  strict: true,
});

export default class GalleryStore extends VuexModule {
  config: Config | null = null;
  galleryIndex: Index | null = null;
  tagsIndex: TagIndex = {};
  tagsCategories: TagCategory[] = [];
  currentPath: string | null = null;
  currentSearch: TagSearch[] = [];

  // ---

  @mutation private setConfig(config: Config) {
    this.config = config;
  }

  @mutation setGalleryIndex(galleryIndex: Index) {
    this.galleryIndex = Object.freeze(galleryIndex);
  }

  @mutation private setTagsIndex(tagsIndex: TagIndex) {
    this.tagsIndex = Object.freeze(tagsIndex);
  }

  @mutation private setTagsCategories(tagsCategories: TagCategory[]) {
    this.tagsCategories = tagsCategories;
  }

  @mutation setCurrentPath(currentPath: string) {
    this.currentPath = currentPath;
  }

  @mutation setCurrentSearch(currentSearch: TagSearch[]) {
    this.currentSearch = currentSearch;
  }

  // ---

  get currentItemPath(): Item[] {
    const root = this.galleryIndex?.tree;
    if (root && this.currentPath) return Navigation.searchCurrentItemPath(root, this.currentPath);
    return [];
  }

  get currentItem(): Item | null {
    const path = this.currentItemPath;
    return path.length > 0 ? path[path.length - 1] : null;
  }

  get galleryTitle(): string {
    return this.galleryIndex?.properties.galleryTitle ?? "ldgallery";
  }

  get resourceRoot(): string {
    return process.env.VUE_APP_DATA_URL + this.config!.galleryRoot;
  }

  // ---

  // Fetches the gallery's JSON config
  @action async fetchConfig() {
    await fetch(`${process.env.VUE_APP_DATA_URL}${GalleryStore.getUrlConfig()}`, { cache: "no-cache" })
      .then(GalleryStore.responseToJson)
      .then(this.setConfig);
    return this.config!;
  }

  // Fetches the gallery's JSON metadata
  @action async fetchGalleryItems() {
    const root = this.config?.galleryRoot ?? "";
    const index = this.config?.galleryIndex ?? "index.json";
    await fetch(`${process.env.VUE_APP_DATA_URL}${root}${index}`, { cache: "no-cache" })
      .then(GalleryStore.responseToJson)
      .then(this.setGalleryIndex)
      .then(this.indexTags)
      .then(this.indexTagCategories);
    return this.galleryIndex!;
  }

  // Indexes the gallery
  @action async indexTags() {
    const root = this.galleryIndex?.tree ?? null;
    const index = IndexFactory.generateTags(root);
    this.setTagsIndex(index);
    return index;
  }

  // Indexes the proposed categories
  @action async indexTagCategories() {
    const categories = IndexFactory.generateCategories(this.tagsIndex, this.galleryIndex?.properties.tagCategories);
    this.setTagsCategories(categories);
    return categories;
  }

  // Searches for tags
  @action async search(filters: string[]) {
    const results = filters.flatMap(filter => IndexFactory.searchTags(this.tagsIndex, filter, true));
    this.setCurrentSearch(results);
    return results;
  }

  private static getUrlConfig() {
    const search = window.location.search;
    if (search.length > 1) return search.substr(1) + ".json";
    return "config.json";
  }

  private static responseToJson(response: Response) {
    if (!response.ok) throw new Error(`${response.status}: ${response.statusText}`);
    return response.json();
  }
}