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){
545 Roo.each(box, function(b){
547 b.el.setVisibilityMode(Roo.Element.DISPLAY);
550 mx = Math.max(mx, b.x);
554 maxX = maxX - this.unitWidth * mx - this.gutter * (mx - 1) - this.padWidth;
570 Roo.each(items, function(item, k){
572 item.el.setVisibilityMode(Roo.Element.DISPLAY);
580 if(size + item.y > 3){
587 var width = Math.floor(this.unitWidth * item.x + (this.gutter * (item.x - 1)) + item.el.getPadding('lr'));
589 x = Math.min(x, maxX - width - this.padWidth);
597 size = size + item.y;
608 this._processHorizontalLayoutQueue( queue, eItems, isInstant );
611 /** Sets position of item in DOM
612 * @param {Element} item
613 * @param {Number} x - horizontal position
614 * @param {Number} y - vertical position
615 * @param {Boolean} isInstant - disables transitions
617 _processVerticalLayoutQueue : function( queue, isInstant )
619 var pos = this.el.getBox(true);
624 for (var i = 0; i < this.cols; i++){
628 Roo.each(queue, function(box, k){
630 var col = k % this.cols;
632 Roo.each(box, function(b,kk){
634 b.el.position('absolute');
636 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
637 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
639 if(b.size == 'md-left' || b.size == 'md-right'){
640 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
641 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
644 b.el.setWidth(width);
645 b.el.setHeight(height);
649 for (var i = 0; i < this.cols; i++){
651 if(maxY[i] < maxY[col]){
656 col = Math.min(col, i);
660 x = pos.x + col * (this.colWidth + this.padWidth);
668 positions = this.getVerticalOneBoxColPositions(x, y, box);
671 positions = this.getVerticalTwoBoxColPositions(x, y, box);
674 positions = this.getVerticalThreeBoxColPositions(x, y, box);
677 positions = this.getVerticalFourBoxColPositions(x, y, box);
683 Roo.each(box, function(b,kk){
685 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
687 var sz = b.el.getSize();
689 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
697 for (var i = 0; i < this.cols; i++){
698 mY = Math.max(mY, maxY[i]);
701 this.el.setHeight(mY - pos.y);
705 // _processVerticalAlternativeLayoutQueue : function( items, isInstant )
707 // var pos = this.el.getBox(true);
710 // var maxX = pos.right;
712 // var maxHeight = 0;
714 // Roo.each(items, function(item, k){
718 // item.el.position('absolute');
720 // var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
722 // item.el.setWidth(width);
724 // var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
726 // item.el.setHeight(height);
729 // item.el.setXY([x, y], isInstant ? false : true);
731 // item.el.setXY([maxX - width, y], isInstant ? false : true);
734 // y = y + height + this.alternativePadWidth;
736 // maxHeight = maxHeight + height + this.alternativePadWidth;
740 // this.el.setHeight(maxHeight);
744 _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
746 var pos = this.el.getBox(true);
751 var maxX = pos.right;
753 this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
755 var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
757 Roo.each(queue, function(box, k){
759 Roo.each(box, function(b, kk){
761 b.el.position('absolute');
763 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
764 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
766 if(b.size == 'md-left' || b.size == 'md-right'){
767 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
768 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
771 b.el.setWidth(width);
772 b.el.setHeight(height);
784 positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
787 positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
790 positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
796 Roo.each(box, function(b,kk){
798 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
800 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
808 _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
810 Roo.each(eItems, function(b,k){
812 b.size = (k == 0) ? 'sm' : 'xs';
813 b.x = (k == 0) ? 2 : 1;
814 b.y = (k == 0) ? 2 : 1;
816 b.el.position('absolute');
818 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
820 b.el.setWidth(width);
822 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
824 b.el.setHeight(height);
831 x : maxX - this.unitWidth * 2 - this.gutter,
836 x : maxX - this.unitWidth,
837 y : minY + (this.unitWidth + this.gutter) * 2
841 x : maxX - this.unitWidth * 3 - this.gutter * 2,
845 Roo.each(eItems, function(b,k){
847 b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
853 getVerticalOneBoxColPositions : function(x, y, box)
857 var rand = Math.floor(Math.random() * ((4 - box[0].x)));
859 if(box[0].size == 'md-left'){
863 if(box[0].size == 'md-right'){
868 x : x + (this.unitWidth + this.gutter) * rand,
875 getVerticalTwoBoxColPositions : function(x, y, box)
879 if(box[0].size == 'xs'){
883 y : y + ((this.unitWidth + this.gutter) * Math.floor(Math.random() * box[1].y))
887 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
901 x : x + (this.unitWidth + this.gutter) * 2,
902 y : y + ((this.unitWidth + this.gutter) * Math.floor(Math.random() * box[0].y))
909 getVerticalThreeBoxColPositions : function(x, y, box)
913 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
921 x : x + (this.unitWidth + this.gutter) * 1,
926 x : x + (this.unitWidth + this.gutter) * 2,
934 if(box[0].size == 'xs' && box[1].size == 'xs'){
943 y : y + ((this.unitWidth + this.gutter) * (box[2].y - 1))
947 x : x + (this.unitWidth + this.gutter) * 1,
961 x : x + (this.unitWidth + this.gutter) * 2,
966 x : x + (this.unitWidth + this.gutter) * 2,
967 y : y + (this.unitWidth + this.gutter) * (box[0].y - 1)
974 getVerticalFourBoxColPositions : function(x, y, box)
978 if(box[0].size == 'xs'){
987 y : y + (this.unitWidth + this.gutter) * 1
992 y : y + (this.unitWidth + this.gutter) * 2
996 x : x + (this.unitWidth + this.gutter) * 1,
1010 x : x + (this.unitWidth + this.gutter) * 2,
1015 x : x + (this.unitWidth + this.gutter) * 2,
1016 y : y + (this.unitWidth + this.gutter) * 1
1020 x : x + (this.unitWidth + this.gutter) * 2,
1021 y : y + (this.unitWidth + this.gutter) * 2
1028 getHorizontalOneBoxColPositions : function(maxX, minY, box)
1032 if(box[0].size == 'md-left'){
1034 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1041 if(box[0].size == 'md-right'){
1043 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1044 y : minY + (this.unitWidth + this.gutter) * 1
1050 var rand = Math.floor(Math.random() * (4 - box[0].y));
1053 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1054 y : minY + (this.unitWidth + this.gutter) * rand
1061 getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1066 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1071 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1072 y : minY + (this.unitWidth + this.gutter) * (3 - box[1].y)
1079 getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1084 x : maxX - this.unitWidth,
1089 x : maxX - this.unitWidth,
1090 y : minY - this.unitWidth - this.gutter
1094 x : maxX - this.unitWidth,
1095 y : minY - (this.unitWidth + this.gutter) * 2