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);
671 for (var i = 0; i < this.cols; i++){
673 if(maxY[i] < maxY[col]){
678 col = Math.min(col, i);
682 x = pos.x + col * (this.colWidth + this.padWidth);
690 positions = this.getVerticalOneBoxColPositions(x, y, box);
693 positions = this.getVerticalTwoBoxColPositions(x, y, box);
696 positions = this.getVerticalThreeBoxColPositions(x, y, box);
699 positions = this.getVerticalFourBoxColPositions(x, y, box);
705 Roo.each(box, function(b,kk){
707 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
709 var sz = b.el.getSize();
711 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
719 for (var i = 0; i < this.cols; i++){
720 mY = Math.max(mY, maxY[i]);
723 this.el.setHeight(mY - pos.y);
727 // _processVerticalAlternativeLayoutQueue : function( items, isInstant )
729 // var pos = this.el.getBox(true);
732 // var maxX = pos.right;
734 // var maxHeight = 0;
736 // Roo.each(items, function(item, k){
740 // item.el.position('absolute');
742 // var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
744 // item.el.setWidth(width);
746 // var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
748 // item.el.setHeight(height);
751 // item.el.setXY([x, y], isInstant ? false : true);
753 // item.el.setXY([maxX - width, y], isInstant ? false : true);
756 // y = y + height + this.alternativePadWidth;
758 // maxHeight = maxHeight + height + this.alternativePadWidth;
762 // this.el.setHeight(maxHeight);
766 _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
768 var pos = this.el.getBox(true);
773 var maxX = pos.right;
775 this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
777 var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
779 Roo.each(queue, function(box, k){
781 Roo.each(box, function(b, kk){
783 b.el.position('absolute');
785 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
786 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
788 if(b.size == 'md-left' || b.size == 'md-right'){
789 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
790 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
793 b.el.setWidth(width);
794 b.el.setHeight(height);
806 positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
809 positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
812 positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
815 positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
821 Roo.each(box, function(b,kk){
823 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
825 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
833 _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
835 Roo.each(eItems, function(b,k){
837 b.size = (k == 0) ? 'sm' : 'xs';
838 b.x = (k == 0) ? 2 : 1;
839 b.y = (k == 0) ? 2 : 1;
841 b.el.position('absolute');
843 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
845 b.el.setWidth(width);
847 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
849 b.el.setHeight(height);
856 x : maxX - this.unitWidth * 2 - this.gutter,
861 x : maxX - this.unitWidth,
862 y : minY + (this.unitWidth + this.gutter) * 2
866 x : maxX - this.unitWidth * 3 - this.gutter * 2,
870 Roo.each(eItems, function(b,k){
872 b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
878 getVerticalOneBoxColPositions : function(x, y, box)
882 var rand = Math.floor(Math.random() * ((4 - box[0].x)));
884 if(box[0].size == 'md-left'){
888 if(box[0].size == 'md-right'){
893 x : x + (this.unitWidth + this.gutter) * rand,
900 getVerticalTwoBoxColPositions : function(x, y, box)
904 if(box[0].size == 'xs'){
908 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[1].y))
912 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
926 x : x + (this.unitWidth + this.gutter) * 2,
927 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[0].y))
934 getVerticalThreeBoxColPositions : function(x, y, box)
938 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
946 x : x + (this.unitWidth + this.gutter) * 1,
951 x : x + (this.unitWidth + this.gutter) * 2,
959 if(box[0].size == 'xs' && box[1].size == 'xs'){
968 y : y + ((this.unitHeight + this.gutter) * (box[2].y - 1))
972 x : x + (this.unitWidth + this.gutter) * 1,
986 x : x + (this.unitWidth + this.gutter) * 2,
991 x : x + (this.unitWidth + this.gutter) * 2,
992 y : y + (this.unitHeight + this.gutter) * (box[0].y - 1)
999 getVerticalFourBoxColPositions : function(x, y, box)
1003 if(box[0].size == 'xs'){
1012 y : y + (this.unitHeight + this.gutter) * 1
1017 y : y + (this.unitHeight + this.gutter) * 2
1021 x : x + (this.unitWidth + this.gutter) * 1,
1035 x : x + (this.unitWidth + this.gutter) * 2,
1040 x : x + (this.unitHeightunitWidth + this.gutter) * 2,
1041 y : y + (this.unitHeight + this.gutter) * 1
1045 x : x + (this.unitWidth + this.gutter) * 2,
1046 y : y + (this.unitWidth + this.gutter) * 2
1053 getHorizontalOneBoxColPositions : function(maxX, minY, box)
1057 if(box[0].size == 'md-left'){
1059 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1066 if(box[0].size == 'md-right'){
1068 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1069 y : minY + (this.unitWidth + this.gutter) * 1
1075 var rand = Math.floor(Math.random() * (4 - box[0].y));
1078 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1079 y : minY + (this.unitWidth + this.gutter) * rand
1086 getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1090 if(box[0].size == 'xs'){
1093 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1098 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1099 y : minY + (this.unitWidth + this.gutter) * (3 - box[1].y)
1107 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1112 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1113 y : minY + (this.unitWidth + this.gutter) * 2
1120 getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1124 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
1127 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1132 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1133 y : minY + (this.unitWidth + this.gutter) * 1
1137 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1138 y : minY + (this.unitWidth + this.gutter) * 2
1145 if(box[0].size == 'xs' && box[1].size == 'xs'){
1148 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1153 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1158 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1159 y : minY + (this.unitWidth + this.gutter) * 1
1167 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1172 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1173 y : minY + (this.unitWidth + this.gutter) * 2
1177 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1178 y : minY + (this.unitWidth + this.gutter) * 2
1185 getHorizontalFourBoxColPositions : function(maxX, minY, box)
1189 if(box[0].size == 'xs'){
1192 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1197 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].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) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1207 x : maxX - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1208 y : minY + (this.unitWidth + this.gutter) * 1
1216 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1221 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1222 y : minY + (this.unitWidth + this.gutter) * 2
1226 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].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) - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1232 y : minY + (this.unitWidth + this.gutter) * 2
1240 * remove a Masonry Brick
1241 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to remove
1243 removeBrick : function(brick_id)
1249 for (var i = 0; i<this.bricks.length; i++) {
1250 if (this.bricks[i].id == brick_id) {
1251 this.bricks.splice(i,1);
1252 this.el.dom.removeChild(Roo.get(brick_id).dom);
1259 * adds a Masonry Brick
1260 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1262 addBrick : function(cfg)
1264 var cn = new Roo.bootstrap.MasonryBrick(cfg);
1265 //this.register(cn);
1266 cn.parentId = this.id;
1272 * register a Masonry Brick
1273 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1276 register : function(brick)
1278 this.bricks.push(brick);
1279 brick.masonryId = this.id;
1283 * clear all the Masonry Brick
1285 clearAll : function()
1288 //this.getChildContainer().dom.innerHTML = "";
1289 this.el.dom.innerHTML = '';
1292 getSelected : function()
1294 if (!this.selectedBrick) {
1298 return this.selectedBrick;
1302 Roo.apply(Roo.bootstrap.LayoutMasonry, {
1306 * register a Masonry Layout
1307 * @param {Roo.bootstrap.LayoutMasonry} the masonry layout to add
1310 register : function(layout)
1312 this.groups[layout.id] = layout;
1315 * fetch a Masonry Layout based on the masonry layout ID
1316 * @param {string} the masonry layout to add
1317 * @returns {Roo.bootstrap.LayoutMasonry} the masonry layout
1320 get: function(layout_id) {
1321 if (typeof(this.groups[layout_id]) == 'undefined') {
1324 return this.groups[layout_id] ;