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