(function($){ var windowhasloaded = false; $(window).load(function(){ windowhasloaded = true; }); ///////// public functions ///////// /** * gallerify a gallery and render rows * * @param params can contain margin, width, mode, jssetup and imagesperrow */ $.fn.gallerify = function(params){ var _this = this; //sample parameters params = params || {}; params.margin = params.margin != undefined && params.margin != null ? params.margin : 10; params.width = params.width || undefined; //width of the whole gallery params.mode = params.mode || 'default'; //default, bootstrap, flickr, small params.jssetup = params.jssetup != undefined ? params.jssetup : true; //if you are going to set the css variables for the elements in css params.imagesperrow = params.imagesperrow || undefined; //how many images should show up at a minimum params.debounceload = params.debounceload != undefined ? params.debounceload : true; //how many images should show up at a minimum params.lastrow = params.lastrow || "adjust"; init(_this, params); this.gallerify.render = function(){ setupchilds(_this, params.margin) rendergallery(_this, params); }; var asyncimagesloadedfinished = debounce(function() { rendergallery(_this, params); }, 100); this.gallerify.renderasyncimages = function(){ setupchilds(_this, params.margin); if(params.debounceload){ _this.find("img").load(function(){ asyncimagesloadedfinished() }); }else{ _this.find("img").load(function(){ rendergallery(_this, params); }); } }; return _this; }; ///////// private functions ///////// function init(jgallery, params){ //allow params.jssetup && setupchilds(jgallery, params.margin); jgallery.addclass("xgallerify"); if(windowhasloaded){ rendergallery(jgallery, params); }else{ $(window).on("load", function(){ rendergallery(jgallery, params); }); } // if(params.continuousresizerender){ $(window).resize(function(){ rendergallery(jgallery, params); }); // }else{ // $(window).bind('resizeend', function() { // rendergallery(jgallery, params); // }); // $(window).resize(function() { // if(this.resizeto) cleartimeout(this.resizeto); // this.resizeto = settimeout(function() { // $(this).trigger('resizeend'); // }, 50); // }); // } } function setupchilds(jgallery, margin){ jchildren = $(jgallery.children()); jchildren .css("display", "inline-block") .css("margin", margin) .find("img") .css("width", "100%") .addclass("ximage-loaded"); } function rendergallery(jgallery, _params){ var jchildren = []; //jquery childs var jchildrows = []; //jquery childs var dchildren = jgallery.children(); //dom childs var width = _params.width || jgallery.width(); var screensettings = getscreensettings(width, _params.mode); imagesperrow = _params.imagesperrow || screensettings.itemsperrow; var lastrowheight; //todo might need some rework if(_params.width){ jgallery.width(width); } //todo this code looks a little too complex - seperate in multiple functions?! for (var i = 0; i < dchildren.length; i++){ var _jchild = $(dchildren[i]); if(_jchild.width()){ jchildren.push(_jchild); if(jchildren.length >= imagesperrow || i == dchildren.length -1){ jchildrows.push(jchildren); if( !( i == dchildren.length -1 //check if last row && jchildren.length < screensettings.itemsperrow // check if the miminum items per row are reched ) //checking if current row is a complete row || _params.lastrow == "fullwidth" //check if a non-complete row should be displayed with the full width ){ lastrowheight = renderrow(jchildrows[jchildrows.length - 1], width, _params.margin, screensettings.maxheight); }else{ //adjust renderlastrow(jchildrows[jchildrows.length - 1], width, _params.margin, lastrowheight); } if(lastrowheight < screensettings.maxheight){ //if the row height is smaller than the maxheight property beginn a new row. otherwise add another image to decrese the height jchildren = []; } } } }; } function renderrow(jchildren, gallerywidth, margin, maxheight){ resizetosameheight(jchildren, maxheight); return resizetowidth(jchildren, gallerywidth, margin); //returning height of the current row } function renderlastrow(jchildren, gallerywidth, margin, rowheight){ rowheight = resizetosameheight(jchildren, rowheight); var currentwidth = 0; $(jchildren).each( function(){ currentwidth += $(this).width(); }); if(currentwidth > gallerywidth){ rowheight = resizetowidth(jchildren, gallerywidth, margin); } return rowheight; } function resizetosameheight(jchildren, childheight){ for (var i = 0; i < jchildren.length; i++){ var factor = childheight / jchildren[i].height(); var x = jchildren[i].width(); jchildren[i].width(jchildren[i].width() * factor); }; return jchildren[0].height(); //returning height of the current row } function resizetowidth(jchildren, rowwidth, margin){ var currentwidth = 0; $(jchildren).each( function(){ currentwidth += $(this).width(); }); //adding 4px to the margin to let the gallery float smooth var factor = (rowwidth - (jchildren.length * (margin + 4) * 2)) / currentwidth; for (var i = 0; i < jchildren.length; i++){ jchildren[i].css('width', jchildren[i].width() * factor); }; return jchildren[0].height(); } function getscreensettings(gallerywidth, mode){ var ret = { itemsperrow : undefined, maxheight : undefined }; //default max height for mobile if(gallerywidth <= 768){ ret.maxheight = screen.height; } if(mode == "bootstrap"){ // ------- bootstrap mode ------- if(gallerywidth > 1200){ ret.itemsperrow = 4; }else if(gallerywidth > 992){ ret.itemsperrow = 3; }else if(gallerywidth > 768){ ret.itemsperrow = 2; }else { ret.itemsperrow = 0.4; } //max height if(gallerywidth > 768){ ret.maxheight = screen.height * 0.5; } }else if(mode == "flickr"){ // ------- flickr mode ------- if(gallerywidth > 1800){ ret.itemsperrow = 4; }else if(gallerywidth > 1300){ ret.itemsperrow = 3; }else if(gallerywidth > 610){ ret.itemsperrow = 2; }else { ret.itemsperrow = 1; } //max height if(gallerywidth > 768){ ret.maxheight = screen.height * 0.4; } }else if(mode == "small"){ // ------- small mode ------- if(gallerywidth > 1800){ ret.itemsperrow = 14; }else if(gallerywidth > 1300){ ret.itemsperrow = 10; }else if(gallerywidth > 610){ ret.itemsperrow = 6; }else { ret.itemsperrow = 4; } //max height if(gallerywidth > 768){ ret.maxheight = screen.height * 0.4; } }else{ // ------- default mode ------- if(gallerywidth > 1800){ ret.itemsperrow = 4; }else if(gallerywidth > 1200){ ret.itemsperrow = 3; }else if(gallerywidth > 768){ ret.itemsperrow = 2; }else { ret.itemsperrow = 1; } //max height if(gallerywidth > 768){ ret.maxheight = screen.height * 0.5; } } return ret; } function debounce(func, wait, immediate) { var timeout; return function() { var context = this, args = arguments; var later = function() { timeout = null; if (!immediate) func.apply(context, args); }; var callnow = immediate && !timeout; cleartimeout(timeout); timeout = settimeout(later, wait); if (callnow) func.apply(context, args); }; }; }( jquery ));