1 /*Copyright (C) 2013 Baptiste Clement
\r
3 This program is free software: you can redistribute it and/or modify
\r
4 it under the terms of the GNU General Public License as published by
\r
5 the Free Software Foundation, either version 3 of the License, or
\r
6 (at your option) any later version.
\r
8 This program is distributed in the hope that it will be useful,
\r
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
11 GNU General Public License for more details.
\r
13 You should have received a copy of the GNU General Public License
\r
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
17 function applyOptimalImagesSize(bestHeight)
\r
19 var divimages = document.getElementsByClassName("image imagepreview");
\r
20 var images = new Array();
\r
21 for(var i = 0 ; i < divimages.length ; ++i)
\r
23 images.push(divimages[i].children[0].children[0]);
\r
25 var newHList = getOptimalHeights(images, bestHeight);
\r
26 for( var i = 0 ; i < images.length ; ++i)
\r
28 images[i].height = newHList[i];
\r
32 //Renvoi un tuple (with,height) de la taille de la surface d'affichage du navigateur
\r
33 function getClientWindowSize()
\r
40 w = (document.body.clientWidth);
\r
41 h = (document.body.clientHeight);
\r
45 w = (window.innerWidth);
\r
46 h = (window.innerHeight);
\r
52 //renvoie la hauteur optimal de chaque image pour occuper toute la largeur de la surface d'affichage
\r
53 function getOptimalHeights(images, bestHeight)
\r
55 var imageHeight = bestHeight;
\r
56 var originalSizes = getAllImageSizesForHeight(images, imageHeight);
\r
57 var clientSize = getClientWindowSize();
\r
58 var newHeights = new Array();
\r
59 var allWidth = new Array();
\r
60 for(var i = 0 ; i < originalSizes.length ; ++i)
\r
62 allWidth.push(originalSizes[i][0]);
\r
65 var splittedWidth = splitTab(allWidth, clientSize[0]);
\r
66 for(var i = 0 ; i < splittedWidth.length ; ++i)
\r
70 for(var j=0 ; j< splittedWidth[i].length ; ++j)
\r
72 sum += splittedWidth[i][j] + 5;
\r
73 ratiosum += parseFloat(splittedWidth[i][j]) / parseFloat(imageHeight);
\r
75 var deltaHeight = parseInt((clientSize[0] - sum) / parseFloat(ratiosum));
\r
77 var lastandtoobig = i == (splittedWidth.length-1) && sum < clientSize[0]/2;
\r
79 for(var j=0 ; j< splittedWidth[i].length ; ++j)
\r
83 newHeights.push(bestHeight);
\r
87 newHeights.push(imageHeight + deltaHeight);
\r
94 //donne la taille de toutes les images affichées
\r
95 function getAllImageSizes(images)
\r
97 var sizes = new Array();
\r
98 for (var i = 0; i < images.length; i++) {
\r
99 sizes.push([images[i].width, images[i].height]);
\r
104 //donne la taille de toutes les images affichées, redimensionnées pour une hauteur donnée
\r
105 function getAllImageSizesForHeight(images, height)
\r
107 var sizes = new Array();
\r
108 for (var i = 0; i < images.length; i++) {
\r
109 sizes.push([images[i].width / (images[i].height / height) , height]);
\r
114 //Renvoi un tableau de tableau, correspondant au découpage du tableau d'entrée découpé par tranche de MAX
\r
115 function splitTab(tab, max)
\r
117 var splitedTabs = [new Array()];
\r
119 for (var i = 0; i < tab.length; i++)
\r
123 var total = sumSize + element + 5;
\r
124 //regles arbirtaire de découpage en plus de total > max
\r
125 //(parce que ca rend mieux visuellement)
\r
126 //=> on ajoute la suite l'élement qui dépasse si sa taille fait moins d'1/4 de max
\r
127 //=> ou si l'écart restant représente plus de la moitié de l'élément à ajouter
\r
128 if((total > max && (sumSize > max ||(max - sumSize) > (element / 2) || element > max / 4)) || element >= max)
\r
130 splitedTabs.push(new Array());
\r
131 splitedTabs[splitedTabs.length - 1].push(element);
\r
132 sumSize = 16 + element + 4;
\r
136 splitedTabs[splitedTabs.length - 1].push(element);
\r
140 return splitedTabs;
\r