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

Merge pull request #2266 from ProjectMirador/reselect-windows

Convert window selectors to use reselect
parents 1eefa8d3 f6ed6947
Branches
Tags
No related merge requests found
Showing
with 182 additions and 156 deletions
...@@ -7,7 +7,6 @@ import manifesto from 'manifesto.js'; ...@@ -7,7 +7,6 @@ import manifesto from 'manifesto.js';
import { WindowSideBarCanvasPanel } from '../../../src/components/WindowSideBarCanvasPanel'; import { WindowSideBarCanvasPanel } from '../../../src/components/WindowSideBarCanvasPanel';
import { CanvasThumbnail } from '../../../src/components/CanvasThumbnail'; import { CanvasThumbnail } from '../../../src/components/CanvasThumbnail';
import manifestJson from '../../fixtures/version-2/019.json'; import manifestJson from '../../fixtures/version-2/019.json';
import { getIdAndLabelOfCanvases } from '../../../src/state/selectors';
/** /**
* Helper function to create a shallow wrapper around WindowSideBarCanvasPanel * Helper function to create a shallow wrapper around WindowSideBarCanvasPanel
...@@ -68,21 +67,20 @@ describe('WindowSideBarCanvasPanel', () => { ...@@ -68,21 +67,20 @@ describe('WindowSideBarCanvasPanel', () => {
it('should set the correct labels', () => { it('should set the correct labels', () => {
const wrapper = createWrapper(); const wrapper = createWrapper();
const canvases = manifesto.create(manifestJson).getSequences()[0].getCanvases();
const idsAndLabels = getIdAndLabelOfCanvases(canvases);
expect(wrapper expect(wrapper
.find(List) .find(List)
.find(Typography) .find(Typography)
.at(0) .at(0)
.render() .render()
.text()).toBe(idsAndLabels[0].label); .text()).toBe('Test 19 Canvas: 1');
expect(wrapper expect(wrapper
.find(List) .find(List)
.find(Typography) .find(Typography)
.at(1) .at(1)
.render() .render()
.text()).toBe(idsAndLabels[1].label); .text()).toBe('Image 1');
}); });
it('should call the onClick handler of a list item', () => { it('should call the onClick handler of a list item', () => {
...@@ -90,4 +88,36 @@ describe('WindowSideBarCanvasPanel', () => { ...@@ -90,4 +88,36 @@ describe('WindowSideBarCanvasPanel', () => {
wrapper.find(ListItem).at(1).simulate('click'); wrapper.find(ListItem).at(1).simulate('click');
expect(setCanvas).toHaveBeenCalledTimes(1); expect(setCanvas).toHaveBeenCalledTimes(1);
}); });
describe('getIdAndLabelOfCanvases', () => {
it('should return id and label of each canvas in manifest', () => {
const canvases = manifesto
.create(manifestJson)
.getSequences()[0]
.getCanvases();
const wrapper = createWrapper({ canvases });
const received = wrapper.instance().getIdAndLabelOfCanvases(canvases);
const expected = [
{
id: 'http://iiif.io/api/presentation/2.0/example/fixtures/canvas/24/c1.json',
label: 'Test 19 Canvas: 1',
},
{
id: 'https://purl.stanford.edu/fr426cg9537/iiif/canvas/fr426cg9537_1',
label: 'Image 1',
},
{
id: 'https://purl.stanford.edu/rz176rt6531/iiif/canvas/rz176rt6531_1',
label: 'Image 2',
},
];
expect(received).toEqual(expected);
});
it('should return empty array if canvas if empty', () => {
const wrapper = createWrapper({ canvases: [] });
const received = wrapper.instance().getIdAndLabelOfCanvases([]);
expect(received).toEqual([]);
});
});
}); });
import manifesto from 'manifesto.js';
import manifestFixture001 from '../../fixtures/version-2/001.json'; import manifestFixture001 from '../../fixtures/version-2/001.json';
import manifestFixture002 from '../../fixtures/version-2/002.json';
import manifestFixture019 from '../../fixtures/version-2/019.json';
import manifestFixtureWithAProvider from '../../fixtures/version-3/with_a_provider.json';
import { import {
getCanvasLabel, getCanvasLabel,
getCompanionWindowForPosition, getCompanionWindowForPosition,
getAnnotationResourcesByMotivation, getAnnotationResourcesByMotivation,
getIdAndContentOfResources, getIdAndContentOfResources,
getLanguagesFromConfigWithCurrent, getLanguagesFromConfigWithCurrent,
getSelectedCanvas,
getSelectedCanvases,
getThumbnailNavigationPosition, getThumbnailNavigationPosition,
getSelectedAnnotationIds, getSelectedAnnotationIds,
getSelectedTargetAnnotations, getSelectedTargetAnnotations,
getSelectedTargetsAnnotations, getSelectedTargetsAnnotations,
getSelectedTargetAnnotationResources, getSelectedTargetAnnotationResources,
getWindowViewType, getWindowViewType,
getIdAndLabelOfCanvases,
getCompanionWindowsOfWindow, getCompanionWindowsOfWindow,
getWindowTitles,
} 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';
...@@ -36,17 +28,17 @@ describe('getThumbnailNavigationPosition', () => { ...@@ -36,17 +28,17 @@ describe('getThumbnailNavigationPosition', () => {
}; };
it('should return thumbnail navigation position if window exists', () => { it('should return thumbnail navigation position if window exists', () => {
const received = getThumbnailNavigationPosition(state, 'a'); const received = getThumbnailNavigationPosition(state, { windowId: 'a' });
expect(received).toBe('bottom'); expect(received).toBe('bottom');
}); });
it('should return undefined if position does not exist in window', () => { it('should return undefined if position does not exist in window', () => {
const received = getThumbnailNavigationPosition(state, 'b'); const received = getThumbnailNavigationPosition(state, { windowId: 'b' });
expect(received).toBeUndefined(); expect(received).toBeUndefined();
}); });
it('should return undefined if window does not exists', () => { it('should return undefined if window does not exists', () => {
const received = getThumbnailNavigationPosition(state, 'c'); const received = getThumbnailNavigationPosition(state, { windowId: 'c' });
expect(received).toBeUndefined(); expect(received).toBeUndefined();
}); });
}); });
...@@ -60,40 +52,53 @@ describe('getWindowViewType', () => { ...@@ -60,40 +52,53 @@ describe('getWindowViewType', () => {
}; };
it('should return view type if window exists', () => { it('should return view type if window exists', () => {
const received = getWindowViewType(state, 'a'); const received = getWindowViewType(state, { windowId: 'a' });
expect(received).toBe('single'); expect(received).toBe('single');
}); });
it('should return undefined if view type does not exist in window', () => { it('should return undefined if view type does not exist in window', () => {
const received = getWindowViewType(state, 'b'); const received = getWindowViewType(state, { windowId: 'b' });
expect(received).toBeUndefined(); expect(received).toBeUndefined();
}); });
it('should return undefined if window does not exists', () => { it('should return undefined if window does not exists', () => {
const received = getWindowViewType(state, 'c'); const received = getWindowViewType(state, { windowId: 'c' });
expect(received).toBeUndefined(); expect(received).toBeUndefined();
}); });
}); });
describe('getCanvasLabel', () => { describe('getCanvasLabel', () => {
it('should return label of the canvas', () => { it('should return label of the canvas', () => {
const canvas = manifesto const state = { manifests: { a: { json: manifestFixture001 } } };
.create(manifestFixture001) const received = getCanvasLabel(state, { manifestId: 'a', canvasIndex: 0 });
.getSequences()[0]
.getCanvases()[0];
const received = getCanvasLabel(canvas);
expect(received).toBe('Whole Page'); expect(received).toBe('Whole Page');
}); });
it('should return undefined if the canvas is undefined', () => { it('should return undefined if the canvas is undefined', () => {
expect(getCanvasLabel(undefined)).toBeUndefined(); 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', () => { it('should return the canvas index as (+1) as string if no label given', () => {
const canvas = { getLabel: () => [] }; const manifest = {
const received = getCanvasLabel(canvas, 42); '@context': 'http://iiif.io/api/presentation/2/context.json',
expect(received).toBe('43'); '@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');
}); });
}); });
...@@ -202,36 +207,6 @@ describe('getIdAndContentOfResources', () => { ...@@ -202,36 +207,6 @@ describe('getIdAndContentOfResources', () => {
}); });
}); });
describe('getIdAndLabelOfCanvases', () => {
it('should return id and label of each canvas in manifest', () => {
const canvases = manifesto
.create(manifestFixture019)
.getSequences()[0]
.getCanvases();
const received = getIdAndLabelOfCanvases(canvases);
const expected = [
{
id: 'http://iiif.io/api/presentation/2.0/example/fixtures/canvas/24/c1.json',
label: 'Test 19 Canvas: 1',
},
{
id: 'https://purl.stanford.edu/fr426cg9537/iiif/canvas/fr426cg9537_1',
label: 'Image 1',
},
{
id: 'https://purl.stanford.edu/rz176rt6531/iiif/canvas/rz176rt6531_1',
label: 'Image 2',
},
];
expect(received).toEqual(expected);
});
it('should return empty array if canvas if empty', () => {
const received = getIdAndLabelOfCanvases([]);
expect(received).toEqual([]);
});
});
describe('getCompanionWindowForPosition', () => { describe('getCompanionWindowForPosition', () => {
const state = { const state = {
windows: { a: { companionWindowIds: ['abc'] } }, windows: { a: { companionWindowIds: ['abc'] } },
...@@ -242,19 +217,19 @@ describe('getCompanionWindowForPosition', () => { ...@@ -242,19 +217,19 @@ describe('getCompanionWindowForPosition', () => {
}; };
it('the companion window type based on the given position', () => { it('the companion window type based on the given position', () => {
const received = getCompanionWindowForPosition(state, 'a', 'right'); const received = getCompanionWindowForPosition(state, { windowId: 'a', position: 'right' });
expect(received.id).toEqual('abc'); expect(received.id).toEqual('abc');
}); });
it('returns undefined if the given window does not exist', () => { it('returns undefined if the given window does not exist', () => {
const received = getCompanionWindowForPosition(state, 'c', 'right'); const received = getCompanionWindowForPosition(state, { windowId: 'c', position: 'right' });
expect(received).toBeUndefined(); expect(received).toBeUndefined();
}); });
it('returns undefined if a companion window at the given position does not exist', () => { it('returns undefined if a companion window at the given position does not exist', () => {
const received = getCompanionWindowForPosition(state, 'a', 'bottom'); const received = getCompanionWindowForPosition(state, { windowId: 'a', position: 'bottom' });
expect(received).toBeUndefined(); expect(received).toBeUndefined();
}); });
...@@ -295,7 +270,7 @@ describe('getCompanionWindowsOfWindow', () => { ...@@ -295,7 +270,7 @@ describe('getCompanionWindowsOfWindow', () => {
}; };
it('should return companion windows for a given window id', () => { it('should return companion windows for a given window id', () => {
const received = getCompanionWindowsOfWindow(state, 'abc123'); const received = getCompanionWindowsOfWindow(state, { windowId: 'abc123' });
expect(received).toEqual([ expect(received).toEqual([
{ id: 'foo', content: 'info' }, { id: 'foo', content: 'info' },
......
...@@ -2,7 +2,6 @@ import React, { Component } from 'react'; ...@@ -2,7 +2,6 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import Typography from '@material-ui/core/Typography'; import Typography from '@material-ui/core/Typography';
import ManifestoCanvas from '../lib/ManifestoCanvas'; import ManifestoCanvas from '../lib/ManifestoCanvas';
import { getCanvasLabel } from '../state/selectors';
import { CanvasThumbnail } from './CanvasThumbnail'; import { CanvasThumbnail } from './CanvasThumbnail';
import ns from '../config/css-ns'; import ns from '../config/css-ns';
...@@ -52,7 +51,7 @@ export class GalleryView extends Component { ...@@ -52,7 +51,7 @@ export class GalleryView extends Component {
aspectRatio={manifestoCanvas.aspectRatio} aspectRatio={manifestoCanvas.aspectRatio}
/> />
<Typography variant="caption"> <Typography variant="caption">
{getCanvasLabel(canvas, canvas.index)} {manifestoCanvas.getLabel()}
</Typography> </Typography>
</div> </div>
); );
......
...@@ -5,7 +5,6 @@ import Grid from 'react-virtualized/dist/commonjs/Grid'; ...@@ -5,7 +5,6 @@ import Grid from 'react-virtualized/dist/commonjs/Grid';
import GridListTile from '@material-ui/core/GridListTile'; import GridListTile from '@material-ui/core/GridListTile';
import GridListTileBar from '@material-ui/core/GridListTileBar'; import GridListTileBar from '@material-ui/core/GridListTileBar';
import Typography from '@material-ui/core/Typography'; import Typography from '@material-ui/core/Typography';
import { getCanvasLabel } from '../state/selectors';
import { CanvasThumbnail } from './CanvasThumbnail'; import { CanvasThumbnail } from './CanvasThumbnail';
import ManifestoCanvas from '../lib/ManifestoCanvas'; import ManifestoCanvas from '../lib/ManifestoCanvas';
import ns from '../config/css-ns'; import ns from '../config/css-ns';
...@@ -97,7 +96,7 @@ export class ThumbnailNavigation extends Component { ...@@ -97,7 +96,7 @@ export class ThumbnailNavigation extends Component {
classes={{ root: classes.root }} classes={{ root: classes.root }}
title={( title={(
<Typography classes={{ root: classes.title }} variant="caption"> <Typography classes={{ root: classes.title }} variant="caption">
{getCanvasLabel(canvas, canvas.index)} {manifestoCanvas.getLabel()}
</Typography> </Typography>
)} )}
/> />
......
...@@ -10,7 +10,6 @@ import Select from '@material-ui/core/Select'; ...@@ -10,7 +10,6 @@ import Select from '@material-ui/core/Select';
import { CanvasThumbnail } from './CanvasThumbnail'; import { CanvasThumbnail } from './CanvasThumbnail';
import ManifestoCanvas from '../lib/ManifestoCanvas'; import ManifestoCanvas from '../lib/ManifestoCanvas';
import CompanionWindow from '../containers/CompanionWindow'; import CompanionWindow from '../containers/CompanionWindow';
import { getIdAndLabelOfCanvases } from '../state/selectors';
/** /**
* a panel showing the canvases for a given manifest * a panel showing the canvases for a given manifest
...@@ -24,6 +23,16 @@ export class WindowSideBarCanvasPanel extends Component { ...@@ -24,6 +23,16 @@ export class WindowSideBarCanvasPanel extends Component {
this.handleVariantChange = this.handleVariantChange.bind(this); this.handleVariantChange = this.handleVariantChange.bind(this);
} }
/** @private */
getIdAndLabelOfCanvases() {
const { canvases } = this.props;
return canvases.map((canvas, index) => ({
id: canvas.id,
label: new ManifestoCanvas(canvas).getLabel(),
}));
}
/** */ /** */
handleVariantChange(event) { handleVariantChange(event) {
this.setState({ variant: event.target.value }); this.setState({ variant: event.target.value });
...@@ -88,7 +97,7 @@ export class WindowSideBarCanvasPanel extends Component { ...@@ -88,7 +97,7 @@ export class WindowSideBarCanvasPanel extends Component {
const { variant } = this.state; const { variant } = this.state;
const canvasesIdAndLabel = getIdAndLabelOfCanvases(canvases); const canvasesIdAndLabel = this.getIdAndLabelOfCanvases(canvases);
return ( return (
<CompanionWindow <CompanionWindow
title={t('canvasIndex')} title={t('canvasIndex')}
......
...@@ -10,7 +10,7 @@ import { CompanionArea } from '../components/CompanionArea'; ...@@ -10,7 +10,7 @@ import { CompanionArea } from '../components/CompanionArea';
/** */ /** */
const mapStateToProps = (state, { windowId, position }) => ({ const mapStateToProps = (state, { windowId, position }) => ({
sideBarOpen: state.windows[windowId].sideBarOpen, sideBarOpen: state.windows[windowId].sideBarOpen,
companionWindows: getCompanionWindowsOfWindow(state, windowId) companionWindows: getCompanionWindowsOfWindow(state, { windowId })
.filter(cw => cw.position === position), .filter(cw => cw.position === position),
companionAreaOpen: position !== 'left' || state.windows[windowId].companionAreaOpen, companionAreaOpen: position !== 'left' || state.windows[windowId].companionAreaOpen,
}); });
......
...@@ -9,7 +9,6 @@ import * as actions from '../state/actions'; ...@@ -9,7 +9,6 @@ import * as actions from '../state/actions';
import { import {
getCanvasLabel, getCanvasLabel,
getSelectedAnnotationIds, getSelectedAnnotationIds,
getSelectedCanvas,
getSelectedTargetAnnotationResources, getSelectedTargetAnnotationResources,
} from '../state/selectors'; } from '../state/selectors';
...@@ -22,10 +21,7 @@ const mapStateToProps = ({ ...@@ -22,10 +21,7 @@ const mapStateToProps = ({
viewers, windows, manifests, annotations, viewers, windows, manifests, annotations,
}, { windowId, currentCanvases }) => ({ }, { windowId, currentCanvases }) => ({
viewer: viewers[windowId], viewer: viewers[windowId],
label: getCanvasLabel( label: getCanvasLabel({ windows, manifests }, { windowId, canvasIndex: 'selected' }),
getSelectedCanvas({ windows, manifests }, { windowId }),
windows[windowId].canvasIndex,
),
annotations: getSelectedTargetAnnotationResources( annotations: getSelectedTargetAnnotationResources(
{ annotations }, { annotations },
currentCanvases.map(c => c.id), currentCanvases.map(c => c.id),
......
...@@ -18,7 +18,7 @@ const mapStateToProps = (state, props) => { ...@@ -18,7 +18,7 @@ const mapStateToProps = (state, props) => {
return { return {
canvasCount: canvases.length, canvasCount: canvases.length,
canvasIndex, canvasIndex,
canvasLabel: getCanvasLabel(canvases[canvasIndex], canvasIndex), canvasLabel: getCanvasLabel(state, { windowId, canvasIndex }),
}; };
}; };
......
...@@ -18,7 +18,7 @@ const mapStateToProps = (state, props) => ({ ...@@ -18,7 +18,7 @@ const mapStateToProps = (state, props) => ({
window: state.windows[props.window.id], window: state.windows[props.window.id],
workspaceType: state.config.workspace.type, workspaceType: state.config.workspace.type,
label: getManifestTitle(state, { windowId: props.window.id }), label: getManifestTitle(state, { windowId: props.window.id }),
thumbnailNavigationPosition: getThumbnailNavigationPosition(state, props.window.id), thumbnailNavigationPosition: getThumbnailNavigationPosition(state, { windowId: props.window.id }),
}); });
/** /**
......
...@@ -3,17 +3,13 @@ import { compose } from 'redux'; ...@@ -3,17 +3,13 @@ import { compose } from 'redux';
import { withPlugins } from '../extend'; import { withPlugins } from '../extend';
import { import {
getCanvasLabel, getCanvasLabel,
getSelectedCanvas,
} from '../state/selectors'; } from '../state/selectors';
import { WindowCanvasNavigationControls } from '../components/WindowCanvasNavigationControls'; import { WindowCanvasNavigationControls } from '../components/WindowCanvasNavigationControls';
/** */ /** */
const mapStateToProps = (state, { windowId }) => ({ const mapStateToProps = (state, { windowId }) => ({
window: state.windows[windowId], window: state.windows[windowId],
canvasLabel: getCanvasLabel( canvasLabel: getCanvasLabel(state, { windowId, canvasIndex: 'selected' }),
getSelectedCanvas(state, { windowId }),
state.windows[windowId].canvasIndex,
),
visible: state.workspace.focusedWindowId === windowId, visible: state.workspace.focusedWindowId === windowId,
}); });
......
...@@ -36,7 +36,7 @@ const mapStateToProps = (state, { windowId }) => ({ ...@@ -36,7 +36,7 @@ const mapStateToProps = (state, { windowId }) => ({
getSelectedTargetAnnotations(state, (getSelectedCanvas(state, { windowId }) || {}).id), getSelectedTargetAnnotations(state, (getSelectedCanvas(state, { windowId }) || {}).id),
['oa:commenting', 'sc:painting'], ['oa:commenting', 'sc:painting'],
).length > 0, ).length > 0,
sideBarPanel: (getCompanionWindowForPosition(state, windowId, 'left') || {}).content, sideBarPanel: (getCompanionWindowForPosition(state, { windowId, position: 'left' }) || {}).content,
}); });
/** */ /** */
......
...@@ -20,11 +20,8 @@ import { WindowSideBarInfoPanel } from '../components/WindowSideBarInfoPanel'; ...@@ -20,11 +20,8 @@ import { WindowSideBarInfoPanel } from '../components/WindowSideBarInfoPanel';
* @private * @private
*/ */
const mapStateToProps = (state, { windowId }) => ({ const mapStateToProps = (state, { windowId }) => ({
canvasLabel: getCanvasLabel( canvasLabel: getCanvasLabel(state, { windowId, canvasIndex: 'selected' }),
getSelectedCanvas(state, { windowId }), canvasDescription: getCanvasDescription(state, { windowId, canvasIndex: 'selected' }),
state.windows[windowId].canvasIndex,
),
canvasDescription: getCanvasDescription(getSelectedCanvas(state, { windowId })),
canvasMetadata: getDestructuredMetadata(getSelectedCanvas(state, { windowId })), canvasMetadata: getDestructuredMetadata(getSelectedCanvas(state, { windowId })),
manifestLabel: getManifestTitle(state, { windowId }), manifestLabel: getManifestTitle(state, { windowId }),
manifestDescription: getManifestDescription(state, { windowId }), manifestDescription: getManifestDescription(state, { windowId }),
......
...@@ -19,9 +19,9 @@ const mapDispatchToProps = { setWindowThumbnailPosition: actions.setWindowThumbn ...@@ -19,9 +19,9 @@ const mapDispatchToProps = { setWindowThumbnailPosition: actions.setWindowThumbn
* @memberof WindowViewer * @memberof WindowViewer
* @private * @private
*/ */
const mapStateToProps = (state, props) => ( const mapStateToProps = (state, { windowId }) => (
{ {
thumbnailNavigationPosition: getThumbnailNavigationPosition(state, props.windowId), thumbnailNavigationPosition: getThumbnailNavigationPosition(state, { windowId }),
} }
); );
......
...@@ -19,9 +19,9 @@ const mapDispatchToProps = { setWindowViewType: actions.setWindowViewType }; ...@@ -19,9 +19,9 @@ const mapDispatchToProps = { setWindowViewType: actions.setWindowViewType };
* @memberof WindowViewer * @memberof WindowViewer
* @private * @private
*/ */
const mapStateToProps = (state, props) => ( const mapStateToProps = (state, { windowId }) => (
{ {
windowViewType: getWindowViewType(state, props.windowId), windowViewType: getWindowViewType(state, { windowId }),
} }
); );
......
...@@ -79,6 +79,15 @@ export default class ManifestoCanvas { ...@@ -79,6 +79,15 @@ export default class ManifestoCanvas {
); );
} }
/**
* Get the canvas label
*/
getLabel() {
return this.canvas.getLabel().length > 0
? this.canvas.getLabel().map(label => label.value)[0]
: String(this.canvas.index + 1);
}
/** /**
* Creates a canonical image request for a thumb * Creates a canonical image request for a thumb
* @param {Number} height * @param {Number} height
......
...@@ -2,18 +2,27 @@ import { createSelector } from 'reselect'; ...@@ -2,18 +2,27 @@ import { createSelector } from 'reselect';
import CanvasGroupings from '../../lib/CanvasGroupings'; import CanvasGroupings from '../../lib/CanvasGroupings';
import { getManifestoInstance } from './manifests'; import { getManifestoInstance } from './manifests';
export const getCanvases = createSelector(
[getManifestoInstance],
manifest => manifest && manifest.getSequences()[0].getCanvases(),
);
/** /**
* Return the current canvas selected in a window * Return the canvas selected by an index
* @param {object} state * @param {object} state
* @param {object} props * @param {object} props
* @param {string} props.manifestId * @param {string} props.manifestId
* @param {string} props.windowId * @param {string} props.windowId
* @return {Object} * @return {Object}
*/ */
export const getSelectedCanvas = createSelector( export const getCanvas = createSelector(
[ [
getManifestoInstance, getManifestoInstance,
(state, { windowId }) => state.windows[windowId].canvasIndex, (state, { windowId, canvasIndex }) => (
canvasIndex === 'selected'
? state.windows[windowId].canvasIndex
: canvasIndex
),
], ],
(manifest, canvasIndex) => manifest (manifest, canvasIndex) => manifest
&& manifest && manifest
...@@ -21,6 +30,18 @@ export const getSelectedCanvas = createSelector( ...@@ -21,6 +30,18 @@ export const getSelectedCanvas = createSelector(
.getCanvasByIndex(canvasIndex), .getCanvasByIndex(canvasIndex),
); );
/**
* Return the current canvas selected in a window
* @param {object} state
* @param {object} props
* @param {string} props.manifestId
* @param {string} props.windowId
* @return {Object}
*/
export function getSelectedCanvas(state, props) {
return getCanvas(state, { ...props, canvasIndex: 'selected' });
}
/** /**
* Return the current canvases selected in a window * Return the current canvases selected in a window
* For book view returns 2, for single returns 1 * For book view returns 2, for single returns 1
...@@ -32,12 +53,12 @@ export const getSelectedCanvas = createSelector( ...@@ -32,12 +53,12 @@ export const getSelectedCanvas = createSelector(
*/ */
export const getSelectedCanvases = createSelector( export const getSelectedCanvases = createSelector(
[ [
getManifestoInstance, getCanvases,
(state, { windowId }) => state.windows[windowId], (state, { windowId }) => state.windows[windowId],
], ],
(manifest, { canvasIndex, view }) => manifest (canvases, { canvasIndex, view }) => canvases
&& new CanvasGroupings( && new CanvasGroupings(
manifest.getSequences()[0].getCanvases(), canvases,
view, view,
).getCanvases(canvasIndex), ).getCanvases(canvasIndex),
); );
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';
export * from './windows'; export * from './windows';
/**
* Return ids and labels of canvases
* @ param {Array} canvases
* @return {Array} - [ {id: 'id', label: 'label' }, ... ]
*/
export function getIdAndLabelOfCanvases(canvases) {
return canvases.map((canvas, index) => ({
id: canvas.id,
label: getCanvasLabel(canvas, index),
}));
}
/** /**
* Return annotations for an array of targets * Return annotations for an array of targets
* @param {object} state * @param {object} state
...@@ -76,18 +66,25 @@ export function getIdAndContentOfResources(resources) { ...@@ -76,18 +66,25 @@ export function getIdAndContentOfResources(resources) {
})); }));
} }
/** */
function getWindow(state, { windowId }) {
return state.windows && state.windows[windowId];
}
/** Return position of thumbnail navigation in a certain window. /** Return position of thumbnail navigation in a certain window.
* @param {object} state * @param {object} state
* @param {String} windowId * @param {String} windowId
* @param {String} * @param {String}
*/ */
export function getThumbnailNavigationPosition(state, windowId) { export const getThumbnailNavigationPosition = createSelector(
return state.windows[windowId] [
&& state.windows[windowId].thumbnailNavigationId getWindow,
&& state.companionWindows[state.windows[windowId].thumbnailNavigationId] state => state.companionWindows,
&& state.companionWindows[state.windows[windowId].thumbnailNavigationId].position; ],
} (window, companionWindows) => window
&& companionWindows[window.thumbnailNavigationId]
&& companionWindows[window.thumbnailNavigationId].position,
);
/** Return type of view in a certain window. /** Return type of view in a certain window.
* @param {object} state * @param {object} state
...@@ -96,68 +93,66 @@ export function getThumbnailNavigationPosition(state, windowId) { ...@@ -96,68 +93,66 @@ export function getThumbnailNavigationPosition(state, windowId) {
* @param {string} props.windowId * @param {string} props.windowId
* @param {String} * @param {String}
*/ */
export function getWindowViewType(state, windowId) { export const getWindowViewType = createSelector(
return state.windows[windowId] && state.windows[windowId].view; [getWindow],
} window => window && window.view,
);
/** /**
* Return canvas label, or alternatively return the given index + 1 to be displayed * Return canvas label, or alternatively return the given index + 1 to be displayed
* @param {object} canvas * @param {object} canvas
* @return {String|Integer} * @return {String|Integer}
*/ */
export function getCanvasLabel(canvas, canvasIndex) { export const getCanvasLabel = createSelector(
if (!canvas) { [getCanvas],
return undefined; canvas => (canvas && (
} canvas.getLabel().length > 0
if (canvas.getLabel().length > 0) { ? canvas.getLabel().map(label => label.value)[0]
return canvas.getLabel().map(label => label.value)[0]; : String(canvas.index + 1)
} )),
return String(canvasIndex + 1); );
}
/** /**
* Return canvas description * Return canvas description
* @param {object} canvas * @param {object} canvas
* @param {String} * @param {String}
*/ */
export function getCanvasDescription(canvas) { export const getCanvasDescription = createSelector(
return canvas [getCanvas],
&& canvas.getProperty('description'); canvas => canvas && canvas.getProperty('description'),
} );
/** /**
* Return the companion window string from state in a given windowId and position * Return compantion window ids from a window
* @param {object} state
* @param {String} windowId * @param {String} windowId
* @param {String} position * @return {Array}
* @return {String}
*/ */
export function getCompanionWindowForPosition(state, windowId, position) { export const getCompanionWindowIds = createSelector(
return ((state.windows[windowId] || {}).companionWindowIds || []).map(key => ( [getWindow],
state.companionWindows[key] window => (window && window.companionWindowIds) || [],
)).find(cw => ( );
cw.position === position
));
}
/** /**
* Return compantion window ids from a window * Return companion windows of a window
* @param {String} windowId * @param {String} windowId
* @return {Array} * @return {Array}
*/ */
export function getCompanionWindowIds(state, windowId) { export const getCompanionWindowsOfWindow = createSelector(
return state.windows[windowId].companionWindowIds; [getCompanionWindowIds, state => state.companionWindows],
} (companionWindowIds, companionWindows) => companionWindowIds.map(id => companionWindows[id]),
);
/** /**
* Return companion windows of a window * Return the companion window string from state in a given windowId and position
* @param {object} state
* @param {String} windowId * @param {String} windowId
* @return {Array} * @param {String} position
* @return {String}
*/ */
export function getCompanionWindowsOfWindow(state, windowId) { export const getCompanionWindowForPosition = createSelector(
return getCompanionWindowIds(state, windowId) [getCompanionWindowsOfWindow, (state, { position }) => position],
.map(id => state.companionWindows[id]); (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
......
...@@ -6,7 +6,7 @@ import ManifestoCanvas from '../../lib/ManifestoCanvas'; ...@@ -6,7 +6,7 @@ import ManifestoCanvas from '../../lib/ManifestoCanvas';
export function getManifest(state, { manifestId, windowId }) { export function getManifest(state, { manifestId, windowId }) {
return state.manifests[ return state.manifests[
manifestId manifestId
|| (windowId && state.windows[windowId] && state.windows[windowId].manifestId) || (windowId && state.windows && state.windows[windowId] && state.windows[windowId].manifestId)
]; ];
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment