// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require foundation
//= require turbolinks
//= require leaflet
//= require_tree .

$(function() {
  $(document).foundation();

  // Map init
  var myMap = L.map('mapId').setView([48.7455, -3.4696], 13);
  mapLink =
    '<a href="http://openstreetmap.org">OpenStreetMap</a>';

  L.tileLayer(
    'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: 'Map data &copy; ' + mapLink,
      maxZoom: 18,
    }).addTo(myMap);

  var markers = [];

  $('form').on('submit', function(e) {

    var formData = $("#form :input")
      .filter(function(index, element) {
        return $(element).val() !== "";
      })
      .serialize();

    var url = "/legacies?" + formData;

    $.ajax({
      type: "GET",
      url: url,
      success: function(data) {
        clearMarkers();
        displayMarkers(data);
      }
    });

    e.preventDefault();

  });

  function displayMarkers(data) {

    data.forEach(function(element, index, array) {
      var marker = L.marker([element.latitude, element.longitude]);
      var info = element.lieu_dit !== "" ? element.lieu_dit : element.titre_courant;
      var text = parseInfo(element);

      marker.addTo(myMap)
        .bindPopup(text);
      markers.push(marker);
    });
  }

  function parseInfo(element) {
    var text = "";

    if (element.hasOwnProperty("adresse"))
      text += "Adresse: " + element.adresse + "<br>";

    if (element.hasOwnProperty("cadre_etude"))
      text += "Cadre d'etude: " + element.cadre_etude + "<br>";

    if (element.hasOwnProperty("commune"))
      text += "Commune: " + element.commune + "<br>";

    if (element.hasOwnProperty("datations_principales"))
      text += "Datations: " + element.datations_principales + "<br>";

    if (element.hasOwnProperty("date_enquete"))
      text += "Date Enquete: " + element.date_enquete + "<br>";

    if (element.hasOwnProperty("denomination"))
      text += "Denomination: " + element.denomination + "<br>";

    if (element.hasOwnProperty("lieu_dit"))
      text += "Lieu dit: " + element.lieu_dit + "<br>";

    if (element.hasOwnProperty("phase"))
      text += "Phase: " + element.phase + "<br>";

    if (element.hasOwnProperty("statut"))
      text += "Statut: " + element.statut + "<br>";

    if (element.hasOwnProperty("lien_image"))
      text += '<img src=' + element.lien_image + ' alt=""><br>';

    return text;
  }

  function clearMarkers() {
    markers.forEach(function(element, index, array) {
      myMap.removeLayer(element);
    });
  }

  function onMapClick(e) {
    var radius = 2000;
    var url = "/legacies?latitude=" + e.latlng.lat + "&longitude=" + e.latlng
      .lng + "&radius=" + radius;


    $.ajax({
      type: "GET",
      url: url,
      success: function(data) {
        clearMarkers();
        var circle = L.circle([e.latlng.lat, e.latlng.lng], radius, {
          color: 'orange',
          fillColor: '#FFA500',
          fillOpacity: 0.2
        }).addTo(myMap);
        markers.push(circle);
        displayMarkers(data);
      }
    });



  }

  myMap.on('click', onMapClick);

});

@@ -1929,33 +1929,24 @@
 ent.
-hasOwnProperty(%22adresse%22)
+adresse !== null
 )%0A  
@@ -2014,32 +2014,16 @@
 element.
-hasOwnProperty(%22
 cadre_et
@@ -2025,18 +2025,25 @@
 re_etude
-%22)
+ !== null
 )%0A      
@@ -2121,33 +2121,24 @@
 ent.
-hasOwnProperty(%22commune%22)
+commune !== null
 )%0A  
@@ -2206,32 +2206,16 @@
 element.
-hasOwnProperty(%22
 datation
@@ -2227,18 +2227,25 @@
 ncipales
-%22)
+ !== null
 )%0A      
@@ -2325,32 +2325,16 @@
 element.
-hasOwnProperty(%22
 date_enq
@@ -2337,18 +2337,25 @@
 _enquete
-%22)
+ !== null
 )%0A      
@@ -2429,32 +2429,16 @@
 element.
-hasOwnProperty(%22
 denomina
@@ -2441,18 +2441,25 @@
 mination
-%22)
+ !== null
 )%0A      
@@ -2537,34 +2537,25 @@
 ent.
-hasOwnProperty(%22lieu_dit%22)
+lieu_dit !== null
 )%0A  
@@ -2629,31 +2629,22 @@
 ent.
-hasOwnProperty(%22phase%22)
+phase !== null
 )%0A  
@@ -2712,32 +2712,23 @@
 ent.
-hasOwnProperty(%22statut%22)
+statut !== null
 )%0A  
@@ -2798,24 +2798,8 @@
 ent.
-hasOwnProperty(%22
 lien
@@ -2804,18 +2804,25 @@
 en_image
-%22)
+ !== null
 )%0A      
@@ -3055,24 +3055,172 @@
 pClick(e) %7B%0A
+    var formData = $(%22#form :input%22)%0A      .filter(function(index, element) %7B%0A        return $(element).val() !== %22%22;%0A      %7D)%0A      .serialize();%0A%0A
     var radi
@@ -3255,16 +3255,33 @@
 egacies?
+%22 + formData + %22&
 latitude
@@ -3299,16 +3299,22 @@
 ng.lat +
+%0A     
  %22&longi
@@ -3330,23 +3330,16 @@
 e.latlng
-%0A      
 .lng + %22
@@ -3722,18 +3722,16 @@
    %7D);%0A%0A
-%0A%0A
   %7D%0A%0A  m

Improve null detection and fix radius request