All files / Sources/Widgets/Core/AbstractWidget index.js

65.3% Statements 32/49
12.5% Branches 1/8
26.66% Functions 4/15
71.42% Lines 30/42

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                    10x 10x     10x 195x     10x                           10x           10x       10x     10x     10x       10x 10x 10x         10x         10x     3x 9x 9x               10x           10x         1x                         10x   10x 10x   10x         10x         10x 10x   10x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkInteractorObserver from 'vtk.js/Sources/Rendering/Core/InteractorObserver';
import vtkProp from 'vtk.js/Sources/Rendering/Core/Prop';
 
import { RenderingTypes } from 'vtk.js/Sources/Widgets/Core/WidgetManager/Constants';
import { WIDGET_PRIORITY } from 'vtk.js/Sources/Widgets/Core/AbstractWidget/Constants';
 
// ----------------------------------------------------------------------------
 
function vtkAbstractWidget(publicAPI, model) {
  model.classHierarchy.push('vtkAbstractWidget');
  model.actorToRepresentationMap = new WeakMap();
 
  // --------------------------------------------------------------------------
  publicAPI.getBounds = model.widgetState.getBounds;
  publicAPI.getNestedProps = () => model.representations;
  // --------------------------------------------------------------------------
 
  publicAPI.activateHandle = ({ selectedState, representation }) => {
    model.widgetState.activateOnly(selectedState);
    model.activeState = selectedState;
    if (selectedState && selectedState.updateManipulator) {
      selectedState.updateManipulator();
    }
    publicAPI.invokeActivateHandle({ selectedState, representation });
    if (publicAPI.updateCursor) {
      publicAPI.updateCursor();
    }
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.deactivateAllHandles = () => {
    model.widgetState.deactivate();
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.hasActor = (actor) => model.actorToRepresentationMap.has(actor);
 
  // --------------------------------------------------------------------------
 
  publicAPI.grabFocus = () => {
    model.hasFocus = true;
  };
  publicAPI.loseFocus = () => {
    model.hasFocus = false;
  };
  publicAPI.hasFocus = () => model.hasFocus;
 
  // --------------------------------------------------------------------------
 
  publicAPI.placeWidget = (bounds) => model.widgetState.placeWidget(bounds);
  publicAPI.getPlaceFactor = () => model.widgetState.getPlaceFactor();
  publicAPI.setPlaceFactor = (factor) =>
    model.widgetState.setPlaceFactor(factor);
 
  // --------------------------------------------------------------------------
 
  publicAPI.getRepresentationFromActor = (actor) =>
    model.actorToRepresentationMap.get(actor);
 
  // --------------------------------------------------------------------------
 
  publicAPI.updateRepresentationForRender = (
    renderingType = RenderingTypes.FRONT_BUFFER
  ) => {
    for (let i = 0; i < model.representations.length; i++) {
      const representation = model.representations[i];
      representation.updateActorVisibility(
        renderingType,
        model.contextVisibility,
        model.handleVisibility
      );
    }
  };
 
  publicAPI.getViewWidgets = () => model._factory.getViewWidgets();
 
  // --------------------------------------------------------------------------
  // Initialization calls
  // --------------------------------------------------------------------------
 
  publicAPI.setPriority(WIDGET_PRIORITY);
}
 
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  contextVisibility: true,
  handleVisibility: true,
  hasFocus: false,
};
 
/**
 * @param {*} publicAPI public methods to populate
 * @param {*} model internal values to populate
 * @param {object} initialValues Contains at least
 *   {viewType, _renderer, _camera, _openGLRenderWindow, _factory}
 */
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkProp.extend(publicAPI, model, initialValues);
  vtkInteractorObserver.extend(publicAPI, model, initialValues);
 
  macro.setGet(publicAPI, model, [
    'contextVisibility',
    'handleVisibility',
    '_widgetManager',
  ]);
  macro.get(publicAPI, model, [
    'representations',
    'widgetState',
    'activeState', // stores the last activated sub state(handle)
  ]);
  macro.moveToProtected(publicAPI, model, ['widgetManager']);
  macro.event(publicAPI, model, 'ActivateHandle');
 
  vtkAbstractWidget(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkAbstractWidget');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };