Skip to content

Commit

Permalink
fixed #519
Browse files Browse the repository at this point in the history
  • Loading branch information
edsilv committed Mar 7, 2018
1 parent 50aa2b5 commit 0d29b1a
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 27 deletions.
2 changes: 1 addition & 1 deletion dist/iiif-metadata-component.bundle.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion dist/iiif-metadata-component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ declare namespace IIIFComponents {
private _render();
private _buildMetadataGroup(metadataGroup);
private _buildMetadataItem(item);
private _getItemLocale(item);
private _getLabelLocale(item);
private _getValueLocale(item);
private _buildMetadataItemValue(value, locale);
private _buildMetadataItemURIValue(value);
private _addReadingDirection($elem, locale);
Expand Down
33 changes: 26 additions & 7 deletions dist/iiif-metadata-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion dist/iiif-metadata-component.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
<!-- <button class="testFixture" data-json="https://iiif.riksarkivet.se/arkis!F0001185/manifest">Merge source reference</button> -->
<button class="testFixture" data-json="https://edsilv.github.io/test-manifests/all-metadata.json">All metadata</button>
<button class="testFixture" data-json="http://tomcrane.github.io/scratch/manifests/vdc_100000004987.0x000001.json">RTL</button>

<!-- resolves to http://bluemountain.princeton.edu/exist/restxq/iiif/bmtnaab_1920-02_01/manifest -->
<button class="testFixture" data-json="http://bluemountain.princeton.edu/exist/restxq/iiif/collection/top">Collection</button>

<button class="testFixture" data-json="https://edsilv.github.io/test-manifests/range-renderings.json">Multilingual</button>
</div>

Expand All @@ -47,6 +50,7 @@
<input id="allLanguages" type="checkbox" /><label for="allLanguages">Show All Languages</label>&nbsp;
<input id="locale1" checked type="radio" name="locale" value="en-gb"><label for="locale1">en-GB</label>
<input id="locale2" type="radio" name="locale" value="cy-gb"><label for="locale2">cy-GB</label>
<input id="locale3" type="radio" name="locale" value="ar"><label for="locale2">ar</label>
</div>

</div>
Expand Down
33 changes: 26 additions & 7 deletions examples/js/iiif-metadata-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
49 changes: 40 additions & 9 deletions src/MetadataComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -360,14 +360,14 @@ namespace IIIFComponents {
$label.html(<string>label);

// rtl?
this._addReadingDirection($label, this._getItemLocale(item));
this._addReadingDirection($label, this._getLabelLocale(item));

$metadataItem.addClass(Utils.Strings.toCssClass(<string>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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0d29b1a

Please sign in to comment.