2cf1c9eb9e843be8dbefa7b6575f60df38c368ba
[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 {Boolean} drag_group  group for drag
49  * 
50  
51  * @constructor
52  * Create a new Container
53  * @param {Object} config The config object
54  */
55
56 Roo.bootstrap.Card = function(config){
57     Roo.bootstrap.Card.superclass.constructor.call(this, config);
58     
59     this.addEvents({
60         
61     });
62 };
63
64
65 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component,  {
66     
67     
68     weight : '',
69     
70     margin: '', /// may be better in component?
71     margin_top: '', 
72     margin_bottom: '', 
73     margin_left: '',
74     margin_right: '',
75     margin_x: '',
76     margin_y: '',
77     
78     padding : '',
79     padding_top: '', 
80     padding_bottom: '', 
81     padding_left: '',
82     padding_right: '',
83     padding_x: '',
84     padding_y: '',
85     
86     display: '', 
87     display_xs: '', 
88     display_sm: '', 
89     display_lg: '',
90     display_xl: '',
91  
92     header_image  : '',
93     header : '',
94     header_size : 0,
95     title : '',
96     subtitle : '',
97     html : '',
98     footer: '',
99     
100     dragable : false,
101     drag_group : false,
102     
103     childContainer : false,
104
105     layoutCls : function()
106     {
107         var cls = '';
108         var t = this;
109         Roo.log(this.margin_bottom.length);
110         ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
111             // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
112             
113             if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
114                 cls += ' m' +  (v.length ? v[0]  : '') + '-' +  t['margin' + (v.length ? '_' : '') + v];
115             }
116             if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
117                 cls += ' p' +  (v.length ? v[0]  : '') + '-' +  t['padding' + (v.length ? '_' : '') + v];
118             }
119         });
120         
121         ['', 'xs', 'sm', 'lg', 'xl'].forEach(function(v) {
122             if (('' + t['display' + (v.length ? '_' : '') + v]).length) {
123                 cls += ' d' +  (v.length ? '-' : '') + v + '-' + t['margin' + (v.length ? '_' : '') + v]
124             }
125         });
126         
127         // more generic support?
128         if (this.hidden) {
129             cls += ' d-none';
130         }
131         
132         return cls;
133     },
134  
135        // Roo.log("Call onRender: " + this.xtype);
136         /*  We are looking at something like this.
137 <div class="card">
138     <img src="..." class="card-img-top" alt="...">
139     <div class="card-body">
140         <h5 class="card-title">Card title</h5>
141          <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
142
143         >> this bit is really the body...
144         <div> << we will ad dthis in hopefully it will not break shit.
145         
146         ** card text does not actually have any styling...
147         
148             <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>
149         
150         </div> <<
151           <a href="#" class="card-link">Card link</a>
152           
153     </div>
154     <div class="card-footer">
155         <small class="text-muted">Last updated 3 mins ago</small>
156     </div>
157 </div>
158          */
159     getAutoCreate : function(){
160         
161         var cfg = {
162             tag : 'div',
163             cls : 'card',
164             cn : [ ]
165         };
166         
167         if (this.weight.length && this.weight != 'light') {
168             cfg.cls += ' text-white';
169         } else {
170             cfg.cls += ' text-dark'; // need as it's nested..
171         }
172         if (this.weight.length) {
173             cfg.cls += ' bg-' + this.weight;
174         }
175         
176         cfg.cls += this.layoutCls(); 
177         
178         if (this.header.length) {
179             cfg.cn.push({
180                 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
181                 cls : 'card-header',
182                 html : this.header // escape?
183             });
184         } else {
185             cfg.cn.push({
186                 tag : 'div',
187                 cls : 'card-header d-none'
188             });
189         }
190         if (this.header_image.length) {
191             cfg.cn.push({
192                 tag : 'img',
193                 cls : 'card-img-top',
194                 src: this.header_image // escape?
195             });
196         } else {
197             cfg.cn.push({
198                 tag : 'div',
199                 cls : 'card-img-top d-none' 
200             });
201         }
202         
203         var body = {
204             tag : 'div',
205             cls : 'card-body',
206             cn : []
207         };
208         cfg.cn.push(body);
209         
210         if (this.title.length) {
211             body.cn.push({
212                 tag : 'div',
213                 cls : 'card-title',
214                 src: this.title // escape?
215             });
216         }
217         
218         if (this.subtitle.length) {
219             body.cn.push({
220                 tag : 'div',
221                 cls : 'card-title',
222                 src: this.subtitle // escape?
223             });
224         }
225         
226         body.cn.push({
227             tag : 'div',
228             cls : 'roo-card-body-ctr'
229         });
230         
231         if (this.html.length) {
232             body.cn.push({
233                 tag: 'div',
234                 html : this.html
235             });
236         }
237         // fixme ? handle objects?
238         if (this.footer.length) {
239             cfg.cn.push({
240                 tag : 'div',
241                 cls : 'card-footer',
242                 html: this.footer // escape?
243             });
244         }
245         // footer...
246         
247         return cfg;
248     },
249     
250     
251     getCardHeader : function()
252     {
253         var  ret = this.el.select('.card-header',true).first();
254         if (ret.hasClass('d-none')) {
255             ret.removeClass('d-none');
256         }
257         
258         return ret;
259     },
260     
261     getCardImageTop : function()
262     {
263         var  ret = this.el.select('.card-img-top',true).first();
264         if (ret.hasClass('d-none')) {
265             ret.removeClass('d-none');
266         }
267         
268         return ret;
269     },
270     
271     getChildContainer : function()
272     {
273         
274         if(!this.el){
275             return false;
276         }
277         return this.el.select('.roo-card-body-ctr',true).first();    
278     },
279     
280     initEvents: function() 
281     {
282         if(this.dragable){
283              this.dragZone = new Roo.dd.DragZone(this.getEl(), {
284                     containerScroll: true,
285                     ddGroup: this.drag_group || 'default_card_drag_group'
286             });
287             this.dragZone.getDragData = this.getDragData.createDelegate(this);
288         }
289         
290         
291         
292     },
293     getDragData : function(e) {
294         var target = this.getEl();
295         if (target) {
296             //this.handleSelection(e);
297             
298             var dragData = {
299                 source: this,
300                 copy: false,
301                 nodes: this.getEl(),
302                 records: []
303             };
304             
305             
306             dragData.ddel = target.dom ;        // the div element
307             Roo.log(target.getWidth( ));
308              dragData.ddel.style.width = target.getWidth() + 'px';
309             
310             return dragData;
311         }
312         return false;
313     }
314     
315 });
316