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