App.vue 8.98 KB
Newer Older
Mathias Goebel's avatar
Mathias Goebel committed
1
2
<template>
  <div id="q-app">
3
    <q-layout view="hHh lpr fFf">
4
      <Header v-if="config.headers.all"
5
        :collectiontitle="collectiontitle"
6
        :config="config"
7
        :imageurl="imageurl"
8
        :itemlabel="itemlabel"
9
10
11
12
13
14
        :itemurls="itemurls"
        :manifests="manifests"
      />

      <q-page-container>
        <router-view
15
          :annotations="annotations"
16
          :collection="collection"
17
          :config="config"
18
          :contenturl="contenturl"
schneider210's avatar
schneider210 committed
19
          :fontsize="fontsize"
20
          :imageurl="imageurl"
21
          :itemlabel="itemlabel"
22
          :labels="config.labels"
23
          :language="itemlanguage"
24
25
26
27
28
          :manifests="manifests"
          :request="request"
          :tree="tree"
        />
      </q-page-container>
29

30
      <Footer :standalone="config.standalone" />
31
    </q-layout>
Mathias Goebel's avatar
Mathias Goebel committed
32
33
34
35
  </div>
</template>

<script>
schneider210's avatar
schneider210 committed
36
37
import Header from '@/components/header.vue';
import Footer from '@/components/footer.vue';
38

Mathias Goebel's avatar
Mathias Goebel committed
39
export default {
40
41
42
  name: 'Viewer',
  components: {
    Header,
43
    Footer,
44
45
46
  },
  data() {
    return {
47
      annotations: {},
48
      collection: {},
49
50
      collectiontitle: '',
      contenturl: '',
51
      config: {},
schneider210's avatar
schneider210 committed
52
      fontsize: 14,
53
      imageurl: '',
54
      isCollection: false,
55
      itemlabel: '',
56
      itemlanguage: '',
57
58
59
60
61
62
63
64
      itemurl: '',
      itemurls: [],
      label: '',
      manifests: [],
      tree: [],
    };
  },
  methods: {
schneider210's avatar
schneider210 committed
65
66
67
68
69
70
71
72
73
74
    /**
      * get resources using JavaScript's native fetch api
      * caller: *getCollection()*, *getItemData()*, *getManifest()*
      *         *@/components/content.vue::getSupport()*, *@/components/content.vue::created-hook*
      *
      * @param string url
      * @param string responsetype
      *
      * @return promise data
      */
75
76
77
78
79
80
    async request(url, responsetype = 'json') {
      const response = await fetch(url);
      const data = await (responsetype === 'text' ? response.text() : response.json());

      return data;
    },
schneider210's avatar
schneider210 committed
81
82
83
84
85
86
87
88
    /**
      * get collection data according to 'entrypoint'
      * (number of requests equal the number of manifests contained within a collection)
      * initialize the tree's root node
      * caller: *init()*
      *
      * @param string url
      */
89
    getCollection(url) {
90
91
      this.isCollection = true;

92
93
94
95
96
      this.request(url)
        .then((data) => {
          this.collection = data;
          this.label = this.getLabel(data);

97
98
99
100
101
          this.request(data.annotationCollection)
            .then((annotations) => {
              this.annotations = annotations.annotationCollection;
            });

102
103
104
105
106
107
108
109
110
111
112
          this.tree.push(
            {
              children: [],
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
              label: this.label,
              'label-key': this.label,
              selectable: false,
            },
          );
113
114
115
116
117
118

          if (Array.isArray(data.sequence)) {
            data.sequence.forEach((seq) => this.getManifest(seq.id));
          }
        });
    },
schneider210's avatar
schneider210 committed
119
120
121
122
    /**
      * get config object (JSON) from index.html
      * caller: *created-hook*
      */
123
124
125
    getConfig() {
      this.config = JSON.parse(document.getElementById('emo-config').text);
    },
schneider210's avatar
schneider210 committed
126
127
128
129
130
131
    /**
      * fetch all data provided on 'item level'
      * caller: *mounted-hook*, *getManifest()*
      *
      * @param string url
      */
132
    getItemData(url) {
133
134
      this.request(url)
        .then((data) => {
135
          this.collectiontitle = data.title;
136

137
          this.contenturl = data.content;
138
          this.imageurl = data.image && data.image.id ? data.image.id : '';
139
          this.itemlabel = data.n ? data.n : 'No itemlabel :(';
140
141
          // eslint-disable-next-line prefer-destructuring
          this.itemlanguage = data['x-langString'].split(',')[0];
142
143
        });
    },
schneider210's avatar
schneider210 committed
144
145
146
147
148
149
150
    /**
      * caller: *getItemUrls()*
      *
      * @param string nodelabel
      *
      * @return number idx
      */
151
152
153
154
155
156
157
158
159
    getItemIndex(nodelabel) {
      let idx = 0;
      this.itemurls.forEach((item, index) => {
        if (item === nodelabel) {
          idx = index;
        }
      });
      return idx;
    },
schneider210's avatar
schneider210 committed
160
161
162
163
164
165
166
167
168
    /**
      * get all itemurls hosted by each manifest's sequence to populate the aprropriate tree node
      * caller: *getManifest()*
      *
      * @param array sequence
      * @param string label
      *
      * @return array urls
      */
169
170
    getItemUrls(sequence, label) {
      const urls = [];
171

172
      sequence.forEach((obj) => {
173
174
        const pagelabel = this.getPageLabel(obj.id);

175
176
177
        urls.push(
          {
            label: obj.id,
178
            'label-key': `${this.config.labels.item} ${pagelabel}`,
179
180
181
182
            handler: (node) => {
              if (this.itemurl === node.label) {
                return;
              }
schneider210's avatar
schneider210 committed
183
184
185
              // node.label === itemurl
              // @param label === manifest label; passed by getManifest()
              this.$root.$emit('update-item', node.label, this.getSequenceIndex(label));
186
              this.$root.$emit('update-item-index', this.getItemIndex(node.label));
187
              this.$root.$emit('update-sequence-index', this.getSequenceIndex(label));
188
189
190
191
192
193
            },
          },
        );
      });
      return urls;
    },
schneider210's avatar
schneider210 committed
194
195
196
197
198
199
200
201
    /**
      * get the collection label, if provided; otherwise get the manifest label
      * caller: *getCollection()*, *getManifest()*
      *
      * @param object data
      *
      * @return string 'label'
      */
202
203
204
205
206
207
    getLabel(data) {
      if (Object.keys(this.collection).length) {
        return data.title && data.title[0].title ? data.title[0].title : data.label;
      }
      return data.label ? data.label : 'Manifest <small>(No label available)</small>';
    },
schneider210's avatar
schneider210 committed
208
209
210
211
212
213
    /**
      * get all the data provided on 'manifest level'
      * caller: *init()*, *getCollection()*
      *
      * @param string url
      */
214
215
216
    getManifest(url) {
      this.request(url)
        .then((data) => {
217
218
219
220
221
          // if the entrypoint points to a single manifest, initialize the tree
          if (this.isCollection === false) {
            this.tree.push({ label: '', 'label-key': this.config.labels.manifest, children: [] });
          }

222
223
224
225
226
227
228
229
230
231
          if (!Array.isArray(data.sequence)) {
            data.sequence = [data.sequence];
          }

          if (data.sequence[0] !== 'undefined') {
            data.sequence.map((seq) => this.itemurls.push(seq.id));
          }
          this.manifests.push(data);

          this.tree[0].children.push(
232
233
            {
              children: this.getItemUrls(data.sequence, data.label),
234
235
236
237
238
239
              label: data.label,
              'label-key': data.label,
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
              selectable: false,
240
            },
241
242
243
244
245
246
247
248
          );

          if (!this.label) {
            this.label = this.getLabel(data);
          }
          // make sure that urls are set just once on init
          if (!this.itemurl && data.sequence[0]) {
            this.itemurl = data.sequence[0].id;
249
            this.getItemData(data.sequence[0].id);
250
251
252
          }
        });
    },
schneider210's avatar
schneider210 committed
253
254
255
256
257
258
259
260
    /**
      * extract the 'label part' of the itemurl
      * caller: *getItemUrls()*
      *
      * @param string itemurl
      *
      * @return string 'label part'
      */
261
262
263
    getPageLabel(itemurl) {
      return itemurl.replace(/.*-(.*)\/latest.*$/, '$1');
    },
schneider210's avatar
schneider210 committed
264
265
266
267
268
269
270
    /**
      * caller: *getItemUrls()*
      *
      * @param string label
      *
      * @return number index
      */
271
    getSequenceIndex(label) {
272
273
      let index = 0;
      this.manifests.forEach((manifest, idx) => {
274
        if (manifest.label === label) {
275
276
277
278
279
          index = idx;
        }
      });
      return index;
    },
schneider210's avatar
schneider210 committed
280
281
282
283
284
285
    /**
      * decide whether to start with a collection or a single manifest
      * caller: *created-hook*
      *
      * @return function getCollection() | getManifest()
      */
286
287
288
289
290
291
292
293
294
295
296
297
298
    init() {
      return this.config.entrypoint.match(/collection.json\s?$/)
        ? this.getCollection(this.config.entrypoint)
        : this.getManifest(this.config.entrypoint);
    },
  },
  created() {
    this.getConfig();
    this.init();

    this.itemurls.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
  },
  mounted() {
schneider210's avatar
schneider210 committed
299
300
301
302
303
304
305
    /**
      * listen to fontsize change (user interaction). emitted in @/components/content.vue
      * in- or rather decrease fontsize of the text by 1px
      * default fontsize: 14px
      *
      * @param number fontsize
      */
306
307
308
    this.$root.$on('update-fontsize', (fontsize) => {
      this.fontsize = fontsize;
    });
schneider210's avatar
schneider210 committed
309
310
311
312
313
314
    /**
      * listen to item change (user interaction).
      * emitted in: *getItemurls*; handler for tree nodes. fired on user interaction
      *
      * @param string url
      */
315
316
317
318
    this.$root.$on('update-item', (url) => {
      this.itemurl = url;
      this.$router.push({ query: { itemurl: url } });
      // NOTE: Set imageurl to an empty string. Otherwise, if there is no corresponding image,
319
      // the "preceding" image according to the "preceding" item will be shown.
320
      this.imageurl = '';
321
      this.getItemData(url);
322
323
    });
  },
Mathias Goebel's avatar
Mathias Goebel committed
324
325
};
</script>