logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://anongit.hacktivis.me/git/searx.git/

searx_imageresult.js (4643B)


  1. /**
  2. *
  3. * Google Image Layout v0.0.1
  4. * Description, by Anh Trinh.
  5. * Heavily modified for searx
  6. * http://trinhtrunganh.com
  7. *
  8. * @license Free to use under the MIT License.
  9. *
  10. */
  11. (function(w, d) {
  12. 'use strict';
  13. function ImageLayout(container_selector, results_selector, img_selector, maxHeight) {
  14. this.container_selector = container_selector;
  15. this.results_selector = results_selector;
  16. this.img_selector = img_selector;
  17. this.margin = 10;
  18. this.maxHeight = maxHeight;
  19. this._alignAllDone = true;
  20. }
  21. /**
  22. * Get the height that make all images fit the container
  23. *
  24. * width = w1 + w2 + w3 + ... = r1*h + r2*h + r3*h + ...
  25. *
  26. * @param {[type]} images the images to be calculated
  27. * @param {[type]} width the container witdth
  28. * @param {[type]} margin the margin between each image
  29. *
  30. * @return {[type]} the height
  31. */
  32. ImageLayout.prototype._getHeigth = function(images, width) {
  33. var r = 0,
  34. img;
  35. width -= images.length * this.margin;
  36. for (var i = 0; i < images.length; i++) {
  37. img = images[i];
  38. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  39. r += img.naturalWidth / img.naturalHeight;
  40. } else {
  41. // assume that not loaded images are square
  42. r += 1;
  43. }
  44. }
  45. return width / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
  46. };
  47. ImageLayout.prototype._setSize = function(images, height) {
  48. var img, imgWidth, imagesLength = images.length;
  49. for (var i = 0; i < imagesLength; i++) {
  50. img = images[i];
  51. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  52. imgWidth = height * img.naturalWidth / img.naturalHeight;
  53. } else {
  54. // not loaded image : make it square as _getHeigth said it
  55. imgWidth = height;
  56. }
  57. img.style.width = imgWidth + 'px';
  58. img.style.height = height + 'px';
  59. img.style.marginLeft = '3px';
  60. img.style.marginTop = '3px';
  61. img.style.marginRight = this.margin - 7 + 'px'; // -4 is the negative margin of the inline element
  62. img.style.marginBottom = this.margin - 7 + 'px';
  63. }
  64. };
  65. ImageLayout.prototype._alignImgs = function(imgGroup) {
  66. var slice, h,
  67. containerWidth = d.querySelector(this.container_selector).clientWidth;
  68. w: while (imgGroup.length > 0) {
  69. for (var i = 1; i <= imgGroup.length; i++) {
  70. slice = imgGroup.slice(0, i);
  71. h = this._getHeigth(slice, containerWidth);
  72. if (h < this.maxHeight) {
  73. this._setSize(slice, h);
  74. imgGroup = imgGroup.slice(i);
  75. continue w;
  76. }
  77. }
  78. this._setSize(slice, Math.min(this.maxHeight, h));
  79. break;
  80. }
  81. };
  82. ImageLayout.prototype.align = function(results_selector) {
  83. var results_selectorNode = d.querySelectorAll(this.results_selector),
  84. results_length = results_selectorNode.length,
  85. previous = null,
  86. current = null,
  87. imgGroup = [];
  88. for (var i = 0; i < results_length; i++) {
  89. current = results_selectorNode[i];
  90. if (current.previousElementSibling !== previous && imgGroup.length > 0) {
  91. // the current image is not conected to previous one
  92. // so the current image is the start of a new group of images.
  93. // so call _alignImgs to align the current group
  94. this._alignImgs(imgGroup);
  95. // and start a new empty group of images
  96. imgGroup = [];
  97. }
  98. // add the current image to the group (only the img tag)
  99. imgGroup.push(current.querySelector(this.img_selector));
  100. // update the previous variable
  101. previous = current;
  102. }
  103. // align the remaining images
  104. if (imgGroup.length > 0) {
  105. this._alignImgs(imgGroup);
  106. }
  107. };
  108. ImageLayout.prototype.watch = function() {
  109. var i, img, imgGroup, imgNodeLength,
  110. obj = this,
  111. results_nodes = d.querySelectorAll(this.results_selector),
  112. results_length = results_nodes.length;
  113. function align(e) {
  114. obj.align();
  115. }
  116. function throttleAlign(e) {
  117. if (obj._alignAllDone) {
  118. obj._alignAllDone = false;
  119. setTimeout(function() {
  120. obj.align();
  121. obj._alignAllDone = true;
  122. }, 100);
  123. }
  124. }
  125. w.addEventListener('resize', throttleAlign);
  126. w.addEventListener('pageshow', align);
  127. for (i = 0; i < results_length; i++) {
  128. img = results_nodes[i].querySelector(this.img_selector);
  129. if (typeof img !== 'undefined') {
  130. img.addEventListener('load', throttleAlign);
  131. img.addEventListener('error', throttleAlign);
  132. }
  133. }
  134. };
  135. w.searx.ImageLayout = ImageLayout;
  136. })(window, document);