<commit_before>goog.provide('hutmap.map.Map');

goog.require('goog.array');
goog.require('goog.debug.Logger');
goog.require('goog.dom');
goog.require('goog.iter');
goog.require('goog.structs');
goog.require('goog.structs.Map');

goog.require('Wkt.Wkt');
goog.require('Wkt.gmap3');
goog.require('hutmap.History');
goog.require('hutmap.Huts');
goog.require('hutmap.map.Sidebar');
goog.require('hutmap.map.Types');
goog.require('markerclusterer.MarkerClusterer');

/**
 * Wraps the google.maps.Map class with hutmap specific functionality.
 *
 * @param {Object} mapIds an object containing ids of elements needed by the
 *    map. This object should have the following properties: 'mapDivId',
 *    'sidebarDivId', 'sidebarContentDivid', 'sidebarToggleDivId', and
 *    'sidebarToggleIconDivId'.
 * @param {hutmap.Huts} huts 
 * @constructor
 */
hutmap.map.Map = function(mapIds, huts) {
  /**
   * Logger for the map.
   *
   * @type goog.debug.Logger
   */
  this.logger = goog.debug.Logger.getLogger('hutmap.map.Map');
  /**
   * Object containing element ids.
   *
   * @type Object
   */
  this.mapIds = mapIds;
  /**
   * Reference to the huts object which is a container for all huts currently
   * loaded.
   *
   * @type Object
   */
  this.huts = huts;
  /**
   * The google map object.
   *
   * @type google.maps.Map
   */
  this.gmap = new google.maps.Map(goog.dom.getElement(mapIds.mapDivId), {
    zoom: 3,
    center:  new google.maps.LatLng(47.6, -122.3), 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
  }); 
  /**
   * A map from hut id to google.maps.Marker
   *
   * @type goog.structs.Map
   */
  this.markers = new goog.structs.Map();
  /**
   * A clusterer for the markers on the map.
   *
   * @type MarkerClusterer
   */
  this.markerClusterer = new MarkerClusterer(this.gmap);
  /**
   * The currently selected marker.
   *
   * @type google.maps.Marker
   */
  this.selectedMarker = null;
  /**
   * A rectangle showing the bounds of huts that are currently shown.
   *
   * @type google.maps.Rectangle
   */
  this.rectangle = new google.maps.Rectangle({
    'map': this.gmap,
    'clickable': false,
    'editable': false,
    'visible': false,
    'fillColor': 'black',
    'fillOpacity': 0.0,
    'strokeColor': 'black',
    'strokeOpacity': 0.5
  });
  /**
   * The Sidebar object which allows updating the hut display on the sidebar.
   *
   * @type Object
   */
  this.sidebar = new hutmap.map.Sidebar(mapIds.sidebarContentDivId);


  // set up additional map layers
  var mapTypes = new hutmap.map.Types(this.gmap);

  this.gmap.mapTypes.set(mapTypes.MSR_TOPO.name, mapTypes.MSR_TOPO);
  this.gmap.mapTypes.set(mapTypes.ARC_GIS_USA.name, mapTypes.ARC_GIS_USA);
  this.gmap.mapTypes.set(mapTypes.ARC_GIS_WORLD.name, mapTypes.ARC_GIS_WORLD);

  this.gmap.setOptions({
    mapTypeControlOptions: {
      position: google.maps.ControlPosition.RIGHT_TOP,
      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
      mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, 
                   google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN, 
                   mapTypes.MSR_TOPO.name, mapTypes.ARC_GIS_USA.name, 
                   mapTypes.ARC_GIS_WORLD.name]
    }
  });

  // attach listener to when new huts are loaded
  goog.events.listen(this.huts, hutmap.Huts.EventType.HUTS_CHANGED,
      goog.bind(this.onHutsChanged, this));
  
  // attach listener to hide sidebar
  var sidebarToggle = goog.dom.getElement(mapIds.sidebarToggleDivId);
  goog.events.listen(sidebarToggle, goog.events.EventType.CLICK,
      goog.bind(this.toggleSidebar, this));
};

hutmap.map.Map.RED_MARKER = 'http://maps.google.com/intl/en_us/mapfiles/ms/micons/red.png';
hutmap.map.Map.BLUE_MARKER = 'http://maps.google.com/intl/en_us/mapfiles/ms/micons/blue.png';

/**
 * Clears all hut markers from the map.
 */
hutmap.map.Map.prototype.clearHuts = function() {
  this.markerClusterer.removeMarkers(this.markers.getValues());
  this.markers.clear();
};

/**
 * Adds huts to the map. If the map already contains a given hut, will not add
 * it again.
 *
 * @param {Object[]} huts The huts to add to the map.
 */
hutmap.map.Map.prototype.addHuts = function(huts) {
  if (!goog.array.isEmpty(huts)) {
    var self = this;
    var newMarkers = [];
    goog.array.forEach(
      huts, 
      function(hut, index, array) {
        var position = new Wkt.Wkt(hut.location);
        var marker = position.toObject({
          visible: true,
          title: hut.name,
          icon: new google.maps.MarkerImage(hutmap.map.Map.RED_MARKER)
        });
        this.setCallbacks(marker, hut);
        if (!this.markers.containsKey(hut.id)) {
          this.markers.set(hut.id, marker);
          newMarkers.push(marker);
        }
      },
      self);
    this.markerClusterer.addMarkers(newMarkers);
  }
};<commit_msg>Updated addHuts to do less work if it gets a hut it already has<commit_after>goog.provide('hutmap.map.Map');

goog.require('goog.array');
goog.require('goog.debug.Logger');
goog.require('goog.dom');
goog.require('goog.iter');
goog.require('goog.structs');
goog.require('goog.structs.Map');

goog.require('Wkt.Wkt');
goog.require('Wkt.gmap3');
goog.require('hutmap.History');
goog.require('hutmap.Huts');
goog.require('hutmap.map.Filter');
goog.require('hutmap.map.Sidebar');
goog.require('hutmap.map.Types');
goog.require('markerclusterer.MarkerClusterer');

/**
 * Wraps the google.maps.Map class with hutmap specific functionality.
 *
 * @param {Object} mapIds an object containing ids of elements needed by the
 *    map. This object should have the following properties: 'mapDivId',
 *    'sidebarDivId', 'sidebarContentDivid', 'sidebarToggleDivId',
 *    'sidebarToggleIconDivId', and 'filterDivId'.
 * @param {hutmap.Huts} huts 
 * @constructor
 */
hutmap.map.Map = function(mapIds, huts) {
  /**
   * Logger for the map.
   *
   * @type goog.debug.Logger
   */
  this.logger = goog.debug.Logger.getLogger('hutmap.map.Map');
  /**
   * Object containing element ids.
   *
   * @type Object
   */
  this.mapIds = mapIds;
  /**
   * Reference to the huts object which is a container for all huts currently
   * loaded.
   *
   * @type Object
   */
  this.huts = huts;
  /**
   * The google map object.
   *
   * @type google.maps.Map
   */
  this.gmap = new google.maps.Map(goog.dom.getElement(mapIds.mapDivId), {
    zoom: 3,
    center:  new google.maps.LatLng(47.6, -122.3), 
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    streetViewControl: false,
    panControl: false,
    /*
    mapTypeControlOptions: {
      position: google.maps.ControlPosition.RIGHT_TOP,
      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
    },
    */
    zoomControlOptions: { 
      position: google.maps.ControlPosition.RIGHT_TOP 
    }
  }); 
  /**
   * A map from hut id to google.maps.Marker
   *
   * @type goog.structs.Map
   */
  this.markers = new goog.structs.Map();
  /**
   * A clusterer for the markers on the map.
   *
   * @type MarkerClusterer
   */
  this.markerClusterer = new MarkerClusterer(this.gmap);
  /**
   * The currently selected marker.
   *
   * @type google.maps.Marker
   */
  this.selectedMarker = null;
  /**
   * A rectangle showing the bounds of huts that are currently shown.
   *
   * @type google.maps.Rectangle
   */
  this.rectangle = new google.maps.Rectangle({
    'map': this.gmap,
    'clickable': false,
    'editable': false,
    'visible': false,
    'fillColor': 'black',
    'fillOpacity': 0.0,
    'strokeColor': 'black',
    'strokeOpacity': 0.5
  });
  /**
   * The Sidebar object which allows updating the hut display on the sidebar.
   *
   * @type Object
   */
  this.sidebar = new hutmap.map.Sidebar(mapIds.sidebarContentDivId);
  /**
   * The Filter object which allows filtering the huts displayed on the map.
   *
   * @type Object
   */
  this.filter = new hutmap.map.Filter(mapIds.filterDivId);


  // set up additional map layers
  var mapTypes = new hutmap.map.Types(this.gmap);

  this.gmap.mapTypes.set(mapTypes.MSR_TOPO.name, mapTypes.MSR_TOPO);
  this.gmap.mapTypes.set(mapTypes.ARC_GIS_USA.name, mapTypes.ARC_GIS_USA);
  this.gmap.mapTypes.set(mapTypes.ARC_GIS_WORLD.name, mapTypes.ARC_GIS_WORLD);

  this.gmap.setOptions({
    mapTypeControlOptions: {
      mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, 
                   google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN, 
                   mapTypes.MSR_TOPO.name, mapTypes.ARC_GIS_USA.name, 
                   mapTypes.ARC_GIS_WORLD.name]
    }
  });

  // attach listener to when new huts are loaded
  goog.events.listen(this.huts, hutmap.Huts.EventType.HUTS_CHANGED,
      goog.bind(this.onHutsChanged, this));
  
  // attach listener to hide sidebar
  var sidebarToggle = goog.dom.getElement(mapIds.sidebarToggleDivId);
  goog.events.listen(sidebarToggle, goog.events.EventType.CLICK,
      goog.bind(this.toggleSidebar, this));
};

hutmap.map.Map.RED_MARKER = 'http://maps.google.com/intl/en_us/mapfiles/ms/micons/red.png';
hutmap.map.Map.BLUE_MARKER = 'http://maps.google.com/intl/en_us/mapfiles/ms/micons/blue.png';

/**
 * Clears all hut markers from the map.
 */
hutmap.map.Map.prototype.clearHuts = function() {
  this.markerClusterer.removeMarkers(this.markers.getValues());
  this.markers.clear();
};

/**
 * Adds huts to the map. If the map already contains a given hut, will not add
 * it again.
 *
 * @param {Object[]} huts The huts to add to the map.
 */
hutmap.map.Map.prototype.addHuts = function(huts) {
  if (!goog.array.isEmpty(huts)) {
    var self = this;
    var newMarkers = [];
    goog.array.forEach(
      huts, 
      function(hut, index, array) {
        if (!this.markers.containsKey(hut.id)) {
          var position = new Wkt.Wkt(hut.location);
          var marker = position.toObject({
            visible: true,
            title: hut.name,
            icon: new google.maps.MarkerImage(hutmap.map.Map.RED_MARKER)
          });
          this.setCallbacks(marker, hut);
          this.markers.set(hut.id, marker);
          newMarkers.push(marker);
        }
      },
      self);
    this.markerClusterer.addMarkers(newMarkers);
  }
};
{'fraction_non_alphanumeric': 0.13984298331697742, 'fraction_numerical': 0.0025515210991167814, 'mean_word_length': 3.020118343195266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4982776', 'n_tokens_mistral': 3673, 'n_tokens_neox': 3553, 'n_words': 682}