UNPKG

@cornerstonejs/core

Version:
112 lines (111 loc) 3.74 kB
class PlanarMountedData { constructor(host) { this.host = host; } resolveBindingRole(options) { return options.role === 'source' ? 'source' : 'overlay'; } getActiveDataId() { return this.sourceDataId; } clearActiveDataId() { this.sourceDataId = undefined; } getCurrentBinding() { if (this.sourceDataId) { return (this.host.getBinding(this.sourceDataId) ?? this.host.getFirstBinding()); } return this.host.getFirstBinding(); } getFirstBoundDataId() { for (const [dataId] of this.host.getBindings()) { return dataId; } } hasBinding(dataId) { return Boolean(this.host.getBinding(dataId)); } promoteSourceDataId(dataId) { for (const [bindingDataId, binding] of this.host.getBindings()) { binding.role = bindingDataId === dataId ? 'source' : 'overlay'; } this.sourceDataId = dataId; } handleRemovedData(dataId) { if (this.sourceDataId !== dataId) { return; } this.sourceDataId = undefined; this.promoteFirstAvailableBindingToSource(); } removeBindingsExcept(keepDataIds) { const dataIds = Array.from(this.host.getBindings(), ([dataId]) => dataId); for (const dataId of dataIds) { if (!keepDataIds.has(dataId)) { this.host.removeData(dataId); } } } getActorForDataId(dataId) { const binding = this.host.getBinding(dataId); return binding?.getActorEntry?.(binding.data); } getBindingActor(dataId) { const rendering = this.host.getBinding(dataId)?.rendering; return rendering?.actor ?? rendering?.compatibilityActor; } getActors() { return [ ...this.getProjectedActorEntries('source'), ...this.getProjectedActorEntries('overlay'), ]; } getDefaultActor() { return (this.getProjectedActorEntries('source')[0] ?? this.getProjectedActorEntries()[0]); } getProjectedActorEntries(role) { const actorEntries = []; for (const [, binding] of this.host.getBindings()) { if (role && binding.role !== role) { continue; } const actorEntry = binding.getActorEntry?.(binding.data); if (actorEntry) { actorEntries.push(actorEntry); } } return actorEntries; } findBindingDataIdByActorEntryUID(actorEntryUID) { for (const [dataId, binding] of this.host.getBindings()) { const actorEntry = binding.getActorEntry?.(binding.data); if (actorEntry?.uid === actorEntryUID) { return dataId; } } } findDataIdByVolumeId(volumeId) { for (const [dataId, binding] of this.host.getBindings()) { const bindingVolumeId = binding.data?.volumeId; if (bindingVolumeId === volumeId) { return dataId; } } } getDefaultVOIRange(dataId) { const targetDataId = dataId ?? this.sourceDataId ?? this.getCurrentBinding()?.data.id; const rendering = targetDataId ? this.host.getBinding(targetDataId)?.rendering : undefined; const defaultVOIRange = rendering?.defaultVOIRange; return defaultVOIRange ? { ...defaultVOIRange } : undefined; } promoteFirstAvailableBindingToSource() { const firstBindingDataId = this.getFirstBoundDataId(); if (firstBindingDataId) { this.promoteSourceDataId(firstBindingDataId); } } } export default PlanarMountedData;