forked from kombai/freewall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freewall.min.js
1 lines (1 loc) · 13.8 KB
/
freewall.min.js
1
!function(a){function f(c){function l(b){j.gutterX,j.gutterY;var f=j.cellH,g=j.cellW,k=a(b),l=k.find(k.attr("data-handle"));d.setDraggable(b,{handle:l[0],onStart:function(a){i.animate&&d.transition&&d.setTransition(this,""),k.css("z-index",9999).addClass("fw-float"),i.onBlockDrag.call(b,a)},onDrag:function(a){var d=k.position(),e=Math.round(d.top/f),l=Math.round(d.left/g),m=Math.round(k.width()/g),n=Math.round(k.height()/f);e=Math.min(Math.max(0,e),j.limitRow-n),l=Math.min(Math.max(0,l),j.limitCol-m),h.setHoles({top:e,left:l,width:m,height:n}),h.refresh(),i.onBlockMove.call(b,a)},onDrop:function(c){var d=k.position(),e=Math.round(d.top/f),l=Math.round(d.left/g),m=Math.round(k.width()/g),n=Math.round(k.height()/f);e=Math.min(Math.max(0,e),j.limitRow-n),l=Math.min(Math.max(0,l),j.limitCol-m),k.removeClass("fw-float"),k.css({zIndex:"auto",top:e*f,left:l*g});var o,p,q,r;for(p=0;n>p;++p)for(o=0;m>o;++o)q=p+e+"-"+(o+l),r=j.matrix[q],r&&1!=r&&a("#"+r).removeAttr("data-position");j.holes={},k.attr({"data-width":k.width(),"data-height":k.height(),"data-position":e+"-"+l}),h.refresh(),i.onBlockDrop.call(b,c)}})}var f=a(c);"static"==f.css("position")&&f.css("position","relative");var g=Number.MAX_VALUE,h=this;d.totalGrid+=1;var i=a.extend({},d.defaultConfig),j={arguments:null,blocks:{},events:{},matrix:{},holes:{},cellW:0,cellH:0,cellS:1,filter:"",lastId:0,length:0,maxWoB:0,maxHoB:0,minWoB:g,minHoB:g,running:0,gutterX:15,gutterY:15,totalCol:0,totalRow:0,limitCol:666666,limitRow:666666,sortFunc:null,keepOrder:!1};i.runtime=j,j.totalGrid=d.totalGrid;var k=document.body.style;d.transition||(null!=k.webkitTransition||null!=k.MozTransition||null!=k.msTransition||null!=k.OTransition||null!=k.transition)&&(d.transition=!0),a.extend(h,{addCustomEvent:function(a,b){var c=j.events;return a=a.toLowerCase(),!c[a]&&(c[a]=[]),b.eid=c[a].length,c[a].push(b),this},appendBlock:function(b){var c=a(b).appendTo(f),g=null,h=[];j.arguments&&(a.isFunction(j.sortFunc)&&c.sort(j.sortFunc),c.each(function(a,b){b.index=++a,g=d.loadBlock(b,i),g&&h.push(g)}),e[i.engine](h,i),d.setWallSize(j,f),j.length=c.length,c.each(function(a,b){d.showBlock(b,i),(i.draggable||b.getAttribute("data-draggable"))&&l(b)}))},appendHoles:function(a){var d,b=[].concat(a),c={};for(d=0;d<b.length;++d)c=b[d],j.holes[c.top+"-"+c.left+"-"+c.width+"-"+c.height]=c;return this},container:f,destroy:function(){var b=f.find(i.selector).removeAttr("id");b.each(function(b,c){$item=a(c);var d=1*$item.attr("data-width")||"",e=1*$item.attr("data-height")||"";$item.width(d).height(e).css({position:"static"})})},fillHoles:function(a){if(0==arguments.length)j.holes={};else{var d,b=[].concat(a),c={};for(d=0;d<b.length;++d)c=b[d],delete j.holes[c.top+"-"+c.left+"-"+c.width+"-"+c.height]}return this},filter:function(a){return j.filter=a,j.arguments&&this.refresh(),this},fireEvent:function(a,b,c){var d=j.events;if(a=a.toLowerCase(),d[a]&&d[a].length)for(var e=0;e<d[a].length;++e)d[a][e].call(this,b,c);return this},fitHeight:function(a){var a=a?a:f.height()||b.height();this.fitZone("auto",a),j.arguments=arguments},fitWidth:function(a){var a=a?a:f.width()||b.width();this.fitZone(a,"auto"),j.arguments=arguments},fitZone:function(c,g){var k=f.find(i.selector).removeAttr("id"),m=null,n=[];g=g?g:f.height()||b.height(),c=c?c:f.width()||b.width(),j.arguments=arguments,d.resetGrid(j),d.adjustUnit(c,g,i),j.filter?(k.data("active",0),k.filter(j.filter).data("active",1)):k.data("active",1),a.isFunction(j.sortFunc)&&k.sort(j.sortFunc),k.each(function(b,c){var e=a(c);c.index=++b,m=d.loadBlock(c,i),m&&e.data("active")&&n.push(m)}),h.fireEvent("onGridReady",f,i),e[i.engine](n,i),d.setWallSize(j,f),h.fireEvent("onGridArrange",f,i),j.length=k.length,k.each(function(a,b){d.showBlock(b,i),(i.draggable||b.getAttribute("data-draggable"))&&l(b)})},fixPos:function(b){return a(b.block).attr({"data-position":b.top+"-"+b.left}),this},fixSize:function(b){return null!=b.height&&a(b.block).attr({"data-height":b.height}),null!=b.width&&a(b.block).attr({"data-width":b.width}),this},prepend:function(a){return f.prepend(a),j.arguments&&this.refresh(),this},refresh:function(){var a=arguments.length?arguments:j.arguments,b=j.arguments,c=b?b.callee:this.fitWidth;return c.apply(this,Array.prototype.slice.call(a,0)),this},reset:function(b){return a.extend(i,b),this},setHoles:function(a){var d,b=[].concat(a),c={};for(j.holes={},d=0;d<b.length;++d)c=b[d],j.holes[c.top+"-"+c.left+"-"+c.width+"-"+c.height]=c;return this},sortBy:function(a){return j.sortFunc=a,j.arguments&&this.refresh(),this},unFilter:function(){return delete j.filter,this.refresh(),this}}),f.attr("data-min-width",80*Math.floor(b.width()/80));for(var m in d.plugin)d.plugin.hasOwnProperty(m)&&d.plugin[m].call(h,i,f);b.resize(function(){j.running||(j.running=1,setTimeout(function(){j.running=0,i.onResize.call(h,f)},122),f.attr("data-min-width",80*Math.floor(b.width()/80)))})}null==a.isNumeric&&(a.isNumeric=function(a){return null!=a&&a.constructor===Number}),null==a.isFunction&&(a.isFunction=function(a){return null!=a&&a instanceof Function});var b=a(window),c=a(document),d={defaultConfig:{animate:!1,cellW:100,cellH:100,delay:0,engine:"giot",fixSize:null,gutterX:15,gutterY:15,keepOrder:!1,selector:"> div",draggable:!1,cacheSize:!0,rightToLeft:!1,bottomToTop:!1,onGapFound:function(){},onComplete:function(){},onResize:function(){},onBlockDrag:function(){},onBlockMove:function(){},onBlockDrop:function(){},onBlockReady:function(){},onBlockFinish:function(){},onBlockActive:function(){},onBlockResize:function(){}},plugin:{},totalGrid:1,transition:!1,loadBlock:function(b,c){var d=c.runtime,e=d.gutterX,f=d.gutterY,g=d.cellH,h=d.cellW,i=null,j=a(b),k=j.data("active"),l=j.attr("data-position"),m=parseInt(j.attr("data-fixSize")),n=d.lastId++ +"-"+d.totalGrid;if(j.hasClass("fw-float"))return null;j.attr({id:n,"data-delay":b.index}),c.animate&&this.transition&&this.setTransition(b,""),isNaN(m)&&(m=null),null==m&&(m=c.fixSize);var o=m?"ceil":"round";null==j.attr("data-height")&&j.attr("data-height",j.height()),null==j.attr("data-width")&&j.attr("data-width",j.width());var p=1*j.attr("data-height"),q=1*j.attr("data-width");c.cacheSize||(b.style.width="",q=j.width(),b.style.height="",p=j.height());var r=q?Math[o]((q+e)/h):0,s=p?Math[o]((p+f)/g):0;if(m||"auto"!=c.cellH||(j.width(h*r-e),b.style.height="",p=j.height(),s=p?Math.round((p+f)/g):0),m||"auto"!=c.cellW||(j.height(g*s-f),b.style.width="",q=j.width(),r=q?Math.round((q+e)/h):0),null!=m&&(r>d.limitCol||s>d.limitRow))i=null;else if(s&&s<d.minHoB&&(d.minHoB=s),r&&r<d.minWoB&&(d.minWoB=r),s>d.maxHoB&&(d.maxHoB=s),r>d.maxWoB&&(d.maxWoB=r),0==q&&(r=0),0==p&&(s=0),i={resize:!1,id:n,width:r,height:s,fixSize:m},l){l=l.split("-"),i.y=1*l[0],i.x=1*l[1],i.width=null!=m?r:Math.min(r,d.limitCol-i.x),i.height=null!=m?s:Math.min(s,d.limitRow-i.y);var t=i.y+"-"+i.x+"-"+i.width+"-"+i.height;k?(d.holes[t]={id:i.id,top:i.y,left:i.x,width:i.width,height:i.height},this.setBlock(i,c)):delete d.holes[t]}return null==j.attr("data-state")?j.attr("data-state","init"):j.attr("data-state","move"),c.onBlockReady.call(b,i,c),l&&k?null:i},setBlock:function(a,b){var c=b.runtime,d=c.gutterX,e=c.gutterY,f=a.height,g=a.width,h=c.cellH,i=c.cellW,j=a.x,k=a.y;b.rightToLeft&&(j=c.limitCol-j-g),b.bottomToTop&&(k=c.limitRow-k-f);var l={fixSize:a.fixSize,resize:a.resize,top:k*h,left:j*i,width:i*g-d,height:h*f-e};return l.top=1*l.top.toFixed(2),l.left=1*l.left.toFixed(2),l.width=1*l.width.toFixed(2),l.height=1*l.height.toFixed(2),a.id&&(c.blocks[a.id]=l),l},showBlock:function(b,c){function k(){if(i&&g.attr("data-state","start"),c.animate&&h.transition&&h.setTransition(b,j),f)f.fixSize&&(f.height=1*g.attr("data-height"),f.width=1*g.attr("data-width")),g.css({opacity:1,width:f.width,height:f.height}),g[e]({top:f.top,left:f.left}),null!=g.attr("data-nested")&&h.nestedGrid(b,c);else{var a=parseInt(b.style.height)||0,k=parseInt(b.style.width)||0,l=parseInt(b.style.left)||0,m=parseInt(b.style.top)||0;g[e]({left:l+k/2,top:m+a/2,width:0,height:0,opacity:0})}d.length-=1,c.onBlockFinish.call(b,f,c),0==d.length&&c.onComplete.call(b,f,c)}var d=c.runtime,e=c.animate&&!this.transition?"animate":"css",f=d.blocks[b.id],g=a(b),h=this,i="move"!=g.attr("data-state"),j=i?"width 0.5s, height 0.5s":"top 0.5s, left 0.5s, width 0.5s, height 0.5s, opacity 0.5s";b.delay&&clearTimeout(b.delay),g.hasClass("fw-float")||(h.setTransition(b,""),b.style.position="absolute",c.onBlockActive.call(b,f,c),f&&f.resize&&c.onBlockResize.call(b,f,c),c.delay>0?b.delay=setTimeout(k,c.delay*g.attr("data-delay")):k())},nestedGrid:function(b,c){var d,e=a(b),g=c.runtime,h=e.attr("data-gutterX")||c.gutterX,i=e.attr("data-gutterY")||c.gutterY,j=e.attr("data-method")||"fitZone",k=e.attr("data-nested")||"> div",l=e.attr("data-cellH")||c.cellH,m=e.attr("data-cellW")||c.cellW,n=g.blocks[b.id];if(n)switch(d=new f(e),d.reset({cellH:l,cellW:m,gutterX:1*h,gutterY:1*i,selector:k,cacheSize:!1}),j){case"fitHeight":d[j](n.height);break;case"fitWidth":d[j](n.width);break;case"fitZone":d[j](n.width,n.height)}},adjustBlock:function(b,c){var d=c.runtime,e=d.gutterX,f=d.gutterY,g=a("#"+b.id),h=d.cellH,i=d.cellW;"auto"==c.cellH&&(g.width(b.width*i-e),g[0].style.height="",b.height=Math.round((g.height()+f)/h))},adjustUnit:function(b,c,d){var e=d.gutterX,f=d.gutterY,g=d.runtime,h=d.cellW,i=d.cellH;if(a.isFunction(h)&&(h=h(b)),h=1*h,!a.isNumeric(h)&&(h=1),a.isFunction(i)&&(i=i(c)),i=1*i,!a.isNumeric(i)&&(i=1),a.isNumeric(b)){1>h&&(h*=b);var j=Math.max(1,Math.floor(b/h));a.isNumeric(e)||(e=(b-j*h)/Math.max(1,j-1),e=Math.max(0,e)),j=Math.floor((b+e)/h),g.cellW=(b+e)/Math.max(j,1),g.cellS=g.cellW/h,g.gutterX=e,g.limitCol=j}if(a.isNumeric(c)){1>i&&(i*=c);var k=Math.max(1,Math.floor(c/i));a.isNumeric(f)||(f=(c-k*i)/Math.max(1,k-1),f=Math.max(0,f)),k=Math.floor((c+f)/i),g.cellH=(c+f)/Math.max(k,1),g.cellS=g.cellH/i,g.gutterY=f,g.limitRow=k}a.isNumeric(b)||(1>h&&(h=g.cellH),g.cellW=1!=h?h*g.cellS:1,g.gutterX=e,g.limitCol=666666),a.isNumeric(c)||(1>i&&(i=g.cellW),g.cellH=1!=i?i*g.cellS:1,g.gutterY=f,g.limitRow=666666),g.keepOrder=d.keepOrder},resetGrid:function(a){a.blocks={},a.length=0,a.cellH=0,a.cellW=0,a.lastId=1,a.matrix={},a.totalCol=0,a.totalRow=0},setDraggable:function(b,d){var e=!1,f={startX:0,startY:0,top:0,left:0,handle:null,onDrop:function(){},onDrag:function(){},onStart:function(){}};a(b).each(function(){function l(a){return a.stopPropagation(),a=a.originalEvent,a.touches&&(e=!0,a=a.changedTouches[0]),2!=a.button&&3!=a.which&&(b.onStart.call(h,a),b.startX=a.clientX,b.startY=a.clientY,b.top=parseInt(i.css("top"))||0,b.left=parseInt(i.css("left"))||0,c.bind("mouseup touchend",n),c.bind("mousemove touchmove",m)),!1}function m(a){a=a.originalEvent,e&&(a=a.changedTouches[0]),i.css({top:b.top-(b.startY-a.clientY),left:b.left-(b.startX-a.clientX)}),b.onDrag.call(h,a)}function n(a){a=a.originalEvent,e&&(a=a.changedTouches[0]),b.onDrop.call(h,a),c.unbind("mouseup touchend",n),c.unbind("mousemove touchmove",m)}var b=a.extend({},f,d),g=b.handle||this,h=this,i=a(h),j=a(g),k=i.css("position");"absolute"!=k&&i.css("position","relative"),i.find("iframe, form, input, textarea, .ignore-drag").each(function(){a(this).on("touchstart mousedown",function(a){a.stopPropagation()})}),c.unbind("mouseup touchend",n),c.unbind("mousemove touchmove",m),j.unbind("mousedown touchstart").bind("mousedown touchstart",l)})},setTransition:function(b,c){var d=b.style,e=a(b);!this.transition&&e.stop?e.stop():null!=d.webkitTransition?d.webkitTransition=c:null!=d.MozTransition?d.MozTransition=c:null!=d.msTransition?d.msTransition=c:null!=d.OTransition?d.OTransition=c:d.transition=c},getFreeArea:function(a,b,c){for(var d=Math.min(a+c.maxHoB,c.limitRow),e=Math.min(b+c.maxWoB,c.limitCol),f=e,g=d,h=c.matrix,i=a;g>i;++i)for(var j=b;e>j;++j)h[i+"-"+j]&&j>b&&f>j&&(f=j);for(var i=a;d>i;++i)for(var j=b;f>j;++j)h[i+"-"+j]&&i>a&&g>i&&(g=i);return{top:a,left:b,width:f-b,height:g-a}},setWallSize:function(a,b){var c=a.totalRow,d=a.totalCol,e=a.gutterY,f=a.gutterX,g=a.cellH,h=a.cellW,i=Math.max(0,h*d-f),j=Math.max(0,g*c-e);b.attr({"data-total-col":d,"data-total-row":c,"data-wall-width":Math.ceil(i),"data-wall-height":Math.ceil(j)}),a.limitCol<a.limitRow&&!b.attr("data-height")&&b.height(Math.ceil(j))}},e={giot:function(a,b){function u(a,b,c,d,e){for(var f=b;b+e>f;){for(var g=c;c+d>g;)n[f+"-"+g]=a,++g>i&&(i=g);++f>j&&(j=f)}}var c=b.runtime,e=c.limitRow,f=c.limitCol,g=0,h=0,i=c.totalCol,j=c.totalRow,k={},l=c.holes,m=null,n=c.matrix,o=Math.max(f,e),p=null,q=null,r=e>f?1:0,s=null,t=Math.min(f,e);for(var v in l)l.hasOwnProperty(v)&&u(l[v].id||!0,l[v].top,l[v].left,l[v].width,l[v].height);for(var w=0;o>w&&a.length;++w){r?h=w:g=w,s=null;for(var x=0;t>x&&a.length;++x)if(m=null,r?g=x:h=x,!c.matrix[h+"-"+g]){if(p=d.getFreeArea(h,g,c),null==b.fixSize){if(s&&!r&&c.minHoB>p.height){s.height+=p.height,s.resize=!0,u(s.id,s.y,s.x,s.width,s.height),d.setBlock(s,b);continue}if(s&&r&&c.minWoB>p.width){s.width+=p.width,s.resize=!0,u(s.id,s.y,s.x,s.width,s.height),d.setBlock(s,b);continue}}if(c.keepOrder)m=a.shift(),m.resize=!0;else{for(var v=0;v<a.length;++v)if(!(a[v].height>p.height||a[v].width>p.width)){m=a.splice(v,1)[0];break}if(null==m&&null==b.fixSize)for(var v=0;v<a.length;++v)if(null==a[v].fixSize){m=a.splice(v,1)[0],m.resize=!0;break}}if(null!=m)m.resize&&(r?(m.width=p.width,"auto"==b.cellH&&d.adjustBlock(m,b),m.height=Math.min(m.height,p.height)):(m.height=p.height,m.width=Math.min(m.width,p.width))),k[m.id]={id:m.id,x:g,y:h,width:m.width,height:m.height,resize:m.resize,fixSize:m.fixSize},s=k[m.id],u(s.id,s.y,s.x,s.width,s.height),d.setBlock(s,b);else{var q={x:g,y:h,fixSize:0};if(r){q.width=p.width,q.height=0;for(var y=g-1,z=h;n[z+"-"+y];)n[z+"-"+g]=!0,q.height+=1,z+=1}else{q.height=p.height,q.width=0;for(var z=h-1,y=g;n[z+"-"+y];)n[h+"-"+y]=!0,q.width+=1,y+=1}b.onGapFound(d.setBlock(q,b),b)}}}c.matrix=n,c.totalRow=j,c.totalCol=i}};f.addConfig=function(b){a.extend(d.defaultConfig,b)},f.createEngine=function(b){a.extend(e,b)},f.createPlugin=function(b){a.extend(d.plugin,b)},f.getMethod=function(a){return d[a]},window.Freewall=window.freewall=f}(window.Zepto||window.jQuery);