Roo/bootstrap/MasonryBrick.js
[roojs1] / Roo / bootstrap / MasonryBrick.js
1 /*
2  * - LGPL
3  *
4  * element
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.MasonryBrick
10  * @extends Roo.bootstrap.Component
11  * Bootstrap MasonryBrick class
12  * 
13  * @constructor
14  * Create a new MasonryBrick
15  * @param {Object} config The config object
16  */
17
18 Roo.bootstrap.MasonryBrick = function(config){
19     
20     Roo.bootstrap.MasonryBrick.superclass.constructor.call(this, config);
21     
22     Roo.bootstrap.MasonryBrick.register(this);
23     
24     this.addEvents({
25         // raw events
26         /**
27          * @event click
28          * When a MasonryBrick is clcik
29          * @param {Roo.bootstrap.MasonryBrick} this
30          * @param {Roo.EventObject} e
31          */
32         "click" : true
33     });
34 };
35
36 Roo.extend(Roo.bootstrap.MasonryBrick, Roo.bootstrap.Component,  {
37     
38     /**
39      * @cfg {String} title
40      */   
41     title : '',
42     /**
43      * @cfg {String} html
44      */   
45     html : '',
46     /**
47      * @cfg {String} bgimage
48      */   
49     bgimage : '',
50     /**
51      * @cfg {String} videourl
52      */   
53     videourl : '',
54     /**
55      * @cfg {String} cls
56      */   
57     cls : '',
58     /**
59      * @cfg {String} href
60      */   
61     href : '',
62     /**
63      * @cfg {String} size (xs|sm|md|md-left|md-right|tall|wide)
64      */   
65     size : 'xs',
66     
67     /**
68      * @cfg {String} placetitle (center|bottom)
69      */   
70     placetitle : '',
71     
72     /**
73      * @cfg {Boolean} isFitContainer defalut true
74      */   
75     isFitContainer : true, 
76     
77     /**
78      * @cfg {Boolean} preventDefault defalut false
79      */   
80     preventDefault : false, 
81     
82     /**
83      * @cfg {Boolean} inverse defalut false
84      */   
85     maskInverse : false, 
86     
87     getAutoCreate : function()
88     {
89         if(!this.isFitContainer){
90             return this.getSplitAutoCreate();
91         }
92         
93         var cls = 'masonry-brick masonry-brick-full';
94         
95         if(this.href.length){
96             cls += ' masonry-brick-link';
97         }
98         
99         if(this.bgimage.length){
100             cls += ' masonry-brick-image';
101         }
102         
103         if(this.maskInverse){
104             cls += ' mask-inverse';
105         }
106         
107         if(!this.html.length && !this.maskInverse && !this.videourl.length){
108             cls += ' enable-mask';
109         }
110         
111         if(this.size){
112             cls += ' masonry-' + this.size + '-brick';
113         }
114         
115         if(this.placetitle.length){
116             
117             switch (this.placetitle) {
118                 case 'center' :
119                     cls += ' masonry-center-title';
120                     break;
121                 case 'bottom' :
122                     cls += ' masonry-bottom-title';
123                     break;
124                 default:
125                     break;
126             }
127             
128         } else {
129             if(!this.html.length && !this.bgimage.length){
130                 cls += ' masonry-center-title';
131             }
132
133             if(!this.html.length && this.bgimage.length){
134                 cls += ' masonry-bottom-title';
135             }
136         }
137         
138         if(this.cls){
139             cls += ' ' + this.cls;
140         }
141         
142         var cfg = {
143             tag: (this.href.length) ? 'a' : 'div',
144             cls: cls,
145             cn: [
146                 {
147                     tag: 'div',
148                     cls: 'masonry-brick-mask'
149                 },
150                 {
151                     tag: 'div',
152                     cls: 'masonry-brick-paragraph',
153                     cn: []
154                 }
155             ]
156         };
157         
158         if(this.href.length){
159             cfg.href = this.href;
160         }
161         
162         var cn = cfg.cn[1].cn;
163         
164         if(this.title.length){
165             cn.push({
166                 tag: 'h4',
167                 cls: 'masonry-brick-title',
168                 html: this.title
169             });
170         }
171         
172         if(this.html.length){
173             cn.push({
174                 tag: 'p',
175                 cls: 'masonry-brick-text',
176                 html: this.html
177             });
178         }
179         
180         if (!this.title.length && !this.html.length) {
181             cfg.cn[1].cls += ' hide';
182         }
183         
184         if(this.bgimage.length){
185             cfg.cn.push({
186                 tag: 'img',
187                 cls: 'masonry-brick-image-view',
188                 src: this.bgimage
189             });
190         }
191         
192         if(this.videourl.length){
193             var vurl = this.videourl.replace(/https:\/\/youtu\.be/, 'https://www.youtube.com/embed/');
194             // youtube support only?
195             cfg.cn.push({
196                 tag: 'iframe',
197                 cls: 'masonry-brick-image-view',
198                 src: vurl,
199                 frameborder : 0,
200                 allowfullscreen : true
201             });
202         }
203         
204         return cfg;
205         
206     },
207     
208     getSplitAutoCreate : function()
209     {
210         var cls = 'masonry-brick masonry-brick-split';
211         
212         if(this.href.length){
213             cls += ' masonry-brick-link';
214         }
215         
216         if(this.bgimage.length){
217             cls += ' masonry-brick-image';
218         }
219         
220         if(this.size){
221             cls += ' masonry-' + this.size + '-brick';
222         }
223         
224         switch (this.placetitle) {
225             case 'center' :
226                 cls += ' masonry-center-title';
227                 break;
228             case 'bottom' :
229                 cls += ' masonry-bottom-title';
230                 break;
231             default:
232                 if(!this.bgimage.length){
233                     cls += ' masonry-center-title';
234                 }
235
236                 if(this.bgimage.length){
237                     cls += ' masonry-bottom-title';
238                 }
239                 break;
240         }
241         
242         if(this.cls){
243             cls += ' ' + this.cls;
244         }
245         
246         var cfg = {
247             tag: (this.href.length) ? 'a' : 'div',
248             cls: cls,
249             cn: [
250                 {
251                     tag: 'div',
252                     cls: 'masonry-brick-split-head',
253                     cn: [
254                         {
255                             tag: 'div',
256                             cls: 'masonry-brick-paragraph',
257                             cn: []
258                         }
259                     ]
260                 },
261                 {
262                     tag: 'div',
263                     cls: 'masonry-brick-split-body',
264                     cn: []
265                 }
266             ]
267         };
268         
269         if(this.href.length){
270             cfg.href = this.href;
271         }
272         
273         if(this.title.length){
274             cfg.cn[0].cn[0].cn.push({
275                 tag: 'h4',
276                 cls: 'masonry-brick-title',
277                 html: this.title
278             });
279         }
280         
281         if(this.html.length){
282             cfg.cn[1].cn.push({
283                 tag: 'p',
284                 cls: 'masonry-brick-text',
285                 html: this.html
286             });
287         }
288
289         if(this.bgimage.length){
290             cfg.cn[0].cn.push({
291                 tag: 'img',
292                 cls: 'masonry-brick-image-view',
293                 src: this.bgimage
294             });
295         }
296         
297         if(this.videourl.length){
298             var vurl = this.videourl.replace(/https:\/\/youtu\.be/, 'https://www.youtube.com/embed/');
299             // youtube support only?
300             cfg.cn[0].cn.cn.push({
301                 tag: 'iframe',
302                 cls: 'masonry-brick-image-view',
303                 src: vurl,
304                 frameborder : 5,
305                 allowfullscreen : true,
306                 style: "border: 5px dotted green;"
307             });
308         }
309         
310         return cfg;
311     },
312     
313     initEvents: function() 
314     {
315         switch (this.size) {
316             case 'xs' :
317                 this.x = 1;
318                 this.y = 1;
319                 break;
320             case 'sm' :
321                 this.x = 2;
322                 this.y = 2;
323                 break;
324             case 'md' :
325             case 'md-left' :
326             case 'md-right' :
327                 this.x = 3;
328                 this.y = 3;
329                 break;
330             case 'tall' :
331                 this.x = 2;
332                 this.y = 3;
333                 break;
334             case 'wide' :
335                 this.x = 3;
336                 this.y = 2;
337                 break;
338             case 'wide-thin' :
339                 this.x = 3;
340                 this.y = 1;
341                 break;
342                         
343             default :
344                 break;
345         }
346         
347         if(Roo.isTouch){
348             this.el.on('touchstart', this.onTouchStart, this);
349             this.el.on('touchmove', this.onTouchMove, this);
350             this.el.on('touchend', this.onTouchEnd, this);
351             this.el.on('contextmenu', this.onContextMenu, this);
352         } else {
353             this.el.on('mouseenter'  ,this.enter, this);
354             this.el.on('mouseleave', this.leave, this);
355             this.el.on('click', this.onClick, this);
356         }
357         
358         if (typeof(this.parent().bricks) == 'object' && this.parent().bricks != null) {
359             this.parent().bricks.push(this);   
360         }
361         
362     },
363     
364     onClick: function(e, el)
365     {
366         var time = this.endTimer - this.startTimer;
367         // Roo.log(e.preventDefault());
368         if(Roo.isTouch){
369             if(time > 1000){
370                 e.preventDefault();
371                 return;
372             }
373         }
374         
375         if(!this.preventDefault){
376             return;
377         }
378         
379         e.preventDefault();
380         
381         if (this.activeClass != '') {
382             this.selectBrick();
383         }
384         
385         this.fireEvent('click', this, e);
386     },
387     
388     enter: function(e, el)
389     {
390         e.preventDefault();
391         
392         if(!this.isFitContainer || this.maskInverse || this.videourl.length){
393             return;
394         }
395         
396         if(this.bgimage.length && this.html.length){
397             this.el.select('.masonry-brick-paragraph', true).first().setOpacity(0.9, true);
398         }
399     },
400     
401     leave: function(e, el)
402     {
403         e.preventDefault();
404         
405         if(!this.isFitContainer || this.maskInverse  || this.videourl.length){
406             return;
407         }
408         
409         if(this.bgimage.length && this.html.length){
410             this.el.select('.masonry-brick-paragraph', true).first().setOpacity(0, true);
411         }
412     },
413     
414     onTouchStart: function(e, el)
415     {
416 //        e.preventDefault();
417         
418         this.touchmoved = false;
419         
420         if(!this.isFitContainer){
421             return;
422         }
423         
424         if(!this.bgimage.length || !this.html.length){
425             return;
426         }
427         
428         this.el.select('.masonry-brick-paragraph', true).first().setOpacity(0.9, true);
429         
430         this.timer = new Date().getTime();
431         
432     },
433     
434     onTouchMove: function(e, el)
435     {
436         this.touchmoved = true;
437     },
438     
439     onContextMenu : function(e,el)
440     {
441         e.preventDefault();
442         e.stopPropagation();
443         return false;
444     },
445     
446     onTouchEnd: function(e, el)
447     {
448 //        e.preventDefault();
449         
450         if((new Date().getTime() - this.timer > 1000) || !this.href.length || this.touchmoved){
451         
452             this.leave(e,el);
453             
454             return;
455         }
456         
457         if(!this.bgimage.length || !this.html.length){
458             
459             if(this.href.length){
460                 window.location.href = this.href;
461             }
462             
463             return;
464         }
465         
466         if(!this.isFitContainer){
467             return;
468         }
469         
470         this.el.select('.masonry-brick-paragraph', true).first().setOpacity(0, true);
471         
472         window.location.href = this.href;
473     },
474     
475     //selection on single brick only
476     selectBrick : function() {
477         
478         if (!this.parentId) {
479             return;
480         }
481         
482         var m = Roo.bootstrap.LayoutMasonry.get(this.parentId);
483         var index = m.selectedBrick.indexOf(this.id);
484         
485         if ( index > -1) {
486             m.selectedBrick.splice(index,1);
487             this.el.removeClass(this.activeClass);
488             return;
489         }
490         
491         for(var i = 0; i < m.selectedBrick.length; i++) {
492             var b = Roo.bootstrap.MasonryBrick.get(m.selectedBrick[i]);
493             b.el.removeClass(b.activeClass);
494         }
495         
496         m.selectedBrick = [];
497         
498         m.selectedBrick.push(this.id);
499         this.el.addClass(this.activeClass);
500         return;
501     },
502     
503     isSelected : function(){
504         return this.el.hasClass(this.activeClass);
505         
506     }
507 });
508
509 Roo.apply(Roo.bootstrap.MasonryBrick, {
510     
511     //groups: {},
512     groups : new Roo.util.MixedCollection(false, function(o) { return o.el.id; }),
513      /**
514     * register a Masonry Brick
515     * @param {Roo.bootstrap.MasonryBrick} the masonry brick to add
516     */
517     
518     register : function(brick)
519     {
520         //this.groups[brick.id] = brick;
521         this.groups.add(brick.id, brick);
522     },
523     /**
524     * fetch a  masonry brick based on the masonry brick ID
525     * @param {string} the masonry brick to add
526     * @returns {Roo.bootstrap.MasonryBrick} the masonry brick
527     */
528     
529     get: function(brick_id) 
530     {
531         // if (typeof(this.groups[brick_id]) == 'undefined') {
532         //     return false;
533         // }
534         // return this.groups[brick_id] ;
535         
536         if(this.groups.key(brick_id)) {
537             return this.groups.key(brick_id);
538         }
539         
540         return false;
541     }
542     
543     
544     
545 });
546
547