123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- (function(w, d) {
- 'use strict';
-
- function ImageLayout(container_selector, results_selector, img_selector, maxHeight) {
- this.container_selector = container_selector;
- this.results_selector = results_selector;
- this.img_selector = img_selector;
- this.margin = 10;
- this.maxHeight = maxHeight;
- this._alignAllDone = true;
- }
-
- ImageLayout.prototype._getHeigth = function(images, width) {
- var r = 0,
- img;
- width -= images.length * this.margin;
- for (var i = 0; i < images.length; i++) {
- img = images[i];
- if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
- r += img.naturalWidth / img.naturalHeight;
- } else {
-
- r += 1;
- }
- }
- return width / r;
- };
- ImageLayout.prototype._setSize = function(images, height) {
- var img, imgWidth, imagesLength = images.length;
- for (var i = 0; i < imagesLength; i++) {
- img = images[i];
- if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
- imgWidth = height * img.naturalWidth / img.naturalHeight;
- } else {
-
- imgWidth = height;
- }
- img.style.width = imgWidth + 'px';
- img.style.height = height + 'px';
- img.style.marginLeft = '3px';
- img.style.marginTop = '3px';
- img.style.marginRight = this.margin - 7 + 'px';
- img.style.marginBottom = this.margin - 7 + 'px';
- }
- };
- ImageLayout.prototype._alignImgs = function(imgGroup) {
- var slice, h,
- containerWidth = d.querySelector(this.container_selector).clientWidth;
- w: while (imgGroup.length > 0) {
- for (var i = 1; i <= imgGroup.length; i++) {
- slice = imgGroup.slice(0, i);
- h = this._getHeigth(slice, containerWidth);
- if (h < this.maxHeight) {
- this._setSize(slice, h);
- imgGroup = imgGroup.slice(i);
- continue w;
- }
- }
- this._setSize(slice, Math.min(this.maxHeight, h));
- break;
- }
- };
- ImageLayout.prototype.align = function(results_selector) {
- var results_selectorNode = d.querySelectorAll(this.results_selector),
- results_length = results_selectorNode.length,
- previous = null,
- current = null,
- imgGroup = [];
- for (var i = 0; i < results_length; i++) {
- current = results_selectorNode[i];
- if (current.previousElementSibling !== previous && imgGroup.length > 0) {
-
-
-
- this._alignImgs(imgGroup);
-
- imgGroup = [];
- }
-
- imgGroup.push(current.querySelector(this.img_selector));
-
- previous = current;
- }
-
- if (imgGroup.length > 0) {
- this._alignImgs(imgGroup);
- }
- };
- ImageLayout.prototype.watch = function() {
- var i, img, imgGroup, imgNodeLength,
- obj = this,
- results_nodes = d.querySelectorAll(this.results_selector),
- results_length = results_nodes.length;
- function align(e) {
- obj.align();
- }
- function throttleAlign(e) {
- if (obj._alignAllDone) {
- obj._alignAllDone = false;
- setTimeout(function() {
- obj.align();
- obj._alignAllDone = true;
- }, 100);
- }
- }
- w.addEventListener('resize', throttleAlign);
- w.addEventListener('pageshow', align);
- for (i = 0; i < results_length; i++) {
- img = results_nodes[i].querySelector(this.img_selector);
- if (typeof img !== 'undefined') {
- img.addEventListener('load', throttleAlign);
- img.addEventListener('error', throttleAlign);
- }
- }
- };
- w.searx.ImageLayout = ImageLayout;
- })(window, document);
|