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