searx.js 16 KB

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