logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

searx_mapresult.js (7251B)


  1. /**
  2. * searx is free software: you can redistribute it and/or modify
  3. * it under the terms of the GNU Affero General Public License as published by
  4. * the Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * searx is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Affero General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU Affero General Public License
  13. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  14. *
  15. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  16. * (C) 2017 by Alexandre Flament, <alex@al-f.net>
  17. */
  18. (function (w, d, searx) {
  19. 'use strict';
  20. searx.ready(function () {
  21. searx.on('.searx_overpass_request', 'click', function(event) {
  22. // no more request
  23. this.classList.remove("searx_overpass_request");
  24. //
  25. var overpass_url = "https://overpass-api.de/api/interpreter?data=";
  26. var query_start = overpass_url + "[out:json][timeout:25];(";
  27. var query_end = ");out meta;";
  28. var osm_id = this.dataset.osmId;
  29. var osm_type = this.dataset.osmType;
  30. var result_table = d.querySelector("#" + this.dataset.resultTable);
  31. var result_table_loadicon = d.querySelector("#" + this.dataset.resultTableLoadicon);
  32. // tags which can be ignored
  33. var osm_ignore_tags = [ "addr:city", "addr:country", "addr:housenumber", "addr:postcode", "addr:street" ];
  34. if(osm_id && osm_type && result_table) {
  35. var query = null;
  36. switch(osm_type) {
  37. case 'node':
  38. query = query_start + "node(" + osm_id + ");" + query_end;
  39. break;
  40. case 'way':
  41. query = query_start + "way(" + osm_id + ");" + query_end;
  42. break;
  43. case 'relation':
  44. query = query_start + "relation(" + osm_id + ");" + query_end;
  45. break;
  46. default:
  47. break;
  48. }
  49. if(query) {
  50. // console.log(query);
  51. searx.http( 'GET', query ).then(function(html, contentType) {
  52. html = JSON.parse(html);
  53. if(html && html.elements && html.elements[0]) {
  54. var element = html.elements[0];
  55. var newHtml = "";
  56. for (var row in element.tags) {
  57. if(element.tags.name === null || osm_ignore_tags.indexOf(row) == -1) {
  58. newHtml += "<tr><td>" + row + "</td><td>";
  59. switch(row) {
  60. case "phone":
  61. case "fax":
  62. newHtml += "<a href=\"tel:" + element.tags[row].replace(/ /g,'') + "\">" + element.tags[row] + "</a>";
  63. break;
  64. case "email":
  65. newHtml += "<a href=\"mailto:" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  66. break;
  67. case "website":
  68. case "url":
  69. newHtml += "<a href=\"" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  70. break;
  71. case "wikidata":
  72. newHtml += "<a href=\"https://www.wikidata.org/wiki/" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  73. break;
  74. case "wikipedia":
  75. if(element.tags[row].indexOf(":") != -1) {
  76. newHtml += "<a href=\"https://" + element.tags[row].substring(0,element.tags[row].indexOf(":")) + ".wikipedia.org/wiki/" + element.tags[row].substring(element.tags[row].indexOf(":")+1) + "\">" + element.tags[row] + "</a>";
  77. break;
  78. }
  79. /* jshint ignore:start */
  80. default:
  81. /* jshint ignore:end */
  82. newHtml += element.tags[row];
  83. break;
  84. }
  85. newHtml += "</td></tr>";
  86. }
  87. }
  88. result_table_loadicon.parentNode.removeChild(result_table_loadicon);
  89. result_table.classList.remove('invisible');
  90. result_table.querySelector("tbody").innerHTML = newHtml;
  91. }
  92. })
  93. .catch(function() {
  94. result_table_loadicon.classList.remove('invisible');
  95. result_table_loadicon.innerHTML = "could not load data!";
  96. });
  97. }
  98. }
  99. // this event occour only once per element
  100. event.preventDefault();
  101. });
  102. searx.on('.searx_init_map', 'click', function(event) {
  103. // no more request
  104. this.classList.remove("searx_init_map");
  105. //
  106. var leaflet_target = this.dataset.leafletTarget;
  107. var map_lon = parseFloat(this.dataset.mapLon);
  108. var map_lat = parseFloat(this.dataset.mapLat);
  109. var map_zoom = parseFloat(this.dataset.mapZoom);
  110. var map_boundingbox = JSON.parse(this.dataset.mapBoundingbox);
  111. var map_geojson = JSON.parse(this.dataset.mapGeojson);
  112. searx.loadStyle('leaflet/leaflet.css');
  113. searx.loadScript('leaflet/leaflet.js', function() {
  114. var map_bounds = null;
  115. if(map_boundingbox) {
  116. var southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]);
  117. var northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]);
  118. map_bounds = L.latLngBounds(southWest, northEast);
  119. }
  120. // init map
  121. var map = L.map(leaflet_target);
  122. // create the tile layer with correct attribution
  123. var osmMapnikUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  124. var osmMapnikAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  125. var osmMapnik = new L.TileLayer(osmMapnikUrl, {minZoom: 1, maxZoom: 19, attribution: osmMapnikAttrib});
  126. var osmWikimediaUrl='https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png';
  127. var osmWikimediaAttrib = 'Wikimedia maps beta | Maps data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  128. var osmWikimedia = new L.TileLayer(osmWikimediaUrl, {minZoom: 1, maxZoom: 19, attribution: osmWikimediaAttrib});
  129. // init map view
  130. if(map_bounds) {
  131. // TODO hack: https://github.com/Leaflet/Leaflet/issues/2021
  132. // Still useful ?
  133. setTimeout(function () {
  134. map.fitBounds(map_bounds, {
  135. maxZoom:17
  136. });
  137. }, 0);
  138. } else if (map_lon && map_lat) {
  139. if(map_zoom) {
  140. map.setView(new L.latLng(map_lat, map_lon),map_zoom);
  141. } else {
  142. map.setView(new L.latLng(map_lat, map_lon),8);
  143. }
  144. }
  145. map.addLayer(osmMapnik);
  146. var baseLayers = {
  147. "OSM Mapnik": osmMapnik/*,
  148. "OSM Wikimedia": osmWikimedia*/
  149. };
  150. L.control.layers(baseLayers).addTo(map);
  151. if(map_geojson) {
  152. L.geoJson(map_geojson).addTo(map);
  153. } /*else if(map_bounds) {
  154. L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);
  155. }*/
  156. });
  157. // this event occour only once per element
  158. event.preventDefault();
  159. });
  160. });
  161. })(window, document, window.searx);