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 this.containerWidth = this.el.getBox(true).width; //maybe use getComputedWidth
277 layoutItems : function( isInstant )
279 Roo.log(this.bricks);
281 var items = Roo.apply([], this.bricks);
283 if(this.isHorizontal){
284 this._horizontalLayoutItems( items , isInstant );
288 // if(Roo.lib.Dom.getViewWidth() < 768 && this.isAlternative){
289 // this._verticalAlternativeLayoutItems( items , isInstant );
293 this._verticalLayoutItems( items , isInstant );
297 _verticalLayoutItems : function ( items , isInstant)
299 if ( !items || !items.length ) {
304 ['xs', 'xs', 'xs', 'tall'],
305 ['xs', 'xs', 'tall'],
317 ['tall', 'xs', 'xs', 'xs'],
318 ['tall', 'xs', 'xs'],
330 Roo.each(items, function(item, k){
333 // these layouts take up a full box,
367 var filterPattern = function(box, length)
375 var pattern = box.slice(0, length);
379 Roo.each(pattern, function(i){
383 Roo.each(standard, function(s){
385 if(String(s) != String(format)){
394 if(!match && length == 1){
399 filterPattern(box, length - 1);
405 box = box.slice(length, box.length);
407 filterPattern(box, 4);
413 Roo.each(boxes, function(box, k){
424 filterPattern(box, 4);
428 this._processVerticalLayoutQueue( queue, isInstant );
432 // _verticalAlternativeLayoutItems : function( items , isInstant )
434 // if ( !items || !items.length ) {
438 // this._processVerticalAlternativeLayoutQueue( items, isInstant );
442 _horizontalLayoutItems : function ( items , isInstant)
444 if ( !items || !items.length || items.length < 3) {
450 var eItems = items.slice(0, 3);
452 items = items.slice(3, items.length);
455 ['xs', 'xs', 'xs', 'wide'],
456 ['xs', 'xs', 'wide'],
468 ['wide', 'xs', 'xs', 'xs'],
469 ['wide', 'xs', 'xs'],
482 Roo.each(items, function(item, k){
519 var filterPattern = function(box, length)
527 var pattern = box.slice(0, length);
531 Roo.each(pattern, function(i){
535 Roo.each(standard, function(s){
537 if(String(s) != String(format)){
546 if(!match && length == 1){
551 filterPattern(box, length - 1);
557 box = box.slice(length, box.length);
559 filterPattern(box, 4);
565 Roo.each(boxes, function(box, k){
576 filterPattern(box, 4);
583 var pos = this.el.getBox(true);
587 var maxX = pos.right - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
591 Roo.each(queue, function(box){
595 Roo.each(box, function(b){
597 b.el.setVisibilityMode(Roo.Element.DISPLAY);
607 Roo.each(box, function(b){
609 b.el.setVisibilityMode(Roo.Element.DISPLAY);
612 mx = Math.max(mx, b.x);
616 maxX = maxX - this.unitWidth * mx - this.gutter * (mx - 1) - this.padWidth;
620 Roo.each(box, function(b){
622 b.el.setVisibilityMode(Roo.Element.DISPLAY);
636 this._processHorizontalLayoutQueue( prune, eItems, isInstant );
639 /** Sets position of item in DOM
640 * @param {Element} item
641 * @param {Number} x - horizontal position
642 * @param {Number} y - vertical position
643 * @param {Boolean} isInstant - disables transitions
645 _processVerticalLayoutQueue : function( queue, isInstant )
647 var pos = this.el.getBox(true);
652 for (var i = 0; i < this.cols; i++){
656 Roo.each(queue, function(box, k){
658 var col = k % this.cols;
660 Roo.each(box, function(b,kk){
662 b.el.position('absolute');
664 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
665 var height = Math.floor(this.unitHeight * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
667 if(b.size == 'md-left' || b.size == 'md-right'){
668 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
669 height = Math.floor(this.unitHeight * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
672 b.el.setWidth(width);
673 b.el.setHeight(height);
675 b.el.select('iframe',true).setSize(width,height);
679 for (var i = 0; i < this.cols; i++){
681 if(maxY[i] < maxY[col]){
686 col = Math.min(col, i);
690 x = pos.x + col * (this.colWidth + this.padWidth);
698 positions = this.getVerticalOneBoxColPositions(x, y, box);
701 positions = this.getVerticalTwoBoxColPositions(x, y, box);
704 positions = this.getVerticalThreeBoxColPositions(x, y, box);
707 positions = this.getVerticalFourBoxColPositions(x, y, box);
713 Roo.each(box, function(b,kk){
715 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
717 var sz = b.el.getSize();
719 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
727 for (var i = 0; i < this.cols; i++){
728 mY = Math.max(mY, maxY[i]);
731 this.el.setHeight(mY - pos.y);
735 // _processVerticalAlternativeLayoutQueue : function( items, isInstant )
737 // var pos = this.el.getBox(true);
740 // var maxX = pos.right;
742 // var maxHeight = 0;
744 // Roo.each(items, function(item, k){
748 // item.el.position('absolute');
750 // var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
752 // item.el.setWidth(width);
754 // var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
756 // item.el.setHeight(height);
759 // item.el.setXY([x, y], isInstant ? false : true);
761 // item.el.setXY([maxX - width, y], isInstant ? false : true);
764 // y = y + height + this.alternativePadWidth;
766 // maxHeight = maxHeight + height + this.alternativePadWidth;
770 // this.el.setHeight(maxHeight);
774 _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
776 var pos = this.el.getBox(true);
781 var maxX = pos.right;
783 this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
785 var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
787 Roo.each(queue, function(box, k){
789 Roo.each(box, function(b, kk){
791 b.el.position('absolute');
793 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
794 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
796 if(b.size == 'md-left' || b.size == 'md-right'){
797 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
798 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
801 b.el.setWidth(width);
802 b.el.setHeight(height);
814 positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
817 positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
820 positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
823 positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
829 Roo.each(box, function(b,kk){
831 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
833 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
841 _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
843 Roo.each(eItems, function(b,k){
845 b.size = (k == 0) ? 'sm' : 'xs';
846 b.x = (k == 0) ? 2 : 1;
847 b.y = (k == 0) ? 2 : 1;
849 b.el.position('absolute');
851 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
853 b.el.setWidth(width);
855 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
857 b.el.setHeight(height);
864 x : maxX - this.unitWidth * 2 - this.gutter,
869 x : maxX - this.unitWidth,
870 y : minY + (this.unitWidth + this.gutter) * 2
874 x : maxX - this.unitWidth * 3 - this.gutter * 2,
878 Roo.each(eItems, function(b,k){
880 b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
886 getVerticalOneBoxColPositions : function(x, y, box)
890 var rand = Math.floor(Math.random() * ((4 - box[0].x)));
892 if(box[0].size == 'md-left'){
896 if(box[0].size == 'md-right'){
901 x : x + (this.unitWidth + this.gutter) * rand,
908 getVerticalTwoBoxColPositions : function(x, y, box)
912 if(box[0].size == 'xs'){
916 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[1].y))
920 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
934 x : x + (this.unitWidth + this.gutter) * 2,
935 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[0].y))
942 getVerticalThreeBoxColPositions : function(x, y, box)
946 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
954 x : x + (this.unitWidth + this.gutter) * 1,
959 x : x + (this.unitWidth + this.gutter) * 2,
967 if(box[0].size == 'xs' && box[1].size == 'xs'){
976 y : y + ((this.unitHeight + this.gutter) * (box[2].y - 1))
980 x : x + (this.unitWidth + this.gutter) * 1,
994 x : x + (this.unitWidth + this.gutter) * 2,
999 x : x + (this.unitWidth + this.gutter) * 2,
1000 y : y + (this.unitHeight + this.gutter) * (box[0].y - 1)
1007 getVerticalFourBoxColPositions : function(x, y, box)
1011 if(box[0].size == 'xs'){
1020 y : y + (this.unitHeight + this.gutter) * 1
1025 y : y + (this.unitHeight + this.gutter) * 2
1029 x : x + (this.unitWidth + this.gutter) * 1,
1043 x : x + (this.unitWidth + this.gutter) * 2,
1048 x : x + (this.unitHeightunitWidth + this.gutter) * 2,
1049 y : y + (this.unitHeight + this.gutter) * 1
1053 x : x + (this.unitWidth + this.gutter) * 2,
1054 y : y + (this.unitWidth + this.gutter) * 2
1061 getHorizontalOneBoxColPositions : function(maxX, minY, box)
1065 if(box[0].size == 'md-left'){
1067 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1074 if(box[0].size == 'md-right'){
1076 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1077 y : minY + (this.unitWidth + this.gutter) * 1
1083 var rand = Math.floor(Math.random() * (4 - box[0].y));
1086 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1087 y : minY + (this.unitWidth + this.gutter) * rand
1094 getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1098 if(box[0].size == 'xs'){
1101 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1106 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1107 y : minY + (this.unitWidth + this.gutter) * (3 - box[1].y)
1115 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1120 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1121 y : minY + (this.unitWidth + this.gutter) * 2
1128 getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1132 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
1135 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1140 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1141 y : minY + (this.unitWidth + this.gutter) * 1
1145 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1146 y : minY + (this.unitWidth + this.gutter) * 2
1153 if(box[0].size == 'xs' && box[1].size == 'xs'){
1156 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1161 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1166 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1167 y : minY + (this.unitWidth + this.gutter) * 1
1175 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1180 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1181 y : minY + (this.unitWidth + this.gutter) * 2
1185 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1186 y : minY + (this.unitWidth + this.gutter) * 2
1193 getHorizontalFourBoxColPositions : function(maxX, minY, box)
1197 if(box[0].size == 'xs'){
1200 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1205 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1210 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),
1215 x : maxX - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1216 y : minY + (this.unitWidth + this.gutter) * 1
1224 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1229 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1230 y : minY + (this.unitWidth + this.gutter) * 2
1234 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1235 y : minY + (this.unitWidth + this.gutter) * 2
1239 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),
1240 y : minY + (this.unitWidth + this.gutter) * 2
1248 * remove a Masonry Brick
1249 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to remove
1251 removeBrick : function(brick_id)
1257 for (var i = 0; i<this.bricks.length; i++) {
1258 if (this.bricks[i].id == brick_id) {
1259 this.bricks.splice(i,1);
1260 this.selectedBrick = [];
1261 this.el.dom.removeChild(Roo.get(brick_id).dom);
1268 * adds a Masonry Brick
1269 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1271 addBrick : function(cfg)
1273 var cn = new Roo.bootstrap.MasonryBrick(cfg);
1274 //this.register(cn);
1275 cn.parentId = this.id;
1276 cn.onRender(this.el, null);
1281 * register a Masonry Brick
1282 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1285 register : function(brick)
1287 this.bricks.push(brick);
1288 brick.masonryId = this.id;
1292 * clear all the Masonry Brick
1294 clearAll : function()
1297 //this.getChildContainer().dom.innerHTML = "";
1298 this.el.dom.innerHTML = '';
1301 getSelected : function()
1303 if (!this.selectedBrick) {
1307 return this.selectedBrick;
1311 Roo.apply(Roo.bootstrap.LayoutMasonry, {
1315 * register a Masonry Layout
1316 * @param {Roo.bootstrap.LayoutMasonry} the masonry layout to add
1319 register : function(layout)
1321 this.groups[layout.id] = layout;
1324 * fetch a Masonry Layout based on the masonry layout ID
1325 * @param {string} the masonry layout to add
1326 * @returns {Roo.bootstrap.LayoutMasonry} the masonry layout
1329 get: function(layout_id) {
1330 if (typeof(this.groups[layout_id]) == 'undefined') {
1333 return this.groups[layout_id] ;