All files / packages/core/src getEnabledElement.ts

54.16% Statements 13/24
75% Branches 9/12
40% Functions 2/5
52.38% Lines 11/21

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                                                                        1733x       1733x   1733x                                 1940x 36x     1904x   1904x       1904x   1904x       1904x   1904x                                                        
import getRenderingEngine, {
  getRenderingEngines,
} from './RenderingEngine/getRenderingEngine';
import { IEnabledElement } 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);
 
  Iif (!viewport) {
    return;
  }
 
  const FrameOfReferenceUID = viewport.getFrameOfReferenceUID();
 
  return {
    viewport,
    renderingEngine,
    viewportId,
    renderingEngineId,
    FrameOfReferenceUID,
  };
}
 
/**
 * 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;
}