All files / Sources/Common/DataModel/Collection index.js

96% Statements 48/50
69.23% Branches 9/13
100% Functions 15/15
95.45% Lines 42/44

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    1x               6x   6x 11x 11x     6x 2x     2x 2x     6x 1x 1x     6x   3x 3x 3x 3x           6x 1x 1x     6x   6x   55x   37x   6x 1x   1x     6x 1x   6x   6x 2x 2x 10x 10x     2x 2x                 1x             6x     6x     6x         1x          
import macro from 'vtk.js/Sources/macros';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkCollection methods
// ----------------------------------------------------------------------------
 
function vtkCollection(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkCollection');
 
  publicAPI.addItem = (item) => {
    model.collection.push(item);
    publicAPI.modified();
  };
 
  publicAPI.insertItem = (idx, item) => {
    Iif (idx < 0 || model.collection.length < idx) {
      vtkErrorMacro('idx out of bounds for insertion.');
    }
    model.collection.splice(idx, 0, item);
    publicAPI.modified();
  };
 
  publicAPI.replaceItem = (idx, item) => {
    model.collection.splice(idx, 1, item);
    publicAPI.modified();
  };
 
  publicAPI.removeItem = (inValue) => {
    const idx =
      typeof inValue === 'number' ? inValue : model.collection.indexOf(inValue);
    if (idx >= 0 && idx < model.collection.length) {
      model.collection.splice(idx, 1);
      publicAPI.modified();
    } else E{
      vtkErrorMacro('idx out of bounds for removeItem.');
    }
  };
 
  publicAPI.removeAllItems = () => {
    model.collection = [];
    publicAPI.modified();
  };
 
  publicAPI.isItemPresent = (item) => model.collection.includes(item);
 
  publicAPI.getNumberOfItems = () => model.collection.length;
 
  publicAPI.empty = () => model.collection.length === 0;
 
  publicAPI.getItem = (idx) => model.collection[idx];
 
  publicAPI.forEach = (ftor) => {
    model.collection.forEach(ftor);
    // Call modified() for the collection, since ftor could have modified the elements.
    publicAPI.updateMTimeWithElements();
  };
 
  publicAPI.reduce = (ftor, initialValue) =>
    model.collection.reduce(ftor, initialValue);
 
  publicAPI.map = (ftor) => model.collection.map(ftor);
 
  publicAPI.updateMTimeWithElements = () => {
    let maxMTimeOfItems = 0; // we expect time values to be positive numbers
    for (let i = 0; i < model.collection.length; ++i) {
      const elem = model.collection[i];
      maxMTimeOfItems = Math.max(maxMTimeOfItems, elem.getMTime());
    }
 
    if (maxMTimeOfItems > publicAPI.getMTime()) {
      publicAPI.modified();
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  collection: [],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  macro.obj(publicAPI, model);
 
  // Object specific methods
  vtkCollection(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkCollection');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };