-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadataTab.js
463 lines (409 loc) · 16.8 KB
/
metadataTab.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
456
457
458
459
460
461
462
463
/*
* Display the metadata of the current canvas (image) in the left sidebar.
*
* Contains code adapted from
* - metadataView.js (to display the list of metadata from the manifest)
* - canvasLink.js (for the event handlers).
*/
var MetadataTab = {
/* options of the plugin */
options: {
metadataListingCls: 'metadata-listing',
},
/* all of the needed locales */
locales: {
'en': {
'image-metadata': 'Meta',
'no-image-metadata': 'No metadata for this image',
},
'fr': {
'image-metadata': 'Méta',
'no-image-metadata': 'Pas de métadonnées pour cette image',
},
},
metadataTabTemplate: Mirador.Handlebars.compile([
'<li class="tab metadataTab" data-tabid="metadataTab">{{t "image-metadata"}}</li>'
].join('')),
tocTabTemplate: Mirador.Handlebars.compile([
'<li class="tab tocTab" data-tabid="tocTab">{{t "tabTitleIndex"}}</li>'
].join('')),
emptyTemplate: Mirador.Handlebars.compile([
'<li class="leaf-item open">',
'<h2><span>{{t "no-image-metadata"}}</span></h2>',
'</li>',
].join('')),
// Same template than metadataView.
template: Mirador.Handlebars.compile([
'<div class="sub-title">{{t "details"}}:</div>',
'<div class="{{metadataListingCls}}">',
'{{#each details}}',
'<div class="metadata-item"><div class="metadata-label">{{label}}:</div><div class="metadata-value">{{{value}}}</div></div>',
'{{/each}}',
'</div>',
'<div class="sub-title">{{t "rights"}}:</div>',
'{{#if rights}}',
'<div class="{{metadataListingCls}}">',
'{{#each rights}}',
'<div class="metadata-item {{identifier}}"><div class="metadata-label">{{label}}:</div><div class="metadata-value">{{{value}}}</div></div>',
'{{/each}}',
'{{#if logo}}',
'<div class="metadata-item"><div class="metadata-label">{{t "logo"}}:</div><img class="metadata-logo" src="{{logo}}"/></div>',
'{{/if}}',
'</div>',
'{{else}}',
'<div class="{{metadataListingCls}}">',
'<div class="metadata-item"><div class="metadata-label">{{t "rightsStatus"}}:</div><div class="metadata-value">{{t "unspecified"}}</div></div>',
'</div>',
'{{/if}}',
'{{#if links}}',
'<div class="sub-title">{{t "links"}}:</div>',
'<div class="{{metadataListingCls}}">',
'{{#each links}}',
'<div class="metadata-item {{identifier}}"><div class="metadata-label">{{label}}:</div><div class="metadata-value">{{{value}}}</div></div>',
'{{/each}}',
// '{{#if relatedLinks}}',
// '<dt>{{label}}:</dt><dd>{{{value}}}</dd>',
// '{{/if}}',
'</dl>',
'{{/if}}',
].join(''), {
noEscape: true
}),
/* Copy of all helpers from metadataView to build the list of metadata.*/
// TODO Avoid to copy the helpers of metadataView in imageMetadata.
// Base code from https://github.com/padolsey/prettyprint.js. Modified to fit Mirador needs
stringifyObject: function(obj, nestingMargin) {
var type = typeof obj,
_this = this,
str,
first = true,
increment = 15,
delimiter = '<br/>';
if (obj instanceof RegExp) {
return '/' + obj.source + '/';
}
if (typeof nestingMargin === 'undefined') {
nestingMargin = 0;
}
if (obj instanceof Array) {
str = '[ ';
jQuery.each(obj, function(i, item) {
str += (i === 0 ? '' : ', ') + _this.stringifyObject(item, nestingMargin + increment);
});
return str + ' ]';
}
if (typeof obj === 'object' && obj['@type'] === 'sc:Collection') {
var collectionUrl = obj['@id'];
var collectionLabel = obj.label || collectionUrl;
return '<a href="' + collectionUrl + '" target="_blank">' + collectionLabel + '</a>';
} else if (typeof obj === 'object') {
str = '<div style="margin-left:' + nestingMargin + 'px">';
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
str += (first ? '' : delimiter) + i + ': ' + _this.stringifyObject(obj[i], nestingMargin + increment);
first = false;
}
}
return str + '</div>';
}
return obj.toString();
},
stringifyRelated: function(obj) {
var _this = this,
str,
next,
label,
format;
if (obj instanceof Array) {
str = '';
jQuery.each(obj, function(i, item) {
next = _this.stringifyRelated(item);
if (next !== '') str += (i === 0 ? '' : '<br/>') + next;
});
return str;
}
if (typeof obj === 'object' && '@id' in obj) {
label = ('label' in obj) ? obj.label : obj['@id'];
format = ('format' in obj && obj.format !== 'text/html') ? '(' + obj.format + ')' : '';
return '<a href="' + obj['@id'] + '" target="_blank">' + label + '</a> ' + format;
}
return _this.addLinksToUris(obj.toString());
},
getMetadataDetails: function(jsonLd) {
var mdList = [{
label: i18next.t('label'),
value: '<b>' + (Mirador.JsonLd.getTextValue(jsonLd.label) || '') + '</b>'
},
{
label: i18next.t('description'),
value: Mirador.JsonLd.getTextValue(jsonLd.description) || ''
}
];
if (jsonLd.metadata) {
value = "";
label = "";
jQuery.each(jsonLd.metadata, function(index, item) {
label = Mirador.JsonLd.getTextValue(item.label);
value = Mirador.JsonLd.getTextValue(item.value);
mdList.push({
label: label,
value: value
});
});
}
return mdList;
},
getMetadataRights: function(jsonLd) {
return [{
identifier: 'license',
label: i18next.t('license'),
value: jsonLd.license || ''
}, {
identifier: 'attribution',
label: i18next.t('attribution'),
value: Mirador.JsonLd.getTextValue(jsonLd.attribution) || ''
}];
},
getMetadataLinks: function(jsonLd) {
// #414
return [{
identifier: 'related',
label: i18next.t('related'),
value: this.stringifyRelated(jsonLd.related || '')
}, {
identifier: 'seeAlso',
label: i18next.t('seeAlso'),
value: this.stringifyRelated(jsonLd.seeAlso || '')
}, {
// TODO Add the links of the current image (unlike main manifest, they may be uri, not url).
// identifier: 'manifest',
// label: i18next.t('manifest'),
// value: this.stringifyRelated(jsonLd['@id'] || '')
//}, {
identifier: 'within',
label: i18next.t('within'),
value: this.getWithin(jsonLd.within || '')
}];
},
getWithin: function(within) {
if (typeof within === 'object' && within['@type'] === 'sc:Collection') {
var collectionUrl = within['@id'];
var collectionLabel = within.label || collectionUrl;
return '<a href="' + collectionUrl + '" target="_blank">' + collectionLabel + '</a>';
} else if (within instanceof Array) {
return within.map(this.getWithin, this).join("<br/>");
} else {
return this.stringifyObject(within);
}
},
extractLabelFromAttribute: function(attr) {
var label = attr;
label = label.replace(/^@/, '');
label = label.replace(/\s{2,}/g, ' ');
return label;
},
bindEvents: function() {},
toggle: function(stateValue) {
if (stateValue) {
this.show();
} else {
this.hide();
}
},
show: function() {
var element = jQuery(this.element);
if (this.panel) {
element = element.parent();
}
element.show({
effect: "slide",
direction: "right",
duration: 300,
easing: "swing"
});
},
hide: function() {
var element = jQuery(this.element);
if (this.panel) {
element = element.parent();
}
element.hide({
effect: "slide",
direction: "right",
duration: 300,
easing: "swing"
});
},
addLinksToUris: function(text) {
// http://stackoverflow.com/questions/8188645/javascript-regex-to-match-a-url-in-a-field-of-text
var regexUrl = /(http|ftp|https):\/\/[\w\-]+(\.[\w\-]+)+([\w.,@?\^=%&:\/~+#\-]*[\w@?\^=%&\/~+#\-])?/gi,
textWithLinks = text,
matches,
parsedTextWithLinks;
if (typeof text === 'string') {
if (textWithLinks.indexOf('<a ') === -1) {
matches = text.match(regexUrl);
if (matches) {
jQuery.each(matches, function(index, match) {
textWithLinks = textWithLinks.replace(match, '<a href="' + match + '" target="_blank">' + match + '</a>');
});
}
} else {
parsedTextWithLinks = jQuery('<div />').append(textWithLinks);
jQuery(parsedTextWithLinks[0]).find('a').attr('target', '_blank');
textWithLinks = parsedTextWithLinks[0].innerHTML;
}
}
return textWithLinks;
},
/* Extracts metadata of the current image or canvasId */
extractInformationFromWindow: function(viewerWindow, canvasId) {
var _this = this;
var currentImage;
if (canvasId) {
viewerWindow.imagesList.some(function(el) {
if (el['@id'] === canvasId) {
currentImage = el;
return true;
}
});
} else {
currentImage = viewerWindow.imagesList[viewerWindow.focusModules[viewerWindow.viewType].currentImgIndex];
}
if (!currentImage) {
return;
}
var tplData = {
metadataListingCls: this.options.metadataListingCls || 'metadata-listing',
};
_this.manifest = currentImage;
this.imageMetadataTypes = {};
this.imageMetadataTypes.details = _this.getMetadataDetails(_this.manifest);
this.imageMetadataTypes.rights = _this.getMetadataRights(_this.manifest);
this.imageMetadataTypes.links = _this.getMetadataLinks(_this.manifest);
//vvvvv This is *not* how this should be done.
jQuery.each(this.imageMetadataTypes, function(metadataKey, metadataValues) {
tplData[metadataKey] = [];
jQuery.each(metadataValues, function(idx, itm) {
if (typeof itm.value === 'object') {
itm.value = _this.stringifyObject(itm.value);
}
if (typeof itm.value === 'string' && itm.value !== '') {
tplData[metadataKey].push({
identifier: itm.identifier || '',
label: _this.extractLabelFromAttribute(itm.label),
value: (metadataKey === 'links') ? itm.value : _this.addLinksToUris(itm.value)
});
}
});
});
if (_this.manifest.logo) {
var logo = '';
if (typeof _this.manifest.logo === "string") {
logo = _this.manifest.logo;
} else if (typeof _this.manifest.logo['@id'] !== 'undefined') {
logo = _this.manifest.logo['@id'];
}
tplData.logo = logo;
}
var noMetadata = jQuery.isEmptyObject(this.imageMetadataTypes.details) &&
jQuery.isEmptyObject(this.imageMetadataTypes.rights) &&
jQuery.isEmptyObject(this.imageMetadataTypes.links);
return noMetadata ? null : tplData;
},
/* End of copy and adaptation of all helpers from metadataView. */
/* Adaptation of helpers from canvasLink.js to inject tab */
/* adds the locales to the internationalization module of the œviewer */
addLocalesToViewer: function() {
var currentLocales = {};
for (var language in this.locales) {
currentLocales = this.locales[language];
i18next.addResources(
language, 'translation',
currentLocales
);
}
},
/* injects the panel to the dom */
injectPanelToViewerWindow: function(viewerWindow, canvasId) {
var tplData = this.extractInformationFromWindow(viewerWindow, canvasId);
var metadataPanel = tplData ? this.template(tplData) : this.emptyTemplate();
// TODO Don't use mirador-container, but this.element (there may be multiple viewer and multiple item in a viewer).
// Append the first time, else replace with the metadata of the current image.
if (jQuery('.mirador-container .sidePanel .tabContentArea > .metadataPanel').length == 0) {
// The toc is set in all cases, but there may be no tab on init, and in
// that case, the tabs are hidden. See tabs.js.
var hasTabs = jQuery('.mirador-container .sidePanel > .tabGroup li').length > 0;
if (hasTabs) {
jQuery('.mirador-container .sidePanel > .tabGroup').append(this.metadataTabTemplate);
} else {
// jQuery('.mirador-container .sidePanel > .tabGroup').hide();
jQuery('.mirador-container .sidePanel > .tabGroup').append(this.tocTabTemplate);
jQuery('.mirador-container .sidePanel > .tabGroup .tocTab').addClass('selected');
}
metadataPanel = '<div class="metadataPanel" style="display: none;">' + metadataPanel + '</div>';
jQuery('.mirador-container .sidePanel > .tabGroup').append(this.metadataTabTemplate);
jQuery('.mirador-container .sidePanel .tabContentArea').append(metadataPanel);
} else {
jQuery('.mirador-container .sidePanel .tabContentArea > .metadataPanel').html(metadataPanel);
}
},
/* injects the needed viewer event handler */
injectViewerEventHandler: function() {
var this_ = this;
var origFunc = Mirador.Viewer.prototype.setupViewer;
Mirador.Viewer.prototype.setupViewer = function() {
origFunc.apply(this);
var options = this.state.getStateProperty('metadataTab');
if (jQuery.isPlainObject(options)) {
this_.options = options;
}
};
},
/* Injects the content of the initial image (canvasId is the current ui.*/
injectWindowEventHandler: function() {
var _this = this;
var origFunc = Mirador.Window.prototype.init;
Mirador.Window.prototype.init = function() {
var _thisViewerWindow = this;
// Update the metadata for the current image.
this.eventEmitter.subscribe("SET_CURRENT_CANVAS_ID." + this.id, function(evt, canvasId) {
_this.injectPanelToViewerWindow(_thisViewerWindow, canvasId);
});
// TODO Subscribe and/or publish state of tabs (selected). Simple jQuery works for now.
jQuery(document).on('click', '.mirador-container .sidePanel > .tabGroup .tab', function(e) {
// In Mirador, div panes are not the same than tabs, so a list is needed.
var tab = jQuery(this);
var tabs = {
'tocTab': '.toc',
'searchTab': '.search-result',
'layersTab': '.layersPanel',
'metadataTab': '.metadataPanel',
};
var tabId = tab.data('tabid');
tab.parent().find('.tab').removeClass('selected');
tab.addClass('selected');
tab.closest('.sidePanel').find('.tabContentArea > ul, .tabContentArea > div').hide();
tab.closest('.sidePanel').find('.tabContentArea > ' + tabs[tabId]).show();
});
// Initial callback: Mirador is loaded and the canvas is set.
jQuery(document).ready(function() {
_this.injectPanelToViewerWindow(_thisViewerWindow);
});
origFunc.apply(this);
}
},
/* End of adaptation of helpers from canvasLink.js */
/* initializes the plugin */
init: function() {
var _this = this;
i18next.on('initialized', function() {
this.addLocalesToViewer();
}.bind(this));
this.injectViewerEventHandler();
this.injectWindowEventHandler();
},
}
$(document).ready(function() {
MetadataTab.init();
});