forked from kristinallarsen/gallery-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
286 lines (227 loc) · 10 KB
/
script.js
File metadata and controls
286 lines (227 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
111
112
113
114
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
let viewer;
let collectedManifests = []; // This will hold the individual manifests
document.addEventListener('DOMContentLoaded', () => {
viewer = OpenSeadragon({
id: 'viewer',
prefixUrl: 'https://cdnjs.cloudflare.com/ajax/libs/openseadragon/4.0.0/images/',
tileSources: []
});
});
// Clear current gallery and add images from loaded collection
function repopulateGallery(manifestData) {
const gallery = document.getElementById('gallery');
gallery.innerHTML = ''; // Clear the current gallery
// Check if items array exists
const manifests = manifestData.items; // Update based on IIIF spec
if (!Array.isArray(manifests)) {
console.error('No valid items found in the manifest data.');
return;
}
manifests.forEach(manifest => {
// Access the sequences within each manifest
if (manifest.sequences && manifest.sequences.length > 0) {
const canvasItems = manifest.sequences[0].canvases;
canvasItems.forEach(canvas => {
addCanvasToGallery(canvas, manifest); // A new function to handle adding canvases
});
} else {
console.error('Manifest does not contain valid sequences.');
}
});
}
function getMetadataValue(metadata, label, getLast = false) {
const items = metadata.filter(item => item.label === label);
if (getLast && items.length > 0) {
const lastItem = items[items.length - 1]; // Get the last instance found
if (Array.isArray(lastItem.value)) {
return lastItem.value[0]; // Return the first value of the array (Digital Commonwealth)
}
return lastItem.value; // Return the value directly if it's not an array
}
return items.length > 0 ? items[0].value : null; // Return the first instance or null
}
function isAbsoluteURL(url) {
return /^(http|https):\/\//i.test(url);
}
function addCanvasToGallery(canvas, manifest) {
const imageService = canvas.images[0].resource.service;
if (!imageService || !imageService['@id']) {
console.error('Image service is missing or does not contain an @id field:', canvas);
return;
}
const imageUrl = `${imageService['@id']}/full/!200,200/0/default.jpg`;
const highResUrl = `${imageService['@id']}/info.json`;
// Retrieve metadata from both the manifest and the canvas
const manifestMetadata = manifest.metadata || [];
const canvasMetadata = canvas.metadata || [];
console.log('Manifest Metadata:', manifestMetadata);
console.log('Canvas Metadata:', canvasMetadata);
// Attempt to get title, date and author
let title = getMetadataValue(canvasMetadata, 'Title') || getMetadataValue(manifestMetadata, 'Title') || manifest.label || 'No title returned';
let date = getMetadataValue(canvasMetadata, 'Date') || getMetadataValue(manifestMetadata, 'Date') || getMetadataValue(manifestMetadata, 'Created Published') || 'No date returned';
let author = getMetadataValue(canvasMetadata, 'Creator') || getMetadataValue(manifestMetadata, 'Creator') || getMetadataValue(canvasMetadata, 'Contributors') || getMetadataValue(manifestMetadata, 'Contributors') || getMetadataValue(canvasMetadata, 'Author') || getMetadataValue(manifestMetadata, 'Author') || getMetadataValue(canvasMetadata, 'Contributor') || getMetadataValue(manifestMetadata, 'Contributor') || 'No author returned';
// Get the location link
let locationLink = null;
// Check for a valid URL in the related field, considering both formats
if (manifest.related) {
// If related is an object and has an @id
if (typeof manifest.related === 'object' && manifest.related["@id"]) {
locationLink = manifest.related["@id"];
}
// If it's a string, use that directly
else if (typeof manifest.related === 'string') {
locationLink = manifest.related;
}
}
// If locationLink is still not defined, check other sources
if (!locationLink) {
locationLink = getMetadataValue(canvasMetadata, 'Identifier') ||
getMetadataValue(manifestMetadata, 'Identifier', true) || // Get last occurrence
getMetadataValue(canvasMetadata, 'Item Url') || // LOC label
getMetadataValue(manifestMetadata, 'Item Url') || // covering canvas and manifest metadata sources
'No link available';
}
// Debugging logs for verification
console.log('Location Link:', locationLink);
// Ensure the link is absolute
if (!isAbsoluteURL(locationLink) && locationLink !== 'No link available') {
locationLink = 'http://' + locationLink; // Adjust based on your needs
}
// Get collection name and attribution
let collection = getMetadataValue(canvasMetadata, 'Location') || getMetadataValue(manifestMetadata, 'Location') || 'No collection returned';
const attribution = manifest.attribution || 'No attribution returned';
const card = document.createElement('div');
card.className = 'card';
const img = document.createElement('img');
img.src = imageUrl;
img.addEventListener('click', () => {
viewer.open(highResUrl);
});
const deleteBtn = document.createElement('button');
deleteBtn.className = 'delete-btn';
deleteBtn.textContent = 'x';
deleteBtn.addEventListener('click', () => {
const shouldRemove = confirm('Do you want to remove this image from the gallery?');
if (shouldRemove) {
card.remove();
}
});
const titleEl = document.createElement('p');
titleEl.textContent = `Title: ${title}`;
const authorEl = document.createElement('p');
authorEl.textContent = `Author: ${author}`;
const dateEl = document.createElement('p');
dateEl.textContent = `Date: ${date}`;
const collectionEl = document.createElement('p');
collectionEl.textContent = `Collection: ${collection}`;
const attributionEl = document.createElement('p');
attributionEl.textContent = `Attribution: ${attribution}`;
// Link to the item's location
const locationLinkEl = document.createElement('a');
locationLinkEl.href = locationLink === 'No link available' ? '#' : locationLink; // Link to '#' if no link
locationLinkEl.textContent = 'View Item';
locationLinkEl.target = '_blank'; // Opens in a new tab
const locationParagraph = document.createElement('p');
locationParagraph.appendChild(locationLinkEl);
card.appendChild(deleteBtn);
card.appendChild(img);
card.appendChild(titleEl);
card.appendChild(authorEl);
card.appendChild(dateEl);
card.appendChild(collectionEl);
card.appendChild(attributionEl);
card.appendChild(locationParagraph); // Add the link to the card
document.getElementById('gallery').appendChild(card);
}
// Function to add a IIIF manifest to the gallery
async function addManifestToGallery(manifestUrl) {
try {
const response = await fetch(manifestUrl);
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
const manifest = await response.json();
if (!manifest.sequences || !manifest.sequences[0].canvases) {
throw new Error('Manifest does not contain sequences or canvases in the expected format.');
}
// Store the manifest for later export
collectedManifests.push(manifest);
const canvasItems = manifest.sequences[0].canvases;
const gallery = document.getElementById('gallery');
canvasItems.forEach(canvas => {
addCanvasToGallery(canvas, manifest);
});
} catch (error) {
console.error('Error fetching IIIF Manifest:', error);
alert(`There was an error fetching the IIIF Manifest: ${error.message}`);
}
}
// Function to export the collected manifests as a combined manifest
function exportCombinedManifest() {
// Get the user-defined name for the manifest
const manifestName = document.getElementById('manifestName').value.trim() || 'combined_manifest';
const combinedManifest = {
'@context': 'http://iiif.io/api/presentation/3.0/context.json',
id: 'http://example.org/combined-manifest',
type: 'Manifest',
label: { 'en': ['Combined Manifest'] },
items: [] // Use 'items' for a single manifest
};
// Add each of the collected manifests to the combined manifest
collectedManifests.forEach(manifest => {
combinedManifest.items.push(manifest);
});
// Convert the combined manifest to a JSON string
const jsonStr = JSON.stringify(combinedManifest, null, 2);
// Create a blob to download
const blob = new Blob([jsonStr], { type: 'application/json' });
const url = URL.createObjectURL(blob);
// Create a link to download the file
const a = document.createElement('a');
a.href = url;
a.download = `${manifestName}.json`; // Use the user-defined name
// Programmatically click the link to trigger the download
document.body.appendChild(a);
a.click();
// Cleanup
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 0);
}
// Event listener to add manifest URLs to the gallery
document.getElementById('addManifest').addEventListener('click', async () => {
const manifestUrls = document.getElementById('manifestUrl').value.split(',').map(url => url.trim());
if (!manifestUrls.length) {
alert('Please enter one or more IIIF Manifest URLs');
return;
}
for (const manifestUrl of manifestUrls) {
if (manifestUrl) {
await addManifestToGallery(manifestUrl);
}
}
});
// Event listener to load the uploaded combined manifest
document.getElementById('loadManifest').addEventListener('click', async () => {
const fileInput = document.getElementById('uploadManifest');
const file = fileInput.files[0];
if (!file) {
alert('Please select a JSON file to upload.');
return;
}
const reader = new FileReader();
reader.onload = async function(event) {
const jsonContent = event.target.result;
try {
const manifestData = JSON.parse(jsonContent);
repopulateGallery(manifestData); // Call your existing method to populate the gallery
} catch (error) {
console.error('Error parsing JSON:', error);
alert('Failed to load manifest: ' + error.message);
}
};
reader.readAsText(file); // Read the file as text
});
// Event listener for the export button
document.getElementById('export-manifest').addEventListener('click', exportCombinedManifest);