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).first().set('width', width);
668 b.el.select('iframe',true).first().set(height);
670 // b.el.select('iframe',true).setSize(width,height);
674 for (var i = 0; i < this.cols; i++){
676 if(maxY[i] < maxY[col]){
681 col = Math.min(col, i);
685 x = pos.x + col * (this.colWidth + this.padWidth);
693 positions = this.getVerticalOneBoxColPositions(x, y, box);
696 positions = this.getVerticalTwoBoxColPositions(x, y, box);
699 positions = this.getVerticalThreeBoxColPositions(x, y, box);
702 positions = this.getVerticalFourBoxColPositions(x, y, box);
708 Roo.each(box, function(b,kk){
710 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
712 var sz = b.el.getSize();
714 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
722 for (var i = 0; i < this.cols; i++){
723 mY = Math.max(mY, maxY[i]);
726 this.el.setHeight(mY - pos.y);
730 // _processVerticalAlternativeLayoutQueue : function( items, isInstant )
732 // var pos = this.el.getBox(true);
735 // var maxX = pos.right;
737 // var maxHeight = 0;
739 // Roo.each(items, function(item, k){
743 // item.el.position('absolute');
745 // var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
747 // item.el.setWidth(width);
749 // var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
751 // item.el.setHeight(height);
754 // item.el.setXY([x, y], isInstant ? false : true);
756 // item.el.setXY([maxX - width, y], isInstant ? false : true);
759 // y = y + height + this.alternativePadWidth;
761 // maxHeight = maxHeight + height + this.alternativePadWidth;
765 // this.el.setHeight(maxHeight);
769 _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
771 var pos = this.el.getBox(true);
776 var maxX = pos.right;
778 this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
780 var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
782 Roo.each(queue, function(box, k){
784 Roo.each(box, function(b, kk){
786 b.el.position('absolute');
788 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
789 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
791 if(b.size == 'md-left' || b.size == 'md-right'){
792 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
793 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
796 b.el.setWidth(width);
797 b.el.setHeight(height);
809 positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
812 positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
815 positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
818 positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
824 Roo.each(box, function(b,kk){
826 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
828 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
836 _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
838 Roo.each(eItems, function(b,k){
840 b.size = (k == 0) ? 'sm' : 'xs';
841 b.x = (k == 0) ? 2 : 1;
842 b.y = (k == 0) ? 2 : 1;
844 b.el.position('absolute');
846 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
848 b.el.setWidth(width);
850 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
852 b.el.setHeight(height);
859 x : maxX - this.unitWidth * 2 - this.gutter,
864 x : maxX - this.unitWidth,
865 y : minY + (this.unitWidth + this.gutter) * 2
869 x : maxX - this.unitWidth * 3 - this.gutter * 2,
873 Roo.each(eItems, function(b,k){
875 b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
881 getVerticalOneBoxColPositions : function(x, y, box)
885 var rand = Math.floor(Math.random() * ((4 - box[0].x)));
887 if(box[0].size == 'md-left'){
891 if(box[0].size == 'md-right'){
896 x : x + (this.unitWidth + this.gutter) * rand,
903 getVerticalTwoBoxColPositions : function(x, y, box)
907 if(box[0].size == 'xs'){
911 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[1].y))
915 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
929 x : x + (this.unitWidth + this.gutter) * 2,
930 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[0].y))
937 getVerticalThreeBoxColPositions : function(x, y, box)
941 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
949 x : x + (this.unitWidth + this.gutter) * 1,
954 x : x + (this.unitWidth + this.gutter) * 2,
962 if(box[0].size == 'xs' && box[1].size == 'xs'){
971 y : y + ((this.unitHeight + this.gutter) * (box[2].y - 1))
975 x : x + (this.unitWidth + this.gutter) * 1,
989 x : x + (this.unitWidth + this.gutter) * 2,
994 x : x + (this.unitWidth + this.gutter) * 2,
995 y : y + (this.unitHeight + this.gutter) * (box[0].y - 1)
1002 getVerticalFourBoxColPositions : function(x, y, box)
1006 if(box[0].size == 'xs'){
1015 y : y + (this.unitHeight + this.gutter) * 1
1020 y : y + (this.unitHeight + this.gutter) * 2
1024 x : x + (this.unitWidth + this.gutter) * 1,
1038 x : x + (this.unitWidth + this.gutter) * 2,
1043 x : x + (this.unitHeightunitWidth + this.gutter) * 2,
1044 y : y + (this.unitHeight + this.gutter) * 1
1048 x : x + (this.unitWidth + this.gutter) * 2,
1049 y : y + (this.unitWidth + this.gutter) * 2
1056 getHorizontalOneBoxColPositions : function(maxX, minY, box)
1060 if(box[0].size == 'md-left'){
1062 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1069 if(box[0].size == 'md-right'){
1071 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1072 y : minY + (this.unitWidth + this.gutter) * 1
1078 var rand = Math.floor(Math.random() * (4 - box[0].y));
1081 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1082 y : minY + (this.unitWidth + this.gutter) * rand
1089 getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1093 if(box[0].size == 'xs'){
1096 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1101 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1102 y : minY + (this.unitWidth + this.gutter) * (3 - box[1].y)
1110 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1115 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1116 y : minY + (this.unitWidth + this.gutter) * 2
1123 getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1127 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
1130 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1135 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1136 y : minY + (this.unitWidth + this.gutter) * 1
1140 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1141 y : minY + (this.unitWidth + this.gutter) * 2
1148 if(box[0].size == 'xs' && box[1].size == 'xs'){
1151 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1156 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1161 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1162 y : minY + (this.unitWidth + this.gutter) * 1
1170 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1175 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1176 y : minY + (this.unitWidth + this.gutter) * 2
1180 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1181 y : minY + (this.unitWidth + this.gutter) * 2
1188 getHorizontalFourBoxColPositions : function(maxX, minY, box)
1192 if(box[0].size == 'xs'){
1195 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1200 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].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) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1210 x : maxX - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1211 y : minY + (this.unitWidth + this.gutter) * 1
1219 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1224 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1225 y : minY + (this.unitWidth + this.gutter) * 2
1229 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].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) - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1235 y : minY + (this.unitWidth + this.gutter) * 2
1243 * remove a Masonry Brick
1244 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to remove
1246 removeBrick : function(brick_id)
1252 for (var i = 0; i<this.bricks.length; i++) {
1253 if (this.bricks[i].id == brick_id) {
1254 this.bricks.splice(i,1);
1255 this.el.dom.removeChild(Roo.get(brick_id).dom);
1262 * adds a Masonry Brick
1263 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1265 addBrick : function(cfg)
1267 var cn = new Roo.bootstrap.MasonryBrick(cfg);
1268 //this.register(cn);
1269 cn.parentId = this.id;
1275 * register a Masonry Brick
1276 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1279 register : function(brick)
1281 this.bricks.push(brick);
1282 brick.masonryId = this.id;
1286 * clear all the Masonry Brick
1288 clearAll : function()
1291 //this.getChildContainer().dom.innerHTML = "";
1292 this.el.dom.innerHTML = '';
1295 getSelected : function()
1297 if (!this.selectedBrick) {
1301 return this.selectedBrick;
1305 Roo.apply(Roo.bootstrap.LayoutMasonry, {
1309 * register a Masonry Layout
1310 * @param {Roo.bootstrap.LayoutMasonry} the masonry layout to add
1313 register : function(layout)
1315 this.groups[layout.id] = layout;
1318 * fetch a Masonry Layout based on the masonry layout ID
1319 * @param {string} the masonry layout to add
1320 * @returns {Roo.bootstrap.LayoutMasonry} the masonry layout
1323 get: function(layout_id) {
1324 if (typeof(this.groups[layout_id]) == 'undefined') {
1327 return this.groups[layout_id] ;