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         
246         this.unitHeight = this.boxHeight > 0 ? this.boxHeight  : this.unitWidth;
247     },
248     
249     horizontalMeasureColumns : function()
250     {
251         this.getContainerWidth();
252         
253         var boxWidth = this.boxWidth;
254         
255         if(this.containerWidth < boxWidth){
256             boxWidth = this.containerWidth;
257         }
258         
259         this.unitWidth = Math.floor((boxWidth - (this.gutter * 2)) / 3);
260         
261         this.el.setHeight(boxWidth);
262         
263     },
264     
265     getContainerWidth : function()
266     {
267         this.containerWidth = this.el.getBox(true).width;  //maybe use getComputedWidth
268     },
269     
270     layoutItems : function( isInstant )
271     {
272         Roo.log(this.bricks);
273         
274         var items = Roo.apply([], this.bricks);
275         
276         if(this.isHorizontal){
277             this._horizontalLayoutItems( items , isInstant );
278             return;
279         }
280         
281 //        if(Roo.lib.Dom.getViewWidth() < 768 && this.isAlternative){
282 //            this._verticalAlternativeLayoutItems( items , isInstant );
283 //            return;
284 //        }
285         
286         this._verticalLayoutItems( items , isInstant );
287         
288     },
289     
290     _verticalLayoutItems : function ( items , isInstant)
291     {
292         if ( !items || !items.length ) {
293             return;
294         }
295         
296         var standard = [
297             ['xs', 'xs', 'xs', 'tall'],
298             ['xs', 'xs', 'tall'],
299             ['xs', 'xs', 'sm'],
300             ['xs', 'xs', 'xs'],
301             ['xs', 'tall'],
302             ['xs', 'sm'],
303             ['xs', 'xs'],
304             ['xs'],
305             
306             ['sm', 'xs', 'xs'],
307             ['sm', 'xs'],
308             ['sm'],
309             
310             ['tall', 'xs', 'xs', 'xs'],
311             ['tall', 'xs', 'xs'],
312             ['tall', 'xs'],
313             ['tall']
314             
315         ];
316         
317         var queue = [];
318         
319         var boxes = [];
320         
321         var box = [];
322         
323         Roo.each(items, function(item, k){
324             
325             switch (item.size) {
326                 // these layouts take up a full box,
327                 case 'md' :
328                 case 'md-left' :
329                 case 'md-right' :
330                 case 'wide' :
331                     
332                     if(box.length){
333                         boxes.push(box);
334                         box = [];
335                     }
336                     
337                     boxes.push([item]);
338                     
339                     break;
340                     
341                 case 'xs' :
342                 case 'sm' :
343                 case 'tall' :
344                     
345                     box.push(item);
346                     
347                     break;
348                 default :
349                     break;
350                     
351             }
352             
353         }, this);
354         
355         if(box.length){
356             boxes.push(box);
357             box = [];
358         }
359         
360         var filterPattern = function(box, length)
361         {
362             if(!box.length){
363                 return;
364             }
365             
366             var match = false;
367             
368             var pattern = box.slice(0, length);
369             
370             var format = [];
371             
372             Roo.each(pattern, function(i){
373                 format.push(i.size);
374             }, this);
375             
376             Roo.each(standard, function(s){
377                 
378                 if(String(s) != String(format)){
379                     return;
380                 }
381                 
382                 match = true;
383                 return false;
384                 
385             }, this);
386             
387             if(!match && length == 1){
388                 return;
389             }
390             
391             if(!match){
392                 filterPattern(box, length - 1);
393                 return;
394             }
395                 
396             queue.push(pattern);
397
398             box = box.slice(length, box.length);
399
400             filterPattern(box, 4);
401
402             return;
403             
404         }
405         
406         Roo.each(boxes, function(box, k){
407             
408             if(!box.length){
409                 return;
410             }
411             
412             if(box.length == 1){
413                 queue.push(box);
414                 return;
415             }
416             
417             filterPattern(box, 4);
418             
419         }, this);
420         
421         this._processVerticalLayoutQueue( queue, isInstant );
422         
423     },
424     
425 //    _verticalAlternativeLayoutItems : function( items , isInstant )
426 //    {
427 //        if ( !items || !items.length ) {
428 //            return;
429 //        }
430 //
431 //        this._processVerticalAlternativeLayoutQueue( items, isInstant );
432 //        
433 //    },
434     
435     _horizontalLayoutItems : function ( items , isInstant)
436     {
437         if ( !items || !items.length || items.length < 3) {
438             return;
439         }
440         
441         items.reverse();
442         
443         var eItems = items.slice(0, 3);
444         
445         items = items.slice(3, items.length);
446         
447         var standard = [
448             ['xs', 'xs', 'xs', 'wide'],
449             ['xs', 'xs', 'wide'],
450             ['xs', 'xs', 'sm'],
451             ['xs', 'xs', 'xs'],
452             ['xs', 'wide'],
453             ['xs', 'sm'],
454             ['xs', 'xs'],
455             ['xs'],
456             
457             ['sm', 'xs', 'xs'],
458             ['sm', 'xs'],
459             ['sm'],
460             
461             ['wide', 'xs', 'xs', 'xs'],
462             ['wide', 'xs', 'xs'],
463             ['wide', 'xs'],
464             ['wide'],
465             
466             ['wide-thin']
467         ];
468         
469         var queue = [];
470         
471         var boxes = [];
472         
473         var box = [];
474         
475         Roo.each(items, function(item, k){
476             
477             switch (item.size) {
478                 case 'md' :
479                 case 'md-left' :
480                 case 'md-right' :
481                 case 'tall' :
482                     
483                     if(box.length){
484                         boxes.push(box);
485                         box = [];
486                     }
487                     
488                     boxes.push([item]);
489                     
490                     break;
491                     
492                 case 'xs' :
493                 case 'sm' :
494                 case 'wide' :
495                 case 'wide-thin' :
496                     
497                     box.push(item);
498                     
499                     break;
500                 default :
501                     break;
502                     
503             }
504             
505         }, this);
506         
507         if(box.length){
508             boxes.push(box);
509             box = [];
510         }
511         
512         var filterPattern = function(box, length)
513         {
514             if(!box.length){
515                 return;
516             }
517             
518             var match = false;
519             
520             var pattern = box.slice(0, length);
521             
522             var format = [];
523             
524             Roo.each(pattern, function(i){
525                 format.push(i.size);
526             }, this);
527             
528             Roo.each(standard, function(s){
529                 
530                 if(String(s) != String(format)){
531                     return;
532                 }
533                 
534                 match = true;
535                 return false;
536                 
537             }, this);
538             
539             if(!match && length == 1){
540                 return;
541             }
542             
543             if(!match){
544                 filterPattern(box, length - 1);
545                 return;
546             }
547                 
548             queue.push(pattern);
549
550             box = box.slice(length, box.length);
551
552             filterPattern(box, 4);
553
554             return;
555             
556         }
557         
558         Roo.each(boxes, function(box, k){
559             
560             if(!box.length){
561                 return;
562             }
563             
564             if(box.length == 1){
565                 queue.push(box);
566                 return;
567             }
568             
569             filterPattern(box, 4);
570             
571         }, this);
572         
573         
574         var prune = [];
575         
576         var pos = this.el.getBox(true);
577         
578         var minX = pos.x;
579         
580         var maxX = pos.right - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
581         
582         var hit_end = false;
583         
584         Roo.each(queue, function(box){
585             
586             if(hit_end){
587                 
588                 Roo.each(box, function(b){
589                 
590                     b.el.setVisibilityMode(Roo.Element.DISPLAY);
591                     b.el.hide();
592
593                 }, this);
594
595                 return;
596             }
597             
598             var mx = 0;
599             
600             Roo.each(box, function(b){
601                 
602                 b.el.setVisibilityMode(Roo.Element.DISPLAY);
603                 b.el.show();
604
605                 mx = Math.max(mx, b.x);
606                 
607             }, this);
608             
609             maxX = maxX - this.unitWidth * mx - this.gutter * (mx - 1) - this.padWidth;
610             
611             if(maxX < minX){
612                 
613                 Roo.each(box, function(b){
614                 
615                     b.el.setVisibilityMode(Roo.Element.DISPLAY);
616                     b.el.hide();
617                     
618                 }, this);
619                 
620                 hit_end = true;
621                 
622                 return;
623             }
624             
625             prune.push(box);
626             
627         }, this);
628         
629         this._processHorizontalLayoutQueue( prune, eItems, isInstant );
630     },
631     
632     /** Sets position of item in DOM
633     * @param {Element} item
634     * @param {Number} x - horizontal position
635     * @param {Number} y - vertical position
636     * @param {Boolean} isInstant - disables transitions
637     */
638     _processVerticalLayoutQueue : function( queue, isInstant )
639     {
640         var pos = this.el.getBox(true);
641         var x = pos.x;
642         var y = pos.y;
643         Roo.log(pos);
644         alert(JSON.stringify(pos));
645         var maxY = [];
646         
647         for (var i = 0; i < this.cols; i++){
648             maxY[i] = pos.y;
649         }
650         
651         Roo.each(queue, function(box, k){
652             
653             var col = k % this.cols;
654             
655             Roo.each(box, function(b,kk){
656                 
657                 b.el.position('absolute');
658                 
659                 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
660                 var height = Math.floor(this.unitHeight * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
661                 
662                 if(b.size == 'md-left' || b.size == 'md-right'){
663                     width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
664                     height = Math.floor(this.unitHeight * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
665                 }
666                 
667                 b.el.setWidth(width);
668                 b.el.setHeight(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