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             }
428         }
429         if (!ret.cards.length) {
430             ret.card = true;
431             ret.position = below;
432             ret.items_n;
433             return ret;
434         }
435         // could not find a card.. stick it at the end..
436         if (ret.card_n < 0) {
437             ret.card_n = last_card_pos;
438             ret.items_n = this.items.indexOf(ret.cards[last_card_pos]);
439             cpos = 'below';
440         }
441         
442         if (this.items[ret.items_n].el == dragged_card_el) {
443             return false;
444         }
445         
446         if (cpos == 'below') {
447             var card_after = cards_pos+1 == cards.length ? false : cards[cards_pos+1];
448             
449             if (card_after  && card_after.el == dragged_card_el) {
450                 return false;
451             }
452             return ret;
453         }
454         
455         // its's after ..
456         var card_before = ctarget > 0 ? cards[ctarget-1] : false;
457         
458         if (card_before  && card_before.el == dragged_card_el) {
459             return false;
460         }
461         
462         return ret;
463     },
464     
465     onNodeEnter : function(n, dd, e, data){
466         return false;
467     },
468     onNodeOver : function(n, dd, e, data)
469     {
470        
471         var target_info = this.getTargetFromEvent(e,data.source.el);
472         if (target_info === false) {
473             this.dropPlaceHolder('hide');
474             return false;
475         }
476         Roo.log(['getTargetFromEvent', target_info ]);
477         
478          
479         this.dropPlaceHolder('show', target_info,data);
480         
481         return false; 
482     },
483     onNodeOut : function(n, dd, e, data){
484         this.dropPlaceHolder('hide');
485      
486     },
487     onNodeDrop : function(n, dd, e, data)
488     {
489         
490         // call drop - return false if  
491         if (this.fireEvent("drop", this, n, dd, e, data) === false) {
492             return false;
493         }
494         
495         var target_info = this.getTargetFromEvent(e,data.source.el);
496         if (target_info === false) {
497             return false;
498         }
499         
500         var pt = this.getDropPoint(e, n, dd);
501         var insertAt = (n == this.bodyEl.dom) ? this.items.length : n.nodeIndex;
502         if (pt == "below") {
503             insertAt++;
504         }
505         for (var i = 0; i < this.items.length; i++) {
506             var r = this.items[i];
507             //var dup = this.store.getById(r.id);
508             if (dup && (dd != this.dragZone)) {
509                 Roo.fly(this.getNode(this.store.indexOf(dup))).frame("red", 1);
510             } else {
511             if (data.copy) {
512                 this.store.insert(insertAt++, r.copy());
513             } else {
514                 data.source.isDirtyFlag = true;
515                 r.store.remove(r);
516                 this.store.insert(insertAt++, r);
517             }
518             this.isDirtyFlag = true;
519             }
520         }
521         this.dragZone.cachedTarget = null;
522         return true;
523     },
524     
525     /**    Decide whether to drop above or below a View node. */
526     getDropPoint : function(e, n, dd)
527     {
528         if (dd) {
529              return false;
530         }
531         if (n == this.bodyEl.dom) {
532             return "above";
533         }
534         var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
535         var c = t + (b - t) / 2;
536         var y = Roo.lib.Event.getPageY(e);
537         if(y <= c) {
538             return "above";
539         }else{
540             return "below";
541         }
542     },
543     onToggleCollapse : function(e)
544         {
545         if (this.collapsed) {
546             this.el.select('.roo-collapse-toggle').removeClass('collapsed');
547             this.el.select('.roo-collapsable').addClass('show');
548             this.collapsed = false;
549             return;
550         }
551         this.el.select('.roo-collapse-toggle').addClass('collapsed');
552         this.el.select('.roo-collapsable').removeClass('show');
553         this.collapsed = true;
554         
555     
556     },
557     dropPlaceHolder: function (action, info, data)
558     {
559         if (this.dropEl === false) {
560             this.dropEl = Roo.DomHelper.append(this.bodyEl, {
561             cls : 'd-none'
562             },true);
563         }
564         this.dropEl.removeClass(['d-none', 'd-block']);        
565         if (action == 'hide') {
566             
567             this.dropEl.addClass('d-none');
568             return;
569         }
570         var cardel = info.card.el.dom;
571         
572         this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
573         if (info.position == 'above') {
574             cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
575         } else if (cardel.nextSibling) {
576             cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
577         } else {
578             cardel.parentNode.append(this.dropEl.dom);
579         }
580         this.dropEl.addClass('d-block roo-card-dropzone');
581         
582         this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
583         
584         
585     
586     
587     
588     }
589
590     
591 });
592