-
Notifications
You must be signed in to change notification settings - Fork 588
/
application.js
2066 lines (1916 loc) · 55.9 KB
/
application.js
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {viewEventNames} from '../image/view';
import {ViewFactory} from '../image/viewFactory';
import {
getMatrixFromName,
getOrientationStringLPS,
Orientation
} from '../math/orientation';
import {Point3D} from '../math/point';
import {Stage} from '../gui/stage';
import {Style} from '../gui/style';
import {
getViewOrientation,
getLayerDetailsFromLayerDivId
} from '../gui/layerGroup';
import {ListenerHandler} from '../utils/listen';
import {State} from '../io/state';
import {logger} from '../utils/logger';
import {getUriQuery, decodeQuery} from '../utils/uri';
import {UndoStack} from '../tools/undo';
import {ToolboxController} from './toolboxController';
import {LoadController} from './loadController';
import {DataController} from './dataController';
import {OverlayData} from '../gui/overlayData';
import {toolList, defaultToolList, toolOptions} from '../tools';
import {binderList} from '../gui/stage';
import {WindowLevel} from '../image/windowLevel';
// doc imports
/* eslint-disable no-unused-vars */
import {LayerGroup} from '../gui/layerGroup';
import {ViewLayer} from '../gui/viewLayer';
import {DrawLayer} from '../gui/drawLayer';
import {Image} from '../image/image';
import {Matrix33} from '../math/matrix';
import {DataElement} from '../dicom/dataElement';
import {Scalar3D} from '../math/scalar';
/* eslint-enable no-unused-vars */
/**
* View configuration: mainly defines the ´divId´
* of the associated HTML div.
*/
export class ViewConfig {
/**
* Associated HTML div id.
*
* @type {string}
*/
divId;
/**
* Optional orientation of the data; 'axial', 'coronal' or 'sagittal'.
* If undefined, will use the data aquisition plane.
*
* @type {string|undefined}
*/
orientation;
/**
* Optional view colour map name.
*
* @type {string|undefined}
*/
colourMap;
/**
* Optional layer opacity; in [0, 1] range.
*
* @type {number|undefined}
*/
opacity;
/**
* Optional layer window level preset name.
* If present, the preset name will be used and
* the window centre and width ignored.
*
* @type {string|undefined}
*/
wlPresetName;
/**
* Optional layer window center.
*
* @type {number|undefined}
*/
windowCenter;
/**
* Optional layer window width.
*
* @type {number|undefined}
*/
windowWidth;
/**
* @param {string} divId The associated HTML div id.
*/
constructor(divId) {
this.divId = divId;
}
}
/**
* Tool configuration.
*/
export class ToolConfig {
/**
* Optional tool options.
* For Draw: list of shape names.
* For Filter: list of filter names.
*
* @type {string[]|undefined}
*/
options;
/**
* @param {string[]} [options] Optional tool options.
*/
constructor(options) {
this.options = options;
}
}
/**
* Application options.
*/
export class AppOptions {
/**
* DataId indexed object containing the data view configurations.
*
* @type {Object<string, ViewConfig[]>|undefined}
*/
dataViewConfigs;
/**
* Tool name indexed object containing individual tool configurations.
*
* @type {Object<string, ToolConfig>|undefined}
*/
tools;
/**
* Optional array of layerGroup binder names.
*
* @type {string[]|undefined}
*/
binders;
/**
* Optional boolean flag to trigger the first data render
* after the first loaded data or not. Defaults to true.
*
* @type {boolean|undefined}
*/
viewOnFirstLoadItem;
/**
* Optional default chraracterset string used for DICOM parsing if
* not passed in DICOM file.
*
* Valid values: {@link https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings}.
*
* @type {string|undefined}
*/
defaultCharacterSet;
/**
* Optional overlay config.
*
* @type {object|undefined}
*/
overlayConfig;
/**
* DOM root document.
*
* @type {DocumentFragment}
*/
rootDocument;
/**
* @param {Object<string, ViewConfig[]>} [dataViewConfigs] Optional dataId
* indexed object containing the data view configurations.
*/
constructor(dataViewConfigs) {
this.dataViewConfigs = dataViewConfigs;
}
}
/**
* List of ViewConfigs indexed by dataIds.
*
* @typedef {Object<string, ViewConfig[]>} DataViewConfigs
*/
/**
* Main application class.
*
* @example
* // create the dwv app
* const app = new dwv.App();
* // initialise
* const viewConfig0 = new dwv.ViewConfig('layerGroup0');
* const viewConfigs = {'*': [viewConfig0]};
* const options = new dwv.AppOptions(viewConfigs);
* app.init(options);
* // load dicom data
* app.loadURLs([
* 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm'
* ]);
*/
export class App {
/**
* App options.
*
* @type {AppOptions}
*/
#options = null;
/**
* Data controller.
*
* @type {DataController}
*/
#dataController = null;
/**
* Toolbox controller.
*
* @type {ToolboxController}
*/
#toolboxController = null;
/**
* Load controller.
*
* @type {LoadController}
*/
#loadController = null;
/**
* Stage.
*
* @type {Stage}
*/
#stage = null;
/**
* Undo stack.
*
* @type {UndoStack}
*/
#undoStack = null;
/**
* Style.
*
* @type {Style}
*/
#style = new Style();
// overlay datas
#overlayDatas = {};
/**
* Listener handler.
*
* @type {ListenerHandler}
*/
#listenerHandler = new ListenerHandler();
/**
* Get the image.
*
* @param {string} dataId The data id.
* @returns {Image|undefined} The associated image.
*/
getImage(dataId) {
let res;
if (typeof this.#dataController.get(dataId) !== 'undefined') {
res = this.#dataController.get(dataId).image;
}
return res;
}
/**
* Set the image at the given id.
*
* @param {string} dataId The data id.
* @param {Image} img The associated image.
*/
setImage(dataId, img) {
this.#dataController.setImage(dataId, img);
}
/**
* Add a new image.
*
* @param {Image} image The new image.
* @param {object} meta The image meta.
* @param {string} source The source of the new image,
* will be passed with load events.
* @returns {string} The new image data id.
*/
addNewImage(image, meta, source) {
const dataId = this.#dataController.getNextDataId();
// load start event
this.#fireEvent({
type: 'loadstart',
loadtype: 'image',
source: source,
dataid: dataId
});
// add image to data controller
this.#dataController.addNew(dataId, image, meta);
// load item event
this.#fireEvent({
type: 'loaditem',
loadtype: 'image',
data: meta,
source: source,
dataid: dataId,
isfirstitem: true
});
// optional render
if (this.#options.viewOnFirstLoadItem) {
this.render(dataId);
}
// load events
this.#fireEvent({
type: 'load',
loadtype: 'image',
source: source,
dataid: dataId
});
this.#fireEvent({
type: 'loadend',
loadtype: 'image',
source: source,
dataid: dataId
});
return dataId;
}
/**
* Get the meta data.
*
* @param {string} dataId The data id.
* @returns {Object<string, DataElement>|undefined} The list of meta data.
*/
getMetaData(dataId) {
let res;
if (typeof this.#dataController.get(dataId) !== 'undefined') {
res = this.#dataController.get(dataId).meta;
}
return res;
}
/**
* Get the list of ids in the data storage.
*
* @returns {string[]} The list of data ids.
*/
getDataIds() {
return this.#dataController.getDataIds();
}
/**
* Get the list of dataIds that contain the input UIDs.
*
* @param {string[]} uids A list of UIDs.
* @returns {string[]} The list of dataIds that contain the UIDs.
*/
getDataIdsFromSopUids(uids) {
return this.#dataController.getDataIdsFromSopUids(uids);
}
/**
* Can the data (of the active view of the active layer) be scrolled?
*
* @returns {boolean} True if the data has a third dimension greater than one.
* @deprecated Please use the ViewController equivalent directly instead.
*/
canScroll() {
const viewLayer = this.#stage.getActiveLayerGroup().getActiveViewLayer();
const controller = viewLayer.getViewController();
return controller.canScroll();
}
/**
* Can window and level be applied to the data
* (of the active view of the active layer)?
*
* @returns {boolean} True if the data is monochrome.
* @deprecated Please use the ViewController equivalent directly instead.
*/
canWindowLevel() {
const viewLayer = this.#stage.getActiveLayerGroup().getActiveViewLayer();
const controller = viewLayer.getViewController();
return controller.canWindowLevel();
}
/**
* Get the active layer group scale on top of the base scale.
*
* @returns {Scalar3D} The scale as {x,y,z}.
*/
getAddedScale() {
return this.#stage.getActiveLayerGroup().getAddedScale();
}
/**
* Get the base scale of the active layer group.
*
* @returns {Scalar3D} The scale as {x,y,z}.
*/
getBaseScale() {
return this.#stage.getActiveLayerGroup().getBaseScale();
}
/**
* Get the layer offset of the active layer group.
*
* @returns {Scalar3D} The offset as {x,y,z}.
*/
getOffset() {
return this.#stage.getActiveLayerGroup().getOffset();
}
/**
* Get the toolbox controller.
*
* @returns {ToolboxController} The controller.
*/
getToolboxController() {
return this.#toolboxController;
}
/**
* Get the active layer group.
* The layer is available after the first loaded item.
*
* @returns {LayerGroup|undefined} The layer group.
*/
getActiveLayerGroup() {
return this.#stage.getActiveLayerGroup();
}
/**
* Set the active layer group.
*
* @param {number} index The layer group index.
*/
setActiveLayerGroup(index) {
this.#stage.setActiveLayerGroup(index);
}
/**
* Get the view layers associated to a data id.
* The layer are available after the first loaded item.
*
* @param {string} dataId The data id.
* @returns {ViewLayer[]} The layers.
*/
getViewLayersByDataId(dataId) {
return this.#stage.getViewLayersByDataId(dataId);
}
/**
* Get the draw layers associated to a data id.
* The layer are available after the first loaded item.
*
* @param {string} dataId The data id.
* @returns {DrawLayer[]} The layers.
*/
getDrawLayersByDataId(dataId) {
return this.#stage.getDrawLayersByDataId(dataId);
}
/**
* Get a layer group by div id.
* The layer is available after the first loaded item.
*
* @param {string} divId The div id.
* @returns {LayerGroup} The layer group.
*/
getLayerGroupByDivId(divId) {
return this.#stage.getLayerGroupByDivId(divId);
}
/**
* Get the number of layer groups.
*
* @returns {number} The number of groups.
*/
getNumberOfLayerGroups() {
return this.#stage.getNumberOfLayerGroups();
}
/**
* Get the app style.
*
* @returns {object} The app style.
*/
getStyle() {
return this.#style;
}
/**
* Add a command to the undo stack.
*
* @param {object} cmd The command to add.
* @fires UndoStack#undoadd
* @function
*/
addToUndoStack = (cmd) => {
if (this.#undoStack !== null) {
this.#undoStack.add(cmd);
}
};
/**
* Initialise the application.
*
* @param {AppOptions} opt The application options.
* @example
* // create the dwv app
* const app = new dwv.App();
* // initialise
* const viewConfig0 = new dwv.ViewConfig('layerGroup0');
* const viewConfigs = {'*': [viewConfig0]};
* const options = new dwv.AppOptions(viewConfigs);
* options.viewOnFirstLoadItem = false;
* app.init(options);
* // render button
* const button = document.createElement('button');
* button.id = 'render';
* button.disabled = true;
* button.appendChild(document.createTextNode('render'));
* document.body.appendChild(button);
* app.addEventListener('load', function () {
* const button = document.getElementById('render');
* button.disabled = false;
* button.onclick = function () {
* // render data #0
* app.render(0);
* };
* });
* // load dicom data
* app.loadURLs([
* 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm'
* ]);
*/
init(opt) {
// store
this.#options = opt;
// defaults
if (typeof this.#options.viewOnFirstLoadItem === 'undefined') {
this.#options.viewOnFirstLoadItem = true;
}
if (typeof this.#options.dataViewConfigs === 'undefined') {
this.#options.dataViewConfigs = {};
}
if (typeof this.#options.rootDocument === 'undefined') {
this.#options.rootDocument = document;
}
// undo stack
this.#undoStack = new UndoStack();
this.#undoStack.addEventListener('undoadd', this.#fireEvent);
this.#undoStack.addEventListener('undo', this.#fireEvent);
this.#undoStack.addEventListener('redo', this.#fireEvent);
// tools
if (typeof this.#options.tools !== 'undefined') {
// setup the tool list
const appToolList = {};
const keys = Object.keys(this.#options.tools);
for (let t = 0; t < keys.length; ++t) {
const toolName = keys[t];
// find the tool in the default tool list
let toolConstructor = defaultToolList[toolName];
// or use external one
if (typeof toolConstructor === 'undefined') {
toolConstructor = toolList[toolName];
}
if (typeof toolConstructor !== 'undefined') {
// create tool instance
appToolList[toolName] = new toolConstructor(this);
// register listeners
if (typeof appToolList[toolName].addEventListener !== 'undefined') {
const names = appToolList[toolName].getEventNames();
for (let j = 0; j < names.length; ++j) {
appToolList[toolName].addEventListener(names[j], this.#fireEvent);
}
}
// tool options
const toolParams = this.#options.tools[toolName];
if (typeof toolParams.options !== 'undefined' &&
toolParams.options.length !== 0) {
let type = 'raw';
if (typeof appToolList[toolName].getOptionsType !== 'undefined') {
type = appToolList[toolName].getOptionsType();
}
let appToolOptions;
if (type === 'instance' || type === 'factory') {
appToolOptions = {};
for (let i = 0; i < toolParams.options.length; ++i) {
const optionName = toolParams.options[i];
let optionClassName = optionName;
if (type === 'factory') {
optionClassName += 'Factory';
}
const toolNamespace = toolName.charAt(0).toLowerCase() +
toolName.slice(1);
if (typeof toolOptions[toolNamespace][optionClassName] !==
'undefined') {
appToolOptions[optionName] =
toolOptions[toolNamespace][optionClassName];
} else {
logger.warn('Could not find option class for: ' +
optionName);
}
}
} else {
appToolOptions = toolParams.options;
}
appToolList[toolName].setOptions(appToolOptions);
}
} else {
logger.warn('Could not initialise unknown tool: ' + toolName);
}
}
// add tools to the controller
this.#toolboxController = new ToolboxController(appToolList);
}
// create load controller
this.#loadController =
new LoadController(this.#options.defaultCharacterSet);
this.#loadController.onloadstart = this.#onloadstart;
this.#loadController.onprogress = this.#onloadprogress;
this.#loadController.onloaditem = this.#onloaditem;
this.#loadController.onload = this.#onload;
this.#loadController.onloadend = this.#onloadend;
this.#loadController.onerror = this.#onloaderror;
this.#loadController.onabort = this.#onloadabort;
// create data controller
this.#dataController = new DataController();
// create stage
this.#stage = new Stage();
if (typeof this.#options.binders !== 'undefined') {
this.#stage.setBinders(this.#options.binders);
}
}
/**
* Reset the application.
*/
reset() {
// clear objects
this.#dataController.reset();
this.#stage.empty();
this.#overlayDatas = {};
// reset undo/redo
if (this.#undoStack) {
this.#undoStack = new UndoStack();
this.#undoStack.addEventListener('undoadd', this.#fireEvent);
this.#undoStack.addEventListener('undo', this.#fireEvent);
this.#undoStack.addEventListener('redo', this.#fireEvent);
}
}
/**
* Reset the layout of the application.
*/
resetLayout() {
this.#stage.reset();
this.#stage.draw();
}
/**
* Add an event listener to this class.
*
* @param {string} type The event type.
* @param {Function} callback The function associated with the provided
* event type, will be called with the fired event.
*/
addEventListener(type, callback) {
this.#listenerHandler.add(type, callback);
}
/**
* Remove an event listener from this class.
*
* @param {string} type The event type.
* @param {Function} callback The function associated with the provided
* event type.
*/
removeEventListener(type, callback) {
this.#listenerHandler.remove(type, callback);
}
// load API [begin] -------------------------------------------------------
/**
* Load a list of files. Can be image files or a state file.
*
* @param {File[]} files The list of files to load.
* @fires App#loadstart
* @fires App#loadprogress
* @fires App#loaditem
* @fires App#loadend
* @fires App#error
* @fires App#abort
* @function
*/
loadFiles = (files) => {
// Get new data id
const dataId = this.#dataController.getNextDataId();
if (files.length === 0) {
logger.warn('Ignoring empty input file list.');
return;
}
this.#loadController.loadFiles(files, dataId);
};
/**
* Load a list of URLs. Can be image files or a state file.
*
* @param {string[]} urls The list of urls to load.
* @param {object} [options] The options object, can contain:
* - requestHeaders: an array of {name, value} to use as request headers,
* - withCredentials: boolean xhr.withCredentials flag to pass to the request,
* - batchSize: the size of the request url batch.
* @fires App#loadstart
* @fires App#loadprogress
* @fires App#loaditem
* @fires App#loadend
* @fires App#error
* @fires App#abort
* @function
*/
loadURLs = (urls, options) => {
// Get new data id
const dataId = this.#dataController.getNextDataId();
if (urls.length === 0) {
logger.warn('Ignoring empty input url list.');
return;
}
this.#loadController.loadURLs(urls, dataId, options);
};
/**
* Load from an input uri.
*
* @param {string} uri The input uri, for example: 'window.location.href'.
* @param {object} [options] Optional url request options.
* @function
*/
loadFromUri = (uri, options) => {
const query = getUriQuery(uri);
// load end callback: loads the state.
const onLoadEnd = (/*event*/) => {
this.removeEventListener('loadend', onLoadEnd);
this.loadURLs([query.state]);
};
// check query
if (query && typeof query.input !== 'undefined') {
// optional display state
if (typeof query.state !== 'undefined') {
// queue after main data load
this.addEventListener('loadend', onLoadEnd);
}
// load base image
decodeQuery(query, this.loadURLs, options);
}
// no else to allow for empty uris
};
/**
* Load a list of ArrayBuffers.
*
* @param {Array} data The list of ArrayBuffers to load
* in the form of [{name: "", filename: "", data: data}].
* @fires App#loadstart
* @fires App#loadprogress
* @fires App#loaditem
* @fires App#loadend
* @fires App#error
* @fires App#abort
* @function
*/
loadImageObject = (data) => {
// Get new data id
const dataId = this.#dataController.getNextDataId();
this.#loadController.loadImageObject(data, dataId);
};
/**
* Abort all the current loads.
*/
abortAllLoads() {
const ids = this.#loadController.getLoadingDataIds();
for (const id of ids) {
this.abortLoad(id);
}
}
/**
* Abort an individual data load.
*
* @param {string} dataId The data to stop loading.
*/
abortLoad(dataId) {
// abort load
this.#loadController.abort(dataId);
// remove data
this.#dataController.remove(dataId);
// clean up stage
this.#stage.removeLayersByDataId(dataId);
}
// load API [end] ---------------------------------------------------------
/**
* Fit the display to the data of each layer group.
* To be called once the image is loaded.
*/
fitToContainer() {
this.#stage.fitToContainer();
}
/**
* Init the Window/Level display
* (of the active layer of the active layer group).
*
* @deprecated Please set the opacity of the desired view layer directly.
*/
initWLDisplay() {
const viewLayer = this.#stage.getActiveLayerGroup().getActiveViewLayer();
const controller = viewLayer.getViewController();
controller.initialise();
}
/**
* Set the imageSmoothing flag value. Default is false.
*
* @param {boolean} flag True to enable smoothing.
*/
setImageSmoothing(flag) {
this.#stage.setImageSmoothing(flag);
this.#stage.draw();
}
/**
* Get the layer group configuration from a data id.
*
* @param {string} dataId The data id.
* @param {boolean} [excludeStarConfig] Exclude the star config
* (default to false).
* @returns {ViewConfig[]} The list of associated configs.
*/
getViewConfigs(dataId, excludeStarConfig) {
// check options
if (this.#options.dataViewConfigs === null ||
typeof this.#options.dataViewConfigs === 'undefined') {
throw new Error('No available data view configuration');
}
let configs = [];
if (typeof this.#options.dataViewConfigs[dataId] !== 'undefined') {
configs = this.#options.dataViewConfigs[dataId];
} else if (!excludeStarConfig &&
typeof this.#options.dataViewConfigs['*'] !== 'undefined') {
configs = this.#options.dataViewConfigs['*'];
}
return configs;
}
/**
* Get the layer group configuration for a data id and group
* div id.
*
* @param {string} dataId The data id.
* @param {string} groupDivId The layer group div id.
* @param {boolean} [excludeStarConfig] Exclude the star config
* (default to false).
* @returns {ViewConfig|undefined} The associated config.
*/
getViewConfig(dataId, groupDivId, excludeStarConfig) {
const configs = this.getViewConfigs(dataId, excludeStarConfig);
return configs.find(function (item) {
return item.divId === groupDivId;
});
}
/**
* Get the data view config.
* Carefull, returns a reference, do not modify without resetting.
*
* @returns {Object<string, ViewConfig[]>} The configuration list.
*/
getDataViewConfigs() {
return this.#options.dataViewConfigs;
}
/**
* Set the data view configuration.
* Resets the stage and recreates all the views.
*
* @param {Object<string, ViewConfig[]>} configs The configuration list.
*/
setDataViewConfigs(configs) {
// clean up
this.#stage.empty();
// set new
this.#options.dataViewConfigs = configs;
// create layer groups
this.#createLayerGroups(configs);
}
/**
* Add a data view config.
*
* @param {string} dataId The data id.
* @param {ViewConfig} config The view configuration.
*/
addDataViewConfig(dataId, config) {
// add to list
const configs = this.#options.dataViewConfigs;
if (typeof configs[dataId] === 'undefined') {
configs[dataId] = [];
}
const equalDivId = function (item) {
return item.divId === config.divId;
};
const itemIndex = configs[dataId].findIndex(equalDivId);
if (itemIndex === -1) {
this.#options.dataViewConfigs[dataId].push(config);
} else {
throw new Error('Duplicate view config for data ' + dataId +
' and div ' + config.divId);
}
// add layer group if not done
if (typeof this.#stage.getLayerGroupByDivId(config.divId) === 'undefined') {
this.#createLayerGroup(config);
}
// render (will create layers)
this.render(dataId, [config]);
}
/**
* Remove a data view config.
*
* @param {string} dataId The data id.
* @param {string} divId The div id.
*/
removeDataViewConfig(dataId, divId) {
// remove from list
const configs = this.#options.dataViewConfigs;
if (typeof configs[dataId] === 'undefined') {
// no config for dataId
return;
}
const equalDivId = function (item) {
return item.divId === divId;
};
const itemIndex = configs[dataId].findIndex(equalDivId);
if (itemIndex === -1) {
// no config for divId
return;
}
configs[dataId].splice(itemIndex, 1);
if (configs[dataId].length === 0) {
delete configs[dataId];
}
// data is loaded, remove view
if (typeof this.#dataController.get(dataId) !== 'undefined') {
const lg = this.#stage.getLayerGroupByDivId(divId);
if (typeof lg !== 'undefined') {
const vls = lg.getViewLayersByDataId(dataId);
if (vls.length === 1) {
lg.removeLayer(vls[0]);
} else {
throw new Error('Expected one view layer, got ' + vls.length);
}
const dls = lg.getDrawLayersByDataId(dataId);
if (dls.length === 1) {
lg.removeLayer(dls[0]);
} else {
throw new Error('Expected one draw layer, got ' + dls.length);
}
if (lg.getNumberOfLayers() === 0) {
this.#stage.removeLayerGroup(lg);
}
}
}
}