App.vue 11.8 KB
Newer Older
Mathias Goebel's avatar
Mathias Goebel committed
1
<template>
2
3
  <q-layout
    id="q-app"
4
    class="root viewport"
5
6
7
8
    view="hHh Lpr fFf"
    class="app"
    style="height: 100vh;"
  >
9
10
11
12
13
14
15
16
17
    <Header
      v-if="config.headers.all"
      :collectiontitle="collectiontitle"
      :config="config"
      :imageurl="imageurl"
      :item="item"
      :itemurls="itemurls"
      :manifests="manifests"
      :panels="panels"
18
19
      :projectcolors="config.colors"
      :standalone="config.standalone"
20
    />
21

22
23
    <q-page-container class="root">
      <router-view
24
        :annotations="annotations"
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
        :collection="collection"
        :config="config"
        :contenttypes="contentTypes"
        :contenturls="contenturls"
        :fontsize="fontsize"
        :imageurl="imageurl"
        :item="item"
        :labels="config.labels"
        :manifests="manifests"
        :panels="panels"
        :request="request"
        :tree="tree"
      />
    </q-page-container>
  </q-layout>
Mathias Goebel's avatar
Mathias Goebel committed
40
41
42
</template>

<script>
schneider210's avatar
schneider210 committed
43
import { colors } from 'quasar';
44
import Header from '@/components/header.vue';
45
import Panels from '@/mixins/panels';
46

Mathias Goebel's avatar
Mathias Goebel committed
47
export default {
48
  name: 'TIDO',
49
50
51
  components: {
    Header,
  },
52
  mixins: [Panels],
53
54
  data() {
    return {
55
      annotations: [],
56
      collection: {},
57
      collectiontitle: '',
58
      config: {},
59
      contentTypes: [],
60
      contenturls: [],
schneider210's avatar
schneider210 committed
61
      fontsize: 14,
62
      imageurl: '',
63
      isCollection: false,
64
      item: {},
65
66
67
68
69
70
      itemurl: '',
      itemurls: [],
      manifests: [],
      tree: [],
    };
  },
nwindis's avatar
nwindis committed
71
72
73
74
75
  created() {
    this.getConfig();
    this.init();
    this.itemurls.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));

schneider210's avatar
schneider210 committed
76
77
    this.$q.dark.set('auto');

nwindis's avatar
nwindis committed
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
    if (this.config.colors.primary && this.config.colors.secondary && this.config.colors.accent) {
      colors.setBrand('primary', this.config.colors.primary);
      colors.setBrand('secondary', this.config.colors.secondary);
      colors.setBrand('accent', this.config.colors.accent);
    }
  },
  mounted() {
    /**
      * 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
      */
    this.$root.$on('update-fontsize', (fontsize) => {
      this.fontsize = fontsize;
    });
    this.$root.$on('panels-position', (newPanels) => {
      this.panels = newPanels;
    });
    /**
      * listen to item change (user interaction).
      * emitted in: *getItemurls*; handler for tree nodes. fired on user interaction
      *
      * @param string url
      */
    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,
      // the "preceding" image according to the "preceding" item will be shown.
      this.imageurl = '';
      this.getItemData(url);
    });
  },
113
  methods: {
schneider210's avatar
schneider210 committed
114
115
116
117
118
119
120
121
122
123
    /**
      * 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
      */
124
125
126
127
128
129
    async request(url, responsetype = 'json') {
      const response = await fetch(url);
      const data = await (responsetype === 'text' ? response.text() : response.json());

      return data;
    },
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    /**
    * filter the annotation IDs and the matching text part of the current item
    *
    * caller: *getAnnotations()*
    *
    * @param array annotations
    *
    * @return array identifiers
    */
    filterAnnotations(annotations) {
      const identifiers = [];

      annotations.forEach((annotation) => {
        const id = this.getAnnotationId(annotation);
        const text = this.getAnnotationText(id);

        identifiers.push({
          id,
          contenttype: annotation.body['x-content-type'],
          description: annotation.body.value,
          selected: this.config.annotations.show,
          text,
        });
      });

      return identifiers;
    },

    /**
    * get the annotation id/s of the current item
    *
    * caller: *filterAnnotations()*
    *
    * @param object annotation
    *
    * @return string
    */
    getAnnotationId(annotation) {
      const split = annotation.target.id.split('/');

      return split[split.length - 1];
    },
    /**
      * match annotation against it's text counterpart
      * caller: *filterAnnotations()*
      *
      * @param string id
      *
      * @return string
      */
    getAnnotationText(id) {
      const element = document.getElementById(id);

      return element !== null
        ? element.innerText
        : false;
    },
    /**
      * get annotations of the current item
      * caller: *getItemData()*
      *
      * @param string url
      */
    getAnnotations(url) {
      this.request(url)
        .then((annotations) => {
          if (annotations.annotationCollection.first) {
            this.request(annotations.annotationCollection.first)
              .then((current) => {
                if (current.annotationPage.items && current.annotationPage.items.length) {
                  this.annotations = this.filterAnnotations(current.annotationPage.items);
                } else this.annotations = [];
              });
          }
        })
        .catch(() => {
          this.$q.notify({ message: 'No annotations available' });
        });
    },
schneider210's avatar
schneider210 committed
209
210
211
212
213
214
215
216
    /**
      * 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
      */
217
    getCollection(url) {
218
219
      this.isCollection = true;

220
221
222
      this.request(url)
        .then((data) => {
          this.collection = data;
223
          this.collectiontitle = this.getLabel(data);
224

225
226
227
228
229
230
          this.tree.push(
            {
              children: [],
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
231
232
              label: this.collectiontitle,
              'label-key': this.collectiontitle,
233
234
235
              selectable: false,
            },
          );
236
237
238
239
240
241

          if (Array.isArray(data.sequence)) {
            data.sequence.forEach((seq) => this.getManifest(seq.id));
          }
        });
    },
schneider210's avatar
schneider210 committed
242
243
244
245
    /**
      * get config object (JSON) from index.html
      * caller: *created-hook*
      */
246
    getConfig() {
247
      this.config = JSON.parse(document.getElementById('tido-config').text);
248
    },
249
    /**
250
      * filter all urls that match either of the MIME types "application/xhtml+xml" and "text/html"
251
252
253
254
255
256
      * caller: *getItemData()*
      *
      * @param string array
      *
      * @return array
      */
257
258
259
260
    getContentUrl(content) {
      const urls = [];

      if (Array.isArray(content) && content.length) {
261
262
        this.contentTypes = [];

263
264
265
        content.forEach((c) => {
          if (c.type.match(/(application\/xhtml\+xml|text\/html)/)) {
            urls.push(c.url);
266
267

            this.contentTypes.push(c.type.split('type=')[1]);
268
269
270
271
          }
        });
      }
      return urls;
272
    },
schneider210's avatar
schneider210 committed
273
274
275
276
277
278
    /**
      * fetch all data provided on 'item level'
      * caller: *mounted-hook*, *getManifest()*
      *
      * @param string url
      */
279
    getItemData(url) {
280
281
      this.request(url)
        .then((data) => {
282
          this.item = data;
283

284
          this.contenturls = this.getContentUrl(data.content);
285
          this.imageurl = data.image.id || '';
286
287
        });
    },
schneider210's avatar
schneider210 committed
288
289
290
291
292
293
294
    /**
      * caller: *getItemUrls()*
      *
      * @param string nodelabel
      *
      * @return number idx
      */
295
296
297
298
299
300
301
302
303
    getItemIndex(nodelabel) {
      let idx = 0;
      this.itemurls.forEach((item, index) => {
        if (item === nodelabel) {
          idx = index;
        }
      });
      return idx;
    },
304
305
306
307
308
309
310
311
312
313
314
    /**
      * extract the 'label part' of the itemurl
      * caller: *getItemUrls()*
      *
      * @param string itemurl
      *
      * @return string 'label part'
      */
    getItemLabel(itemurl) {
      return itemurl.replace(/.*-(.*)\/latest.*$/, '$1');
    },
schneider210's avatar
schneider210 committed
315
316
317
318
319
320
321
322
323
    /**
      * 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
      */
324
325
    getItemUrls(sequence, label) {
      const urls = [];
326

327
328
      sequence.forEach((item) => {
        const itemLabel = this.getItemLabel(item.id);
329

330
331
        urls.push(
          {
332
333
            label: item.id,
            'label-key': `${this.config.labels.item} ${itemLabel}`,
334
335
336
337
            handler: (node) => {
              if (this.itemurl === node.label) {
                return;
              }
schneider210's avatar
schneider210 committed
338
339
340
              // node.label === itemurl
              // @param label === manifest label; passed by getManifest()
              this.$root.$emit('update-item', node.label, this.getSequenceIndex(label));
341
              this.$root.$emit('update-item-index', this.getItemIndex(node.label));
342
              this.$root.$emit('update-sequence-index', this.getSequenceIndex(label));
343
344
345
346
347
348
            },
          },
        );
      });
      return urls;
    },
schneider210's avatar
schneider210 committed
349
350
351
352
353
354
355
356
    /**
      * get the collection label, if provided; otherwise get the manifest label
      * caller: *getCollection()*, *getManifest()*
      *
      * @param object data
      *
      * @return string 'label'
      */
357
358
359
360
361
362
    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
363
364
365
366
367
368
    /**
      * get all the data provided on 'manifest level'
      * caller: *init()*, *getCollection()*
      *
      * @param string url
      */
369
370
371
    getManifest(url) {
      this.request(url)
        .then((data) => {
372
373
374
375
376
          // 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: [] });
          }

377
378
379
380
381
382
383
384
385
386
          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(
387
388
            {
              children: this.getItemUrls(data.sequence, data.label),
389
390
391
392
393
394
              label: data.label,
              'label-key': data.label,
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
              selectable: false,
395
            },
396
397
398
399
          );
          // make sure that urls are set just once on init
          if (!this.itemurl && data.sequence[0]) {
            this.itemurl = data.sequence[0].id;
400
            this.getItemData(data.sequence[0].id);
401
402
403
          }
        });
    },
schneider210's avatar
schneider210 committed
404
405
406
407
408
409
410
    /**
      * caller: *getItemUrls()*
      *
      * @param string label
      *
      * @return number index
      */
411
    getSequenceIndex(label) {
412
413
      let index = 0;
      this.manifests.forEach((manifest, idx) => {
414
        if (manifest.label === label) {
415
416
417
418
419
          index = idx;
        }
      });
      return index;
    },
schneider210's avatar
schneider210 committed
420
421
422
423
424
425
    /**
      * decide whether to start with a collection or a single manifest
      * caller: *created-hook*
      *
      * @return function getCollection() | getManifest()
      */
426
427
428
429
430
431
432
    init() {
      return this.config.entrypoint.match(/collection.json\s?$/)
        ? this.getCollection(this.config.entrypoint)
        : this.getManifest(this.config.entrypoint);
    },
  },

Mathias Goebel's avatar
Mathias Goebel committed
433
434
};
</script>
435
436

<style scoped>
437
.root {
438
439
440
441
442
443
  display: flex;
  flex: 1;
  flex-direction: column;
  overflow: hidden;
}

Mathias Goebel's avatar
Mathias Goebel committed
444
.viewport {
445
  height: 100vh;
446
447
}
</style>