diff --git a/src/state/selectors/annotations.js b/src/state/selectors/annotations.js new file mode 100644 index 0000000000000000000000000000000000000000..315eb6b1d08f1729637f42d42809894752cd7a5b --- /dev/null +++ b/src/state/selectors/annotations.js @@ -0,0 +1,86 @@ +import { createSelector } from 'reselect'; +import filter from 'lodash/filter'; +import flatten from 'lodash/flatten'; +import Annotation from '../../lib/Annotation'; +import { getSelectedCanvases } from './canvases'; + +const getAnnotationsOnSelectedCanvases = createSelector( + [ + getSelectedCanvases, + state => state.annotations, + ], + (canvases, annotations) => { + if (!annotations || !canvases) return []; + return flatten( + canvases.map(c => c.id).map( + targetId => annotations[targetId] && Object.values(annotations[targetId]), + ), + ); + }, +); + +const getPresentAnnotationsOnSelectedCanvases = createSelector( + [ + getAnnotationsOnSelectedCanvases, + ], + annotations => filter( + Object.values(annotations).map(annotation => annotation && new Annotation(annotation.json)), + annotation => annotation && annotation.present(), + ), +); + +/** +* Return an array of annotation resources filtered by the given motivation +* @param {Array} annotations +* @param {Array} motivations +* @return {Array} +*/ +export const getAnnotationResourcesByMotivation = createSelector( + [ + getPresentAnnotationsOnSelectedCanvases, + (state, { motivations }) => motivations, + ], + (annotations, motivations) => filter( + flatten(annotations.map(annotation => annotation.resources)), + resource => resource.motivations.some( + motivation => motivations.includes(motivation), + ), + ), +); + +/** + * Return the selected annotations IDs of a given CanvasId + * @param {Object} state + * @param {String} windowId + * @param {Array} targetIds + * @return {Array} + */ +export const getSelectedAnnotationIds = createSelector( + [ + (state, { windowId }) => state.windows[windowId].selectedAnnotations, + getSelectedCanvases, + ], + (selectedAnnotations, canvases) => ( + flatten( + canvases.map(c => c.id).map(targetId => selectedAnnotations && selectedAnnotations[targetId]), + ) + ), +); + +export const getAllOrSelectedAnnotationsOnCanvases = createSelector( + [ + getPresentAnnotationsOnSelectedCanvases, + getSelectedAnnotationIds, + (state, { windowId }) => state.windows[windowId].displayAllAnnotations, + ], + (canvasAnnotations, selectedAnnotationIds, displayAllAnnotations) => { + if (displayAllAnnotations) return canvasAnnotations; + + return canvasAnnotations.map(annotation => ({ + id: (annotation['@id'] || annotation.id), + resources: annotation.resources.filter( + r => selectedAnnotationIds && selectedAnnotationIds.includes(r.id), + ), + })); + }, +); diff --git a/src/state/selectors/config.js b/src/state/selectors/config.js new file mode 100644 index 0000000000000000000000000000000000000000..9a498d8337281b3f2710d0829f6d36c243bdb6be --- /dev/null +++ b/src/state/selectors/config.js @@ -0,0 +1,14 @@ +import { createSelector } from 'reselect'; +/** +* Return languages from config (in state) and indicate which is currently set +* @param {object} state +* @return {Array} [ {locale: 'de', label: 'Deutsch', current: true}, ... ] +*/ +export const getLanguagesFromConfigWithCurrent = createSelector( + [state => state.config], + ({ availableLanguages, language }) => Object.keys(availableLanguages).map(key => ({ + current: key === language, + label: availableLanguages[key], + locale: key, + })), +); diff --git a/src/state/selectors/index.js b/src/state/selectors/index.js index 9b2903a8646f8fa3490c7c0961b12e7e067b35eb..099a6675d98d26f7b1464f1f0fea4d178d98229a 100644 --- a/src/state/selectors/index.js +++ b/src/state/selectors/index.js @@ -1,105 +1,5 @@ -import { createSelector } from 'reselect'; -import filter from 'lodash/filter'; -import flatten from 'lodash/flatten'; -import Annotation from '../../lib/Annotation'; -import { getSelectedCanvases } from './canvases'; - +export * from './annotations'; export * from './canvases'; +export * from './config'; export * from './manifests'; export * from './windows'; - -const getAnnotationsOnSelectedCanvases = createSelector( - [ - getSelectedCanvases, - state => state.annotations, - ], - (canvases, annotations) => { - if (!annotations || !canvases) return []; - return flatten( - canvases.map(c => c.id).map( - targetId => annotations[targetId] && Object.values(annotations[targetId]), - ), - ); - }, -); - -const getPresentAnnotationsOnSelectedCanvases = createSelector( - [ - getAnnotationsOnSelectedCanvases, - ], - annotations => filter( - Object.values(annotations).map(annotation => annotation && new Annotation(annotation.json)), - annotation => annotation && annotation.present(), - ), -); - -/** -* Return an array of annotation resources filtered by the given motivation -* @param {Array} annotations -* @param {Array} motivations -* @return {Array} -*/ -export const getAnnotationResourcesByMotivation = createSelector( - [ - getPresentAnnotationsOnSelectedCanvases, - (state, { motivations }) => motivations, - ], - (annotations, motivations) => filter( - flatten(annotations.map(annotation => annotation.resources)), - resource => resource.motivations.some( - motivation => motivations.includes(motivation), - ), - ), -); - -/** -* Return languages from config (in state) and indicate which is currently set -* @param {object} state -* @return {Array} [ {locale: 'de', label: 'Deutsch', current: true}, ... ] -*/ -export function getLanguagesFromConfigWithCurrent(state) { - const { availableLanguages, language } = state.config; - - return Object.keys(availableLanguages).map(key => ({ - current: key === language, - label: availableLanguages[key], - locale: key, - })); -} - -/** - * Return the selected annotations IDs of a given CanvasId - * @param {Object} state - * @param {String} windowId - * @param {Array} targetIds - * @return {Array} - */ -export const getSelectedAnnotationIds = createSelector( - [ - (state, { windowId }) => state.windows[windowId].selectedAnnotations, - getSelectedCanvases, - ], - (selectedAnnotations, canvases) => ( - flatten( - canvases.map(c => c.id).map(targetId => selectedAnnotations && selectedAnnotations[targetId]), - ) - ), -); - -export const getAllOrSelectedAnnotationsOnCanvases = createSelector( - [ - getPresentAnnotationsOnSelectedCanvases, - getSelectedAnnotationIds, - (state, { windowId }) => state.windows[windowId].displayAllAnnotations, - ], - (canvasAnnotations, selectedAnnotationIds, displayAllAnnotations) => { - if (displayAllAnnotations) return canvasAnnotations; - - return canvasAnnotations.map(annotation => ({ - id: (annotation['@id'] || annotation.id), - resources: annotation.resources.filter( - r => selectedAnnotationIds && selectedAnnotationIds.includes(r.id), - ), - })); - }, -);