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     Roo.bootstrap.MasonryBrick.superclass.constructor.call(this, config);
20     
21     this.addEvents({
22         // raw events
23         /**
24          * @event click
25          * When a MasonryBrick is clcik
26          * @param {Roo.bootstrap.MasonryBrick} this
27          * @param {Roo.EventObject} e
28          */
29         "click" : true
30     });
31 };
32
33 Roo.extend(Roo.bootstrap.MasonryBrick, Roo.bootstrap.Component,  {
34     
35     /**
36      * @cfg {String} title
37      */   
38     title : '',
39     /**
40      * @cfg {String} html
41      */   
42     html : '',
43     /**
44      * @cfg {String} bgimage
45      */   
46     bgimage : '',
47     /**
48      * @cfg {String} videourl
49      */   
50     videourl : '',
51     /**
52      * @cfg {String} cls
53      */   
54     cls : '',
55     /**
56      * @cfg {String} href
57      */   
58     href : '',
59     /**
60      * @cfg {String} (xs|sm|md|md-left|md-right|tall|wide) size
61      */   
62     size : 'xs',
63     
64     /**
65      * @cfg {String} (center|bottom) placetitle
66      */   
67     placetitle : '',
68     
69     /**
70      * @cfg {Boolean} isFitContainer defalut true
71      */   
72     isFitContainer : true, 
73     
74     getAutoCreate : function()
75     {
76         if(!this.isFitContainer){
77             return this.getSplitAutoCreate();
78         }
79         
80         var cls = 'masonry-brick masonry-brick-full';
81         
82         if(this.href.length){
83             cls += ' masonry-brick-link';
84         }
85         
86         if(this.bgimage.length){
87             cls += ' masonry-brick-image';
88         }
89         
90         if(!this.html.length){
91             cls += ' enable-mask';
92         }
93         
94         if(this.size){
95             cls += ' masonry-' + this.size + '-brick';
96         }
97         
98         if(this.placetitle.length){
99             
100             switch (this.placetitle) {
101                 case 'center' :
102                     cls += ' masonry-center-title';
103                     break;
104                 case 'bottom' :
105                     cls += ' masonry-bottom-title';
106                     break;
107                 default:
108                     break;
109             }
110             
111         } else {
112             if(!this.html.length && !this.bgimage.length){
113                 cls += ' masonry-center-title';
114             }
115
116             if(!this.html.length && this.bgimage.length){
117                 cls += ' masonry-bottom-title';
118             }
119         }
120         
121         if(this.cls){
122             cls += ' ' + this.cls;
123         }
124         
125         var cfg = {
126             tag: (this.href.length) ? 'a' : 'div',
127             cls: cls,
128             cn: [
129                 {
130                     tag: 'div',
131                     cls: 'masonry-brick-paragraph',
132                     cn: []
133                 }
134             ]
135         };
136         
137         if(this.href.length){
138             cfg.href = this.href;
139         }
140         
141         var cn = cfg.cn[0].cn;
142         
143         if(this.title.length){
144             cn.push({
145                 tag: 'h4',
146                 cls: 'masonry-brick-title',
147                 html: this.title
148             });
149         }
150         
151         if(this.html.length){
152             cn.push({
153                 tag: 'p',
154                 cls: 'masonry-brick-text',
155                 html: this.html
156             });
157         }  
158         if (!this.title.length && !this.html.length) {
159             cfg.cn[0].cls += ' hide';
160         }
161         
162         if(this.bgimage.length){
163             cfg.cn.push({
164                 tag: 'img',
165                 cls: 'masonry-brick-image-view',
166                 src: this.bgimage
167             });
168         }
169         
170         if(this.videourl.length){
171             var vurl = this.videourl.replace(/https:\/\/youtu\.be/, 'https://www.youtube.com/embed/');
172             // youtube support only?
173             cfg.cn.push({
174                 tag: 'iframe',
175                 cls: 'masonry-brick-image-view',
176                 src: vurl,
177                 frameborder : 0,
178                 allowfullscreen : true
179             });
180             
181             
182         }
183         
184         cfg.cn.push({
185             tag: 'div',
186             cls: 'masonry-brick-mask'
187         });
188         
189         return cfg;
190         
191     },
192     
193     getSplitAutoCreate : function()
194     {
195         var cls = 'masonry-brick masonry-brick-split';
196         
197         if(this.href.length){
198             cls += ' masonry-brick-link';
199         }
200         
201         if(this.bgimage.length){
202             cls += ' masonry-brick-image';
203         }
204         
205         if(this.size){
206             cls += ' masonry-' + this.size + '-brick';
207         }
208         
209         switch (this.placetitle) {
210             case 'center' :
211                 cls += ' masonry-center-title';
212                 break;
213             case 'bottom' :
214                 cls += ' masonry-bottom-title';
215                 break;
216             default:
217                 if(!this.bgimage.length){
218                     cls += ' masonry-center-title';
219                 }
220
221                 if(this.bgimage.length){
222                     cls += ' masonry-bottom-title';
223                 }
224                 break;
225         }
226         
227         if(this.cls){
228             cls += ' ' + this.cls;
229         }
230         
231         var cfg = {
232             tag: (this.href.length) ? 'a' : 'div',
233             cls: cls,
234             cn: [
235                 {
236                     tag: 'div',
237                     cls: 'masonry-brick-split-head',
238                     cn: [
239                         {
240                             tag: 'div',
241                             cls: 'masonry-brick-paragraph',
242                             cn: []
243                         }
244                     ]
245                 },
246                 {
247                     tag: 'div',
248                     cls: 'masonry-brick-split-body',
249                     cn: []
250                 }
251             ]
252         };
253         
254         if(this.href.length){
255             cfg.href = this.href;
256         }
257         
258         if(this.title.length){
259             cfg.cn[0].cn[0].cn.push({
260                 tag: 'h4',
261                 cls: 'masonry-brick-title',
262                 html: this.title
263             });
264         }
265         
266         if(this.html.length){
267             cfg.cn[1].cn.push({
268                 tag: 'p',
269                 cls: 'masonry-brick-text',
270                 html: this.html
271             });
272         }
273
274         if(this.bgimage.length){
275             cfg.cn[0].cn.push({
276                 tag: 'img',
277                 cls: 'masonry-brick-image-view',
278                 src: this.bgimage
279             });
280         }
281         
282         if(this.videourl.length){
283             var vurl = this.videourl.replace(/https:\/\/youtu\.be/, 'https://www.youtube.com/embed/');
284             // youtube support only?
285             cfg.cn[0].cn.cn.push({
286                 tag: 'iframe',
287                 cls: 'masonry-brick-image-view',
288                 src: vurl,
289                 frameborder : 0,
290                 allowfullscreen : true
291             });
292         }
293         
294         return cfg;
295     },
296     
297     initEvents: function() 
298     {
299         switch (this.size) {
300             case 'xs' :
301                 this.x = 1;
302                 this.y = 1;
303                 break;
304             case 'sm' :
305                 this.x = 2;
306                 this.y = 2;
307                 break;
308             case 'md' :
309             case 'md-left' :
310             case 'md-right' :
311                 this.x = 3;
312                 this.y = 3;
313                 break;
314             case 'tall' :
315                 this.x = 2;
316                 this.y = 3;
317                 break;
318             case 'wide' :
319                 this.x = 3;
320                 this.y = 2;
321                 break;
322             case 'wide-thin' :
323                 this.x = 3;
324                 this.y = 1;
325                 break;
326                         
327             default :
328                 break;
329         }
330         
331         
332         
333         if(Roo.isTouch){
334             this.el.on('touchstart', this.onTouchStart, this);
335             this.el.on('touchmove', this.onTouchMove, this);
336             this.el.on('touchend', this.onTouchEnd, this);
337             this.el.on('contextmenu', this.onContextMenu, this);
338         } else {
339             this.el.on('mouseenter'  ,this.enter, this);
340             this.el.on('mouseleave', this.leave, this);
341         }
342         
343         if (typeof(this.parent().bricks) == 'object' && this.parent().bricks != null) {
344             this.parent().bricks.push(this);   
345         }
346         
347     },
348     
349     onClick: function(e, el)
350     {
351         if(!Roo.isTouch){
352             return;
353         }
354         
355         var time = this.endTimer - this.startTimer;
356         
357         //alert(time);
358         
359         if(time < 1000){
360             return;
361         }
362         
363         e.preventDefault();
364     },
365     
366     enter: function(e, el)
367     {
368         e.preventDefault();
369         
370         if(!this.isFitContainer){
371             return;
372         }
373         
374         if(this.bgimage.length && this.html.length){
375             this.el.select('.masonry-brick-paragraph', true).first().setOpacity(0.9, true);
376         }
377     },
378     
379     leave: function(e, el)
380     {
381         e.preventDefault();
382         
383         if(!this.isFitContainer){
384             return;
385         }
386         
387         if(this.bgimage.length && this.html.length){
388             this.el.select('.masonry-brick-paragraph', true).first().setOpacity(0, true);
389         }
390     },
391     
392     onTouchStart: function(e, el)
393     {
394 //        e.preventDefault();
395         
396         this.touchmoved = false;
397         
398         if(!this.isFitContainer){
399             return;
400         }
401         
402         if(!this.bgimage.length || !this.html.length){
403             return;
404         }
405         
406         this.el.select('.masonry-brick-paragraph', true).first().setOpacity(0.9, true);
407         
408         this.timer = new Date().getTime();
409         
410     },
411     
412     onTouchMove: function(e, el)
413     {
414         this.touchmoved = true;
415     },
416     
417     onContextMenu : function(e,el)
418     {
419         e.preventDefault();
420         e.stopPropagation();
421         return false;
422     },
423     
424     onTouchEnd: function(e, el)
425     {
426 //        e.preventDefault();
427         
428         if((new Date().getTime() - this.timer > 1000) || !this.href.length || this.touchmoved){
429         
430             this.leave(e,el);
431             
432             return;
433         }
434         
435         if(!this.bgimage.length || !this.html.length){
436             
437             if(this.href.length){
438                 window.location.href = this.href;
439             }
440             
441             return;
442         }
443         
444         if(!this.isFitContainer){
445             return;
446         }
447         
448         this.el.select('.masonry-brick-paragraph', true).first().setOpacity(0, true);
449         
450         window.location.href = this.href;
451     }
452     
453 });
454
455  
456
457