image_layout.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. *
  3. * Google Image Layout v0.0.1
  4. * Description, by Anh Trinh.
  5. * Heavily modified for searx
  6. * https://ptgamr.github.io/2014-09-12-google-image-layout/
  7. * https://ptgamr.github.io/google-image-layout/src/google-image-layout.js
  8. *
  9. * @license Free to use under the MIT License.
  10. *
  11. * @example <caption>Example usage of searxng.ImageLayout class.</caption>
  12. * searxng.image_thumbnail_layout = new searxng.ImageLayout(
  13. * '#urls', // container_selector
  14. * '#urls .result-images', // results_selector
  15. * 'img.image_thumbnail', // img_selector
  16. * 14, // verticalMargin
  17. * 6, // horizontalMargin
  18. * 200 // maxHeight
  19. * );
  20. * searxng.image_thumbnail_layout.watch();
  21. */
  22. (function (w, d) {
  23. function ImageLayout(container_selector, results_selector, img_selector, verticalMargin, horizontalMargin, maxHeight) {
  24. this.container_selector = container_selector;
  25. this.results_selector = results_selector;
  26. this.img_selector = img_selector;
  27. this.verticalMargin = verticalMargin;
  28. this.horizontalMargin = horizontalMargin;
  29. this.maxHeight = maxHeight;
  30. this.isAlignDone = true;
  31. }
  32. /**
  33. * Get the height that make all images fit the container
  34. *
  35. * width = w1 + w2 + w3 + ... = r1*h + r2*h + r3*h + ...
  36. *
  37. * @param {[type]} images the images to be calculated
  38. * @param {[type]} width the container witdth
  39. * @param {[type]} margin the margin between each image
  40. *
  41. * @return {[type]} the height
  42. */
  43. ImageLayout.prototype._getHeigth = function (images, width) {
  44. var i, img;
  45. var r = 0;
  46. for (i = 0; i < images.length; i++) {
  47. img = images[i];
  48. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  49. r += img.naturalWidth / img.naturalHeight;
  50. } else {
  51. // assume that not loaded images are square
  52. r += 1;
  53. }
  54. }
  55. return (width - images.length * this.verticalMargin) / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
  56. };
  57. ImageLayout.prototype._setSize = function (images, height) {
  58. var i, img, imgWidth;
  59. var imagesLength = images.length, resultNode;
  60. for (i = 0; i < imagesLength; i++) {
  61. img = images[i];
  62. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  63. imgWidth = height * img.naturalWidth / img.naturalHeight;
  64. } else {
  65. // not loaded image : make it square as _getHeigth said it
  66. imgWidth = height;
  67. }
  68. img.style.width = imgWidth + 'px';
  69. img.style.height = height + 'px';
  70. img.style.marginLeft = this.horizontalMargin + 'px';
  71. img.style.marginTop = this.horizontalMargin + 'px';
  72. img.style.marginRight = this.verticalMargin - 7 + 'px'; // -4 is the negative margin of the inline element
  73. img.style.marginBottom = this.verticalMargin - 7 + 'px';
  74. resultNode = img.parentNode.parentNode;
  75. if (!resultNode.classList.contains('js')) {
  76. resultNode.classList.add('js');
  77. }
  78. }
  79. };
  80. ImageLayout.prototype._alignImgs = function (imgGroup) {
  81. var isSearching, slice, i, h;
  82. var containerElement = d.querySelector(this.container_selector);
  83. var containerCompStyles = window.getComputedStyle(containerElement);
  84. var containerPaddingLeft = parseInt(containerCompStyles.getPropertyValue('padding-left'), 10);
  85. var containerPaddingRight = parseInt(containerCompStyles.getPropertyValue('padding-right'), 10);
  86. var containerWidth = containerElement.clientWidth - containerPaddingLeft - containerPaddingRight;
  87. while (imgGroup.length > 0) {
  88. isSearching = true;
  89. for (i = 1; i <= imgGroup.length && isSearching; i++) {
  90. slice = imgGroup.slice(0, i);
  91. h = this._getHeigth(slice, containerWidth);
  92. if (h < this.maxHeight) {
  93. this._setSize(slice, h);
  94. // continue with the remaining images
  95. imgGroup = imgGroup.slice(i);
  96. isSearching = false;
  97. }
  98. }
  99. if (isSearching) {
  100. this._setSize(slice, Math.min(this.maxHeight, h));
  101. break;
  102. }
  103. }
  104. };
  105. ImageLayout.prototype.align = function () {
  106. var i;
  107. var results_selectorNode = d.querySelectorAll(this.results_selector);
  108. var results_length = results_selectorNode.length;
  109. var previous = null;
  110. var current = null;
  111. var imgGroup = [];
  112. for (i = 0; i < results_length; i++) {
  113. current = results_selectorNode[i];
  114. if (current.previousElementSibling !== previous && imgGroup.length > 0) {
  115. // the current image is not connected to previous one
  116. // so the current image is the start of a new group of images.
  117. // so call _alignImgs to align the current group
  118. this._alignImgs(imgGroup);
  119. // and start a new empty group of images
  120. imgGroup = [];
  121. }
  122. // add the current image to the group (only the img tag)
  123. imgGroup.push(current.querySelector(this.img_selector));
  124. // update the previous variable
  125. previous = current;
  126. }
  127. // align the remaining images
  128. if (imgGroup.length > 0) {
  129. this._alignImgs(imgGroup);
  130. }
  131. };
  132. ImageLayout.prototype.watch = function () {
  133. var i, img;
  134. var obj = this;
  135. var results_nodes = d.querySelectorAll(this.results_selector);
  136. var results_length = results_nodes.length;
  137. function img_load_error(event) {
  138. // console.log("ERROR can't load: " + event.originalTarget.src);
  139. event.originalTarget.src = w.searxng.static_path + w.searxng.theme.img_load_error;
  140. }
  141. function throttleAlign() {
  142. if (obj.isAlignDone) {
  143. obj.isAlignDone = false;
  144. setTimeout(function () {
  145. obj.align();
  146. obj.isAlignDone = true;
  147. }, 100);
  148. }
  149. }
  150. // https://developer.mozilla.org/en-US/docs/Web/API/Window/pageshow_event
  151. w.addEventListener('pageshow', throttleAlign);
  152. // https://developer.mozilla.org/en-US/docs/Web/API/FileReader/load_event
  153. w.addEventListener('load', throttleAlign);
  154. // https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_event
  155. w.addEventListener('resize', throttleAlign);
  156. for (i = 0; i < results_length; i++) {
  157. img = results_nodes[i].querySelector(this.img_selector);
  158. if (img !== null && img !== undefined) {
  159. img.addEventListener('load', throttleAlign);
  160. // https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
  161. img.addEventListener('error', throttleAlign);
  162. if (w.searxng.theme.img_load_error) {
  163. img.addEventListener('error', img_load_error, {once: true});
  164. }
  165. }
  166. }
  167. };
  168. w.searxng.ImageLayout = ImageLayout;
  169. }(window, document));