Skip to content

Commit d947941

Browse files
committed
Push property localization down to getValues instead of part of parsing the property values.
1 parent 6af39a4 commit d947941

File tree

4 files changed

+37
-51
lines changed

4 files changed

+37
-51
lines changed

__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
/** */
@@ -114,7 +105,7 @@ export const getManifestProviderName = createSelector(
114105
],
115106
(provider, locale) => provider
116107
&& provider[0].label
117-
&& PropertyValue.parse(provider[0].label, locale).getValue(),
108+
&& PropertyValue.parse(provider[0].label).getValue(locale),
118109
);
119110

120111
/**
@@ -159,8 +150,8 @@ export const getManifestHomepage = createSelector(
159150
(homepages, locale) => homepages
160151
&& asArray(homepages).map(homepage => (
161152
{
162-
label: PropertyValue.parse(homepage.label, locale)
163-
.getValue(),
153+
label: PropertyValue.parse(homepage.label)
154+
.getValue(locale),
164155
value: homepage.id || homepage['@id'],
165156
}
166157
)),
@@ -175,11 +166,11 @@ export const getManifestHomepage = createSelector(
175166
* @returns {string|null}
176167
*/
177168
export const getManifestRenderings = createSelector(
178-
[getManifestoInstance],
179-
manifest => manifest
169+
[getManifestoInstance, getManifestLocale],
170+
(manifest, locale) => manifest
180171
&& manifest.getRenderings().map(rendering => (
181172
{
182-
label: rendering.getLabel().getValue(),
173+
label: rendering.getLabel().getValue(locale),
183174
value: rendering.id,
184175
}
185176
)),
@@ -202,8 +193,8 @@ export const getManifestSeeAlso = createSelector(
202193
&& asArray(seeAlso).map(related => (
203194
{
204195
format: related.format,
205-
label: PropertyValue.parse(related.label, locale)
206-
.getValue(),
196+
label: PropertyValue.parse(related.label)
197+
.getValue(locale),
207198
value: related.id || related['@id'],
208199
}
209200
)),
@@ -243,8 +234,8 @@ export const getManifestRelated = createSelector(
243234
}
244235
: {
245236
format: related.format,
246-
label: PropertyValue.parse(related.label, locale)
247-
.getValue(),
237+
label: PropertyValue.parse(related.label)
238+
.getValue(locale),
248239
value: related.id || related['@id'],
249240
}
250241
)),
@@ -259,13 +250,13 @@ export const getManifestRelated = createSelector(
259250
* @returns {string|null}
260251
*/
261252
export const getRequiredStatement = createSelector(
262-
[getManifestoInstance],
263-
manifest => manifest
253+
[getManifestoInstance, getManifestLocale],
254+
(manifest, locale) => manifest
264255
&& asArray(manifest.getRequiredStatement())
265256
.filter(l => l && l.getValues().some(v => v))
266257
.map(labelValuePair => ({
267-
label: (labelValuePair.label && labelValuePair.label.getValue()) || null,
268-
values: labelValuePair.getValues(),
258+
label: (labelValuePair.label && labelValuePair.label.getValue(locale)) || null,
259+
values: labelValuePair.getValues(locale),
269260
})),
270261
);
271262

@@ -285,7 +276,7 @@ export const getRights = createSelector(
285276
],
286277
(rights, license, locale) => {
287278
const data = rights || license;
288-
return asArray(PropertyValue.parse(data, locale).getValues());
279+
return asArray(PropertyValue.parse(data).getValues(locale));
289280
},
290281
);
291282

@@ -319,9 +310,9 @@ export function getManifestThumbnail(state, props) {
319310
* @returns {string}
320311
*/
321312
export const getManifestTitle = createSelector(
322-
[getManifestoInstance],
323-
manifest => manifest
324-
&& manifest.getLabel().getValue(),
313+
[getManifestoInstance, getManifestLocale],
314+
(manifest, locale) => manifest
315+
&& manifest.getLabel().getValue(locale),
325316
);
326317

327318
/**
@@ -352,7 +343,7 @@ export const getManifestSummary = createSelector(
352343
getManifestLocale,
353344
],
354345
(summary, locale) => summary
355-
&& PropertyValue.parse(summary, locale).getValue(locale),
346+
&& PropertyValue.parse(summary).getValue(locale),
356347
);
357348

358349
/**
@@ -377,11 +368,11 @@ export const getManifestUrl = createSelector(
377368
* @param iiifResource
378369
* @returns {Array[Object]}
379370
*/
380-
export function getDestructuredMetadata(iiifResource) {
371+
export function getDestructuredMetadata(iiifResource, locale = undefined) {
381372
return (iiifResource
382373
&& iiifResource.getMetadata().map(labelValuePair => ({
383-
label: labelValuePair.getLabel(),
384-
values: labelValuePair.getValues(),
374+
label: labelValuePair.getLabel(locale),
375+
values: labelValuePair.getValues(locale),
385376
}))
386377
);
387378
}
@@ -395,8 +386,8 @@ export function getDestructuredMetadata(iiifResource) {
395386
* @returns {Array[Object]}
396387
*/
397388
export const getManifestMetadata = createSelector(
398-
[getManifestoInstance],
399-
manifest => manifest && getDestructuredMetadata(manifest),
389+
[getManifestoInstance, getManifestLocale],
390+
(manifest, locale) => manifest && getDestructuredMetadata(manifest, locale),
400391
);
401392

402393
/** */

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)