Roo/bootstrap/LayoutMasonry.js
[roojs1] / Roo / bootstrap / LayoutMasonry.js
1 /**
2  *
3  * This is based on 
4  * http://masonry.desandro.com
5  *
6  * The idea is to render all the bricks based on vertical width...
7  *
8  * The original code extends 'outlayer' - we might need to use that....
9  * 
10  */
11
12
13 /**
14  * @class Roo.bootstrap.LayoutMasonry
15  * @extends Roo.bootstrap.Component
16  * Bootstrap Layout Masonry class
17  * 
18  * @constructor
19  * Create a new Element
20  * @param {Object} config The config object
21  */
22
23 Roo.bootstrap.LayoutMasonry = function(config){
24     
25     Roo.bootstrap.LayoutMasonry.superclass.constructor.call(this, config);
26     
27     this.bricks = [];
28     
29     Roo.bootstrap.LayoutMasonry.register(this);
30     
31     this.addEvents({
32         // raw events
33         /**
34          * @event layout
35          * Fire after layout the items
36          * @param {Roo.bootstrap.LayoutMasonry} this
37          * @param {Roo.EventObject} e
38          */
39         "layout" : true
40     });
41     
42 };
43
44 Roo.extend(Roo.bootstrap.LayoutMasonry, Roo.bootstrap.Component,  {
45     
46     /**
47      * @cfg {Boolean} isLayoutInstant = no animation?
48      */   
49     isLayoutInstant : false, // needed?
50    
51     /**
52      * @cfg {Number} boxWidth  width of the columns
53      */   
54     boxWidth : 450,
55     
56       /**
57      * @cfg {Number} boxHeight  - 0 for square, or fix it at a certian height
58      */   
59     boxHeight : 0,
60     
61     /**
62      * @cfg {Number} padWidth padding below box..
63      */   
64     padWidth : 10, 
65     
66     /**
67      * @cfg {Number} gutter gutter width..
68      */   
69     gutter : 10,
70     
71      /**
72      * @cfg {Number} maxCols maximum number of columns
73      */   
74     
75     maxCols: 0,
76     
77     /**
78      * @cfg {Boolean} isAutoInitial defalut true
79      */   
80     isAutoInitial : true, 
81     
82     containerWidth: 0,
83     
84     /**
85      * @cfg {Boolean} isHorizontal defalut false
86      */   
87     isHorizontal : false, 
88
89     currentSize : null,
90     
91     tag: 'div',
92     
93     cls: '',
94     
95     bricks: null, //CompositeElement
96     
97     cols : 1,
98     
99     _isLayoutInited : false,
100     
101 //    isAlternative : false, // only use for vertical layout...
102     
103     /**
104      * @cfg {Number} alternativePadWidth padding below box..
105      */   
106     alternativePadWidth : 50,
107     
108     selectedBrick : [],
109     
110     getAutoCreate : function(){
111         
112         var cfg = Roo.apply({}, Roo.bootstrap.LayoutMasonry.superclass.getAutoCreate.call(this));
113         
114         var cfg = {
115             tag: this.tag,
116             cls: 'blog-masonary-wrapper ' + this.cls,
117             cn : {
118                 cls : 'mas-boxes masonary'
119             }
120         };
121         
122         return cfg;
123     },
124     
125     getChildContainer: function( )
126     {
127         if (this.boxesEl) {
128             return this.boxesEl;
129         }
130         
131         this.boxesEl = this.el.select('.mas-boxes').first();
132         
133         return this.boxesEl;
134     },
135     
136     
137     initEvents : function()
138     {
139         var _this = this;
140         
141         if(this.isAutoInitial){
142             Roo.log('hook children rendered');
143             this.on('childrenrendered', function() {
144                 Roo.log('children rendered');
145                 _this.initial();
146             } ,this);
147         }
148     },
149     
150     initial : function()
151     {
152         this.selectedBrick = [];
153         
154         this.currentSize = this.el.getBox(true);
155         
156         Roo.EventManager.onWindowResize(this.resize, this); 
157
158         if(!this.isAutoInitial){
159             this.layout();
160             return;
161         }
162         
163         this.layout();
164         
165         return;
166         //this.layout.defer(500,this);
167         
168     },
169     
170     resize : function()
171     {
172         var cs = this.el.getBox(true);
173         
174         if (
175                 this.currentSize.width == cs.width && 
176                 this.currentSize.x == cs.x && 
177                 this.currentSize.height == cs.height && 
178                 this.currentSize.y == cs.y 
179         ) {
180             Roo.log("no change in with or X or Y");
181             return;
182         }
183         
184         this.currentSize = cs;
185         
186         this.layout();
187         
188     },
189     
190     layout : function()
191     {   
192         this._resetLayout();
193         
194         var isInstant = this.isLayoutInstant !== undefined ? this.isLayoutInstant : !this._isLayoutInited;
195         
196         this.layoutItems( isInstant );
197       
198         this._isLayoutInited = true;
199         
200         this.fireEvent('layout', this);
201         
202     },
203     
204     _resetLayout : function()
205     {
206         if(this.isHorizontal){
207             this.horizontalMeasureColumns();
208             return;
209         }
210         
211         this.verticalMeasureColumns();
212         
213     },
214     
215     verticalMeasureColumns : function()
216     {
217         this.getContainerWidth();
218         
219 //        if(Roo.lib.Dom.getViewWidth() < 768 && this.isAlternative){
220 //            this.colWidth = Math.floor(this.containerWidth * 0.8);
221 //            return;
222 //        }
223         
224         var boxWidth = this.boxWidth + this.padWidth;
225         
226         if(this.containerWidth < this.boxWidth){
227             boxWidth = this.containerWidth
228         }
229         
230         var containerWidth = this.containerWidth;
231         
232         var cols = Math.floor(containerWidth / boxWidth);
233         
234         this.cols = Math.max( cols, 1 );
235         
236         this.cols = this.maxCols > 0 ? Math.min( this.cols, this.maxCols ) : this.cols;
237         
238         var totalBoxWidth = this.cols * boxWidth - this.padWidth;
239         
240         var avail = Math.floor((containerWidth - totalBoxWidth) / this.cols);
241         
242         this.colWidth = boxWidth + avail - this.padWidth;
243         
244         this.unitWidth = Math.round((this.colWidth - (this.gutter * 2)) / 3);
245         this.unitHeight = this.boxHeight > 0 ? this.boxHeight  : this.unitWidth;
246     },
247     
248     horizontalMeasureColumns : function()
249     {
250         this.getContainerWidth();
251         
252         var boxWidth = this.boxWidth;
253         
254         if(this.containerWidth < boxWidth){
255             boxWidth = this.containerWidth;
256         }
257         
258         this.unitWidth = Math.floor((boxWidth - (this.gutter * 2)) / 3);
259         
260         this.el.setHeight(boxWidth);
261         
262     },
263     
264     getContainerWidth : function()
265     {
266         this.containerWidth = this.el.getBox(true).width;  //maybe use getComputedWidth
267     },
268     
269     layoutItems : function( isInstant )
270     {
271         Roo.log(this.bricks);
272         
273         var items = Roo.apply([], this.bricks);
274         
275         if(this.isHorizontal){
276             this._horizontalLayoutItems( items , isInstant );
277             return;
278         }
279         
280 //        if(Roo.lib.Dom.getViewWidth() < 768 && this.isAlternative){
281 //            this._verticalAlternativeLayoutItems( items , isInstant );
282 //            return;
283 //        }
284         
285         this._verticalLayoutItems( items , isInstant );
286         
287     },
288     
289     _verticalLayoutItems : function ( items , isInstant)
290     {
291         if ( !items || !items.length ) {
292             return;
293         }
294         
295         var standard = [
296             ['xs', 'xs', 'xs', 'tall'],
297             ['xs', 'xs', 'tall'],
298             ['xs', 'xs', 'sm'],
299             ['xs', 'xs', 'xs'],
300             ['xs', 'tall'],
301             ['xs', 'sm'],
302             ['xs', 'xs'],
303             ['xs'],
304             
305             ['sm', 'xs', 'xs'],
306             ['sm', 'xs'],
307             ['sm'],
308             
309             ['tall', 'xs', 'xs', 'xs'],
310             ['tall', 'xs', 'xs'],
311             ['tall', 'xs'],
312             ['tall']
313             
314         ];
315         
316         var queue = [];
317         
318         var boxes = [];
319         
320         var box = [];
321         
322         Roo.each(items, function(item, k){
323             
324             switch (item.size) {
325                 // these layouts take up a full box,
326                 case 'md' :
327                 case 'md-left' :
328                 case 'md-right' :
329                 case 'wide' :
330                     
331                     if(box.length){
332                         boxes.push(box);
333                         box = [];
334                     }
335                     
336                     boxes.push([item]);
337                     
338                     break;
339                     
340                 case 'xs' :
341                 case 'sm' :
342                 case 'tall' :
343                     
344                     box.push(item);
345                     
346                     break;
347                 default :
348                     break;
349                     
350             }
351             
352         }, this);
353         
354         if(box.length){
355             boxes.push(box);
356             box = [];
357         }
358         
359         var filterPattern = function(box, length)
360         {
361             if(!box.length){
362                 return;
363             }
364             
365             var match = false;
366             
367             var pattern = box.slice(0, length);
368             
369             var format = [];
370             
371             Roo.each(pattern, function(i){
372                 format.push(i.size);
373             }, this);
374             
375             Roo.each(standard, function(s){
376                 
377                 if(String(s) != String(format)){
378                     return;
379                 }
380                 
381                 match = true;
382                 return false;
383                 
384             }, this);
385             
386             if(!match && length == 1){
387                 return;
388             }
389             
390             if(!match){
391                 filterPattern(box, length - 1);
392                 return;
393             }
394                 
395             queue.push(pattern);
396
397             box = box.slice(length, box.length);
398
399             filterPattern(box, 4);
400
401             return;
402             
403         }
404         
405         Roo.each(boxes, function(box, k){
406             
407             if(!box.length){
408                 return;
409             }
410             
411             if(box.length == 1){
412                 queue.push(box);
413                 return;
414             }
415             
416             filterPattern(box, 4);
417             
418         }, this);
419         
420         this._processVerticalLayoutQueue( queue, isInstant );
421         
422     },
423     
424 //    _verticalAlternativeLayoutItems : function( items , isInstant )
425 //    {
426 //        if ( !items || !items.length ) {
427 //            return;
428 //        }
429 //
430 //        this._processVerticalAlternativeLayoutQueue( items, isInstant );
431 //        
432 //    },
433     
434     _horizontalLayoutItems : function ( items , isInstant)
435     {
436         if ( !items || !items.length || items.length < 3) {
437             return;
438         }
439         
440         items.reverse();
441         
442         var eItems = items.slice(0, 3);
443         
444         items = items.slice(3, items.length);
445         
446         var standard = [
447             ['xs', 'xs', 'xs', 'wide'],
448             ['xs', 'xs', 'wide'],
449             ['xs', 'xs', 'sm'],
450             ['xs', 'xs', 'xs'],
451             ['xs', 'wide'],
452             ['xs', 'sm'],
453             ['xs', 'xs'],
454             ['xs'],
455             
456             ['sm', 'xs', 'xs'],
457             ['sm', 'xs'],
458             ['sm'],
459             
460             ['wide', 'xs', 'xs', 'xs'],
461             ['wide', 'xs', 'xs'],
462             ['wide', 'xs'],
463             ['wide'],
464             
465             ['wide-thin']
466         ];
467         
468         var queue = [];
469         
470         var boxes = [];
471         
472         var box = [];
473         
474         Roo.each(items, function(item, k){
475             
476             switch (item.size) {
477                 case 'md' :
478                 case 'md-left' :
479                 case 'md-right' :
480                 case 'tall' :
481                     
482                     if(box.length){
483                         boxes.push(box);
484                         box = [];
485                     }
486                     
487                     boxes.push([item]);
488                     
489                     break;
490                     
491                 case 'xs' :
492                 case 'sm' :
493                 case 'wide' :
494                 case 'wide-thin' :
495                     
496                     box.push(item);
497                     
498                     break;
499                 default :
500                     break;
501                     
502             }
503             
504         }, this);
505         
506         if(box.length){
507             boxes.push(box);
508             box = [];
509         }
510         
511         var filterPattern = function(box, length)
512         {
513             if(!box.length){
514                 return;
515             }
516             
517             var match = false;
518             
519             var pattern = box.slice(0, length);
520             
521             var format = [];
522             
523             Roo.each(pattern, function(i){
524                 format.push(i.size);
525             }, this);
526             
527             Roo.each(standard, function(s){
528                 
529                 if(String(s) != String(format)){
530                     return;
531                 }
532                 
533                 match = true;
534                 return false;
535                 
536             }, this);
537             
538             if(!match && length == 1){
539                 return;
540             }
541             
542             if(!match){
543                 filterPattern(box, length - 1);
544                 return;
545             }
546                 
547             queue.push(pattern);
548
549             box = box.slice(length, box.length);
550
551             filterPattern(box, 4);
552
553             return;
554             
555         }
556         
557         Roo.each(boxes, function(box, k){
558             
559             if(!box.length){
560                 return;
561             }
562             
563             if(box.length == 1){
564                 queue.push(box);
565                 return;
566             }
567             
568             filterPattern(box, 4);
569             
570         }, this);
571         
572         
573         var prune = [];
574         
575         var pos = this.el.getBox(true);
576         
577         var minX = pos.x;
578         
579         var maxX = pos.right - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
580         
581         var hit_end = false;
582         
583         Roo.each(queue, function(box){
584             
585             if(hit_end){
586                 
587                 Roo.each(box, function(b){
588                 
589                     b.el.setVisibilityMode(Roo.Element.DISPLAY);
590                     b.el.hide();
591
592                 }, this);
593
594                 return;
595             }
596             
597             var mx = 0;
598             
599             Roo.each(box, function(b){
600                 
601                 b.el.setVisibilityMode(Roo.Element.DISPLAY);
602                 b.el.show();
603
604                 mx = Math.max(mx, b.x);
605                 
606             }, this);
607             
608             maxX = maxX - this.unitWidth * mx - this.gutter * (mx - 1) - this.padWidth;
609             
610             if(maxX < minX){
611                 
612                 Roo.each(box, function(b){
613                 
614                     b.el.setVisibilityMode(Roo.Element.DISPLAY);
615                     b.el.hide();
616                     
617                 }, this);
618                 
619                 hit_end = true;
620                 
621                 return;
622             }
623             
624             prune.push(box);
625             
626         }, this);
627         
628         this._processHorizontalLayoutQueue( prune, eItems, isInstant );
629     },
630     
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
636     */
637     _processVerticalLayoutQueue : function( queue, isInstant )
638     {
639         var pos = this.el.getBox(true);
640         var x = pos.x;
641         var y = pos.y;
642         var maxY = [];
643         
644         for (var i = 0; i < this.cols; i++){
645             maxY[i] = pos.y;
646         }
647         
648         Roo.each(queue, function(box, k){
649             
650             var col = k % this.cols;
651             
652             Roo.each(box, function(b,kk){
653                 
654                 b.el.position('absolute');
655                 
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'));
658                 
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'));
662                 }
663                 
664                 b.el.setWidth(width);
665                 b.el.setHeight(height);
666                 
667                 b.el.select('iframe',true).set('width', width);
668                 b.el.select('iframe',true).set(height)
669                 // iframe?
670 //                b.el.select('iframe',true).setSize(width,height);
671                 
672             }, this);
673             
674             for (var i = 0; i < this.cols; i++){
675                 
676                 if(maxY[i] < maxY[col]){
677                     col = i;
678                     continue;
679                 }
680                 
681                 col = Math.min(col, i);
682                 
683             }
684             
685             x = pos.x + col * (this.colWidth + this.padWidth);
686             
687             y = maxY[col];
688             
689             var positions = [];
690             
691             switch (box.length){
692                 case 1 :
693                     positions = this.getVerticalOneBoxColPositions(x, y, box);
694                     break;
695                 case 2 :
696                     positions = this.getVerticalTwoBoxColPositions(x, y, box);
697                     break;
698                 case 3 :
699                     positions = this.getVerticalThreeBoxColPositions(x, y, box);
700                     break;
701                 case 4 :
702                     positions = this.getVerticalFourBoxColPositions(x, y, box);
703                     break;
704                 default :
705                     break;
706             }
707             
708             Roo.each(box, function(b,kk){
709                 
710                 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
711                 
712                 var sz = b.el.getSize();
713                 
714                 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
715                 
716             }, this);
717             
718         }, this);
719         
720         var mY = 0;
721         
722         for (var i = 0; i < this.cols; i++){
723             mY = Math.max(mY, maxY[i]);
724         }
725         
726         this.el.setHeight(mY - pos.y);
727         
728     },
729     
730 //    _processVerticalAlternativeLayoutQueue : function( items, isInstant )
731 //    {
732 //        var pos = this.el.getBox(true);
733 //        var x = pos.x;
734 //        var y = pos.y;
735 //        var maxX = pos.right;
736 //        
737 //        var maxHeight = 0;
738 //        
739 //        Roo.each(items, function(item, k){
740 //            
741 //            var c = k % 2;
742 //            
743 //            item.el.position('absolute');
744 //                
745 //            var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
746 //
747 //            item.el.setWidth(width);
748 //
749 //            var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
750 //
751 //            item.el.setHeight(height);
752 //            
753 //            if(c == 0){
754 //                item.el.setXY([x, y], isInstant ? false : true);
755 //            } else {
756 //                item.el.setXY([maxX - width, y], isInstant ? false : true);
757 //            }
758 //            
759 //            y = y + height + this.alternativePadWidth;
760 //            
761 //            maxHeight = maxHeight + height + this.alternativePadWidth;
762 //            
763 //        }, this);
764 //        
765 //        this.el.setHeight(maxHeight);
766 //        
767 //    },
768     
769     _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
770     {
771         var pos = this.el.getBox(true);
772         
773         var minX = pos.x;
774         var minY = pos.y;
775         
776         var maxX = pos.right;
777         
778         this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
779         
780         var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
781         
782         Roo.each(queue, function(box, k){
783             
784             Roo.each(box, function(b, kk){
785                 
786                 b.el.position('absolute');
787                 
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'));
790                 
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'));
794                 }
795                 
796                 b.el.setWidth(width);
797                 b.el.setHeight(height);
798                 
799             }, this);
800             
801             if(!box.length){
802                 return;
803             }
804             
805             var positions = [];
806             
807             switch (box.length){
808                 case 1 :
809                     positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
810                     break;
811                 case 2 :
812                     positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
813                     break;
814                 case 3 :
815                     positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
816                     break;
817                 case 4 :
818                     positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
819                     break;
820                 default :
821                     break;
822             }
823             
824             Roo.each(box, function(b,kk){
825                 
826                 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
827                 
828                 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
829                 
830             }, this);
831             
832         }, this);
833         
834     },
835     
836     _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
837     {
838         Roo.each(eItems, function(b,k){
839             
840             b.size = (k == 0) ? 'sm' : 'xs';
841             b.x = (k == 0) ? 2 : 1;
842             b.y = (k == 0) ? 2 : 1;
843             
844             b.el.position('absolute');
845             
846             var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
847                 
848             b.el.setWidth(width);
849             
850             var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
851             
852             b.el.setHeight(height);
853             
854         }, this);
855
856         var positions = [];
857         
858         positions.push({
859             x : maxX - this.unitWidth * 2 - this.gutter,
860             y : minY
861         });
862         
863         positions.push({
864             x : maxX - this.unitWidth,
865             y : minY + (this.unitWidth + this.gutter) * 2
866         });
867         
868         positions.push({
869             x : maxX - this.unitWidth * 3 - this.gutter * 2,
870             y : minY
871         });
872         
873         Roo.each(eItems, function(b,k){
874             
875             b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
876
877         }, this);
878         
879     },
880     
881     getVerticalOneBoxColPositions : function(x, y, box)
882     {
883         var pos = [];
884         
885         var rand = Math.floor(Math.random() * ((4 - box[0].x)));
886         
887         if(box[0].size == 'md-left'){
888             rand = 0;
889         }
890         
891         if(box[0].size == 'md-right'){
892             rand = 1;
893         }
894         
895         pos.push({
896             x : x + (this.unitWidth + this.gutter) * rand,
897             y : y
898         });
899         
900         return pos;
901     },
902     
903     getVerticalTwoBoxColPositions : function(x, y, box)
904     {
905         var pos = [];
906         
907         if(box[0].size == 'xs'){
908             
909             pos.push({
910                 x : x,
911                 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[1].y))
912             });
913
914             pos.push({
915                 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
916                 y : y
917             });
918             
919             return pos;
920             
921         }
922         
923         pos.push({
924             x : x,
925             y : y
926         });
927
928         pos.push({
929             x : x + (this.unitWidth + this.gutter) * 2,
930             y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[0].y))
931         });
932         
933         return pos;
934         
935     },
936     
937     getVerticalThreeBoxColPositions : function(x, y, box)
938     {
939         var pos = [];
940         
941         if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
942             
943             pos.push({
944                 x : x,
945                 y : y
946             });
947
948             pos.push({
949                 x : x + (this.unitWidth + this.gutter) * 1,
950                 y : y
951             });
952             
953             pos.push({
954                 x : x + (this.unitWidth + this.gutter) * 2,
955                 y : y
956             });
957             
958             return pos;
959             
960         }
961         
962         if(box[0].size == 'xs' && box[1].size == 'xs'){
963             
964             pos.push({
965                 x : x,
966                 y : y
967             });
968
969             pos.push({
970                 x : x,
971                 y : y + ((this.unitHeight + this.gutter) * (box[2].y - 1))
972             });
973             
974             pos.push({
975                 x : x + (this.unitWidth + this.gutter) * 1,
976                 y : y
977             });
978             
979             return pos;
980             
981         }
982         
983         pos.push({
984             x : x,
985             y : y
986         });
987
988         pos.push({
989             x : x + (this.unitWidth + this.gutter) * 2,
990             y : y
991         });
992
993         pos.push({
994             x : x + (this.unitWidth + this.gutter) * 2,
995             y : y + (this.unitHeight + this.gutter) * (box[0].y - 1)
996         });
997             
998         return pos;
999         
1000     },
1001     
1002     getVerticalFourBoxColPositions : function(x, y, box)
1003     {
1004         var pos = [];
1005         
1006         if(box[0].size == 'xs'){
1007             
1008             pos.push({
1009                 x : x,
1010                 y : y
1011             });
1012
1013             pos.push({
1014                 x : x,
1015                 y : y + (this.unitHeight + this.gutter) * 1
1016             });
1017             
1018             pos.push({
1019                 x : x,
1020                 y : y + (this.unitHeight + this.gutter) * 2
1021             });
1022             
1023             pos.push({
1024                 x : x + (this.unitWidth + this.gutter) * 1,
1025                 y : y
1026             });
1027             
1028             return pos;
1029             
1030         }
1031         
1032         pos.push({
1033             x : x,
1034             y : y
1035         });
1036
1037         pos.push({
1038             x : x + (this.unitWidth + this.gutter) * 2,
1039             y : y
1040         });
1041
1042         pos.push({
1043             x : x + (this.unitHeightunitWidth + this.gutter) * 2,
1044             y : y + (this.unitHeight + this.gutter) * 1
1045         });
1046
1047         pos.push({
1048             x : x + (this.unitWidth + this.gutter) * 2,
1049             y : y + (this.unitWidth + this.gutter) * 2
1050         });
1051
1052         return pos;
1053         
1054     },
1055     
1056     getHorizontalOneBoxColPositions : function(maxX, minY, box)
1057     {
1058         var pos = [];
1059         
1060         if(box[0].size == 'md-left'){
1061             pos.push({
1062                 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1063                 y : minY
1064             });
1065             
1066             return pos;
1067         }
1068         
1069         if(box[0].size == 'md-right'){
1070             pos.push({
1071                 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1072                 y : minY + (this.unitWidth + this.gutter) * 1
1073             });
1074             
1075             return pos;
1076         }
1077         
1078         var rand = Math.floor(Math.random() * (4 - box[0].y));
1079         
1080         pos.push({
1081             x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1082             y : minY + (this.unitWidth + this.gutter) * rand
1083         });
1084         
1085         return pos;
1086         
1087     },
1088     
1089     getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1090     {
1091         var pos = [];
1092         
1093         if(box[0].size == 'xs'){
1094             
1095             pos.push({
1096                 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1097                 y : minY
1098             });
1099
1100             pos.push({
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)
1103             });
1104             
1105             return pos;
1106             
1107         }
1108         
1109         pos.push({
1110             x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1111             y : minY
1112         });
1113
1114         pos.push({
1115             x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1116             y : minY + (this.unitWidth + this.gutter) * 2
1117         });
1118         
1119         return pos;
1120         
1121     },
1122     
1123     getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1124     {
1125         var pos = [];
1126         
1127         if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
1128             
1129             pos.push({
1130                 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1131                 y : minY
1132             });
1133
1134             pos.push({
1135                 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1136                 y : minY + (this.unitWidth + this.gutter) * 1
1137             });
1138             
1139             pos.push({
1140                 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1141                 y : minY + (this.unitWidth + this.gutter) * 2
1142             });
1143             
1144             return pos;
1145             
1146         }
1147         
1148         if(box[0].size == 'xs' && box[1].size == 'xs'){
1149             
1150             pos.push({
1151                 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1152                 y : minY
1153             });
1154
1155             pos.push({
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),
1157                 y : minY
1158             });
1159             
1160             pos.push({
1161                 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1162                 y : minY + (this.unitWidth + this.gutter) * 1
1163             });
1164             
1165             return pos;
1166             
1167         }
1168         
1169         pos.push({
1170             x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1171             y : minY
1172         });
1173
1174         pos.push({
1175             x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1176             y : minY + (this.unitWidth + this.gutter) * 2
1177         });
1178
1179         pos.push({
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
1182         });
1183             
1184         return pos;
1185         
1186     },
1187     
1188     getHorizontalFourBoxColPositions : function(maxX, minY, box)
1189     {
1190         var pos = [];
1191         
1192         if(box[0].size == 'xs'){
1193             
1194             pos.push({
1195                 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1196                 y : minY
1197             });
1198
1199             pos.push({
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),
1201                 y : minY
1202             });
1203             
1204             pos.push({
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),
1206                 y : minY
1207             });
1208             
1209             pos.push({
1210                 x : maxX - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1211                 y : minY + (this.unitWidth + this.gutter) * 1
1212             });
1213             
1214             return pos;
1215             
1216         }
1217         
1218         pos.push({
1219             x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1220             y : minY
1221         });
1222         
1223         pos.push({
1224             x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1225             y : minY + (this.unitWidth + this.gutter) * 2
1226         });
1227         
1228         pos.push({
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
1231         });
1232         
1233         pos.push({
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
1236         });
1237
1238         return pos;
1239         
1240     },
1241     
1242     /**
1243     * remove a Masonry Brick
1244     * @param {Roo.bootstrap.MasonryBrick} the masonry brick to remove
1245     */
1246     removeBrick : function(brick_id)
1247     {
1248         if (!brick_id) {
1249             return;
1250         }
1251         
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);
1256                 this.initial();
1257             }
1258         }
1259     },
1260     
1261     /**
1262     * adds a Masonry Brick
1263     * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1264     */
1265     addBrick : function(cfg)
1266     {
1267         var cn = new Roo.bootstrap.MasonryBrick(cfg);
1268         //this.register(cn);
1269         cn.parentId = this.id;
1270         cn.render(this.el);
1271         return cn;
1272     },
1273     
1274     /**
1275     * register a Masonry Brick
1276     * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1277     */
1278     
1279     register : function(brick)
1280     {
1281         this.bricks.push(brick);
1282         brick.masonryId = this.id;
1283     },
1284     
1285     /**
1286     * clear all the Masonry Brick
1287     */
1288     clearAll : function()
1289     {
1290         this.bricks = [];
1291         //this.getChildContainer().dom.innerHTML = "";
1292         this.el.dom.innerHTML = '';
1293     },
1294     
1295     getSelected : function()
1296     {
1297         if (!this.selectedBrick) {
1298             return false;
1299         }
1300         
1301         return this.selectedBrick;
1302     }
1303 });
1304
1305 Roo.apply(Roo.bootstrap.LayoutMasonry, {
1306     
1307     groups: {},
1308      /**
1309     * register a Masonry Layout
1310     * @param {Roo.bootstrap.LayoutMasonry} the masonry layout to add
1311     */
1312     
1313     register : function(layout)
1314     {
1315         this.groups[layout.id] = layout;
1316     },
1317     /**
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
1321     */
1322     
1323     get: function(layout_id) {
1324         if (typeof(this.groups[layout_id]) == 'undefined') {
1325             return false;
1326         }
1327         return this.groups[layout_id] ;
1328     }
1329     
1330     
1331     
1332 });
1333
1334  
1335
1336