searx.js 15 KB

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