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){
25 Roo.bootstrap.LayoutMasonry.superclass.constructor.call(this, config);
29 Roo.bootstrap.LayoutMasonry.register(this);
35 * Fire after layout the items
36 * @param {Roo.bootstrap.LayoutMasonry} this
37 * @param {Roo.EventObject} e
44 Roo.extend(Roo.bootstrap.LayoutMasonry, Roo.bootstrap.Component, {
47 * @cfg {Boolean} isLayoutInstant = no animation?
49 isLayoutInstant : false, // needed?
52 * @cfg {Number} boxWidth width of the columns
57 * @cfg {Number} boxHeight - 0 for square, or fix it at a certian height
62 * @cfg {Number} padWidth padding below box..
67 * @cfg {Number} gutter gutter width..
72 * @cfg {Number} maxCols maximum number of columns
78 * @cfg {Boolean} isAutoInitial defalut true
85 * @cfg {Boolean} isHorizontal defalut false
95 bricks: null, //CompositeElement
99 _isLayoutInited : false,
101 // isAlternative : false, // only use for vertical layout...
104 * @cfg {Number} alternativePadWidth padding below box..
106 alternativePadWidth : 50,
110 getAutoCreate : function(){
112 var cfg = Roo.apply({}, Roo.bootstrap.LayoutMasonry.superclass.getAutoCreate.call(this));
116 cls: 'blog-masonary-wrapper ' + this.cls,
118 cls : 'mas-boxes masonary'
125 getChildContainer: function( )
131 this.boxesEl = this.el.select('.mas-boxes').first();
137 initEvents : function()
141 if(this.isAutoInitial){
142 Roo.log('hook children rendered');
143 this.on('childrenrendered', function() {
144 Roo.log('children rendered');
152 this.selectedBrick = [];
154 this.currentSize = this.el.getBox(true);
156 Roo.EventManager.onWindowResize(this.resize, this);
158 if(!this.isAutoInitial){
166 //this.layout.defer(500,this);
172 var cs = this.el.getBox(true);
175 this.currentSize.width == cs.width &&
176 this.currentSize.x == cs.x &&
177 this.currentSize.height == cs.height &&
178 this.currentSize.y == cs.y
180 Roo.log("no change in with or X or Y");
184 this.currentSize = cs;
194 var isInstant = this.isLayoutInstant !== undefined ? this.isLayoutInstant : !this._isLayoutInited;
196 this.layoutItems( isInstant );
198 this._isLayoutInited = true;
200 this.fireEvent('layout', this);
204 _resetLayout : function()
206 if(this.isHorizontal){
207 this.horizontalMeasureColumns();
211 this.verticalMeasureColumns();
215 verticalMeasureColumns : function()
217 this.getContainerWidth();
219 // if(Roo.lib.Dom.getViewWidth() < 768 && this.isAlternative){
220 // this.colWidth = Math.floor(this.containerWidth * 0.8);
224 var boxWidth = this.boxWidth + this.padWidth;
226 if(this.containerWidth < this.boxWidth){
227 boxWidth = this.containerWidth
230 var containerWidth = this.containerWidth;
232 var cols = Math.floor(containerWidth / boxWidth);
234 this.cols = Math.max( cols, 1 );
236 this.cols = this.maxCols > 0 ? Math.min( this.cols, this.maxCols ) : this.cols;
238 var totalBoxWidth = this.cols * boxWidth - this.padWidth;
240 var avail = Math.floor((containerWidth - totalBoxWidth) / this.cols);
242 this.colWidth = boxWidth + avail - this.padWidth;
244 this.unitWidth = Math.round((this.colWidth - (this.gutter * 2)) / 3);
245 this.unitHeight = this.boxHeight > 0 ? this.boxHeight : this.unitWidth;
248 horizontalMeasureColumns : function()
250 this.getContainerWidth();
252 Roo.log('this.containerWidth : ' + this.containerWidth);
254 var boxWidth = this.boxWidth;
256 Roo.log('this.boxWidth : ' + this.boxWidth);
258 if(this.containerWidth < boxWidth){
259 boxWidth = this.containerWidth;
262 Roo.log('boxWidth : ' + boxWidth);
264 this.unitWidth = Math.floor((boxWidth - (this.gutter * 2)) / 3);
266 Roo.log('this.unitWidth : ' + this.unitWidth);
268 this.el.setHeight(boxWidth);
272 getContainerWidth : function()
274 Roo.log(this.el.getBox(true));
275 this.containerWidth = this.el.getBox(true).width; //maybe use getComputedWidth
278 layoutItems : function( isInstant )
280 Roo.log(this.bricks);
282 var items = Roo.apply([], this.bricks);
284 if(this.isHorizontal){
285 this._horizontalLayoutItems( items , isInstant );
289 // if(Roo.lib.Dom.getViewWidth() < 768 && this.isAlternative){
290 // this._verticalAlternativeLayoutItems( items , isInstant );
294 this._verticalLayoutItems( items , isInstant );
298 _verticalLayoutItems : function ( items , isInstant)
300 if ( !items || !items.length ) {
305 ['xs', 'xs', 'xs', 'tall'],
306 ['xs', 'xs', 'tall'],
318 ['tall', 'xs', 'xs', 'xs'],
319 ['tall', 'xs', 'xs'],
331 Roo.each(items, function(item, k){
334 // these layouts take up a full box,
368 var filterPattern = function(box, length)
376 var pattern = box.slice(0, length);
380 Roo.each(pattern, function(i){
384 Roo.each(standard, function(s){
386 if(String(s) != String(format)){
395 if(!match && length == 1){
400 filterPattern(box, length - 1);
406 box = box.slice(length, box.length);
408 filterPattern(box, 4);
414 Roo.each(boxes, function(box, k){
425 filterPattern(box, 4);
429 this._processVerticalLayoutQueue( queue, isInstant );
433 // _verticalAlternativeLayoutItems : function( items , isInstant )
435 // if ( !items || !items.length ) {
439 // this._processVerticalAlternativeLayoutQueue( items, isInstant );
443 _horizontalLayoutItems : function ( items , isInstant)
445 if ( !items || !items.length || items.length < 3) {
451 var eItems = items.slice(0, 3);
453 items = items.slice(3, items.length);
456 ['xs', 'xs', 'xs', 'wide'],
457 ['xs', 'xs', 'wide'],
469 ['wide', 'xs', 'xs', 'xs'],
470 ['wide', 'xs', 'xs'],
483 Roo.each(items, function(item, k){
520 var filterPattern = function(box, length)
528 var pattern = box.slice(0, length);
532 Roo.each(pattern, function(i){
536 Roo.each(standard, function(s){
538 if(String(s) != String(format)){
547 if(!match && length == 1){
552 filterPattern(box, length - 1);
558 box = box.slice(length, box.length);
560 filterPattern(box, 4);
566 Roo.each(boxes, function(box, k){
577 filterPattern(box, 4);
584 var pos = this.el.getBox(true);
588 var maxX = pos.right - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
592 Roo.each(queue, function(box){
596 Roo.each(box, function(b){
598 b.el.setVisibilityMode(Roo.Element.DISPLAY);
608 Roo.each(box, function(b){
610 b.el.setVisibilityMode(Roo.Element.DISPLAY);
613 mx = Math.max(mx, b.x);
617 maxX = maxX - this.unitWidth * mx - this.gutter * (mx - 1) - this.padWidth;
621 Roo.each(box, function(b){
623 b.el.setVisibilityMode(Roo.Element.DISPLAY);
637 this._processHorizontalLayoutQueue( prune, eItems, isInstant );
640 /** Sets position of item in DOM
641 * @param {Element} item
642 * @param {Number} x - horizontal position
643 * @param {Number} y - vertical position
644 * @param {Boolean} isInstant - disables transitions
646 _processVerticalLayoutQueue : function( queue, isInstant )
648 var pos = this.el.getBox(true);
653 for (var i = 0; i < this.cols; i++){
657 Roo.each(queue, function(box, k){
659 var col = k % this.cols;
661 Roo.each(box, function(b,kk){
663 b.el.position('absolute');
665 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
666 var height = Math.floor(this.unitHeight * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
668 if(b.size == 'md-left' || b.size == 'md-right'){
669 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
670 height = Math.floor(this.unitHeight * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
673 b.el.setWidth(width);
674 b.el.setHeight(height);
676 b.el.select('iframe',true).setSize(width,height);
680 for (var i = 0; i < this.cols; i++){
682 if(maxY[i] < maxY[col]){
687 col = Math.min(col, i);
691 x = pos.x + col * (this.colWidth + this.padWidth);
699 positions = this.getVerticalOneBoxColPositions(x, y, box);
702 positions = this.getVerticalTwoBoxColPositions(x, y, box);
705 positions = this.getVerticalThreeBoxColPositions(x, y, box);
708 positions = this.getVerticalFourBoxColPositions(x, y, box);
714 Roo.each(box, function(b,kk){
716 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
718 var sz = b.el.getSize();
720 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
728 for (var i = 0; i < this.cols; i++){
729 mY = Math.max(mY, maxY[i]);
732 this.el.setHeight(mY - pos.y);
736 // _processVerticalAlternativeLayoutQueue : function( items, isInstant )
738 // var pos = this.el.getBox(true);
741 // var maxX = pos.right;
743 // var maxHeight = 0;
745 // Roo.each(items, function(item, k){
749 // item.el.position('absolute');
751 // var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
753 // item.el.setWidth(width);
755 // var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
757 // item.el.setHeight(height);
760 // item.el.setXY([x, y], isInstant ? false : true);
762 // item.el.setXY([maxX - width, y], isInstant ? false : true);
765 // y = y + height + this.alternativePadWidth;
767 // maxHeight = maxHeight + height + this.alternativePadWidth;
771 // this.el.setHeight(maxHeight);
775 _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
777 var pos = this.el.getBox(true);
782 var maxX = pos.right;
784 this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
786 var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
788 Roo.each(queue, function(box, k){
790 Roo.each(box, function(b, kk){
792 b.el.position('absolute');
794 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
795 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
797 if(b.size == 'md-left' || b.size == 'md-right'){
798 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
799 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
802 b.el.setWidth(width);
803 b.el.setHeight(height);
815 positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
818 positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
821 positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
824 positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
830 Roo.each(box, function(b,kk){
832 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
834 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
842 _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
844 Roo.each(eItems, function(b,k){
846 b.size = (k == 0) ? 'sm' : 'xs';
847 b.x = (k == 0) ? 2 : 1;
848 b.y = (k == 0) ? 2 : 1;
850 b.el.position('absolute');
852 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
854 b.el.setWidth(width);
856 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
858 b.el.setHeight(height);
865 x : maxX - this.unitWidth * 2 - this.gutter,
870 x : maxX - this.unitWidth,
871 y : minY + (this.unitWidth + this.gutter) * 2
875 x : maxX - this.unitWidth * 3 - this.gutter * 2,
879 Roo.each(eItems, function(b,k){
881 b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
887 getVerticalOneBoxColPositions : function(x, y, box)
891 var rand = Math.floor(Math.random() * ((4 - box[0].x)));
893 if(box[0].size == 'md-left'){
897 if(box[0].size == 'md-right'){
902 x : x + (this.unitWidth + this.gutter) * rand,
909 getVerticalTwoBoxColPositions : function(x, y, box)
913 if(box[0].size == 'xs'){
917 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[1].y))
921 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
935 x : x + (this.unitWidth + this.gutter) * 2,
936 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[0].y))
943 getVerticalThreeBoxColPositions : function(x, y, box)
947 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
955 x : x + (this.unitWidth + this.gutter) * 1,
960 x : x + (this.unitWidth + this.gutter) * 2,
968 if(box[0].size == 'xs' && box[1].size == 'xs'){
977 y : y + ((this.unitHeight + this.gutter) * (box[2].y - 1))
981 x : x + (this.unitWidth + this.gutter) * 1,
995 x : x + (this.unitWidth + this.gutter) * 2,
1000 x : x + (this.unitWidth + this.gutter) * 2,
1001 y : y + (this.unitHeight + this.gutter) * (box[0].y - 1)
1008 getVerticalFourBoxColPositions : function(x, y, box)
1012 if(box[0].size == 'xs'){
1021 y : y + (this.unitHeight + this.gutter) * 1
1026 y : y + (this.unitHeight + this.gutter) * 2
1030 x : x + (this.unitWidth + this.gutter) * 1,
1044 x : x + (this.unitWidth + this.gutter) * 2,
1049 x : x + (this.unitHeightunitWidth + this.gutter) * 2,
1050 y : y + (this.unitHeight + this.gutter) * 1
1054 x : x + (this.unitWidth + this.gutter) * 2,
1055 y : y + (this.unitWidth + this.gutter) * 2
1062 getHorizontalOneBoxColPositions : function(maxX, minY, box)
1066 if(box[0].size == 'md-left'){
1068 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1075 if(box[0].size == 'md-right'){
1077 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1078 y : minY + (this.unitWidth + this.gutter) * 1
1084 var rand = Math.floor(Math.random() * (4 - box[0].y));
1087 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1088 y : minY + (this.unitWidth + this.gutter) * rand
1095 getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1099 if(box[0].size == 'xs'){
1102 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1107 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1108 y : minY + (this.unitWidth + this.gutter) * (3 - box[1].y)
1116 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1121 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1122 y : minY + (this.unitWidth + this.gutter) * 2
1129 getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1133 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
1136 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1141 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1142 y : minY + (this.unitWidth + this.gutter) * 1
1146 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1147 y : minY + (this.unitWidth + this.gutter) * 2
1154 if(box[0].size == 'xs' && box[1].size == 'xs'){
1157 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1162 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1167 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1168 y : minY + (this.unitWidth + this.gutter) * 1
1176 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1181 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1182 y : minY + (this.unitWidth + this.gutter) * 2
1186 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1187 y : minY + (this.unitWidth + this.gutter) * 2
1194 getHorizontalFourBoxColPositions : function(maxX, minY, box)
1198 if(box[0].size == 'xs'){
1201 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1206 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1211 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1216 x : maxX - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1217 y : minY + (this.unitWidth + this.gutter) * 1
1225 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1230 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1231 y : minY + (this.unitWidth + this.gutter) * 2
1235 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1236 y : minY + (this.unitWidth + this.gutter) * 2
1240 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1) - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1241 y : minY + (this.unitWidth + this.gutter) * 2
1249 * remove a Masonry Brick
1250 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to remove
1252 removeBrick : function(brick_id)
1258 for (var i = 0; i<this.bricks.length; i++) {
1259 if (this.bricks[i].id == brick_id) {
1260 this.bricks.splice(i,1);
1261 this.selectedBrick = [];
1262 this.el.dom.removeChild(Roo.get(brick_id).dom);
1269 * adds a Masonry Brick
1270 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1272 addBrick : function(cfg)
1274 var cn = new Roo.bootstrap.MasonryBrick(cfg);
1275 //this.register(cn);
1276 cn.parentId = this.id;
1277 cn.onRender(this.el, null);
1282 * register a Masonry Brick
1283 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1286 register : function(brick)
1288 this.bricks.push(brick);
1289 brick.masonryId = this.id;
1293 * clear all the Masonry Brick
1295 clearAll : function()
1298 //this.getChildContainer().dom.innerHTML = "";
1299 this.el.dom.innerHTML = '';
1302 getSelected : function()
1304 if (!this.selectedBrick) {
1308 return this.selectedBrick;
1312 Roo.apply(Roo.bootstrap.LayoutMasonry, {
1316 * register a Masonry Layout
1317 * @param {Roo.bootstrap.LayoutMasonry} the masonry layout to add
1320 register : function(layout)
1322 this.groups[layout.id] = layout;
1325 * fetch a Masonry Layout based on the masonry layout ID
1326 * @param {string} the masonry layout to add
1327 * @returns {Roo.bootstrap.LayoutMasonry} the masonry layout
1330 get: function(layout_id) {
1331 if (typeof(this.groups[layout_id]) == 'undefined') {
1334 return this.groups[layout_id] ;