<commit_before>import config from '../config/config';
import getIcon from '../libs/get-icon';

/* global google */

export default class {
  /**
   * Constructs the map
   */
  constructor({
    onHotspotClick = () => {}
  }) {
    const $container = document.querySelector('.map'),
      options = {
        center: config.defaultLocation,
        disableDefaultUI: true,
        zoomControl: true,
        zoomControlOptions: {
          position: google.maps.ControlPosition.LEFT_BOTTOM
        },
        zoom: 15
      };

    this.mapCanvas = new google.maps.Map($container, options);
    this.markers = [];
    this.onHotspotClick = onHotspotClick;
  }

  /**
   * Show the users position
   * @param {Object} position The position of the user
   */
  showUserPosition(position) {
    if (!position && this.userMarker) {
      position = this.userMarker.getPosition();
    }

    this.mapCanvas.panTo(position);

    if (this.userMarker) {
      this.userMarker.setPosition(position);
      this.userMarker.setMap(this.mapCanvas);
      return;
    }

    this.userMarker = new google.maps.Marker({
      map: this.mapCanvas,
      clickable: false,
      zIndex: -1,
      icon: getIcon('user'),
      position
    });
  }

  /**
   * Update the hotspots
   * @param  {String} currentFilter The current selected filter
   */
  updateHotspots(currentFilter) {
    if (currentFilter === 'all') {
      this.markers.forEach(marker => marker.setVisible(true));
      return;
    }

    this.markers.forEach(marker => {
      const isActive = currentFilter === marker.hotspot.category;

      marker.setVisible(isActive);
    });
  }

  /**
   * Add a marker for every hotspot
   * @param {Object} hotspots Array with the hotspots infos
   */
  addHotspots(hotspots) {
    hotspots.forEach(hotspot => {
      const position = {
          lat: parseFloat(hotspot.position.lat),
          lng: parseFloat(hotspot.position.lng)
        },
        icon = getIcon(hotspot.category),
        background = new google.maps.Marker({
          map: this.mapCanvas,
          icon: config.markerBackground,
          position
        }),
        marker = new google.maps.Marker({
          map: this.mapCanvas,
          clickable: false,
          icon,
          position
        });

      background.hotspot = hotspot;
      marker.hotspot = hotspot;

      background.addListener('click', () => {
        this.onHotspotClick(hotspot);
      });

      this.markers.push(background);
      this.markers.push(marker);
    });
  }

  /**
   * Set the center of the map
   * @param {Object} position The position which should be centered
   */
  setCenter(position) {
    this.mapCanvas.setCenter(position);
  }
}<commit_msg>feat(map): zoom to hotspots bounds on load<commit_after>import config from '../config/config';
import getIcon from '../libs/get-icon';

/* global google */

export default class {
  /**
   * Constructs the map
   */
  constructor({
    onHotspotClick = () => {}
  }) {
    const $container = document.querySelector('.map'),
      options = {
        center: config.defaultLocation,
        disableDefaultUI: true,
        zoomControl: true,
        zoomControlOptions: {
          position: google.maps.ControlPosition.LEFT_BOTTOM
        },
        zoom: 15
      };

    this.mapCanvas = new google.maps.Map($container, options);
    this.markers = [];
    this.onHotspotClick = onHotspotClick;
  }

  /**
   * Show the users position
   * @param {Object} position The position of the user
   */
  showUserPosition(position) {
    if (!position && this.userMarker) {
      position = this.userMarker.getPosition();
    }

    this.mapCanvas.panTo(position);

    if (this.userMarker) {
      this.userMarker.setPosition(position);
      this.userMarker.setMap(this.mapCanvas);
      return;
    }

    this.userMarker = new google.maps.Marker({
      map: this.mapCanvas,
      clickable: false,
      zIndex: -1,
      icon: getIcon('user'),
      position
    });
  }

  /**
   * Update the hotspots
   * @param  {String} currentFilter The current selected filter
   */
  updateHotspots(currentFilter) {
    if (currentFilter === 'all') {
      this.markers.forEach(marker => marker.setVisible(true));
      return;
    }

    this.markers.forEach(marker => {
      const isActive = currentFilter === marker.hotspot.category;

      marker.setVisible(isActive);
    });
  }

  /**
   * Add a marker for every hotspot
   * @param {Object} hotspots Array with the hotspots infos
   */
  addHotspots(hotspots) {
    const bounds = new google.maps.LatLngBounds();

    hotspots.forEach(hotspot => {
      const position = new google.maps.LatLng(
          parseFloat(hotspot.position.lat),
          parseFloat(hotspot.position.lng)
        ),
        icon = getIcon(hotspot.category),
        background = new google.maps.Marker({
          map: this.mapCanvas,
          icon: config.markerBackground,
          position
        }),
        marker = new google.maps.Marker({
          map: this.mapCanvas,
          clickable: false,
          icon,
          position
        });

      background.hotspot = hotspot;
      marker.hotspot = hotspot;

      bounds.extend(position);

      background.addListener('click', () => {
        this.onHotspotClick(hotspot);
      });

      this.markers.push(background);
      this.markers.push(marker);
    });

    this.mapCanvas.panToBounds(bounds);
  }

  /**
   * Set the center of the map
   * @param {Object} position The position which should be centered
   */
  setCenter(position) {
    this.mapCanvas.setCenter(position);
  }
}
{'fraction_non_alphanumeric': 0.12388432702606213, 'fraction_numerical': 0.0010710460549803642, 'mean_word_length': 2.313424009461857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4121724', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1628, 'n_words': 342}