-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathCollectionDialog.js
More file actions
244 lines (223 loc) · 7.6 KB
/
CollectionDialog.js
File metadata and controls
244 lines (223 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { useContext, useState } from 'react';
import PropTypes from 'prop-types';
import {
Button,
Chip,
Dialog,
DialogActions,
DialogTitle,
Link,
MenuList,
MenuItem,
Typography,
} from '@mui/material';
import ArrowBackIcon from '@mui/icons-material/ArrowBackSharp';
import Skeleton from '@mui/material/Skeleton';
import { styled } from '@mui/material/styles';
import { useTranslation } from 'react-i18next';
import asArray from '../lib/asArray';
import { LabelValueMetadata } from './LabelValueMetadata';
import CollapsibleSection from '../containers/CollapsibleSection';
import ScrollIndicatedDialogContent from '../containers/ScrollIndicatedDialogContent';
import ManifestInfo from '../containers/ManifestInfo';
import WorkspaceContext from '../contexts/WorkspaceContext';
import { IIIFResourceLabel } from './IIIFResourceLabel';
const StyledScrollIndicatedDialogContent = styled(ScrollIndicatedDialogContent)(() => ({
padding: (theme) => theme.spacing(1),
}));
const StyledCollectionMetadata = styled('div')(() => ({
'& .MuiPaper-root': {
background: 'transparent',
},
padding: (theme) => theme.spacing(2),
}));
const StyledCollectionFilter = styled('div')(() => ({
padding: (theme) => theme.spacing(2),
paddingTop: 0,
}));
/** */
const Placeholder = ({ onClose, container }) => (
<Dialog
variant="contained"
onClose={onClose}
open
container={container}
>
<DialogTitle id="select-collection">
<Skeleton variant="text" />
</DialogTitle>
<ScrollIndicatedDialogContent>
<Skeleton variant="text" />
<Skeleton variant="text" />
</ScrollIndicatedDialogContent>
</Dialog>
);
Placeholder.propTypes = {
container: PropTypes.instanceOf(Element).isRequired,
onClose: PropTypes.func.isRequired,
};
/**
* a dialog providing the possibility to select the collection
*/
export function CollectionDialog({
addWindow, collection = null, collectionPath = [], error = null, hideCollectionDialog,
isMultipart = false, manifest, manifestId, ready = false,
setWorkspaceAddVisibility, showCollectionDialog, updateWindow, windowId = null,
}) {
const container = useContext(WorkspaceContext);
const { t } = useTranslation();
const [filter, setFilter] = useState(null);
/** */
const hideDialog = () => {
hideCollectionDialog(windowId);
};
/** */
const selectCollection = (c) => {
showCollectionDialog(c.id, [...collectionPath, manifestId], windowId);
};
/** */
const goToPreviousCollection = () => {
showCollectionDialog(
collectionPath[collectionPath.length - 1],
collectionPath.slice(0, -1),
windowId,
);
};
/** */
const selectManifest = (m) => {
if (windowId) {
updateWindow(windowId, {
canvasId: null, collectionPath: [...collectionPath, manifestId], manifestId: m.id,
});
} else {
addWindow({ collectionPath: [...collectionPath, manifestId], manifestId: m.id });
}
hideDialog();
setWorkspaceAddVisibility(false);
};
/** */
const dialogContainer = (container?.current || document.body).querySelector(`#${windowId}`);
if (error) return null;
if (!dialogContainer) {
return null;
}
if (!ready) return <Placeholder container={dialogContainer} onClose={hideDialog} />;
const rights = manifest && (asArray(manifest.getProperty('rights') || manifest.getProperty('license')));
const requiredStatement = manifest
&& asArray(manifest.getRequiredStatement()).filter(l => l && l.getValue()).map(labelValuePair => ({
label: null,
values: labelValuePair.getValues(),
}));
const collections = manifest.getCollections();
const currentFilter = filter || (collections.length > 0 ? 'collections' : 'manifests');
return (
<Dialog
variant="contained"
onClose={hideDialog}
container={dialogContainer}
open
>
<DialogTitle id="select-collection">
<Typography component="div" variant="overline">
{ t(isMultipart ? 'multipartCollection' : 'collection') }
</Typography>
<Typography component="div" variant="h3">
<IIIFResourceLabel resource={manifest} />
</Typography>
</DialogTitle>
<StyledScrollIndicatedDialogContent>
{ collection && (
<Button
startIcon={<ArrowBackIcon />}
onClick={() => goToPreviousCollection()}
>
<IIIFResourceLabel resource={collection} />
</Button>
)}
<StyledCollectionMetadata>
<ManifestInfo manifestId={manifest.id} />
<CollapsibleSection
id="select-collection-rights"
label={t('attributionTitle')}
>
{ requiredStatement && (
<LabelValueMetadata labelValuePairs={requiredStatement} defaultLabel={t('attribution')} />
)}
{
rights && rights.length > 0 && (
<>
<Typography variant="subtitle2" component="dt">{t('rights')}</Typography>
{ rights.map(v => (
<Typography variant="body1" component="dd" key={v}>
<Link target="_blank" rel="noopener noreferrer" href={v}>
{v}
</Link>
</Typography>
)) }
</>
)
}
</CollapsibleSection>
</StyledCollectionMetadata>
<StyledCollectionFilter>
{manifest.getTotalCollections() > 0 && (
<Chip clickable color={currentFilter === 'collections' ? 'primary' : 'default'} onClick={() => setFilter('collections')} label={t('totalCollections', { count: manifest.getTotalCollections() })} />
)}
{manifest.getTotalManifests() > 0 && (
<Chip clickable color={currentFilter === 'manifests' ? 'primary' : 'default'} onClick={() => setFilter('manifests')} label={t('totalManifests', { count: manifest.getTotalManifests() })} />
)}
</StyledCollectionFilter>
{ currentFilter === 'collections' && (
<MenuList>
{
collections.map(c => (
<MenuItem
key={c.id}
onClick={() => { selectCollection(c); }}
variant="multiline"
>
<IIIFResourceLabel resource={c} />
</MenuItem>
))
}
</MenuList>
)}
{ currentFilter === 'manifests' && (
<MenuList>
{
manifest.getManifests().map(m => (
<MenuItem
key={m.id}
onClick={() => { selectManifest(m); }}
variant="multiline"
>
<IIIFResourceLabel resource={m} />
</MenuItem>
))
}
</MenuList>
)}
</StyledScrollIndicatedDialogContent>
<DialogActions>
<Button onClick={hideDialog}>
{t('close')}
</Button>
</DialogActions>
</Dialog>
);
}
CollectionDialog.propTypes = {
addWindow: PropTypes.func.isRequired,
collection: PropTypes.object, // eslint-disable-line react/forbid-prop-types
collectionPath: PropTypes.arrayOf(PropTypes.string),
error: PropTypes.string,
hideCollectionDialog: PropTypes.func.isRequired,
isMultipart: PropTypes.bool,
manifest: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
manifestId: PropTypes.string.isRequired,
ready: PropTypes.bool,
setWorkspaceAddVisibility: PropTypes.func.isRequired,
showCollectionDialog: PropTypes.func.isRequired,
updateWindow: PropTypes.func.isRequired,
windowId: PropTypes.string,
};