All files / packages/core/src getEnabledElement.ts

64.28% Statements 18/28
64.28% Branches 9/14
83.33% Functions 5/6
66.66% Lines 18/27

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                                                                        3082x       3082x   3082x                                 3541x 67x     3474x   3474x       3474x       3474x       3474x   3474x                                                                   13x   13x   13x 13x   13x 13x       13x    
import getRenderingEngine, {
  getRenderingEngines,
} from './RenderingEngine/getRenderingEngine';
import { IEnabledElement, IStackViewport, IVolumeViewport } from './types';
 
/**
 * A convenience method to find an EnabledElement given a reference to its
 * associated element. Commonly used in code that's handling a custom
 * event emitted by this library.
 *
 * @example
 * Using the renderingEngine to find the enabled element:
 * ```javascript
 * const element = getRenderingEngine(renderingEngineId)
 *    .getViewport(viewportId)
 *    .element
 *
 * const enabledElement = getEnabledElement(element)
 * ```
 *
 * @example
 * Using a cornerstone event's "element"
 * ```javascript
 * // Our "cornerstone events" contain the source element, which is
 * // raised on the viewport's div element
 * const { element } = evt.detail
 * const enabledElement = getEnabledElement(element)
 * ```
 *
 * @param element - a reference to an EnabledElement/Viewport's div element
 * @returns the associated EnabledElement, or undefined if no matching EnabledElement
 * can be found
 */
export default function getEnabledElement(
  element: HTMLDivElement | undefined
): IEnabledElement | undefined {
  Iif (!element) {
    return;
  }
 
  const { viewportUid, renderingEngineUid } = element.dataset;
 
  return getEnabledElementByIds(viewportUid, renderingEngineUid);
}
 
/**
 * Similar to {@link getEnabledElement}, but takes the IDs of the
 * renderingEngine and viewport as parameters to return the associated
 * EnabledElement.
 *
 * @param viewportId - The Id of the viewport
 * @param renderingEngineId - The Id of the rendering engine.
 * @returns The enabled element which is an object that contains the viewport, rendering
 * engine, viewport Id, rendering engine Id, and the Frame of Reference UID.
 */
export function getEnabledElementByIds(
  viewportId: string,
  renderingEngineId: string
): IEnabledElement {
  if (!renderingEngineId || !viewportId) {
    return;
  }
 
  const renderingEngine = getRenderingEngine(renderingEngineId);
 
  Iif (!renderingEngine || renderingEngine.hasBeenDestroyed) {
    return;
  }
 
  const viewport = renderingEngine.getViewport(viewportId) as
    | IStackViewport
    | IVolumeViewport;
 
  Iif (!viewport) {
    return;
  }
 
  const FrameOfReferenceUID = viewport.getFrameOfReferenceUID();
 
  return {
    viewport,
    renderingEngine,
    viewportId,
    renderingEngineId,
    FrameOfReferenceUID,
  };
}
 
/**
 * Retrieves the enabled element by the specified viewport ID. it searches
 * through all the rendering engines to find the viewport with the specified
 *
 * @param viewportId - The ID of the viewport.
 * @returns The enabled element associated with the specified viewport ID.
 */
export function getEnabledElementByViewportId(viewportId: string) {
  const renderingEngines = getRenderingEngines();
 
  for (let i = 0; i < renderingEngines.length; i++) {
    const renderingEngine = renderingEngines[i];
    const viewport = renderingEngine.getViewport(viewportId);
 
    if (viewport) {
      return getEnabledElementByIds(viewportId, renderingEngine.id);
    }
  }
}
 
/**
 * Get all the enabled elements from all the rendering engines
 * @returns An array of enabled elements.
 */
export function getEnabledElements(): IEnabledElement[] {
  const enabledElements = [];
 
  const renderingEngines = getRenderingEngines();
 
  renderingEngines.forEach((renderingEngine) => {
    const viewports = renderingEngine.getViewports();
 
    viewports.forEach(({ element }) => {
      enabledElements.push(getEnabledElement(element));
    });
  });
 
  return enabledElements;
}