You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.5 KiB

5 years ago
  1. var urlroutes = "https://api.routes.fair.coop/api/";
  2. var routes = [];
  3. var routesMarkers = L.markerClusterGroup();
  4. function getRoute(id) {
  5. for(var i=0; i<routes.length; i++) {
  6. console.log(routes[i]._id);
  7. console.log(id);
  8. if(routes[i]._id==id) {
  9. return(routes[i]);
  10. }
  11. }
  12. return("not found");
  13. }
  14. function showRouteRightCard(route) {
  15. console.log(route);
  16. document.getElementById("rightCard-title").innerHTML = route.title;
  17. if(route.description!=null) {
  18. document.getElementById("rightCard-text").innerHTML = "<i>"+route.description+"</i>";
  19. }else{
  20. document.getElementById("rightCard-text").innerHTML = "";
  21. }
  22. var html = `<img style='width:30px;height:30px;'
  23. src='statics/img/categories/transport_black.svg'
  24. title="CommonRoute"/>`;
  25. document.getElementById("rightCard-categories").innerHTML = html;
  26. document.getElementById("rightCard-img").src="";
  27. document.getElementById("rightCard-img").className = "card-img-top hidden";
  28. document.getElementById("rightCard-web").href= "https://routes.fair.coop";
  29. document.getElementById("rightCard-web").innerHTML = "CommonRoutes website";
  30. document.getElementById("rightCard-mastodon").href="#";
  31. document.getElementById("rightCard-mastodon").innerHTML="";
  32. document.getElementById("rightCard-twitter").href="#";
  33. document.getElementById("rightCard-twitter").innerHTML="";
  34. document.getElementById("rightCard-facebook").href="#";
  35. document.getElementById("rightCard-facebook").innerHTML="";
  36. document.getElementById("rightCard-telegram").href="#";
  37. document.getElementById("rightCard-telegram").innerHTML="";
  38. document.getElementById("rightCard-email").href="#";
  39. document.getElementById("rightCard-email").innerHTML="";
  40. html = `
  41. <p>
  42. From: <b>` + route.from.name + `</b>
  43. </p>
  44. <p>
  45. To: <b>` + route.to.name + `</b>
  46. </p>
  47. <div class='float-right'>` + route.date + `</div>
  48. `;
  49. document.getElementById("rightCard-text").innerHTML += html;
  50. document.getElementById("rightCard").className+=" rightCard-show";
  51. }
  52. function updateRoutesMarkers() {
  53. routesMarkers.clearLayers();
  54. for(var i=0; i<routes.length; i++) {
  55. console.log(routes[i]);
  56. var lat = routes[i].from.lat;
  57. var lon = routes[i].from.long;
  58. var icon = L.divIcon({
  59. className: 'markerInvisible',
  60. iconAnchor: [20, 10],
  61. popupAnchor: [-4, 0], // point from which the popup should open relative to the iconAnchor
  62. html: "<img src='statics/img/gps.svg' style='width:30px;height:30px;' class='routeMarker' />"
  63. });
  64. var marker = L.marker([lat, lon], {icon: icon});
  65. marker.id = routes[i]._id;
  66. marker.bindPopup("From: <b>"+routes[i].from.name+"</b><br>" + routes[i].date);
  67. marker.on('mouseover', function(e) {
  68. this.openPopup();
  69. });
  70. marker.on('mouseout', function(e) {
  71. this.closePopup();
  72. });
  73. marker.on('click', function(e) {
  74. var route = getRoute(this.id);
  75. showRouteRightCard(JSON.parse(JSON.stringify(route)));
  76. });
  77. routesMarkers.addLayer(marker);
  78. var lat = routes[i].to.lat;
  79. var lon = routes[i].to.long;
  80. var icon = L.divIcon({
  81. className: 'markerInvisible',
  82. iconAnchor: [20, 10],
  83. popupAnchor: [-4, 0], // point from which the popup should open relative to the iconAnchor
  84. html: "<img src='statics/img/gps.svg' style='width:30px;height:30px;' class='routeMarker' />"
  85. });
  86. var marker = L.marker([lat, lon], {icon: icon});
  87. marker.id = routes[i]._id;
  88. marker.bindPopup("To: <b>"+routes[i].to.name+"</b><br>" + routes[i].date);
  89. marker.on('mouseover', function(e) {
  90. this.openPopup();
  91. });
  92. marker.on('mouseout', function(e) {
  93. this.closePopup();
  94. });
  95. marker.on('click', function(e) {
  96. var route = getRoute(this.id);
  97. showRouteRightCard(JSON.parse(JSON.stringify(route)));
  98. });
  99. routesMarkers.addLayer(marker);
  100. var polyline = L.polyline([
  101. [routes[i].from.lat, routes[i].from.long],
  102. [routes[i].to.lat, routes[i].to.long]
  103. ],
  104. {
  105. color: '#9575CD',
  106. weight: 5,
  107. opacity: .7,
  108. dashArray: '20,15',
  109. lineJoin: 'round'
  110. }
  111. ).addTo(routesMarkers);
  112. }
  113. map.addLayer(routesMarkers);
  114. }
  115. function showCommonRoutes() {
  116. $.get(urlroutes+"travels?page=", function(data){
  117. console.log(data);
  118. routes = data;
  119. console.log(routes);
  120. updateRoutesMarkers();
  121. // document.getElementById("loadingbar").innerHTML="";
  122. });
  123. }