12 |
- /*! Tiles.js | http://thinkpixellab.com/tilesjs | 2012-12-03 */
- var Tiles={};(function(e){var t=Tiles.Tile=function(t,n){this.id=t,this.top=0,this.left=0,this.width=0,this.height=0,this.$el=e(n||document.createElement("div"))};t.prototype.appendTo=function(e,t,n,r){this.$el.hide().appendTo(e),t?this.$el.delay(n).fadeIn(r):this.$el.show()},t.prototype.remove=function(t,n){t?this.$el.fadeOut({complete:function(){e(this).remove()}}):this.$el.remove()},t.prototype.resize=function(e,t,n,r,i){var s={},o=!1;this.left!==t.x&&(s.left=t.x,this.left=t.x,o=!0),this.top!==t.y&&(s.top=t.y,this.top=t.y,o=!0),this.width!==t.width&&(s.width=t.width,this.width=t.width,o=!0),this.height!==t.height&&(s.height=t.height,this.height=t.height,o=!0);var u=this,a=function(){var e=u.$el[0];u.left!==e.offsetLeft&&u.$el.css("left",u.left),u.top!==e.offsetTop&&u.$el.css("top",u.top),i&&i()};n&&o?this.$el.animate(s,{duration:r,easing:"swing",complete:a}):(o&&this.$el.css(s),setTimeout(a,r))}})(jQuery),function(e){function n(e,t,n,r){this.x=e,this.y=t,this.width=n,this.height=r}var t=function(e){var t=[],n=e.length,r,i,s,o,u;for(i=0;i<n;i++){s=e[i],t[i]=[];for(r=0,o=s.length;r<o;r++)u=s[r],u!==" "&&t[i].push(u)}return t};n.prototype.copy=function(){return new n(this.x,this.y,this.width,this.height)},Tiles.Rectangle=n;var r=function(e){var t=[],r=e.length,i=r===0?0:e[0].length,s,o,u,a,f,l,c;e=e.slice();for(f=0;f<r;f++)e[f]=e[f].slice();for(f=0;f<r;f++)for(a=0;a<i;a++){s=e[f][a];if(s==null)continue;u=1,o=1;if(s!==Tiles.Template.SINGLE_CELL){while(u+a<i&&s===e[f][a+u])u++;while(o+f<r&&s===e[f+o][a])o++}for(c=0;c<o;c++)for(l=0;l<u;l++)e[f+c][a+l]=null;t.push(new n(a,f,u,o))}return t};Tiles.Template=function(e,t,n){this.rects=e,this.numTiles=this.rects.length,this.numRows=n,this.numCols=t},Tiles.Template.prototype.copy=function(){var e=[],t,n;for(n=0,t=this.rects.length;n<t;n++)e.push(this.rects[n].copy());return new Tiles.Template(e,this.numCols,this.numRows)},Tiles.Template.prototype.append=function(e){if(this.numCols!==e.numCols)throw"Appended templates must have the same number of columns";var t=this.numRows,r,i,s;for(r=0,i=e.rects.length;r<i;r++)s=e.rects[r],this.rects.push(new n(s.x,t+s.y,s.width,s.height));this.numRows+=e.numRows},Tiles.Template.fromJSON=function(e){var n=t(e),i=r(n);return new Tiles.Template(i,n.length>0?n[0].length:0,n.length)},Tiles.Template.prototype.toJSON=function(){var e="ABCDEFGHIJKLMNOPQRSTUVWXYZ",t=e.length,n=0,r=[],i,s,o,u,a,f;for(a=0;a<this.numRows;a++){r[a]=[];for(u=0;u<this.numCols;u++)r[a][u]=Tiles.Template.SINGLE_CELL}for(i=0,s=this.rects.length;i<s;i++){o=this.rects[i];if(o.width>1||o.height>1){f=e[n];for(a=0;a<o.height;a++)for(u=0;u<o.width;u++)r[o.y+a][o.x+u]=f;n=(n+1)%t}}for(a=0;a<this.numRows;a++)r[a]=r[a].join("");return r},Tiles.Template.SINGLE_CELL="."}(jQuery),Tiles.UniformTemplates={get:function(e,t){var n=Math.ceil(t/e),r=[],i,s;for(s=0;s<n;s++)for(i=0;i<e;i++)r.push(new Tiles.Rectangle(i,s,1,1));return new Tiles.Template(r,e,n)}},function(e){function n(t){var n=[],r=t?t.length:0,i,s;for(i=0;i<r;i++)s=t[i],e.inArray(s,n)===-1&&n.push(s);return n}function r(e,t,n){var r=e.y+e.height,i=e.x+e.width;return!t||t.top>r||t.top+t.height<e.y||t.left>i||t.left+t.width<e.x?!n||n.y>r||n.y+n.height<e.y||n.x>i||n.x+n.width<e.x?!1:!0:!0}var t=Tiles.Grid=function(t){this.$el=e(t),this.animationDuration=500,this.cellSizeMin=150,this.templateFactory=Tiles.UniformTemplates,this.priorityPageSize=Number.MAX_VALUE,this.cellPadding=10,this.cellSize=0,this.numCols=1,this.template=null,this.isDirty=!0,this.tiles=[],this.tilesAdded=[],this.tilesRemoved=[]};t.prototype.getContentWidth=function(){return this.$el.width()},t.prototype.resizeColumns=function(){var e=this.getContentWidth();return Math.max(1,Math.floor((e+this.cellPadding)/(this.cellSizeMin+this.cellPadding)))},t.prototype.resizeCellSize=function(){var e=this.getContentWidth();return Math.ceil((e+this.cellPadding)/this.numCols)-this.cellPadding},t.prototype.resize=function(){var e=this.resizeColumns();this.numCols!==e&&e>0&&(this.numCols=e,this.isDirty=!0);var t=this.resizeCellSize();this.cellSize!==t&&t>0&&(this.cellSize=t,this.isDirty=!0)},t.prototype.updateTiles=function(t){t=n(t);var r=t.length,i=[],s,o,u,a;for(s=this.tiles.length-1;s>=0;s--)o=this.tiles[s],a=e.inArray(o.id,t),a<0?this.tilesRemoved.push(o):i[a]=o;this.tiles=[];for(s=0;s<r;s++){o=i[s];if(!o){u=t[s];if(this.createTile){o=this.createTile(u);if(!o)continue}else o=new Tiles.Tile(u);this.tilesAdded.push(o)}this.tiles.push(o)}},t.prototype.insertTiles=function(e){this.addTiles(e,!0)},t.prototype.addTiles=function(e,t){if(!e||e.length===0)return;var n=[],r=this.tiles.length,i;for(i=0;i<r;i++)n.push(this.tiles[i].id);var s=t?e.concat(n):n.concat(e);this.updateTiles(s)},t.prototype.removeTiles=function(t){if(!t||t.length===0)return;var n=[],r,i,s;for(r=0,i=this.tiles.length;r<i;r++)s=this.tiles[r].id,e.inArray(s,t)===-1&&n.push(s);this.updateTiles(n)},t.prototype.createTemplate=function(e,t){e=Math.max(1,e);var n=this.templateFactory.get(e,t);return n||(n=Tiles.UniformTemplates.get(e,t)),n},t.prototype.ensureTemplate=function(e){if(!this.template||this.template.numCols!==this.numCols)this.template=this.createTemplate(this.numCols,e),this.isDirty=!0;else{var t=e-this.template.rects.length;t>0&&(this.template.append(this.createTemplate(this.numCols,t)),this.isDirty=!0)}},t.prototype.shouldRedraw=function(){this.cellSize<=0&&this.resize(),this.ensureTemplate(this.tiles.length);var e=this.isDirty||this.tilesAdded.length>0||this.tilesRemoved.length>0;return e},t.prototype.redraw=function(t,n){if(!this.shouldRedraw()){n&&n(!1);return}var i=this.tiles.length,s=this.priorityPageSize,o=this.animationDuration,u=this.cellSize+this.cellPadding,a=0,f=0,l=new Tiles.Rectangle(this.$el.scrollLeft(),this.$el.scrollTop(),this.$el.width(),this.$el.height()),c,h,p,d,v,m,g,y,b,w,E;for(a=0;a<i;a+=s){p=this.template.rects.slice(a,a+s),d=this.tiles.slice(a,a+s),w=p.slice(0),E=d.slice(0),this.prioritizePage&&this.prioritizePage(w,E);for(v=0,m=E.length;v<m;v++)c=E[v],h=e.inArray(c,this.tilesAdded)>=0,g=w[v],y=new Tiles.Rectangle(g.x*u,g.y*u,g.width*u-this.cellPadding,g.height*u-this.cellPadding),c.resize(g,y,t&&!h&&r(l,c,y),o),h&&(b=t&&r(l,null,y),b&&this.getAppendDelay?f=this.getAppendDelay(g,p,w,c,d,E):f=0,c.appendTo(this.$el,b,f,o))}for(v=0,m=this.tilesRemoved.length;v<m;v++)c=this.tilesRemoved[v],b=t&&r(l,c,null),c.remove(b,o);this.tilesRemoved=[],this.tilesAdded=[],this.isDirty=!1,n&&setTimeout(function(){n(!0)},o+10)}}(jQuery);
|