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

50% Statements 29/58
35% Branches 7/20
33.33% Functions 4/12
51.92% Lines 27/52

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    1x                       163x 163x 163x 163x             163x 141x 141x   141x 141x 441x 441x 130x   441x                               163x                                 163x                     163x   163x                 163x                       163x   163x           163x         1x             163x 163x 163x 163x            
import macro from 'vtk.js/Sources/macros';
 
const DEFAULT_LABEL = 'default';
 
function removeObjectInArray(array, obj) {
  const idx = array.indexOf(obj);
  if (idx !== -1) {
    array.splice(idx, 1);
  }
}
 
// ----------------------------------------------------------------------------
 
function vtkWidgetState(publicAPI, model) {
  model.classHierarchy.push('vtkWidgetState');
  const subscriptions = [];
  model.labels = {};
  model.nestedStates = [];
 
  // --------------------------------------------------------------------------
  // labels can be a string or an array of strings.
  // If nothing (or empty array) provided the default label will be used.
  // --------------------------------------------------------------------------
 
  publicAPI.bindState = (nested, labels = [DEFAULT_LABEL]) => {
    model.nestedStates.push(nested);
    subscriptions.push(nested.onModified(publicAPI.modified));
 
    if (Array.isArray(labels) && labels.length) {
      for (let i = 0; i < labels.length; i++) {
        const label = labels[i];
        if (!model.labels[label]) {
          model.labels[label] = [];
        }
        model.labels[label].push(nested);
      }
    } else E{
      // Need to bind to a label
      const labelToUse = Array.isArray(labels)
        ? DEFAULT_LABEL
        : labels || DEFAULT_LABEL;
      if (!model.labels[labelToUse]) {
        model.labels[labelToUse] = [];
      }
      model.labels[labelToUse].push(nested);
    }
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.unbindState = (nested) => {
    while (subscriptions.length) {
      subscriptions.pop().unsubscribe();
    }
    removeObjectInArray(model.nestedStates, nested);
    for (let i = 0; i < model.nestedStates.length; i++) {
      subscriptions.push(model.nestedStates[i].onModified(publicAPI.modified));
    }
 
    Object.keys(model.labels).forEach((label) => {
      const list = model.labels[label];
      removeObjectInArray(list, nested);
    });
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.unbindAll = () => {
    while (subscriptions.length) {
      subscriptions.pop().unsubscribe();
    }
    model.nestedStates = [];
  };
 
  // --------------------------------------------------------------------------
  // Active flag API
  // --------------------------------------------------------------------------
 
  publicAPI.activate = () => publicAPI.setActive(true);
 
  publicAPI.deactivate = (excludingState) => {
    if (excludingState !== publicAPI) {
      publicAPI.setActive(false);
    }
    for (let i = 0; i < model.nestedStates.length; i++) {
      model.nestedStates[i].deactivate(excludingState);
    }
  };
 
  publicAPI.activateOnly = (subState) => {
    if (subState) {
      subState.setActive(true);
    }
    // deactivate current state, but exclude the sub-state
    publicAPI.deactivate(subState);
  };
 
  // --------------------------------------------------------------------------
  // Nested state methods
  // --------------------------------------------------------------------------
 
  publicAPI.getStatesWithLabel = (name) => model.labels[name];
 
  publicAPI.getAllNestedStates = () => model.nestedStates;
 
  // --------------------------------------------------------------------------
  // Clean on delete
  // --------------------------------------------------------------------------
 
  publicAPI.delete = macro.chain(publicAPI.unbindAll, publicAPI.delete);
}
 
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  active: false,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, ['active']);
  vtkWidgetState(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export default { extend };