searx.js 15 KB

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