forked from UTS-eResearch/ro-crate-html-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathro-crate-preview.js
455 lines (397 loc) · 15.3 KB
/
ro-crate-preview.js
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
This is part of ro-crate-html-js a tool for generating HTMl
previews of HTML files.
Copyright (C) 2021 University of Technology Sydney
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const defaults = require("./defaults");
//const _ = require("lodash");
const Page = require("./paginate");
//const { times } = require("lodash");
const display_keys = [
"@id",
"name",
"familyName",
"givenName",
"@type",
"description",
"funder",
"memberOf",
"isPartOf",
"fileOf",
"thumbnail",
"datePublished",
"author",
"encodingFormat",
"contentSize",
"affiliation",
"email"
];
const displayTypeTemplates = {
"PropertyValue" : (item)=> {return `${item.name}: ${item.value}`},
"GeoCoordinates" : (item)=> {return `Lat: ${item.latitude }, Long: ${item.longitude}`}
}
class Preview {
constructor(crate, config, id) {
this.defaults = defaults;
this.crate = crate;
this.config = config || {}; //TODO - add some defaults here;
this.crate.index();
this.crate.addBackLinks();
this.rootId = this.crate.getRootId();
this.baseID = id || this.rootId;
this.root = this.crate.getRootDataset();
if (!this.crate.context) {
this.crate.resolveContext();
}
this.places = [];
}
async display(id) {
const datasetDisplay = await this.renderMetadataForItem(id);
document.getElementById("summary").innerHTML = datasetDisplay;
}
completeDataset(entryID, dontShowRootDataset) {
entryID = entryID || this.crate.getRootId();
var html = "";
html += this.metaTable(this.crate.getItem(entryID));
this.baseID = entryID;
for (let item of this.crate.getJson()["@graph"]) {
if (item["@id"] != entryID &&
!this.displayTypeAsString(item) &&
!this.crate.defaults.roCrateMetadataIDs.includes(item["@id"]) && !(dontShowRootDataset && item["@id"] === this.crate.getRootID())){
html += this.metaTable(item, true);
}
}
return html;
}
// only display direct children of the root dataset
async summarizeDataset() {
// Makes HTML tables for RO-Crate Core Metadata - just a teaser of the contents, not all of it
var keepIds = [this.rootId];
for (let prop of this.sortKeys(Object.keys(this.root))) {
var values = this.crate.utils.asArray(this.root[prop]);
for (let v of values) {
if (v["@id"] && !keepIds.includes(v["@id"]) ) {
keepIds.push(v["@id"]);
}
}
}
// Now prune out stuff we don't need into a new graph
var newGraph = []
for (let i of this.crate.getJson()["@graph"]) {
if (keepIds.includes(i['@id'])) {
newGraph.push(i);
}
}
//this.crate.getJson()["@graph"] = newGraph;
//this.crate.init(this.crate.getJson());
// And generate HTML for what's left
const dontShowPreviews = (this.root.hasPart && this.root.hasPart.length > defaults.pageSize);
var allMeta = `<div class='all-meta'>`;
for (let i of keepIds) {
allMeta += await this.renderMetadataForItem(i, dontShowPreviews);
allMeta += "<hr/><br/><br/>";
}
allMeta += `</div>`;
// Don't try to show files if there are a lot - ie more than one
return allMeta;
}
async renderMetadataForItem(id, dontShowPreviews) {
//var item = _.clone(this.crate.getItem(id));
var item = this.crate.getItem(id);
if (!item) {
return "";
}
// Check if there are any parts that should be displayed up top
// Thumbnail if present
// Display a table or table-like - core metadata show all the properties
var itemHtml = `
<div>
${this.header(item)}
${this.image(item)}
${this.previews(item, dontShowPreviews)}
${this.articleBody(item)}
${this.metaTable(item)}
</div>
`;
return itemHtml;
}
displayPlaces() {
const config = this.config;
// Places is an GeoJSON object
if (config && this.places && this.places.type === "FeatureCollection") {
var jsonString = JSON.stringify(places,null,2)
const dir = config.geoURL;
return `
<link rel='stylesheet' href='${dir}/css/leaflet.css'/>
<script src='${dir}/js/jquery-3.5.1.min.js'></script>
<script src='${dir}/js/leaflet.js'></script>
<script src='${dir}/js/timeSliderControl.js'></script>
<link rel='stylesheet' href='${dir}/css/jquery-ui.css'/>
<script src='${dir}/js/jquery-ui.js'></script>
<link rel='stylesheet' href='${dir}/css/style.css'/>
<link rel='stylesheet' href='${dir}/css/MarkerCluster.css'/>
<link rel='stylesheet' href='${dir}/css/MarkerCluster.Default.css'/>
<script src='${dir}/js/leaflet.markercluster.js'></script>
<div id='mapdiv' style='height: 420px;'></div>
<script src='${dir}/js/mapmaker.js'></script><!-- Handles a lot of the marker stuff -->
<script>mapinit(${JSON.stringify(this.places)})</script>
<a href="data:text/json;charset=utf-8,${encodeURIComponent(JSON.stringify(this.places))}" download="geojson.json">Download GeoJSON</a>
`
} else {
return ``;
}
}
header(item) {
// Display the name of the thing with apropriate download links etc
var name = item.name ? item.name : item["@id"];
var types = this.crate.utils.asArray(item["@type"]);
var view;
var path = item["@id"];
var idLink = "";
// Special treatment for Datasets - add download links if there is a distribution
if (path.match(/^https?:\/\//i)) {
view = "Go to: ";
} else if (types.includes("Dataset")){
if (window.location.href.match(/^file:\/\//i)){
view = "Browse files ";
}
if (item["distribution"]) {
for (let dist of this.crate.utils.asArray(item["distribution"])){
const download = this.crate.getItem(dist["@id"]);
// Dealing with legacy - we used to have path mapped to contentUrl
if (download) {
var downloadName = download.name ? download.name : name;
var u = download["contentUrl"] ? download["contentUrl"] : download["path"];
if (u) {
idLink += `<a href="${u}">⬇️📦 Download this dataset: ${downloadName}</a><br/>`;
}
}
}
}
} else if ( types.includes("File") || types.includes("ImageObject") || types.includes("MediaObject") || path === "ro-crate-metadata.jsonld"){
view = "⬇️ Download: ";
}
if (view){
idLink += `<a href="${path}">${view}</a>`;
}
return `<h3>${idLink} ${name}</h3>`
}
image(item) {
var image = "";
if (item.image || item.thumbnail) {
var src;
if (item.image && item.image.length > 0 ) {
src = this.crate.utils.asArray(item.image)[0];
delete item.image;
} else if (item.thumbnail && item.thumbnail.length > 0){
src = this.crate.utils.asArray(item.thumbnail)[0];
delete item.thumbnail;
}
if (src) {
if (src["@id"]) {
src = src["@id"];
}
image = `<img src="${src}" width="80%">`;
}
}
return image;
}
articleBody(item) {
// See if there are any fragments to display - if there are references to things which have
// articleBody but no name (for lab notebook display)
var articleBody = ""
for (let part of this.crate.utils.asArray(item.hasPart)) {
const p = this.crate.getItem(part["@id"]);
if (p && this.crate.utils.asArray(p["@type"]).includes("Article") && p.articleBody) {
for (let b of this.crate.utils.asArray(p.articleBody))
{
articleBody += `<div><hr><div style="font: small; text-align: right" >${p.description}</div>${b}</div>`
}
}
}
return articleBody;
}
script() {
const url = this.config.renderScript || this.crate.defaults.render_script;
return `<script src="${url}"></script>`
}
previews(item, dontShowPreviews) {
var p = `${item["@id"]}`;
if (this.config.utils) {
p = this.config.utils.getImagePath(this.baseID, p)
}
var previews = "";
var types = this.crate.utils.asArray(item["@type"]);
if (!dontShowPreviews && (types.includes("Dataset") || types.includes("File") ||types.includes("ImageObject") ||types.includes("MediaObject"))) {
if (p.match(/(\.txt$)|(\.html?$)/i)){
previews += `<iframe src='${p}' width='100%' height='500'></iframe>`;
} else if (p.match(/(\.mp3)|(\.ogg?)|(\.wav)$/i)){
previews += `<audio controls><source src='${p}'/></audio>`;
} else if (p.match(/(\.jpe?g)|(\.png)$/i)){
previews += `<img width='100%' style='object-fit: contain' src='${p}'/>`;
}
else if (p.match(/pdf$/i)){
previews += `<embed src="${p}" type="application/pdf" width="100%" height="600px" />`;
}
}
return previews;
}
metaTable(it, showName) {
// Generate a "table" (or other structure)
//const item = _.clone(it);
const item = it;
var name = "";
if (showName) {
name = `<h4>${this.crate.utils.asArray(item.name).join(" ")}</h4>`;
delete item.name;
}
var rows = "";
for (let prop of this.sortKeys(Object.keys(item))) {
if (prop === "@reverse") {
// Do nothing
} else {
rows += this.metadataRow(item, prop);
}
}
if (item["@reverse"]) {
rows += `<tr><th colspan="2" style="text-align:center">Items that reference this one</th><tr>`;
for (let prop of Object.keys(item["@reverse"])) {
rows += this.metadataRow(item["@reverse"], prop);
}
}
return `
<div id="${item["@id"]}">
${name}
<table class="table metadata table-striped" >
<tbody>${rows}</tbody>
</table>
</div>`;
}
sortKeys(keys) {
// Sort a set or array of keys to be in a nice order
// Returns set
var keys_in_order = new Set();
keys = new Set(keys);
for (let key of display_keys) {
if (keys.has(key)) {
keys_in_order.add(key);
}
}
for (let key of keys) {
if (!keys_in_order.has(key)) {
keys_in_order.add(key);
}
}
return keys_in_order;
}
metadataRow(item, prop){
if (this.crate.context) {
const def = this.crate.getDefinition(prop);
var propName = prop;
if (def["rdfs:comment"]) {
propName = def["rdfs:label"] || propName;
propName = `${propName}<span> </span><span name="propName" title="${def["rdfs:comment"]}">[?]</span>`
} else if (def["@id"]) {
propName = `${propName}<span> </span><a href="${def["@id"]}">[?]</a>`;
}
}
return `<tr>
<th style="text-align:left;" class="prop">${propName}</th>
<td style='text-align:left'>${this.displayValues(item[prop])}</td>
</tr>`;
}
displayValuesAsString(v) {
const vals = this.crate.utils.asArray(v);
var html = "";
for (v of vals) {
html += this.displayValue(v);
}
return html;
}
displayValues(v) {
const vals = this.crate.utils.asArray(v);
const page = new Page({values: vals, pageSize: this.defaults.pageSize});
return this.displayPage(page);
}
displayPage(page) {
var html = "";
if (page.pages.length > 0) {
for (let p of page.pages) {
if (p.first && p.last) {
html += `
<details style='margin-left: 3%'>
<summary>
${this.displayValue(p.first)} -to- ${this.displayValue(p.last)}
</summary>
${this.displayPage(p)}
</details>
`;
}
}
} else {
if (page.values.length > 1 ) {
html += "<ul>";
for (let p of page.values) {
html += `
<li>${this.displayValue(p)}</li>
`;
}
html += "</ul>";
} else if (page.values[0]) {
html += `${this.displayValue(page.values[0])}`;
}
}
return html;
}
displayTypeAsString(item){
// Return either false or a function to render this particular type of item
const types = this.crate.utils.asArray(item["@type"]);
for (let type of types) {
const renderFunction = displayTypeTemplates[type];
if (renderFunction) {
return renderFunction;
}
}
return null;
}
displayValue(val) {
if (val["@id"]) {
var target = this.crate.getItem(val["@id"]);
if (target) {
var name = target.name || target.value || target["@id"];
if (this.config.utils && this.config.utils.hasOwnPage(target, this.config)) {
return `<a href="${this.config.utils.getLink(this.baseID, target["@id"])}">${name}</a>`;
}
const renderFunction = this.displayTypeAsString(target);
if (!renderFunction) {
return `<a href="#${escape(target["@id"])}">${name}</a>`
} else {
return renderFunction(target);
}
} else {
if (val["@id"].toString().match(/^https?:\/\//i)) {
return `<a href="${val["@id"]}">${val["@id"]}</a>`
}
else {
return val["@id"];
}
}
}
else if (val.toString().match(/^https?:\/\//i)) {
return `<a href="${val}">${val}</a>`;
} else {
return `<span>${val}</span>`;
}
}
}
module.exports = Preview;