All files / Sources/Interaction/Manipulators/MouseBoxSelectorManipulator index.js

30% Statements 21/70
0% Branches 0/40
30% Functions 3/10
30% Lines 21/70

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      1x   1x                                         1x     1x 1x 1x 1x 1x 1x                                                         1x                                                                 1x                                             1x                                                                   1x                             1x     1x 1x 1x 1x 1x               1x         1x                
import * as macro from 'vtk.js/Sources/macros';
import vtkCompositeMouseManipulator from 'vtk.js/Sources/Interaction/Manipulators/CompositeMouseManipulator';
 
const OUTSIDE_BOUNDS = [-2, -1, -2, -1];
 
const DEFAULT_STYLE = {
  position: 'absolute',
  zIndex: 1,
  border: '2px solid #F44336',
  backgroundColor: 'rgba(0, 0, 0, 0.1)',
  borderRadius: '4px',
  boxSizing: 'border-box',
};
 
function applyStyle(element, style) {
  Object.keys(style).forEach((name) => {
    element.style[name] = style[name];
  });
}
 
// ----------------------------------------------------------------------------
// vtkMouseBoxSelectionManipulator methods
// ----------------------------------------------------------------------------
 
function vtkMouseBoxSelectionManipulator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkMouseBoxSelectionManipulator');
 
  // Private variable
  let view = null;
  let container = null;
  let previousPosition = null;
  let currentPosition = null;
  let div = null;
  let inDOM = false;
 
  function getBounds() {
    if (!previousPosition || !currentPosition) {
      return OUTSIDE_BOUNDS;
    }
    return [
      Math.min(previousPosition.x, currentPosition.x),
      Math.max(previousPosition.x, currentPosition.x),
      Math.min(previousPosition.y, currentPosition.y),
      Math.max(previousPosition.y, currentPosition.y),
    ];
  }
 
  function applyStyleToDiv() {
    if (!view || !container) {
      return;
    }
    const [viewWidth, viewHeight] = view.getSize();
    const { width, height } = container.getBoundingClientRect();
    const [xMin, xMax, yMin, yMax] = getBounds();
    div.style.left = `${(width * xMin) / viewWidth}px`;
    div.style.top = `${height - (height * yMax) / viewHeight}px`;
    div.style.width = `${(width * (xMax - xMin)) / viewWidth}px`;
    div.style.height = `${(height * (yMax - yMin)) / viewHeight}px`;
  }
 
  //-------------------------------------------------------------------------
 
  publicAPI.onButtonDown = (interactor, renderer, position) => {
    previousPosition = position;
 
    if (model.renderSelection) {
      // Need window size and location to convert to style
      if (!view) {
        view = interactor.getView();
      }
 
      if (!container && view?.getContainer) {
        container = view.getContainer();
      }
 
      if (!container) {
        container = model.container;
      }
 
      if (!div) {
        div = document.createElement('div');
        applyStyle(div, model.selectionStyle);
      }
 
      applyStyleToDiv();
 
      if (container && !inDOM) {
        inDOM = true;
        container.appendChild(div);
      }
    }
  };
 
  //-------------------------------------------------------------------------
 
  publicAPI.onMouseMove = (interactor, renderer, position) => {
    if (!previousPosition) {
      return;
    }
    if (!position) {
      return;
    }
 
    currentPosition = position;
 
    publicAPI.invokeBoxSelectInput({
      view,
      container,
      selection: getBounds(),
    });
 
    if (model.renderSelection) {
      applyStyleToDiv();
    }
  };
 
  //-------------------------------------------------------------------------
 
  publicAPI.onButtonUp = (interactor, renderer) => {
    if (!previousPosition || (!currentPosition && !model.boxChangeOnClick)) {
      return;
    }
 
    // needed because of boxChangeOnClick
    if (!currentPosition) {
      currentPosition = previousPosition;
    }
 
    publicAPI.invokeBoxSelectChange({
      view,
      container,
      selection: getBounds(),
    });
 
    if (inDOM) {
      div.parentElement.removeChild(div);
      inDOM = false;
    }
 
    // clear positions
    view = null;
    container = null;
    previousPosition = null;
    currentPosition = null;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
function DEFAULT_VALUES(initialValues) {
  return {
    // container: null,
    boxChangeOnClick: false,
    renderSelection: true,
    ...initialValues,
    selectionStyle: {
      ...DEFAULT_STYLE,
      ...initialValues.selectionStyle,
    },
  };
}
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES(initialValues));
 
  // Inheritance
  macro.obj(publicAPI, model);
  vtkCompositeMouseManipulator.extend(publicAPI, model, initialValues);
  macro.event(publicAPI, model, 'BoxSelectChange'); // Trigger at release
  macro.event(publicAPI, model, 'BoxSelectInput'); // Trigger while dragging
  macro.setGet(publicAPI, model, [
    'renderSelection',
    'boxChangeOnClick',
    'selectionStyle',
    'container',
  ]);
 
  // Object specific methods
  vtkMouseBoxSelectionManipulator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkMouseBoxSelectionManipulator'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };