All files / Sources/Interaction/UI/Slider index.js

21.66% Statements 26/120
0% Branches 0/52
16.66% Functions 3/18
21.84% Lines 26/119

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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268                                                        1x   1x 1x                                                                               1x 1x 1x                                                                                                                                                   1x                               1x                                 1x                             1x                 1x                     1x 1x 1x 1x 1x 3x         1x             1x                               1x     1x 1x 1x 1x     1x         1x          
import macro from 'vtk.js/Sources/macros';
import Constants from 'vtk.js/Sources/Interaction/UI/Slider/Constants';
import style from 'vtk.js/Sources/Interaction/UI/Slider/Slider.module.css';
 
// ----------------------------------------------------------------------------
// Helper methods
// ----------------------------------------------------------------------------
 
function findClosestValue(value, values) {
  let distance = Number.MAX_VALUE;
  let index = -1;
  let count = values.length;
  while (count--) {
    const dist = Math.abs(values[count] - value);
    if (dist < distance) {
      distance = dist;
      index = count;
    }
  }
  return index !== -1 ? values[index] : undefined;
}
 
// ----------------------------------------------------------------------------
// vtkSlider methods
// ----------------------------------------------------------------------------
 
function vtkSlider(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkSlider');
 
  model.el = document.createElement('div');
  model.el.setAttribute('class', style.cursor);
 
  // --------------------------------------------------------------------------
  // Private methods
  // --------------------------------------------------------------------------
 
  function getDisplacementRatio() {
    return (
      ((model.containerSizes[1] - model.containerSizes[0]) *
        (model.value - model.values[0])) /
      (model.values[model.values.length - 1] - model.values[0])
    );
  }
 
  function updateCursorPosition() {
    if (!model.container) {
      return;
    }
    const cursorSize = model.containerSizes[0];
    const position = getDisplacementRatio();
    if (Number.isNaN(position) || Number.isNaN(cursorSize)) {
      return;
    }
    model.el.style.width = `${cursorSize}px`;
    model.el.style.height = `${cursorSize}px`;
    if (model.orientation === Constants.SliderOrientation.VERTICAL) {
      // VERTICAL
      model.el.style.left = '0';
      model.el.style.top = `${position}px`;
      model.el.style.cursor = 'row-resize';
    } else {
      // HORIZONTAL
      model.el.style.top = '0';
      model.el.style.left = `${position}px`;
      model.el.style.cursor = 'col-resize';
    }
  }
 
  // --------------------------------------------------------------------------
 
  let isDragging = false;
  let offset = 0;
  let ratio = 0;
 
  function handleDragEvents(enable) {
    const rootElm = document.querySelector('body');
    const method = enable ? 'addEventListener' : 'removeEventListener';
 
    /* eslint-disable no-use-before-define */
    rootElm[method]('mousemove', onMouseMove);
    rootElm[method]('mouseleave', onMouseOut);
    rootElm[method]('mouseup', onMouseUp);
    /* eslint-enable no-use-before-define */
  }
 
  function onMouseMove(e) {
    e.preventDefault();
    if (isDragging) {
      const newRatio =
        ratio +
        ((model.orientation ? e.clientX : e.clientY) - offset) /
          (model.containerSizes[1] - model.containerSizes[0]);
      const value = newRatio * model.range + model.values[0];
      const newValue = findClosestValue(value, model.values);
      if (newValue !== undefined) {
        publicAPI.setValue(newValue);
      }
    }
  }
 
  function onMouseOut(e) {
    isDragging = false;
  }
 
  function onMouseUp(e) {
    handleDragEvents(false);
    if (!isDragging) {
      const isClick = !((model.orientation ? e.clientX : e.clientY) - offset);
      if (isClick) {
        const absValue =
          model.values[0] +
          (model.range *
            (offset -
              model.container.getBoundingClientRect()[
                model.orientation ? 'left' : 'top'
              ] -
              0.5 * model.containerSizes[0])) /
            (model.containerSizes[1] - model.containerSizes[0]);
        const newValue = findClosestValue(absValue, model.values);
        if (newValue !== undefined) {
          publicAPI.setValue(newValue);
        }
      }
    }
    isDragging = false;
  }
 
  function onMouseDown(e) {
    handleDragEvents(true);
    e.preventDefault();
    isDragging = e.target === model.el;
    offset = model.orientation ? e.clientX : e.clientY;
    ratio = (model.value - model.values[0]) / model.range;
  }
 
  function bindEvents() {
    model.container.addEventListener('mousedown', onMouseDown);
  }
 
  function unbindEvents() {
    handleDragEvents(false);
    model.container.removeEventListener('mousedown', onMouseDown);
  }
 
  // --------------------------------------------------------------------------
 
  publicAPI.setContainer = (el) => {
    if (model.container && model.container !== el) {
      model.container.removeChild(model.el);
      unbindEvents();
    }
    if (model.container !== el) {
      model.container = el;
      if (model.container) {
        model.container.appendChild(model.el);
        publicAPI.resize();
        bindEvents();
      }
      publicAPI.modified();
    }
  };
 
  publicAPI.resize = () => {
    if (model.container) {
      const dims = model.container.getBoundingClientRect();
      const width = Math.floor(dims.width);
      const height = Math.floor(dims.height);
      const min = Math.min(width, height);
      const max = Math.max(width, height);
      publicAPI.setOrientation(
        height === max
          ? Constants.SliderOrientation.VERTICAL
          : Constants.SliderOrientation.HORIZONTAL
      );
      model.containerSizes = [min, max];
      updateCursorPosition();
    }
  };
 
  publicAPI.setValue = (v) => {
    if (
      model.value !== v &&
      model.values[0] <= v &&
      v <= model.values.slice(-1)[0]
    ) {
      model.value = v;
      updateCursorPosition();
      publicAPI.modified();
      publicAPI.invokeValueChange(v);
      return true;
    }
    return false;
  };
 
  publicAPI.setValues = (values) => {
    if (model.values !== values) {
      model.values = values;
      model.range = values[values.length - 1] - values[0];
      updateCursorPosition();
      publicAPI.modified();
    }
  };
 
  publicAPI.generateValues = (min, max, nbSteps) => {
    const step = (max - min) / (nbSteps - 1);
    model.values = [];
    for (let i = 0; i < nbSteps; i++) {
      model.values.push(min + i * step);
    }
    model.range = max - min;
    updateCursorPosition();
    publicAPI.modified();
  };
 
  publicAPI.updateCursorStyle = (cursorStyle) => {
    model.cursorStyle = { ...model.cursorStyle, ...cursorStyle };
    const keys = Object.keys(model.cursorStyle);
    let count = keys.length;
    while (count--) {
      model.el.style[keys[count]] = model.cursorStyle[keys[count]];
    }
  };
 
  // Apply default style
  publicAPI.updateCursorStyle();
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  orientation: Constants.SliderOrientation.VERTICAL,
  value: 0.5,
  values: [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1],
  range: 1,
  containerSizes: [10, 100],
  cursorStyle: {
    border: 'solid 4px #aaa',
    backgroundColor: '#ccc',
    transform: 'scale(0.7)',
  },
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  macro.obj(publicAPI, model);
  macro.get(publicAPI, model, ['orientation', 'value', 'values']);
  macro.set(publicAPI, model, ['orientation']);
  macro.event(publicAPI, model, 'ValueChange');
 
  // Object specific methods
  vtkSlider(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkSlider');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...Constants };