<commit_before>/* global google,googleMapStyle,errorMessage */
var map;
var markers = [];
var pacInput = document.getElementById('place');
var autocomplete;

/**
 * Initialize the map
 * async triggered on google map script load
 */
function initMap() {
  var geocode = JSON.parse(document.getElementById('place').getAttribute('data-json'));
  map = new google.maps.Map(document.getElementById('map'), {
    center: geocode.geometry.location,
    clickableIcons: false,
    disableDefaultUI: true,
    keyboardShortcuts: false,
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    maxZoom: 15,
    minZoom: 4,
    zoomControl: true,
    zoomControlOptions: {
      position: google.maps.ControlPosition.TOP_LEFT
    },
    styles: googleMapStyle,
    zoom: 10
  });
  if (geocode.geometry.viewport) {
    var resultBounds = new google.maps.LatLngBounds(
        geocode.geometry.viewport.southwest,
        geocode.geometry.viewport.northeast
    );
    map.fitBounds(resultBounds);
  }
  initAutocomplete();
  var videos = [];
  Array.from(document.getElementsByClassName('videolist-media')).forEach(function(element) {
    videos.push(JSON.parse(element.getAttribute('data-json')));
  });
  initMarkers(videos);
  google.maps.event.addListenerOnce(map, 'idle', function() {
    map.addListener('dragend', refreshVideoList);
    map.addListener('zoom_changed', refreshVideoList);
  });
}

/**
 * Initialize place input field with google places autocomplete api
 */
function initAutocomplete() {
  (function pacSelectFirst(input) {
    var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent;

    function addEventListenerWrapper(type, listener) {
      if (type === 'keydown') {
        var orig_listener = listener;
        listener = function(event) {
          var suggestion_selected = $('.pac-item-selected').length > 0;
          if (event.which === 13 && !suggestion_selected) {
            var simulated_downarrow = $.Event('keydown', {
              keyCode: 40,
              which: 40
            });
            orig_listener.apply(input, [simulated_downarrow]);
          }
          orig_listener.apply(input, [event]);
        };
      }
      _addEventListener.apply(input, [type, listener]);
    }

    input.addEventListener = addEventListenerWrapper;
    input.attachEvent = addEventListenerWrapper;
  })(pacInput);
  autocomplete = new google.maps.places.Autocomplete(pacInput);
  autocomplete.addListener('place_changed', function() {
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      return;
    }
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.panTo(place.geometry.location);
      map.setZoom(10);
    }
  });
}

/**
 * Init map markers
 * triggered on load and after populating videolist-medias dom element
 */
function initMarkers(videos) {
  videos.forEach(function(data) {
    var marker = new google.maps.Marker({
      position: data.location,
      map: map,
      title: data.title
    });
    if (data.icon) {
      marker.setLabel({
        fontFamily: 'Fontawesome',
        text: data.icon
      });
    }
    var element = document.querySelector('[data-id="' + data.youtubeId + '"]');
    marker.addListener('click', function(i) {
      $('#videolist-list').animate({
        scrollTop: element.getBoundingClientRect().top + element.parentElement.scrollTop - element.parentElement.getBoundingClientRect().top
      }, 500);
      var event = new MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true
      });
      element.dispatchEvent(event);
    });
    markers.push(marker);
  });
}

document.querySelector('#search-reset-params').addEventListener('click', resetSearchParams);

document.getElementById('search-save-params').addEventListener('click', function(event) {
  var data = {};
  document.getElementById('search-params').querySelectorAll('input, select').forEach(function(control) {
    if (control.value) {
      data[control.getAttribute('name')] = control.value;
    }
  });
  document.getElementById('paramsBtn').setAttribute('class', 'btn btn-primary');
  document.getElementById('paramsBtn').setAttribute('data-json', JSON.stringify(data));
  refreshVideoList();
});

$('#search-params')
    .on('show.bs.modal', function() {
      var data = JSON.parse(document.getElementById('paramsBtn').getAttribute('data-json'));
      loadSearchParams(data);
    })
    .on('shown.bs.modal', function() {
      document.getElementById('search-params-q').focus();
    });

function resetSearchParams() {
  document.querySelectorAll('#search-params select').forEach(function(select) {
    select.value = select.getAttribute('data-default');
  });
  document.querySelectorAll('#search-params input').forEach(function(input) {
    input.value = '';
  });
}

function loadSearchParams(data) {
  resetSearchParams();
  Object.keys(data).forEach(function(prop) {
    document.querySelector('#search-params [name=' + prop + ']').value = data[prop];
  });
}

document.querySelector('#search').addEventListener('submit', function(event) {
  event.preventDefault();
});

function playVideo(id) {
  var element = document.querySelector('.video[data-id="' + id + '"]');
  var data = JSON.parse(element.getAttribute('data-json'));
  $('#player-title').text(data.title);
  $('a.videolist-media.active').removeClass('active');
  element.classList.add('visited');
  element.classList.add('active');
  $('#player-video')
      .empty()
      .append('<iframe allowfullscreen class="embed-responsive-item" src="https://www.youtube.com/embed/' + id + '?rel=0&showinfo=0&autoplay=1" />');
  $('#player-meta-title').text(data.title);
  $('#player-headbar-title').text(data.title);
  $('#player-meta-timeago-value').text(data.timeago);
  $('#player-meta-description').html(data.description.replace(/(?:\r\n|\r|\n)/g, '<br />'));
  $('#player-meta-tags').empty();
  if (data.tags) {
    var tags = data.tags.map(Function.prototype.call, String.prototype.trim).filter(String);
    tags.forEach(function(tag) {
      $('#player-meta-tags').append('<span class=\'badge badge-secondary mr-1\'><i class=\'fa fa-tag mr-1\'></i>' + tag + '</span>');
    });
  }
  $('#player').show();
}

document.querySelector('#player-headbar-close').addEventListener('click', function() {
  history.pushState('', document.title, window.location.pathname + window.location.search);
  backToMap();
});

/**
 * Close the video and back to map
 */
function backToMap() {
  $('#player').hide();
  $('a.videolist-media.active').removeClass('active');
  $('#player-video').empty();
}

/**
 * Triggered when map bounds changed
 * refresh videolist
 */
function refreshVideoList() {
  spinSearch();
  var params = {
    lat: map.getCenter().lat,
    lng: map.getCenter().lng,
    boundlat: map.getBounds().getNorthEast().lat,
    boundlng: map.getBounds().getNorthEast().lng
  };
  var paramsBtn = $('#paramsBtn');
  if (paramsBtn.attr('data-json')) {
    params.params = paramsBtn.attr('data-json');
  }
  $.getJSON(
      '/search.json',
      params,
      function(data) {
        document.getElementById('videolist-medias').innerHTML = '';
        clearMarkers();
        if (data.nextPageToken) {
          $('#nextPage')
              .show()<commit_msg>fix remove hash when return to map<commit_after>/* global google,googleMapStyle,errorMessage */
var map;
var markers = [];
var pacInput = document.getElementById('place');
var autocomplete;

/**
 * Initialize the map
 * async triggered on google map script load
 */
function initMap() {
  var geocode = JSON.parse(document.getElementById('place').getAttribute('data-json'));
  map = new google.maps.Map(document.getElementById('map'), {
    center: geocode.geometry.location,
    clickableIcons: false,
    disableDefaultUI: true,
    keyboardShortcuts: false,
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    maxZoom: 15,
    minZoom: 4,
    zoomControl: true,
    zoomControlOptions: {
      position: google.maps.ControlPosition.TOP_LEFT
    },
    styles: googleMapStyle,
    zoom: 10
  });
  if (geocode.geometry.viewport) {
    var resultBounds = new google.maps.LatLngBounds(
        geocode.geometry.viewport.southwest,
        geocode.geometry.viewport.northeast
    );
    map.fitBounds(resultBounds);
  }
  initAutocomplete();
  var videos = [];
  Array.from(document.getElementsByClassName('videolist-media')).forEach(function(element) {
    videos.push(JSON.parse(element.getAttribute('data-json')));
  });
  initMarkers(videos);
  google.maps.event.addListenerOnce(map, 'idle', function() {
    map.addListener('dragend', refreshVideoList);
    map.addListener('zoom_changed', refreshVideoList);
  });
}

/**
 * Initialize place input field with google places autocomplete api
 */
function initAutocomplete() {
  (function pacSelectFirst(input) {
    var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent;

    function addEventListenerWrapper(type, listener) {
      if (type === 'keydown') {
        var orig_listener = listener;
        listener = function(event) {
          var suggestion_selected = $('.pac-item-selected').length > 0;
          if (event.which === 13 && !suggestion_selected) {
            var simulated_downarrow = $.Event('keydown', {
              keyCode: 40,
              which: 40
            });
            orig_listener.apply(input, [simulated_downarrow]);
          }
          orig_listener.apply(input, [event]);
        };
      }
      _addEventListener.apply(input, [type, listener]);
    }

    input.addEventListener = addEventListenerWrapper;
    input.attachEvent = addEventListenerWrapper;
  })(pacInput);
  autocomplete = new google.maps.places.Autocomplete(pacInput);
  autocomplete.addListener('place_changed', function() {
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      return;
    }
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.panTo(place.geometry.location);
      map.setZoom(10);
    }
  });
}

/**
 * Init map markers
 * triggered on load and after populating videolist-medias dom element
 */
function initMarkers(videos) {
  videos.forEach(function(data) {
    var marker = new google.maps.Marker({
      position: data.location,
      map: map,
      title: data.title
    });
    if (data.icon) {
      marker.setLabel({
        fontFamily: 'Fontawesome',
        text: data.icon
      });
    }
    var element = document.querySelector('[data-id="' + data.youtubeId + '"]');
    marker.addListener('click', function(i) {
      $('#videolist-list').animate({
        scrollTop: element.getBoundingClientRect().top + element.parentElement.scrollTop - element.parentElement.getBoundingClientRect().top
      }, 500);
      var event = new MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true
      });
      element.dispatchEvent(event);
    });
    markers.push(marker);
  });
}

document.querySelector('#search-reset-params').addEventListener('click', resetSearchParams);

document.getElementById('search-save-params').addEventListener('click', function(event) {
  var data = {};
  document.getElementById('search-params').querySelectorAll('input, select').forEach(function(control) {
    if (control.value) {
      data[control.getAttribute('name')] = control.value;
    }
  });
  document.getElementById('paramsBtn').setAttribute('class', 'btn btn-primary');
  document.getElementById('paramsBtn').setAttribute('data-json', JSON.stringify(data));
  refreshVideoList();
});

$('#search-params')
    .on('show.bs.modal', function() {
      var data = JSON.parse(document.getElementById('paramsBtn').getAttribute('data-json'));
      loadSearchParams(data);
    })
    .on('shown.bs.modal', function() {
      document.getElementById('search-params-q').focus();
    });

function resetSearchParams() {
  document.querySelectorAll('#search-params select').forEach(function(select) {
    select.value = select.getAttribute('data-default');
  });
  document.querySelectorAll('#search-params input').forEach(function(input) {
    input.value = '';
  });
}

function loadSearchParams(data) {
  resetSearchParams();
  Object.keys(data).forEach(function(prop) {
    document.querySelector('#search-params [name=' + prop + ']').value = data[prop];
  });
}

document.querySelector('#search').addEventListener('submit', function(event) {
  event.preventDefault();
});

function playVideo(id) {
  var element = document.querySelector('.video[data-id="' + id + '"]');
  var data = JSON.parse(element.getAttribute('data-json'));
  $('#player-title').text(data.title);
  $('a.videolist-media.active').removeClass('active');
  element.classList.add('visited');
  element.classList.add('active');
  $('#player-video')
      .empty()
      .append('<iframe allowfullscreen class="embed-responsive-item" src="https://www.youtube.com/embed/' + id + '?rel=0&showinfo=0&autoplay=1" />');
  $('#player-meta-title').text(data.title);
  $('#player-headbar-title').text(data.title);
  $('#player-meta-timeago-value').text(data.timeago);
  $('#player-meta-description').html(data.description.replace(/(?:\r\n|\r|\n)/g, '<br />'));
  $('#player-meta-tags').empty();
  if (data.tags) {
    var tags = data.tags.map(Function.prototype.call, String.prototype.trim).filter(String);
    tags.forEach(function(tag) {
      $('#player-meta-tags').append('<span class=\'badge badge-secondary mr-1\'><i class=\'fa fa-tag mr-1\'></i>' + tag + '</span>');
    });
  }
  $('#player').show();
}

document.querySelector('#player-headbar-close').addEventListener('click', backToMap);

/**
 * Close the video and back to map
 */
function backToMap() {
  history.pushState('', document.title, window.location.pathname + window.location.search);
  $('#player').hide();
  $('a.videolist-media.active').removeClass('active');
  $('#player-video').empty();
}

/**
 * Triggered when map bounds changed
 * refresh videolist
 */
function refreshVideoList() {
  spinSearch();
  var params = {
    lat: map.getCenter().lat,
    lng: map.getCenter().lng,
    boundlat: map.getBounds().getNorthEast().lat,
    boundlng: map.getBounds().getNorthEast().lng
  };
  var paramsBtn = $('#paramsBtn');
  if (paramsBtn.attr('data-json')) {
    params.params = paramsBtn.attr('data-json');
  }
  $.getJSON(
      '/search.json',
      params,
      function(data) {
        document.getElementById('videolist-medias').innerHTML = '';
        clearMarkers();
        if (data.nextPageToken) {
          $('#nextPage')
              .show()
{'fraction_non_alphanumeric': 0.16850242867893547, 'fraction_numerical': 0.0030101936101799274, 'mean_word_length': 3.6554140127388535, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '344151', 'n_tokens_mistral': 4392, 'n_tokens_neox': 4369, 'n_words': 710}