@vitessce/layer-controller 2.0.3-beta.0 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/deflate.b44720d0.mjs +13 -0
  2. package/dist/index.ca9e70ae.mjs +137540 -0
  3. package/dist/index.mjs +10 -0
  4. package/dist/jpeg.edb20ff0.mjs +840 -0
  5. package/dist/lerc.df2f1733.mjs +1943 -0
  6. package/dist/lzw.2a639594.mjs +128 -0
  7. package/dist/packbits.958f7a40.mjs +30 -0
  8. package/dist/pako.esm.4b234125.mjs +3940 -0
  9. package/dist/raw.4239192e.mjs +12 -0
  10. package/dist/webimage.de21cf13.mjs +32 -0
  11. package/{dist → dist-tsc}/index.js +0 -0
  12. package/package.json +10 -10
  13. package/src/BitmaskChannelController.js +63 -0
  14. package/src/ChannelOptions.js +62 -0
  15. package/src/ColorPalette.js +49 -0
  16. package/src/ImageAddButton.js +51 -0
  17. package/src/LayerController.js +599 -0
  18. package/src/LayerControllerSubscriber.js +510 -0
  19. package/src/LayerOptions.js +463 -0
  20. package/src/RasterChannelController.js +256 -0
  21. package/src/RasterChannelController.test.js +23 -0
  22. package/src/VectorLayerController.js +65 -0
  23. package/src/VolumeOptions.js +218 -0
  24. package/src/constants.js +14 -0
  25. package/src/index.js +3 -0
  26. package/{dist → src}/shared-channel-controls.js +35 -6
  27. package/src/styles.js +113 -0
  28. package/src/utils.js +129 -0
  29. package/dist/BitmaskChannelController.js +0 -31
  30. package/dist/ChannelOptions.js +0 -39
  31. package/dist/ColorPalette.js +0 -31
  32. package/dist/ImageAddButton.js +0 -28
  33. package/dist/LayerController.js +0 -311
  34. package/dist/LayerControllerSubscriber.js +0 -197
  35. package/dist/LayerOptions.js +0 -211
  36. package/dist/RasterChannelController.js +0 -160
  37. package/dist/RasterChannelController.test.js +0 -21
  38. package/dist/VectorLayerController.js +0 -30
  39. package/dist/VolumeOptions.js +0 -78
  40. package/dist/constants.js +0 -12
  41. package/dist/styles.js +0 -103
  42. package/dist/utils.js +0 -109
@@ -0,0 +1,463 @@
1
+ import React from 'react';
2
+ import range from 'lodash/range';
3
+ import { Matrix4 } from 'math.gl';
4
+ import Grid from '@material-ui/core/Grid';
5
+ import Slider from '@material-ui/core/Slider';
6
+ import InputLabel from '@material-ui/core/InputLabel';
7
+ import Select from '@material-ui/core/Select';
8
+ import Checkbox from '@material-ui/core/Checkbox';
9
+ import { viv } from '@vitessce/gl';
10
+ import { DEFAULT_RASTER_DOMAIN_TYPE } from '@vitessce/spatial-utils';
11
+ import {
12
+ COLORMAP_OPTIONS,
13
+ canLoadResolution,
14
+ formatBytes,
15
+ getStatsForResolution,
16
+ } from '@vitessce/utils';
17
+ import {
18
+ getBoundingCube, getMultiSelectionStats,
19
+ } from './utils';
20
+ import { StyledSelectionSlider, useSelectStyles } from './styles';
21
+
22
+ const DOMAIN_OPTIONS = ['Full', 'Min/Max'];
23
+
24
+ /**
25
+ * Wrapper for the dropdown that selects a colormap (None, viridis, magma, etc.).
26
+ * @prop {Object} loader Loader object with metadata.
27
+ * @prop {function} handleMultiPropertyChange Function to propgate multiple layer changes at once.
28
+ * This prevents updates from overridding each other.
29
+ * @prop {number} resolution Current 3D resolution.
30
+ * @prop {boolean} disable3d Whether or not to enable 3D selection
31
+ * @prop {function} setRasterLayerCallback Setter for callbacks that fire after raster/volume loads.
32
+ * @prop {function} setAreAllChannelsLoading Setter for whether or not a given channel is loading.
33
+ * @prop {Object} setViewState Setter for the current view state.
34
+ * @prop {number} spatialHeight Height of the spatial component.
35
+ * @prop {number} spatialWidth Width of the spatial component.
36
+ * @prop {object} channels Channels object.
37
+ * @prop {boolean} use3d Whether or not 3D is enabled for this layer.
38
+ */
39
+ function VolumeDropdown({
40
+ loader: loaderWithMeta,
41
+ handleMultiPropertyChange,
42
+ resolution: currResolution,
43
+ disable3d,
44
+ setRasterLayerCallback,
45
+ setAreAllChannelsLoading,
46
+ setViewState,
47
+ spatialHeight,
48
+ spatialWidth,
49
+ channels,
50
+ use3d,
51
+ modelMatrix,
52
+ }) {
53
+ const classes = useSelectStyles();
54
+ const selections = channels.map(i => i.selection);
55
+ const { data: loader } = loaderWithMeta;
56
+ const handleChange = async (val) => {
57
+ // val is the resolution - null indicates 2D
58
+ const shouldUse3D = typeof val === 'number';
59
+ setAreAllChannelsLoading(true);
60
+ setRasterLayerCallback(() => {
61
+ setAreAllChannelsLoading(false);
62
+ setRasterLayerCallback(null);
63
+ });
64
+ if (shouldUse3D) {
65
+ const [xSlice, ySlice, zSlice] = getBoundingCube(loader);
66
+ const propertiesChanged = {
67
+ resolution: val,
68
+ xSlice,
69
+ ySlice,
70
+ zSlice,
71
+ use3d: shouldUse3D,
72
+ };
73
+ // Only make the fetch if needed i.e if the 3d was just being turned on.
74
+ if (!use3d) {
75
+ const { sliders } = await getMultiSelectionStats({
76
+ loader,
77
+ selections,
78
+ use3d: shouldUse3D,
79
+ });
80
+ propertiesChanged.channels = [...channels];
81
+ propertiesChanged.channels.forEach((ch, i) => {
82
+ // eslint-disable-next-line no-param-reassign
83
+ ch.slider = sliders[i];
84
+ });
85
+ }
86
+ // Update all properties at once to avoid overriding calls.
87
+ handleMultiPropertyChange(propertiesChanged);
88
+ const defaultViewState = viv.getDefaultInitialViewState(loader,
89
+ { height: spatialHeight, width: spatialWidth }, 1.5, true, new Matrix4(modelMatrix));
90
+ setViewState({
91
+ ...defaultViewState,
92
+ rotationX: 0,
93
+ rotationOrbit: 0,
94
+ });
95
+ } else {
96
+ const { sliders } = await getMultiSelectionStats({
97
+ loader, selections, use3d: shouldUse3D,
98
+ });
99
+ const newChannels = [...channels];
100
+ newChannels.forEach((ch, i) => {
101
+ // eslint-disable-next-line no-param-reassign
102
+ ch.slider = sliders[i];
103
+ });
104
+ // Update all properties at once to avoid overriding calls.
105
+ handleMultiPropertyChange({
106
+ resolution: val,
107
+ use3d: shouldUse3D,
108
+ spatialAxisFixed: false,
109
+ channels: newChannels,
110
+ });
111
+ const defaultViewState = viv.getDefaultInitialViewState(loader,
112
+ { height: spatialHeight, width: spatialWidth }, 0.5, false, new Matrix4(modelMatrix));
113
+ setViewState({
114
+ ...defaultViewState,
115
+ rotationX: null,
116
+ rotationOrbit: null,
117
+ orbitAxis: null,
118
+ });
119
+ }
120
+ };
121
+ const { labels, shape } = Array.isArray(loader) ? loader[0] : loader;
122
+ const hasZStack = shape[labels.indexOf('z')] > 1;
123
+ return (
124
+ <>
125
+ <Select
126
+ native
127
+ value={currResolution}
128
+ onChange={e => handleChange(
129
+ e.target.value === '2D' ? e.target.value : Number(e.target.value),
130
+ )
131
+ }
132
+ classes={{ root: classes.selectRoot }}
133
+ >
134
+ <option key="2D" value="2D">
135
+ 2D Visualization
136
+ </option>
137
+ {Array.from({ length: loader.length })
138
+ .fill(0)
139
+ // eslint-disable-next-line no-unused-vars
140
+ .map((_, resolution) => {
141
+ if (loader) {
142
+ if (canLoadResolution(loader, resolution)) {
143
+ const {
144
+ height,
145
+ width,
146
+ depthDownsampled,
147
+ totalBytes,
148
+ } = getStatsForResolution(loader, resolution);
149
+ return (
150
+ <option
151
+ key={`(${height}, ${width}, ${depthDownsampled})`}
152
+ value={resolution}
153
+ disabled={
154
+ disable3d
155
+ || !hasZStack
156
+ }
157
+ >
158
+ {`3D: ${resolution}x Downsampled, ~${formatBytes(
159
+ totalBytes,
160
+ )} per channel, (${height}, ${width}, ${depthDownsampled})`}
161
+ </option>
162
+ );
163
+ }
164
+ }
165
+ return null;
166
+ })}
167
+ </Select>
168
+ </>
169
+ );
170
+ }
171
+
172
+
173
+ /**
174
+ * Wrapper for the dropdown that selects a colormap (None, viridis, magma, etc.).
175
+ * @prop {string} value Currently selected value for the colormap.
176
+ * @prop {string} inputId Css id.
177
+ * @prop {function} handleChange Callback for every change in colormap.
178
+ */
179
+ function ColormapSelect({ value, inputId, handleChange }) {
180
+ const classes = useSelectStyles();
181
+ return (
182
+ <Select
183
+ native
184
+ onChange={e => handleChange(e.target.value === '' ? null : e.target.value)}
185
+ value={value}
186
+ inputProps={{ name: 'colormap', id: inputId }}
187
+ style={{ width: '100%' }}
188
+ classes={{ root: classes.selectRoot }}
189
+ >
190
+ <option aria-label="None" value="">None</option>
191
+ {COLORMAP_OPTIONS.map(name => (
192
+ <option key={name} value={name}>
193
+ {name}
194
+ </option>
195
+ ))}
196
+ </Select>
197
+ );
198
+ }
199
+
200
+ function TransparentColorCheckbox({ value, handleChange }) {
201
+ return (
202
+ <Checkbox
203
+ style={{ float: 'left', padding: 0 }}
204
+ color="default"
205
+ onChange={() => {
206
+ if (value) {
207
+ handleChange(null);
208
+ } else {
209
+ handleChange([0, 0, 0]);
210
+ }
211
+ }}
212
+ checked={Boolean(value)}
213
+ />
214
+ );
215
+ }
216
+
217
+ /**
218
+ * Wrapper for the slider that updates opacity.
219
+ * @prop {string} value Currently selected value between 0 and 1.
220
+ * @prop {function} handleChange Callback for every change in opacity.
221
+ */
222
+ function OpacitySlider({ value, handleChange }) {
223
+ return (
224
+ <Slider
225
+ value={value}
226
+ onChange={(e, v) => handleChange(v)}
227
+ valueLabelDisplay="auto"
228
+ getAriaLabel={() => 'opacity slider'}
229
+ min={0}
230
+ max={1}
231
+ step={0.01}
232
+ orientation="horizontal"
233
+ style={{ marginTop: '7px' }}
234
+ />
235
+ );
236
+ }
237
+
238
+ /**
239
+ * Wrapper for the dropdown that chooses the domain type.
240
+ * @prop {string} value Currently selected value (i.e 'Max/Min').
241
+ * @prop {string} inputId Css id.
242
+ * @prop {function} handleChange Callback for every change in domain.
243
+ */
244
+ function SliderDomainSelector({ value, inputId, handleChange }) {
245
+ const classes = useSelectStyles();
246
+ return (
247
+ <Select
248
+ native
249
+ onChange={e => handleChange(e.target.value)}
250
+ value={value}
251
+ inputProps={{ name: 'domain-selector', id: inputId }}
252
+ style={{ width: '100%' }}
253
+ classes={{ root: classes.selectRoot }}
254
+ >
255
+ {DOMAIN_OPTIONS.map(name => (
256
+ <option key={name} value={name}>
257
+ {name}
258
+ </option>
259
+ ))}
260
+ </Select>
261
+ );
262
+ }
263
+
264
+ /**
265
+ * Wrapper for the slider that chooses global selections (z, t etc.).
266
+ * @prop {string} field The dimension this selects for (z, t etc.).
267
+ * @prop {number} value Currently selected index (1, 4, etc.).
268
+ * @prop {function} handleChange Callback for every change in selection.
269
+ * @prop {function} possibleValues All available values for the field.
270
+ */
271
+ function GlobalSelectionSlider({
272
+ field,
273
+ value,
274
+ handleChange,
275
+ possibleValues,
276
+ }) {
277
+ return (
278
+ <StyledSelectionSlider
279
+ value={value}
280
+ // See https://github.com/hms-dbmi/viv/issues/176 for why
281
+ // we have the two handlers.
282
+ onChange={
283
+ (event, newValue) => {
284
+ handleChange({ selection: { [field]: newValue }, event });
285
+ }
286
+ }
287
+ onChangeCommitted={
288
+ (event, newValue) => {
289
+ handleChange({ selection: { [field]: newValue }, event });
290
+ }
291
+ }
292
+ valueLabelDisplay="auto"
293
+ getAriaLabel={() => `${field} slider`}
294
+ marks={possibleValues.map(val => ({ value: val }))}
295
+ min={Number(possibleValues[0])}
296
+ max={Number(possibleValues.slice(-1))}
297
+ orientation="horizontal"
298
+ step={null}
299
+ />
300
+ );
301
+ }
302
+
303
+ /**
304
+ * Wrapper for each of the options to show its name and then its UI component.
305
+ * @prop {string} name Display name for option.
306
+ * @prop {number} opacity Current opacity value.
307
+ * @prop {string} inputId An id for css.
308
+ * @prop {object} children Components to be rendered next to the name (slider, dropdown etc.).
309
+ */
310
+ function LayerOption({ name, inputId, children }) {
311
+ return (
312
+ <Grid container direction="row" alignItems="center" justifyContent="center">
313
+ <Grid item xs={6}>
314
+ <InputLabel htmlFor={inputId}>{name}:</InputLabel>
315
+ </Grid>
316
+ <Grid item xs={6}>
317
+ {children}
318
+ </Grid>
319
+ </Grid>
320
+ );
321
+ }
322
+
323
+ /**
324
+ * Gloabl options for all channels (opacity, colormap, etc.).
325
+ * @prop {string} colormap What colormap is currently selected (None, viridis etc.).
326
+ * @prop {number} opacity Current opacity value.
327
+ * @prop {function} handleColormapChange Callback for when colormap changes.
328
+ * @prop {function} handleOpacityChange Callback for when opacity changes.
329
+ * @prop {object} globalControlLabels All available options for global control (z and t).
330
+ * @prop {function} handleGlobalChannelsSelectionChange Callback for global selection changes.
331
+ * @prop {function} handleDomainChange Callback for domain type changes (full or min/max).
332
+ * @prop {array} channels Current channel object for inferring the current global selection.
333
+ * @prop {array} dimensions Currently available dimensions (channel, z, t etc.).
334
+ * @prop {string} domainType One of Max/Min or Full (soon presets as well).
335
+ * @prop {boolean} disableChannelsIfRgbDetected Whether or not we need colormap controllers if RGB.
336
+ */
337
+ function LayerOptions({
338
+ colormap,
339
+ opacity,
340
+ handleColormapChange,
341
+ handleOpacityChange,
342
+ handleTransparentColorChange,
343
+ globalControlLabels,
344
+ globalLabelValues,
345
+ handleGlobalChannelsSelectionChange,
346
+ handleSliderChange,
347
+ handleDomainChange,
348
+ transparentColor,
349
+ channels,
350
+ domainType,
351
+ disableChannelsIfRgbDetected,
352
+ shouldShowTransparentColor,
353
+ shouldShowDomain,
354
+ shouldShowColormap,
355
+ use3d,
356
+ loader,
357
+ handleMultiPropertyChange,
358
+ resolution,
359
+ disable3d,
360
+ setRasterLayerCallback,
361
+ setAreAllChannelsLoading,
362
+ setViewState,
363
+ spatialHeight,
364
+ spatialWidth,
365
+ modelMatrix,
366
+ }) {
367
+ const { labels, shape } = Array.isArray(loader.data) ? loader.data[0] : loader.data;
368
+ const hasDimensionsAndChannels = labels.length > 0 && channels.length > 0;
369
+ const hasZStack = shape[labels.indexOf('z')] > 1;
370
+ // Only show volume button if we can actually view resolutions.
371
+ const hasViewableResolutions = Boolean(Array.from({
372
+ length: loader.data.length,
373
+ }).filter((_, res) => canLoadResolution(loader.data, res)).length);
374
+ return (
375
+ <Grid container direction="column" style={{ width: '100%' }}>
376
+ {hasZStack
377
+ && !disable3d
378
+ && hasViewableResolutions
379
+ && (
380
+ <VolumeDropdown
381
+ loader={loader}
382
+ handleSliderChange={handleSliderChange}
383
+ handleDomainChange={handleDomainChange}
384
+ channels={channels}
385
+ handleMultiPropertyChange={handleMultiPropertyChange}
386
+ resolution={resolution}
387
+ disable3d={disable3d}
388
+ setRasterLayerCallback={setRasterLayerCallback}
389
+ setAreAllChannelsLoading={setAreAllChannelsLoading}
390
+ setViewState={setViewState}
391
+ spatialHeight={spatialHeight}
392
+ spatialWidth={spatialWidth}
393
+ use3d={use3d}
394
+ modelMatrix={modelMatrix}
395
+ />
396
+ )
397
+ }
398
+ {hasDimensionsAndChannels
399
+ && !use3d
400
+ && globalControlLabels.map(
401
+ field => shape[labels.indexOf(field)] > 1 && (
402
+ <LayerOption name={field} inputId={`${field}-slider`} key={field}>
403
+ <GlobalSelectionSlider
404
+ field={field}
405
+ value={globalLabelValues[field]}
406
+ handleChange={handleGlobalChannelsSelectionChange}
407
+ possibleValues={range(shape[labels.indexOf(field)])}
408
+ />
409
+ </LayerOption>
410
+ ),
411
+ )}
412
+ {!disableChannelsIfRgbDetected ? (
413
+ <>
414
+ {shouldShowColormap && (
415
+ <Grid item>
416
+ <LayerOption name="Colormap" inputId="colormap-select">
417
+ <ColormapSelect
418
+ value={colormap || ''}
419
+ inputId="colormap-select"
420
+ handleChange={handleColormapChange}
421
+ />
422
+ </LayerOption>
423
+ </Grid>
424
+ )}
425
+ {shouldShowDomain && (
426
+ <Grid item>
427
+ <LayerOption name="Domain" inputId="domain-selector">
428
+ <SliderDomainSelector
429
+ value={domainType || DEFAULT_RASTER_DOMAIN_TYPE}
430
+ handleChange={(value) => {
431
+ handleDomainChange(value);
432
+ }}
433
+ />
434
+ </LayerOption>
435
+ </Grid>
436
+ )}
437
+ </>
438
+ ) : null}
439
+ {!use3d && (
440
+ <Grid item>
441
+ <LayerOption name="Opacity" inputId="opacity-slider">
442
+ <OpacitySlider value={opacity} handleChange={handleOpacityChange} />
443
+ </LayerOption>
444
+ </Grid>
445
+ )}
446
+ {shouldShowTransparentColor && !use3d && (
447
+ <Grid item>
448
+ <LayerOption
449
+ name="Zero Transparent"
450
+ inputId="transparent-color-selector"
451
+ >
452
+ <TransparentColorCheckbox
453
+ value={transparentColor}
454
+ handleChange={handleTransparentColorChange}
455
+ />
456
+ </LayerOption>
457
+ </Grid>
458
+ )}
459
+ </Grid>
460
+ );
461
+ }
462
+
463
+ export default LayerOptions;