searxng.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /**
  2. * @license
  3. * (C) Copyright Contributors to the SearXNG project.
  4. * (C) Copyright Contributors to the searx project (2014 - 2021).
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. window.searxng = (function(d) {
  8. 'use strict';
  9. //
  10. d.getElementsByTagName("html")[0].className = "js";
  11. // add data- properties
  12. var script = d.currentScript || (function() {
  13. var scripts = d.getElementsByTagName('script');
  14. return scripts[scripts.length - 1];
  15. })();
  16. return {
  17. autocompleter: script.getAttribute('data-autocompleter') === 'true',
  18. method: script.getAttribute('data-method'),
  19. translations: JSON.parse(script.getAttribute('data-translations'))
  20. };
  21. })(document);
  22. window.searx = {};
  23. ;/**
  24. * @license
  25. * (C) Copyright Contributors to the SearXNG project.
  26. * (C) Copyright Contributors to the searx project (2014 - 2021).
  27. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  28. * SPDX-License-Identifier: AGPL-3.0-or-later
  29. */
  30. $(document).ready(function(){
  31. var original_search_value = '';
  32. if(searxng.autocompleter) {
  33. var searchResults = new Bloodhound({
  34. datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  35. queryTokenizer: Bloodhound.tokenizers.whitespace,
  36. remote: {
  37. url: './autocompleter?q=%QUERY',
  38. wildcard: '%QUERY'
  39. }
  40. });
  41. searchResults.initialize();
  42. $("#q").on('keydown', function(e) {
  43. if(e.which == 13) {
  44. original_search_value = $('#q').val();
  45. }
  46. });
  47. $('#q').typeahead({
  48. name: 'search-results',
  49. highlight: false,
  50. hint: true,
  51. displayKey: function(result) {
  52. return result;
  53. },
  54. classNames: {
  55. input: 'tt-input',
  56. hint: 'tt-hint',
  57. menu: 'tt-dropdown-menu',
  58. dataset: 'tt-dataset-search-results',
  59. },
  60. }, {
  61. name: 'autocomplete',
  62. source: searchResults,
  63. });
  64. $('#q').bind('typeahead:select', function(ev, suggestion) {
  65. if(original_search_value) {
  66. $('#q').val(original_search_value);
  67. }
  68. $("#search_form").submit();
  69. });
  70. }
  71. });
  72. ;/**
  73. * @license
  74. * (C) Copyright Contributors to the SearXNG project.
  75. * (C) Copyright Contributors to the searx project (2014 - 2021).
  76. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  77. * SPDX-License-Identifier: AGPL-3.0-or-later
  78. */
  79. $(document).ready(function(){
  80. /**
  81. * focus element if class="autofocus" and id="q"
  82. */
  83. $('#q.autofocus').focus();
  84. /**
  85. * Empty search bar when click on reset button
  86. */
  87. $("#clear_search").click(function () {
  88. document.getElementById("q").value = "";
  89. });
  90. /**
  91. * select full content on click if class="select-all-on-click"
  92. */
  93. $(".select-all-on-click").click(function () {
  94. $(this).select();
  95. });
  96. /**
  97. * change text during btn-collapse click if possible
  98. */
  99. $('.btn-collapse').click(function() {
  100. var btnTextCollapsed = $(this).data('btn-text-collapsed');
  101. var btnTextNotCollapsed = $(this).data('btn-text-not-collapsed');
  102. if(btnTextCollapsed !== '' && btnTextNotCollapsed !== '') {
  103. if($(this).hasClass('collapsed')) {
  104. new_html = $(this).html().replace(btnTextCollapsed, btnTextNotCollapsed);
  105. } else {
  106. new_html = $(this).html().replace(btnTextNotCollapsed, btnTextCollapsed);
  107. }
  108. $(this).html(new_html);
  109. }
  110. });
  111. /**
  112. * change text during btn-toggle click if possible
  113. */
  114. $('.btn-toggle .btn').click(function() {
  115. var btnClass = 'btn-' + $(this).data('btn-class');
  116. var btnLabelDefault = $(this).data('btn-label-default');
  117. var btnLabelToggled = $(this).data('btn-label-toggled');
  118. if(btnLabelToggled !== '') {
  119. if($(this).hasClass('btn-default')) {
  120. new_html = $(this).html().replace(btnLabelDefault, btnLabelToggled);
  121. } else {
  122. new_html = $(this).html().replace(btnLabelToggled, btnLabelDefault);
  123. }
  124. $(this).html(new_html);
  125. }
  126. $(this).toggleClass(btnClass);
  127. $(this).toggleClass('btn-default');
  128. });
  129. /**
  130. * change text during btn-toggle click if possible
  131. */
  132. $('.media-loader').click(function() {
  133. var target = $(this).data('target');
  134. var iframe_load = $(target + ' > iframe');
  135. var srctest = iframe_load.attr('src');
  136. if(srctest === undefined || srctest === false){
  137. iframe_load.attr('src', iframe_load.data('src'));
  138. }
  139. });
  140. /**
  141. * Select or deselect every categories on double clic
  142. */
  143. $(".btn-sm").dblclick(function() {
  144. var btnClass = 'btn-' + $(this).data('btn-class'); // primary
  145. if($(this).hasClass('btn-default')) {
  146. $(".btn-sm > input").attr('checked', 'checked');
  147. $(".btn-sm > input").prop("checked", true);
  148. $(".btn-sm").addClass(btnClass);
  149. $(".btn-sm").addClass('active');
  150. $(".btn-sm").removeClass('btn-default');
  151. } else {
  152. $(".btn-sm > input").attr('checked', '');
  153. $(".btn-sm > input").removeAttr('checked');
  154. $(".btn-sm > input").checked = false;
  155. $(".btn-sm").removeClass(btnClass);
  156. $(".btn-sm").removeClass('active');
  157. $(".btn-sm").addClass('btn-default');
  158. }
  159. });
  160. $(".nav-tabs").click(function(a) {
  161. var tabs = $(a.target).parents("ul");
  162. tabs.children().attr("aria-selected", "false");
  163. $(a.target).parent().attr("aria-selected", "true");
  164. });
  165. /**
  166. * Layout images according to their sizes
  167. */
  168. searxng.image_thumbnail_layout = new searx.ImageLayout('#main_results', '#main_results .result-images', 'img.img-thumbnail', 15, 200);
  169. searxng.image_thumbnail_layout.watch();
  170. });
  171. ;/**
  172. * @license
  173. * (C) Copyright Contributors to the SearXNG project.
  174. * (C) Copyright Contributors to the searx project (2014 - 2021).
  175. * SPDX-License-Identifier: AGPL-3.0-or-later
  176. */
  177. window.addEventListener('load', function() {
  178. // Hide infobox toggle if shrunk size already fits all content.
  179. $('.infobox').each(function() {
  180. var infobox_body = $(this).find('.infobox_body');
  181. var total_height = infobox_body.prop('scrollHeight') + infobox_body.find('img.infobox_part').height();
  182. var max_height = infobox_body.css('max-height').replace('px', '');
  183. if (total_height <= max_height) {
  184. $(this).find('.infobox_toggle').hide();
  185. }
  186. });
  187. });
  188. ;/**
  189. * @license
  190. * (C) Copyright Contributors to the SearXNG project.
  191. * (C) Copyright Contributors to the searx project (2014 - 2021).
  192. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  193. * SPDX-License-Identifier: AGPL-3.0-or-later
  194. */
  195. $(document).ready(function(){
  196. $(".searxng_init_map").on( "click", function( event ) {
  197. var leaflet_target = $(this).data('leaflet-target');
  198. var map_lon = $(this).data('map-lon');
  199. var map_lat = $(this).data('map-lat');
  200. var map_zoom = $(this).data('map-zoom');
  201. var map_boundingbox = $(this).data('map-boundingbox');
  202. var map_geojson = $(this).data('map-geojson');
  203. if(map_boundingbox) {
  204. southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]);
  205. northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]);
  206. map_bounds = L.latLngBounds(southWest, northEast);
  207. }
  208. // change default imagePath
  209. L.Icon.Default.imagePath = "./static/themes/oscar/css/images/";
  210. // init map
  211. var map = L.map(leaflet_target);
  212. // create the tile layer with correct attribution
  213. var osmMapnikUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  214. var osmMapnikAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  215. var osmMapnik = new L.TileLayer(osmMapnikUrl, {minZoom: 1, maxZoom: 19, attribution: osmMapnikAttrib});
  216. var osmWikimediaUrl='https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png';
  217. var osmWikimediaAttrib = 'Wikimedia maps beta | Maps data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  218. var osmWikimedia = new L.TileLayer(osmWikimediaUrl, {minZoom: 1, maxZoom: 19, attribution: osmWikimediaAttrib});
  219. // init map view
  220. setTimeout(function() {
  221. if(map_bounds) {
  222. map.fitBounds(map_bounds, {
  223. maxZoom:17
  224. });
  225. } else if (map_lon && map_lat) {
  226. if(map_zoom)
  227. map.setView(new L.LatLng(map_lat, map_lon),map_zoom);
  228. else
  229. map.setView(new L.LatLng(map_lat, map_lon),8);
  230. }
  231. }, 0);
  232. map.addLayer(osmMapnik);
  233. var baseLayers = {
  234. "OSM Mapnik": osmMapnik/*,
  235. "OSM Wikimedia": osmWikimedia*/
  236. };
  237. L.control.layers(baseLayers).addTo(map);
  238. if(map_geojson)
  239. L.geoJson(map_geojson).addTo(map);
  240. /*else if(map_bounds)
  241. L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);*/
  242. // this event occour only once per element
  243. $( this ).off( event );
  244. });
  245. });
  246. ;/**
  247. * @license
  248. * (C) Copyright Contributors to the SearXNG project.
  249. * (C) Copyright Contributors to the searx project (2014 - 2021).
  250. * SPDX-License-Identifier: AGPL-3.0-or-later
  251. */
  252. $(document).ready(function(){
  253. let engine_descriptions = null;
  254. function load_engine_descriptions() {
  255. if (engine_descriptions == null) {
  256. $.ajax("engine_descriptions.json", dataType="json").done(function(data) {
  257. engine_descriptions = data;
  258. for (const [engine_name, description] of Object.entries(data)) {
  259. let elements = $('[data-engine-name="' + engine_name + '"] .description');
  260. for(const element of elements) {
  261. let source = ' (<i>' + searxng.translations.Source + ':&nbsp;' + description[1] + '</i>)';
  262. element.innerHTML = description[0] + source;
  263. }
  264. }
  265. });
  266. }
  267. }
  268. if (document.querySelector('body[class="preferences_endpoint"]')) {
  269. $('[data-engine-name]').hover(function() {
  270. load_engine_descriptions();
  271. });
  272. }
  273. });
  274. ;/**
  275. * @license
  276. * (C) Copyright Contributors to the SearXNG project.
  277. * (C) Copyright Contributors to the searx project (2014 - 2021).
  278. * SPDX-License-Identifier: AGPL-3.0-or-later
  279. */
  280. $(document).ready(function(){
  281. $("#allow-all-engines").click(function() {
  282. $(".onoffswitch-checkbox").each(function() { this.checked = false;});
  283. });
  284. $("#disable-all-engines").click(function() {
  285. $(".onoffswitch-checkbox").each(function() { this.checked = true;});
  286. });
  287. });
  288. ;/**
  289. *
  290. * Google Image Layout v0.0.1
  291. * Description, by Anh Trinh.
  292. * Heavily modified for searx
  293. * https://ptgamr.github.io/2014-09-12-google-image-layout/
  294. * https://ptgamr.github.io/google-image-layout/src/google-image-layout.js
  295. *
  296. * @license Free to use under the MIT License.
  297. *
  298. */
  299. (function (w, d) {
  300. function ImageLayout(container_selector, results_selector, img_selector, margin, maxHeight) {
  301. this.container_selector = container_selector;
  302. this.results_selector = results_selector;
  303. this.img_selector = img_selector;
  304. this.margin = margin;
  305. this.maxHeight = maxHeight;
  306. this.isAlignDone = true;
  307. }
  308. /**
  309. * Get the height that make all images fit the container
  310. *
  311. * width = w1 + w2 + w3 + ... = r1*h + r2*h + r3*h + ...
  312. *
  313. * @param {[type]} images the images to be calculated
  314. * @param {[type]} width the container witdth
  315. * @param {[type]} margin the margin between each image
  316. *
  317. * @return {[type]} the height
  318. */
  319. ImageLayout.prototype._getHeigth = function (images, width) {
  320. var i, img;
  321. var r = 0;
  322. for (i = 0; i < images.length; i++) {
  323. img = images[i];
  324. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  325. r += img.naturalWidth / img.naturalHeight;
  326. } else {
  327. // assume that not loaded images are square
  328. r += 1;
  329. }
  330. }
  331. return (width - images.length * this.margin) / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
  332. };
  333. ImageLayout.prototype._setSize = function (images, height) {
  334. var i, img, imgWidth;
  335. var imagesLength = images.length, resultNode;
  336. for (i = 0; i < imagesLength; i++) {
  337. img = images[i];
  338. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  339. imgWidth = height * img.naturalWidth / img.naturalHeight;
  340. } else {
  341. // not loaded image : make it square as _getHeigth said it
  342. imgWidth = height;
  343. }
  344. img.style.width = imgWidth + 'px';
  345. img.style.height = height + 'px';
  346. img.style.marginLeft = '3px';
  347. img.style.marginTop = '3px';
  348. img.style.marginRight = this.margin - 7 + 'px'; // -4 is the negative margin of the inline element
  349. img.style.marginBottom = this.margin - 7 + 'px';
  350. resultNode = img.parentNode.parentNode;
  351. if (!resultNode.classList.contains('js')) {
  352. resultNode.classList.add('js');
  353. }
  354. }
  355. };
  356. ImageLayout.prototype._alignImgs = function (imgGroup) {
  357. var isSearching, slice, i, h;
  358. var containerElement = d.querySelector(this.container_selector);
  359. var containerCompStyles = window.getComputedStyle(containerElement);
  360. var containerPaddingLeft = parseInt(containerCompStyles.getPropertyValue('padding-left'), 10);
  361. var containerPaddingRight = parseInt(containerCompStyles.getPropertyValue('padding-right'), 10);
  362. var containerWidth = containerElement.clientWidth - containerPaddingLeft - containerPaddingRight;
  363. while (imgGroup.length > 0) {
  364. isSearching = true;
  365. for (i = 1; i <= imgGroup.length && isSearching; i++) {
  366. slice = imgGroup.slice(0, i);
  367. h = this._getHeigth(slice, containerWidth);
  368. if (h < this.maxHeight) {
  369. this._setSize(slice, h);
  370. // continue with the remaining images
  371. imgGroup = imgGroup.slice(i);
  372. isSearching = false;
  373. }
  374. }
  375. if (isSearching) {
  376. this._setSize(slice, Math.min(this.maxHeight, h));
  377. break;
  378. }
  379. }
  380. };
  381. ImageLayout.prototype.align = function () {
  382. var i;
  383. var results_selectorNode = d.querySelectorAll(this.results_selector);
  384. var results_length = results_selectorNode.length;
  385. var previous = null;
  386. var current = null;
  387. var imgGroup = [];
  388. for (i = 0; i < results_length; i++) {
  389. current = results_selectorNode[i];
  390. if (current.previousElementSibling !== previous && imgGroup.length > 0) {
  391. // the current image is not connected to previous one
  392. // so the current image is the start of a new group of images.
  393. // so call _alignImgs to align the current group
  394. this._alignImgs(imgGroup);
  395. // and start a new empty group of images
  396. imgGroup = [];
  397. }
  398. // add the current image to the group (only the img tag)
  399. imgGroup.push(current.querySelector(this.img_selector));
  400. // update the previous variable
  401. previous = current;
  402. }
  403. // align the remaining images
  404. if (imgGroup.length > 0) {
  405. this._alignImgs(imgGroup);
  406. }
  407. };
  408. ImageLayout.prototype.watch = function () {
  409. var i, img;
  410. var obj = this;
  411. var results_nodes = d.querySelectorAll(this.results_selector);
  412. var results_length = results_nodes.length;
  413. function throttleAlign() {
  414. if (obj.isAlignDone) {
  415. obj.isAlignDone = false;
  416. setTimeout(function () {
  417. obj.align();
  418. obj.isAlignDone = true;
  419. }, 100);
  420. }
  421. }
  422. w.addEventListener('pageshow', throttleAlign);
  423. w.addEventListener('load', throttleAlign);
  424. w.addEventListener('resize', throttleAlign);
  425. for (i = 0; i < results_length; i++) {
  426. img = results_nodes[i].querySelector(this.img_selector);
  427. if (img !== null && img !== undefined) {
  428. img.addEventListener('load', throttleAlign);
  429. img.addEventListener('error', throttleAlign);
  430. }
  431. }
  432. };
  433. w.searx.ImageLayout = ImageLayout;
  434. }(window, document));