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                 if(b.videourl && b.videourl.length) {
655                     
656                 }
657                 
658                 b.el.position('absolute');
659                 
660                 var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
661                 var height = Math.floor(this.unitHeight * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
662                 
663                 if(b.size == 'md-left' || b.size == 'md-right'){
664                     width = Math.floor(this.unitWidth * (b.x - 1) + (this.gutter * (b.x - 2)) + b.el.getPadding('lr'));
665                     height = Math.floor(this.unitHeight * (b.y - 1) + (this.gutter * (b.y - 2)) + b.el.getPadding('tb'));
666                 }
667                 
668                 b.el.setWidth(width);
669                 b.el.setHeight(height);
670                 // iframe?
671                 b.el.select('iframe',true).setSize(width,height);
672                 
673             }, this);
674             
675             for (var i = 0; i < this.cols; i++){
676                 
677                 if(maxY[i] < maxY[col]){
678                     col = i;
679                     continue;
680                 }
681                 
682                 col = Math.min(col, i);
683                 
684             }
685             
686             x = pos.x + col * (this.colWidth + this.padWidth);
687             
688             y = maxY[col];
689             
690             var positions = [];
691             
692             switch (box.length){
693                 case 1 :
694                     positions = this.getVerticalOneBoxColPositions(x, y, box);
695                     break;
696                 case 2 :
697                     positions = this.getVerticalTwoBoxColPositions(x, y, box);
698                     break;
699                 case 3 :
700                     positions = this.getVerticalThreeBoxColPositions(x, y, box);
701                     break;
702                 case 4 :
703                     positions = this.getVerticalFourBoxColPositions(x, y, box);
704                     break;
705                 default :
706                     break;
707             }
708             
709             Roo.each(box, function(b,kk){
710                 
711                 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
712                 
713                 var sz = b.el.getSize();
714                 
715                 maxY[col] = Math.max(maxY[col], positions[kk].y + sz.height + this.padWidth);
716                 
717             }, this);
718             
719         }, this);
720         
721         var mY = 0;
722         
723         for (var i = 0; i < this.cols; i++){
724             mY = Math.max(mY, maxY[i]);
725         }
726         
727         this.el.setHeight(mY - pos.y);
728         
729     },
730     
731 //    _processVerticalAlternativeLayoutQueue : function( items, isInstant )
732 //    {
733 //        var pos = this.el.getBox(true);
734 //        var x = pos.x;
735 //        var y = pos.y;
736 //        var maxX = pos.right;
737 //        
738 //        var maxHeight = 0;
739 //        
740 //        Roo.each(items, function(item, k){
741 //            
742 //            var c = k % 2;
743 //            
744 //            item.el.position('absolute');
745 //                
746 //            var width = Math.floor(this.colWidth + item.el.getPadding('lr'));
747 //
748 //            item.el.setWidth(width);
749 //
750 //            var height = Math.floor(this.colWidth * item.y / item.x + item.el.getPadding('tb'));
751 //
752 //            item.el.setHeight(height);
753 //            
754 //            if(c == 0){
755 //                item.el.setXY([x, y], isInstant ? false : true);
756 //            } else {
757 //                item.el.setXY([maxX - width, y], isInstant ? false : true);
758 //            }
759 //            
760 //            y = y + height + this.alternativePadWidth;
761 //            
762 //            maxHeight = maxHeight + height + this.alternativePadWidth;
763 //            
764 //        }, this);
765 //        
766 //        this.el.setHeight(maxHeight);
767 //        
768 //    },
769     
770     _processHorizontalLayoutQueue : function( queue, eItems, isInstant )
771     {
772         var pos = this.el.getBox(true);
773         
774         var minX = pos.x;
775         var minY = pos.y;
776         
777         var maxX = pos.right;
778         
779         this._processHorizontalEndItem(eItems, maxX, minX, minY, isInstant);
780         
781         var maxX = maxX - this.unitWidth * 3 - this.gutter * 2 - this.padWidth;
782         
783         Roo.each(queue, function(box, k){
784             
785             Roo.each(box, function(b, kk){
786                 
787                 b.el.position('absolute');
788                 
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'));
791                 
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'));
795                 }
796                 
797                 b.el.setWidth(width);
798                 b.el.setHeight(height);
799                 
800             }, this);
801             
802             if(!box.length){
803                 return;
804             }
805             
806             var positions = [];
807             
808             switch (box.length){
809                 case 1 :
810                     positions = this.getHorizontalOneBoxColPositions(maxX, minY, box);
811                     break;
812                 case 2 :
813                     positions = this.getHorizontalTwoBoxColPositions(maxX, minY, box);
814                     break;
815                 case 3 :
816                     positions = this.getHorizontalThreeBoxColPositions(maxX, minY, box);
817                     break;
818                 case 4 :
819                     positions = this.getHorizontalFourBoxColPositions(maxX, minY, box);
820                     break;
821                 default :
822                     break;
823             }
824             
825             Roo.each(box, function(b,kk){
826                 
827                 b.el.setXY([positions[kk].x, positions[kk].y], isInstant ? false : true);
828                 
829                 maxX = Math.min(maxX, positions[kk].x - this.padWidth);
830                 
831             }, this);
832             
833         }, this);
834         
835     },
836     
837     _processHorizontalEndItem : function(eItems, maxX, minX, minY, isInstant)
838     {
839         Roo.each(eItems, function(b,k){
840             
841             b.size = (k == 0) ? 'sm' : 'xs';
842             b.x = (k == 0) ? 2 : 1;
843             b.y = (k == 0) ? 2 : 1;
844             
845             b.el.position('absolute');
846             
847             var width = Math.floor(this.unitWidth * b.x + (this.gutter * (b.x - 1)) + b.el.getPadding('lr'));
848                 
849             b.el.setWidth(width);
850             
851             var height = Math.floor(this.unitWidth * b.y + (this.gutter * (b.y - 1)) + b.el.getPadding('tb'));
852             
853             b.el.setHeight(height);
854             
855         }, this);
856
857         var positions = [];
858         
859         positions.push({
860             x : maxX - this.unitWidth * 2 - this.gutter,
861             y : minY
862         });
863         
864         positions.push({
865             x : maxX - this.unitWidth,
866             y : minY + (this.unitWidth + this.gutter) * 2
867         });
868         
869         positions.push({
870             x : maxX - this.unitWidth * 3 - this.gutter * 2,
871             y : minY
872         });
873         
874         Roo.each(eItems, function(b,k){
875             
876             b.el.setXY([positions[k].x, positions[k].y], isInstant ? false : true);
877
878         }, this);
879         
880     },
881     
882     getVerticalOneBoxColPositions : function(x, y, box)
883     {
884         var pos = [];
885         
886         var rand = Math.floor(Math.random() * ((4 - box[0].x)));
887         
888         if(box[0].size == 'md-left'){
889             rand = 0;
890         }
891         
892         if(box[0].size == 'md-right'){
893             rand = 1;
894         }
895         
896         pos.push({
897             x : x + (this.unitWidth + this.gutter) * rand,
898             y : y
899         });
900         
901         return pos;
902     },
903     
904     getVerticalTwoBoxColPositions : function(x, y, box)
905     {
906         var pos = [];
907         
908         if(box[0].size == 'xs'){
909             
910             pos.push({
911                 x : x,
912                 y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[1].y))
913             });
914
915             pos.push({
916                 x : x + (this.unitWidth + this.gutter) * (3 - box[1].x),
917                 y : y
918             });
919             
920             return pos;
921             
922         }
923         
924         pos.push({
925             x : x,
926             y : y
927         });
928
929         pos.push({
930             x : x + (this.unitWidth + this.gutter) * 2,
931             y : y + ((this.unitHeight + this.gutter) * Math.floor(Math.random() * box[0].y))
932         });
933         
934         return pos;
935         
936     },
937     
938     getVerticalThreeBoxColPositions : function(x, y, box)
939     {
940         var pos = [];
941         
942         if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
943             
944             pos.push({
945                 x : x,
946                 y : y
947             });
948
949             pos.push({
950                 x : x + (this.unitWidth + this.gutter) * 1,
951                 y : y
952             });
953             
954             pos.push({
955                 x : x + (this.unitWidth + this.gutter) * 2,
956                 y : y
957             });
958             
959             return pos;
960             
961         }
962         
963         if(box[0].size == 'xs' && box[1].size == 'xs'){
964             
965             pos.push({
966                 x : x,
967                 y : y
968             });
969
970             pos.push({
971                 x : x,
972                 y : y + ((this.unitHeight + this.gutter) * (box[2].y - 1))
973             });
974             
975             pos.push({
976                 x : x + (this.unitWidth + this.gutter) * 1,
977                 y : y
978             });
979             
980             return pos;
981             
982         }
983         
984         pos.push({
985             x : x,
986             y : y
987         });
988
989         pos.push({
990             x : x + (this.unitWidth + this.gutter) * 2,
991             y : y
992         });
993
994         pos.push({
995             x : x + (this.unitWidth + this.gutter) * 2,
996             y : y + (this.unitHeight + this.gutter) * (box[0].y - 1)
997         });
998             
999         return pos;
1000         
1001     },
1002     
1003     getVerticalFourBoxColPositions : function(x, y, box)
1004     {
1005         var pos = [];
1006         
1007         if(box[0].size == 'xs'){
1008             
1009             pos.push({
1010                 x : x,
1011                 y : y
1012             });
1013
1014             pos.push({
1015                 x : x,
1016                 y : y + (this.unitHeight + this.gutter) * 1
1017             });
1018             
1019             pos.push({
1020                 x : x,
1021                 y : y + (this.unitHeight + this.gutter) * 2
1022             });
1023             
1024             pos.push({
1025                 x : x + (this.unitWidth + this.gutter) * 1,
1026                 y : y
1027             });
1028             
1029             return pos;
1030             
1031         }
1032         
1033         pos.push({
1034             x : x,
1035             y : y
1036         });
1037
1038         pos.push({
1039             x : x + (this.unitWidth + this.gutter) * 2,
1040             y : y
1041         });
1042
1043         pos.push({
1044             x : x + (this.unitHeightunitWidth + this.gutter) * 2,
1045             y : y + (this.unitHeight + this.gutter) * 1
1046         });
1047
1048         pos.push({
1049             x : x + (this.unitWidth + this.gutter) * 2,
1050             y : y + (this.unitWidth + this.gutter) * 2
1051         });
1052
1053         return pos;
1054         
1055     },
1056     
1057     getHorizontalOneBoxColPositions : function(maxX, minY, box)
1058     {
1059         var pos = [];
1060         
1061         if(box[0].size == 'md-left'){
1062             pos.push({
1063                 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1064                 y : minY
1065             });
1066             
1067             return pos;
1068         }
1069         
1070         if(box[0].size == 'md-right'){
1071             pos.push({
1072                 x : maxX - this.unitWidth * (box[0].x - 1) - this.gutter * (box[0].x - 2),
1073                 y : minY + (this.unitWidth + this.gutter) * 1
1074             });
1075             
1076             return pos;
1077         }
1078         
1079         var rand = Math.floor(Math.random() * (4 - box[0].y));
1080         
1081         pos.push({
1082             x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1083             y : minY + (this.unitWidth + this.gutter) * rand
1084         });
1085         
1086         return pos;
1087         
1088     },
1089     
1090     getHorizontalTwoBoxColPositions : function(maxX, minY, box)
1091     {
1092         var pos = [];
1093         
1094         if(box[0].size == 'xs'){
1095             
1096             pos.push({
1097                 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1098                 y : minY
1099             });
1100
1101             pos.push({
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)
1104             });
1105             
1106             return pos;
1107             
1108         }
1109         
1110         pos.push({
1111             x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1112             y : minY
1113         });
1114
1115         pos.push({
1116             x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1117             y : minY + (this.unitWidth + this.gutter) * 2
1118         });
1119         
1120         return pos;
1121         
1122     },
1123     
1124     getHorizontalThreeBoxColPositions : function(maxX, minY, box)
1125     {
1126         var pos = [];
1127         
1128         if(box[0].size == 'xs' && box[1].size == 'xs' && box[2].size == 'xs'){
1129             
1130             pos.push({
1131                 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1132                 y : minY
1133             });
1134
1135             pos.push({
1136                 x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1137                 y : minY + (this.unitWidth + this.gutter) * 1
1138             });
1139             
1140             pos.push({
1141                 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1142                 y : minY + (this.unitWidth + this.gutter) * 2
1143             });
1144             
1145             return pos;
1146             
1147         }
1148         
1149         if(box[0].size == 'xs' && box[1].size == 'xs'){
1150             
1151             pos.push({
1152                 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1153                 y : minY
1154             });
1155
1156             pos.push({
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),
1158                 y : minY
1159             });
1160             
1161             pos.push({
1162                 x : maxX - this.unitWidth * box[2].x - this.gutter * (box[2].x - 1),
1163                 y : minY + (this.unitWidth + this.gutter) * 1
1164             });
1165             
1166             return pos;
1167             
1168         }
1169         
1170         pos.push({
1171             x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1172             y : minY
1173         });
1174
1175         pos.push({
1176             x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1177             y : minY + (this.unitWidth + this.gutter) * 2
1178         });
1179
1180         pos.push({
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
1183         });
1184             
1185         return pos;
1186         
1187     },
1188     
1189     getHorizontalFourBoxColPositions : function(maxX, minY, box)
1190     {
1191         var pos = [];
1192         
1193         if(box[0].size == 'xs'){
1194             
1195             pos.push({
1196                 x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1197                 y : minY
1198             });
1199
1200             pos.push({
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),
1202                 y : minY
1203             });
1204             
1205             pos.push({
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),
1207                 y : minY
1208             });
1209             
1210             pos.push({
1211                 x : maxX - this.unitWidth * box[3].x - this.gutter * (box[3].x - 1),
1212                 y : minY + (this.unitWidth + this.gutter) * 1
1213             });
1214             
1215             return pos;
1216             
1217         }
1218         
1219         pos.push({
1220             x : maxX - this.unitWidth * box[0].x - this.gutter * (box[0].x - 1),
1221             y : minY
1222         });
1223         
1224         pos.push({
1225             x : maxX - this.unitWidth * box[1].x - this.gutter * (box[1].x - 1),
1226             y : minY + (this.unitWidth + this.gutter) * 2
1227         });
1228         
1229         pos.push({
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
1232         });
1233         
1234         pos.push({
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
1237         });
1238
1239         return pos;
1240         
1241     },
1242     
1243     /**
1244     * remove a Masonry Brick
1245     * @param {Roo.bootstrap.MasonryBrick} the masonry brick to remove
1246     */
1247     removeBrick : function(brick_id)
1248     {
1249         if (!brick_id) {
1250             return;
1251         }
1252         
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);
1257                 this.initial();
1258             }
1259         }
1260     },
1261     
1262     /**
1263     * adds a Masonry Brick
1264     * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1265     */
1266     addBrick : function(cfg)
1267     {
1268         var cn = new Roo.bootstrap.MasonryBrick(cfg);
1269         //this.register(cn);
1270         cn.parentId = this.id;
1271         cn.render(this.el);
1272         return cn;
1273     },
1274     
1275     /**
1276     * register a Masonry Brick
1277     * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
1278     */
1279     
1280     register : function(brick)
1281     {
1282         this.bricks.push(brick);
1283         brick.masonryId = this.id;
1284     },
1285     
1286     /**
1287     * clear all the Masonry Brick
1288     */
1289     clearAll : function()
1290     {
1291         this.bricks = [];
1292         //this.getChildContainer().dom.innerHTML = "";
1293         this.el.dom.innerHTML = '';
1294     },
1295     
1296     getSelected : function()
1297     {
1298         if (!this.selectedBrick) {
1299             return false;
1300         }
1301         
1302         return this.selectedBrick;
1303     }
1304 });
1305
1306 Roo.apply(Roo.bootstrap.LayoutMasonry, {
1307     
1308     groups: {},
1309      /**
1310     * register a Masonry Layout
1311     * @param {Roo.bootstrap.LayoutMasonry} the masonry layout to add
1312     */
1313     
1314     register : function(layout)
1315     {
1316         this.groups[layout.id] = layout;
1317     },
1318     /**
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
1322     */
1323     
1324     get: function(layout_id) {
1325         if (typeof(this.groups[layout_id]) == 'undefined') {
1326             return false;
1327         }
1328         return this.groups[layout_id] ;
1329     }
1330     
1331     
1332     
1333 });
1334
1335  
1336
1337