Roo/bootstrap/Card.js
[roojs1] / Roo / bootstrap / Card.js
1 /*
2  *  - LGPL
3  *
4  *  This is BS4's Card element.. - similar to our containers probably..
5  * 
6  */
7 /**
8  * @class Roo.bootstrap.Card
9  * @extends Roo.bootstrap.Component
10  * Bootstrap Card class
11  *
12  *
13  * possible... may not be implemented..
14  * @cfg {String} header_image  src url of image.
15  * @cfg {String|Object} header
16  * @cfg {Number} header_size (0|1|2|3|4|5) H1 or H2 etc.. 0 indicates default
17  * 
18  * @cfg {String} title
19  * @cfg {String} subtitle
20  * @cfg {String} html -- html contents - or just use children..
21  * @cfg {String} footer
22  
23  * @cfg {String} weight (primary|warning|info|danger|secondary|success|light|dark)
24  * 
25  * @cfg {String} margin (0|1|2|3|4|5|auto)
26  * @cfg {String} margin_top (0|1|2|3|4|5|auto)
27  * @cfg {String} margin_bottom (0|1|2|3|4|5|auto)
28  * @cfg {String} margin_left (0|1|2|3|4|5|auto)
29  * @cfg {String} margin_right (0|1|2|3|4|5|auto)
30  * @cfg {String} margin_x (0|1|2|3|4|5|auto)
31  * @cfg {String} margin_y (0|1|2|3|4|5|auto)
32  *
33  * @cfg {String} padding (0|1|2|3|4|5)
34  * @cfg {String} padding_top (0|1|2|3|4|5)
35  * @cfg {String} padding_bottom (0|1|2|3|4|5)
36  * @cfg {String} padding_left (0|1|2|3|4|5)
37  * @cfg {String} padding_right (0|1|2|3|4|5)
38  * @cfg {String} padding_x (0|1|2|3|4|5)
39  * @cfg {String} padding_y (0|1|2|3|4|5)
40  *
41  * @cfg {String} display (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
42  * @cfg {String} display_xs (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
43  * @cfg {String} display_sm (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
44  * @cfg {String} display_lg (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
45  * @cfg {String} display_xl (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
46  
47  * @config {Boolean} dragable  if this card can be dragged.
48  * @config {String} drag_group  group for drag
49  * @config {Boolean} dropable  if this card can recieve other cards being dropped onto it..
50  * @config {String} drop_group  group for drag
51  * 
52  * @config {Boolean} collapsable can the body be collapsed.
53  * @config {Boolean} collapsed is the body collapsed when rendered...
54  * @constructor
55  * Create a new Container
56  * @param {Object} config The config object
57  */
58
59 Roo.bootstrap.Card = function(config){
60     Roo.bootstrap.Card.superclass.constructor.call(this, config);
61     
62     this.addEvents({
63          // raw events
64         /**
65          * @event drop
66          * When a element a card is dropped
67          * @param {Roo.bootstrap.Element} this
68          * @param {Roo.Element} n the node being dropped?
69          * @param {Object} dd Drag and drop data
70          * @param {Roo.EventObject} e
71          * @param {Roo.EventObject} data  the data passed via getDragData
72          */
73         'drop' : true
74         
75     });
76 };
77
78
79 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component,  {
80     
81     
82     weight : '',
83     
84     margin: '', /// may be better in component?
85     margin_top: '', 
86     margin_bottom: '', 
87     margin_left: '',
88     margin_right: '',
89     margin_x: '',
90     margin_y: '',
91     
92     padding : '',
93     padding_top: '', 
94     padding_bottom: '', 
95     padding_left: '',
96     padding_right: '',
97     padding_x: '',
98     padding_y: '',
99     
100     display: '', 
101     display_xs: '', 
102     display_sm: '', 
103     display_lg: '',
104     display_xl: '',
105  
106     header_image  : '',
107     header : '',
108     header_size : 0,
109     title : '',
110     subtitle : '',
111     html : '',
112     footer: '',
113
114     collapsable : false,
115     collapsed : false,
116     
117     dragable : false,
118     drag_group : false,
119     dropable : false,
120     drop_group : false,
121     childContainer : false,
122     dropEl : false, /// the dom placeholde element that indicates drop location.
123     
124     layoutCls : function()
125     {
126         var cls = '';
127         var t = this;
128         Roo.log(this.margin_bottom.length);
129         ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
130             // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
131             
132             if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
133                 cls += ' m' +  (v.length ? v[0]  : '') + '-' +  t['margin' + (v.length ? '_' : '') + v];
134             }
135             if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
136                 cls += ' p' +  (v.length ? v[0]  : '') + '-' +  t['padding' + (v.length ? '_' : '') + v];
137             }
138         });
139         
140         ['', 'xs', 'sm', 'lg', 'xl'].forEach(function(v) {
141             if (('' + t['display' + (v.length ? '_' : '') + v]).length) {
142                 cls += ' d' +  (v.length ? '-' : '') + v + '-' + t['margin' + (v.length ? '_' : '') + v]
143             }
144         });
145         
146         // more generic support?
147         if (this.hidden) {
148             cls += ' d-none';
149         }
150         
151         return cls;
152     },
153  
154        // Roo.log("Call onRender: " + this.xtype);
155         /*  We are looking at something like this.
156 <div class="card">
157     <img src="..." class="card-img-top" alt="...">
158     <div class="card-body">
159         <h5 class="card-title">Card title</h5>
160          <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
161
162         >> this bit is really the body...
163         <div> << we will ad dthis in hopefully it will not break shit.
164         
165         ** card text does not actually have any styling...
166         
167             <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
168         
169         </div> <<
170           <a href="#" class="card-link">Card link</a>
171           
172     </div>
173     <div class="card-footer">
174         <small class="text-muted">Last updated 3 mins ago</small>
175     </div>
176 </div>
177          */
178     getAutoCreate : function(){
179         
180         var cfg = {
181             tag : 'div',
182             cls : 'card',
183             cn : [ ]
184         };
185         
186         if (this.weight.length && this.weight != 'light') {
187             cfg.cls += ' text-white';
188         } else {
189             cfg.cls += ' text-dark'; // need as it's nested..
190         }
191         if (this.weight.length) {
192             cfg.cls += ' bg-' + this.weight;
193         }
194         
195         cfg.cls += this.layoutCls(); 
196         
197     var hdr = false;
198         if (this.header.length) {
199             hdr = {
200                 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
201                 cls : 'card-header',
202         cn : []
203             };
204         cfg.cn.push(hdr);
205         hdr_ctr = hdr;
206         } else {
207         hdr = {
208                 tag : 'div',
209                 cls : 'card-header d-none',
210         cn : []
211             };
212         cfg.cn.push(hdr);
213     }
214     if (this.collapsable) {
215         hdr_ctr = {
216         tag : 'a',
217         cls : 'd-block user-select-none',
218         cn: [
219             {
220             tag: 'i',
221             cls : 'roo-collapse-toggle fa fa-chevron-down float-right'
222             }
223            
224         ]
225         };
226         hdr.cn.push(hdr_ctr);
227     }
228     if (this.header.length) {
229         hdr_ctr.cn.push(        {
230         tag: 'span',
231         cls: 'roo-card-header-ctr',
232         html : this.header
233         })
234     }
235     
236         if (this.header_image.length) {
237             cfg.cn.push({
238                 tag : 'img',
239                 cls : 'card-img-top',
240                 src: this.header_image // escape?
241             });
242         } else {
243         cfg.cn.push({
244                 tag : 'div',
245                 cls : 'card-img-top d-none' 
246             });
247     }
248         
249         var body = {
250             tag : 'div',
251             cls : 'card-body',
252             cn : []
253         };
254     var obody = body;
255     if (this.collapsable) {
256         obody = {
257         tag: 'div',
258         cls : 'roo-collapsable collapse ' + (this.collapsed ? '' : 'show'),
259         cn : [  body ]
260         };
261     }
262     
263         cfg.cn.push(obody);
264         
265         if (this.title.length) {
266             body.cn.push({
267                 tag : 'div',
268                 cls : 'card-title',
269                 src: this.title // escape?
270             });
271         }
272         
273         if (this.subtitle.length) {
274             body.cn.push({
275                 tag : 'div',
276                 cls : 'card-title',
277                 src: this.subtitle // escape?
278             });
279         }
280         
281         body.cn.push({
282             tag : 'div',
283             cls : 'roo-card-body-ctr'
284         });
285         
286         if (this.html.length) {
287             body.cn.push({
288                 tag: 'div',
289                 html : this.html
290             });
291         }
292         // fixme ? handle objects?
293         if (this.footer.length) {
294             cfg.cn.push({
295                 tag : 'div',
296                 cls : 'card-footer',
297                 html: this.footer // escape?
298             });
299         }
300         // footer...
301         
302         return cfg;
303     },
304     
305     
306     getCardHeader : function()
307     {
308         var  ret = this.el.select('.card-header',true).first();
309     if (ret.hasClass('d-none')) {
310         ret.removeClass('d-none');
311     }
312         
313         return ret;
314     },
315     
316     getCardImageTop : function()
317     {
318         var  ret = this.el.select('.card-img-top',true).first();
319     if (ret.hasClass('d-none')) {
320         ret.removeClass('d-none');
321     }
322         
323         return ret;
324     },
325     
326     getChildContainer : function()
327     {
328         
329         if(!this.el){
330             return false;
331         }
332         return this.el.select('.roo-card-body-ctr',true).first();    
333     },
334     
335     initEvents: function() 
336     {
337         
338     this.bodyEl = this.getChildContainer();
339     if(this.dragable){
340             this.dragZone = new Roo.dd.DragZone(this.getEl(), {
341                     containerScroll: true,
342                     ddGroup: this.drag_group || 'default_card_drag_group'
343             });
344             this.dragZone.getDragData = this.getDragData.createDelegate(this);
345         }
346         if (this.dropable) {
347         this.dropZone = new Roo.dd.DropZone(this.el.select('.card-body',true).first() , {
348             containerScroll: true,
349             ddGroup: this.drop_group || 'default_card_drag_group'
350         });
351         this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
352         this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
353         this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
354         this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
355         this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
356     }
357         
358         if (this.collapsable) {
359         this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
360     }
361     },
362     getDragData : function(e)
363     {
364         var target = this.getEl();
365         if (target) {
366             //this.handleSelection(e);
367             
368             var dragData = {
369                 source: this,
370                 copy: false,
371                 nodes: this.getEl(),
372                 records: []
373             };
374             
375             
376             dragData.ddel = target.dom ;    // the div element
377             Roo.log(target.getWidth( ));
378             dragData.ddel.style.width = target.getWidth() + 'px';
379             
380             return dragData;
381         }
382         return false;
383     },
384     /**
385     *    Part of the Roo.dd.DropZone interface. If no target node is found, the
386     *    whole Element becomes the target, and this causes the drop gesture to append.
387     */
388     getTargetFromEvent : function(e, dragged_card_el)
389     {
390         var target = e.getTarget();
391         while ((target !== null) && (target.parentNode != this.bodyEl.dom)) {
392             target = target.parentNode;
393         }
394         
395         var ret = {
396             position: '',
397             cards : [],
398             card_n : -1,
399             items_n : -1,
400             card : false,
401         };
402         
403         //Roo.log([ 'target' , target ? target.id : '--nothing--']);
404         // see if target is one of the 'cards'...
405         
406         
407         //Roo.log(this.items.length);
408         var pos = false;
409         
410         var last_card_n = 0;
411         var cards_len  = 0;
412         for (var i = 0;i< this.items.length;i++) {
413             
414             if (!this.items[i].el.hasClass('card')) {
415                  continue;
416             }
417             pos = this.getDropPoint(e, this.items[i].el.dom);
418             
419             cards_len = ret.cards.length;
420             //Roo.log(this.items[i].el.dom.id);
421             ret.cards.push(this.items[i]);
422             last_card_n  = i;
423             if (ret.card_n < 0 && pos == 'above') {
424                 ret.position = cards_len > 0 ? 'below' : pos;
425                 ret.items_n = i > 0 ? i - 1 : 0;
426                 ret.card_n  = cards_len  > 0 ? cards_len - 1 : 0;
427                 ret.card = ret.cards[ret.card_n];
428             }
429         }
430         if (!ret.cards.length) {
431             ret.card = true;
432             ret.position = below;
433             ret.items_n;
434             return ret;
435         }
436         // could not find a card.. stick it at the end..
437         if (ret.card_n < 0) {
438             ret.card_n = last_card_n;
439             ret.card = ret.cards[last_card_n];
440             ret.items_n = this.items.indexOf(ret.cards[last_card_n]);
441             cpos = 'below';
442         }
443         
444         if (this.items[ret.items_n].el == dragged_card_el) {
445             return false;
446         }
447         
448         if (ret.position == 'below') {
449             var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
450             
451             if (card_after  && card_after.el == dragged_card_el) {
452                 return false;
453             }
454             return ret;
455         }
456         
457         // its's after ..
458         var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
459         
460         if (card_before  && card_before.el == dragged_card_el) {
461             return false;
462         }
463         
464         return ret;
465     },
466     
467     onNodeEnter : function(n, dd, e, data){
468         return false;
469     },
470     onNodeOver : function(n, dd, e, data)
471     {
472        
473         var target_info = this.getTargetFromEvent(e,data.source.el);
474         if (target_info === false) {
475             this.dropPlaceHolder('hide');
476             return false;
477         }
478         Roo.log(['getTargetFromEvent', target_info ]);
479         
480          
481         this.dropPlaceHolder('show', target_info,data);
482         
483         return false; 
484     },
485     onNodeOut : function(n, dd, e, data){
486         this.dropPlaceHolder('hide');
487      
488     },
489     onNodeDrop : function(n, dd, e, data)
490     {
491         
492         // call drop - return false if  
493         if (this.fireEvent("drop", this, n, dd, e, data) === false) {
494             return false;
495         }
496         
497         var target_info = this.getTargetFromEvent(e,data.source.el);
498         if (target_info === false) {
499             return false;
500         }
501          
502         this.dropPlaceHolder('hide');
503         
504         
505         // add this to the correct place in items.
506         
507         
508         return true;
509     },
510     
511     /**    Decide whether to drop above or below a View node. */
512     getDropPoint : function(e, n, dd)
513     {
514         if (dd) {
515              return false;
516         }
517         if (n == this.bodyEl.dom) {
518             return "above";
519         }
520         var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
521         var c = t + (b - t) / 2;
522         var y = Roo.lib.Event.getPageY(e);
523         if(y <= c) {
524             return "above";
525         }else{
526             return "below";
527         }
528     },
529     onToggleCollapse : function(e)
530         {
531         if (this.collapsed) {
532             this.el.select('.roo-collapse-toggle').removeClass('collapsed');
533             this.el.select('.roo-collapsable').addClass('show');
534             this.collapsed = false;
535             return;
536         }
537         this.el.select('.roo-collapse-toggle').addClass('collapsed');
538         this.el.select('.roo-collapsable').removeClass('show');
539         this.collapsed = true;
540         
541     
542     },
543     dropPlaceHolder: function (action, info, data)
544     {
545         if (this.dropEl === false) {
546             this.dropEl = Roo.DomHelper.append(this.bodyEl, {
547             cls : 'd-none'
548             },true);
549         }
550         this.dropEl.removeClass(['d-none', 'd-block']);        
551         if (action == 'hide') {
552             
553             this.dropEl.addClass('d-none');
554             return;
555         }
556         var cardel = info.card.el.dom;
557         
558         this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
559         if (info.position == 'above') {
560             cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
561         } else if (cardel.nextSibling) {
562             cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
563         } else {
564             cardel.parentNode.append(this.dropEl.dom);
565         }
566         this.dropEl.addClass('d-block roo-card-dropzone');
567         
568         this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
569         
570         
571     
572     
573     
574     }
575
576     
577 });
578