WidgetState

Methods

activate

Activate the widget state instance. Same as calling vtkWidgetState.setActive(true)

activateOnly

Activate only the passed in sub state. Every other sub states will be deactivated.

Argument Type Required Description
subState vtkWidgetState Yes The sub-state that should be activated.

bindState

Bind a state to one or more labels. If no label is provided, the default one will be used.

Argument Type Required Description
subState vtkWidgetState Yes The state to bound.
labels String or Array. No The labels to which the state should be bound.

deactivate

Deactivate thie widget state instance and all its sub states, except the excludingState argument.

Argument Type Required Description
excludingState vtkWidgetState No A sub-state instance that should not be deactivated.

extend

Method use to decorate a given object (publicAPI+model) with vtkWidgetState characteristics.

Argument Type Required Description
publicAPI Yes object on which methods will be bounds (public)
model Yes object on which data structure will be bounds (protected)
initialValues object No (default: {})

getActive

Get the active flag of the widget state instance

getAllNestedStates

Get all the nested states on the widget state instance.

getStatesWithLabel

Get every states that are associated with the given label.

Argument Type Required Description
label String Yes The label from which to retrieve the states.

setActive

Set the active flag of the widget state instance

Argument Type Required Description
active Yes The active flag

unbindAll

Unbind all states from the widget state instance

unbindState

Unbind a specific state from the widget state instance

Argument Type Required Description
subState vtkWidgetState Yes The state to be unbound.

Source

index.d.ts
import { vtkObject } from '../../../interfaces';

export interface vtkWidgetState extends vtkObject {
/**
* Set the active flag of the widget state instance
*
* @param active The active flag
*/
setActive(active: boolean): boolean;

/**
* Get the active flag of the widget state instance
*/
getActive(): boolean;

/**
* Bind a state to one or more labels. If no label is provided, the default one will be used.
*
* @param {vtkWidgetState} subState The state to bound.
* @param {String | String[]} [labels] The labels to which the state should be bound.
*/
bindState(subState: vtkWidgetState, labels?: string | string[]): void;

/**
* Unbind a specific state from the widget state instance
*
* @param {vtkWidgetState} subState The state to be unbound.
*/
unbindState(subState: vtkWidgetState): void;

/**
* Unbind all states from the widget state instance
*/
unbindAll(): void;

/**
* Activate the widget state instance. Same as calling `vtkWidgetState.setActive(true)`
*/
activate(): void;

/**
* Deactivate thie widget state instance and all its sub states, except the `excludingState` argument.
*
* @param {vtkWidgetState} [excludingState] A sub-state instance that should not be deactivated.
*/
deactivate(excludingState?: vtkWidgetState): void;

/**
* Activate only the passed in sub state. Every other sub states will be deactivated.
*
* @param {vtkWidgetState} subState The sub-state that should be activated.
*/
activateOnly(subState: vtkWidgetState): void;

/**
* Get every states that are associated with the given label.
*
* @param {String} label The label from which to retrieve the states.
*/
getStatesWithLabel(label: string): vtkWidgetState[];

/**
* Get all the nested states on the widget state instance.
*/
getAllNestedStates(): vtkWidgetState[];
}

/**
* Method use to decorate a given object (publicAPI+model) with vtkWidgetState characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param {object} [initialValues] (default: {})
*/
export function extend(
publicAPI: object,
model: object,
initialValues?: object
): vtkWidgetState;

export declare const vtkWidgetState: {
extend: typeof extend;
};

export default vtkWidgetState;
index.js
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 {
// 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 };