searx.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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_init_map").on( "click", function( event ) {
  227. var leaflet_target = $(this).data('leaflet-target');
  228. var map_lon = $(this).data('map-lon');
  229. var map_lat = $(this).data('map-lat');
  230. var map_zoom = $(this).data('map-zoom');
  231. var map_boundingbox = $(this).data('map-boundingbox');
  232. var map_geojson = $(this).data('map-geojson');
  233. if(map_boundingbox) {
  234. southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]);
  235. northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]);
  236. map_bounds = L.latLngBounds(southWest, northEast);
  237. }
  238. // change default imagePath
  239. L.Icon.Default.imagePath = "./static/themes/oscar/css/images/";
  240. // init map
  241. var map = L.map(leaflet_target);
  242. // create the tile layer with correct attribution
  243. var osmMapnikUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  244. var osmMapnikAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  245. var osmMapnik = new L.TileLayer(osmMapnikUrl, {minZoom: 1, maxZoom: 19, attribution: osmMapnikAttrib});
  246. var osmWikimediaUrl='https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png';
  247. var osmWikimediaAttrib = 'Wikimedia maps beta | Maps data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  248. var osmWikimedia = new L.TileLayer(osmWikimediaUrl, {minZoom: 1, maxZoom: 19, attribution: osmWikimediaAttrib});
  249. // init map view
  250. setTimeout(function() {
  251. if(map_bounds) {
  252. map.fitBounds(map_bounds, {
  253. maxZoom:17
  254. });
  255. } else if (map_lon && map_lat) {
  256. if(map_zoom)
  257. map.setView(new L.LatLng(map_lat, map_lon),map_zoom);
  258. else
  259. map.setView(new L.LatLng(map_lat, map_lon),8);
  260. }
  261. }, 0);
  262. map.addLayer(osmMapnik);
  263. var baseLayers = {
  264. "OSM Mapnik": osmMapnik/*,
  265. "OSM Wikimedia": osmWikimedia*/
  266. };
  267. L.control.layers(baseLayers).addTo(map);
  268. if(map_geojson)
  269. L.geoJson(map_geojson).addTo(map);
  270. /*else if(map_bounds)
  271. L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);*/
  272. // this event occour only once per element
  273. $( this ).off( event );
  274. });
  275. });
  276. ;$(document).ready(function(){
  277. let engine_descriptions = null;
  278. function load_engine_descriptions() {
  279. if (engine_descriptions == null) {
  280. $.ajax("engine_descriptions.json", dataType="json").done(function(data) {
  281. engine_descriptions = data;
  282. for (const [engine_name, description] of Object.entries(data)) {
  283. let elements = $('[data-engine-name="' + engine_name + '"] .description');
  284. for(const element of elements) {
  285. let source = ' (<i>' + searx.translations['Source'] + ':&nbsp;' + description[1] + '</i>)';
  286. element.innerHTML = description[0] + source;
  287. }
  288. }
  289. });
  290. }
  291. }
  292. if (document.querySelector('body[class="preferences_endpoint"]')) {
  293. $('[data-engine-name]').hover(function() {
  294. load_engine_descriptions();
  295. });
  296. }
  297. });
  298. ;$(document).ready(function(){
  299. $("#allow-all-engines").click(function() {
  300. $(".onoffswitch-checkbox").each(function() { this.checked = false;});
  301. });
  302. $("#disable-all-engines").click(function() {
  303. $(".onoffswitch-checkbox").each(function() { this.checked = true;});
  304. });
  305. });
  306. ;/**
  307. *
  308. * Google Image Layout v0.0.1
  309. * Description, by Anh Trinh.
  310. * Heavily modified for searx
  311. * https://ptgamr.github.io/2014-09-12-google-image-layout/
  312. * https://ptgamr.github.io/google-image-layout/src/google-image-layout.js
  313. *
  314. * @license Free to use under the MIT License.
  315. *
  316. */
  317. (function (w, d) {
  318. function ImageLayout(container_selector, results_selector, img_selector, margin, maxHeight) {
  319. this.container_selector = container_selector;
  320. this.results_selector = results_selector;
  321. this.img_selector = img_selector;
  322. this.margin = margin;
  323. this.maxHeight = maxHeight;
  324. this.isAlignDone = true;
  325. }
  326. /**
  327. * Get the height that make all images fit the container
  328. *
  329. * width = w1 + w2 + w3 + ... = r1*h + r2*h + r3*h + ...
  330. *
  331. * @param {[type]} images the images to be calculated
  332. * @param {[type]} width the container witdth
  333. * @param {[type]} margin the margin between each image
  334. *
  335. * @return {[type]} the height
  336. */
  337. ImageLayout.prototype._getHeigth = function (images, width) {
  338. var i, img;
  339. var r = 0;
  340. for (i = 0; i < images.length; i++) {
  341. img = images[i];
  342. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  343. r += img.naturalWidth / img.naturalHeight;
  344. } else {
  345. // assume that not loaded images are square
  346. r += 1;
  347. }
  348. }
  349. return (width - images.length * this.margin) / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
  350. };
  351. ImageLayout.prototype._setSize = function (images, height) {
  352. var i, img, imgWidth;
  353. var imagesLength = images.length, resultNode;
  354. for (i = 0; i < imagesLength; i++) {
  355. img = images[i];
  356. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  357. imgWidth = height * img.naturalWidth / img.naturalHeight;
  358. } else {
  359. // not loaded image : make it square as _getHeigth said it
  360. imgWidth = height;
  361. }
  362. img.style.width = imgWidth + 'px';
  363. img.style.height = height + 'px';
  364. img.style.marginLeft = '3px';
  365. img.style.marginTop = '3px';
  366. img.style.marginRight = this.margin - 7 + 'px'; // -4 is the negative margin of the inline element
  367. img.style.marginBottom = this.margin - 7 + 'px';
  368. resultNode = img.parentNode.parentNode;
  369. if (!resultNode.classList.contains('js')) {
  370. resultNode.classList.add('js');
  371. }
  372. }
  373. };
  374. ImageLayout.prototype._alignImgs = function (imgGroup) {
  375. var isSearching, slice, i, h;
  376. var containerElement = d.querySelector(this.container_selector);
  377. var containerCompStyles = window.getComputedStyle(containerElement);
  378. var containerPaddingLeft = parseInt(containerCompStyles.getPropertyValue('padding-left'), 10);
  379. var containerPaddingRight = parseInt(containerCompStyles.getPropertyValue('padding-right'), 10);
  380. var containerWidth = containerElement.clientWidth - containerPaddingLeft - containerPaddingRight;
  381. while (imgGroup.length > 0) {
  382. isSearching = true;
  383. for (i = 1; i <= imgGroup.length && isSearching; i++) {
  384. slice = imgGroup.slice(0, i);
  385. h = this._getHeigth(slice, containerWidth);
  386. if (h < this.maxHeight) {
  387. this._setSize(slice, h);
  388. // continue with the remaining images
  389. imgGroup = imgGroup.slice(i);
  390. isSearching = false;
  391. }
  392. }
  393. if (isSearching) {
  394. this._setSize(slice, Math.min(this.maxHeight, h));
  395. break;
  396. }
  397. }
  398. };
  399. ImageLayout.prototype.align = function () {
  400. var i;
  401. var results_selectorNode = d.querySelectorAll(this.results_selector);
  402. var results_length = results_selectorNode.length;
  403. var previous = null;
  404. var current = null;
  405. var imgGroup = [];
  406. for (i = 0; i < results_length; i++) {
  407. current = results_selectorNode[i];
  408. if (current.previousElementSibling !== previous && imgGroup.length > 0) {
  409. // the current image is not connected to previous one
  410. // so the current image is the start of a new group of images.
  411. // so call _alignImgs to align the current group
  412. this._alignImgs(imgGroup);
  413. // and start a new empty group of images
  414. imgGroup = [];
  415. }
  416. // add the current image to the group (only the img tag)
  417. imgGroup.push(current.querySelector(this.img_selector));
  418. // update the previous variable
  419. previous = current;
  420. }
  421. // align the remaining images
  422. if (imgGroup.length > 0) {
  423. this._alignImgs(imgGroup);
  424. }
  425. };
  426. ImageLayout.prototype.watch = function () {
  427. var i, img;
  428. var obj = this;
  429. var results_nodes = d.querySelectorAll(this.results_selector);
  430. var results_length = results_nodes.length;
  431. function throttleAlign() {
  432. if (obj.isAlignDone) {
  433. obj.isAlignDone = false;
  434. setTimeout(function () {
  435. obj.align();
  436. obj.isAlignDone = true;
  437. }, 100);
  438. }
  439. }
  440. w.addEventListener('pageshow', throttleAlign);
  441. w.addEventListener('load', throttleAlign);
  442. w.addEventListener('resize', throttleAlign);
  443. for (i = 0; i < results_length; i++) {
  444. img = results_nodes[i].querySelector(this.img_selector);
  445. if (img !== null && img !== undefined) {
  446. img.addEventListener('load', throttleAlign);
  447. img.addEventListener('error', throttleAlign);
  448. }
  449. }
  450. };
  451. w.searx.ImageLayout = ImageLayout;
  452. }(window, document));