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 if(b.videourl && b.videourl.length) {
665 alert([width, height]);
668 b.el.setWidth(width);
669 b.el.setHeight(height);
671 b.el.select('iframe',true).setSize(width,height);
675 for (var i = 0; i < this.cols; i++){
677 if(maxY[i] < maxY[col]){
682 col = Math.min(col, i);
686 x = pos.x + col * (this.colWidth + this.padWidth);
694 positions = this.getVerticalOneBoxColPositions(x, y, box);
697 positions = this.getVerticalTwoBoxColPositions(x, y, box);
700 positions = this.getVerticalThreeBoxColPositions(x, y, box);
703 positions = this.getVerticalFourBoxColPositions(x, y, box);
709 Roo.each(box, function(b,kk){
711 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
713 var sz = b.el.getSize();
715 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
723 for (var i = 0; i < this.cols; i++){
724 mY = Math.max(mY, maxY[i]);
727 this.el.setHeight(mY - pos.y);
731 // _processVerticalAlternativeLayoutQueue : function( items, isInstant )
733 // var pos = this.el.getBox(true);
736 // var maxX = pos.right;
738 // var maxHeight = 0;
740 // Roo.each(items, function(item, k){
744 // item.el.position('absolute');
746 // var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
748 // item.el.setWidth(width);
750 // var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
752 // item.el.setHeight(height);
755 // item.el.setXY([x, y], isInstant ? false : true);
757 // item.el.setXY([maxX - width, y], isInstant ? false : true);
760 // y = y + height + this.alternativePadWidth;
762 // maxHeight = maxHeight + height + this.alternativePadWidth;
766 // this.el.setHeight(maxHeight);
770 _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
772 var pos = this.el.getBox(true);
777 var maxX = pos.right;
779 this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
781 var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
783 Roo.each(queue, function(box, k){
785 Roo.each(box, function(b, kk){
787 b.el.position('absolute');
789 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
790 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
792 if(b.size == 'md-left' || b.size == 'md-right'){
793 width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
794 height = Math.floor(this.unitWidth * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
797 b.el.setWidth(width);
798 b.el.setHeight(height);
810 positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
813 positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
816 positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
819 positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
825 Roo.each(box, function(b,kk){
827 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
829 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
837 _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
839 Roo.each(eItems, function(b,k){
841 b.size = (k == 0) ? 'sm' : 'xs';
842 b.x = (k == 0) ? 2 : 1;
843 b.y = (k == 0) ? 2 : 1;
845 b.el.position('absolute');
847 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
849 b.el.setWidth(width);
851 var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
853 b.el.setHeight(height);
860 x : maxX - this.unitWidth * 2 - this.gutter,
865 x : maxX - this.unitWidth,
866 y : minY + (this.unitWidth + this.gutter) * 2
870 x : maxX - this.unitWidth * 3 - this.gutter * 2,
874 Roo.each(eItems, function(b,k){
876 b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
882 getVerticalOneBoxColPositions : function(x, y, box)
886 var rand = Math.floor(Math.random() * ((4 - box[0].x)));
888 if(box[0].size == 'md-left'){
892 if(box[0].size == 'md-right'){
897 x : x + (this.unitWidth + this.gutter) * rand,
904 getVerticalTwoBoxColPositions : function(x, y, box)
908 if(box[0].size == 'xs'){
912 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[1].y))
916 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
930 x : x + (this.unitWidth + this.gutter) * 2,
931 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[0].y))
938 getVerticalThreeBoxColPositions : function(x, y, box)
942 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
950 x : x + (this.unitWidth + this.gutter) * 1,
955 x : x + (this.unitWidth + this.gutter) * 2,
963 if(box[0].size == 'xs' && box[1].size == 'xs'){
972 y : y + ((this.unitHeight + this.gutter) * (box[2].y - 1))
976 x : x + (this.unitWidth + this.gutter) * 1,
990 x : x + (this.unitWidth + this.gutter) * 2,
995 x : x + (this.unitWidth + this.gutter) * 2,
996 y : y + (this.unitHeight + this.gutter) * (box[0].y - 1)
1003 getVerticalFourBoxColPositions : function(x, y, box)
1007 if(box[0].size == 'xs'){
1016 y : y + (this.unitHeight + this.gutter) * 1
1021 y : y + (this.unitHeight + this.gutter) * 2
1025 x : x + (this.unitWidth + this.gutter) * 1,
1039 x : x + (this.unitWidth + this.gutter) * 2,
1044 x : x + (this.unitHeightunitWidth + this.gutter) * 2,
1045 y : y + (this.unitHeight + this.gutter) * 1
1049 x : x + (this.unitWidth + this.gutter) * 2,
1050 y : y + (this.unitWidth + this.gutter) * 2
1057 getHorizontalOneBoxColPositions : function(maxX, minY, box)
1061 if(box[0].size == 'md-left'){
1063 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1070 if(box[0].size == 'md-right'){
1072 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1073 y : minY + (this.unitWidth + this.gutter) * 1
1079 var rand = Math.floor(Math.random() * (4 - box[0].y));
1082 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1083 y : minY + (this.unitWidth + this.gutter) * rand
1090 getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1094 if(box[0].size == 'xs'){
1097 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1102 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1103 y : minY + (this.unitWidth + this.gutter) * (3 - box[1].y)
1111 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1116 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1117 y : minY + (this.unitWidth + this.gutter) * 2
1124 getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1128 if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
1131 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1136 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1137 y : minY + (this.unitWidth + this.gutter) * 1
1141 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1142 y : minY + (this.unitWidth + this.gutter) * 2
1149 if(box[0].size == 'xs' && box[1].size == 'xs'){
1152 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1157 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1162 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1163 y : minY + (this.unitWidth + this.gutter) * 1
1171 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1176 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1177 y : minY + (this.unitWidth + this.gutter) * 2
1181 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1182 y : minY + (this.unitWidth + this.gutter) * 2
1189 getHorizontalFourBoxColPositions : function(maxX, minY, box)
1193 if(box[0].size == 'xs'){
1196 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1201 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1) - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1206 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),
1211 x : maxX - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1212 y : minY + (this.unitWidth + this.gutter) * 1
1220 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1225 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1226 y : minY + (this.unitWidth + this.gutter) * 2
1230 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1) - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1231 y : minY + (this.unitWidth + this.gutter) * 2
1235 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),
1236 y : minY + (this.unitWidth + this.gutter) * 2
1244 * remove a Masonry Brick
1245 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to remove
1247 removeBrick : function(brick_id)
1253 for (var i = 0; i<this.bricks.length; i++) {
1254 if (this.bricks[i].id == brick_id) {
1255 this.bricks.splice(i,1);
1256 this.el.dom.removeChild(Roo.get(brick_id).dom);
1263 * adds a Masonry Brick
1264 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1266 addBrick : function(cfg)
1268 var cn = new Roo.bootstrap.MasonryBrick(cfg);
1269 //this.register(cn);
1270 cn.parentId = this.id;
1276 * register a Masonry Brick
1277 * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1280 register : function(brick)
1282 this.bricks.push(brick);
1283 brick.masonryId = this.id;
1287 * clear all the Masonry Brick
1289 clearAll : function()
1292 //this.getChildContainer().dom.innerHTML = "";
1293 this.el.dom.innerHTML = '';
1296 getSelected : function()
1298 if (!this.selectedBrick) {
1302 return this.selectedBrick;
1306 Roo.apply(Roo.bootstrap.LayoutMasonry, {
1310 * register a Masonry Layout
1311 * @param {Roo.bootstrap.LayoutMasonry} the masonry layout to add
1314 register : function(layout)
1316 this.groups[layout.id] = layout;
1319 * fetch a Masonry Layout based on the masonry layout ID
1320 * @param {string} the masonry layout to add
1321 * @returns {Roo.bootstrap.LayoutMasonry} the masonry layout
1324 get: function(layout_id) {
1325 if (typeof(this.groups[layout_id]) == 'undefined') {
1328 return this.groups[layout_id] ;