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

79.31% Statements 46/58
47.05% Branches 8/17
91.66% Functions 11/12
78.94% Lines 45/57

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                                      1x           1x                                                   163x 139x     163x 710x 710x 710x         163x   163x             24x 24x   24x   24x         11x 11x   11x 19x       19x 19x 19x 19x   11x                       11x 15x 11x 4x           4x   11x       120x 120x 120x 120x 120x       2x 2x 2x 2x       77x 17x             60x   77x 77x       24x                 24x            
import macro from 'vtk.js/Sources/macros';
 
import vtkWidgetState from 'vtk.js/Sources/Widgets/Core/WidgetState';
 
import bounds from 'vtk.js/Sources/Widgets/Core/StateBuilder/boundsMixin';
import color from 'vtk.js/Sources/Widgets/Core/StateBuilder/colorMixin';
import color3 from 'vtk.js/Sources/Widgets/Core/StateBuilder/color3Mixin';
import corner from 'vtk.js/Sources/Widgets/Core/StateBuilder/cornerMixin';
import direction from 'vtk.js/Sources/Widgets/Core/StateBuilder/directionMixin';
import manipulator from 'vtk.js/Sources/Widgets/Core/StateBuilder/manipulatorMixin';
import name from 'vtk.js/Sources/Widgets/Core/StateBuilder/nameMixin';
import orientation from 'vtk.js/Sources/Widgets/Core/StateBuilder/orientationMixin';
import origin from 'vtk.js/Sources/Widgets/Core/StateBuilder/originMixin';
import scale1 from 'vtk.js/Sources/Widgets/Core/StateBuilder/scale1Mixin';
import scale3 from 'vtk.js/Sources/Widgets/Core/StateBuilder/scale3Mixin';
import text from 'vtk.js/Sources/Widgets/Core/StateBuilder/textMixin';
import visible from 'vtk.js/Sources/Widgets/Core/StateBuilder/visibleMixin';
import shape from 'vtk.js/Sources/Widgets/Core/StateBuilder/shapeMixin';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// Global type lookup map
// ----------------------------------------------------------------------------
 
const MIXINS = {
  bounds,
  color,
  color3,
  corner,
  direction,
  manipulator,
  name,
  orientation,
  origin,
  scale1,
  scale3,
  text,
  visible,
  shape,
};
 
// ----------------------------------------------------------------------------
 
function newInstance(
  mixins,
  initialValues,
  publicAPI = {},
  model = {},
  skipWidgetState = false
) {
  if (!skipWidgetState) {
    vtkWidgetState.extend(publicAPI, model, initialValues);
  }
 
  for (let i = 0; i < mixins.length; i++) {
    const mixin = MIXINS[mixins[i]];
    if (mixin) {
      mixin.extend(publicAPI, model, initialValues);
    } else E{
      vtkErrorMacro('Invalid mixin name:', mixins[i]);
    }
  }
  macro.safeArrays(model);
 
  return Object.freeze(publicAPI);
}
 
// ----------------------------------------------------------------------------
 
class Builder {
  constructor() {
    this.publicAPI = {};
    this.model = {};
 
    vtkWidgetState.extend(this.publicAPI, this.model);
    // The root state should always have the bounds/placeWidget/widgetFactor
    bounds.extend(this.publicAPI, this.model);
  }
 
  /* eslint-disable no-shadow */
  addDynamicMixinState({ labels, mixins, name, initialValues }) {
    const listName = `${name}List`;
    this.model[listName] = [];
    // Create new Instance method
    this.publicAPI[`add${macro.capitalize(name)}`] = (values) => {
      const instance = newInstance(mixins, {
        ...initialValues,
        ...values,
      });
      this.publicAPI.bindState(instance, labels);
      this.model[listName].push(instance);
      this.publicAPI.modified();
      return instance;
    };
    this.publicAPI[`remove${macro.capitalize(name)}`] = (instanceOrIndex) => {
      let removeIndex = this.model[listName].indexOf(instanceOrIndex);
      if (removeIndex === -1 && instanceOrIndex < this.model[listName].length) {
        removeIndex = instanceOrIndex;
      }
      const instance = this.model[listName][removeIndex];
      if (instance) {
        this.publicAPI.unbindState(instance);
      }
      this.model[listName].splice(removeIndex, 1);
      this.publicAPI.modified();
    };
    this.publicAPI[`get${macro.capitalize(name)}List`] = () =>
      this.model[listName].slice();
    this.publicAPI[`clear${macro.capitalize(name)}List`] = () => {
      while (this.model[listName].length) {
        const instance = this.model[listName].pop();
        if (instance) {
          this.publicAPI.unbindState(instance);
        }
      }
      this.publicAPI.modified();
    };
    return this;
  }
 
  addStateFromMixin({ labels, mixins, name, initialValues }) {
    const instance = newInstance(mixins, initialValues);
    this.model[name] = instance;
    this.publicAPI.bindState(instance, labels);
    macro.setGet(this.publicAPI, this.model, [name]);
    return this;
  }
 
  addStateFromInstance({ labels, name, instance }) {
    this.model[name] = instance;
    this.publicAPI.bindState(instance, labels);
    macro.setGet(this.publicAPI, this.model, [name]);
    return this;
  }
 
  addField({ name, initialValue }) {
    if (Array.isArray(initialValue)) {
      macro.setGetArray(
        this.publicAPI,
        this.model,
        [name],
        initialValue.length
      );
    } else {
      macro.setGet(this.publicAPI, this.model, [name]);
    }
    this.model[name] = initialValue;
    return this;
  }
 
  build(...mixins) {
    return newInstance(mixins, {}, this.publicAPI, this.model, true);
  }
}
 
// ----------------------------------------------------------------------------
// Public API
// ----------------------------------------------------------------------------
 
export function createBuilder() {
  return new Builder();
}
 
export default {
  createBuilder,
};