Skip to content

Commit

Permalink
Push property localization down to getValues instead of part of parsi…
Browse files Browse the repository at this point in the history
…ng the property values.
  • Loading branch information
cbeer committed Jan 8, 2025
1 parent 6af39a4 commit d947941
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 51 deletions.
6 changes: 0 additions & 6 deletions __tests__/src/selectors/manifests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ describe('getManifestoInstance', () => {
const received = getManifestoInstance(state, { manifestId: 'x' });
expect(received.id).toEqual('http://iiif.io/api/presentation/2.1/example/fixtures/19/manifest.json');
});
it('is cached based off of input props', () => {
const state = { manifests: { x: { json: manifestFixture019 } } };
const received = getManifestoInstance(state, { manifestId: 'x' });
expect(getManifestoInstance(state, { manifestId: 'x' })).toBe(received);
expect(getManifestoInstance(state, { manifestId: 'x', windowId: 'y' })).not.toBe(received);
});
});

describe('getManifestLogo()', () => {
Expand Down
7 changes: 4 additions & 3 deletions src/state/selectors/canvases.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { miradorSlice } from './utils';
import { getWindow } from './getters';
import { getSequence } from './sequences';
import { getWindowViewType } from './windows';
import { getManifestLocale } from './manifests';

/**
* Returns the info response.
Expand Down Expand Up @@ -183,10 +184,10 @@ export const getPreviousCanvasGrouping = createSelector(
* @returns {string|number}
*/
export const getCanvasLabel = createSelector(
[getCanvas],
canvas => (canvas && (
[getCanvas, getManifestLocale],
(canvas, locale) => (canvas && (
canvas.getLabel().length > 0
? canvas.getLabel().getValue()
? canvas.getLabel().getValue(locale)
: String(canvas.index + 1)
)),
);
Expand Down
73 changes: 32 additions & 41 deletions src/state/selectors/manifests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createSelector } from 'reselect';
import { createCachedSelector } from 're-reselect';
import { PropertyValue, Utils, Resource } from 'manifesto.js';
import getThumbnail from '../../lib/ThumbnailFactory';
import asArray from '../../lib/asArray';
Expand All @@ -24,9 +23,10 @@ export const getLocale = createSelector(
[
getCompanionWindowLocale,
getConfig,
(state, { locale }) => locale,
],
(companionWindowLocale, config = {}) => (
companionWindowLocale || config.language
(companionWindowLocale, config = {}, locale) => (
locale || companionWindowLocale || config.language || config.fallbackLanguages
),
);

Expand Down Expand Up @@ -57,17 +57,9 @@ export const getManifestError = createSelector(
);

/** Instantiate a manifesto instance */
const getContextualManifestoInstance = createCachedSelector(
const getContextualManifestoInstance = createSelector(
getManifest,
getLocale,
(manifest, locale) => manifest
&& createManifestoInstance(manifest.json, locale),
)(
(state, { companionWindowId, manifestId, windowId }) => [
manifestId,
windowId,
getLocale(state, { companionWindowId }),
].join(' - '), // Cache key consisting of manifestId, windowId, and locale
manifest => manifest && createManifestoInstance(manifest.json),
);

/**
Expand All @@ -80,15 +72,14 @@ const getContextualManifestoInstance = createCachedSelector(
export const getManifestoInstance = createSelector(
getContextualManifestoInstance,
(state, { json }) => json,
getLocale,
(manifesto, manifestJson, locale) => (
(manifesto, manifestJson) => (
manifestJson && createManifestoInstance(manifestJson, locale)
) || manifesto,
);

export const getManifestLocale = createSelector(
[getManifestoInstance],
manifest => manifest && manifest.options && manifest.options.locale && manifest.options.locale.replace(/-.*$/, ''),
[getManifestoInstance, getLocale],
(manifest, locale) => locale ?? (manifest && manifest.options && manifest.options.locale && manifest.options.locale.replace(/-.*$/, '')),
);

/** */
Expand All @@ -114,7 +105,7 @@ export const getManifestProviderName = createSelector(
],
(provider, locale) => provider
&& provider[0].label
&& PropertyValue.parse(provider[0].label, locale).getValue(),
&& PropertyValue.parse(provider[0].label).getValue(locale),
);

/**
Expand Down Expand Up @@ -159,8 +150,8 @@ export const getManifestHomepage = createSelector(
(homepages, locale) => homepages
&& asArray(homepages).map(homepage => (
{
label: PropertyValue.parse(homepage.label, locale)
.getValue(),
label: PropertyValue.parse(homepage.label)
.getValue(locale),
value: homepage.id || homepage['@id'],
}
)),
Expand All @@ -175,11 +166,11 @@ export const getManifestHomepage = createSelector(
* @returns {string|null}
*/
export const getManifestRenderings = createSelector(
[getManifestoInstance],
manifest => manifest
[getManifestoInstance, getManifestLocale],
(manifest, locale) => manifest
&& manifest.getRenderings().map(rendering => (
{
label: rendering.getLabel().getValue(),
label: rendering.getLabel().getValue(locale),
value: rendering.id,
}
)),
Expand All @@ -202,8 +193,8 @@ export const getManifestSeeAlso = createSelector(
&& asArray(seeAlso).map(related => (
{
format: related.format,
label: PropertyValue.parse(related.label, locale)
.getValue(),
label: PropertyValue.parse(related.label)
.getValue(locale),
value: related.id || related['@id'],
}
)),
Expand Down Expand Up @@ -243,8 +234,8 @@ export const getManifestRelated = createSelector(
}
: {
format: related.format,
label: PropertyValue.parse(related.label, locale)
.getValue(),
label: PropertyValue.parse(related.label)
.getValue(locale),
value: related.id || related['@id'],
}
)),
Expand All @@ -259,13 +250,13 @@ export const getManifestRelated = createSelector(
* @returns {string|null}
*/
export const getRequiredStatement = createSelector(
[getManifestoInstance],
manifest => manifest
[getManifestoInstance, getManifestLocale],
(manifest, locale) => manifest
&& asArray(manifest.getRequiredStatement())
.filter(l => l && l.getValues().some(v => v))
.map(labelValuePair => ({
label: (labelValuePair.label && labelValuePair.label.getValue()) || null,
values: labelValuePair.getValues(),
label: (labelValuePair.label && labelValuePair.label.getValue(locale)) || null,
values: labelValuePair.getValues(locale),
})),
);

Expand All @@ -285,7 +276,7 @@ export const getRights = createSelector(
],
(rights, license, locale) => {
const data = rights || license;
return asArray(PropertyValue.parse(data, locale).getValues());
return asArray(PropertyValue.parse(data).getValues(locale));
},
);

Expand Down Expand Up @@ -319,9 +310,9 @@ export function getManifestThumbnail(state, props) {
* @returns {string}
*/
export const getManifestTitle = createSelector(
[getManifestoInstance],
manifest => manifest
&& manifest.getLabel().getValue(),
[getManifestoInstance, getManifestLocale],
(manifest, locale) => manifest
&& manifest.getLabel().getValue(locale),
);

/**
Expand Down Expand Up @@ -352,7 +343,7 @@ export const getManifestSummary = createSelector(
getManifestLocale,
],
(summary, locale) => summary
&& PropertyValue.parse(summary, locale).getValue(locale),
&& PropertyValue.parse(summary).getValue(locale),
);

/**
Expand All @@ -377,11 +368,11 @@ export const getManifestUrl = createSelector(
* @param iiifResource
* @returns {Array[Object]}
*/
export function getDestructuredMetadata(iiifResource) {
export function getDestructuredMetadata(iiifResource, locale = undefined) {
return (iiifResource
&& iiifResource.getMetadata().map(labelValuePair => ({
label: labelValuePair.getLabel(),
values: labelValuePair.getValues(),
label: labelValuePair.getLabel(locale),
values: labelValuePair.getValues(locale),
}))
);
}
Expand All @@ -395,8 +386,8 @@ export function getDestructuredMetadata(iiifResource) {
* @returns {Array[Object]}
*/
export const getManifestMetadata = createSelector(
[getManifestoInstance],
manifest => manifest && getDestructuredMetadata(manifest),
[getManifestoInstance, getManifestLocale],
(manifest, locale) => manifest && getDestructuredMetadata(manifest, locale),
);

/** */
Expand Down
2 changes: 1 addition & 1 deletion src/state/selectors/searches.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export const getResourceAnnotationLabel = createSelector(
!(resourceAnnotation && resourceAnnotation.resource && resourceAnnotation.resource.label)
) return [];

return PropertyValue.parse(resourceAnnotation.resource.label, locale).getValues();
return PropertyValue.parse(resourceAnnotation.resource.label).getValues(locale);
},
);

Expand Down

0 comments on commit d947941

Please sign in to comment.