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

71.57% Statements 68/95
78.94% Branches 15/19
52.17% Functions 12/23
78.82% Lines 67/85

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209                  23x     23x       23x           15x 10x         10x 10x 10x   10x 10x     10x                 10x           10x 10x 10x       10x 10x     25x                 10x 25x 25x 48x       10x   10x   64x 64x 64x         64x 152x     152x 105x   152x 105x       64x 192x 192x 58x 134x 60x             10x 3x     10x 10x 10x 10x   5x       23x   23x             23x             23x             23x             23x             23x                     23x 23x 23x           23x 23x   23x 21x 386x               23x 23x 23x 23x 23x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkAbstractWidget from 'vtk.js/Sources/Widgets/Core/AbstractWidget';
import { extractRenderingComponents } from 'vtk.js/Sources/Widgets/Core/WidgetManager';
 
function NoOp() {}
 
// ----------------------------------------------------------------------------
 
function vtkAbstractWidgetFactory(publicAPI, model) {
  model.classHierarchy.push('vtkAbstractWidgetFactory');
 
  // DO NOT share on the model ------------------------------------------------
  const viewToWidget = {};
  // DO NOT share on the model ------------------------------------------------
 
  // Can be called with just ViewId after the widget has been registered
  publicAPI.getWidgetForView = ({
    viewId,
    renderer,
    viewType,
    initialValues,
  }) => {
    if (!viewToWidget[viewId]) {
      Iif (!renderer) {
        return null;
      }
 
      const { interactor, apiSpecificRenderWindow, camera } =
        extractRenderingComponents(renderer);
      const widgetModel = {};
      const widgetPublicAPI = {};
 
      macro.obj(widgetPublicAPI, widgetModel);
      Object.assign(widgetPublicAPI, {
        onWidgetChange: publicAPI.onWidgetChange,
      });
      Object.assign(widgetModel, {
        widgetState: model.widgetState,
        manipulator: model.manipulator,
        viewType,
        renderer,
        camera,
        apiSpecificRenderWindow,
        factory: publicAPI,
      });
      macro.moveToProtected(widgetPublicAPI, widgetModel, [
        'renderer',
        'camera',
        'apiSpecificRenderWindow',
        'factory',
      ]);
      macro.get(widgetPublicAPI, widgetModel, ['viewType']);
      macro.safeArrays(widgetModel);
      vtkAbstractWidget.extend(widgetPublicAPI, widgetModel, initialValues);
 
      // Create representations for that view
      /* eslint-disable no-shadow */
      const widgetInitialValues = initialValues; // Avoid shadowing
      widgetModel.representations = publicAPI
        .getRepresentationsForViewType(viewType)
        .map(({ builder, labels, initialValues }) =>
          builder.newInstance({
            _parentProp: widgetPublicAPI,
            labels,
            ...initialValues,
            ...widgetInitialValues,
          })
        );
      /* eslint-enable no-shadow */
 
      widgetModel.representations.forEach((r) => {
        r.setInputData(widgetModel.widgetState);
        r.getActors().forEach((actor) => {
          widgetModel.actorToRepresentationMap.set(actor, r);
        });
      });
 
      model.behavior(widgetPublicAPI, widgetModel);
      // Forward representation methods
      ['coincidentTopologyParameters', ...(model.methodsToLink || [])].forEach(
        (methodName) => {
          const set = `set${macro.capitalize(methodName)}`;
          const get = `get${macro.capitalize(methodName)}`;
          const methods = {
            [methodName]: [],
            [set]: [],
            [get]: [],
          };
          widgetModel.representations.forEach((representation) => {
            Iif (representation[methodName]) {
              methods[methodName].push(representation[methodName]);
            }
            if (representation[set]) {
              methods[set].push(representation[set]);
            }
            if (representation[get]) {
              methods[get].push(representation[get]);
            }
          });
 
          Object.keys(methods).forEach((name) => {
            const calls = methods[name];
            if (calls.length === 1) {
              widgetPublicAPI[name] = calls[0];
            } else if (calls.length > 1) {
              widgetPublicAPI[name] = macro.chain(...calls);
            }
          });
        }
      );
 
      // Custom delete to detach from parent
      widgetPublicAPI.delete = macro.chain(() => {
        delete viewToWidget[viewId];
      }, widgetPublicAPI.delete);
 
      widgetPublicAPI.setInteractor(interactor);
      const viewWidget = Object.freeze(widgetPublicAPI);
      viewToWidget[viewId] = viewWidget;
      return viewWidget;
    }
    return viewToWidget[viewId];
  };
 
  // List of all the views the widget has been registered to.
  publicAPI.getViewIds = () => Object.keys(viewToWidget);
 
  publicAPI.getViewWidgets = () => Object.values(viewToWidget);
 
  // --------------------------------------------------------------------------
  // Widget visibility / enable
  // --------------------------------------------------------------------------
  // Call methods on all its view widgets
 
  publicAPI.setVisibility = (value) => {
    const viewIds = Object.keys(viewToWidget);
    for (let i = 0; i < viewIds.length; i++) {
      viewToWidget[viewIds[i]].setVisibility(value);
    }
  };
 
  publicAPI.setPickable = (value) => {
    const viewIds = Object.keys(viewToWidget);
    for (let i = 0; i < viewIds.length; i++) {
      viewToWidget[viewIds[i]].setPickable(value);
    }
  };
 
  publicAPI.setDragable = (value) => {
    const viewIds = Object.keys(viewToWidget);
    for (let i = 0; i < viewIds.length; i++) {
      viewToWidget[viewIds[i]].setDragable(value);
    }
  };
 
  publicAPI.setContextVisibility = (value) => {
    const viewIds = Object.keys(viewToWidget);
    for (let i = 0; i < viewIds.length; i++) {
      viewToWidget[viewIds[i]].setContextVisibility(value);
    }
  };
 
  publicAPI.setHandleVisibility = (value) => {
    const viewIds = Object.keys(viewToWidget);
    for (let i = 0; i < viewIds.length; i++) {
      viewToWidget[viewIds[i]].setHandleVisibility(value);
    }
  };
 
  // --------------------------------------------------------------------------
  // Place Widget API
  // --------------------------------------------------------------------------
 
  publicAPI.placeWidget = (bounds) => model.widgetState.placeWidget(bounds);
  publicAPI.getPlaceFactor = () => model.widgetState.getPlaceFactor();
  publicAPI.setPlaceFactor = (factor) =>
    model.widgetState.setPlaceFactor(factor);
 
  // --------------------------------------------------------------------------
  // Event Widget API
  // --------------------------------------------------------------------------
  let unsubscribe = NoOp;
  publicAPI.delete = macro.chain(publicAPI.delete, () => unsubscribe());
 
  if (model.widgetState) {
    unsubscribe = model.widgetState.onModified(() =>
      publicAPI.invokeWidgetChange(model.widgetState)
    ).unsubscribe;
  }
}
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, initialValues);
  macro.obj(publicAPI, model);
  macro.get(publicAPI, model, ['widgetState']);
  macro.event(publicAPI, model, 'WidgetChange');
  vtkAbstractWidgetFactory(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkAbstractWidget');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };