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.

371 lines
14 KiB

5 years ago
  1. var allitems = []; // array with all the items before filtering
  2. var items = []; // the items showed in the map, after the categories
  3. var categories = [];
  4. var allCheckbox = {checked: true};
  5. function hexToRGBA(hex,opacity){
  6. hex = hex.replace('#','');
  7. r = parseInt(hex.substring(0,2), 16);
  8. g = parseInt(hex.substring(2,4), 16);
  9. b = parseInt(hex.substring(4,6), 16);
  10. result = 'rgba('+r+','+g+','+b+','+opacity+')';
  11. return result;
  12. }
  13. function printFiltersList() {
  14. var html = "";
  15. for (var property in categories) {
  16. if (categories.hasOwnProperty(property)) {
  17. html += `<div class='checkbox'>
  18. <label><input id="check` + property + `" onclick="updateFilter('`+property+`')" type='checkbox' value='' checked='`+categories[property].checked+`' >
  19. <img style='width:30px;height:30px;
  20. border: 2px solid `+categories[property].icon_marker_color+`;'
  21. class="marker"
  22. src='statics/img/categories/` + categories[property].icon_name + `_black.svg' />
  23. `+categories[property].name+`</label>
  24. </div>`;
  25. }
  26. }
  27. document.getElementById("filtersList").innerHTML = html;
  28. }
  29. function getItem(id) {
  30. for(var i=0; i<items.length; i++) {
  31. if(items[i].properties.slug==id) {
  32. return(items[i]);
  33. }
  34. }
  35. return("not found");
  36. }
  37. function showRightCard(item) {
  38. document.getElementById("rightCard-title").innerHTML = item.properties.name;
  39. if(item.properties.description!=null) {
  40. document.getElementById("rightCard-text").innerHTML = "<i>"+item.properties.description+"</i>";
  41. }else{
  42. document.getElementById("rightCard-text").innerHTML = "";
  43. }
  44. // gallery images
  45. if(item.properties.gallery_images.length>0) {
  46. var html = "";
  47. html += `<br>
  48. <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
  49. <ol class="carousel-indicators">`;
  50. for(var i=0; i<item.properties.gallery_images.length; i++) {
  51. if(i===0) {
  52. html += `<li data-target="#carouselExampleIndicators" data-slide-to="`+i+`" class="active"></li>`;
  53. } else {
  54. html += `<li data-target="#carouselExampleIndicators" data-slide-to="`+i+`"></li>`;
  55. }
  56. }
  57. html += `
  58. </ol>
  59. <div class="carousel-inner">`;
  60. for(var i=0; i<item.properties.gallery_images.length; i++) {
  61. if(i===0) {
  62. html += `<div class="carousel-item active">`;
  63. } else {
  64. html += `<div class="carousel-item">`;
  65. }
  66. html += `
  67. <a href="` + item.properties.gallery_images[i].original_url + `" target="_blank">
  68. <img class="d-block w-100" src="` + item.properties.gallery_images[i].thumb_url + `"
  69. alt="` + item.properties.gallery_images[i].caption + `"
  70. title="` + item.properties.gallery_images[i].caption + `">
  71. </a>
  72. </div>`;
  73. }
  74. html += `
  75. </div>
  76. <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
  77. <span class="carousel-control-prev-icon" aria-hidden="true"></span>
  78. <span class="sr-only">Previous</span>
  79. </a>
  80. <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
  81. <span class="carousel-control-next-icon" aria-hidden="true"></span>
  82. <span class="sr-only">Next</span>
  83. </a>
  84. </div>`;
  85. document.getElementById("rightCard-text").innerHTML += html;
  86. }
  87. html = "";
  88. for (var property in item.properties.categories) {
  89. if (item.properties.categories.hasOwnProperty(property)) {
  90. html += `<img style='width:30px;height:30px;'
  91. src='statics/img/categories/` + item.properties.categories[property].icon_name + `_black.svg'
  92. title="` + item.properties.categories[property].icon_name + `"/>`;
  93. }
  94. }
  95. document.getElementById("rightCard-categories").innerHTML = html;
  96. if(item.properties.pictureUrl) {
  97. document.getElementById("rightCard-img").src="https://map.komun.org/" + item.properties.pictureUrl;
  98. document.getElementById("rightCard-img").className = "card-img-top";
  99. } else {
  100. document.getElementById("rightCard-img").src="";
  101. document.getElementById("rightCard-img").className = "card-img-top hidden";
  102. }
  103. if(item.properties.web) {
  104. document.getElementById("rightCard-web").href= item.properties.web;
  105. document.getElementById("rightCard-web").innerHTML = "Web";
  106. } else {
  107. document.getElementById("rightCard-web").href="#";
  108. document.getElementById("rightCard-web").innerHTML="";
  109. }
  110. if(item.properties.mastodon) {
  111. document.getElementById("rightCard-mastodon").href= item.properties.mastodon;
  112. document.getElementById("rightCard-mastodon").innerHTML = "<i class='fab fa-mastodon-f'></i>";
  113. } else {
  114. document.getElementById("rightCard-mastodon").href="#";
  115. document.getElementById("rightCard-mastodon").innerHTML="";
  116. }
  117. if(item.properties.twitter) {
  118. document.getElementById("rightCard-twitter").href= item.properties.twitter;
  119. document.getElementById("rightCard-twitter").innerHTML = "<i class='fab fa-twitter'></i>";
  120. } else {
  121. document.getElementById("rightCard-twitter").href="#";
  122. document.getElementById("rightCard-twitter").innerHTML="";
  123. }
  124. if(item.properties.facebook) {
  125. document.getElementById("rightCard-facebook").href= item.properties.facebook;
  126. document.getElementById("rightCard-facebook").innerHTML = "<i class='fab fa-facebook-f'></i>";
  127. } else {
  128. document.getElementById("rightCard-facebook").href="#";
  129. document.getElementById("rightCard-facebook").innerHTML="";
  130. }
  131. if(item.properties.telegram) {
  132. document.getElementById("rightCard-telegram").href= item.properties.telegram;
  133. document.getElementById("rightCard-telegram").innerHTML = "<i class='fab fa-telegram-f'></i>";
  134. } else {
  135. document.getElementById("rightCard-telegram").href="#";
  136. document.getElementById("rightCard-telegram").innerHTML="";
  137. }
  138. if(item.properties.email) {
  139. document.getElementById("rightCard-email").href= "mailto:" + item.properties.email;
  140. document.getElementById("rightCard-email").innerHTML = "<i class='fa fa-envelope'></i>";
  141. } else {
  142. document.getElementById("rightCard-email").href="#";
  143. document.getElementById("rightCard-email").innerHTML="";
  144. }
  145. document.getElementById("rightCard-text").innerHTML += "<p>"+item.properties.body+"</p>";
  146. document.getElementById("rightCard").className+=" rightCard-show";
  147. }
  148. function hideRightCard() {
  149. document.getElementById("rightCard").className ="card";
  150. }
  151. function updateMarkers() {
  152. markers.clearLayers();
  153. for(var i=0; i<items.length; i++) {
  154. var lat = items[i].geometry.coordinates[1];
  155. var lon = items[i].geometry.coordinates[0];
  156. var icon = L.divIcon({
  157. className: 'markerInvisible',
  158. popupAnchor: [10, 0], // point from which the popup should open relative to the iconAnchor
  159. html: `<img style='width:30px;height:30px;border: 2px solid `+items[i].properties.icon_marker_color+`;
  160. box-shadow: 0 0 5px 4px ` + hexToRGBA(items[i].properties.icon_marker_color, 0.2) + `
  161. ;' class='marker' src='statics/img/categories/` + items[i].properties.icon_name + `_black.svg' />`
  162. // html: "<img style='width:30px;height:30px;border: 2px solid " + hexToRGBA(items[i].properties.icon_marker_color, 0.2) + ";' class='marker marker-" + items[i].properties.icon_marker_color + "' src='statics/img/categories/" + items[i].properties.icon_name + "_black.svg' />"
  163. });
  164. var marker = L.marker([lat, lon], {icon: icon});
  165. marker.id = items[i].properties.slug;
  166. if(items[i].properties.description!=null) {
  167. marker.bindPopup("<b>"+items[i].properties.name+"</b><br><i>"+items[i].properties.description+"</i>");
  168. } else {
  169. marker.bindPopup("<b>"+items[i].properties.name+"</b>");
  170. }
  171. marker.on('mouseover', function(e) {
  172. this.openPopup();
  173. });
  174. marker.on('mouseout', function(e) {
  175. this.closePopup();
  176. });
  177. marker.on('click', function(e) {
  178. var item = getItem(this.id);
  179. placeid = this.id;
  180. console.log(item.properties.gallery_images);
  181. showRightCard(item);
  182. window.history.pushState({},"", "?lang="+lang+"&lat=" + lat + "&lon=" + lon + "&zoom=" + zoom + "&placeid=" + placeid);
  183. });
  184. markers.addLayer(marker);
  185. }
  186. map.addLayer(markers);
  187. if (placeid!=="none") {
  188. var item = getItem(placeid);
  189. showRightCard(item);
  190. window.history.pushState({},"", "?lang="+lang+"&lat=" + lat + "&lon=" + lon + "&zoom=" + zoom + "&placeid=" + placeid);
  191. }
  192. }
  193. function updateFilter(selectedFilter) {
  194. if(categories[selectedFilter].checked===true) {
  195. categories[selectedFilter].checked = false;
  196. document.getElementById('check' + selectedFilter).checked = false;
  197. } else {
  198. categories[selectedFilter].checked = true;
  199. document.getElementById('check' + selectedFilter).checked = true;
  200. }
  201. applyFilters();
  202. }
  203. function selectAllFilters() {
  204. if(allCheckbox.checked===true) {
  205. items = [];
  206. allCheckbox.checked = false;
  207. for (var property in categories) {
  208. if (categories.hasOwnProperty(property)) {
  209. categories[property].checked=false;
  210. document.getElementById('check' + property).checked = false;
  211. }
  212. }
  213. } else {
  214. items = JSON.parse(JSON.stringify(allitems));
  215. allCheckbox.checked= true;
  216. for (var property in categories) {
  217. if (categories.hasOwnProperty(property)) {
  218. categories[property].checked=true;
  219. document.getElementById('check' + property).checked = true;
  220. }
  221. }
  222. }
  223. updateMarkers();
  224. }
  225. function itemInItems(array, el) {
  226. for(var i=0;i<array.length; i++) {
  227. if(array[i].properties.slug===el.properties.slug) {
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. function applyFilters() {
  234. items = [];
  235. for(var i=0; i<allitems.length; i++) {
  236. for (var property in allitems[i].properties.categories) {
  237. if (allitems[i].properties.categories.hasOwnProperty(property)) {
  238. if(categories[property].checked===true) {
  239. // check if item is not yet in items array
  240. if(itemInItems(items, allitems[i])===false) {
  241. items.push(JSON.parse(JSON.stringify(allitems[i])));
  242. }
  243. }
  244. }
  245. }
  246. }
  247. updateMarkers();
  248. }
  249. function changeLanguage() {
  250. lang = document.getElementById("lang").value;
  251. window.history.pushState({},"", "?lang="+lang+"&lat=" + lat + "&lon=" + lon + "&zoom=" + zoom + "&placeid=" + placeid);
  252. location.reload();
  253. }
  254. function printSearchResults(items) {
  255. var html = "";
  256. html += "<div class='list-group'>";
  257. for(var i=0; i<items.length; i++) {
  258. html += `
  259. <div onclick="goToItem('` + items[i].properties.slug + `')" class="list-group-item list-group-item-action flex-column align-items-start">
  260. <div class="d-flex w-100 justify-content-between">
  261. <h5 class="mb-1">` + items[i].properties.name + `</h5>
  262. <small>3 days ago</small>
  263. </div>`;
  264. if(items[i].properties.description!=null) {
  265. html += `<p class="mb-1">` + items[i].properties.description + `</p>`;
  266. }
  267. html += `
  268. </div>
  269. `;
  270. }
  271. html += "</div>";
  272. document.getElementById("searchResults").innerHTML = html;
  273. }
  274. function search() {
  275. var searchResults = [];
  276. var key = document.getElementById("searchinput").value.toUpperCase();
  277. if(key=="") {
  278. document.getElementById("searchResults").innerHTML = "";
  279. return;
  280. }
  281. for(var i=0; i<allitems.length; i++) {
  282. if(allitems[i].properties.name.toUpperCase().includes(key)) {
  283. searchResults.push(allitems[i]);
  284. }
  285. }
  286. printSearchResults(searchResults);
  287. }
  288. function showInMap(item) {
  289. lat = item.geometry.coordinates[1];
  290. lon = item.geometry.coordinates[0];
  291. window.history.pushState({},"", "?lang="+lang+"&lat=" + lat + "&lon=" + lon + "&zoom=" + zoom);
  292. map.setView([lat, lon], 16);
  293. }
  294. function goToItem(id) {
  295. var item = getItem(id);
  296. showRightCard(item);
  297. showInMap(item);
  298. }
  299. // -----
  300. // init
  301. // -----
  302. // get url parameters
  303. var url = new URL(window.location.href);
  304. var lang = url.searchParams.get("lang");
  305. var lat = url.searchParams.get("lat");
  306. var lon = url.searchParams.get("lon");
  307. var zoom = url.searchParams.get("zoom");
  308. var placeid = url.searchParams.get("placeid");
  309. if(lang==undefined) {
  310. lang = "en";
  311. }
  312. if((lat==undefined)||(lon==undefined)) {
  313. lat = 40.007;
  314. lon = -2.488;
  315. }
  316. if(zoom==undefined) {
  317. zoom=7;
  318. }
  319. if(placeid==undefined) {
  320. placeid = "none";
  321. }
  322. // var tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  323. // var tiles = L.tileLayer('https://korona.geog.uni-heidelberg.de/tiles/roadsg/x={x}&y={y}&z={z}', {
  324. // var tiles = L.tileLayer('https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png?lang='+lang, {
  325. var tiles = L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}{r}.png', {
  326. maxZoom: 18,
  327. attribution: 'Map data &copy; OpenStreetMap contributors'
  328. });
  329. var map = L.map('map', {layers: [tiles]});
  330. map.setView([lat, lon], zoom);
  331. map.addEventListener('dragend', function(ev) {
  332. var coord = map.getCenter();
  333. lat = Math.round(coord.lat * 10000) / 10000;
  334. lon = Math.round(coord.lng * 10000) / 10000;
  335. window.history.pushState({},"", "?lang="+lang+"&lat=" + lat + "&lon=" + lon + "&zoom=" + zoom + "&placeid=" + placeid);
  336. });
  337. map.on('zoomend', function(ev) {
  338. zoom = ev.target._zoom;
  339. window.history.pushState({},"", "?lang="+lang+"&lat=" + lat + "&lon=" + lon + "&zoom=" + zoom + "&placeid=" + placeid);
  340. });
  341. var markers = L.markerClusterGroup();
  342. $.get("https://map.komun.org/showCategories/lang/"+lang, function(data){
  343. console.log(data);
  344. categories = data;
  345. printFiltersList();
  346. // get items data
  347. $.get("https://map.komun.org/searchMapPoints/lang/"+lang+"/", function(data){
  348. console.log(data.response[0]);
  349. allitems = data.response;
  350. items = JSON.parse(JSON.stringify(allitems));
  351. updateMarkers();
  352. document.getElementById("loadingbar").innerHTML="";
  353. });
  354. });
  355. var sidebar = L.control.sidebar('sidebar').addTo(map);