App.vue 5.22 KB
Newer Older
Mathias Goebel's avatar
Mathias Goebel committed
1
2
<template>
  <div id="q-app">
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    <q-layout view="hHh lpr fff">
      <Header
        :collection="collection"
        :itemurl="itemurl"
        :itemurls="itemurls"
        :manifests="manifests"
        :status="status"
      />

      <q-page-container>
        <router-view
          :collection="collection"
          :imageurl="imageurl"
          :itemurl="itemurl"
          :manifests="manifests"
          :request="request"
          :tree="tree"
        />
      </q-page-container>
    </q-layout>
Mathias Goebel's avatar
Mathias Goebel committed
23
24
25
26
  </div>
</template>

<script>
27
28
import Header from '@/components/quasar-header.vue';

Mathias Goebel's avatar
Mathias Goebel committed
29
export default {
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  name: 'Viewer',
  components: {
    Header,
  },
  data() {
    return {
      collection: {},
      config: {},
      imageurl: '',
      itemurl: '',
      itemurls: [],
      label: '',
      manifests: [],
      status: {
        image: true, text: true, metadata: true, treeview: true,
      },
      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) {
      this.request(url)
        .then((data) => {
          this.collection = data;
          this.label = this.getLabel(data);

62
          this.tree.push({ label: this.label, labelKey: this.label, children: [] });
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

          if (Array.isArray(data.sequence)) {
            data.sequence.forEach((seq) => this.getManifest(seq.id));
          }
        });
    },
    getConfig() {
      this.config = JSON.parse(document.getElementById('emo-config').text);
    },
    getImageUrl(url) {
      this.request(url)
        .then((data) => {
          this.imageurl = data.image && data.image.id ? data.image.id : '';
        });
    },
    getItemContent(itemurl) {
79
80
      const itemcontent = {};

81
82
      this.request(itemurl)
        .then((data) => {
83
84
85
86
87
          itemcontent.collectiontitle = data.title ? data.title : 'No Title provided';
          itemcontent.contenturl = data.content;
          itemcontent.imageurl = data.image && data.image.id ? data.image.id : '';
          itemcontent.itemlanguage = data.language;
          itemcontent.pagenumber = data.n ? data.n : 'No pagenumber provided';
88
        });
89
      return itemcontent;
90
91
92
93
94
95
96
97
98
99
100
101
    },
    getItemIndex(nodelabel) {
      let idx = 0;
      this.itemurls.forEach((item, index) => {
        if (item === nodelabel) {
          idx = index;
        }
      });
      return idx;
    },
    getItemUrls(sequence, label) {
      const urls = [];
102
      let ctr = 0;
103
104
105
106
      sequence.forEach((obj) => {
        urls.push(
          {
            label: obj.id,
107
            labelKey: ctr += 1,
108
109
110
111
112
113
            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));
114
              this.$root.$emit('update-sequence-index', this.getSequenceIndex(label));
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
            },
          },
        );
      });
      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) => {
          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(
140
141
142
143
144
            {
              label: data.label,
              labelKey: data.label,
              children: this.getItemUrls(data.sequence, data.label),
            },
145
146
147
148
149
150
151
152
153
154
155
156
          );

          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;
            this.getImageUrl(data.sequence[0].id);
          }
        });
    },
157
    getSequenceIndex(label) {
158
159
      let index = 0;
      this.manifests.forEach((manifest, idx) => {
160
        if (manifest.label === label) {
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
          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() {
    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" itemurl will be shown.
      this.imageurl = '';
      this.getImageUrl(url);
    });

    this.$root.$on('update-panel-status', (status) => {
      this.status = status;
    });
  },
Mathias Goebel's avatar
Mathias Goebel committed
192
193
};
</script>