All files / Sources/Rendering/Misc/RenderWindowWithControlBar index.js

58.33% Statements 28/48
14.28% Branches 1/7
53.84% Functions 7/13
58.33% Lines 28/48

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                              1x   1x                                                                                   1x 1x 6x             1x     4x       1x 1x     1x               1x 1x 1x     1x 1x     1x                                       1x           1x             1x 1x   1x 1x             1x                 1x     1x 1x             1x         1x                
import macro from 'vtk.js/Sources/macros';
import vtkGenericRenderWindow from 'vtk.js/Sources/Rendering/Misc/GenericRenderWindow';
import style from 'vtk.js/Sources/Rendering/Misc/RenderWindowWithControlBar/RenderWindowWithControlBar.module.css';
 
// Load basic classes for vtk() factory
import 'vtk.js/Sources/Common/Core/Points';
import 'vtk.js/Sources/Common/Core/DataArray';
import 'vtk.js/Sources/Common/DataModel/PolyData';
import 'vtk.js/Sources/Rendering/Core/Actor';
import 'vtk.js/Sources/Rendering/Core/Mapper';
 
// ----------------------------------------------------------------------------
// Utility functions to control style
// ----------------------------------------------------------------------------
 
const CONTROL_STYLE = {
  left(size) {
    return {
      top: '0',
      left: '0',
      bottom: '0',
      right: 'unset',
      height: 'unset',
      width: `${size}px`,
    };
  },
  right(size) {
    return {
      top: '0',
      right: '0',
      bottom: '0',
      left: 'unset',
      height: 'unset',
      width: `${size}px`,
    };
  },
  top(size) {
    return {
      top: '0',
      left: '0',
      right: '0',
      bottom: 'unset',
      width: 'unset',
      height: `${size}px`,
    };
  },
  bottom(size) {
    return {
      bottom: '0',
      left: '0',
      right: '0',
      top: 'unset',
      width: 'unset',
      height: `${size}px`,
    };
  },
};
 
function applyControlStyle(el, position, size) {
  const styleToApply = CONTROL_STYLE[position](size);
  Object.keys(styleToApply).forEach((key) => {
    el.style[key] = styleToApply[key];
  });
}
 
// ----------------------------------------------------------------------------
 
function vtkRenderWindowWithControlBar(publicAPI, model) {
  const superClass = { ...publicAPI };
 
  function resetStyleToZero(key) {
    model.renderWindowContainer.style[key] = '0px';
  }
 
  function updateControlerStyle() {
    ['left', 'right', 'top', 'bottom'].forEach(resetStyleToZero);
    model.renderWindowContainer.style[
      model.controlPosition
    ] = `${model.controlSize}px`;
    applyControlStyle(
      model.controlContainer,
      model.controlPosition,
      model.controlSize
    );
  }
 
  // Create container for the vtkGenericRenderWindow
  model.renderWindowContainer = document.createElement('div');
  model.renderWindowContainer.classList.add(style.renderWindow);
  superClass.setContainer(model.renderWindowContainer);
 
  // Create container for controls
  model.controlContainer = document.createElement('div');
  model.controlContainer.classList.add(style.control);
 
  // Handle DOM container relocation
  publicAPI.setContainer = (el) => {
    if (model.rootContainer) {
      model.rootContainer.removeChild(model.container);
      model.rootContainer.removeChild(model.controlContainer);
      model.rootContainer.classList.remove(style.rootContainer);
    }
 
    // Switch container
    model.rootContainer = el;
 
    // Bind to new container
    if (model.rootContainer) {
      model.rootContainer.appendChild(model.container);
      model.rootContainer.appendChild(model.controlContainer);
      model.rootContainer.classList.add(style.rootContainer);
      updateControlerStyle();
      publicAPI.resize();
    }
  };
 
  publicAPI.setControlSize = (size) => {
    model.controlSize = size;
    updateControlerStyle();
    publicAPI.modified();
  };
 
  publicAPI.setControlPosition = (pos) => {
    model.controlPosition = pos;
    updateControlerStyle();
    publicAPI.modified();
  };
 
  // Handle size
  if (model.listenWindowResize) {
    window.addEventListener('resize', publicAPI.resize);
  }
  updateControlerStyle();
  publicAPI.resize();
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  rootContainer: null,
  controlPosition: 'left',
  controlSize: 10,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  vtkGenericRenderWindow.extend(publicAPI, model);
  macro.get(publicAPI, model, [
    'rootContainer',
    'controlContainer',
    'renderWindowContainer',
  ]);
 
  // Object specific methods
  vtkRenderWindowWithControlBar(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkRenderWindowWithControlBar'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };