App.vue 10.6 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
    view="hHh Lpr fFf"
  >
7
8
9
10
11
12
13
14
15
    <Header
      v-if="config.headers.all"
      :collectiontitle="collectiontitle"
      :config="config"
      :imageurl="imageurl"
      :item="item"
      :itemurls="itemurls"
      :manifests="manifests"
      :panels="panels"
16
17
      :projectcolors="config.colors"
      :standalone="config.standalone"
18
    />
19

20
21
    <q-page-container class="root">
      <router-view
22
        :annotations="annotations"
23
24
25
        :collection="collection"
        :config="config"
        :contenttypes="contentTypes"
26
        :contenturls="contentUrls"
27
28
29
30
31
32
33
34
35
36
37
        :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
38
39
40
</template>

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

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

schneider210's avatar
schneider210 committed
74
75
    this.$q.dark.set('auto');

nwindis's avatar
nwindis committed
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
    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);
    });
  },
111
  methods: {
schneider210's avatar
schneider210 committed
112
113
114
115
116
117
118
119
120
121
    /**
      * 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
      */
122
123
124
125
126
127
    async request(url, responsetype = 'json') {
      const response = await fetch(url);
      const data = await (responsetype === 'text' ? response.text() : response.json());

      return data;
    },
128
129
130
131
132
133

    /**
      * get annotations of the current item
      * caller: *getItemData()*
      * @param string url
      */
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    async getAnnotations(url) {
      try {
        const annotations = await this.request(url);

        if (!annotations.annotationCollection.first) {
          this.annotations = [];
          return;
        }

        const current = await this.request(annotations.annotationCollection.first);

        if (current.annotationPage.items.length) {
          this.annotations = current.annotationPage.items;
        } else {
          this.annotations = [];
        }
      } catch (err) {
        this.annotations = [];
        this.$q.notify({ message: 'No annotations available' });
      }
154
    },
schneider210's avatar
schneider210 committed
155
156
157
158
159
160
161
162
    /**
      * 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
      */
163
    getCollection(url) {
164
165
      this.isCollection = true;

166
167
168
      this.request(url)
        .then((data) => {
          this.collection = data;
169
          this.collectiontitle = this.getLabel(data);
170

171
172
173
174
175
176
          this.tree.push(
            {
              children: [],
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
177
178
              label: this.collectiontitle,
              'label-key': this.collectiontitle,
179
180
181
              selectable: false,
            },
          );
182
183
184
185
186
187

          if (Array.isArray(data.sequence)) {
            data.sequence.forEach((seq) => this.getManifest(seq.id));
          }
        });
    },
schneider210's avatar
schneider210 committed
188
189
190
191
    /**
      * get config object (JSON) from index.html
      * caller: *created-hook*
      */
192
    getConfig() {
193
      this.config = JSON.parse(document.getElementById('tido-config').text);
194
    },
195
    /**
196
      * filter all urls that match either of the MIME types "application/xhtml+xml" and "text/html"
197
198
199
200
201
202
      * caller: *getItemData()*
      *
      * @param string array
      *
      * @return array
      */
203
    getContentUrls(content) {
204
205
206
      const urls = [];

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

209
210
211
        content.forEach((c) => {
          if (c.type.match(/(application\/xhtml\+xml|text\/html)/)) {
            urls.push(c.url);
212
213

            this.contentTypes.push(c.type.split('type=')[1]);
214
215
216
217
          }
        });
      }
      return urls;
218
    },
schneider210's avatar
schneider210 committed
219
220
221
222
223
224
    /**
      * fetch all data provided on 'item level'
      * caller: *mounted-hook*, *getManifest()*
      *
      * @param string url
      */
225
    getItemData(url) {
226
227
      this.request(url)
        .then((data) => {
228
          this.item = data;
229

230
          this.contentUrls = this.getContentUrls(data.content);
231
          this.imageurl = data.image.id || '';
232
233
234
235

          if (data.annotationCollection) {
            this.getAnnotations(data.annotationCollection);
          }
236
237
        });
    },
schneider210's avatar
schneider210 committed
238
239
240
241
242
243
244
    /**
      * caller: *getItemUrls()*
      *
      * @param string nodelabel
      *
      * @return number idx
      */
245
246
247
248
249
250
251
252
253
    getItemIndex(nodelabel) {
      let idx = 0;
      this.itemurls.forEach((item, index) => {
        if (item === nodelabel) {
          idx = index;
        }
      });
      return idx;
    },
254
255
256
257
258
259
260
261
262
263
264
    /**
      * 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
265
266
267
268
269
270
271
272
273
    /**
      * 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
      */
274
275
    getItemUrls(sequence, label) {
      const urls = [];
276

277
278
      sequence.forEach((item) => {
        const itemLabel = this.getItemLabel(item.id);
279

280
281
        urls.push(
          {
282
            label: item.id,
283
284
            'label-key': `${itemLabel}`,
            labelSheet: true,
285
286
287
288
            handler: (node) => {
              if (this.itemurl === node.label) {
                return;
              }
schneider210's avatar
schneider210 committed
289
290
291
              // node.label === itemurl
              // @param label === manifest label; passed by getManifest()
              this.$root.$emit('update-item', node.label, this.getSequenceIndex(label));
292
              this.$root.$emit('update-item-index', this.getItemIndex(node.label));
293
              this.$root.$emit('update-sequence-index', this.getSequenceIndex(label));
294
295
296
297
298
299
            },
          },
        );
      });
      return urls;
    },
schneider210's avatar
schneider210 committed
300
301
302
303
304
305
306
307
    /**
      * get the collection label, if provided; otherwise get the manifest label
      * caller: *getCollection()*, *getManifest()*
      *
      * @param object data
      *
      * @return string 'label'
      */
308
309
310
311
312
313
    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
314
315
316
317
318
319
    /**
      * get all the data provided on 'manifest level'
      * caller: *init()*, *getCollection()*
      *
      * @param string url
      */
320
321
322
    getManifest(url) {
      this.request(url)
        .then((data) => {
323
324
325
326
327
          // 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: [] });
          }

328
329
330
331
332
333
334
335
336
337
          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(
338
339
            {
              children: this.getItemUrls(data.sequence, data.label),
340
341
342
343
344
345
              label: data.label,
              'label-key': data.label,
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
              selectable: false,
346
            },
347
348
349
350
          );
          // make sure that urls are set just once on init
          if (!this.itemurl && data.sequence[0]) {
            this.itemurl = data.sequence[0].id;
351
            this.getItemData(data.sequence[0].id);
352
353
354
          }
        });
    },
schneider210's avatar
schneider210 committed
355
356
357
358
359
360
361
    /**
      * caller: *getItemUrls()*
      *
      * @param string label
      *
      * @return number index
      */
362
    getSequenceIndex(label) {
363
364
      let index = 0;
      this.manifests.forEach((manifest, idx) => {
365
        if (manifest.label === label) {
366
367
368
369
370
          index = idx;
        }
      });
      return index;
    },
schneider210's avatar
schneider210 committed
371
372
373
374
375
376
    /**
      * decide whether to start with a collection or a single manifest
      * caller: *created-hook*
      *
      * @return function getCollection() | getManifest()
      */
377
378
379
380
381
382
383
    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
384
385
};
</script>
386
387

<style scoped>
388
.root {
389
390
391
392
393
394
  display: flex;
  flex: 1;
  flex-direction: column;
  overflow: hidden;
}

Mathias Goebel's avatar
Mathias Goebel committed
395
.viewport {
396
  height: 100vh;
397
398
}
</style>