Skip to content

Commit

Permalink
Add remove methods, relates to #1573
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Dec 15, 2023
1 parent 48b4a15 commit 8a31960
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/app/dataController.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,26 @@ export class DataController {
image.addEventListener('imagechange', this.#getFireEvent(dataId));
}

/**
* Remove a data from the list.
*
* @param {string} dataId The data id.
*/
remove(dataId) {
if (typeof this.#dataList[dataId] !== 'undefined') {
// stop listeners
this.#dataList[dataId].image.removeEventListener(
'imagechange', this.#getFireEvent(dataId));
// fire an image remove event
this.#fireEvent({
type: 'imageremove',
dataid: dataId
});
// remove data from list
delete this.#dataList[dataId];
}
}

/**
* Update the current data.
*
Expand Down
26 changes: 24 additions & 2 deletions src/gui/layerGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,20 @@ export class LayerGroup {
}
}

/**
* Remove all layers for a specific data.
*
* @param {string} dataId The data to remove its layers.
*/
removeLayersByDataId(dataId) {
for (const layer of this.#layers) {
if (typeof layer !== 'undefined' &&
layer.getDataId() === dataId) {
this.removeLayer(layer);
}
}
}

/**
* Remove a layer from this layer group.
* Warning: if current active layer, the index will
Expand All @@ -726,7 +740,11 @@ export class LayerGroup {
if (layer instanceof ViewLayer) {
this.#unbindViewLayer(layer);
if (this.#activeViewLayerIndex === index) {
this.#activeViewLayerIndex = undefined;
if (index - 2 >= 0) {
this.setActiveViewLayer(index - 2);
} else {
this.#activeViewLayerIndex = undefined;
}
}
} else {
// delete layer draws
Expand All @@ -747,7 +765,11 @@ export class LayerGroup {
}
// reset active index
if (this.#activeDrawLayerIndex === index) {
this.#activeDrawLayerIndex = undefined;
if (index - 2 >= 0) {
this.setActiveDrawLayer(index - 2);
} else {
this.#activeDrawLayerIndex = undefined;
}
}
}
// reset in storage
Expand Down
11 changes: 11 additions & 0 deletions src/gui/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ export class Stage {
this.#activeLayerGroupIndex = null;
}

/**
* Remove all layers for a specific data.
*
* @param {string} dataId The data to remove its layers.
*/
removeLayersByDataId(dataId) {
for (const layerGroup of this.#layerGroups) {
layerGroup.removeLayersByDataId(dataId);
}
}

/**
* Remove a layer group from this stage.
*
Expand Down

0 comments on commit 8a31960

Please sign in to comment.