4 * http://masonry.desandro.com
6 * The idea is to render all the bricks based on vertical width...
8 * The original code extends 'outlayer' - we might need to use that....
14 * @class Roo.bootstrap.LayoutMasonry
15 * @extends Roo.bootstrap.Component
16 * Bootstrap Layout Masonry class
19 * Create a new Element
20 * @param {Object} config The config object
23 Roo.bootstrap.LayoutMasonry = function(config){
24 Roo.bootstrap.LayoutMasonry.superclass.constructor.call(this, config);
30 Roo.extend(Roo.bootstrap.LayoutMasonry, Roo.bootstrap.Component, {
33 * @cfg {Boolean} isLayoutInstant = no animation?
35 isLayoutInstant : false, // needed?
38 * @cfg {Number} boxWidth width of the columns
43 * @cfg {Number} padWidth padding below box..
48 * @cfg {Number} gutter gutter width..
53 * @cfg {Boolean} isAutoInitial defalut true
60 * @cfg {Boolean} isHorizontal defalut false
70 bricks: null, //CompositeElement
74 _isLayoutInited : false,
76 // isAlternative : false, // only use for vertical layout...
79 * @cfg {Number} alternativePadWidth padding below box..
81 alternativePadWidth : 50,
83 getAutoCreate : function(){
87 cls: 'blog-masonary-wrapper ' + this.cls,
89 cls : 'mas-boxes masonary'
96 getChildContainer: function( )
102 this.boxesEl = this.el.select('.mas-boxes').first();
108 initEvents : function()
112 if(this.isAutoInitial){
113 Roo.log('hook children rendered');
114 this.on('childrenrendered', function() {
115 Roo.log('children rendered');
123 this.currentSize = this.el.getBox(true);
125 Roo.EventManager.onWindowResize(this.resize, this);
127 if(!this.isAutoInitial){
135 //this.layout.defer(500,this);
143 var cs = this.el.getBox(true);
145 if (this.currentSize.width == cs.width && this.currentSize.x == cs.x ) {
146 Roo.log("no change in with or X");
150 this.currentSize = cs;
160 var isInstant = this.isLayoutInstant !== undefined ? this.isLayoutInstant : !this._isLayoutInited;
162 this.layoutItems( isInstant );
164 this._isLayoutInited = true;
168 _resetLayout : function()
170 if(this.isHorizontal){
171 this.horizontalMeasureColumns();
175 this.verticalMeasureColumns();
179 verticalMeasureColumns : function()
181 this.getContainerWidth();
183 // if(Roo.lib.Dom.getViewWidth() < 768 && this.isAlternative){
184 // this.colWidth = Math.floor(this.containerWidth * 0.8);
188 var boxWidth = this.boxWidth + this.padWidth;
190 if(this.containerWidth < this.boxWidth){
191 boxWidth = this.containerWidth
194 var containerWidth = this.containerWidth;
196 var cols = Math.floor(containerWidth / boxWidth);
198 this.cols = Math.max( cols, 1 );
200 var totalBoxWidth = this.cols * boxWidth - this.padWidth;
202 var avail = Math.floor((containerWidth - totalBoxWidth) / this.cols);
204 this.colWidth = boxWidth + avail - this.padWidth;
206 this.unitWidth = Math.floor((this.colWidth - (this.gutter * 2)) / 3);
209 horizontalMeasureColumns : function()
211 this.getContainerWidth();
213 var boxWidth = this.boxWidth;
215 if(this.containerWidth < boxWidth){
216 boxWidth = this.containerWidth;
219 this.unitWidth = Math.floor((boxWidth - (this.gutter * 2)) / 3);
221 this.el.setHeight(boxWidth);
225 getContainerWidth : function()
227 this.containerWidth = this.el.getBox(true).width; //maybe use getComputedWidth
230 layoutItems : function( isInstant )
232 var items = Roo.apply([], this.bricks);
234 if(this.isHorizontal){
235 this._horizontalLayoutItems( items , isInstant );
239 // if(Roo.lib.Dom.getViewWidth() < 768 && this.isAlternative){
240 // this._verticalAlternativeLayoutItems( items , isInstant );
244 this._verticalLayoutItems( items , isInstant );
248 _verticalLayoutItems : function ( items , isInstant)
250 if ( !items || !items.length ) {
255 ['xs', 'xs', 'xs', 'tall'],
256 ['xs', 'xs', 'tall'],
268 ['tall', 'xs', 'xs', 'xs'],
269 ['tall', 'xs', 'xs'],
280 Roo.each(items, function(item, k){
316 var filterPattern = function(box, length)
324 var pattern = box.slice(0, length);
328 Roo.each(pattern, function(i){
332 Roo.each(standard, function(s){
334 if(String(s) != String(format)){
343 if(!match && length == 1){
348 filterPattern(box, length - 1);
354 box = box.slice(length, box.length);
356 filterPattern(box, 4);
362 Roo.each(boxes, function(box, k){
373 filterPattern(box, 4);
377 this._processVerticalLayoutQueue( queue, isInstant );
381 // _verticalAlternativeLayoutItems : function( items , isInstant )
383 // if ( !items || !items.length ) {
387 // this._processVerticalAlternativeLayoutQueue( items, isInstant );
391 _horizontalLayoutItems : function ( items , isInstant)
393 if ( !items || !items.length || items.length < 3) {
399 var eItems = items.slice(0, 3);
401 items = items.slice(3, items.length);
404 ['xs', 'xs', 'xs', 'wide'],
405 ['xs', 'xs', 'wide'],
417 ['wide', 'xs', 'xs', 'xs'],
418 ['wide', 'xs', 'xs'],
429 Roo.each(items, function(item, k){
465 var filterPattern = function(box, length)
473 var pattern = box.slice(0, length);
477 Roo.each(pattern, function(i){
481 Roo.each(standard, function(s){
483 if(String(s) != String(format)){
492 if(!match && length == 1){
497 filterPattern(box, length - 1);
503 box = box.slice(length, box.length);
505 filterPattern(box, 4);
511 Roo.each(boxes, function(box, k){
522 filterPattern(box, 4);
529 var pos = this.el.getBox(true);
533 var maxX = pos.right - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
537 Roo.each(queue, function(box){
541 Roo.each(box, function(b){
543 b.el.setVisibilityMode(Roo.Element.DISPLAY);
553 Roo.each(box, function(b){
555 b.el.setVisibilityMode(Roo.Element.DISPLAY);
558 mx = Math.max(mx, b.x);
562 maxX = maxX - this.unitWidth * mx - this.gutter * (mx - 1) - this.padWidth;
566 Roo.each(box, function(b){
568 b.el.setVisibilityMode(Roo.Element.DISPLAY);
582 this._processHorizontalLayoutQueue( prune, eItems, isInstant );
585 /** Sets position of item in DOM
586 * @param {Element} item
587 * @param {Number} x - horizontal position
588 * @param {Number} y - vertical position
589 * @param {Boolean} isInstant - disables transitions
591 _processVerticalLayoutQueue : function( queue, isInstant )
593 var pos = this.el.getBox(true);
598 for (var i = 0; i < this.cols; i++){
602 Roo.each(queue, function(box, k){
604 var col = k % this.cols;
606 Roo.each(box, function(b,kk){
608 b.el.position('absolute');
610 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
611 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
613 if(b.size == 'md-left' || b.size == 'md-right'){
614 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
615 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
618 b.el.setWidth(width);
619 b.el.setHeight(height);
623 for (var i = 0; i < this.cols; i++){
625 if(maxY[i] < maxY[col]){
630 col = Math.min(col, i);
634 x = pos.x + col * (this.colWidth + this.padWidth);
642 positions = this.getVerticalOneBoxColPositions(x, y, box);
645 positions = this.getVerticalTwoBoxColPositions(x, y, box);
648 positions = this.getVerticalThreeBoxColPositions(x, y, box);
651 positions = this.getVerticalFourBoxColPositions(x, y, box);
657 Roo.each(box, function(b,kk){
659 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
661 var sz = b.el.getSize();
663 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
671 for (var i = 0; i < this.cols; i++){
672 mY = Math.max(mY, maxY[i]);
675 this.el.setHeight(mY - pos.y);
679 // _processVerticalAlternativeLayoutQueue : function( items, isInstant )
681 // var pos = this.el.getBox(true);
684 // var maxX = pos.right;
686 // var maxHeight = 0;
688 // Roo.each(items, function(item, k){
692 // item.el.position('absolute');
694 // var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
696 // item.el.setWidth(width);
698 // var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
700 // item.el.setHeight(height);
703 // item.el.setXY([x, y], isInstant ? false : true);
705 // item.el.setXY([maxX - width, y], isInstant ? false : true);
708 // y = y + height + this.alternativePadWidth;
710 // maxHeight = maxHeight + height + this.alternativePadWidth;
714 // this.el.setHeight(maxHeight);
718 _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
720 var pos = this.el.getBox(true);
725 var maxX = pos.right;
727 this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
729 var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
731 Roo.each(queue, function(box, k){
733 Roo.each(box, function(b, kk){
735 b.el.position('absolute');
737 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
738 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
740 if(b.size == 'md-left' || b.size == 'md-right'){
741 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
742 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
745 b.el.setWidth(width);
746 b.el.setHeight(height);
758 positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
761 positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
764 positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
767 positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
773 Roo.each(box, function(b,kk){
775 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
777 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
785 _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
787 Roo.each(eItems, function(b,k){
789 b.size = (k == 0) ? 'sm' : 'xs';
790 b.x = (k == 0) ? 2 : 1;
791 b.y = (k == 0) ? 2 : 1;
793 b.el.position('absolute');
795 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
797 b.el.setWidth(width);
799 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
801 b.el.setHeight(height);
808 x : maxX - this.unitWidth * 2 - this.gutter,
813 x : maxX - this.unitWidth,
814 y : minY + (this.unitWidth + this.gutter) * 2
818 x : maxX - this.unitWidth * 3 - this.gutter * 2,
822 Roo.each(eItems, function(b,k){
824 b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
830 getVerticalOneBoxColPositions : function(x, y, box)
834 var rand = Math.floor(Math.random() * ((4 - box[0].x)));
836 if(box[0].size == 'md-left'){
840 if(box[0].size == 'md-right'){
845 x : x + (this.unitWidth + this.gutter) * rand,
852 getVerticalTwoBoxColPositions : function(x, y, box)
856 if(box[0].size == 'xs'){
860 y : y + ((this.unitWidth + this.gutter) * Math.floor(Math.random() * box[1].y))
864 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
878 x : x + (this.unitWidth + this.gutter) * 2,
879 y : y + ((this.unitWidth + this.gutter) * Math.floor(Math.random() * box[0].y))
886 getVerticalThreeBoxColPositions : function(x, y, box)
890 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
898 x : x + (this.unitWidth + this.gutter) * 1,
903 x : x + (this.unitWidth + this.gutter) * 2,
911 if(box[0].size == 'xs' && box[1].size == 'xs'){
920 y : y + ((this.unitWidth + this.gutter) * (box[2].y - 1))
924 x : x + (this.unitWidth + this.gutter) * 1,
938 x : x + (this.unitWidth + this.gutter) * 2,
943 x : x + (this.unitWidth + this.gutter) * 2,
944 y : y + (this.unitWidth + this.gutter) * (box[0].y - 1)
951 getVerticalFourBoxColPositions : function(x, y, box)
955 if(box[0].size == 'xs'){
964 y : y + (this.unitWidth + this.gutter) * 1
969 y : y + (this.unitWidth + this.gutter) * 2
973 x : x + (this.unitWidth + this.gutter) * 1,
987 x : x + (this.unitWidth + this.gutter) * 2,
992 x : x + (this.unitWidth + this.gutter) * 2,
993 y : y + (this.unitWidth + this.gutter) * 1
997 x : x + (this.unitWidth + this.gutter) * 2,
998 y : y + (this.unitWidth + this.gutter) * 2
1005 getHorizontalOneBoxColPositions : function(maxX, minY, box)
1009 if(box[0].size == 'md-left'){
1011 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1018 if(box[0].size == 'md-right'){
1020 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1021 y : minY + (this.unitWidth + this.gutter) * 1
1027 var rand = Math.floor(Math.random() * (4 - box[0].y));
1030 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1031 y : minY + (this.unitWidth + this.gutter) * rand
1038 getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1042 if(box[0].size == 'xs'){
1045 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1050 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
1064 x : x + (this.unitWidth + this.gutter) * 2,
1065 y : y + ((this.unitWidth + this.gutter) * Math.floor(Math.random() * box[0].y))
1075 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1080 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1081 y : minY + (this.unitWidth + this.gutter) * (3 - box[1].y)
1088 getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1093 x : maxX - this.unitWidth,
1098 x : maxX - this.unitWidth,
1099 y : minY - this.unitWidth - this.gutter
1103 x : maxX - this.unitWidth,
1104 y : minY - (this.unitWidth + this.gutter) * 2