<commit_before>var GM;

Map = {
  
  
  // == MAP LAYERS == //
  
  layer_calculated:  {},
  layer_visible:     {},
  layer_markers:     {},
  layer_managers:    {},
  layer_calculators: {},

  
  layer_forget: function(layer) {
    Map.layer_calculated[layer] = false;
    Map.layer_markers[layer] = {};
    Map.layer_managers[layer] && Map.layer_managers[layer].clearMarkers();
  },
  
  layer_hide: function(layer) {
    Map.layer_managers[layer] && Map.layer_managers[layer].hide();
    Map.layer_visible[layer] = false;
  },
  
  layer_show: function(layer) {
    if (!Map.layer_managers[layer]) {
      Map.layer_managers[layer] = new MarkerManager(GM);  //{maxZoom: 19}
      Map.layer_managers[layer].addMarkers($values(Map.layer_markers[layer]), 0);
    }
    
    Map.layer_managers[layer].show();
    Map.layer_visible[layer] = true;
  },
  
  layer_calculate: function(layer) {
    Map.layer_markers[layer] = Map.layer_calculators[layer]();
    if (Map.layer_managers[layer]) {
      Map.layer_managers[layer].addMarkers($values(Map.layer_markers[layer]), 0);
    }
  },
  
  layer_bounds: function(layer) {
    var points = $values(Map.layer_markers[layer]);
    if (!points || points.length < 2) return null;
    var bounds = new GLatLngBounds(points[0].getLatLng(), points[0].getLatLng());
    for (var i=1; i < points.length; i++) bounds.extend(points[i].getLatLng());
    return bounds;
  },

  layer_center: function(layer) {
    var points = $values(Map.layer_markers[layer]);
    if (points.length == 0) return null;
    else if (points.length == 0) return points[0];
    else return Map.layer_bounds(layer).getCenter();
  },
  
  layer_size: function(layer) {
    return $values(Map.layer_markers[layer]).length;
  },
  
  layer_recalculate: function(layer) {
    if (!Map.layer_visible[layer]) return Map.layer_forget(layer);
    Map.layer_hide(layer);
    Map.layer_forget(layer);
    Map.layer_calculate(layer);
    Map.layer_show(layer);
  },
  
  layer_refresh_icons: function(layer) {
    if (!Map.layer_markers[layer]) return;
    $.each($keys(Map.layer_markers[layer]), function(){
      var site = this;
      var guy = site.resource();
      if (!guy) return;
      Item.calculate_fields(guy);
      Map.site_set_image(layer, site, MapIcons.for_type(guy.map_icon).image);
    });
  },
  
  
  


  // == MAP ZOOMING == //

  zoom_to_bounds: function(bounds) {
    GM.setCenter(bounds.getCenter(), GM.getBoundsZoomLevel(bounds));
  },
  
  zoom_to_marker: function(point, zoom) {
    GM.setCenter(point.getLatLng(), zoom || 15);
  },
  
  zoom_to_layer: function(layer, zoom) {
    var size = Map.layer_size(layer);
    if (size == 1) Map.zoom_to_marker($values(Map.layer_markers[layer])[0], zoom);
    else Map.zoom_to_bounds(Map.layer_bounds(layer));
  },
  
  

  // == MAP FOCUS == //
  // TODO: move this to a domain-specific file
  
  set_focus_worldwide: function() {
    Map.layer_hide('agents');
    Map.layer_hide('landmarks');
    Map.layer_hide('cities');
    Map.layer_forget('agents');
    Map.layer_forget('landmarks');
    Map.layer_forget('cities');
    Map.layer_calculate('cities');
    Map.zoom_to_layer('cities', 3);
    Map.layer_show('cities');
  },
  
  set_focus_on_city: function() {
    Map.layer_hide('cities');
    Map.layer_calculate('agents');
    Map.layer_calculate('landmarks');
    
    // set bounds...
    var agents_count = Map.layer_size('agents');
    var landmarks_count = Map.layer_size('landmarks');
    if (agents_count > 0) Map.zoom_to_layer('agents');
    else if (landmarks_count > 0) Map.zoom_to_layer('landmarks');
    else {
      var city_ll = city_locs[This.city_id];
      GM.setCenter(new GLatLng(city_ll[0], city_ll[1]), 15);
    }
    
    Map.layer_show('agents');
    Map.layer_show('landmarks');
  },
  
  
  
  // == MAP SITES == //
  <commit_msg>clear selection when city changes<commit_after>var GM;

Map = {
  
  
  // == MAP LAYERS == //
  
  layer_calculated:  {},
  layer_visible:     {},
  layer_markers:     {},
  layer_managers:    {},
  layer_calculators: {},

  
  layer_forget: function(layer) {
    Map.layer_calculated[layer] = false;
    Map.layer_markers[layer] = {};
    Map.layer_managers[layer] && Map.layer_managers[layer].clearMarkers();
  },
  
  layer_hide: function(layer) {
    Map.layer_managers[layer] && Map.layer_managers[layer].hide();
    Map.layer_visible[layer] = false;
  },
  
  layer_show: function(layer) {
    if (!Map.layer_managers[layer]) {
      Map.layer_managers[layer] = new MarkerManager(GM);  //{maxZoom: 19}
      Map.layer_managers[layer].addMarkers($values(Map.layer_markers[layer]), 0);
    }
    
    Map.layer_managers[layer].show();
    Map.layer_visible[layer] = true;
  },
  
  layer_calculate: function(layer) {
    Map.layer_markers[layer] = Map.layer_calculators[layer]();
    if (Map.layer_managers[layer]) {
      Map.layer_managers[layer].addMarkers($values(Map.layer_markers[layer]), 0);
    }
  },
  
  layer_bounds: function(layer) {
    var points = $values(Map.layer_markers[layer]);
    if (!points || points.length < 2) return null;
    var bounds = new GLatLngBounds(points[0].getLatLng(), points[0].getLatLng());
    for (var i=1; i < points.length; i++) bounds.extend(points[i].getLatLng());
    return bounds;
  },

  layer_center: function(layer) {
    var points = $values(Map.layer_markers[layer]);
    if (points.length == 0) return null;
    else if (points.length == 0) return points[0];
    else return Map.layer_bounds(layer).getCenter();
  },
  
  layer_size: function(layer) {
    return $values(Map.layer_markers[layer]).length;
  },
  
  layer_recalculate: function(layer) {
    if (!Map.layer_visible[layer]) return Map.layer_forget(layer);
    Map.layer_hide(layer);
    Map.layer_forget(layer);
    Map.layer_calculate(layer);
    Map.layer_show(layer);
  },
  
  layer_refresh_icons: function(layer) {
    if (!Map.layer_markers[layer]) return;
    $.each($keys(Map.layer_markers[layer]), function(){
      var site = this;
      var guy = site.resource();
      if (!guy) return;
      Item.calculate_fields(guy);
      Map.site_set_image(layer, site, MapIcons.for_type(guy.map_icon).image);
    });
  },
  
  
  


  // == MAP ZOOMING == //

  zoom_to_bounds: function(bounds) {
    GM.setCenter(bounds.getCenter(), GM.getBoundsZoomLevel(bounds));
  },
  
  zoom_to_marker: function(point, zoom) {
    GM.setCenter(point.getLatLng(), zoom || 15);
  },
  
  zoom_to_layer: function(layer, zoom) {
    var size = Map.layer_size(layer);
    if (size == 1) Map.zoom_to_marker($values(Map.layer_markers[layer])[0], zoom);
    else Map.zoom_to_bounds(Map.layer_bounds(layer));
  },
  
  

  // == MAP FOCUS == //
  // TODO: move this to a domain-specific file
  
  set_focus_worldwide: function() {
    Selection.clear();
    Map.layer_hide('agents');
    Map.layer_hide('landmarks');
    Map.layer_hide('cities');
    Map.layer_forget('agents');
    Map.layer_forget('landmarks');
    Map.layer_forget('cities');
    Map.layer_calculate('cities');
    Map.zoom_to_layer('cities', 3);
    Map.layer_show('cities');
  },
  
  set_focus_on_city: function() {
    Selection.clear();
    Map.layer_hide('cities');
    Map.layer_calculate('agents');
    Map.layer_calculate('landmarks');
    
    // set bounds...
    var agents_count = Map.layer_size('agents');
    var landmarks_count = Map.layer_size('landmarks');
    if (agents_count > 0) Map.zoom_to_layer('agents');
    else if (landmarks_count > 0) Map.zoom_to_layer('landmarks');
    else {
      var city_ll = city_locs[This.city_id];
      GM.setCenter(new GLatLng(city_ll[0], city_ll[1]), 15);
    }
    
    Map.layer_show('agents');
    Map.layer_show('landmarks');
  },
  
  
  
  // == MAP SITES == //
  
{'fraction_non_alphanumeric': 0.16098191214470284, 'fraction_numerical': 0.005684754521963824, 'mean_word_length': 3.436103151862464, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2181797', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2760, 'n_words': 412}