Skip to content

Commit c92dd06

Browse files
committed
Push property localization down to getValues instead of part of parsing the property values.
1 parent fd2eab6 commit c92dd06

4 files changed

Lines changed: 37 additions & 51 deletions

File tree

__tests__/src/selectors/manifests.test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ describe('getManifestoInstance', () => {
5959
const received = getManifestoInstance(state, { manifestId: 'x' });
6060
expect(received.id).toEqual('http://iiif.io/api/presentation/2.1/example/fixtures/19/manifest.json');
6161
});
62-
it('is cached based off of input props', () => {
63-
const state = { manifests: { x: { json: manifestFixture019 } } };
64-
const received = getManifestoInstance(state, { manifestId: 'x' });
65-
expect(getManifestoInstance(state, { manifestId: 'x' })).toBe(received);
66-
expect(getManifestoInstance(state, { manifestId: 'x', windowId: 'y' })).not.toBe(received);
67-
});
6862
});
6963

7064
describe('getManifestLogo()', () => {

src/state/selectors/canvases.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { miradorSlice } from './utils';
66
import { getWindow } from './getters';
77
import { getSequence } from './sequences';
88
import { getWindowViewType } from './windows';
9+
import { getManifestLocale } from './manifests';
910

1011
/**
1112
* Returns the info response.
@@ -183,10 +184,10 @@ export const getPreviousCanvasGrouping = createSelector(
183184
* @returns {string|number}
184185
*/
185186
export const getCanvasLabel = createSelector(
186-
[getCanvas],
187-
canvas => (canvas && (
187+
[getCanvas, getManifestLocale],
188+
(canvas, locale) => (canvas && (
188189
canvas.getLabel().length > 0
189-
? canvas.getLabel().getValue()
190+
? canvas.getLabel().getValue(locale)
190191
: String(canvas.index + 1)
191192
)),
192193
);

src/state/selectors/manifests.js

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { createSelector } from 'reselect';
2-
import { createCachedSelector } from 're-reselect';
32
import { PropertyValue, Utils, Resource } from 'manifesto.js';
43
import getThumbnail from '../../lib/ThumbnailFactory';
54
import asArray from '../../lib/asArray';
@@ -24,9 +23,10 @@ export const getLocale = createSelector(
2423
[
2524
getCompanionWindowLocale,
2625
getConfig,
26+
(state, { locale }) => locale,
2727
],
28-
(companionWindowLocale, config = {}) => (
29-
companionWindowLocale || config.language
28+
(companionWindowLocale, config = {}, locale) => (
29+
locale || companionWindowLocale || config.language || config.fallbackLanguages
3030
),
3131
);
3232

@@ -57,17 +57,9 @@ export const getManifestError = createSelector(
5757
);
5858

5959
/** Instantiate a manifesto instance */
60-
const getContextualManifestoInstance = createCachedSelector(
60+
const getContextualManifestoInstance = createSelector(
6161
getManifest,
62-
getLocale,
63-
(manifest, locale) => manifest
64-
&& createManifestoInstance(manifest.json, locale),
65-
)(
66-
(state, { companionWindowId, manifestId, windowId }) => [
67-
manifestId,
68-
windowId,
69-
getLocale(state, { companionWindowId }),
70-
].join(' - '), // Cache key consisting of manifestId, windowId, and locale
62+
manifest => manifest && createManifestoInstance(manifest.json),
7163
);
7264

7365
/**
@@ -80,15 +72,14 @@ const getContextualManifestoInstance = createCachedSelector(
8072
export const getManifestoInstance = createSelector(
8173
getContextualManifestoInstance,
8274
(state, { json }) => json,
83-
getLocale,
84-
(manifesto, manifestJson, locale) => (
75+
(manifesto, manifestJson) => (
8576
manifestJson && createManifestoInstance(manifestJson, locale)
8677
) || manifesto,
8778
);
8879

8980
export const getManifestLocale = createSelector(
90-
[getManifestoInstance],
91-
manifest => manifest && manifest.options && manifest.options.locale && manifest.options.locale.replace(/-.*$/, ''),
81+
[getManifestoInstance, getLocale],
82+
(manifest, locale) => locale ?? (manifest && manifest.options && manifest.options.locale && manifest.options.locale.replace(/-.*$/, '')),
9283
);
9384

9485
/** */
@@ -128,7 +119,7 @@ export const getManifestProviderName = createSelector(
128119
],
129120
(provider, locale) => provider
130121
&& provider[0].label
131-
&& PropertyValue.parse(provider[0].label, locale).getValue(),
122+
&& PropertyValue.parse(provider[0].label).getValue(locale),
132123
);
133124

134125
/**
@@ -173,8 +164,8 @@ export const getManifestHomepage = createSelector(
173164
(homepages, locale) => homepages
174165
&& asArray(homepages).map(homepage => (
175166
{
176-
label: PropertyValue.parse(homepage.label, locale)
177-
.getValue(),
167+
label: PropertyValue.parse(homepage.label)
168+
.getValue(locale),
178169
value: homepage.id || homepage['@id'],
179170
}
180171
)),
@@ -189,11 +180,11 @@ export const getManifestHomepage = createSelector(
189180
* @returns {string|null}
190181
*/
191182
export const getManifestRenderings = createSelector(
192-
[getManifestoInstance],
193-
manifest => manifest
183+
[getManifestoInstance, getManifestLocale],
184+
(manifest, locale) => manifest
194185
&& manifest.getRenderings().map(rendering => (
195186
{
196-
label: rendering.getLabel().getValue(),
187+
label: rendering.getLabel().getValue(locale),
197188
value: rendering.id,
198189
}
199190
)),
@@ -216,8 +207,8 @@ export const getManifestSeeAlso = createSelector(
216207
&& asArray(seeAlso).map(related => (
217208
{
218209
format: related.format,
219-
label: PropertyValue.parse(related.label, locale)
220-
.getValue(),
210+
label: PropertyValue.parse(related.label)
211+
.getValue(locale),
221212
value: related.id || related['@id'],
222213
}
223214
)),
@@ -257,8 +248,8 @@ export const getManifestRelated = createSelector(
257248
}
258249
: {
259250
format: related.format,
260-
label: PropertyValue.parse(related.label, locale)
261-
.getValue(),
251+
label: PropertyValue.parse(related.label)
252+
.getValue(locale),
262253
value: related.id || related['@id'],
263254
}
264255
)),
@@ -273,13 +264,13 @@ export const getManifestRelated = createSelector(
273264
* @returns {string|null}
274265
*/
275266
export const getRequiredStatement = createSelector(
276-
[getManifestoInstance],
277-
manifest => manifest
267+
[getManifestoInstance, getManifestLocale],
268+
(manifest, locale) => manifest
278269
&& asArray(manifest.getRequiredStatement())
279270
.filter(l => l && l.getValues().some(v => v))
280271
.map(labelValuePair => ({
281-
label: (labelValuePair.label && labelValuePair.label.getValue()) || null,
282-
values: labelValuePair.getValues(),
272+
label: (labelValuePair.label && labelValuePair.label.getValue(locale)) || null,
273+
values: labelValuePair.getValues(locale),
283274
})),
284275
);
285276

@@ -299,7 +290,7 @@ export const getRights = createSelector(
299290
],
300291
(rights, license, locale) => {
301292
const data = rights || license;
302-
return asArray(PropertyValue.parse(data, locale).getValues());
293+
return asArray(PropertyValue.parse(data).getValues(locale));
303294
},
304295
);
305296

@@ -333,9 +324,9 @@ export function getManifestThumbnail(state, props) {
333324
* @returns {string}
334325
*/
335326
export const getManifestTitle = createSelector(
336-
[getManifestoInstance],
337-
manifest => manifest
338-
&& manifest.getLabel().getValue(),
327+
[getManifestoInstance, getManifestLocale],
328+
(manifest, locale) => manifest
329+
&& manifest.getLabel().getValue(locale),
339330
);
340331

341332
/**
@@ -366,7 +357,7 @@ export const getManifestSummary = createSelector(
366357
getManifestLocale,
367358
],
368359
(summary, locale) => summary
369-
&& PropertyValue.parse(summary, locale).getValue(locale),
360+
&& PropertyValue.parse(summary).getValue(locale),
370361
);
371362

372363
/**
@@ -391,11 +382,11 @@ export const getManifestUrl = createSelector(
391382
* @param iiifResource
392383
* @returns {Array[Object]}
393384
*/
394-
export function getDestructuredMetadata(iiifResource) {
385+
export function getDestructuredMetadata(iiifResource, locale = undefined) {
395386
return (iiifResource
396387
&& iiifResource.getMetadata().map(labelValuePair => ({
397-
label: labelValuePair.getLabel(),
398-
values: labelValuePair.getValues(),
388+
label: labelValuePair.getLabel(locale),
389+
values: labelValuePair.getValues(locale),
399390
}))
400391
);
401392
}
@@ -409,8 +400,8 @@ export function getDestructuredMetadata(iiifResource) {
409400
* @returns {Array[Object]}
410401
*/
411402
export const getManifestMetadata = createSelector(
412-
[getManifestoInstance],
413-
manifest => manifest && getDestructuredMetadata(manifest),
403+
[getManifestoInstance, getManifestLocale],
404+
(manifest, locale) => manifest && getDestructuredMetadata(manifest, locale),
414405
);
415406

416407
/** */

src/state/selectors/searches.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ export const getResourceAnnotationLabel = createSelector(
294294
!(resourceAnnotation && resourceAnnotation.resource && resourceAnnotation.resource.label)
295295
) return [];
296296

297-
return PropertyValue.parse(resourceAnnotation.resource.label, locale).getValues();
297+
return PropertyValue.parse(resourceAnnotation.resource.label).getValues(locale);
298298
},
299299
);
300300

0 commit comments

Comments
 (0)