forked from yconst/Freetile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.freetile.min.js
1 lines (1 loc) · 6.9 KB
/
jquery.freetile.min.js
1
(function(b){b.fn.freetile=function(c){if(typeof a[c]==="function"){return a[c].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof c==="object"||!c){return a.init.apply(this,arguments)}else{b.error("Method "+c+" does not exist on jQuery.Freetile")}}return this};var a={init:function(e){var d=this,f=a.setupOptions(d,e),g=a.newContent(f.contentToAppend);if(!f.tiled){a.setupContainerBindings(d,f)}if(f.tiled&&g){d.append(g);g.filter(f.selector||"*").imagesLoaded(function(){a.positionAll(d,f)})}else{d.imagesLoaded(function(){a.positionAll(d,f)})}return d},append:function(e){var d=this,f=a.setupOptions(d,e),g=a.newContent(f.contentToAppend);if(f.tiled&&g){d.append(g);g.imagesLoaded(function(){a.positionAll(d,f)})}return d},layout:function(d){var c=this,e=a.setupOptions(c,d);a.positionAll(c,e);return c},destroy:function(){var c=this,d=c.data("FreetileData");a.resetElementsStyle(c,d);b(window).off("resize",this.windowResizeCallback);c.off(d.customEvents,this.customEventsCallback);return true},setupOptions:function(d,e){var c=d.data("FreetileData");var f=b.extend(true,{},this.defaults,c,this.reset,e);d.data("FreetileData",f);f.tiled=(c!==undefined);f._animate=f.animate&&f.tiled&&b.isEmptyObject(f.contentToAppend);this.reset.callback=f.persistentCallback&&f.callback?f.callback:function(){};return f},windowResizeCallback:function(c,d,e){clearTimeout(c.data("FreetileTimeout"));c.data("FreetileTimeout",setTimeout(function(){var h=b(window),g=h.width(),f=h.height();if(g!=d||f!=e){d=g,e=f;c.freetile("layout")}},400))},customEventsCallback:function(c){clearTimeout(c.data("FreetileTimeout"));c.data("FreetileTimeout",setTimeout(function(){c.freetile("layout")},400))},setupContainerBindings:function(c,f){if(f.containerResize){var e=b(window),d=e.width(),g=e.height();e.resize(b.proxy(this.windowResizeCallback,c,c,d,g))}if(f.customEvents){c.bind(f.customEvents,b.proxy(this.customEventsCallback,c,c))}return c},newContent:function(c){if((typeof c==="object"&&!b.isEmptyObject(c))||(typeof c==="string"&&b(c).length)){return b(c)}return false},calculatePositions:function(c,d,e){d.each(function(o){var p=b(this),n=0;e.ElementWidth=p.outerWidth(true);e.ElementHeight=p.outerHeight(true);e.ElementTop=0;e.ElementIndex=o;e.IndexStart=0;e.IndexEnd=0;e.BestScore=0;e.TestedTop=0;e.TestedLeft=0;e.TestedTop=e.currentPos[0].top;for(n=1;n<e.currentPos.length&&e.currentPos[n].left<e.ElementWidth;n++){e.TestedTop=Math.max(e.TestedTop,e.currentPos[n].top)}e.ElementTop=e.TestedTop;e.IndexEnd=n;e.BestScore=e.scoreFunction(e);for(var o=1;(o<e.currentPos.length)&&(e.currentPos[o].left+e.ElementWidth<=e.containerWidth);o++){e.TestedLeft=e.currentPos[o].left;e.TestedTop=e.currentPos[o].top;for(n=o+1;(n<e.currentPos.length)&&(e.currentPos[n].left-e.currentPos[o].left<e.ElementWidth);n++){e.TestedTop=Math.max(e.TestedTop,e.currentPos[n].top)}var l=e.scoreFunction(e);if(l>e.BestScore){e.IndexStart=o;e.IndexEnd=n;e.ElementTop=e.TestedTop;e.BestScore=l}}var h=p.position(),q={left:e.currentPos[e.IndexStart].left+e.xPadding,top:e.ElementTop+e.yPadding};if(h.top!=q.top||h.left!=q.left){var k={el:p,f:"css",d:0};if(e._animate&&!p.hasClass("noanim")){var f=p.offset(),m={left:q.left+(f.left-h.left),top:q.top+(f.top-h.top)};if((f.top+e.ElementHeight>e.viewportY&&f.top<e.viewportYH||m.top+e.ElementHeight>e.viewportY&&m.top<e.viewportYH)){k.f="animate";k.d=e.currentDelay;++(e.iteration);e.currentDelay+=e.elementDelay}}k.style=q;e.styleQueue.push(k)}--(e.iteration);var r=e.currentPos[e.IndexEnd-1].top,s=e.currentPos[e.IndexEnd]?e.currentPos[e.IndexEnd].left:e.containerWidth,g=e.currentPos[e.IndexStart].left+e.ElementWidth;e.currentPos[e.IndexStart].top=e.ElementTop+e.ElementHeight;if(g<s){e.currentPos.splice(e.IndexStart+1,e.IndexEnd-e.IndexStart-1,{left:g,top:r})}else{e.currentPos.splice(e.IndexStart+1,e.IndexEnd-e.IndexStart-1)}})},prepareElements:function(c,d,e){d.each(function(){var f=b(this);if(f.is(":visible")){f.css({display:"block"})}}).css({position:"absolute"})},applyStyles:function(f){var e;for(var d=0,c=f.styleQueue.length;d<c;d++){e=f.styleQueue[d];if(e.f=="animate"){e.el.delay(e.d).animate(e.style,b.extend(true,{},f.animationOptions))}else{e.el.css(e.style)}}},positionAll:function(d,g){if(b.isEmptyObject(g.contentToAppend)){var c=g.selector?d.children(g.selector):d.children()}else{var c=g.selector?g.contentToAppend.filter(g.selector):g.contentToAppend}g.ElementsCount=c.length;if(!g.ElementsCount){if(typeof(g.callback=="function")){g.callback(g)}var j=g._animate&&g.containerAnimate?"animate":"css";d[j]({height:"0px"});return d}var l=d.css("display")||"";var e=d.css("visibility")||"";d.css({display:"block",width:"",visibility:"hidden"});g.containerWidth=d.width();var h=d.data("FreetilePos");g.currentPos=!b.isEmptyObject(g.contentToAppend)&&h?h:[{left:0,top:0}];g.xPadding=parseInt(d.css("padding-left"),10);g.yPadding=parseInt(d.css("padding-top"),10);g.viewportY=b(window).scrollTop();g.viewportYH=g.viewportY+b(window).height();g.iteration=c.length;g.currentDelay=0;g.styleQueue=[];g.animationOptions.complete=function(){if(--(g.iteration)<=0){g.callback(g)}};a.prepareElements(d,c,g);a.calculatePositions(d,c,g);a.applyStyles(g);var k={};if(l){k.display=l}if(e){k.visibility=e}if(d.css("position")=="static"){k.position="relative"}if(g.forceWidth&&g.containerWidthStep>0){k.width=g.containerWidthStep*(parseInt(d.width()/g.containerWidthStep,10))}d.css(k);var i=b.map(g.currentPos,function(m,f){return m.top});k={height:Math.max.apply(Math,i)};if(g._animate&&g.containerAnimate){d.stop().animate(k,b.extend(true,{},g.animationOptions))}else{d.css(k)}if(g.iteration<=0){g.callback(g)}d.data("FreetilePos",g.currentPos);c.addClass("tiled");return d},resetElementsStyle:function(c,d){var e;if(b.isEmptyObject(d.contentToAppend)){e=d.selector?c.children(d.selector):c.children()}else{e=d.selector?d.contentToAppend.filter(d.selector):d.contentToAppend}e.each(function(){var f=b(this);f.attr("style","")})},defaults:{selector:"*",animate:false,elementDelay:0,containerResize:true,containerAnimate:false,customEvents:"",persistentCallback:false,forceWidth:false,containerWidthStep:1,scoreFunction:function(c){return -(c.TestedTop)}},reset:{animationOptions:{complete:function(){}},callback:function(){},contentToAppend:{}}};b.fn.imagesLoaded=function(m){var k=this,e=k.find("img:not(.load-complete)").add(k.filter("img:not(.load-complete)")).filter(function(){return !(this.complete||this.readyState=="complete")}),h=e.length,i=h,c=[],g="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";function d(){m.call(k,e)}function l(){if(b.fn.imagesLoaded.step){b.fn.imagesLoaded.step.call(k,i,h,e)}}function j(n){if(--i<=0){i=h;e.unbind("load error",j).bind("load error",f).each(function(){this.src=c.shift()})}}function f(n){setTimeout(l);if(--i<=0&&n.target.src!==g){setTimeout(d);e.unbind("load error",f).addClass("load-complete")}}if(!h){d()}e.bind("load error",j).each(function(){c.push(this.src);this.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="});return k}})(jQuery);