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

80.55% Statements 87/108
69.23% Branches 18/26
73.52% Functions 25/34
80.76% Lines 84/104

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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341    34x                           34x       34x 34x             34x 34x 34x 34x             102x       91x                         432x   693x       432x 432x 432x           432x             144x   144x 432x       432x 432x   432x                                 232x 232x   232x 232x   232x 232x   232x 232x     232x 232x   232x 232x   232x 232x   232x 232x   232x 232x     232x 232x   232x 232x         48x               9x     9x     9x 9x             5693x       2x               3164x 3164x       76x 76x             7x       18x 18x 18x   18x   18x 26x   26x 26x   26x   26x   26x               18x 18x                           1776x 1776x 1776x                                         83x 83x   83x                     83x   83x 83x     85x   83x         83x                                                                                             464x 992x               34x      
import { PubSubService } from '../_shared/pubSubServiceInterface';
 
class ViewportGridService extends PubSubService {
  public static readonly EVENTS = {
    ACTIVE_VIEWPORT_ID_CHANGED: 'event::activeviewportidchanged',
    LAYOUT_CHANGED: 'event::layoutChanged',
    GRID_STATE_CHANGED: 'event::gridStateChanged',
    GRID_SIZE_CHANGED: 'event::gridSizeChanged',
    VIEWPORTS_READY: 'event::viewportsReady',
    VIEWPORT_ONDROP_HANDLED: 'event::viewportOnDropHandled',
  };
 
  public static REGISTRATION = {
    name: 'viewportGridService',
    altName: 'ViewportGridService',
    create: ({ configuration = {}, servicesManager }) => {
      return new ViewportGridService({ servicesManager });
    },
  };
 
  serviceImplementation = {};
  servicesManager: AppTypes.ServicesManager;
  presentationIdProviders: Map<
    string,
    (id: string, { viewport, viewports, isUpdatingSameViewport, servicesManager }) => unknown
  >;
 
  constructor({ servicesManager }) {
    super(ViewportGridService.EVENTS);
    this.servicesManager = servicesManager;
    this.serviceImplementation = {};
    this.presentationIdProviders = new Map();
  }
 
  public addPresentationIdProvider(
    id: string,
    provider: (id: string, { viewport, viewports, isUpdatingSameViewport }) => unknown
  ): void {
    this.presentationIdProviders.set(id, provider);
  }
 
  public setIsReferenceViewable(viewportId: string, isReferenceViewable: boolean): void {
    this.serviceImplementation._setIsReferenceViewable(viewportId, isReferenceViewable);
  }
 
  public getPresentationId(id: string, viewportId: string): string | null {
    const state = this.getState();
    const viewport = state.viewports.get(viewportId);
    return this._getPresentationId(id, {
      viewport,
      viewports: state.viewports,
    });
  }
 
  private _getPresentationId(id, { viewport, viewports }) {
    const isUpdatingSameViewport = [...viewports.values()].some(
      v =>
        v.displaySetInstanceUIDs?.toString() === viewport.displaySetInstanceUIDs?.toString() &&
        v.viewportId === viewport.viewportId
    );
 
    const provider = this.presentationIdProviders.get(id);
    if (provider) {
      const result = provider(id, {
        viewport,
        viewports,
        isUpdatingSameViewport,
        servicesManager: this.servicesManager,
      });
      return result;
    }
    return null;
  }
 
  public getPresentationIds({ viewport, viewports }) {
    // Use the keys of the Map to get all registered provider IDs
    const registeredPresentationProviders = Array.from(this.presentationIdProviders.keys());
 
    return registeredPresentationProviders.reduce((acc, id) => {
      const value = this._getPresentationId(id, {
        viewport,
        viewports,
      });
      if (value !== null) {
        acc[id] = value;
      }
      return acc;
    }, {});
  }
 
  public setServiceImplementation({
    getState: getStateImplementation,
    setActiveViewportId: setActiveViewportIdImplementation,
    setDisplaySetsForViewports: setDisplaySetsForViewportsImplementation,
    setLayout: setLayoutImplementation,
    reset: resetImplementation,
    onModeExit: onModeExitImplementation,
    set: setImplementation,
    getNumViewportPanes: getNumViewportPanesImplementation,
    setViewportIsReady: setViewportIsReadyImplementation,
    setIsReferenceViewable: setIsReferenceViewableImplementation,
    getViewportState: getViewportStateImplementation,
  }): void {
    if (getViewportStateImplementation) {
      this.serviceImplementation._getViewportState = getViewportStateImplementation;
    }
    if (getStateImplementation) {
      this.serviceImplementation._getState = getStateImplementation;
    }
    if (setActiveViewportIdImplementation) {
      this.serviceImplementation._setActiveViewport = setActiveViewportIdImplementation;
    }
    if (setDisplaySetsForViewportsImplementation) {
      this.serviceImplementation._setDisplaySetsForViewports =
        setDisplaySetsForViewportsImplementation;
    }
    if (setLayoutImplementation) {
      this.serviceImplementation._setLayout = setLayoutImplementation;
    }
    if (resetImplementation) {
      this.serviceImplementation._reset = resetImplementation;
    }
    if (onModeExitImplementation) {
      this.serviceImplementation._onModeExit = onModeExitImplementation;
    }
    if (setImplementation) {
      this.serviceImplementation._set = setImplementation;
    }
    if (getNumViewportPanesImplementation) {
      this.serviceImplementation._getNumViewportPanes = getNumViewportPanesImplementation;
    }
 
    if (setViewportIsReadyImplementation) {
      this.serviceImplementation._setViewportIsReady = setViewportIsReadyImplementation;
    }
    if (setIsReferenceViewableImplementation) {
      this.serviceImplementation._setIsReferenceViewable = setIsReferenceViewableImplementation;
    }
  }
 
  public publishViewportsReady() {
    this._broadcastEvent(this.EVENTS.VIEWPORTS_READY, {});
  }
 
  public publishViewportOnDropHandled(eventData) {
    this._broadcastEvent(this.EVENTS.VIEWPORT_ONDROP_HANDLED, { eventData });
  }
 
  public setActiveViewportId(id: string) {
    Iif (id === this.getActiveViewportId()) {
      return;
    }
    this.serviceImplementation._setActiveViewport(id);
 
    // Use queueMicrotask to delay the event broadcast
    setTimeout(() => {
      this._broadcastEvent(this.EVENTS.ACTIVE_VIEWPORT_ID_CHANGED, {
        viewportId: id,
      });
    }, 0);
  }
 
  public getState(): AppTypes.ViewportGrid.State {
    return this.serviceImplementation._getState();
  }
 
  public getViewportState(viewportId: string) {
    return this.serviceImplementation._getViewportState(viewportId);
  }
 
  public setViewportIsReady(viewportId, callback) {
    this.serviceImplementation._setViewportIsReady(viewportId, callback);
  }
 
  public getActiveViewportId() {
    const state = this.getState();
    return state.activeViewportId;
  }
 
  public setViewportGridSizeChanged() {
    const state = this.getState();
    this._broadcastEvent(this.EVENTS.GRID_SIZE_CHANGED, {
      state,
    });
  }
 
  public setDisplaySetsForViewport(props) {
    // Just update a single viewport, but use the multi-viewport update for it.
    this.setDisplaySetsForViewports([props]);
  }
 
  public async setDisplaySetsForViewports(viewportsToUpdate) {
    await this.serviceImplementation._setDisplaySetsForViewports(viewportsToUpdate);
    const state = this.getState();
    const updatedViewports = [];
 
    const removedViewportIds = [];
 
    for (const viewport of viewportsToUpdate) {
      const updatedViewport = state.viewports.get(viewport.viewportId);
 
      if (updatedViewport) {
        updatedViewports.push(updatedViewport);
 
        const updatedDisplaySetUIDs = updatedViewport.displaySetInstanceUIDs || [];
 
        const isCleared = updatedDisplaySetUIDs.length === 0;
 
        Iif (isCleared) {
          removedViewportIds.push(viewport.viewportId);
        }
      } else E{
        removedViewportIds.push(viewport.viewportId);
      }
    }
 
    setTimeout(() => {
      this._broadcastEvent(ViewportGridService.EVENTS.GRID_STATE_CHANGED, {
        state,
        viewports: updatedViewports,
        removedViewportIds,
      });
    });
  }
 
  /**
   * Retrieves the display set instance UIDs for a given viewport.
   * @param viewportId The ID of the viewport.
   * @returns An array of display set instance UIDs.
   */
  public getDisplaySetsUIDsForViewport(viewportId: string) {
    const state = this.getState();
    const viewport = state.viewports.get(viewportId);
    return viewport?.displaySetInstanceUIDs;
  }
 
  /**
   *
   * @param numCols, numRows - the number of columns and rows to apply
   * @param findOrCreateViewport is a function which takes the
   *    index position of the viewport, the position id, and a set of
   *    options that is initially provided as {} (eg to store intermediate state)
   *    The function returns a viewport object to use at the given position.
   */
  public async setLayout({
    numCols,
    numRows,
    layoutOptions,
    layoutType = 'grid',
    activeViewportId = undefined,
    findOrCreateViewport = undefined,
    isHangingProtocolLayout = false,
  }) {
    // Get the previous state before the layout change
    const prevState = this.getState();
    const prevViewportIds = new Set(prevState.viewports.keys());
 
    await this.serviceImplementation._setLayout({
      numCols,
      numRows,
      layoutOptions,
      layoutType,
      activeViewportId,
      findOrCreateViewport,
      isHangingProtocolLayout,
    });
 
    // Use queueMicrotask to ensure the layout changed event is published after
    setTimeout(() => {
      // Get the new state after the layout change
      const state = this.getState();
      const currentViewportIds = new Set(state.viewports.keys());
 
      // Determine which viewport IDs have been removed
      const removedViewportIds = [...prevViewportIds].filter(id => !currentViewportIds.has(id));
 
      this._broadcastEvent(this.EVENTS.LAYOUT_CHANGED, {
        numCols,
        numRows,
      });
 
      this._broadcastEvent(this.EVENTS.GRID_STATE_CHANGED, {
        state,
        removedViewportIds,
      });
    }, 0);
  }
 
  public reset() {
    this.serviceImplementation._reset();
  }
 
  /**
   * The onModeExit must set the state of the viewport grid to a standard/clean
   * state.  To implement store/recover of the viewport grid, perform
   * a state store in the mode or extension onModeExit, and recover that
   * data if appropriate in the onModeEnter of the mode or extension.
   */
  public onModeExit(): void {
    this.serviceImplementation._onModeExit();
  }
 
  public set(newState) {
    const prevState = this.getState();
    const prevViewportIds = new Set(prevState.viewports.keys());
 
    this.serviceImplementation._set(newState);
 
    const state = this.getState();
    const currentViewportIds = new Set(state.viewports.keys());
 
    const removedViewportIds = [...prevViewportIds].filter(id => !currentViewportIds.has(id));
 
    setTimeout(() => {
      this._broadcastEvent(this.EVENTS.GRID_STATE_CHANGED, {
        state,
        removedViewportIds,
      });
    }, 0);
  }
 
  public getNumViewportPanes() {
    return this.serviceImplementation._getNumViewportPanes();
  }
 
  public getLayoutOptionsFromState(
    state: any
  ): { x: number; y: number; width: number; height: number }[] {
    return Array.from(state.viewports.entries()).map(([_, viewport]) => {
      return {
        x: viewport.x,
        y: viewport.y,
        width: viewport.width,
        height: viewport.height,
      };
    });
  }
}
 
export default ViewportGridService;