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

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