import CompositeClosureHelper from 'paraviewweb/src/Common/Core/CompositeClosureHelper'; import dataHelper from 'paraviewweb/src/InfoViz/Core/SelectionProvider/dataHelper';
function selectionProvider(publicAPI, model) { const dataSubscriptions = [];
if (!model.selectionData) { model.selectionData = {}; } if (!model.selectionMetaData) { model.selectionMetaData = {}; }
function off() { let count = dataSubscriptions.length; while (count) { count -= 1; dataSubscriptions[count] = null; } }
function flushDataToListener(dataListener, dataChanged) { try { if (dataListener) { const event = dataHelper.getNotificationData( model.selectionData, dataListener.request ); if (event) { if (dataChanged && dataChanged.type === dataListener.request.type) { dataListener.onDataReady(event); } else if (!dataChanged) { dataListener.onDataReady(event); } } } } catch (err) { console.log('flushDataToListener error caught:', err); } }
publicAPI.setSelectionData = (data) => { dataHelper.set(model.selectionData, data);
dataSubscriptions.forEach((listener) => flushDataToListener(listener, data) ); };
publicAPI.getSelectionData = (query) => dataHelper.get(model.selectionData, query);
publicAPI.updateSelectionMetadata = (addon) => { model.selectionMetaData[addon.type] = Object.assign( {}, model.selectionMetaData[addon.type], addon.metadata ); };
publicAPI.getSelectionMetadata = (type) => model.selectionMetaData[type];
publicAPI.setSelection = (selection) => { model.selection = selection; publicAPI.fireSelectionChange(selection); };
publicAPI.setAnnotation = (annotation) => { model.annotation = annotation; if (annotation.selection) { publicAPI.setSelection(annotation.selection); } else { annotation.selection = model.selection; } model.shouldCreateNewAnnotation = false; publicAPI.fireAnnotationChange(annotation); };
publicAPI.shouldCreateNewAnnotation = () => model.shouldCreateNewAnnotation; publicAPI.setCreateNewAnnotationFlag = (shouldCreate) => { model.shouldCreateNewAnnotation = shouldCreate; return shouldCreate; };
publicAPI.subscribeToDataSelection = ( type, onDataReady, variables = [], metadata = {} ) => { const id = dataSubscriptions.length; const request = { id, type, variables, metadata }; const dataListener = { onDataReady, request }; dataSubscriptions.push(dataListener); publicAPI.fireDataSelectionSubscriptionChange(request); flushDataToListener(dataListener, null); return { unsubscribe() { request.action = 'unsubscribe'; publicAPI.fireDataSelectionSubscriptionChange(request); dataSubscriptions[id] = null; }, update(vars, meta) { request.variables = [].concat(vars); request.metadata = Object.assign({}, request.metadata, meta); publicAPI.fireDataSelectionSubscriptionChange(request); flushDataToListener(dataListener, null); }, }; };
publicAPI.destroy = CompositeClosureHelper.chain(off, publicAPI.destroy); }
const DEFAULT_VALUES = { shouldCreateNewAnnotation: false, };
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
CompositeClosureHelper.destroy(publicAPI, model); CompositeClosureHelper.isA(publicAPI, model, 'SelectionProvider'); CompositeClosureHelper.get(publicAPI, model, ['selection', 'annotation']); CompositeClosureHelper.event(publicAPI, model, 'selectionChange'); CompositeClosureHelper.event(publicAPI, model, 'annotationChange'); CompositeClosureHelper.event( publicAPI, model, 'dataSelectionSubscriptionChange' );
selectionProvider(publicAPI, model); }
export const newInstance = CompositeClosureHelper.newInstance(extend);
export default { newInstance, extend };
|