App.vue 9.22 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
        :itemurls="itemurls"
        :manifests="manifests"
11
        :panels="panels"
12
13
14
15
      />

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

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

<script>
schneider210's avatar
schneider210 committed
38
import Footer from '@/components/footer.vue';
39
import Header from '@/components/header.vue';
dindigala's avatar
dindigala committed
40
import PanelsMixin from '@/config/panels.js';
41

Mathias Goebel's avatar
Mathias Goebel committed
42
export default {
43
44
45
  name: 'Viewer',
  components: {
    Header,
46
    Footer,
47
  },
dindigala's avatar
dindigala committed
48
  mixins: [PanelsMixin],
49
50
  data() {
    return {
51
      annotations: {},
52
      collection: {},
53
54
      collectiontitle: '',
      contenturl: '',
55
      config: {},
schneider210's avatar
schneider210 committed
56
      fontsize: 14,
57
      imageurl: '',
58
      isCollection: false,
59
      itemlabel: '',
60
      itemlanguage: '',
61
62
63
64
65
66
67
68
      itemurl: '',
      itemurls: [],
      label: '',
      manifests: [],
      tree: [],
    };
  },
  methods: {
schneider210's avatar
schneider210 committed
69
70
71
72
73
74
75
76
77
78
    /**
      * 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
      */
79
80
81
82
83
84
    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
85
86
87
88
89
90
91
92
    /**
      * 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
      */
93
    getCollection(url) {
94
95
      this.isCollection = true;

96
97
98
99
100
      this.request(url)
        .then((data) => {
          this.collection = data;
          this.label = this.getLabel(data);

101
102
103
104
105
          this.request(data.annotationCollection)
            .then((annotations) => {
              this.annotations = annotations.annotationCollection;
            });

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

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

141
          this.contenturl = data.content;
142
          this.imageurl = data.image && data.image.id ? data.image.id : '';
143
          this.itemlabel = data.n ? data.n : 'No itemlabel :(';
144
145
          // eslint-disable-next-line prefer-destructuring
          this.itemlanguage = data['x-langString'].split(',')[0];
146
147
        });
    },
schneider210's avatar
schneider210 committed
148
149
150
151
152
153
154
    /**
      * caller: *getItemUrls()*
      *
      * @param string nodelabel
      *
      * @return number idx
      */
155
156
157
158
159
160
161
162
163
    getItemIndex(nodelabel) {
      let idx = 0;
      this.itemurls.forEach((item, index) => {
        if (item === nodelabel) {
          idx = index;
        }
      });
      return idx;
    },
schneider210's avatar
schneider210 committed
164
165
166
167
168
169
170
171
172
    /**
      * 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
      */
173
174
    getItemUrls(sequence, label) {
      const urls = [];
175

176
      sequence.forEach((obj) => {
177
178
        const pagelabel = this.getPageLabel(obj.id);

179
180
181
        urls.push(
          {
            label: obj.id,
182
            'label-key': `${this.config.labels.item} ${pagelabel}`,
183
184
185
186
            handler: (node) => {
              if (this.itemurl === node.label) {
                return;
              }
schneider210's avatar
schneider210 committed
187
188
189
              // node.label === itemurl
              // @param label === manifest label; passed by getManifest()
              this.$root.$emit('update-item', node.label, this.getSequenceIndex(label));
190
              this.$root.$emit('update-item-index', this.getItemIndex(node.label));
191
              this.$root.$emit('update-sequence-index', this.getSequenceIndex(label));
192
193
194
195
196
197
            },
          },
        );
      });
      return urls;
    },
schneider210's avatar
schneider210 committed
198
199
200
201
202
203
204
205
    /**
      * get the collection label, if provided; otherwise get the manifest label
      * caller: *getCollection()*, *getManifest()*
      *
      * @param object data
      *
      * @return string 'label'
      */
206
207
208
209
210
211
    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
212
213
214
215
216
217
    /**
      * get all the data provided on 'manifest level'
      * caller: *init()*, *getCollection()*
      *
      * @param string url
      */
218
219
220
    getManifest(url) {
      this.request(url)
        .then((data) => {
221
222
223
224
225
          // 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: [] });
          }

226
227
228
229
230
231
232
233
234
235
          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(
236
237
            {
              children: this.getItemUrls(data.sequence, data.label),
238
239
240
241
242
243
              label: data.label,
              'label-key': data.label,
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
              selectable: false,
244
            },
245
246
247
248
249
250
251
252
          );

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

nwindis's avatar
nwindis committed
300
301
    this.$q.dark.set('auto');

302
303
304
    this.itemurls.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
  },
  mounted() {
schneider210's avatar
schneider210 committed
305
306
307
308
309
310
311
    /**
      * 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
      */
312
313
314
    this.$root.$on('update-fontsize', (fontsize) => {
      this.fontsize = fontsize;
    });
315
316
317
    this.$root.$on('panels-position', (newPanels) => {
      this.panels = newPanels;
    });
schneider210's avatar
schneider210 committed
318
319
320
321
322
323
    /**
      * listen to item change (user interaction).
      * emitted in: *getItemurls*; handler for tree nodes. fired on user interaction
      *
      * @param string url
      */
324
325
326
327
    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,
328
      // the "preceding" image according to the "preceding" item will be shown.
329
      this.imageurl = '';
330
      this.getItemData(url);
331
332
    });
  },
Mathias Goebel's avatar
Mathias Goebel committed
333
334
};
</script>