From 0d29b1a4e921b5447e96e9e13d67e98d4583d3a5 Mon Sep 17 00:00:00 2001 From: edsilv Date: Wed, 7 Mar 2018 13:26:40 +0000 Subject: [PATCH] fixed #519 --- dist/iiif-metadata-component.bundle.js | 2 +- dist/iiif-metadata-component.d.ts | 3 +- dist/iiif-metadata-component.js | 33 +++++++++++++---- dist/iiif-metadata-component.min.js | 2 +- examples/index.html | 4 +++ examples/js/iiif-metadata-component.js | 33 +++++++++++++---- package.json | 2 +- src/MetadataComponent.ts | 49 +++++++++++++++++++++----- 8 files changed, 101 insertions(+), 27 deletions(-) diff --git a/dist/iiif-metadata-component.bundle.js b/dist/iiif-metadata-component.bundle.js index 2c3f881..79de02f 100644 --- a/dist/iiif-metadata-component.bundle.js +++ b/dist/iiif-metadata-component.bundle.js @@ -1174,4 +1174,4 @@ global.Utils = module.exports = Utils; }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{}]},{},[1])(1) }); -!function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{var g;g="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,g.iiifMetadataComponent=f()}}(function(){return function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o
'),this._$metadataItemTemplate=$('
'),this._$metadataItemValueTemplate=$('
'),this._$metadataItemURIValueTemplate=$(''),this._$copyTextTemplate=$('
'+this._data.content.copiedToClipboard+"
"),this._$metadataGroups=$('
'),this._$element.append(this._$metadataGroups),this._$noData=$('
'+this._data.content.noData+"
"),this._$element.append(this._$noData),success},MetadataComponent.prototype.data=function(){return{aggregateValues:"",canvases:null,canvasDisplayOrder:"",metadataGroupOrder:"",canvasExclude:"",canvasLabels:"",content:{attribution:"Attribution",canvasHeader:"About the canvas",copiedToClipboard:"Copied to clipboard",copyToClipboard:"Copy to clipboard",description:"Description",imageHeader:"About the image",less:"less",license:"License",logo:"Logo",manifestHeader:"About the item",more:"more",noData:"No data to display",rangeHeader:"About the range",sequenceHeader:"About the sequence"},copiedMessageDuration:2e3,copyToClipboardEnabled:!1,helper:null,licenseFormatter:null,limit:4,limitType:IIIFComponents.MetadataComponentOptions.LimitType.LINES,manifestDisplayOrder:"",manifestExclude:"",range:null,rtlLanguageCodes:"ar, ara, dv, div, he, heb, ur, urd",sanitizer:function(html){return html},showAllLanguages:!1}},MetadataComponent.prototype._getManifestGroup=function(){return this._metadataGroups.en().where(function(x){return x.resource.isManifest()}).first()},MetadataComponent.prototype._getCanvasGroups=function(){return this._metadataGroups.en().where(function(x){return x.resource.isCanvas()}).toArray()},MetadataComponent.prototype.set=function(data){var _this=this;if($.extend(this._data,data),this._data&&this._data.helper){var options={canvases:this._data.canvases,licenseFormatter:this._data.licenseFormatter,range:this._data.range};if(this._metadataGroups=this._data.helper.getMetadata(options),this._data.manifestDisplayOrder){var manifestGroup=this._getManifestGroup();manifestGroup.items=this._sortItems(manifestGroup.items,this._readCSV(this._data.manifestDisplayOrder))}if(this._data.canvasDisplayOrder){var canvasGroups=this._getCanvasGroups();canvasGroups.forEach(function(canvasGroup,index){canvasGroup.items=_this._sortItems(canvasGroup.items,_this._readCSV(_this._data.canvasDisplayOrder))})}if(this._data.metadataGroupOrder&&(this._metadataGroups=this._sortGroups(this._metadataGroups,this._readCSV(this._data.metadataGroupOrder))),this._data.canvasLabels&&this._label(this._getCanvasGroups(),this._readCSV(this._data.canvasLabels,!1)),this._data.manifestExclude){var manifestGroup=this._getManifestGroup();manifestGroup.items=this._exclude(manifestGroup.items,this._readCSV(this._data.manifestExclude))}if(this._data.canvasExclude){var canvasGroups=this._getCanvasGroups();canvasGroups.forEach(function(canvasGroup,index){canvasGroup.items=_this._exclude(canvasGroup.items,_this._readCSV(_this._data.canvasExclude))})}this._render()}},MetadataComponent.prototype._sortItems=function(items,displayOrder){var _this=this,sorted=[],unsorted=items.slice(0);return displayOrder.forEach(function(item,index){var match=unsorted.en().where(function(x){return _this._normalise(x.getLabel())===item}).first();if(match){sorted.push(match);var index_1=unsorted.indexOf(match);index_1>-1&&unsorted.splice(index_1,1)}}),unsorted.forEach(function(item,index){sorted.push(item)}),sorted},MetadataComponent.prototype._sortGroups=function(groups,metadataGroupOrder){var sorted=[],unsorted=groups.slice(0);return metadataGroupOrder.forEach(function(group,index){var match=unsorted.en().where(function(x){return x.resource.constructor.name.toLowerCase()==group}).first();if(match){sorted.push(match);var index_2=unsorted.indexOf(match);index_2>-1&&unsorted.splice(index_2,1)}}),sorted},MetadataComponent.prototype._label=function(groups,labels){groups.forEach(function(group,index){group.label=labels[index]})},MetadataComponent.prototype._exclude=function(items,excludeConfig){var _this=this;return excludeConfig.forEach(function(item,index){var match=items.en().where(function(x){return _this._normalise(x.getLabel())===item}).first();if(match){var index_3=items.indexOf(match);index_3>-1&&items.splice(index_3,1)}}),items},MetadataComponent.prototype._normalise=function(value){return value?value.toLowerCase().replace(/ /g,""):null},MetadataComponent.prototype._render=function(){var _this=this;return this._metadataGroups.length?(this._$noData.hide(),this._$metadataGroups.empty(),void this._metadataGroups.forEach(function(metadataGroup,index){var $metadataGroup=_this._buildMetadataGroup(metadataGroup);_this._$metadataGroups.append($metadataGroup),_this._data.limitType===IIIFComponents.MetadataComponentOptions.LimitType.LINES?$metadataGroup.find(".value").toggleExpandTextByLines(_this._data.limit,_this._data.content.less,_this._data.content.more,function(){}):_this._data.limitType===IIIFComponents.MetadataComponentOptions.LimitType.CHARS&&$metadataGroup.find(".value").ellipsisHtmlFixed(_this._data.limit,function(){})})):void this._$noData.show()},MetadataComponent.prototype._buildMetadataGroup=function(metadataGroup){var $metadataGroup=this._$metadataGroupTemplate.clone(),$header=$metadataGroup.find(">.header");if(metadataGroup.resource.isManifest()&&this._data.content.manifestHeader)$header.html(this._sanitize(this._data.content.manifestHeader));else if(metadataGroup.resource.isSequence()&&this._data.content.sequenceHeader)$header.html(this._sanitize(this._data.content.sequenceHeader));else if(metadataGroup.resource.isRange()&&this._data.content.rangeHeader)$header.html(this._sanitize(this._data.content.rangeHeader));else if(metadataGroup.resource.isCanvas()&&(metadataGroup.label||this._data.content.canvasHeader)){var header=metadataGroup.label||this._data.content.canvasHeader;$header.html(this._sanitize(header))}else metadataGroup.resource.isAnnotation()&&this._data.content.imageHeader&&$header.html(this._sanitize(this._data.content.imageHeader));$header.text()||$header.hide();for(var $items=$metadataGroup.find(".items"),i=0;i1)for(var i=0;i");var $value=this._$metadataItemValueTemplate.clone();return $value.html(value),$value.targetBlank(),locale&&this._addReadingDirection($value,locale),$value},MetadataComponent.prototype._buildMetadataItemURIValue=function(value){value=this._sanitize(value);var $value=this._$metadataItemURIValueTemplate.clone();return $value.prop("href",value),$value.text(value),$value},MetadataComponent.prototype._addReadingDirection=function($elem,locale){locale=Manifesto.Utils.getInexactLocale(locale);var rtlLanguages=this._readCSV(this._data.rtlLanguageCodes),match=rtlLanguages.en().where(function(x){return x===locale}).toArray().length>0;match&&($elem.prop("dir","rtl"),$elem.addClass("rtl"))},MetadataComponent.prototype._addCopyButton=function($elem,$header,$values){var $copyBtn=this._$copyTextTemplate.clone(),$copiedText=$copyBtn.children();$header.append($copyBtn),Utils.Device.isTouch()?$copyBtn.show():($elem.on("mouseenter",function(){$copyBtn.show()}),$elem.on("mouseleave",function(){$copyBtn.hide()}),$copyBtn.on("mouseleave",function(){$copiedText.hide()}));var that=this,originalValue=$values.text();$copyBtn.on("click",function(e){that._copyItemValues($copyBtn,originalValue)})},MetadataComponent.prototype._copyItemValues=function($copyButton,originalValue){Utils.Clipboard.copy(originalValue);var $copiedText=$copyButton.find(".copiedText");$copiedText.show(),setTimeout(function(){$copiedText.hide()},this._data.copiedMessageDuration)},MetadataComponent.prototype._readCSV=function(config,normalise){void 0===normalise&&(normalise=!0);var csv=[];if(config&&(csv=config.split(","),normalise))for(var i=0;i
'),this._$metadataItemTemplate=$('
'),this._$metadataItemValueTemplate=$('
'),this._$metadataItemURIValueTemplate=$(''),this._$copyTextTemplate=$('
'+this._data.content.copiedToClipboard+"
"),this._$metadataGroups=$('
'),this._$element.append(this._$metadataGroups),this._$noData=$('
'+this._data.content.noData+"
"),this._$element.append(this._$noData),success},MetadataComponent.prototype.data=function(){return{aggregateValues:"",canvases:null,canvasDisplayOrder:"",metadataGroupOrder:"",canvasExclude:"",canvasLabels:"",content:{attribution:"Attribution",canvasHeader:"About the canvas",copiedToClipboard:"Copied to clipboard",copyToClipboard:"Copy to clipboard",description:"Description",imageHeader:"About the image",less:"less",license:"License",logo:"Logo",manifestHeader:"About the item",more:"more",noData:"No data to display",rangeHeader:"About the range",sequenceHeader:"About the sequence"},copiedMessageDuration:2e3,copyToClipboardEnabled:!1,helper:null,licenseFormatter:null,limit:4,limitType:IIIFComponents.MetadataComponentOptions.LimitType.LINES,manifestDisplayOrder:"",manifestExclude:"",range:null,rtlLanguageCodes:"ar, ara, dv, div, he, heb, ur, urd",sanitizer:function(html){return html},showAllLanguages:!1}},MetadataComponent.prototype._getManifestGroup=function(){return this._metadataGroups.en().where(function(x){return x.resource.isManifest()}).first()},MetadataComponent.prototype._getCanvasGroups=function(){return this._metadataGroups.en().where(function(x){return x.resource.isCanvas()}).toArray()},MetadataComponent.prototype.set=function(data){var _this=this;if($.extend(this._data,data),this._data&&this._data.helper){var options={canvases:this._data.canvases,licenseFormatter:this._data.licenseFormatter,range:this._data.range};if(this._metadataGroups=this._data.helper.getMetadata(options),this._data.manifestDisplayOrder){var manifestGroup=this._getManifestGroup();manifestGroup.items=this._sortItems(manifestGroup.items,this._readCSV(this._data.manifestDisplayOrder))}if(this._data.canvasDisplayOrder){var canvasGroups=this._getCanvasGroups();canvasGroups.forEach(function(canvasGroup,index){canvasGroup.items=_this._sortItems(canvasGroup.items,_this._readCSV(_this._data.canvasDisplayOrder))})}if(this._data.metadataGroupOrder&&(this._metadataGroups=this._sortGroups(this._metadataGroups,this._readCSV(this._data.metadataGroupOrder))),this._data.canvasLabels&&this._label(this._getCanvasGroups(),this._readCSV(this._data.canvasLabels,!1)),this._data.manifestExclude){var manifestGroup=this._getManifestGroup();manifestGroup.items=this._exclude(manifestGroup.items,this._readCSV(this._data.manifestExclude))}if(this._data.canvasExclude){var canvasGroups=this._getCanvasGroups();canvasGroups.forEach(function(canvasGroup,index){canvasGroup.items=_this._exclude(canvasGroup.items,_this._readCSV(_this._data.canvasExclude))})}this._render()}},MetadataComponent.prototype._sortItems=function(items,displayOrder){var _this=this,sorted=[],unsorted=items.slice(0);return displayOrder.forEach(function(item,index){var match=unsorted.en().where(function(x){return _this._normalise(x.getLabel())===item}).first();if(match){sorted.push(match);var index_1=unsorted.indexOf(match);index_1>-1&&unsorted.splice(index_1,1)}}),unsorted.forEach(function(item,index){sorted.push(item)}),sorted},MetadataComponent.prototype._sortGroups=function(groups,metadataGroupOrder){var sorted=[],unsorted=groups.slice(0);return metadataGroupOrder.forEach(function(group,index){var match=unsorted.en().where(function(x){return x.resource.constructor.name.toLowerCase()==group}).first();if(match){sorted.push(match);var index_2=unsorted.indexOf(match);index_2>-1&&unsorted.splice(index_2,1)}}),sorted},MetadataComponent.prototype._label=function(groups,labels){groups.forEach(function(group,index){group.label=labels[index]})},MetadataComponent.prototype._exclude=function(items,excludeConfig){var _this=this;return excludeConfig.forEach(function(item,index){var match=items.en().where(function(x){return _this._normalise(x.getLabel())===item}).first();if(match){var index_3=items.indexOf(match);index_3>-1&&items.splice(index_3,1)}}),items},MetadataComponent.prototype._normalise=function(value){return value?value.toLowerCase().replace(/ /g,""):null},MetadataComponent.prototype._render=function(){var _this=this;return this._metadataGroups.length?(this._$noData.hide(),this._$metadataGroups.empty(),void this._metadataGroups.forEach(function(metadataGroup,index){var $metadataGroup=_this._buildMetadataGroup(metadataGroup);_this._$metadataGroups.append($metadataGroup),_this._data.limitType===IIIFComponents.MetadataComponentOptions.LimitType.LINES?$metadataGroup.find(".value").toggleExpandTextByLines(_this._data.limit,_this._data.content.less,_this._data.content.more,function(){}):_this._data.limitType===IIIFComponents.MetadataComponentOptions.LimitType.CHARS&&$metadataGroup.find(".value").ellipsisHtmlFixed(_this._data.limit,function(){})})):void this._$noData.show()},MetadataComponent.prototype._buildMetadataGroup=function(metadataGroup){var $metadataGroup=this._$metadataGroupTemplate.clone(),$header=$metadataGroup.find(">.header");if(metadataGroup.resource.isManifest()&&this._data.content.manifestHeader)$header.html(this._sanitize(this._data.content.manifestHeader));else if(metadataGroup.resource.isSequence()&&this._data.content.sequenceHeader)$header.html(this._sanitize(this._data.content.sequenceHeader));else if(metadataGroup.resource.isRange()&&this._data.content.rangeHeader)$header.html(this._sanitize(this._data.content.rangeHeader));else if(metadataGroup.resource.isCanvas()&&(metadataGroup.label||this._data.content.canvasHeader)){var header=metadataGroup.label||this._data.content.canvasHeader;$header.html(this._sanitize(header))}else metadataGroup.resource.isAnnotation()&&this._data.content.imageHeader&&$header.html(this._sanitize(this._data.content.imageHeader));$header.text()||$header.hide();for(var $items=$metadataGroup.find(".items"),i=0;i1)for(var i=0;i");var $value=this._$metadataItemValueTemplate.clone();return $value.html(value),$value.targetBlank(),locale&&this._addReadingDirection($value,locale),$value},MetadataComponent.prototype._buildMetadataItemURIValue=function(value){value=this._sanitize(value);var $value=this._$metadataItemURIValueTemplate.clone();return $value.prop("href",value),$value.text(value),$value},MetadataComponent.prototype._addReadingDirection=function($elem,locale){locale=Manifesto.Utils.getInexactLocale(locale);var rtlLanguages=this._readCSV(this._data.rtlLanguageCodes),match=rtlLanguages.en().where(function(x){return x===locale}).toArray().length>0;match&&($elem.prop("dir","rtl"),$elem.addClass("rtl"))},MetadataComponent.prototype._addCopyButton=function($elem,$header,$values){var $copyBtn=this._$copyTextTemplate.clone(),$copiedText=$copyBtn.children();$header.append($copyBtn),Utils.Device.isTouch()?$copyBtn.show():($elem.on("mouseenter",function(){$copyBtn.show()}),$elem.on("mouseleave",function(){$copyBtn.hide()}),$copyBtn.on("mouseleave",function(){$copiedText.hide()}));var that=this,originalValue=$values.text();$copyBtn.on("click",function(e){that._copyItemValues($copyBtn,originalValue)})},MetadataComponent.prototype._copyItemValues=function($copyButton,originalValue){Utils.Clipboard.copy(originalValue);var $copiedText=$copyButton.find(".copiedText");$copiedText.show(),setTimeout(function(){$copiedText.hide()},this._data.copiedMessageDuration)},MetadataComponent.prototype._readCSV=function(config,normalise){void 0===normalise&&(normalise=!0);var csv=[];if(config&&(csv=config.split(","),normalise))for(var i=0;i
'),this._$metadataItemTemplate=$('
'),this._$metadataItemValueTemplate=$('
'),this._$metadataItemURIValueTemplate=$(''),this._$copyTextTemplate=$('
'+this._data.content.copiedToClipboard+"
"),this._$metadataGroups=$('
'),this._$element.append(this._$metadataGroups),this._$noData=$('
'+this._data.content.noData+"
"),this._$element.append(this._$noData),success},MetadataComponent.prototype.data=function(){return{aggregateValues:"",canvases:null,canvasDisplayOrder:"",metadataGroupOrder:"",canvasExclude:"",canvasLabels:"",content:{attribution:"Attribution",canvasHeader:"About the canvas",copiedToClipboard:"Copied to clipboard",copyToClipboard:"Copy to clipboard",description:"Description",imageHeader:"About the image",less:"less",license:"License",logo:"Logo",manifestHeader:"About the item",more:"more",noData:"No data to display",rangeHeader:"About the range",sequenceHeader:"About the sequence"},copiedMessageDuration:2e3,copyToClipboardEnabled:!1,helper:null,licenseFormatter:null,limit:4,limitType:IIIFComponents.MetadataComponentOptions.LimitType.LINES,manifestDisplayOrder:"",manifestExclude:"",range:null,rtlLanguageCodes:"ar, ara, dv, div, he, heb, ur, urd",sanitizer:function(html){return html},showAllLanguages:!1}},MetadataComponent.prototype._getManifestGroup=function(){return this._metadataGroups.en().where(function(x){return x.resource.isManifest()}).first()},MetadataComponent.prototype._getCanvasGroups=function(){return this._metadataGroups.en().where(function(x){return x.resource.isCanvas()}).toArray()},MetadataComponent.prototype.set=function(data){var _this=this;if($.extend(this._data,data),this._data&&this._data.helper){var options={canvases:this._data.canvases,licenseFormatter:this._data.licenseFormatter,range:this._data.range};if(this._metadataGroups=this._data.helper.getMetadata(options),this._data.manifestDisplayOrder){var manifestGroup=this._getManifestGroup();manifestGroup.items=this._sortItems(manifestGroup.items,this._readCSV(this._data.manifestDisplayOrder))}if(this._data.canvasDisplayOrder){var canvasGroups=this._getCanvasGroups();canvasGroups.forEach(function(canvasGroup,index){canvasGroup.items=_this._sortItems(canvasGroup.items,_this._readCSV(_this._data.canvasDisplayOrder))})}if(this._data.metadataGroupOrder&&(this._metadataGroups=this._sortGroups(this._metadataGroups,this._readCSV(this._data.metadataGroupOrder))),this._data.canvasLabels&&this._label(this._getCanvasGroups(),this._readCSV(this._data.canvasLabels,!1)),this._data.manifestExclude){var manifestGroup=this._getManifestGroup();manifestGroup.items=this._exclude(manifestGroup.items,this._readCSV(this._data.manifestExclude))}if(this._data.canvasExclude){var canvasGroups=this._getCanvasGroups();canvasGroups.forEach(function(canvasGroup,index){canvasGroup.items=_this._exclude(canvasGroup.items,_this._readCSV(_this._data.canvasExclude))})}this._render()}},MetadataComponent.prototype._sortItems=function(items,displayOrder){var _this=this,sorted=[],unsorted=items.slice(0);return displayOrder.forEach(function(item,index){var match=unsorted.en().where(function(x){return _this._normalise(x.getLabel())===item}).first();if(match){sorted.push(match);var index_1=unsorted.indexOf(match);index_1>-1&&unsorted.splice(index_1,1)}}),unsorted.forEach(function(item,index){sorted.push(item)}),sorted},MetadataComponent.prototype._sortGroups=function(groups,metadataGroupOrder){var sorted=[],unsorted=groups.slice(0);return metadataGroupOrder.forEach(function(group,index){var match=unsorted.en().where(function(x){return x.resource.constructor.name.toLowerCase()==group}).first();if(match){sorted.push(match);var index_2=unsorted.indexOf(match);index_2>-1&&unsorted.splice(index_2,1)}}),sorted},MetadataComponent.prototype._label=function(groups,labels){groups.forEach(function(group,index){group.label=labels[index]})},MetadataComponent.prototype._exclude=function(items,excludeConfig){var _this=this;return excludeConfig.forEach(function(item,index){var match=items.en().where(function(x){return _this._normalise(x.getLabel())===item}).first();if(match){var index_3=items.indexOf(match);index_3>-1&&items.splice(index_3,1)}}),items},MetadataComponent.prototype._normalise=function(value){return value?value.toLowerCase().replace(/ /g,""):null},MetadataComponent.prototype._render=function(){var _this=this;return this._metadataGroups.length?(this._$noData.hide(),this._$metadataGroups.empty(),void this._metadataGroups.forEach(function(metadataGroup,index){var $metadataGroup=_this._buildMetadataGroup(metadataGroup);_this._$metadataGroups.append($metadataGroup),_this._data.limitType===IIIFComponents.MetadataComponentOptions.LimitType.LINES?$metadataGroup.find(".value").toggleExpandTextByLines(_this._data.limit,_this._data.content.less,_this._data.content.more,function(){}):_this._data.limitType===IIIFComponents.MetadataComponentOptions.LimitType.CHARS&&$metadataGroup.find(".value").ellipsisHtmlFixed(_this._data.limit,function(){})})):void this._$noData.show()},MetadataComponent.prototype._buildMetadataGroup=function(metadataGroup){var $metadataGroup=this._$metadataGroupTemplate.clone(),$header=$metadataGroup.find(">.header");if(metadataGroup.resource.isManifest()&&this._data.content.manifestHeader)$header.html(this._sanitize(this._data.content.manifestHeader));else if(metadataGroup.resource.isSequence()&&this._data.content.sequenceHeader)$header.html(this._sanitize(this._data.content.sequenceHeader));else if(metadataGroup.resource.isRange()&&this._data.content.rangeHeader)$header.html(this._sanitize(this._data.content.rangeHeader));else if(metadataGroup.resource.isCanvas()&&(metadataGroup.label||this._data.content.canvasHeader)){var header=metadataGroup.label||this._data.content.canvasHeader;$header.html(this._sanitize(header))}else metadataGroup.resource.isAnnotation()&&this._data.content.imageHeader&&$header.html(this._sanitize(this._data.content.imageHeader));$header.text()||$header.hide();for(var $items=$metadataGroup.find(".items"),i=0;i1)for(var i=0;i");var $value=this._$metadataItemValueTemplate.clone();return $value.html(value),$value.targetBlank(),locale&&this._addReadingDirection($value,locale),$value},MetadataComponent.prototype._buildMetadataItemURIValue=function(value){value=this._sanitize(value);var $value=this._$metadataItemURIValueTemplate.clone();return $value.prop("href",value),$value.text(value),$value},MetadataComponent.prototype._addReadingDirection=function($elem,locale){locale=Manifesto.Utils.getInexactLocale(locale);var rtlLanguages=this._readCSV(this._data.rtlLanguageCodes),match=rtlLanguages.en().where(function(x){return x===locale}).toArray().length>0;match&&($elem.prop("dir","rtl"),$elem.addClass("rtl"))},MetadataComponent.prototype._addCopyButton=function($elem,$header,$values){var $copyBtn=this._$copyTextTemplate.clone(),$copiedText=$copyBtn.children();$header.append($copyBtn),Utils.Device.isTouch()?$copyBtn.show():($elem.on("mouseenter",function(){$copyBtn.show()}),$elem.on("mouseleave",function(){$copyBtn.hide()}),$copyBtn.on("mouseleave",function(){$copiedText.hide()}));var that=this,originalValue=$values.text();$copyBtn.on("click",function(e){that._copyItemValues($copyBtn,originalValue)})},MetadataComponent.prototype._copyItemValues=function($copyButton,originalValue){Utils.Clipboard.copy(originalValue);var $copiedText=$copyButton.find(".copiedText");$copiedText.show(),setTimeout(function(){$copiedText.hide()},this._data.copiedMessageDuration)},MetadataComponent.prototype._readCSV=function(config,normalise){void 0===normalise&&(normalise=!0);var csv=[];if(config&&(csv=config.split(","),normalise))for(var i=0;i
'),this._$metadataItemTemplate=$('
'),this._$metadataItemValueTemplate=$('
'),this._$metadataItemURIValueTemplate=$(''),this._$copyTextTemplate=$('
'+this._data.content.copiedToClipboard+"
"),this._$metadataGroups=$('
'),this._$element.append(this._$metadataGroups),this._$noData=$('
'+this._data.content.noData+"
"),this._$element.append(this._$noData),success},MetadataComponent.prototype.data=function(){return{aggregateValues:"",canvases:null,canvasDisplayOrder:"",metadataGroupOrder:"",canvasExclude:"",canvasLabels:"",content:{attribution:"Attribution",canvasHeader:"About the canvas",copiedToClipboard:"Copied to clipboard",copyToClipboard:"Copy to clipboard",description:"Description",imageHeader:"About the image",less:"less",license:"License",logo:"Logo",manifestHeader:"About the item",more:"more",noData:"No data to display",rangeHeader:"About the range",sequenceHeader:"About the sequence"},copiedMessageDuration:2e3,copyToClipboardEnabled:!1,helper:null,licenseFormatter:null,limit:4,limitType:IIIFComponents.MetadataComponentOptions.LimitType.LINES,manifestDisplayOrder:"",manifestExclude:"",range:null,rtlLanguageCodes:"ar, ara, dv, div, he, heb, ur, urd",sanitizer:function(html){return html},showAllLanguages:!1}},MetadataComponent.prototype._getManifestGroup=function(){return this._metadataGroups.en().where(function(x){return x.resource.isManifest()}).first()},MetadataComponent.prototype._getCanvasGroups=function(){return this._metadataGroups.en().where(function(x){return x.resource.isCanvas()}).toArray()},MetadataComponent.prototype.set=function(data){var _this=this;if($.extend(this._data,data),this._data&&this._data.helper){var options={canvases:this._data.canvases,licenseFormatter:this._data.licenseFormatter,range:this._data.range};if(this._metadataGroups=this._data.helper.getMetadata(options),this._data.manifestDisplayOrder){var manifestGroup=this._getManifestGroup();manifestGroup.items=this._sortItems(manifestGroup.items,this._readCSV(this._data.manifestDisplayOrder))}if(this._data.canvasDisplayOrder){var canvasGroups=this._getCanvasGroups();canvasGroups.forEach(function(canvasGroup,index){canvasGroup.items=_this._sortItems(canvasGroup.items,_this._readCSV(_this._data.canvasDisplayOrder))})}if(this._data.metadataGroupOrder&&(this._metadataGroups=this._sortGroups(this._metadataGroups,this._readCSV(this._data.metadataGroupOrder))),this._data.canvasLabels&&this._label(this._getCanvasGroups(),this._readCSV(this._data.canvasLabels,!1)),this._data.manifestExclude){var manifestGroup=this._getManifestGroup();manifestGroup.items=this._exclude(manifestGroup.items,this._readCSV(this._data.manifestExclude))}if(this._data.canvasExclude){var canvasGroups=this._getCanvasGroups();canvasGroups.forEach(function(canvasGroup,index){canvasGroup.items=_this._exclude(canvasGroup.items,_this._readCSV(_this._data.canvasExclude))})}this._render()}},MetadataComponent.prototype._sortItems=function(items,displayOrder){var _this=this,sorted=[],unsorted=items.slice(0);return displayOrder.forEach(function(item,index){var match=unsorted.en().where(function(x){return _this._normalise(x.getLabel())===item}).first();if(match){sorted.push(match);var index_1=unsorted.indexOf(match);index_1>-1&&unsorted.splice(index_1,1)}}),unsorted.forEach(function(item,index){sorted.push(item)}),sorted},MetadataComponent.prototype._sortGroups=function(groups,metadataGroupOrder){var sorted=[],unsorted=groups.slice(0);return metadataGroupOrder.forEach(function(group,index){var match=unsorted.en().where(function(x){return x.resource.constructor.name.toLowerCase()==group}).first();if(match){sorted.push(match);var index_2=unsorted.indexOf(match);index_2>-1&&unsorted.splice(index_2,1)}}),sorted},MetadataComponent.prototype._label=function(groups,labels){groups.forEach(function(group,index){group.label=labels[index]})},MetadataComponent.prototype._exclude=function(items,excludeConfig){var _this=this;return excludeConfig.forEach(function(item,index){var match=items.en().where(function(x){return _this._normalise(x.getLabel())===item}).first();if(match){var index_3=items.indexOf(match);index_3>-1&&items.splice(index_3,1)}}),items},MetadataComponent.prototype._normalise=function(value){return value?value.toLowerCase().replace(/ /g,""):null},MetadataComponent.prototype._render=function(){var _this=this;return this._metadataGroups.length?(this._$noData.hide(),this._$metadataGroups.empty(),void this._metadataGroups.forEach(function(metadataGroup,index){var $metadataGroup=_this._buildMetadataGroup(metadataGroup);_this._$metadataGroups.append($metadataGroup),_this._data.limitType===IIIFComponents.MetadataComponentOptions.LimitType.LINES?$metadataGroup.find(".value").toggleExpandTextByLines(_this._data.limit,_this._data.content.less,_this._data.content.more,function(){}):_this._data.limitType===IIIFComponents.MetadataComponentOptions.LimitType.CHARS&&$metadataGroup.find(".value").ellipsisHtmlFixed(_this._data.limit,function(){})})):void this._$noData.show()},MetadataComponent.prototype._buildMetadataGroup=function(metadataGroup){var $metadataGroup=this._$metadataGroupTemplate.clone(),$header=$metadataGroup.find(">.header");if(metadataGroup.resource.isManifest()&&this._data.content.manifestHeader)$header.html(this._sanitize(this._data.content.manifestHeader));else if(metadataGroup.resource.isSequence()&&this._data.content.sequenceHeader)$header.html(this._sanitize(this._data.content.sequenceHeader));else if(metadataGroup.resource.isRange()&&this._data.content.rangeHeader)$header.html(this._sanitize(this._data.content.rangeHeader));else if(metadataGroup.resource.isCanvas()&&(metadataGroup.label||this._data.content.canvasHeader)){var header=metadataGroup.label||this._data.content.canvasHeader;$header.html(this._sanitize(header))}else metadataGroup.resource.isAnnotation()&&this._data.content.imageHeader&&$header.html(this._sanitize(this._data.content.imageHeader));$header.text()||$header.hide();for(var $items=$metadataGroup.find(".items"),i=0;i1)for(var i=0;i");var $value=this._$metadataItemValueTemplate.clone();return $value.html(value),$value.targetBlank(),locale&&this._addReadingDirection($value,locale),$value},MetadataComponent.prototype._buildMetadataItemURIValue=function(value){value=this._sanitize(value);var $value=this._$metadataItemURIValueTemplate.clone();return $value.prop("href",value),$value.text(value),$value},MetadataComponent.prototype._addReadingDirection=function($elem,locale){locale=Manifesto.Utils.getInexactLocale(locale);var rtlLanguages=this._readCSV(this._data.rtlLanguageCodes),match=rtlLanguages.en().where(function(x){return x===locale}).toArray().length>0;match&&($elem.prop("dir","rtl"),$elem.addClass("rtl"))},MetadataComponent.prototype._addCopyButton=function($elem,$header,$values){var $copyBtn=this._$copyTextTemplate.clone(),$copiedText=$copyBtn.children();$header.append($copyBtn),Utils.Device.isTouch()?$copyBtn.show():($elem.on("mouseenter",function(){$copyBtn.show()}),$elem.on("mouseleave",function(){$copyBtn.hide()}),$copyBtn.on("mouseleave",function(){$copiedText.hide()}));var that=this,originalValue=$values.text();$copyBtn.on("click",function(e){that._copyItemValues($copyBtn,originalValue)})},MetadataComponent.prototype._copyItemValues=function($copyButton,originalValue){Utils.Clipboard.copy(originalValue);var $copiedText=$copyButton.find(".copiedText");$copiedText.show(),setTimeout(function(){$copiedText.hide()},this._data.copiedMessageDuration)},MetadataComponent.prototype._readCSV=function(config,normalise){void 0===normalise&&(normalise=!0);var csv=[];if(config&&(csv=config.split(","),normalise))for(var i=0;iMerge source reference --> + + + @@ -47,6 +50,7 @@   + diff --git a/examples/js/iiif-metadata-component.js b/examples/js/iiif-metadata-component.js index f5a1967..d57270f 100644 --- a/examples/js/iiif-metadata-component.js +++ b/examples/js/iiif-metadata-component.js @@ -343,11 +343,11 @@ var IIIFComponents; label = this._sanitize(label); $label.html(label); // rtl? - this._addReadingDirection($label, this._getItemLocale(item)); + this._addReadingDirection($label, this._getLabelLocale(item)); $metadataItem.addClass(Utils.Strings.toCssClass(label)); var $value; // if the value is a URI - if (originalLabel && originalLabel.toLowerCase() === "license" && (urlPattern.exec(item.value[0].value) != null)) { + if (originalLabel && originalLabel.toLowerCase() === "license" && (urlPattern.exec(item.value[0].value) !== null)) { $value = this._buildMetadataItemURIValue(item.value[0].value); $values.append($value); } @@ -361,12 +361,12 @@ var IIIFComponents; } } else { - var itemLocale = this._getItemLocale(item); + var valueLocale = this._getValueLocale(item); var valueFound = false; // display all values in the item's locale for (var i = 0; i < item.value.length; i++) { var translation = item.value[i]; - if (itemLocale === translation.locale) { + if (valueLocale.toLowerCase() === translation.locale.toLowerCase()) { valueFound = true; $value = this._buildMetadataItemValue(translation.value, translation.locale); $values.append($value); @@ -387,12 +387,31 @@ var IIIFComponents; } return $metadataItem; }; - MetadataComponent.prototype._getItemLocale = function (item) { + MetadataComponent.prototype._getLabelLocale = function (item) { if (!this._data || !this._data.helper) { return ''; } - // the item's label locale takes precedence - return (item.label.length && item.label[0].locale) ? item.label[0].locale : item.defaultLocale || this._data.helper.options.locale; + var defaultLocale = this._data.helper.options.locale; + if (item.label.length) { + var labelLocale = item.label[0].locale; + if (labelLocale.toLowerCase() !== defaultLocale.toLowerCase()) { + return labelLocale; + } + } + return defaultLocale; + }; + MetadataComponent.prototype._getValueLocale = function (item) { + if (!this._data || !this._data.helper) { + return ''; + } + var defaultLocale = this._data.helper.options.locale; + // if (item.value.length) { + // const valueLocale: string = item.value[0].locale; + // if (valueLocale.toLowerCase() !== defaultLocale.toLowerCase()) { + // return valueLocale; + // } + // } + return defaultLocale; }; MetadataComponent.prototype._buildMetadataItemValue = function (value, locale) { value = this._sanitize(value); diff --git a/package.json b/package.json index 0246520..2883610 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iiif-metadata-component", - "version": "1.1.6", + "version": "1.1.7", "description": "", "main": "index.js", "types": "./dist/iiif-metadata-component.d.ts", diff --git a/src/MetadataComponent.ts b/src/MetadataComponent.ts index 9a85669..bd463ba 100644 --- a/src/MetadataComponent.ts +++ b/src/MetadataComponent.ts @@ -337,7 +337,7 @@ namespace IIIFComponents { const originalLabel: string | null = item.getLabel(); let label: string | null = originalLabel; - var urlPattern = new RegExp("/\w+:(\/?\/?)[^\s]+/gm", "i"); + const urlPattern = new RegExp("/\w+:(\/?\/?)[^\s]+/gm", "i"); if (label && item.isRootLevel) { switch (label.toLowerCase()) { @@ -360,14 +360,14 @@ namespace IIIFComponents { $label.html(label); // rtl? - this._addReadingDirection($label, this._getItemLocale(item)); + this._addReadingDirection($label, this._getLabelLocale(item)); $metadataItem.addClass(Utils.Strings.toCssClass(label)); let $value: JQuery; // if the value is a URI - if (originalLabel && originalLabel.toLowerCase() === "license" && (urlPattern.exec(item.value[0].value) != null)) { + if (originalLabel && originalLabel.toLowerCase() === "license" && (urlPattern.exec(item.value[0].value) !== null)) { $value = this._buildMetadataItemURIValue(item.value[0].value); $values.append($value); } else { @@ -381,14 +381,14 @@ namespace IIIFComponents { } } else { - const itemLocale: string = this._getItemLocale(item); + const valueLocale: string = this._getValueLocale(item); let valueFound: boolean = false; // display all values in the item's locale for (let i = 0; i < item.value.length; i++) { const translation: Manifesto.Translation = item.value[i]; - if (itemLocale === translation.locale) { + if (valueLocale.toLowerCase() === translation.locale.toLowerCase()) { valueFound = true; $value = this._buildMetadataItemValue(translation.value, translation.locale); $values.append($value); @@ -416,12 +416,43 @@ namespace IIIFComponents { return $metadataItem; } - private _getItemLocale(item: MetadataItem): string { + private _getLabelLocale(item: MetadataItem): string { if (!this._data || !this._data.helper) { - return ''; // todo: remove + return ''; } - // the item's label locale takes precedence - return (item.label.length && item.label[0].locale) ? item.label[0].locale : item.defaultLocale || this._data.helper.options.locale; + + const defaultLocale: string = this._data.helper.options.locale; + + if (item.label.length) { + + const labelLocale: string = item.label[0].locale; + + if (labelLocale.toLowerCase() !== defaultLocale.toLowerCase()) { + return labelLocale; + } + } + + return defaultLocale; + } + + private _getValueLocale(item: MetadataItem): string { + + if (!this._data || !this._data.helper) { + return ''; + } + + const defaultLocale: string = this._data.helper.options.locale; + + // if (item.value.length) { + + // const valueLocale: string = item.value[0].locale; + + // if (valueLocale.toLowerCase() !== defaultLocale.toLowerCase()) { + // return valueLocale; + // } + // } + + return defaultLocale; } private _buildMetadataItemValue(value: string, locale: string): JQuery {