diff --git a/__tests__/src/selectors/canvases.test.js b/__tests__/src/selectors/canvases.test.js index ba35f9a865c950e37e6da93ce850ef1957bf45f1..a0414225b375e9440494c042068e809bfd990c31 100644 --- a/__tests__/src/selectors/canvases.test.js +++ b/__tests__/src/selectors/canvases.test.js @@ -1,7 +1,9 @@ +import manifestFixture001 from '../../fixtures/version-2/001.json'; import manifestFixture019 from '../../fixtures/version-2/019.json'; import { getSelectedCanvas, getSelectedCanvases, + getCanvasLabel, } from '../../../src/state/selectors/canvases'; describe('getSelectedCanvas', () => { @@ -100,3 +102,38 @@ describe('getSelectedCanvases', () => { expect(selectedCanvas).toBeUndefined(); }); }); + +describe('getCanvasLabel', () => { + it('should return label of the canvas', () => { + const state = { manifests: { a: { json: manifestFixture001 } } }; + const received = getCanvasLabel(state, { manifestId: 'a', canvasIndex: 0 }); + expect(received).toBe('Whole Page'); + }); + + it('should return undefined if the canvas is undefined', () => { + const state = { manifests: { } }; + expect(getCanvasLabel(state, { manifestId: 'b', canvasIndex: 0 })).toBeUndefined(); + }); + + it('should return the canvas index as (+1) as string if no label given', () => { + const manifest = { + '@context': 'http://iiif.io/api/presentation/2/context.json', + '@id': + 'http://iiif.io/api/presentation/2.1/example/fixtures/19/manifest.json', + '@type': 'sc:Manifest', + sequences: [ + { + canvases: [ + { + '@id': 'some-canvas-without-a-label', + }, + ], + }, + ], + }; + + const state = { manifests: { a: { json: manifest } } }; + const received = getCanvasLabel(state, { manifestId: 'a', canvasIndex: 0 }); + expect(received).toBe('1'); + }); +}); diff --git a/__tests__/src/selectors/index.test.js b/__tests__/src/selectors/index.test.js index c2391dfede742134be0bb4c5b15ff55c9540a2b9..67c95aa785ecdf32dd2d6090a9a674c38d2ada78 100644 --- a/__tests__/src/selectors/index.test.js +++ b/__tests__/src/selectors/index.test.js @@ -1,108 +1,15 @@ -import manifestFixture001 from '../../fixtures/version-2/001.json'; import { - getCanvasLabel, - getCompanionWindowForPosition, getAnnotationResourcesByMotivation, getIdAndContentOfResources, getLanguagesFromConfigWithCurrent, - getThumbnailNavigationPosition, getSelectedAnnotationIds, getSelectedTargetAnnotations, getSelectedTargetsAnnotations, getSelectedTargetAnnotationResources, - getWindowViewType, - getCompanionWindowsOfWindow, } from '../../../src/state/selectors'; import Annotation from '../../../src/lib/Annotation'; import AnnotationResource from '../../../src/lib/AnnotationResource'; -describe('getThumbnailNavigationPosition', () => { - const state = { - windows: { - a: { id: 'a', thumbnailNavigationId: 'cw_a' }, - b: { id: 'b', thumbnailNavigationId: 'cw_b' }, - }, - companionWindows: { - cw_a: { position: 'bottom' }, - }, - }; - - it('should return thumbnail navigation position if window exists', () => { - const received = getThumbnailNavigationPosition(state, { windowId: 'a' }); - expect(received).toBe('bottom'); - }); - - it('should return undefined if position does not exist in window', () => { - const received = getThumbnailNavigationPosition(state, { windowId: 'b' }); - expect(received).toBeUndefined(); - }); - - it('should return undefined if window does not exists', () => { - const received = getThumbnailNavigationPosition(state, { windowId: 'c' }); - expect(received).toBeUndefined(); - }); -}); - -describe('getWindowViewType', () => { - const state = { - windows: { - a: { id: 'a', view: 'single' }, - b: { id: 'b' }, - }, - }; - - it('should return view type if window exists', () => { - const received = getWindowViewType(state, { windowId: 'a' }); - expect(received).toBe('single'); - }); - - it('should return undefined if view type does not exist in window', () => { - const received = getWindowViewType(state, { windowId: 'b' }); - expect(received).toBeUndefined(); - }); - - it('should return undefined if window does not exists', () => { - const received = getWindowViewType(state, { windowId: 'c' }); - expect(received).toBeUndefined(); - }); -}); - -describe('getCanvasLabel', () => { - it('should return label of the canvas', () => { - const state = { manifests: { a: { json: manifestFixture001 } } }; - const received = getCanvasLabel(state, { manifestId: 'a', canvasIndex: 0 }); - expect(received).toBe('Whole Page'); - }); - - it('should return undefined if the canvas is undefined', () => { - const state = { manifests: { } }; - expect(getCanvasLabel(state, { manifestId: 'b', canvasIndex: 0 })).toBeUndefined(); - }); - - it('should return the canvas index as (+1) as string if no label given', () => { - const manifest = { - '@context': 'http://iiif.io/api/presentation/2/context.json', - '@id': - 'http://iiif.io/api/presentation/2.1/example/fixtures/19/manifest.json', - '@type': 'sc:Manifest', - sequences: [ - { - canvases: [ - { - '@id': 'some-canvas-without-a-label', - }, - ], - }, - ], - }; - - const state = { manifests: { a: { json: manifest } } }; - const received = getCanvasLabel(state, { manifestId: 'a', canvasIndex: 0 }); - expect(received).toBe('1'); - }); -}); - - describe('getSelectedTargetAnnotations', () => { it('returns annotations for the given canvasId that have resources', () => { const state = { @@ -207,34 +114,6 @@ describe('getIdAndContentOfResources', () => { }); }); -describe('getCompanionWindowForPosition', () => { - const state = { - windows: { a: { companionWindowIds: ['abc'] } }, - companionWindows: { - abc: { id: 'abc', position: 'right' }, - xyz: { id: 'xyz', position: 'bottom' }, - }, - }; - - it('the companion window type based on the given position', () => { - const received = getCompanionWindowForPosition(state, { windowId: 'a', position: 'right' }); - - expect(received.id).toEqual('abc'); - }); - - it('returns undefined if the given window does not exist', () => { - const received = getCompanionWindowForPosition(state, { windowId: 'c', position: 'right' }); - - expect(received).toBeUndefined(); - }); - - it('returns undefined if a companion window at the given position does not exist', () => { - const received = getCompanionWindowForPosition(state, { windowId: 'a', position: 'bottom' }); - - expect(received).toBeUndefined(); - }); -}); - describe('getLanguagesFromConfigWithCurrent', () => { it('returns an array of objects with locale, label, and current properties', () => { const state = { @@ -250,35 +129,6 @@ describe('getLanguagesFromConfigWithCurrent', () => { }); }); -describe('getCompanionWindowsOfWindow', () => { - const state = { - windows: { - abc123: { - companionWindowIds: ['foo', 'bar'], - }, - }, - companionWindows: { - foo: { - id: 'foo', - content: 'info', - }, - bar: { - id: 'bar', - content: 'canvas', - }, - }, - }; - - it('should return companion windows for a given window id', () => { - const received = getCompanionWindowsOfWindow(state, { windowId: 'abc123' }); - - expect(received).toEqual([ - { id: 'foo', content: 'info' }, - { id: 'bar', content: 'canvas' }, - ]); - }); -}); - it('getSelectedAnnotationIds returns an array of selected annotation IDs from state', () => { const state = { windows: { diff --git a/__tests__/src/selectors/windows.test.js b/__tests__/src/selectors/windows.test.js index 996599b87edb5ddffc1cf10f2c96cbb12c6144aa..caa2fe6f73bd325783d6ad34b9d61634c776d4b5 100644 --- a/__tests__/src/selectors/windows.test.js +++ b/__tests__/src/selectors/windows.test.js @@ -3,6 +3,10 @@ import manifestFixture002 from '../../fixtures/version-2/002.json'; import manifestFixture019 from '../../fixtures/version-2/019.json'; import { getWindowTitles, + getThumbnailNavigationPosition, + getWindowViewType, + getCompanionWindowForPosition, + getCompanionWindowsOfWindow, } from '../../../src/state/selectors/windows'; @@ -28,3 +32,112 @@ describe('getWindowTitles', () => { }); }); }); + + +describe('getThumbnailNavigationPosition', () => { + const state = { + windows: { + a: { id: 'a', thumbnailNavigationId: 'cw_a' }, + b: { id: 'b', thumbnailNavigationId: 'cw_b' }, + }, + companionWindows: { + cw_a: { position: 'bottom' }, + }, + }; + + it('should return thumbnail navigation position if window exists', () => { + const received = getThumbnailNavigationPosition(state, { windowId: 'a' }); + expect(received).toBe('bottom'); + }); + + it('should return undefined if position does not exist in window', () => { + const received = getThumbnailNavigationPosition(state, { windowId: 'b' }); + expect(received).toBeUndefined(); + }); + + it('should return undefined if window does not exists', () => { + const received = getThumbnailNavigationPosition(state, { windowId: 'c' }); + expect(received).toBeUndefined(); + }); +}); + +describe('getWindowViewType', () => { + const state = { + windows: { + a: { id: 'a', view: 'single' }, + b: { id: 'b' }, + }, + }; + + it('should return view type if window exists', () => { + const received = getWindowViewType(state, { windowId: 'a' }); + expect(received).toBe('single'); + }); + + it('should return undefined if view type does not exist in window', () => { + const received = getWindowViewType(state, { windowId: 'b' }); + expect(received).toBeUndefined(); + }); + + it('should return undefined if window does not exists', () => { + const received = getWindowViewType(state, { windowId: 'c' }); + expect(received).toBeUndefined(); + }); +}); + +describe('getCompanionWindowForPosition', () => { + const state = { + windows: { a: { companionWindowIds: ['abc'] } }, + companionWindows: { + abc: { id: 'abc', position: 'right' }, + xyz: { id: 'xyz', position: 'bottom' }, + }, + }; + + it('the companion window type based on the given position', () => { + const received = getCompanionWindowForPosition(state, { windowId: 'a', position: 'right' }); + + expect(received.id).toEqual('abc'); + }); + + it('returns undefined if the given window does not exist', () => { + const received = getCompanionWindowForPosition(state, { windowId: 'c', position: 'right' }); + + expect(received).toBeUndefined(); + }); + + it('returns undefined if a companion window at the given position does not exist', () => { + const received = getCompanionWindowForPosition(state, { windowId: 'a', position: 'bottom' }); + + expect(received).toBeUndefined(); + }); +}); + +describe('getCompanionWindowsOfWindow', () => { + const state = { + windows: { + abc123: { + companionWindowIds: ['foo', 'bar'], + }, + }, + companionWindows: { + foo: { + id: 'foo', + content: 'info', + }, + bar: { + id: 'bar', + content: 'canvas', + }, + }, + }; + + it('should return companion windows for a given window id', () => { + const received = getCompanionWindowsOfWindow(state, { windowId: 'abc123' }); + + expect(received).toEqual([ + { id: 'foo', content: 'info' }, + { id: 'bar', content: 'canvas' }, + ]); + }); +}); diff --git a/src/state/selectors/canvases.js b/src/state/selectors/canvases.js index 8051a0d019dd4bcf1270989d0e457888b978834d..d30764305453cd84eeb8eec375943a3bab9a30da 100644 --- a/src/state/selectors/canvases.js +++ b/src/state/selectors/canvases.js @@ -62,3 +62,27 @@ export const getSelectedCanvases = createSelector( view, ).getCanvases(canvasIndex), ); + +/** +* Return canvas label, or alternatively return the given index + 1 to be displayed +* @param {object} canvas +* @return {String|Integer} +*/ +export const getCanvasLabel = createSelector( + [getCanvas], + canvas => (canvas && ( + canvas.getLabel().length > 0 + ? canvas.getLabel().map(label => label.value)[0] + : String(canvas.index + 1) + )), +); + +/** +* Return canvas description +* @param {object} canvas +* @param {String} +*/ +export const getCanvasDescription = createSelector( + [getCanvas], + canvas => canvas && canvas.getProperty('description'), +); diff --git a/src/state/selectors/index.js b/src/state/selectors/index.js index fb5f2f433329dc23d8e9840b12e19654a5acb8c9..41a9aef4b377f0d63c9cd2461fa170a58e59a02d 100644 --- a/src/state/selectors/index.js +++ b/src/state/selectors/index.js @@ -1,8 +1,6 @@ -import { createSelector } from 'reselect'; import filter from 'lodash/filter'; import flatten from 'lodash/flatten'; import Annotation from '../../lib/Annotation'; -import { getCanvas } from './canvases'; export * from './canvases'; export * from './manifests'; @@ -66,94 +64,6 @@ export function getIdAndContentOfResources(resources) { })); } -/** */ -function getWindow(state, { windowId }) { - return state.windows && state.windows[windowId]; -} - -/** Return position of thumbnail navigation in a certain window. -* @param {object} state -* @param {String} windowId -* @param {String} -*/ -export const getThumbnailNavigationPosition = createSelector( - [ - getWindow, - state => state.companionWindows, - ], - (window, companionWindows) => window - && companionWindows[window.thumbnailNavigationId] - && companionWindows[window.thumbnailNavigationId].position, -); - -/** Return type of view in a certain window. -* @param {object} state -* @param {object} props -* @param {string} props.manifestId -* @param {string} props.windowId -* @param {String} -*/ -export const getWindowViewType = createSelector( - [getWindow], - window => window && window.view, -); - -/** -* Return canvas label, or alternatively return the given index + 1 to be displayed -* @param {object} canvas -* @return {String|Integer} -*/ -export const getCanvasLabel = createSelector( - [getCanvas], - canvas => (canvas && ( - canvas.getLabel().length > 0 - ? canvas.getLabel().map(label => label.value)[0] - : String(canvas.index + 1) - )), -); - -/** -* Return canvas description -* @param {object} canvas -* @param {String} -*/ -export const getCanvasDescription = createSelector( - [getCanvas], - canvas => canvas && canvas.getProperty('description'), -); - -/** -* Return compantion window ids from a window -* @param {String} windowId -* @return {Array} -*/ -export const getCompanionWindowIds = createSelector( - [getWindow], - window => (window && window.companionWindowIds) || [], -); - -/** - * Return companion windows of a window - * @param {String} windowId - * @return {Array} - */ -export const getCompanionWindowsOfWindow = createSelector( - [getCompanionWindowIds, state => state.companionWindows], - (companionWindowIds, companionWindows) => companionWindowIds.map(id => companionWindows[id]), -); - -/** -* Return the companion window string from state in a given windowId and position -* @param {object} state -* @param {String} windowId -* @param {String} position -* @return {String} -*/ -export const getCompanionWindowForPosition = createSelector( - [getCompanionWindowsOfWindow, (state, { position }) => position], - (companionWindows, position) => companionWindows.find(cw => cw.position === position), -); - /** * Return languages from config (in state) and indicate which is currently set * @param {object} state diff --git a/src/state/selectors/windows.js b/src/state/selectors/windows.js index 3832c0c3fa549c87ee80d074929435d56e964a2f..31f5a323e2161f1ab414501af4efb5a14679b77b 100644 --- a/src/state/selectors/windows.js +++ b/src/state/selectors/windows.js @@ -1,3 +1,4 @@ +import { createSelector } from 'reselect'; import { getManifestTitle } from './manifests'; /** @@ -14,3 +15,67 @@ export function getWindowTitles(state) { return result; } + +/** */ +function getWindow(state, { windowId }) { + return state.windows && state.windows[windowId]; +} + +/** Return position of thumbnail navigation in a certain window. +* @param {object} state +* @param {String} windowId +* @param {String} +*/ +export const getThumbnailNavigationPosition = createSelector( + [ + getWindow, + state => state.companionWindows, + ], + (window, companionWindows) => window + && companionWindows[window.thumbnailNavigationId] + && companionWindows[window.thumbnailNavigationId].position, +); + +/** Return type of view in a certain window. +* @param {object} state +* @param {object} props +* @param {string} props.manifestId +* @param {string} props.windowId +* @param {String} +*/ +export const getWindowViewType = createSelector( + [getWindow], + window => window && window.view, +); + +/** +* Return compantion window ids from a window +* @param {String} windowId +* @return {Array} +*/ +export const getCompanionWindowIds = createSelector( + [getWindow], + window => (window && window.companionWindowIds) || [], +); + +/** + * Return companion windows of a window + * @param {String} windowId + * @return {Array} + */ +export const getCompanionWindowsOfWindow = createSelector( + [getCompanionWindowIds, state => state.companionWindows], + (companionWindowIds, companionWindows) => companionWindowIds.map(id => companionWindows[id]), +); + +/** +* Return the companion window string from state in a given windowId and position +* @param {object} state +* @param {String} windowId +* @param {String} position +* @return {String} +*/ +export const getCompanionWindowForPosition = createSelector( + [getCompanionWindowsOfWindow, (state, { position }) => position], + (companionWindows, position) => companionWindows.find(cw => cw.position === position), +);