image_layout.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.trottleCallToAlign = null;
  31. this.alignAfterThrotteling = false;
  32. }
  33. /**
  34. * Get the height that make all images fit the container
  35. *
  36. * width = w1 + w2 + w3 + ... = r1*h + r2*h + r3*h + ...
  37. *
  38. * @param {[type]} images the images to be calculated
  39. * @param {[type]} width the container witdth
  40. * @param {[type]} margin the margin between each image
  41. *
  42. * @return {[type]} the height
  43. */
  44. ImageLayout.prototype._getHeigth = function (images, width) {
  45. var i, img;
  46. var r = 0;
  47. for (i = 0; i < images.length; i++) {
  48. img = images[i];
  49. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  50. r += img.naturalWidth / img.naturalHeight;
  51. } else {
  52. // assume that not loaded images are square
  53. r += 1;
  54. }
  55. }
  56. return (width - images.length * this.verticalMargin) / r; // have to round down because Firefox will automatically roundup value with number of decimals > 3
  57. };
  58. ImageLayout.prototype._setSize = function (images, height) {
  59. var i, img, imgWidth;
  60. var imagesLength = images.length, resultNode;
  61. for (i = 0; i < imagesLength; i++) {
  62. img = images[i];
  63. if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
  64. imgWidth = height * img.naturalWidth / img.naturalHeight;
  65. } else {
  66. // not loaded image : make it square as _getHeigth said it
  67. imgWidth = height;
  68. }
  69. img.setAttribute('width', Math.round(imgWidth));
  70. img.setAttribute('height', Math.round(height));
  71. img.style.marginLeft = Math.round(this.horizontalMargin) + 'px';
  72. img.style.marginTop = Math.round(this.horizontalMargin) + 'px';
  73. img.style.marginRight = Math.round(this.verticalMargin - 7) + 'px'; // -4 is the negative margin of the inline element
  74. img.style.marginBottom = Math.round(this.verticalMargin - 7) + 'px';
  75. resultNode = img.parentNode.parentNode;
  76. if (!resultNode.classList.contains('js')) {
  77. resultNode.classList.add('js');
  78. }
  79. }
  80. };
  81. ImageLayout.prototype._alignImgs = function (imgGroup) {
  82. var isSearching, slice, i, h;
  83. var containerElement = d.querySelector(this.container_selector);
  84. var containerCompStyles = window.getComputedStyle(containerElement);
  85. var containerPaddingLeft = parseInt(containerCompStyles.getPropertyValue('padding-left'), 10);
  86. var containerPaddingRight = parseInt(containerCompStyles.getPropertyValue('padding-right'), 10);
  87. var containerWidth = containerElement.clientWidth - containerPaddingLeft - containerPaddingRight;
  88. while (imgGroup.length > 0) {
  89. isSearching = true;
  90. for (i = 1; i <= imgGroup.length && isSearching; i++) {
  91. slice = imgGroup.slice(0, i);
  92. h = this._getHeigth(slice, containerWidth);
  93. if (h < this.maxHeight) {
  94. this._setSize(slice, h);
  95. // continue with the remaining images
  96. imgGroup = imgGroup.slice(i);
  97. isSearching = false;
  98. }
  99. }
  100. if (isSearching) {
  101. this._setSize(slice, Math.min(this.maxHeight, h));
  102. break;
  103. }
  104. }
  105. };
  106. ImageLayout.prototype.throttleAlign = function () {
  107. var obj = this;
  108. if (obj.trottleCallToAlign) {
  109. obj.alignAfterThrotteling = true;
  110. } else {
  111. obj.alignAfterThrotteling = false;
  112. obj.align();
  113. obj.trottleCallToAlign = setTimeout(function () {
  114. if (obj.alignAfterThrotteling) {
  115. obj.align();
  116. }
  117. obj.alignAfterThrotteling = false;
  118. obj.trottleCallToAlign = null;
  119. }, 20);
  120. }
  121. }
  122. ImageLayout.prototype.align = function () {
  123. var i;
  124. var results_selectorNode = d.querySelectorAll(this.results_selector);
  125. var results_length = results_selectorNode.length;
  126. var previous = null;
  127. var current = null;
  128. var imgGroup = [];
  129. for (i = 0; i < results_length; i++) {
  130. current = results_selectorNode[i];
  131. if (current.previousElementSibling !== previous && imgGroup.length > 0) {
  132. // the current image is not connected to previous one
  133. // so the current image is the start of a new group of images.
  134. // so call _alignImgs to align the current group
  135. this._alignImgs(imgGroup);
  136. // and start a new empty group of images
  137. imgGroup = [];
  138. }
  139. // add the current image to the group (only the img tag)
  140. imgGroup.push(current.querySelector(this.img_selector));
  141. // update the previous variable
  142. previous = current;
  143. }
  144. // align the remaining images
  145. if (imgGroup.length > 0) {
  146. this._alignImgs(imgGroup);
  147. }
  148. };
  149. ImageLayout.prototype._monitorImages = function () {
  150. var i, img;
  151. var objthrottleAlign = this.throttleAlign.bind(this);
  152. var results_nodes = d.querySelectorAll(this.results_selector);
  153. var results_length = results_nodes.length;
  154. function img_load_error (event) {
  155. // console.log("ERROR can't load: " + event.originalTarget.src);
  156. event.originalTarget.src = w.searxng.static_path + w.searxng.theme.img_load_error;
  157. }
  158. for (i = 0; i < results_length; i++) {
  159. img = results_nodes[i].querySelector(this.img_selector);
  160. if (img !== null && img !== undefined && !img.classList.contains('aligned')) {
  161. img.addEventListener('load', objthrottleAlign);
  162. // https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
  163. img.addEventListener('error', objthrottleAlign);
  164. img.addEventListener('timeout', objthrottleAlign);
  165. if (w.searxng.theme.img_load_error) {
  166. img.addEventListener('error', img_load_error, {once: true});
  167. }
  168. img.classList.add('aligned');
  169. }
  170. }
  171. }
  172. ImageLayout.prototype.watch = function () {
  173. var objthrottleAlign = this.throttleAlign.bind(this);
  174. // https://developer.mozilla.org/en-US/docs/Web/API/Window/pageshow_event
  175. w.addEventListener('pageshow', objthrottleAlign);
  176. // https://developer.mozilla.org/en-US/docs/Web/API/FileReader/load_event
  177. w.addEventListener('load', objthrottleAlign);
  178. // https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_event
  179. w.addEventListener('resize', objthrottleAlign);
  180. this._monitorImages();
  181. var obj = this;
  182. let observer = new MutationObserver(entries => {
  183. let newElement = false;
  184. for (let i = 0; i < entries.length; i++) {
  185. if (entries[i].addedNodes.length > 0 && entries[i].addedNodes[0].classList.contains('result')) {
  186. newElement = true;
  187. break;
  188. }
  189. }
  190. if (newElement) {
  191. obj._monitorImages();
  192. }
  193. });
  194. observer.observe(d.querySelector(this.container_selector), {
  195. childList: true,
  196. subtree: true,
  197. attributes: false,
  198. characterData: false,
  199. })
  200. };
  201. w.searxng.ImageLayout = ImageLayout;
  202. }(window, document));