forked from yconst/Freetile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.freetile.min.js
92 lines (91 loc) · 8.03 KB
/
jquery.freetile.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
(function($){"use strict";$.fn.freetile=function(method)
{if(typeof Freetile[method]==='function')
{return Freetile[method].apply(this,Array.prototype.slice.call(arguments,1));}
else if(typeof method==='object'||!method)
{return Freetile.init.apply(this,arguments);}
else
{$.error('Method '+method+' does not exist on jQuery.Freetile');}
return this;};var Freetile={init:function(options)
{var container=this,o=Freetile.setupOptions(container,options),c=Freetile.newContent(o.contentToAppend);if(!o.tiled)Freetile.setupContainerBindings(container,o);if(o.tiled&&c)
{container.append(c);c.filter(o.selector||'*').imagesLoaded(function()
{Freetile.positionAll(container,o);});}
else
{container.imagesLoaded(function()
{Freetile.positionAll(container,o);});}
return container;},append:function(options)
{var container=this,o=Freetile.setupOptions(container,options),c=Freetile.newContent(o.contentToAppend);if(o.tiled&&c)
{container.append(c);c.imagesLoaded(function()
{Freetile.positionAll(container,o);});}
return container;},layout:function(options)
{var container=this,o=Freetile.setupOptions(container,options);Freetile.positionAll(container,o);return container;},setupOptions:function(container,options)
{var containerData=container.data('FreetileData');var newOptions=$.extend(true,{},this.defaults,containerData,this.reset,options);container.data('FreetileData',newOptions);newOptions.tiled=(containerData!==undefined);newOptions._animate=newOptions.animate&&newOptions.tiled&&$.isEmptyObject(newOptions.contentToAppend);this.reset.callback=newOptions.persistentCallback&&newOptions.callback?newOptions.callback:function(){};return newOptions;},setupContainerBindings:function(container,o)
{if(o.containerResize)
{var win=$(window),curWidth=win.width(),curHeight=win.height();win.resize(function()
{clearTimeout(container.data("FreetileTimeout"));container.data("FreetileTimeout",setTimeout(function()
{var newWidth=win.width(),newHeight=win.height();curWidth=newWidth,curHeight=newHeight;container.freetile('layout');},400));});}
if(o.customEvents)
{container.bind(o.customEvents,function()
{clearTimeout(container.data("FreetileTimeout"));container.data("FreetileTimeout",setTimeout(function(){container.freetile('layout');},400));});}
return container;},newContent:function(content)
{if((typeof content==='object'&&!$.isEmptyObject(content))||(typeof content==='string'&&$(content).length))
{return $(content);}
return false;},calculatePositions:function(container,elements,o)
{elements.each(function(i)
{var $this=$(this),j=0;o.ElementWidth=$this.outerWidth(true);o.ElementHeight=$this.outerHeight(true);console.log(o.ElementHeight);o.ElementTop=0;o.ElementIndex=i;o.IndexStart=0;o.IndexEnd=0;o.BestScore=0;o.TestedTop=0;o.TestedLeft=0;o.TestedTop=o.currentPos[0].top;for(j=1;j<o.currentPos.length&&o.currentPos[j].left<o.ElementWidth;j++)
{o.TestedTop=Math.max(o.TestedTop,o.currentPos[j].top);}
o.ElementTop=o.TestedTop;o.IndexEnd=j;o.BestScore=o.scoreFunction(o);for(var i=1;(i<o.currentPos.length)&&(o.currentPos[i].left+o.ElementWidth<=o.containerWidth);i++)
{o.TestedLeft=o.currentPos[i].left;o.TestedTop=o.currentPos[i].top;for(j=i+1;(j<o.currentPos.length)&&(o.currentPos[j].left-o.currentPos[i].left<o.ElementWidth);j++)
{o.TestedTop=Math.max(o.TestedTop,o.currentPos[j].top);}
var NewScore=o.scoreFunction(o);if(NewScore>o.BestScore)
{o.IndexStart=i;o.IndexEnd=j;o.ElementTop=o.TestedTop;o.BestScore=NewScore;}}
var curpos=$this.position(),pos={left:o.currentPos[o.IndexStart].left+o.xPadding,top:o.ElementTop+o.yPadding};if(curpos.top!=pos.top||curpos.left!=pos.left)
{var aniObj={el:$this,f:'css',d:0};if(o._animate&&!$this.hasClass('noanim'))
{var curoffset=$this.offset(),offset={left:pos.left+(curoffset.left-curpos.left),top:pos.top+(curoffset.top-curpos.top)};if((curoffset.top+o.ElementHeight>o.viewportY&&curoffset.top<o.viewportYH||offset.top+o.ElementHeight>o.viewportY&&offset.top<o.viewportYH))
{aniObj.f='animate'
aniObj.d=o.currentDelay;++(o.iteration);o.currentDelay+=o.elementDelay;}}
aniObj.style=pos;o.styleQueue.push(aniObj);}
--(o.iteration);var LastSpanTop=o.currentPos[o.IndexEnd-1].top,LastSpanRight=o.currentPos[o.IndexEnd]?o.currentPos[o.IndexEnd].left:o.containerWidth,ElementRight=o.currentPos[o.IndexStart].left+o.ElementWidth;o.currentPos[o.IndexStart].top=o.ElementTop+o.ElementHeight;if(ElementRight<LastSpanRight)
{o.currentPos.splice(o.IndexStart+1,o.IndexEnd-o.IndexStart-1,{left:ElementRight,top:LastSpanTop});}
else
{o.currentPos.splice(o.IndexStart+1,o.IndexEnd-o.IndexStart-1);}});},prepareElements:function(container,elements,o)
{elements.each(function()
{var $this=$(this);if($this.is(':visible'))
{$this.css({'display':'block'});}}).css({'position':'absolute'});},applyStyles:function(o)
{var obj;for(var i=0,len=o.styleQueue.length;i<len;i++)
{obj=o.styleQueue[i];if(obj.f=='animate')
{obj.el.delay(obj.d).animate(obj.style,$.extend(true,{},o.animationOptions));}
else
{obj.el.css(obj.style);}}},positionAll:function(container,o)
{if($.isEmptyObject(o.contentToAppend))
{var Elements=o.selector?container.children(o.selector):container.children();}
else
{var Elements=o.selector?o.contentToAppend.filter(o.selector):o.contentToAppend;}
o.ElementsCount=Elements.length;if(!o.ElementsCount)
{if(typeof(o.callback=="function"))
{o.callback(o);}
var f=o._animate&&o.containerAnimate?'animate':'css';container[f]({'height':'0px'});return container;}
var disp=container.css('display')||'';var vis=container.css('visibility')||'';container.css({display:'block',width:'',visibility:'hidden'});o.containerWidth=container.width();var savedPos=container.data("FreetilePos");o.currentPos=!$.isEmptyObject(o.contentToAppend)&&savedPos?savedPos:[{left:0,top:0}];o.xPadding=parseInt(container.css("padding-left"),10);o.yPadding=parseInt(container.css("padding-top"),10);o.viewportY=$(window).scrollTop();o.viewportYH=o.viewportY+$(window).height();o.iteration=Elements.length;o.currentDelay=0;o.styleQueue=[];o.animationOptions.complete=function(){if(--(o.iteration)<=0)o.callback(o);};Freetile.prepareElements(container,Elements,o);Freetile.calculatePositions(container,Elements,o);Freetile.applyStyles(o);var CalculatedCSS={};if(disp)CalculatedCSS.display=disp;if(vis)CalculatedCSS.visibility=vis;if(container.css('position')=='static')
{CalculatedCSS.position='relative';}
if(o.forceWidth&&o.containerWidthStep>0)
{CalculatedCSS.width=o.containerWidthStep*(parseInt(container.width()/o.containerWidthStep,10))}
container.css(CalculatedCSS);var Tops=$.map(o.currentPos,function(n,i){return n.top;});CalculatedCSS={height:Math.max.apply(Math,Tops)};if(o._animate&&o.containerAnimate)
{container.stop().animate(CalculatedCSS,$.extend(true,{},o.animationOptions));}
else
{container.css(CalculatedCSS);}
if(o.iteration<=0)o.callback(o);container.data("FreetilePos",o.currentPos);Elements.addClass("tiled");return container;},defaults:{selector:'*',animate:false,elementDelay:0,containerResize:true,containerAnimate:false,customEvents:'',persistentCallback:false,forceWidth:false,containerWidthStep:1,scoreFunction:function(o)
{return-(o.TestedTop);}},reset:{animationOptions:{complete:function(){}},callback:function(){},contentToAppend:{}}};$.fn.imagesLoaded=function(callback)
{var $this=this,$images=$this.find('img:not(.load-complete)').add($this.filter('img:not(.load-complete)')),len=$images.length,current=len,images_src=[],blank='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';function triggerCallback()
{callback.call($this,$images);}
function triggerStep()
{if($.fn.imagesLoaded.step)$.fn.imagesLoaded.step.call($this,current,len,$images);}
function imgLoadedI(event)
{if(--current<=0)
{current=len;$images.unbind('load error',imgLoadedI).bind('load error',imgLoadedII).each(function()
{this.src=images_src.shift();});}}
function imgLoadedII(event)
{setTimeout(triggerStep);if(--current<=0&&event.target.src!==blank)
{setTimeout(triggerCallback);$images.unbind('load error',imgLoadedII).addClass('load-complete');}}
if(!len)
{triggerCallback();}
$images.bind('load error',imgLoadedI).each(function()
{images_src.push(this.src);this.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";});return $this;};})(jQuery);