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).set({
672 // b.el.select('iframe',true).setSize(width,height);
676 for (var i = 0; i < this.cols; i++){
678 if(maxY[i] < maxY[col]){
683 col = Math.min(col, i);
687 x = pos.x + col * (this.colWidth + this.padWidth);
695 positions = this.getVerticalOneBoxColPositions(x, y, box);
698 positions = this.getVerticalTwoBoxColPositions(x, y, box);
701 positions = this.getVerticalThreeBoxColPositions(x, y, box);
704 positions = this.getVerticalFourBoxColPositions(x, y, box);
710 Roo.each(box, function(b,kk){
712 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
714 var sz = b.el.getSize();
716 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
724 for (var i = 0; i < this.cols; i++){
725 mY = Math.max(mY, maxY[i]);
728 this.el.setHeight(mY - pos.y);
732 // _processVerticalAlternativeLayoutQueue : function( items, isInstant )
734 // var pos = this.el.getBox(true);
737 // var maxX = pos.right;
739 // var maxHeight = 0;
741 // Roo.each(items, function(item, k){
745 // item.el.position('absolute');
747 // var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
749 // item.el.setWidth(width);
751 // var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
753 // item.el.setHeight(height);
756 // item.el.setXY([x, y], isInstant ? false : true);
758 // item.el.setXY([maxX - width, y], isInstant ? false : true);
761 // y = y + height + this.alternativePadWidth;
763 // maxHeight = maxHeight + height + this.alternativePadWidth;
767 // this.el.setHeight(maxHeight);
771 _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
773 var pos = this.el.getBox(true);
778 var maxX = pos.right;
780 this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
782 var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
784 Roo.each(queue, function(box, k){
786 Roo.each(box, function(b, kk){
788 b.el.position('absolute');
790 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
791 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
793 if(b.size == 'md-left' || b.size == 'md-right'){
794 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
795 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
798 b.el.setWidth(width);
799 b.el.setHeight(height);
811 positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
814 positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
817 positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
820 positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
826 Roo.each(box, function(b,kk){
828 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
830 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
838 _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
840 Roo.each(eItems, function(b,k){
842 b.size = (k == 0) ? 'sm' : 'xs';
843 b.x = (k == 0) ? 2 : 1;
844 b.y = (k == 0) ? 2 : 1;
846 b.el.position('absolute');
848 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
850 b.el.setWidth(width);
852 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
854 b.el.setHeight(height);
861 x : maxX - this.unitWidth * 2 - this.gutter,
866 x : maxX - this.unitWidth,
867 y : minY + (this.unitWidth + this.gutter) * 2
871 x : maxX - this.unitWidth * 3 - this.gutter * 2,
875 Roo.each(eItems, function(b,k){
877 b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
883 getVerticalOneBoxColPositions : function(x, y, box)
887 var rand = Math.floor(Math.random() * ((4 - box[0].x)));
889 if(box[0].size == 'md-left'){
893 if(box[0].size == 'md-right'){
898 x : x + (this.unitWidth + this.gutter) * rand,
905 getVerticalTwoBoxColPositions : function(x, y, box)
909 if(box[0].size == 'xs'){
913 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[1].y))
917 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
931 x : x + (this.unitWidth + this.gutter) * 2,
932 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[0].y))
939 getVerticalThreeBoxColPositions : function(x, y, box)
943 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
951 x : x + (this.unitWidth + this.gutter) * 1,
956 x : x + (this.unitWidth + this.gutter) * 2,
964 if(box[0].size == 'xs' && box[1].size == 'xs'){
973 y : y + ((this.unitHeight + this.gutter) * (box[2].y - 1))
977 x : x + (this.unitWidth + this.gutter) * 1,
991 x : x + (this.unitWidth + this.gutter) * 2,
996 x : x + (this.unitWidth + this.gutter) * 2,
997 y : y + (this.unitHeight + this.gutter) * (box[0].y - 1)
1004 getVerticalFourBoxColPositions : function(x, y, box)
1008 if(box[0].size == 'xs'){
1017 y : y + (this.unitHeight + this.gutter) * 1
1022 y : y + (this.unitHeight + this.gutter) * 2
1026 x : x + (this.unitWidth + this.gutter) * 1,
1040 x : x + (this.unitWidth + this.gutter) * 2,
1045 x : x + (this.unitHeightunitWidth + this.gutter) * 2,
1046 y : y + (this.unitHeight + this.gutter) * 1
1050 x : x + (this.unitWidth + this.gutter) * 2,
1051 y : y + (this.unitWidth + this.gutter) * 2
1058 getHorizontalOneBoxColPositions : function(maxX, minY, box)
1062 if(box[0].size == 'md-left'){
1064 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1071 if(box[0].size == 'md-right'){
1073 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1074 y : minY + (this.unitWidth + this.gutter) * 1
1080 var rand = Math.floor(Math.random() * (4 - box[0].y));
1083 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1084 y : minY + (this.unitWidth + this.gutter) * rand
1091 getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1095 if(box[0].size == 'xs'){
1098 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1103 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1104 y : minY + (this.unitWidth + this.gutter) * (3 - box[1].y)
1112 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1117 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1118 y : minY + (this.unitWidth + this.gutter) * 2
1125 getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1129 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
1132 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1137 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1138 y : minY + (this.unitWidth + this.gutter) * 1
1142 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1143 y : minY + (this.unitWidth + this.gutter) * 2
1150 if(box[0].size == 'xs' && box[1].size == 'xs'){
1153 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1158 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1163 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1164 y : minY + (this.unitWidth + this.gutter) * 1
1172 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1177 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1178 y : minY + (this.unitWidth + this.gutter) * 2
1182 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1183 y : minY + (this.unitWidth + this.gutter) * 2
1190 getHorizontalFourBoxColPositions : function(maxX, minY, box)
1194 if(box[0].size == 'xs'){
1197 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1202 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1207 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),
1212 x : maxX - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1213 y : minY + (this.unitWidth + this.gutter) * 1
1221 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1226 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1227 y : minY + (this.unitWidth + this.gutter) * 2
1231 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1232 y : minY + (this.unitWidth + this.gutter) * 2
1236 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),
1237 y : minY + (this.unitWidth + this.gutter) * 2
1245 * remove a Masonry Brick
1246 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to remove
1248 removeBrick : function(brick_id)
1254 for (var i = 0; i<this.bricks.length; i++) {
1255 if (this.bricks[i].id == brick_id) {
1256 this.bricks.splice(i,1);
1257 this.el.dom.removeChild(Roo.get(brick_id).dom);
1264 * adds a Masonry Brick
1265 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1267 addBrick : function(cfg)
1269 var cn = new Roo.bootstrap.MasonryBrick(cfg);
1270 //this.register(cn);
1271 cn.parentId = this.id;
1277 * register a Masonry Brick
1278 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1281 register : function(brick)
1283 this.bricks.push(brick);
1284 brick.masonryId = this.id;
1288 * clear all the Masonry Brick
1290 clearAll : function()
1293 //this.getChildContainer().dom.innerHTML = "";
1294 this.el.dom.innerHTML = '';
1297 getSelected : function()
1299 if (!this.selectedBrick) {
1303 return this.selectedBrick;
1307 Roo.apply(Roo.bootstrap.LayoutMasonry, {
1311 * register a Masonry Layout
1312 * @param {Roo.bootstrap.LayoutMasonry} the masonry layout to add
1315 register : function(layout)
1317 this.groups[layout.id] = layout;
1320 * fetch a Masonry Layout based on the masonry layout ID
1321 * @param {string} the masonry layout to add
1322 * @returns {Roo.bootstrap.LayoutMasonry} the masonry layout
1325 get: function(layout_id) {
1326 if (typeof(this.groups[layout_id]) == 'undefined') {
1329 return this.groups[layout_id] ;