All files / platform/core/src/services/WorkflowStepsService WorkflowStepsService.ts

25% Statements 18/72
0% Branches 0/12
22.22% Functions 4/18
24.63% Lines 17/69

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244        34x                                                                                                                                                           34x 34x 34x 34x 34x               34x 34x 34x 34x 34x 34x                                                                                                                                                                                                                                                             34x 34x       34x           34x     34x      
import { CommandsManager } from '../../classes';
import { ExtensionManager } from '../../extensions';
import { PubSubService } from '../_shared/pubSubServiceInterface';
 
export const EVENTS = {
  ACTIVE_STEP_CHANGED: 'event::workflowStepsService:activateStepChanged',
  STEPS_CHANGED: 'event::workflowStepsService:stepsChanged',
};
 
/*
  A mode may define a workflow and each workflow may have one or more steps.
  Each step may define a different set of tools, hanging protocol and panels
  layout that will be applied to the viewer once it gets activated making the
  viewer work in a more dynamic way.
 
  Example:
    All keys inside brackets are optionals.
 
    workflow: {
      [initialStepId]: 'step1',
      steps: [
        {
          id: 'firstStep',
          name: 'First Step',
          [toolbar]: {
            buttons: firstStepToolbarButtons,
            sections: [
              {
                key: 'primary',
                buttons: [ 'measurementSection', 'Zoom', ... ],
              },
            ],
          },
          [layout]: {
            [panels]: {
              left: ['firstLeftPanelId', 'secondLeftPanelId'],
              right: ['firstRightPanelId'],
            },
          },
          [hangingProtocol]: {
            protocolId: 'default',
            [stepId]: 'firstStep',
          },
        },
        {
          id: 'secondStep',
          name: 'Second Step',
          ...
        },
      ]
    }
 
  If workflow steps are defined but `initialStepId` is not set then the first
  step is set as active during mode initialization.
*/
 
type CommandCallback = {
  commandName: string;
  options: Record<string, unknown>;
};
 
export type WorkflowStep = {
  id: string;
  name: string;
  toolbarButtons?: {
    buttonSection: string;
    buttons: string[];
  }[];
  hangingProtocol?: {
    protocolId: string;
    stageId?: string;
  };
  layout?: {
    panels: {
      left?: string[];
      right?: string[];
    };
  };
  onEnter: () => void | CommandCallback[];
  onExit: () => void | CommandCallback[];
};
 
class WorkflowStepsService extends PubSubService {
  private _extensionManager: ExtensionManager;
  private _servicesManager: AppTypes.ServicesManager;
  private _commandsManager: CommandsManager;
  private _workflowSteps: WorkflowStep[];
  private _activeWorkflowStep: WorkflowStep;
 
  constructor(
    extensionManager: ExtensionManager,
    commandsManager: CommandsManager,
    servicesManager: AppTypes.ServicesManager
  ) {
    super(EVENTS);
    this._workflowSteps = [];
    this._activeWorkflowStep = null;
    this._extensionManager = extensionManager;
    this._commandsManager = commandsManager;
    this._servicesManager = servicesManager;
  }
 
  public get workflowSteps(): WorkflowStep[] {
    return [...this._workflowSteps];
  }
 
  public get activeWorkflowStep(): WorkflowStep {
    return this._activeWorkflowStep;
  }
 
  public addWorkflowSteps(workflowSteps: WorkflowStep[]): void {
    let workflowStepAdded = false;
 
    workflowSteps.forEach(newWorkflowStep => {
      const workflowStepExists = this._workflowSteps.some(
        workflowStep => workflowStep.id === newWorkflowStep.id
      );
 
      Iif (workflowStepExists) {
        throw new Error(`Duplicated workflow step id (${newWorkflowStep.id})`);
      }
 
      this._workflowSteps.push(newWorkflowStep);
      workflowStepAdded = true;
    });
 
    Iif (workflowStepAdded) {
      this._broadcastEvent(EVENTS.STEPS_CHANGED, {});
    }
  }
 
  private _updateToolBar(workflowStep: WorkflowStep) {
    const { toolbarService } = this._servicesManager.services;
    const { toolbarButtons } = workflowStep;
 
    const toUse = Array.isArray(toolbarButtons) ? toolbarButtons : [toolbarButtons];
 
    toUse.forEach(({ buttonSection, buttons }) => {
      toolbarService.clearButtonSection(buttonSection);
      toolbarService.createButtonSection(buttonSection, buttons);
    });
  }
 
  private _updatePanels(workflowStep: WorkflowStep) {
    const { panelService } = this._servicesManager.services;
    const panels = workflowStep?.layout?.panels;
 
    Iif (!panels) {
      return;
    }
 
    panelService.setPanels(panels, workflowStep?.layout?.options);
  }
 
  private _updateHangingProtocol(workflowStep: WorkflowStep) {
    const { hangingProtocol } = workflowStep;
 
    Iif (!hangingProtocol) {
      return;
    }
 
    this._commandsManager.runCommand('setHangingProtocol', {
      protocolId: hangingProtocol.protocolId,
      stageId: hangingProtocol.stageId,
      stageIndex: hangingProtocol.stageIndex,
    });
  }
 
  private _invokeCallbacks(callbacks) {
    Iif (!callbacks) {
      return;
    }
 
    const commandsManager = this._commandsManager;
 
    Iif (!Array.isArray(callbacks)) {
      callbacks = [callbacks];
    }
 
    // Invoke all callbacks which may be a function or an object like
    // { commandName: string, options?: object }
    callbacks.forEach(callback => {
      let fn = callback;
 
      Iif (callback?.commandName) {
        const { commandName, options } = callback;
        fn = () => commandsManager.runCommand(commandName, options);
      }
 
      fn();
    });
  }
 
  public setActiveWorkflowStep(workflowStepId: string): void {
    const previousWorkflowStep = this._activeWorkflowStep;
 
    Iif (workflowStepId === previousWorkflowStep?.id) {
      return;
    }
 
    const newWorkflowStep = this._workflowSteps.find(step => step.id === workflowStepId);
 
    Iif (!newWorkflowStep) {
      throw new Error(`Invalid workflowStepId (${workflowStepId})`);
    }
 
    Iif (this._activeWorkflowStep) {
      this._invokeCallbacks(previousWorkflowStep.onExit);
    }
 
    // onEnter needs to be called before updating the Hanging Protocol because
    // some displaySets need to be created before moving to the next HP stage
    // (eg: convert segmentations into a chart displaySet). If needed we can
    // change it to onBeforeEnter and onAfterEnter in the future.
    this._invokeCallbacks(newWorkflowStep.onEnter);
 
    this._activeWorkflowStep = newWorkflowStep;
    this._updateToolBar(newWorkflowStep);
    this._updatePanels(newWorkflowStep);
    this._updateHangingProtocol(newWorkflowStep);
    this._broadcastEvent(EVENTS.ACTIVE_STEP_CHANGED, {
      activeWorkflowStep: newWorkflowStep,
    });
  }
 
  public reset(): void {
    this._activeWorkflowStep = null;
    this._workflowSteps = [];
  }
 
  public onModeEnter(): void {
    this.reset();
  }
 
  public static REGISTRATION = {
    name: 'workflowStepsService',
    create: ({ extensionManager, commandsManager, servicesManager }): WorkflowStepsService => {
      return new WorkflowStepsService(extensionManager, commandsManager, servicesManager);
    },
  };
}
 
export { WorkflowStepsService as default, WorkflowStepsService };