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

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

nwindis's avatar
DUMP    
nwindis committed
33
34
      <Footer
        :projectcolors="config.colors"
schneider210's avatar
schneider210 committed
35
        :standalone="config.standalone"
nwindis's avatar
nwindis committed
36
      />
37
    </q-layout>
Mathias Goebel's avatar
Mathias Goebel committed
38
39
40
41
  </div>
</template>

<script>
schneider210's avatar
schneider210 committed
42
import { colors } from 'quasar';
schneider210's avatar
schneider210 committed
43
import Footer from '@/components/footer.vue';
44
import Header from '@/components/header.vue';
dindigala's avatar
dindigala committed
45
import PanelsMixin from '@/config/panels.js';
46

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

schneider210's avatar
schneider210 committed
78
79
    this.$q.dark.set('auto');

nwindis's avatar
nwindis committed
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
    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);
    });
  },
115
  methods: {
schneider210's avatar
schneider210 committed
116
117
118
119
120
121
122
123
124
125
    /**
      * 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
      */
126
127
128
129
130
131
    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
132
133
134
135
136
137
138
139
    /**
      * 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
      */
140
    getCollection(url) {
141
142
      this.isCollection = true;

143
144
145
146
147
      this.request(url)
        .then((data) => {
          this.collection = data;
          this.label = this.getLabel(data);

148
149
150
151
152
153
154
155
156
157
158
          this.tree.push(
            {
              children: [],
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
              label: this.label,
              'label-key': this.label,
              selectable: false,
            },
          );
159
160
161
162
163
164

          if (Array.isArray(data.sequence)) {
            data.sequence.forEach((seq) => this.getManifest(seq.id));
          }
        });
    },
schneider210's avatar
schneider210 committed
165
166
167
168
    /**
      * get config object (JSON) from index.html
      * caller: *created-hook*
      */
169
    getConfig() {
170
      this.config = JSON.parse(document.getElementById('tido-config').text);
171
    },
schneider210's avatar
schneider210 committed
172
173
174
175
176
177
    /**
      * fetch all data provided on 'item level'
      * caller: *mounted-hook*, *getManifest()*
      *
      * @param string url
      */
178
    getItemData(url) {
179
180
      this.request(url)
        .then((data) => {
181
          this.collectiontitle = data.title.title;
182
          this.contenturl = data.content;
183
          this.imageurl = data.image && data.image.id ? data.image.id : '';
184
          this.itemlabel = data.n ? data.n : 'No itemlabel :(';
185
186
187

          // note: the scholars didn't mark the item language yet, so atm the API provides them all.
          // since we know, we are dealing with the arabic part of the collection, we define the language to be arabic.
188
          const [language] = data['x-langString'] ? data['x-langString'].split(',') : data.lang;
189

190
          this.itemlanguage = language;
191
192
        });
    },
schneider210's avatar
schneider210 committed
193
194
195
196
197
198
199
    /**
      * caller: *getItemUrls()*
      *
      * @param string nodelabel
      *
      * @return number idx
      */
200
201
202
203
204
205
206
207
208
    getItemIndex(nodelabel) {
      let idx = 0;
      this.itemurls.forEach((item, index) => {
        if (item === nodelabel) {
          idx = index;
        }
      });
      return idx;
    },
schneider210's avatar
schneider210 committed
209
210
211
212
213
214
215
216
217
    /**
      * 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
      */
218
219
    getItemUrls(sequence, label) {
      const urls = [];
220

221
      sequence.forEach((obj) => {
222
223
        const pagelabel = this.getPageLabel(obj.id);

224
225
226
        urls.push(
          {
            label: obj.id,
227
            'label-key': `${this.config.labels.item} ${pagelabel}`,
228
229
230
231
            handler: (node) => {
              if (this.itemurl === node.label) {
                return;
              }
schneider210's avatar
schneider210 committed
232
233
234
              // node.label === itemurl
              // @param label === manifest label; passed by getManifest()
              this.$root.$emit('update-item', node.label, this.getSequenceIndex(label));
235
              this.$root.$emit('update-item-index', this.getItemIndex(node.label));
236
              this.$root.$emit('update-sequence-index', this.getSequenceIndex(label));
237
238
239
240
241
242
            },
          },
        );
      });
      return urls;
    },
schneider210's avatar
schneider210 committed
243
244
245
246
247
248
249
250
    /**
      * get the collection label, if provided; otherwise get the manifest label
      * caller: *getCollection()*, *getManifest()*
      *
      * @param object data
      *
      * @return string 'label'
      */
251
252
253
254
255
256
    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
257
258
259
260
261
262
    /**
      * get all the data provided on 'manifest level'
      * caller: *init()*, *getCollection()*
      *
      * @param string url
      */
263
264
265
    getManifest(url) {
      this.request(url)
        .then((data) => {
266
267
268
269
270
          // 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: [] });
          }

271
272
273
274
275
276
277
278
279
280
          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(
281
282
            {
              children: this.getItemUrls(data.sequence, data.label),
283
284
285
286
287
288
              label: data.label,
              'label-key': data.label,
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
              selectable: false,
289
            },
290
291
292
293
294
295
296
297
          );

          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;
298
            this.getItemData(data.sequence[0].id);
299
300
301
          }
        });
    },
schneider210's avatar
schneider210 committed
302
303
304
305
306
307
308
309
    /**
      * extract the 'label part' of the itemurl
      * caller: *getItemUrls()*
      *
      * @param string itemurl
      *
      * @return string 'label part'
      */
310
311
312
    getPageLabel(itemurl) {
      return itemurl.replace(/.*-(.*)\/latest.*$/, '$1');
    },
schneider210's avatar
schneider210 committed
313
314
315
316
317
318
319
    /**
      * caller: *getItemUrls()*
      *
      * @param string label
      *
      * @return number index
      */
320
    getSequenceIndex(label) {
321
322
      let index = 0;
      this.manifests.forEach((manifest, idx) => {
323
        if (manifest.label === label) {
324
325
326
327
328
          index = idx;
        }
      });
      return index;
    },
schneider210's avatar
schneider210 committed
329
330
331
332
333
334
    /**
      * decide whether to start with a collection or a single manifest
      * caller: *created-hook*
      *
      * @return function getCollection() | getManifest()
      */
335
336
337
338
339
340
341
    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
342
343
};
</script>