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 Roo.log(this.unitWidth);
246 alert(this.unitWidth)
247 this.unitHeight = this.boxHeight > 0 ? this.boxHeight : this.unitWidth;
250 horizontalMeasureColumns : function()
252 this.getContainerWidth();
254 var boxWidth = this.boxWidth;
256 if(this.containerWidth < boxWidth){
257 boxWidth = this.containerWidth;
260 this.unitWidth = Math.floor((boxWidth - (this.gutter * 2)) / 3);
262 this.el.setHeight(boxWidth);
266 getContainerWidth : function()
268 this.containerWidth = this.el.getBox(true).width; //maybe use getComputedWidth
271 layoutItems : function( isInstant )
273 Roo.log(this.bricks);
275 var items = Roo.apply([], this.bricks);
277 if(this.isHorizontal){
278 this._horizontalLayoutItems( items , isInstant );
282 // if(Roo.lib.Dom.getViewWidth() < 768 && this.isAlternative){
283 // this._verticalAlternativeLayoutItems( items , isInstant );
287 this._verticalLayoutItems( items , isInstant );
291 _verticalLayoutItems : function ( items , isInstant)
293 if ( !items || !items.length ) {
298 ['xs', 'xs', 'xs', 'tall'],
299 ['xs', 'xs', 'tall'],
311 ['tall', 'xs', 'xs', 'xs'],
312 ['tall', 'xs', 'xs'],
324 Roo.each(items, function(item, k){
327 // these layouts take up a full box,
361 var filterPattern = function(box, length)
369 var pattern = box.slice(0, length);
373 Roo.each(pattern, function(i){
377 Roo.each(standard, function(s){
379 if(String(s) != String(format)){
388 if(!match && length == 1){
393 filterPattern(box, length - 1);
399 box = box.slice(length, box.length);
401 filterPattern(box, 4);
407 Roo.each(boxes, function(box, k){
418 filterPattern(box, 4);
422 this._processVerticalLayoutQueue( queue, isInstant );
426 // _verticalAlternativeLayoutItems : function( items , isInstant )
428 // if ( !items || !items.length ) {
432 // this._processVerticalAlternativeLayoutQueue( items, isInstant );
436 _horizontalLayoutItems : function ( items , isInstant)
438 if ( !items || !items.length || items.length < 3) {
444 var eItems = items.slice(0, 3);
446 items = items.slice(3, items.length);
449 ['xs', 'xs', 'xs', 'wide'],
450 ['xs', 'xs', 'wide'],
462 ['wide', 'xs', 'xs', 'xs'],
463 ['wide', 'xs', 'xs'],
476 Roo.each(items, function(item, k){
513 var filterPattern = function(box, length)
521 var pattern = box.slice(0, length);
525 Roo.each(pattern, function(i){
529 Roo.each(standard, function(s){
531 if(String(s) != String(format)){
540 if(!match && length == 1){
545 filterPattern(box, length - 1);
551 box = box.slice(length, box.length);
553 filterPattern(box, 4);
559 Roo.each(boxes, function(box, k){
570 filterPattern(box, 4);
577 var pos = this.el.getBox(true);
581 var maxX = pos.right - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
585 Roo.each(queue, function(box){
589 Roo.each(box, function(b){
591 b.el.setVisibilityMode(Roo.Element.DISPLAY);
601 Roo.each(box, function(b){
603 b.el.setVisibilityMode(Roo.Element.DISPLAY);
606 mx = Math.max(mx, b.x);
610 maxX = maxX - this.unitWidth * mx - this.gutter * (mx - 1) - this.padWidth;
614 Roo.each(box, function(b){
616 b.el.setVisibilityMode(Roo.Element.DISPLAY);
630 this._processHorizontalLayoutQueue( prune, eItems, isInstant );
633 /** Sets position of item in DOM
634 * @param {Element} item
635 * @param {Number} x - horizontal position
636 * @param {Number} y - vertical position
637 * @param {Boolean} isInstant - disables transitions
639 _processVerticalLayoutQueue : function( queue, isInstant )
641 var pos = this.el.getBox(true);
646 for (var i = 0; i < this.cols; i++){
650 Roo.each(queue, function(box, k){
652 var col = k % this.cols;
654 Roo.each(box, function(b,kk){
656 b.el.position('absolute');
658 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
659 var height = Math.floor(this.unitHeight * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
661 if(b.size == 'md-left' || b.size == 'md-right'){
662 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
663 height = Math.floor(this.unitHeight * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
666 b.el.setWidth(width);
667 b.el.setHeight(height);
669 b.el.select('iframe',true).setSize(width,height);
673 for (var i = 0; i < this.cols; i++){
675 if(maxY[i] < maxY[col]){
680 col = Math.min(col, i);
684 x = pos.x + col * (this.colWidth + this.padWidth);
692 positions = this.getVerticalOneBoxColPositions(x, y, box);
695 positions = this.getVerticalTwoBoxColPositions(x, y, box);
698 positions = this.getVerticalThreeBoxColPositions(x, y, box);
701 positions = this.getVerticalFourBoxColPositions(x, y, box);
707 Roo.each(box, function(b,kk){
709 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
711 var sz = b.el.getSize();
713 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
721 for (var i = 0; i < this.cols; i++){
722 mY = Math.max(mY, maxY[i]);
725 this.el.setHeight(mY - pos.y);
729 // _processVerticalAlternativeLayoutQueue : function( items, isInstant )
731 // var pos = this.el.getBox(true);
734 // var maxX = pos.right;
736 // var maxHeight = 0;
738 // Roo.each(items, function(item, k){
742 // item.el.position('absolute');
744 // var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
746 // item.el.setWidth(width);
748 // var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
750 // item.el.setHeight(height);
753 // item.el.setXY([x, y], isInstant ? false : true);
755 // item.el.setXY([maxX - width, y], isInstant ? false : true);
758 // y = y + height + this.alternativePadWidth;
760 // maxHeight = maxHeight + height + this.alternativePadWidth;
764 // this.el.setHeight(maxHeight);
768 _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
770 var pos = this.el.getBox(true);
775 var maxX = pos.right;
777 this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
779 var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
781 Roo.each(queue, function(box, k){
783 Roo.each(box, function(b, kk){
785 b.el.position('absolute');
787 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
788 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
790 if(b.size == 'md-left' || b.size == 'md-right'){
791 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
792 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
795 b.el.setWidth(width);
796 b.el.setHeight(height);
808 positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
811 positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
814 positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
817 positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
823 Roo.each(box, function(b,kk){
825 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
827 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
835 _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
837 Roo.each(eItems, function(b,k){
839 b.size = (k == 0) ? 'sm' : 'xs';
840 b.x = (k == 0) ? 2 : 1;
841 b.y = (k == 0) ? 2 : 1;
843 b.el.position('absolute');
845 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
847 b.el.setWidth(width);
849 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
851 b.el.setHeight(height);
858 x : maxX - this.unitWidth * 2 - this.gutter,
863 x : maxX - this.unitWidth,
864 y : minY + (this.unitWidth + this.gutter) * 2
868 x : maxX - this.unitWidth * 3 - this.gutter * 2,
872 Roo.each(eItems, function(b,k){
874 b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
880 getVerticalOneBoxColPositions : function(x, y, box)
884 var rand = Math.floor(Math.random() * ((4 - box[0].x)));
886 if(box[0].size == 'md-left'){
890 if(box[0].size == 'md-right'){
895 x : x + (this.unitWidth + this.gutter) * rand,
902 getVerticalTwoBoxColPositions : function(x, y, box)
906 if(box[0].size == 'xs'){
910 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[1].y))
914 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
928 x : x + (this.unitWidth + this.gutter) * 2,
929 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[0].y))
936 getVerticalThreeBoxColPositions : function(x, y, box)
940 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
948 x : x + (this.unitWidth + this.gutter) * 1,
953 x : x + (this.unitWidth + this.gutter) * 2,
961 if(box[0].size == 'xs' && box[1].size == 'xs'){
970 y : y + ((this.unitHeight + this.gutter) * (box[2].y - 1))
974 x : x + (this.unitWidth + this.gutter) * 1,
988 x : x + (this.unitWidth + this.gutter) * 2,
993 x : x + (this.unitWidth + this.gutter) * 2,
994 y : y + (this.unitHeight + this.gutter) * (box[0].y - 1)
1001 getVerticalFourBoxColPositions : function(x, y, box)
1005 if(box[0].size == 'xs'){
1014 y : y + (this.unitHeight + this.gutter) * 1
1019 y : y + (this.unitHeight + this.gutter) * 2
1023 x : x + (this.unitWidth + this.gutter) * 1,
1037 x : x + (this.unitWidth + this.gutter) * 2,
1042 x : x + (this.unitHeightunitWidth + this.gutter) * 2,
1043 y : y + (this.unitHeight + this.gutter) * 1
1047 x : x + (this.unitWidth + this.gutter) * 2,
1048 y : y + (this.unitWidth + this.gutter) * 2
1055 getHorizontalOneBoxColPositions : function(maxX, minY, box)
1059 if(box[0].size == 'md-left'){
1061 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1068 if(box[0].size == 'md-right'){
1070 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1071 y : minY + (this.unitWidth + this.gutter) * 1
1077 var rand = Math.floor(Math.random() * (4 - box[0].y));
1080 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1081 y : minY + (this.unitWidth + this.gutter) * rand
1088 getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1092 if(box[0].size == 'xs'){
1095 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1100 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1101 y : minY + (this.unitWidth + this.gutter) * (3 - box[1].y)
1109 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1114 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1115 y : minY + (this.unitWidth + this.gutter) * 2
1122 getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1126 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
1129 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1134 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1135 y : minY + (this.unitWidth + this.gutter) * 1
1139 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1140 y : minY + (this.unitWidth + this.gutter) * 2
1147 if(box[0].size == 'xs' && box[1].size == 'xs'){
1150 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1155 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1160 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1161 y : minY + (this.unitWidth + this.gutter) * 1
1169 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1174 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1175 y : minY + (this.unitWidth + this.gutter) * 2
1179 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1180 y : minY + (this.unitWidth + this.gutter) * 2
1187 getHorizontalFourBoxColPositions : function(maxX, minY, box)
1191 if(box[0].size == 'xs'){
1194 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1199 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1204 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),
1209 x : maxX - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1210 y : minY + (this.unitWidth + this.gutter) * 1
1218 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1223 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1224 y : minY + (this.unitWidth + this.gutter) * 2
1228 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1229 y : minY + (this.unitWidth + this.gutter) * 2
1233 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),
1234 y : minY + (this.unitWidth + this.gutter) * 2
1242 * remove a Masonry Brick
1243 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to remove
1245 removeBrick : function(brick_id)
1251 for (var i = 0; i<this.bricks.length; i++) {
1252 if (this.bricks[i].id == brick_id) {
1253 this.bricks.splice(i,1);
1254 this.el.dom.removeChild(Roo.get(brick_id).dom);
1261 * adds a Masonry Brick
1262 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1264 addBrick : function(cfg)
1266 var cn = new Roo.bootstrap.MasonryBrick(cfg);
1267 //this.register(cn);
1268 cn.parentId = this.id;
1274 * register a Masonry Brick
1275 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1278 register : function(brick)
1280 this.bricks.push(brick);
1281 brick.masonryId = this.id;
1285 * clear all the Masonry Brick
1287 clearAll : function()
1290 //this.getChildContainer().dom.innerHTML = "";
1291 this.el.dom.innerHTML = '';
1294 getSelected : function()
1296 if (!this.selectedBrick) {
1300 return this.selectedBrick;
1304 Roo.apply(Roo.bootstrap.LayoutMasonry, {
1308 * register a Masonry Layout
1309 * @param {Roo.bootstrap.LayoutMasonry} the masonry layout to add
1312 register : function(layout)
1314 this.groups[layout.id] = layout;
1317 * fetch a Masonry Layout based on the masonry layout ID
1318 * @param {string} the masonry layout to add
1319 * @returns {Roo.bootstrap.LayoutMasonry} the masonry layout
1322 get: function(layout_id) {
1323 if (typeof(this.groups[layout_id]) == 'undefined') {
1326 return this.groups[layout_id] ;