All files / si-map/services SelectCluster.ts

71.42% Statements 15/21
12.5% Branches 1/8
85.71% Functions 6/7
71.42% Lines 15/21

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        1x 1x   1x     1x                     19x   19x 19x 19x 19x                                   1x 1x 1x 1x 1x       1x            
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { click } from 'ol/events/condition';
import { createEmpty as createEmptyExtent, extend } from 'ol/extent';
import Feature from 'ol/Feature';
import Select, { SelectEvent } from 'ol/interaction/Select';
import { StyleLike } from 'ol/style/Style';
 
export class SelectCluster extends Select {
  private zoomOnClick: () => boolean;
  private maxZoom: () => number;
  private fitClusterPadding: () => number[];
 
  constructor(options: {
    fitClusterPadding: () => number[];
    maxZoom: () => number;
    style?: StyleLike | null;
    zoomOnClick: () => boolean;
  }) {
    super({ ...options, condition: click });
 
    this.zoomOnClick = options.zoomOnClick;
    this.maxZoom = options.maxZoom;
    this.fitClusterPadding = options.fitClusterPadding;
    this.on('select', this.selectCluster.bind(this));
  }
 
  selectCluster(e: SelectEvent): void {
    const feature = e.selected.at(0);
 
    // Not a cluster (or just one feature)
    const cluster = feature?.get('features');
    if (!cluster || cluster.length === 1) {
      return;
    }
 
    if (this.zoomOnClick()) {
      this.zoomToCluster(cluster);
    }
  }
 
  zoomToCluster(cluster: Feature[]): void {
    const extent = createEmptyExtent();
    cluster.forEach(feature => {
      const geometry = feature.getGeometry();
      Eif (geometry) {
        extend(extent, geometry.getExtent());
      }
    });
 
    this.getMap()!.getView().fit(extent, {
      padding: this.fitClusterPadding(),
      maxZoom: this.maxZoom()
    });
  }
}