App.vue 6.15 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
15
        :itemurls="itemurls"
        :manifests="manifests"
      />

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

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

<script>
34
import Header from '@/components/quasar-header.vue';
35
import Footer from '@/components/quasar-footer.vue';
36

Mathias Goebel's avatar
Mathias Goebel committed
37
export default {
38
39
40
  name: 'Viewer',
  components: {
    Header,
41
    Footer,
42
43
44
45
  },
  data() {
    return {
      collection: {},
46
47
      collectiontitle: '',
      contenturl: '',
48
      config: {},
schneider210's avatar
schneider210 committed
49
      fontsize: 14,
50
      imageurl: '',
51
      isCollection: false,
52
      itemlabel: '',
53
      itemlanguage: '',
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
      itemurl: '',
      itemurls: [],
      label: '',
      manifests: [],
      tree: [],
    };
  },
  methods: {
    async request(url, responsetype = 'json') {
      const response = await fetch(url);
      const data = await (responsetype === 'text' ? response.text() : response.json());

      return data;
    },
    getCollection(url) {
69
70
      this.isCollection = true;

71
72
73
74
75
      this.request(url)
        .then((data) => {
          this.collection = data;
          this.label = this.getLabel(data);

76
77
78
79
80
81
82
83
84
85
86
          this.tree.push(
            {
              children: [],
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
              label: this.label,
              'label-key': this.label,
              selectable: false,
            },
          );
87
88
89
90
91
92
93
94
95

          if (Array.isArray(data.sequence)) {
            data.sequence.forEach((seq) => this.getManifest(seq.id));
          }
        });
    },
    getConfig() {
      this.config = JSON.parse(document.getElementById('emo-config').text);
    },
96
    getItemData(url) {
97
98
      this.request(url)
        .then((data) => {
99
          this.collectiontitle = data.title;
100

101
          this.contenturl = data.content;
102
          this.imageurl = data.image && data.image.id ? data.image.id : '';
103
          this.itemlabel = data.n ? data.n : 'No itemlabel :(';
104
105
          // eslint-disable-next-line prefer-destructuring
          this.itemlanguage = data['x-langString'].split(',')[0];
106
107
108
109
110
111
112
113
114
115
116
117
118
        });
    },
    getItemIndex(nodelabel) {
      let idx = 0;
      this.itemurls.forEach((item, index) => {
        if (item === nodelabel) {
          idx = index;
        }
      });
      return idx;
    },
    getItemUrls(sequence, label) {
      const urls = [];
119

120
      sequence.forEach((obj) => {
121
122
        const pagelabel = this.getPageLabel(obj.id);

123
124
125
        urls.push(
          {
            label: obj.id,
126
            'label-key': `${this.config.labels.item} ${pagelabel}`,
127
128
129
130
131
132
            handler: (node) => {
              if (this.itemurl === node.label) {
                return;
              }
              this.$root.$emit('update-item', node.label);
              this.$root.$emit('update-item-index', this.getItemIndex(node.label));
133
              this.$root.$emit('update-sequence-index', this.getSequenceIndex(label));
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
            },
          },
        );
      });
      return urls;
    },
    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>';
    },
    getManifest(url) {
      this.request(url)
        .then((data) => {
149
150
151
152
153
          // 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: [] });
          }

154
155
156
157
158
159
160
161
162
163
          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(
164
165
            {
              children: this.getItemUrls(data.sequence, data.label),
166
167
168
169
170
171
              label: data.label,
              'label-key': data.label,
              handler: (node) => {
                this.$root.$emit('update-tree-knots', node.label);
              },
              selectable: false,
172
            },
173
174
175
176
177
178
179
180
          );

          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;
181
            this.getItemData(data.sequence[0].id);
182
183
184
          }
        });
    },
185
186
187
    getPageLabel(itemurl) {
      return itemurl.replace(/.*-(.*)\/latest.*$/, '$1');
    },
188
    getSequenceIndex(label) {
189
190
      let index = 0;
      this.manifests.forEach((manifest, idx) => {
191
        if (manifest.label === label) {
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
          index = idx;
        }
      });
      return index;
    },
    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() {
210
211
212
213
    this.$root.$on('update-fontsize', (fontsize) => {
      this.fontsize = fontsize;
    });

214
215
216
217
    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,
218
      // the "preceding" image according to the "preceding" item will be shown.
219
      this.imageurl = '';
220
      this.getItemData(url);
221
222
    });
  },
Mathias Goebel's avatar
Mathias Goebel committed
223
224
};
</script>