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 var boxWidth = this.boxWidth;
254 if(this.containerWidth < boxWidth){
255 boxWidth = this.containerWidth;
258 this.unitWidth = Math.floor((boxWidth - (this.gutter * 2)) / 3);
260 this.el.setHeight(boxWidth);
264 getContainerWidth : function()
266 this.containerWidth = this.el.getBox(true).width; //maybe use getComputedWidth
269 layoutItems : function( isInstant )
271 Roo.log(this.bricks);
273 var items = Roo.apply([], this.bricks);
275 if(this.isHorizontal){
276 this._horizontalLayoutItems( items , isInstant );
280 // if(Roo.lib.Dom.getViewWidth() < 768 && this.isAlternative){
281 // this._verticalAlternativeLayoutItems( items , isInstant );
285 this._verticalLayoutItems( items , isInstant );
289 _verticalLayoutItems : function ( items , isInstant)
291 if ( !items || !items.length ) {
296 ['xs', 'xs', 'xs', 'tall'],
297 ['xs', 'xs', 'tall'],
309 ['tall', 'xs', 'xs', 'xs'],
310 ['tall', 'xs', 'xs'],
322 Roo.each(items, function(item, k){
325 // these layouts take up a full box,
359 var filterPattern = function(box, length)
367 var pattern = box.slice(0, length);
371 Roo.each(pattern, function(i){
375 Roo.each(standard, function(s){
377 if(String(s) != String(format)){
386 if(!match && length == 1){
391 filterPattern(box, length - 1);
397 box = box.slice(length, box.length);
399 filterPattern(box, 4);
405 Roo.each(boxes, function(box, k){
416 filterPattern(box, 4);
420 this._processVerticalLayoutQueue( queue, isInstant );
424 // _verticalAlternativeLayoutItems : function( items , isInstant )
426 // if ( !items || !items.length ) {
430 // this._processVerticalAlternativeLayoutQueue( items, isInstant );
434 _horizontalLayoutItems : function ( items , isInstant)
436 if ( !items || !items.length || items.length < 3) {
442 var eItems = items.slice(0, 3);
444 items = items.slice(3, items.length);
447 ['xs', 'xs', 'xs', 'wide'],
448 ['xs', 'xs', 'wide'],
460 ['wide', 'xs', 'xs', 'xs'],
461 ['wide', 'xs', 'xs'],
474 Roo.each(items, function(item, k){
511 var filterPattern = function(box, length)
519 var pattern = box.slice(0, length);
523 Roo.each(pattern, function(i){
527 Roo.each(standard, function(s){
529 if(String(s) != String(format)){
538 if(!match && length == 1){
543 filterPattern(box, length - 1);
549 box = box.slice(length, box.length);
551 filterPattern(box, 4);
557 Roo.each(boxes, function(box, k){
568 filterPattern(box, 4);
575 var pos = this.el.getBox(true);
579 var maxX = pos.right - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
583 Roo.each(queue, function(box){
587 Roo.each(box, function(b){
589 b.el.setVisibilityMode(Roo.Element.DISPLAY);
599 Roo.each(box, function(b){
601 b.el.setVisibilityMode(Roo.Element.DISPLAY);
604 mx = Math.max(mx, b.x);
608 maxX = maxX - this.unitWidth * mx - this.gutter * (mx - 1) - this.padWidth;
612 Roo.each(box, function(b){
614 b.el.setVisibilityMode(Roo.Element.DISPLAY);
628 this._processHorizontalLayoutQueue( prune, eItems, isInstant );
631 /** Sets position of item in DOM
632 * @param {Element} item
633 * @param {Number} x - horizontal position
634 * @param {Number} y - vertical position
635 * @param {Boolean} isInstant - disables transitions
637 _processVerticalLayoutQueue : function( queue, isInstant )
639 var pos = this.el.getBox(true);
644 for (var i = 0; i < this.cols; i++){
648 Roo.each(queue, function(box, k){
650 var col = k % this.cols;
652 Roo.each(box, function(b,kk){
654 b.el.position('absolute');
656 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
657 var height = Math.floor(this.unitHeight * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
659 if(b.size == 'md-left' || b.size == 'md-right'){
660 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
661 height = Math.floor(this.unitHeight * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
664 b.el.setWidth(width);
665 b.el.setHeight(height);
667 b.el.select('iframe',true).setSize(width,height);
670 console.log(b.el.getStyles());
671 alert(JSON.stringify(b.el.getStyles()));
677 for (var i = 0; i < this.cols; i++){
679 if(maxY[i] < maxY[col]){
684 col = Math.min(col, i);
688 x = pos.x + col * (this.colWidth + this.padWidth);
696 positions = this.getVerticalOneBoxColPositions(x, y, box);
699 positions = this.getVerticalTwoBoxColPositions(x, y, box);
702 positions = this.getVerticalThreeBoxColPositions(x, y, box);
705 positions = this.getVerticalFourBoxColPositions(x, y, box);
711 Roo.each(box, function(b,kk){
713 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
715 var sz = b.el.getSize();
717 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
725 for (var i = 0; i < this.cols; i++){
726 mY = Math.max(mY, maxY[i]);
729 this.el.setHeight(mY - pos.y);
733 // _processVerticalAlternativeLayoutQueue : function( items, isInstant )
735 // var pos = this.el.getBox(true);
738 // var maxX = pos.right;
740 // var maxHeight = 0;
742 // Roo.each(items, function(item, k){
746 // item.el.position('absolute');
748 // var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
750 // item.el.setWidth(width);
752 // var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
754 // item.el.setHeight(height);
757 // item.el.setXY([x, y], isInstant ? false : true);
759 // item.el.setXY([maxX - width, y], isInstant ? false : true);
762 // y = y + height + this.alternativePadWidth;
764 // maxHeight = maxHeight + height + this.alternativePadWidth;
768 // this.el.setHeight(maxHeight);
772 _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
774 var pos = this.el.getBox(true);
779 var maxX = pos.right;
781 this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
783 var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
785 Roo.each(queue, function(box, k){
787 Roo.each(box, function(b, kk){
789 b.el.position('absolute');
791 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
792 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
794 if(b.size == 'md-left' || b.size == 'md-right'){
795 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
796 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
799 b.el.setWidth(width);
800 b.el.setHeight(height);
812 positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
815 positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
818 positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
821 positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
827 Roo.each(box, function(b,kk){
829 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
831 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
839 _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
841 Roo.each(eItems, function(b,k){
843 b.size = (k == 0) ? 'sm' : 'xs';
844 b.x = (k == 0) ? 2 : 1;
845 b.y = (k == 0) ? 2 : 1;
847 b.el.position('absolute');
849 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
851 b.el.setWidth(width);
853 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
855 b.el.setHeight(height);
862 x : maxX - this.unitWidth * 2 - this.gutter,
867 x : maxX - this.unitWidth,
868 y : minY + (this.unitWidth + this.gutter) * 2
872 x : maxX - this.unitWidth * 3 - this.gutter * 2,
876 Roo.each(eItems, function(b,k){
878 b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
884 getVerticalOneBoxColPositions : function(x, y, box)
888 var rand = Math.floor(Math.random() * ((4 - box[0].x)));
890 if(box[0].size == 'md-left'){
894 if(box[0].size == 'md-right'){
899 x : x + (this.unitWidth + this.gutter) * rand,
906 getVerticalTwoBoxColPositions : function(x, y, box)
910 if(box[0].size == 'xs'){
914 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[1].y))
918 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
932 x : x + (this.unitWidth + this.gutter) * 2,
933 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[0].y))
940 getVerticalThreeBoxColPositions : function(x, y, box)
944 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
952 x : x + (this.unitWidth + this.gutter) * 1,
957 x : x + (this.unitWidth + this.gutter) * 2,
965 if(box[0].size == 'xs' && box[1].size == 'xs'){
974 y : y + ((this.unitHeight + this.gutter) * (box[2].y - 1))
978 x : x + (this.unitWidth + this.gutter) * 1,
992 x : x + (this.unitWidth + this.gutter) * 2,
997 x : x + (this.unitWidth + this.gutter) * 2,
998 y : y + (this.unitHeight + this.gutter) * (box[0].y - 1)
1005 getVerticalFourBoxColPositions : function(x, y, box)
1009 if(box[0].size == 'xs'){
1018 y : y + (this.unitHeight + this.gutter) * 1
1023 y : y + (this.unitHeight + this.gutter) * 2
1027 x : x + (this.unitWidth + this.gutter) * 1,
1041 x : x + (this.unitWidth + this.gutter) * 2,
1046 x : x + (this.unitHeightunitWidth + this.gutter) * 2,
1047 y : y + (this.unitHeight + this.gutter) * 1
1051 x : x + (this.unitWidth + this.gutter) * 2,
1052 y : y + (this.unitWidth + this.gutter) * 2
1059 getHorizontalOneBoxColPositions : function(maxX, minY, box)
1063 if(box[0].size == 'md-left'){
1065 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1072 if(box[0].size == 'md-right'){
1074 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1075 y : minY + (this.unitWidth + this.gutter) * 1
1081 var rand = Math.floor(Math.random() * (4 - box[0].y));
1084 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1085 y : minY + (this.unitWidth + this.gutter) * rand
1092 getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1096 if(box[0].size == 'xs'){
1099 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1104 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1105 y : minY + (this.unitWidth + this.gutter) * (3 - box[1].y)
1113 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1118 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1119 y : minY + (this.unitWidth + this.gutter) * 2
1126 getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1130 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
1133 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1138 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1139 y : minY + (this.unitWidth + this.gutter) * 1
1143 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1144 y : minY + (this.unitWidth + this.gutter) * 2
1151 if(box[0].size == 'xs' && box[1].size == 'xs'){
1154 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1159 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1164 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1165 y : minY + (this.unitWidth + this.gutter) * 1
1173 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1178 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1179 y : minY + (this.unitWidth + this.gutter) * 2
1183 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1184 y : minY + (this.unitWidth + this.gutter) * 2
1191 getHorizontalFourBoxColPositions : function(maxX, minY, box)
1195 if(box[0].size == 'xs'){
1198 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1203 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1208 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),
1213 x : maxX - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1214 y : minY + (this.unitWidth + this.gutter) * 1
1222 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1227 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1228 y : minY + (this.unitWidth + this.gutter) * 2
1232 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1233 y : minY + (this.unitWidth + this.gutter) * 2
1237 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),
1238 y : minY + (this.unitWidth + this.gutter) * 2
1246 * remove a Masonry Brick
1247 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to remove
1249 removeBrick : function(brick_id)
1255 for (var i = 0; i<this.bricks.length; i++) {
1256 if (this.bricks[i].id == brick_id) {
1257 this.bricks.splice(i,1);
1258 this.el.dom.removeChild(Roo.get(brick_id).dom);
1265 * adds a Masonry Brick
1266 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1268 addBrick : function(cfg)
1270 var cn = new Roo.bootstrap.MasonryBrick(cfg);
1271 //this.register(cn);
1272 cn.parentId = this.id;
1278 * register a Masonry Brick
1279 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1282 register : function(brick)
1284 this.bricks.push(brick);
1285 brick.masonryId = this.id;
1289 * clear all the Masonry Brick
1291 clearAll : function()
1294 //this.getChildContainer().dom.innerHTML = "";
1295 this.el.dom.innerHTML = '';
1298 getSelected : function()
1300 if (!this.selectedBrick) {
1304 return this.selectedBrick;
1308 Roo.apply(Roo.bootstrap.LayoutMasonry, {
1312 * register a Masonry Layout
1313 * @param {Roo.bootstrap.LayoutMasonry} the masonry layout to add
1316 register : function(layout)
1318 this.groups[layout.id] = layout;
1321 * fetch a Masonry Layout based on the masonry layout ID
1322 * @param {string} the masonry layout to add
1323 * @returns {Roo.bootstrap.LayoutMasonry} the masonry layout
1326 get: function(layout_id) {
1327 if (typeof(this.groups[layout_id]) == 'undefined') {
1330 return this.groups[layout_id] ;