Skip to content
Snippets Groups Projects
Unverified Commit 12825008 authored by Jack Reed's avatar Jack Reed Committed by GitHub
Browse files

Merge pull request #2268 from ProjectMirador/move-selectors

Move canvas and window selectors into their own files
parents b17c8afd 9ca611b4
No related branches found
No related tags found
No related merge requests found
import manifestFixture001 from '../../fixtures/version-2/001.json';
import manifestFixture019 from '../../fixtures/version-2/019.json'; import manifestFixture019 from '../../fixtures/version-2/019.json';
import { import {
getSelectedCanvas, getSelectedCanvas,
getSelectedCanvases, getSelectedCanvases,
getCanvasLabel,
} from '../../../src/state/selectors/canvases'; } from '../../../src/state/selectors/canvases';
describe('getSelectedCanvas', () => { describe('getSelectedCanvas', () => {
...@@ -100,3 +102,38 @@ describe('getSelectedCanvases', () => { ...@@ -100,3 +102,38 @@ describe('getSelectedCanvases', () => {
expect(selectedCanvas).toBeUndefined(); 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');
});
});
import manifestFixture001 from '../../fixtures/version-2/001.json';
import { import {
getCanvasLabel,
getCompanionWindowForPosition,
getAnnotationResourcesByMotivation, getAnnotationResourcesByMotivation,
getIdAndContentOfResources, getIdAndContentOfResources,
getLanguagesFromConfigWithCurrent, getLanguagesFromConfigWithCurrent,
getThumbnailNavigationPosition,
getSelectedAnnotationIds, getSelectedAnnotationIds,
getSelectedTargetAnnotations, getSelectedTargetAnnotations,
getSelectedTargetsAnnotations, getSelectedTargetsAnnotations,
getSelectedTargetAnnotationResources, getSelectedTargetAnnotationResources,
getWindowViewType,
getCompanionWindowsOfWindow,
} from '../../../src/state/selectors'; } from '../../../src/state/selectors';
import Annotation from '../../../src/lib/Annotation'; import Annotation from '../../../src/lib/Annotation';
import AnnotationResource from '../../../src/lib/AnnotationResource'; 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', () => { describe('getSelectedTargetAnnotations', () => {
it('returns annotations for the given canvasId that have resources', () => { it('returns annotations for the given canvasId that have resources', () => {
const state = { const state = {
...@@ -207,34 +114,6 @@ describe('getIdAndContentOfResources', () => { ...@@ -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', () => { describe('getLanguagesFromConfigWithCurrent', () => {
it('returns an array of objects with locale, label, and current properties', () => { it('returns an array of objects with locale, label, and current properties', () => {
const state = { const state = {
...@@ -250,35 +129,6 @@ describe('getLanguagesFromConfigWithCurrent', () => { ...@@ -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', () => { it('getSelectedAnnotationIds returns an array of selected annotation IDs from state', () => {
const state = { const state = {
windows: { windows: {
......
...@@ -3,6 +3,10 @@ import manifestFixture002 from '../../fixtures/version-2/002.json'; ...@@ -3,6 +3,10 @@ import manifestFixture002 from '../../fixtures/version-2/002.json';
import manifestFixture019 from '../../fixtures/version-2/019.json'; import manifestFixture019 from '../../fixtures/version-2/019.json';
import { import {
getWindowTitles, getWindowTitles,
getThumbnailNavigationPosition,
getWindowViewType,
getCompanionWindowForPosition,
getCompanionWindowsOfWindow,
} from '../../../src/state/selectors/windows'; } from '../../../src/state/selectors/windows';
...@@ -28,3 +32,112 @@ describe('getWindowTitles', () => { ...@@ -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' },
]);
});
});
...@@ -62,3 +62,27 @@ export const getSelectedCanvases = createSelector( ...@@ -62,3 +62,27 @@ export const getSelectedCanvases = createSelector(
view, view,
).getCanvases(canvasIndex), ).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'),
);
import { createSelector } from 'reselect';
import filter from 'lodash/filter'; import filter from 'lodash/filter';
import flatten from 'lodash/flatten'; import flatten from 'lodash/flatten';
import Annotation from '../../lib/Annotation'; import Annotation from '../../lib/Annotation';
import { getCanvas } from './canvases';
export * from './canvases'; export * from './canvases';
export * from './manifests'; export * from './manifests';
...@@ -66,94 +64,6 @@ export function getIdAndContentOfResources(resources) { ...@@ -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 * Return languages from config (in state) and indicate which is currently set
* @param {object} state * @param {object} state
......
import { createSelector } from 'reselect';
import { getManifestTitle } from './manifests'; import { getManifestTitle } from './manifests';
/** /**
...@@ -14,3 +15,67 @@ export function getWindowTitles(state) { ...@@ -14,3 +15,67 @@ export function getWindowTitles(state) {
return result; 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),
);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment