All files / packages/tools/src/stateManagement/annotation AnnotationGroup.ts

0% Statements 0/35
0% Branches 0/24
0% Functions 0/12
0% Lines 0/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                 
import { eventTarget, triggerEvent } from '@cornerstonejs/core';
import Events from '../../enums/Events';
import { getAnnotation } from './annotationState';
 
export type BaseEventDetail = {
  viewportId: string;
  renderingEngineId: string;
};
 
/**
 * An annotation group
 */
 
export default class AnnotationGroup {
  private annotationUIDs = new Set<string>();
  private _isVisible = true;
 
  public visibleFilter: (uid: string) => boolean;
 
  constructor() {
    this.visibleFilter = this.unboundVisibleFilter.bind(this);
  }
 
  /**
   * Returns true if other groups are free to hide this annotation.
   * That is, if the annotation is not a member or is hidden.
   */
  protected unboundVisibleFilter(uid: string): boolean {
    return !this._isVisible || !this.annotationUIDs.has(uid);
  }
 
  public has(uid: string): boolean {
    return this.annotationUIDs.has(uid);
  }
  /**
   * Sets whether annotations belonging to this group are visible or not.
   * If there are multiple groups, then the set visible false should be called
   * before before re-enabling the other groups with setVisible true.
   */
  public setVisible(
    isVisible = true,
    baseEvent: BaseEventDetail,
    filter?: (annotationUID: string) => boolean
  ) {
    if (this._isVisible === isVisible) {
      return;
    }
    this._isVisible = isVisible;
    this.annotationUIDs.forEach((uid) => {
      const annotation = getAnnotation(uid);
      if (!annotation) {
        this.annotationUIDs.delete(uid);
        return;
      }
      if (annotation.isVisible === isVisible) {
        return;
      }
      if (!isVisible && filter?.(uid) === false) {
        return;
      }
      annotation.isVisible = isVisible;
      const eventDetail = {
        ...baseEvent,
        annotation,
      };
      triggerEvent(eventTarget, Events.ANNOTATION_MODIFIED, eventDetail);
    });
  }
 
  public get isVisible() {
    return this._isVisible;
  }
 
  /** Finds the nearby/next annotation in the given direction */
  public findNearby(uid: string, direction: 1) {
    const uids = [...this.annotationUIDs];
    if (uids.length === 0) {
      return null;
    }
    if (!uid) {
      return uids[direction === 1 ? 0 : uids.length - 1];
    }
    const index = uids.indexOf(uid);
    if (
      index === -1 ||
      index + direction < 0 ||
      index + direction >= uids.length
    ) {
      return null;
    }
    return uids[index + direction];
  }
 
  /**
   * Adds the annotation to the group
   * Does NOT change the visibility status of the annotation.
   */
  public add(...annotationUIDs: string[]) {
    annotationUIDs.forEach((annotationUID) =>
      this.annotationUIDs.add(annotationUID)
    );
  }
 
  /**
   * Removes the annotation from the group.
   * Does not affect the visibility status of the annotation.
   */
  public remove(...annotationUIDs: string[]) {
    annotationUIDs.forEach((annotationUID) =>
      this.annotationUIDs.delete(annotationUID)
    );
  }
 
  /**
   * Removes everything from the group.
   */
  public clear() {
    this.annotationUIDs.clear();
  }
}