All files / tools/src/stateManagement/annotation annotationVisibility.ts

45.45% Statements 15/33
41.17% Branches 7/17
60% Functions 6/10
45.45% Lines 15/33

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 122 123 124 125 126 127 128 129 130 131 132 133                        428x                                   164x 164x 164x 164x         164x                                         1434x   1434x 1434x               164x                       164x                                               164x                           164x 164x   164x                  
import { eventTarget, triggerEvent } from '@cornerstonejs/core';
import { Events } from '../../enums';
import type { AnnotationVisibilityChangeEventDetail } from '../../types/EventTypes';
import {
  isAnnotationSelected,
  deselectAnnotation,
} from './annotationSelection';
import { getAnnotation } from './annotationState';
 
/*
 * It stores all hidden annotation uids.
 */
const globalHiddenAnnotationUIDsSet: Set<string> = new Set();
 
/*
 * Interface (Public API)
 */
 
/**
 * Set the "visible" state of a given annotation instance.
 *
 * @event ANNOTATION_VISIBILITY_CHANGE
 *
 * @param annotationUID - The annotation uid which will have
 * its visible state changed. An event will only be triggered if the visible state
 * of the given annotation instance changed.
 * @param visible - A boolean value indicating if the instance should
 * be visible (true) or not (false)
 */
function setAnnotationVisibility(annotationUID: string, visible = true): void {
  const detail = makeEventDetail();
  Eif (annotationUID) {
    if (visible) {
      show(annotationUID, globalHiddenAnnotationUIDsSet, detail);
    } else E{
      hide(annotationUID, globalHiddenAnnotationUIDsSet, detail);
    }
  }
  publish(detail);
}
 
/**
 * Clears all the hidden annotations.
 *
 */
function showAllAnnotations(): void {
  const detail = makeEventDetail();
  globalHiddenAnnotationUIDsSet.forEach((annotationUID) => {
    show(annotationUID, globalHiddenAnnotationUIDsSet, detail);
  });
  publish(detail);
}
 
/**
 * Given an annotation UID, return true if it is visible, false if hidden and undefined if does not exist.
 * @param annotationUID - The annotation uid to tell if is visible or not.
 * @returns A boolean value or value if does not exist.
 */
function isAnnotationVisible(annotationUID: string): boolean | undefined {
  const annotation = getAnnotation(annotationUID);
 
  Eif (annotation) {
    return !globalHiddenAnnotationUIDsSet.has(annotationUID);
  }
}
 
/*
 * Private Helpers
 */
function makeEventDetail(): AnnotationVisibilityChangeEventDetail {
  return Object.freeze({
    lastVisible: [],
    lastHidden: [],
    hidden: [],
  });
}
 
function show(
  annotationUID: string,
  annotationUIDsSet: Set<string>,
  detail: AnnotationVisibilityChangeEventDetail
): void {
  Iif (annotationUIDsSet.delete(annotationUID)) {
    detail.lastVisible.push(annotationUID);
    const annotation = getAnnotation(annotationUID);
    annotation.isVisible = true;
  }
}
 
function hide(
  annotationUID: string,
  annotationUIDsSet: Set<string>,
  detail: AnnotationVisibilityChangeEventDetail
): void {
  if (!annotationUIDsSet.has(annotationUID)) {
    annotationUIDsSet.add(annotationUID);
    if (isAnnotationSelected(annotationUID)) {
      deselectAnnotation(annotationUID);
    }
    detail.lastHidden.push(annotationUID);
    const annotation = getAnnotation(annotationUID);
    annotation.isVisible = false;
  }
}
 
function publish(detail: AnnotationVisibilityChangeEventDetail) {
  Iif (detail.lastHidden.length > 0 || detail.lastVisible.length > 0) {
    globalHiddenAnnotationUIDsSet.forEach(
      (item) => void detail.hidden.push(item)
    );
    triggerEvent(eventTarget, Events.ANNOTATION_VISIBILITY_CHANGE, detail);
  }
}
 
/**
 * Properly initialize the isVisible state for an annotation based on its UID.
 * @param annotationUID - The UID of the annotation to be checked.
 * @returns The visibility state of the annotation.
 */
function checkAndSetAnnotationVisibility(annotationUID: string): boolean {
  const isVisible = !globalHiddenAnnotationUIDsSet.has(annotationUID);
  setAnnotationVisibility(annotationUID, isVisible);
 
  return isVisible;
}
 
export {
  setAnnotationVisibility,
  showAllAnnotations,
  isAnnotationVisible,
  checkAndSetAnnotationVisibility,
};