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

59.45% Statements 22/37
57.14% Branches 16/28
61.53% Functions 8/13
59.45% Lines 22/37

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157                          1x                                   107x 107x 101x 101x         107x                                         335x   335x 335x                   107x 107x 107x 107x             107x               107x                       101x                                       107x                 107x 107x 7x         100x               201x                  
import { eventTarget, triggerEvent } from '@cornerstonejs/core';
import { getAnnotation } from './annotationState';
import { Events } from '../../enums';
import { Annotation } from '../../types';
import { AnnotationVisibilityChangeEventDetail } from '../../types/EventTypes';
import {
  isAnnotationSelected,
  deselectAnnotation,
} from './annotationSelection';
 
/*
 * 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();
  if (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);
  }
}
/**
 * It decorates given annotation with isVisible property.
 * It properly initializes the isVisible on annotation(the property will be create if does not exist yet)
 *
 * @param annotation - The annotation object to be checked.
 */
function checkAndDefineIsVisibleProperty(annotation: Annotation): void {
  Eif (annotation) {
    const isVisible = annotation.isVisible ?? true;
    Eif (shouldDefineIsVisibleProperty(annotation)) {
      Object.defineProperty(annotation, 'isVisible', {
        configurable: false,
        enumerable: true,
        set: setIsVisible,
        get: getIsVisible,
      });
    }
    setAnnotationVisibility(annotation.annotationUID, isVisible);
  }
}
 
/*
 * 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);
  }
}
 
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);
  }
}
 
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);
  }
}
 
function shouldDefineIsVisibleProperty(annotation: Annotation): boolean {
  const descriptor = Object.getOwnPropertyDescriptor(annotation, 'isVisible');
  if (descriptor) {
    return (
      descriptor.configurable &&
      (descriptor.set !== setIsVisible || descriptor.get !== getIsVisible)
    );
  }
  return Object.isExtensible(annotation);
}
 
function setIsVisible(hidden: boolean) {
  setAnnotationVisibility((this as Annotation).annotationUID, hidden);
}
 
function getIsVisible() {
  return isAnnotationVisible((this as Annotation).annotationUID);
}
 
export {
  setAnnotationVisibility,
  showAllAnnotations,
  isAnnotationVisible,
  checkAndDefineIsVisibleProperty,
};