searx.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. //
  20. d.getElementsByTagName("html")[0].className = "js";
  21. // add data- properties
  22. var script = d.currentScript || (function() {
  23. var scripts = d.getElementsByTagName('script');
  24. return scripts[scripts.length - 1];
  25. })();
  26. return {
  27. autocompleter: script.getAttribute('data-autocompleter') === 'true',
  28. method: script.getAttribute('data-method'),
  29. translations: JSON.parse(script.getAttribute('data-translations'))
  30. };
  31. })(document);
  32. ;/**
  33. * searx is free software: you can redistribute it and/or modify
  34. * it under the terms of the GNU Affero General Public License as published by
  35. * the Free Software Foundation, either version 3 of the License, or
  36. * (at your option) any later version.
  37. *
  38. * searx is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU Affero General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU Affero General Public License
  44. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  45. *
  46. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  47. */
  48. $(document).ready(function(){
  49. var original_search_value = '';
  50. if(searx.autocompleter) {
  51. var searchResults = new Bloodhound({
  52. datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  53. queryTokenizer: Bloodhound.tokenizers.whitespace,
  54. remote: {
  55. url: './autocompleter?q=%QUERY',
  56. wildcard: '%QUERY'
  57. }
  58. });
  59. searchResults.initialize();
  60. $("#q").on('keydown', function(e) {
  61. if(e.which == 13) {
  62. original_search_value = $('#q').val();
  63. }
  64. });
  65. $('#q').typeahead({
  66. name: 'search-results',
  67. highlight: false,
  68. hint: true,
  69. displayKey: function(result) {
  70. return result;
  71. },
  72. classNames: {
  73. input: 'tt-input',
  74. hint: 'tt-hint',
  75. menu: 'tt-dropdown-menu',
  76. dataset: 'tt-dataset-search-results',
  77. },
  78. }, {
  79. name: 'autocomplete',
  80. source: searchResults,
  81. });
  82. $('#q').bind('typeahead:select', function(ev, suggestion) {
  83. if(original_search_value) {
  84. $('#q').val(original_search_value);
  85. }
  86. $("#search_form").submit();
  87. });
  88. }
  89. });
  90. ;/**
  91. * searx is free software: you can redistribute it and/or modify
  92. * it under the terms of the GNU Affero General Public License as published by
  93. * the Free Software Foundation, either version 3 of the License, or
  94. * (at your option) any later version.
  95. *
  96. * searx is distributed in the hope that it will be useful,
  97. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  98. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  99. * GNU Affero General Public License for more details.
  100. *
  101. * You should have received a copy of the GNU Affero General Public License
  102. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  103. *
  104. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  105. */
  106. $(document).ready(function(){
  107. /**
  108. * focus element if class="autofocus" and id="q"
  109. */
  110. $('#q.autofocus').focus();
  111. /**
  112. * Empty search bar when click on reset button
  113. */
  114. $("#clear_search").click(function () {
  115. document.getElementById("q").value = "";
  116. });
  117. /**
  118. * select full content on click if class="select-all-on-click"
  119. */
  120. $(".select-all-on-click").click(function () {
  121. $(this).select();
  122. });
  123. /**
  124. * change text during btn-collapse click if possible
  125. */
  126. $('.btn-collapse').click(function() {
  127. var btnTextCollapsed = $(this).data('btn-text-collapsed');
  128. var btnTextNotCollapsed = $(this).data('btn-text-not-collapsed');
  129. if(btnTextCollapsed !== '' && btnTextNotCollapsed !== '') {
  130. if($(this).hasClass('collapsed')) {
  131. new_html = $(this).html().replace(btnTextCollapsed, btnTextNotCollapsed);
  132. } else {
  133. new_html = $(this).html().replace(btnTextNotCollapsed, btnTextCollapsed);
  134. }
  135. $(this).html(new_html);
  136. }
  137. });
  138. /**
  139. * change text during btn-toggle click if possible
  140. */
  141. $('.btn-toggle .btn').click(function() {
  142. var btnClass = 'btn-' + $(this).data('btn-class');
  143. var btnLabelDefault = $(this).data('btn-label-default');
  144. var btnLabelToggled = $(this).data('btn-label-toggled');
  145. if(btnLabelToggled !== '') {
  146. if($(this).hasClass('btn-default')) {
  147. new_html = $(this).html().replace(btnLabelDefault, btnLabelToggled);
  148. } else {
  149. new_html = $(this).html().replace(btnLabelToggled, btnLabelDefault);
  150. }
  151. $(this).html(new_html);
  152. }
  153. $(this).toggleClass(btnClass);
  154. $(this).toggleClass('btn-default');
  155. });
  156. /**
  157. * change text during btn-toggle click if possible
  158. */
  159. $('.media-loader').click(function() {
  160. var target = $(this).data('target');
  161. var iframe_load = $(target + ' > iframe');
  162. var srctest = iframe_load.attr('src');
  163. if(srctest === undefined || srctest === false){
  164. iframe_load.attr('src', iframe_load.data('src'));
  165. }
  166. });
  167. /**
  168. * Select or deselect every categories on double clic
  169. */
  170. $(".btn-sm").dblclick(function() {
  171. var btnClass = 'btn-' + $(this).data('btn-class'); // primary
  172. if($(this).hasClass('btn-default')) {
  173. $(".btn-sm > input").attr('checked', 'checked');
  174. $(".btn-sm > input").prop("checked", true);
  175. $(".btn-sm").addClass(btnClass);
  176. $(".btn-sm").addClass('active');
  177. $(".btn-sm").removeClass('btn-default');
  178. } else {
  179. $(".btn-sm > input").attr('checked', '');
  180. $(".btn-sm > input").removeAttr('checked');
  181. $(".btn-sm > input").checked = false;
  182. $(".btn-sm").removeClass(btnClass);
  183. $(".btn-sm").removeClass('active');
  184. $(".btn-sm").addClass('btn-default');
  185. }
  186. });
  187. $(".nav-tabs").click(function(a) {
  188. var tabs = $(a.target).parents("ul");
  189. tabs.children().attr("aria-selected", "false");
  190. $(a.target).parent().attr("aria-selected", "true");
  191. });
  192. /**
  193. * Layout images according to their sizes
  194. */
  195. searx.image_thumbnail_layout = new searx.ImageLayout('#main_results', '#main_results .result-images', 'img.img-thumbnail', 15, 200);
  196. searx.image_thumbnail_layout.watch();
  197. });
  198. ;window.addEventListener('load', function() {
  199. // Hide infobox toggle if shrunk size already fits all content.
  200. $('.infobox').each(function() {
  201. var infobox_body = $(this).find('.infobox_body');
  202. var total_height = infobox_body.prop('scrollHeight') + infobox_body.find('img.infobox_part').height();
  203. var max_height = infobox_body.css('max-height').replace('px', '');
  204. if (total_height <= max_height) {
  205. $(this).find('.infobox_toggle').hide();
  206. }
  207. });
  208. });
  209. ;/**
  210. * searx is free software: you can redistribute it and/or modify
  211. * it under the terms of the GNU Affero General Public License as published by
  212. * the Free Software Foundation, either version 3 of the License, or
  213. * (at your option) any later version.
  214. *
  215. * searx is distributed in the hope that it will be useful,
  216. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  217. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  218. * GNU Affero General Public License for more details.
  219. *
  220. * You should have received a copy of the GNU Affero General Public License
  221. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  222. *
  223. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  224. */
  225. $(document).ready(function(){
  226. $(".searx_overpass_request").on( "click", function( event ) {
  227. var overpass_url = "https://overpass-api.de/api/interpreter?data=";
  228. var query_start = overpass_url + "[out:json][timeout:25];(";
  229. var query_end = ");out meta;";
  230. var osm_id = $(this).data('osm-id');
  231. var osm_type = $(this).data('osm-type');
  232. var result_table = $(this).data('result-table');
  233. var result_table_loadicon = "#" + $(this).data('result-table-loadicon');
  234. // tags which can be ignored
  235. var osm_ignore_tags = [ "addr:city", "addr:country", "addr:housenumber", "addr:postcode", "addr:street" ];
  236. if(osm_id && osm_type && result_table) {
  237. result_table = "#" + result_table;
  238. var query = null;
  239. switch(osm_type) {
  240. case 'node':
  241. query = query_start + "node(" + osm_id + ");" + query_end;
  242. break;
  243. case 'way':
  244. query = query_start + "way(" + osm_id + ");" + query_end;
  245. break;
  246. case 'relation':
  247. query = query_start + "relation(" + osm_id + ");" + query_end;
  248. break;
  249. default:
  250. break;
  251. }
  252. if(query) {
  253. //alert(query);
  254. var ajaxRequest = $.ajax( query )
  255. .done(function( html) {
  256. if(html && html.elements && html.elements[0]) {
  257. var element = html.elements[0];
  258. var newHtml = $(result_table).html();
  259. for (var row in element.tags) {
  260. if(element.tags.name === null || osm_ignore_tags.indexOf(row) == -1) {
  261. newHtml += "<tr><td>" + row + "</td><td>";
  262. switch(row) {
  263. case "phone":
  264. case "fax":
  265. newHtml += "<a href=\"tel:" + element.tags[row].replace(/ /g,'') + "\">" + element.tags[row] + "</a>";
  266. break;
  267. case "email":
  268. newHtml += "<a href=\"mailto:" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  269. break;
  270. case "website":
  271. case "url":
  272. newHtml += "<a href=\"" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  273. break;
  274. case "wikidata":
  275. newHtml += "<a href=\"https://www.wikidata.org/wiki/" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  276. break;
  277. case "wikipedia":
  278. if(element.tags[row].indexOf(":") != -1) {
  279. 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>";
  280. break;
  281. }
  282. /* jshint ignore:start */
  283. default:
  284. /* jshint ignore:end */
  285. newHtml += element.tags[row];
  286. break;
  287. }
  288. newHtml += "</td></tr>";
  289. }
  290. }
  291. $(result_table).html(newHtml);
  292. $(result_table).removeClass('hidden');
  293. $(result_table_loadicon).addClass('hidden');
  294. }
  295. })
  296. .fail(function() {
  297. $(result_table_loadicon).html($(result_table_loadicon).html() + "<p class=\"text-muted\">"+searx.translations.could_not_load+"</p>");
  298. });
  299. }
  300. }
  301. // this event occour only once per element
  302. $( this ).off( event );
  303. });
  304. $(".searx_init_map").on( "click", function( event ) {
  305. var leaflet_target = $(this).data('leaflet-target');
  306. var map_lon = $(this).data('map-lon');
  307. var map_lat = $(this).data('map-lat');
  308. var map_zoom = $(this).data('map-zoom');
  309. var map_boundingbox = $(this).data('map-boundingbox');
  310. var map_geojson = $(this).data('map-geojson');
  311. if(map_boundingbox) {
  312. southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]);
  313. northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]);
  314. map_bounds = L.latLngBounds(southWest, northEast);
  315. }
  316. // change default imagePath
  317. L.Icon.Default.imagePath = "./static/themes/oscar/css/images/";
  318. // init map
  319. var map = L.map(leaflet_target);
  320. // create the tile layer with correct attribution
  321. var osmMapnikUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  322. var osmMapnikAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  323. var osmMapnik = new L.TileLayer(osmMapnikUrl, {minZoom: 1, maxZoom: 19, attribution: osmMapnikAttrib});
  324. var osmWikimediaUrl='https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png';
  325. var osmWikimediaAttrib = 'Wikimedia maps beta | Maps data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  326. var osmWikimedia = new L.TileLayer(osmWikimediaUrl, {minZoom: 1, maxZoom: 19, attribution: osmWikimediaAttrib});
  327. // init map view
  328. setTimeout(function() {
  329. if(map_bounds) {
  330. map.fitBounds(map_bounds, {
  331. maxZoom:17
  332. });
  333. } else if (map_lon && map_lat) {
  334. if(map_zoom)
  335. map.setView(new L.LatLng(map_lat, map_lon),map_zoom);
  336. else
  337. map.setView(new L.LatLng(map_lat, map_lon),8);
  338. }
  339. }, 0);
  340. map.addLayer(osmMapnik);
  341. var baseLayers = {
  342. "OSM Mapnik": osmMapnik/*,
  343. "OSM Wikimedia": osmWikimedia*/
  344. };
  345. L.control.layers(baseLayers).addTo(map);
  346. if(map_geojson)
  347. L.geoJson(map_geojson).addTo(map);
  348. /*else if(map_bounds)
  349. L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);*/
  350. // this event occour only once per element
  351. $( this ).off( event );
  352. });
  353. });
  354. ;$(document).ready(function(){
  355. $("#allow-all-engines").click(function() {
  356. $(".onoffswitch-checkbox").each(function() { this.checked = false;});
  357. });
  358. $("#disable-all-engines").click(function() {
  359. $(".onoffswitch-checkbox").each(function() { this.checked = true;});
  360. });
  361. });
  362. ;/**
  363. *
  364. * Google Image Layout v0.0.1
  365. * Description, by Anh Trinh.
  366. * Heavily modified for searx
  367. * https://ptgamr.github.io/2014-09-12-google-image-layout/
  368. * https://ptgamr.github.io/google-image-layout/src/google-image-layout.js
  369. *
  370. * @license Free to use under the MIT License.
  371. *
  372. */
  373. (function (w, d) {
  374. function ImageLayout(container_selector, results_selector, img_selector, margin, maxHeight) {
  375. this.container_selector = container_selector;
  376. this.results_selector = results_selector;
  377. this.img_selector = img_selector;
  378. this.margin = margin;
  379. this.maxHeight = maxHeight;
  380. this.isAlignDone = true;
  381. }
  382. /**
  383. * Get the height that make all images fit the container
  384. *
  385. * width = w1 + w2 + w3 + ... = r1*h + r2*h + r3*h + ...
  386. *
  387. * @param {[type]} images the images to be calculated
  388. * @param {[type]} width the container witdth
  389. * @param {[type]} margin the margin between each image
  390. *
  391. * @return {[type]} the height
  392. */
  393. ImageLayout.prototype._getHeigth = function (images, width) {
  394. var i, img;
  395. var r = 0;
  396. for (i = 0; i < images.length; i++) {
  397. img = images[i];
  398. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  399. r += img.naturalWidth / img.naturalHeight;
  400. } else {
  401. // assume that not loaded images are square
  402. r += 1;
  403. }
  404. }
  405. return (width - images.length * this.margin) / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
  406. };
  407. ImageLayout.prototype._setSize = function (images, height) {
  408. var i, img, imgWidth;
  409. var imagesLength = images.length, resultNode;
  410. for (i = 0; i < imagesLength; i++) {
  411. img = images[i];
  412. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  413. imgWidth = height * img.naturalWidth / img.naturalHeight;
  414. } else {
  415. // not loaded image : make it square as _getHeigth said it
  416. imgWidth = height;
  417. }
  418. img.style.width = imgWidth + 'px';
  419. img.style.height = height + 'px';
  420. img.style.marginLeft = '3px';
  421. img.style.marginTop = '3px';
  422. img.style.marginRight = this.margin - 7 + 'px'; // -4 is the negative margin of the inline element
  423. img.style.marginBottom = this.margin - 7 + 'px';
  424. resultNode = img.parentNode.parentNode;
  425. if (!resultNode.classList.contains('js')) {
  426. resultNode.classList.add('js');
  427. }
  428. }
  429. };
  430. ImageLayout.prototype._alignImgs = function (imgGroup) {
  431. var isSearching, slice, i, h;
  432. var containerElement = d.querySelector(this.container_selector);
  433. var containerCompStyles = window.getComputedStyle(containerElement);
  434. var containerPaddingLeft = parseInt(containerCompStyles.getPropertyValue('padding-left'), 10);
  435. var containerPaddingRight = parseInt(containerCompStyles.getPropertyValue('padding-right'), 10);
  436. var containerWidth = containerElement.clientWidth - containerPaddingLeft - containerPaddingRight;
  437. while (imgGroup.length > 0) {
  438. isSearching = true;
  439. for (i = 1; i <= imgGroup.length && isSearching; i++) {
  440. slice = imgGroup.slice(0, i);
  441. h = this._getHeigth(slice, containerWidth);
  442. if (h < this.maxHeight) {
  443. this._setSize(slice, h);
  444. // continue with the remaining images
  445. imgGroup = imgGroup.slice(i);
  446. isSearching = false;
  447. }
  448. }
  449. if (isSearching) {
  450. this._setSize(slice, Math.min(this.maxHeight, h));
  451. break;
  452. }
  453. }
  454. };
  455. ImageLayout.prototype.align = function () {
  456. var i;
  457. var results_selectorNode = d.querySelectorAll(this.results_selector);
  458. var results_length = results_selectorNode.length;
  459. var previous = null;
  460. var current = null;
  461. var imgGroup = [];
  462. for (i = 0; i < results_length; i++) {
  463. current = results_selectorNode[i];
  464. if (current.previousElementSibling !== previous && imgGroup.length > 0) {
  465. // the current image is not connected to previous one
  466. // so the current image is the start of a new group of images.
  467. // so call _alignImgs to align the current group
  468. this._alignImgs(imgGroup);
  469. // and start a new empty group of images
  470. imgGroup = [];
  471. }
  472. // add the current image to the group (only the img tag)
  473. imgGroup.push(current.querySelector(this.img_selector));
  474. // update the previous variable
  475. previous = current;
  476. }
  477. // align the remaining images
  478. if (imgGroup.length > 0) {
  479. this._alignImgs(imgGroup);
  480. }
  481. };
  482. ImageLayout.prototype.watch = function () {
  483. var i, img;
  484. var obj = this;
  485. var results_nodes = d.querySelectorAll(this.results_selector);
  486. var results_length = results_nodes.length;
  487. function throttleAlign() {
  488. if (obj.isAlignDone) {
  489. obj.isAlignDone = false;
  490. setTimeout(function () {
  491. obj.align();
  492. obj.isAlignDone = true;
  493. }, 100);
  494. }
  495. }
  496. w.addEventListener('pageshow', throttleAlign);
  497. w.addEventListener('load', throttleAlign);
  498. w.addEventListener('resize', throttleAlign);
  499. for (i = 0; i < results_length; i++) {
  500. img = results_nodes[i].querySelector(this.img_selector);
  501. if (img !== null && img !== undefined) {
  502. img.addEventListener('load', throttleAlign);
  503. img.addEventListener('error', throttleAlign);
  504. }
  505. }
  506. };
  507. w.searx.ImageLayout = ImageLayout;
  508. }(window, document));