searx.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. $("#allow-all-engines").click(function() {
  278. $(".onoffswitch-checkbox").each(function() { this.checked = false;});
  279. });
  280. $("#disable-all-engines").click(function() {
  281. $(".onoffswitch-checkbox").each(function() { this.checked = true;});
  282. });
  283. });
  284. ;/**
  285. *
  286. * Google Image Layout v0.0.1
  287. * Description, by Anh Trinh.
  288. * Heavily modified for searx
  289. * https://ptgamr.github.io/2014-09-12-google-image-layout/
  290. * https://ptgamr.github.io/google-image-layout/src/google-image-layout.js
  291. *
  292. * @license Free to use under the MIT License.
  293. *
  294. */
  295. (function (w, d) {
  296. function ImageLayout(container_selector, results_selector, img_selector, margin, maxHeight) {
  297. this.container_selector = container_selector;
  298. this.results_selector = results_selector;
  299. this.img_selector = img_selector;
  300. this.margin = margin;
  301. this.maxHeight = maxHeight;
  302. this.isAlignDone = true;
  303. }
  304. /**
  305. * Get the height that make all images fit the container
  306. *
  307. * width = w1 + w2 + w3 + ... = r1*h + r2*h + r3*h + ...
  308. *
  309. * @param {[type]} images the images to be calculated
  310. * @param {[type]} width the container witdth
  311. * @param {[type]} margin the margin between each image
  312. *
  313. * @return {[type]} the height
  314. */
  315. ImageLayout.prototype._getHeigth = function (images, width) {
  316. var i, img;
  317. var r = 0;
  318. for (i = 0; i < images.length; i++) {
  319. img = images[i];
  320. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  321. r += img.naturalWidth / img.naturalHeight;
  322. } else {
  323. // assume that not loaded images are square
  324. r += 1;
  325. }
  326. }
  327. return (width - images.length * this.margin) / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
  328. };
  329. ImageLayout.prototype._setSize = function (images, height) {
  330. var i, img, imgWidth;
  331. var imagesLength = images.length, resultNode;
  332. for (i = 0; i < imagesLength; i++) {
  333. img = images[i];
  334. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  335. imgWidth = height * img.naturalWidth / img.naturalHeight;
  336. } else {
  337. // not loaded image : make it square as _getHeigth said it
  338. imgWidth = height;
  339. }
  340. img.style.width = imgWidth + 'px';
  341. img.style.height = height + 'px';
  342. img.style.marginLeft = '3px';
  343. img.style.marginTop = '3px';
  344. img.style.marginRight = this.margin - 7 + 'px'; // -4 is the negative margin of the inline element
  345. img.style.marginBottom = this.margin - 7 + 'px';
  346. resultNode = img.parentNode.parentNode;
  347. if (!resultNode.classList.contains('js')) {
  348. resultNode.classList.add('js');
  349. }
  350. }
  351. };
  352. ImageLayout.prototype._alignImgs = function (imgGroup) {
  353. var isSearching, slice, i, h;
  354. var containerElement = d.querySelector(this.container_selector);
  355. var containerCompStyles = window.getComputedStyle(containerElement);
  356. var containerPaddingLeft = parseInt(containerCompStyles.getPropertyValue('padding-left'), 10);
  357. var containerPaddingRight = parseInt(containerCompStyles.getPropertyValue('padding-right'), 10);
  358. var containerWidth = containerElement.clientWidth - containerPaddingLeft - containerPaddingRight;
  359. while (imgGroup.length > 0) {
  360. isSearching = true;
  361. for (i = 1; i <= imgGroup.length && isSearching; i++) {
  362. slice = imgGroup.slice(0, i);
  363. h = this._getHeigth(slice, containerWidth);
  364. if (h < this.maxHeight) {
  365. this._setSize(slice, h);
  366. // continue with the remaining images
  367. imgGroup = imgGroup.slice(i);
  368. isSearching = false;
  369. }
  370. }
  371. if (isSearching) {
  372. this._setSize(slice, Math.min(this.maxHeight, h));
  373. break;
  374. }
  375. }
  376. };
  377. ImageLayout.prototype.align = function () {
  378. var i;
  379. var results_selectorNode = d.querySelectorAll(this.results_selector);
  380. var results_length = results_selectorNode.length;
  381. var previous = null;
  382. var current = null;
  383. var imgGroup = [];
  384. for (i = 0; i < results_length; i++) {
  385. current = results_selectorNode[i];
  386. if (current.previousElementSibling !== previous && imgGroup.length > 0) {
  387. // the current image is not connected to previous one
  388. // so the current image is the start of a new group of images.
  389. // so call _alignImgs to align the current group
  390. this._alignImgs(imgGroup);
  391. // and start a new empty group of images
  392. imgGroup = [];
  393. }
  394. // add the current image to the group (only the img tag)
  395. imgGroup.push(current.querySelector(this.img_selector));
  396. // update the previous variable
  397. previous = current;
  398. }
  399. // align the remaining images
  400. if (imgGroup.length > 0) {
  401. this._alignImgs(imgGroup);
  402. }
  403. };
  404. ImageLayout.prototype.watch = function () {
  405. var i, img;
  406. var obj = this;
  407. var results_nodes = d.querySelectorAll(this.results_selector);
  408. var results_length = results_nodes.length;
  409. function throttleAlign() {
  410. if (obj.isAlignDone) {
  411. obj.isAlignDone = false;
  412. setTimeout(function () {
  413. obj.align();
  414. obj.isAlignDone = true;
  415. }, 100);
  416. }
  417. }
  418. w.addEventListener('pageshow', throttleAlign);
  419. w.addEventListener('load', throttleAlign);
  420. w.addEventListener('resize', throttleAlign);
  421. for (i = 0; i < results_length; i++) {
  422. img = results_nodes[i].querySelector(this.img_selector);
  423. if (img !== null && img !== undefined) {
  424. img.addEventListener('load', throttleAlign);
  425. img.addEventListener('error', throttleAlign);
  426. }
  427. }
  428. };
  429. w.searx.ImageLayout = ImageLayout;
  430. }(window, document));