Merge remote-tracking branch 'dalf/oscar-images'

This commit is contained in:
Alexandre Flament 2021-04-19 09:36:54 +02:00
commit 92db0227b1
25 changed files with 497 additions and 215 deletions

View File

@ -3,21 +3,21 @@
* Google Image Layout v0.0.1 * Google Image Layout v0.0.1
* Description, by Anh Trinh. * Description, by Anh Trinh.
* Heavily modified for searx * Heavily modified for searx
* http://trinhtrunganh.com * https://ptgamr.github.io/2014-09-12-google-image-layout/
* https://ptgamr.github.io/google-image-layout/src/google-image-layout.js
* *
* @license Free to use under the MIT License. * @license Free to use under the MIT License.
* *
*/ */
(function(w, d) {
'use strict'; (function (w, d) {
function ImageLayout(container_selector, results_selector, img_selector, margin, maxHeight) {
function ImageLayout(container_selector, results_selector, img_selector, maxHeight) {
this.container_selector = container_selector; this.container_selector = container_selector;
this.results_selector = results_selector; this.results_selector = results_selector;
this.img_selector = img_selector; this.img_selector = img_selector;
this.margin = 10; this.margin = margin;
this.maxHeight = maxHeight; this.maxHeight = maxHeight;
this._alignAllDone = true; this.isAlignDone = true;
} }
/** /**
@ -31,12 +31,11 @@
* *
* @return {[type]} the height * @return {[type]} the height
*/ */
ImageLayout.prototype._getHeigth = function(images, width) { ImageLayout.prototype._getHeigth = function (images, width) {
var r = 0, var i, img;
img; var r = 0;
width -= images.length * this.margin; for (i = 0; i < images.length; i++) {
for (var i = 0; i < images.length; i++) {
img = images[i]; img = images[i];
if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) { if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
r += img.naturalWidth / img.naturalHeight; r += img.naturalWidth / img.naturalHeight;
@ -46,12 +45,14 @@
} }
} }
return width / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3 return (width - images.length * this.margin) / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
}; };
ImageLayout.prototype._setSize = function(images, height) { ImageLayout.prototype._setSize = function (images, height) {
var img, imgWidth, imagesLength = images.length; var i, img, imgWidth;
for (var i = 0; i < imagesLength; i++) { var imagesLength = images.length, resultNode;
for (i = 0; i < imagesLength; i++) {
img = images[i]; img = images[i];
if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) { if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
imgWidth = height * img.naturalWidth / img.naturalHeight; imgWidth = height * img.naturalWidth / img.naturalHeight;
@ -65,38 +66,52 @@
img.style.marginTop = '3px'; img.style.marginTop = '3px';
img.style.marginRight = this.margin - 7 + 'px'; // -4 is the negative margin of the inline element img.style.marginRight = this.margin - 7 + 'px'; // -4 is the negative margin of the inline element
img.style.marginBottom = this.margin - 7 + 'px'; img.style.marginBottom = this.margin - 7 + 'px';
resultNode = img.parentNode.parentNode;
if (!resultNode.classList.contains('js')) {
resultNode.classList.add('js');
}
} }
}; };
ImageLayout.prototype._alignImgs = function(imgGroup) { ImageLayout.prototype._alignImgs = function (imgGroup) {
var slice, h, var isSearching, slice, i, h;
containerWidth = d.querySelector(this.container_selector).clientWidth; var containerElement = d.querySelector(this.container_selector);
var containerCompStyles = window.getComputedStyle(containerElement);
var containerPaddingLeft = parseInt(containerCompStyles.getPropertyValue('padding-left'), 10);
var containerPaddingRight = parseInt(containerCompStyles.getPropertyValue('padding-right'), 10);
var containerWidth = containerElement.clientWidth - containerPaddingLeft - containerPaddingRight;
w: while (imgGroup.length > 0) { while (imgGroup.length > 0) {
for (var i = 1; i <= imgGroup.length; i++) { isSearching = true;
for (i = 1; i <= imgGroup.length && isSearching; i++) {
slice = imgGroup.slice(0, i); slice = imgGroup.slice(0, i);
h = this._getHeigth(slice, containerWidth); h = this._getHeigth(slice, containerWidth);
if (h < this.maxHeight) { if (h < this.maxHeight) {
this._setSize(slice, h); this._setSize(slice, h);
// continue with the remaining images
imgGroup = imgGroup.slice(i); imgGroup = imgGroup.slice(i);
continue w; isSearching = false;
} }
} }
this._setSize(slice, Math.min(this.maxHeight, h)); if (isSearching) {
break; this._setSize(slice, Math.min(this.maxHeight, h));
break;
}
} }
}; };
ImageLayout.prototype.align = function(results_selector) { ImageLayout.prototype.align = function () {
var results_selectorNode = d.querySelectorAll(this.results_selector), var i;
results_length = results_selectorNode.length, var results_selectorNode = d.querySelectorAll(this.results_selector);
previous = null, var results_length = results_selectorNode.length;
current = null, var previous = null;
imgGroup = []; var current = null;
for (var i = 0; i < results_length; i++) { var imgGroup = [];
for (i = 0; i < results_length; i++) {
current = results_selectorNode[i]; current = results_selectorNode[i];
if (current.previousElementSibling !== previous && imgGroup.length > 0) { if (current.previousElementSibling !== previous && imgGroup.length > 0) {
// the current image is not conected to previous one // the current image is not connected to previous one
// so the current image is the start of a new group of images. // so the current image is the start of a new group of images.
// so call _alignImgs to align the current group // so call _alignImgs to align the current group
this._alignImgs(imgGroup); this._alignImgs(imgGroup);
@ -114,32 +129,29 @@
} }
}; };
ImageLayout.prototype.watch = function() { ImageLayout.prototype.watch = function () {
var i, img, imgGroup, imgNodeLength, var i, img;
obj = this, var obj = this;
results_nodes = d.querySelectorAll(this.results_selector), var results_nodes = d.querySelectorAll(this.results_selector);
results_length = results_nodes.length; var results_length = results_nodes.length;
function align(e) { function throttleAlign() {
obj.align(); if (obj.isAlignDone) {
} obj.isAlignDone = false;
setTimeout(function () {
function throttleAlign(e) {
if (obj._alignAllDone) {
obj._alignAllDone = false;
setTimeout(function() {
obj.align(); obj.align();
obj._alignAllDone = true; obj.isAlignDone = true;
}, 100); }, 100);
} }
} }
w.addEventListener('pageshow', throttleAlign);
w.addEventListener('load', throttleAlign);
w.addEventListener('resize', throttleAlign); w.addEventListener('resize', throttleAlign);
w.addEventListener('pageshow', align);
for (i = 0; i < results_length; i++) { for (i = 0; i < results_length; i++) {
img = results_nodes[i].querySelector(this.img_selector); img = results_nodes[i].querySelector(this.img_selector);
if (typeof img !== 'undefined') { if (img !== null && img !== undefined) {
img.addEventListener('load', throttleAlign); img.addEventListener('load', throttleAlign);
img.addEventListener('error', throttleAlign); img.addEventListener('error', throttleAlign);
} }
@ -148,4 +160,4 @@
w.searx.ImageLayout = ImageLayout; w.searx.ImageLayout = ImageLayout;
})(window, document); }(window, document));

View File

@ -202,14 +202,39 @@ input[type=checkbox]:not(:checked) + .label_hide_if_checked + .label_hide_if_not
} }
.result-images { .result-images {
float: left !important; float: left !important;
width: 24%; margin: 0;
margin: 0.5%; padding: 0;
} }
.result-images a { .result-images a {
display: block; display: block;
width: 100%; width: 100%;
background-size: cover; background-size: cover;
} }
.result-images a .img-thumbnail {
border: none !important;
padding: 0;
}
.result-images a:hover,
.result-images a:focus {
outline: 0;
}
.result-images a:hover .img-thumbnail,
.result-images a:focus .img-thumbnail {
box-shadow: 5px 5px 15px 0px black;
}
.result-images.js a .img-thumbnail {
max-height: inherit;
min-height: inherit;
}
.result-images:not(.js) {
width: 25%;
padding: 3px 13px 13px 3px;
}
.result-images:not(.js) a .img-thumbnail {
margin: 0;
max-height: 128px;
min-height: 128px;
}
.img-thumbnail { .img-thumbnail {
margin: 5px; margin: 5px;
max-height: 128px; max-height: 128px;

View File

@ -175,14 +175,39 @@ input[type=checkbox]:not(:checked) + .label_hide_if_checked + .label_hide_if_not
} }
.result-images { .result-images {
float: left !important; float: left !important;
width: 24%; margin: 0;
margin: 0.5%; padding: 0;
} }
.result-images a { .result-images a {
display: block; display: block;
width: 100%; width: 100%;
background-size: cover; background-size: cover;
} }
.result-images a .img-thumbnail {
border: none !important;
padding: 0;
}
.result-images a:hover,
.result-images a:focus {
outline: 0;
}
.result-images a:hover .img-thumbnail,
.result-images a:focus .img-thumbnail {
box-shadow: 5px 5px 15px 0px black;
}
.result-images.js a .img-thumbnail {
max-height: inherit;
min-height: inherit;
}
.result-images:not(.js) {
width: 25%;
padding: 3px 13px 13px 3px;
}
.result-images:not(.js) a .img-thumbnail {
margin: 0;
max-height: 128px;
min-height: 128px;
}
.img-thumbnail { .img-thumbnail {
margin: 5px; margin: 5px;
max-height: 128px; max-height: 128px;

Binary file not shown.

View File

@ -60,7 +60,7 @@ module.exports = function(grunt) {
separator: ';' separator: ';'
}, },
dist: { dist: {
src: ['src/js/*.js'], src: ['src/js/*.js', '../__common__/js/image_layout.js'],
dest: 'js/searx.js' dest: 'js/searx.js'
} }
}, },
@ -76,7 +76,7 @@ module.exports = function(grunt) {
} }
}, },
jshint: { jshint: {
files: ['gruntfile.js', 'js/searx_src/*.js'], files: ['gruntfile.js', 'js/searx_src/*.js', '../__common__/js/image_layout.js'],
options: { options: {
reporterOutput: "", reporterOutput: "",
// options here to override JSHint defaults // options here to override JSHint defaults

View File

@ -17,6 +17,9 @@
window.searx = (function(d) { window.searx = (function(d) {
'use strict'; 'use strict';
//
d.getElementsByTagName("html")[0].className = "js";
// add data- properties // add data- properties
var script = d.currentScript || (function() { var script = d.currentScript || (function() {
var scripts = d.getElementsByTagName('script'); var scripts = d.getElementsByTagName('script');
@ -199,6 +202,12 @@ $(document).ready(function(){
tabs.children().attr("aria-selected", "false"); tabs.children().attr("aria-selected", "false");
$(a.target).parent().attr("aria-selected", "true"); $(a.target).parent().attr("aria-selected", "true");
}); });
/**
* Layout images according to their sizes
*/
searx.image_thumbnail_layout = new searx.ImageLayout('#main_results', '#main_results .result-images', 'img.img-thumbnail', 15, 200);
searx.image_thumbnail_layout.watch();
}); });
;window.addEventListener('load', function() { ;window.addEventListener('load', function() {
// Hide infobox toggle if shrunk size already fits all content. // Hide infobox toggle if shrunk size already fits all content.
@ -383,3 +392,166 @@ $(document).ready(function(){
}); });
}); });
;/**
*
* Google Image Layout v0.0.1
* Description, by Anh Trinh.
* Heavily modified for searx
* https://ptgamr.github.io/2014-09-12-google-image-layout/
* https://ptgamr.github.io/google-image-layout/src/google-image-layout.js
*
* @license Free to use under the MIT License.
*
*/
(function (w, d) {
function ImageLayout(container_selector, results_selector, img_selector, margin, maxHeight) {
this.container_selector = container_selector;
this.results_selector = results_selector;
this.img_selector = img_selector;
this.margin = margin;
this.maxHeight = maxHeight;
this.isAlignDone = true;
}
/**
* Get the height that make all images fit the container
*
* width = w1 + w2 + w3 + ... = r1*h + r2*h + r3*h + ...
*
* @param {[type]} images the images to be calculated
* @param {[type]} width the container witdth
* @param {[type]} margin the margin between each image
*
* @return {[type]} the height
*/
ImageLayout.prototype._getHeigth = function (images, width) {
var i, img;
var r = 0;
for (i = 0; i < images.length; i++) {
img = images[i];
if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
r += img.naturalWidth / img.naturalHeight;
} else {
// assume that not loaded images are square
r += 1;
}
}
return (width - images.length * this.margin) / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
};
ImageLayout.prototype._setSize = function (images, height) {
var i, img, imgWidth;
var imagesLength = images.length, resultNode;
for (i = 0; i < imagesLength; i++) {
img = images[i];
if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
imgWidth = height * img.naturalWidth / img.naturalHeight;
} else {
// not loaded image : make it square as _getHeigth said it
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'; // -4 is the negative margin of the inline element
img.style.marginBottom = this.margin - 7 + 'px';
resultNode = img.parentNode.parentNode;
if (!resultNode.classList.contains('js')) {
resultNode.classList.add('js');
}
}
};
ImageLayout.prototype._alignImgs = function (imgGroup) {
var isSearching, slice, i, h;
var containerElement = d.querySelector(this.container_selector);
var containerCompStyles = window.getComputedStyle(containerElement);
var containerPaddingLeft = parseInt(containerCompStyles.getPropertyValue('padding-left'), 10);
var containerPaddingRight = parseInt(containerCompStyles.getPropertyValue('padding-right'), 10);
var containerWidth = containerElement.clientWidth - containerPaddingLeft - containerPaddingRight;
while (imgGroup.length > 0) {
isSearching = true;
for (i = 1; i <= imgGroup.length && isSearching; i++) {
slice = imgGroup.slice(0, i);
h = this._getHeigth(slice, containerWidth);
if (h < this.maxHeight) {
this._setSize(slice, h);
// continue with the remaining images
imgGroup = imgGroup.slice(i);
isSearching = false;
}
}
if (isSearching) {
this._setSize(slice, Math.min(this.maxHeight, h));
break;
}
}
};
ImageLayout.prototype.align = function () {
var i;
var results_selectorNode = d.querySelectorAll(this.results_selector);
var results_length = results_selectorNode.length;
var previous = null;
var current = null;
var imgGroup = [];
for (i = 0; i < results_length; i++) {
current = results_selectorNode[i];
if (current.previousElementSibling !== previous && imgGroup.length > 0) {
// the current image is not connected to previous one
// so the current image is the start of a new group of images.
// so call _alignImgs to align the current group
this._alignImgs(imgGroup);
// and start a new empty group of images
imgGroup = [];
}
// add the current image to the group (only the img tag)
imgGroup.push(current.querySelector(this.img_selector));
// update the previous variable
previous = current;
}
// align the remaining images
if (imgGroup.length > 0) {
this._alignImgs(imgGroup);
}
};
ImageLayout.prototype.watch = function () {
var i, img;
var obj = this;
var results_nodes = d.querySelectorAll(this.results_selector);
var results_length = results_nodes.length;
function throttleAlign() {
if (obj.isAlignDone) {
obj.isAlignDone = false;
setTimeout(function () {
obj.align();
obj.isAlignDone = true;
}, 100);
}
}
w.addEventListener('pageshow', throttleAlign);
w.addEventListener('load', throttleAlign);
w.addEventListener('resize', throttleAlign);
for (i = 0; i < results_length; i++) {
img = results_nodes[i].querySelector(this.img_selector);
if (img !== null && img !== undefined) {
img.addEventListener('load', throttleAlign);
img.addEventListener('error', throttleAlign);
}
}
};
w.searx.ImageLayout = ImageLayout;
}(window, document));

Binary file not shown.

View File

@ -17,6 +17,9 @@
window.searx = (function(d) { window.searx = (function(d) {
'use strict'; 'use strict';
//
d.getElementsByTagName("html")[0].className = "js";
// add data- properties // add data- properties
var script = d.currentScript || (function() { var script = d.currentScript || (function() {
var scripts = d.getElementsByTagName('script'); var scripts = d.getElementsByTagName('script');

View File

@ -108,4 +108,10 @@ $(document).ready(function(){
tabs.children().attr("aria-selected", "false"); tabs.children().attr("aria-selected", "false");
$(a.target).parent().attr("aria-selected", "true"); $(a.target).parent().attr("aria-selected", "true");
}); });
/**
* Layout images according to their sizes
*/
searx.image_thumbnail_layout = new searx.ImageLayout('#main_results', '#main_results .result-images', 'img.img-thumbnail', 15, 200);
searx.image_thumbnail_layout.watch();
}); });

View File

@ -77,12 +77,39 @@
// image formating of results // image formating of results
.result-images { .result-images {
float: left !important; float: left !important;
width: 24%; margin: 0;
margin: .5%; padding: 0;
a { a {
display: block; display: block;
width: 100%; width: 100%;
background-size: cover; background-size: cover;
.img-thumbnail {
border: none !important;
padding: 0;
}
&:hover, &:focus {
outline: 0;
.img-thumbnail {
box-shadow: 5px 5px 15px 0px black;
}
}
}
}
.result-images.js a .img-thumbnail {
max-height: inherit;
min-height: inherit;
}
.result-images:not(.js) {
width: 25%;
padding: 3px 13px 13px 3px;
a {
.img-thumbnail {
margin: 0;
max-height: 128px;
min-height: 128px;
}
} }
} }

View File

@ -1,4 +1,4 @@
/*! searx | 16-03-2021 | */ /*! searx | 23-03-2021 | */
/* /*
* searx, A privacy-respecting, hackable metasearch engine * searx, A privacy-respecting, hackable metasearch engine
* *

Binary file not shown.

View File

@ -1,4 +1,4 @@
/*! searx | 16-03-2021 | */ /*! searx | 23-03-2021 | */
/* /*
* searx, A privacy-respecting, hackable metasearch engine * searx, A privacy-respecting, hackable metasearch engine
* *

Binary file not shown.

View File

@ -11,7 +11,7 @@ module.exports = function(grunt) {
} }
}, },
jshint: { jshint: {
files: ['js/searx_src/*.js', 'js/searx_header/*.js'], files: ['js/searx_src/*.js', 'js/searx_header/*.js', '../__common__/js/*.js'],
options: { options: {
reporterOutput: "", reporterOutput: "",
proto: true, proto: true,
@ -30,7 +30,7 @@ module.exports = function(grunt) {
}, },
files: { files: {
'js/searx.head.js': ['js/searx_head/*.js'], 'js/searx.head.js': ['js/searx_head/*.js'],
'js/searx.js': ['js/searx_src/*.js'] 'js/searx.js': ['js/searx_src/*.js', '../__common__/js/*.js']
} }
} }
}, },

Binary file not shown.

View File

@ -698,157 +698,6 @@ module.exports = AutoComplete;
},{}]},{},[1])(1) },{}]},{},[1])(1)
}); });
;/**
*
* Google Image Layout v0.0.1
* Description, by Anh Trinh.
* Heavily modified for searx
* http://trinhtrunganh.com
*
* @license Free to use under the MIT License.
*
*/
(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;
}
/**
* Get the height that make all images fit the container
*
* width = w1 + w2 + w3 + ... = r1*h + r2*h + r3*h + ...
*
* @param {[type]} images the images to be calculated
* @param {[type]} width the container witdth
* @param {[type]} margin the margin between each image
*
* @return {[type]} the height
*/
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 {
// assume that not loaded images are square
r += 1;
}
}
return width / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
};
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 {
// not loaded image : make it square as _getHeigth said it
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'; // -4 is the negative margin of the inline element
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) {
// the current image is not conected to previous one
// so the current image is the start of a new group of images.
// so call _alignImgs to align the current group
this._alignImgs(imgGroup);
// and start a new empty group of images
imgGroup = [];
}
// add the current image to the group (only the img tag)
imgGroup.push(current.querySelector(this.img_selector));
// update the previous variable
previous = current;
}
// align the remaining images
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);
;searx.ready(function() { ;searx.ready(function() {
searx.on('.result', 'click', function() { searx.on('.result', 'click', function() {
@ -1411,7 +1260,7 @@ module.exports = AutoComplete;
'use strict'; 'use strict';
searx.ready(function() { searx.ready(function() {
searx.image_thumbnail_layout = new searx.ImageLayout('#urls', '#urls .result-images', 'img.image_thumbnail', 200); searx.image_thumbnail_layout = new searx.ImageLayout('#urls', '#urls .result-images', 'img.image_thumbnail', 10, 200);
searx.image_thumbnail_layout.watch(); searx.image_thumbnail_layout.watch();
searx.on('.btn-collapse', 'click', function(event) { searx.on('.btn-collapse', 'click', function(event) {
@ -1575,3 +1424,166 @@ module.exports = AutoComplete;
}); });
})(window, document, window.searx); })(window, document, window.searx);
;/**
*
* Google Image Layout v0.0.1
* Description, by Anh Trinh.
* Heavily modified for searx
* https://ptgamr.github.io/2014-09-12-google-image-layout/
* https://ptgamr.github.io/google-image-layout/src/google-image-layout.js
*
* @license Free to use under the MIT License.
*
*/
(function (w, d) {
function ImageLayout(container_selector, results_selector, img_selector, margin, maxHeight) {
this.container_selector = container_selector;
this.results_selector = results_selector;
this.img_selector = img_selector;
this.margin = margin;
this.maxHeight = maxHeight;
this.isAlignDone = true;
}
/**
* Get the height that make all images fit the container
*
* width = w1 + w2 + w3 + ... = r1*h + r2*h + r3*h + ...
*
* @param {[type]} images the images to be calculated
* @param {[type]} width the container witdth
* @param {[type]} margin the margin between each image
*
* @return {[type]} the height
*/
ImageLayout.prototype._getHeigth = function (images, width) {
var i, img;
var r = 0;
for (i = 0; i < images.length; i++) {
img = images[i];
if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
r += img.naturalWidth / img.naturalHeight;
} else {
// assume that not loaded images are square
r += 1;
}
}
return (width - images.length * this.margin) / r; //have to round down because Firefox will automatically roundup value with number of decimals > 3
};
ImageLayout.prototype._setSize = function (images, height) {
var i, img, imgWidth;
var imagesLength = images.length, resultNode;
for (i = 0; i < imagesLength; i++) {
img = images[i];
if ((img.naturalWidth > 0) && (img.naturalHeight > 0)) {
imgWidth = height * img.naturalWidth / img.naturalHeight;
} else {
// not loaded image : make it square as _getHeigth said it
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'; // -4 is the negative margin of the inline element
img.style.marginBottom = this.margin - 7 + 'px';
resultNode = img.parentNode.parentNode;
if (!resultNode.classList.contains('js')) {
resultNode.classList.add('js');
}
}
};
ImageLayout.prototype._alignImgs = function (imgGroup) {
var isSearching, slice, i, h;
var containerElement = d.querySelector(this.container_selector);
var containerCompStyles = window.getComputedStyle(containerElement);
var containerPaddingLeft = parseInt(containerCompStyles.getPropertyValue('padding-left'), 10);
var containerPaddingRight = parseInt(containerCompStyles.getPropertyValue('padding-right'), 10);
var containerWidth = containerElement.clientWidth - containerPaddingLeft - containerPaddingRight;
while (imgGroup.length > 0) {
isSearching = true;
for (i = 1; i <= imgGroup.length && isSearching; i++) {
slice = imgGroup.slice(0, i);
h = this._getHeigth(slice, containerWidth);
if (h < this.maxHeight) {
this._setSize(slice, h);
// continue with the remaining images
imgGroup = imgGroup.slice(i);
isSearching = false;
}
}
if (isSearching) {
this._setSize(slice, Math.min(this.maxHeight, h));
break;
}
}
};
ImageLayout.prototype.align = function () {
var i;
var results_selectorNode = d.querySelectorAll(this.results_selector);
var results_length = results_selectorNode.length;
var previous = null;
var current = null;
var imgGroup = [];
for (i = 0; i < results_length; i++) {
current = results_selectorNode[i];
if (current.previousElementSibling !== previous && imgGroup.length > 0) {
// the current image is not connected to previous one
// so the current image is the start of a new group of images.
// so call _alignImgs to align the current group
this._alignImgs(imgGroup);
// and start a new empty group of images
imgGroup = [];
}
// add the current image to the group (only the img tag)
imgGroup.push(current.querySelector(this.img_selector));
// update the previous variable
previous = current;
}
// align the remaining images
if (imgGroup.length > 0) {
this._alignImgs(imgGroup);
}
};
ImageLayout.prototype.watch = function () {
var i, img;
var obj = this;
var results_nodes = d.querySelectorAll(this.results_selector);
var results_length = results_nodes.length;
function throttleAlign() {
if (obj.isAlignDone) {
obj.isAlignDone = false;
setTimeout(function () {
obj.align();
obj.isAlignDone = true;
}, 100);
}
}
w.addEventListener('pageshow', throttleAlign);
w.addEventListener('load', throttleAlign);
w.addEventListener('resize', throttleAlign);
for (i = 0; i < results_length; i++) {
img = results_nodes[i].querySelector(this.img_selector);
if (img !== null && img !== undefined) {
img.addEventListener('load', throttleAlign);
img.addEventListener('error', throttleAlign);
}
}
};
w.searx.ImageLayout = ImageLayout;
}(window, document));

Binary file not shown.

View File

@ -18,7 +18,7 @@
'use strict'; 'use strict';
searx.ready(function() { searx.ready(function() {
searx.image_thumbnail_layout = new searx.ImageLayout('#urls', '#urls .result-images', 'img.image_thumbnail', 200); searx.image_thumbnail_layout = new searx.ImageLayout('#urls', '#urls .result-images', 'img.image_thumbnail', 10, 200);
searx.image_thumbnail_layout.watch(); searx.image_thumbnail_layout.watch();
searx.on('.btn-collapse', 'click', function(event) { searx.on('.btn-collapse', 'click', function(event) {

View File

@ -1,6 +1,6 @@
{% from 'oscar/macros.html' import icon %} {% from 'oscar/macros.html' import icon %}
<!DOCTYPE html> <!DOCTYPE html>
<html lang="{{ preferences.get_value('locale') }}" xml:lang="{{ preferences.get_value('locale') }}"{% if rtl %} dir="rtl"{% endif %}> <html lang="{{ preferences.get_value('locale') }}" xml:lang="{{ preferences.get_value('locale') }}"{% if rtl %} dir="rtl"{% endif %} class="nojs">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="description" content="searx - a privacy-respecting, hackable metasearch engine" /> <meta name="description" content="searx - a privacy-respecting, hackable metasearch engine" />