searx.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. */
  17. requirejs.config({
  18. baseUrl: './static/themes/oscar/js',
  19. paths: {
  20. app: '../app'
  21. }
  22. });
  23. ;/**
  24. * searx is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Affero General Public License as published by
  26. * the Free Software Foundation, either version 3 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * searx is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU Affero General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Affero General Public License
  35. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  36. *
  37. * (C) 2019 by Alexandre Flament
  38. */
  39. window.searx = (function(d) {
  40. 'use strict';
  41. // add data- properties
  42. var script = d.currentScript || (function() {
  43. var scripts = d.getElementsByTagName('script');
  44. return scripts[scripts.length - 1];
  45. })();
  46. return {
  47. autocompleter: script.getAttribute('data-autocompleter') === 'true',
  48. method: script.getAttribute('data-method')
  49. };
  50. })(document);
  51. ;/**
  52. * searx is free software: you can redistribute it and/or modify
  53. * it under the terms of the GNU Affero General Public License as published by
  54. * the Free Software Foundation, either version 3 of the License, or
  55. * (at your option) any later version.
  56. *
  57. * searx is distributed in the hope that it will be useful,
  58. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  59. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  60. * GNU Affero General Public License for more details.
  61. *
  62. * You should have received a copy of the GNU Affero General Public License
  63. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  64. *
  65. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  66. */
  67. if(searx.autocompleter) {
  68. searx.searchResults = new Bloodhound({
  69. datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  70. queryTokenizer: Bloodhound.tokenizers.whitespace,
  71. remote: './autocompleter?q=%QUERY'
  72. });
  73. searx.searchResults.initialize();
  74. }
  75. $(document).ready(function(){
  76. if(searx.autocompleter) {
  77. $('#q').typeahead(null, {
  78. name: 'search-results',
  79. displayKey: function(result) {
  80. return result;
  81. },
  82. source: searx.searchResults.ttAdapter()
  83. });
  84. }
  85. });
  86. ;/**
  87. * searx is free software: you can redistribute it and/or modify
  88. * it under the terms of the GNU Affero General Public License as published by
  89. * the Free Software Foundation, either version 3 of the License, or
  90. * (at your option) any later version.
  91. *
  92. * searx is distributed in the hope that it will be useful,
  93. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  94. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  95. * GNU Affero General Public License for more details.
  96. *
  97. * You should have received a copy of the GNU Affero General Public License
  98. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  99. *
  100. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  101. */
  102. $(document).ready(function(){
  103. /**
  104. * focus element if class="autofocus" and id="q"
  105. */
  106. $('#q.autofocus').focus();
  107. /**
  108. * select full content on click if class="select-all-on-click"
  109. */
  110. $(".select-all-on-click").click(function () {
  111. $(this).select();
  112. });
  113. /**
  114. * change text during btn-collapse click if possible
  115. */
  116. $('.btn-collapse').click(function() {
  117. var btnTextCollapsed = $(this).data('btn-text-collapsed');
  118. var btnTextNotCollapsed = $(this).data('btn-text-not-collapsed');
  119. if(btnTextCollapsed !== '' && btnTextNotCollapsed !== '') {
  120. if($(this).hasClass('collapsed')) {
  121. new_html = $(this).html().replace(btnTextCollapsed, btnTextNotCollapsed);
  122. } else {
  123. new_html = $(this).html().replace(btnTextNotCollapsed, btnTextCollapsed);
  124. }
  125. $(this).html(new_html);
  126. }
  127. });
  128. /**
  129. * change text during btn-toggle click if possible
  130. */
  131. $('.btn-toggle .btn').click(function() {
  132. var btnClass = 'btn-' + $(this).data('btn-class');
  133. var btnLabelDefault = $(this).data('btn-label-default');
  134. var btnLabelToggled = $(this).data('btn-label-toggled');
  135. if(btnLabelToggled !== '') {
  136. if($(this).hasClass('btn-default')) {
  137. new_html = $(this).html().replace(btnLabelDefault, btnLabelToggled);
  138. } else {
  139. new_html = $(this).html().replace(btnLabelToggled, btnLabelDefault);
  140. }
  141. $(this).html(new_html);
  142. }
  143. $(this).toggleClass(btnClass);
  144. $(this).toggleClass('btn-default');
  145. });
  146. /**
  147. * change text during btn-toggle click if possible
  148. */
  149. $('.media-loader').click(function() {
  150. var target = $(this).data('target');
  151. var iframe_load = $(target + ' > iframe');
  152. var srctest = iframe_load.attr('src');
  153. if(srctest === undefined || srctest === false){
  154. iframe_load.attr('src', iframe_load.data('src'));
  155. }
  156. });
  157. /**
  158. * Select or deselect every categories on double clic
  159. */
  160. $(".btn-sm").dblclick(function() {
  161. var btnClass = 'btn-' + $(this).data('btn-class'); // primary
  162. if($(this).hasClass('btn-default')) {
  163. $(".btn-sm > input").attr('checked', 'checked');
  164. $(".btn-sm > input").prop("checked", true);
  165. $(".btn-sm").addClass(btnClass);
  166. $(".btn-sm").addClass('active');
  167. $(".btn-sm").removeClass('btn-default');
  168. } else {
  169. $(".btn-sm > input").attr('checked', '');
  170. $(".btn-sm > input").removeAttr('checked');
  171. $(".btn-sm > input").checked = false;
  172. $(".btn-sm").removeClass(btnClass);
  173. $(".btn-sm").removeClass('active');
  174. $(".btn-sm").addClass('btn-default');
  175. }
  176. });
  177. });
  178. ;/**
  179. * searx is free software: you can redistribute it and/or modify
  180. * it under the terms of the GNU Affero General Public License as published by
  181. * the Free Software Foundation, either version 3 of the License, or
  182. * (at your option) any later version.
  183. *
  184. * searx is distributed in the hope that it will be useful,
  185. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  186. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  187. * GNU Affero General Public License for more details.
  188. *
  189. * You should have received a copy of the GNU Affero General Public License
  190. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  191. *
  192. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  193. */
  194. $(document).ready(function(){
  195. $(".searx_overpass_request").on( "click", function( event ) {
  196. var overpass_url = "https://overpass-api.de/api/interpreter?data=";
  197. var query_start = overpass_url + "[out:json][timeout:25];(";
  198. var query_end = ");out meta;";
  199. var osm_id = $(this).data('osm-id');
  200. var osm_type = $(this).data('osm-type');
  201. var result_table = $(this).data('result-table');
  202. var result_table_loadicon = "#" + $(this).data('result-table-loadicon');
  203. // tags which can be ignored
  204. var osm_ignore_tags = [ "addr:city", "addr:country", "addr:housenumber", "addr:postcode", "addr:street" ];
  205. if(osm_id && osm_type && result_table) {
  206. result_table = "#" + result_table;
  207. var query = null;
  208. switch(osm_type) {
  209. case 'node':
  210. query = query_start + "node(" + osm_id + ");" + query_end;
  211. break;
  212. case 'way':
  213. query = query_start + "way(" + osm_id + ");" + query_end;
  214. break;
  215. case 'relation':
  216. query = query_start + "relation(" + osm_id + ");" + query_end;
  217. break;
  218. default:
  219. break;
  220. }
  221. if(query) {
  222. //alert(query);
  223. var ajaxRequest = $.ajax( query )
  224. .done(function( html) {
  225. if(html && html.elements && html.elements[0]) {
  226. var element = html.elements[0];
  227. var newHtml = $(result_table).html();
  228. for (var row in element.tags) {
  229. if(element.tags.name === null || osm_ignore_tags.indexOf(row) == -1) {
  230. newHtml += "<tr><td>" + row + "</td><td>";
  231. switch(row) {
  232. case "phone":
  233. case "fax":
  234. newHtml += "<a href=\"tel:" + element.tags[row].replace(/ /g,'') + "\">" + element.tags[row] + "</a>";
  235. break;
  236. case "email":
  237. newHtml += "<a href=\"mailto:" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  238. break;
  239. case "website":
  240. case "url":
  241. newHtml += "<a href=\"" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  242. break;
  243. case "wikidata":
  244. newHtml += "<a href=\"https://www.wikidata.org/wiki/" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  245. break;
  246. case "wikipedia":
  247. if(element.tags[row].indexOf(":") != -1) {
  248. 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>";
  249. break;
  250. }
  251. /* jshint ignore:start */
  252. default:
  253. /* jshint ignore:end */
  254. newHtml += element.tags[row];
  255. break;
  256. }
  257. newHtml += "</td></tr>";
  258. }
  259. }
  260. $(result_table).html(newHtml);
  261. $(result_table).removeClass('hidden');
  262. $(result_table_loadicon).addClass('hidden');
  263. }
  264. })
  265. .fail(function() {
  266. $(result_table_loadicon).html($(result_table_loadicon).html() + "<p class=\"text-muted\">could not load data!</p>");
  267. });
  268. }
  269. }
  270. // this event occour only once per element
  271. $( this ).off( event );
  272. });
  273. $(".searx_init_map").on( "click", function( event ) {
  274. var leaflet_target = $(this).data('leaflet-target');
  275. var map_lon = $(this).data('map-lon');
  276. var map_lat = $(this).data('map-lat');
  277. var map_zoom = $(this).data('map-zoom');
  278. var map_boundingbox = $(this).data('map-boundingbox');
  279. var map_geojson = $(this).data('map-geojson');
  280. require(['leaflet-0.7.3.min'], function(leaflet) {
  281. if(map_boundingbox) {
  282. southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]);
  283. northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]);
  284. map_bounds = L.latLngBounds(southWest, northEast);
  285. }
  286. // TODO hack
  287. // change default imagePath
  288. L.Icon.Default.imagePath = "./static/themes/oscar/img/map";
  289. // init map
  290. var map = L.map(leaflet_target);
  291. // create the tile layer with correct attribution
  292. var osmMapnikUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  293. var osmMapnikAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  294. var osmMapnik = new L.TileLayer(osmMapnikUrl, {minZoom: 1, maxZoom: 19, attribution: osmMapnikAttrib});
  295. var osmWikimediaUrl='https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png';
  296. var osmWikimediaAttrib = 'Wikimedia maps beta | Maps data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  297. var osmWikimedia = new L.TileLayer(osmWikimediaUrl, {minZoom: 1, maxZoom: 19, attribution: osmWikimediaAttrib});
  298. // init map view
  299. if(map_bounds) {
  300. // TODO hack: https://github.com/Leaflet/Leaflet/issues/2021
  301. setTimeout(function () {
  302. map.fitBounds(map_bounds, {
  303. maxZoom:17
  304. });
  305. }, 0);
  306. } else if (map_lon && map_lat) {
  307. if(map_zoom)
  308. map.setView(new L.LatLng(map_lat, map_lon),map_zoom);
  309. else
  310. map.setView(new L.LatLng(map_lat, map_lon),8);
  311. }
  312. map.addLayer(osmMapnik);
  313. var baseLayers = {
  314. "OSM Mapnik": osmMapnik/*,
  315. "OSM Wikimedia": osmWikimedia*/
  316. };
  317. L.control.layers(baseLayers).addTo(map);
  318. if(map_geojson)
  319. L.geoJson(map_geojson).addTo(map);
  320. /*else if(map_bounds)
  321. L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);*/
  322. });
  323. // this event occour only once per element
  324. $( this ).off( event );
  325. });
  326. });