b5560ffaa02dde9d2e22e584b3f1ff7a437508f9
[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} 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         }
185         if (this.header_image.length) {
186             cfg.cn.push({
187                 tag : 'img',
188                 cls : 'card-img-top',
189                 src: this.header_image // escape?
190             });
191         }
192         
193         var body = {
194             tag : 'div',
195             cls : 'card-body',
196             cn : []
197         };
198         cfg.cn.push(body);
199         
200         if (this.title.length) {
201             body.cn.push({
202                 tag : 'div',
203                 cls : 'card-title',
204                 src: this.title // escape?
205             });
206         }
207         
208         if (this.subtitle.length) {
209             body.cn.push({
210                 tag : 'div',
211                 cls : 'card-title',
212                 src: this.subtitle // escape?
213             });
214         }
215         
216         body.cn.push({
217             tag : 'div',
218             cls : 'roo-card-body-ctr'
219         });
220         
221         if (this.html.length) {
222             body.cn.push({
223                 tag: 'div',
224                 html : this.html
225             });
226         }
227         // fixme ? handle objects?
228         if (this.footer.length) {
229             cfg.cn.push({
230                 tag : 'div',
231                 cls : 'card-footer',
232                 html: this.footer // escape?
233             });
234         }
235         // footer...
236         
237         return cfg;
238     },
239     
240     
241     getChildContainer : function()
242     {
243         
244         if(!this.el){
245             return false;
246         }
247         return this.el.select('.roo-card-body-ctr',true).first();    
248     },
249     
250     initEvents: function() 
251     {
252         if(this.dragable){
253              this.dragZone = new Roo.dd.DragZone(this.getEl(), {
254                     containerScroll: true,
255                     ddGroup: this.drag_group || 'default_card_drag_group'
256             });
257             this.dragZone.getDragData = this.getDragData.createDelegate(this);
258         }
259         
260         
261         
262     },
263     getDragData : function(e) {
264         var target = this.getEl();
265         if (target) {
266             //this.handleSelection(e);
267             
268             var dragData = {
269                 source: this,
270                 copy: false,
271                 nodes: this.getEl(),
272                 records: []
273             };
274             
275             
276             dragData.ddel = target.dom ;        // the div element
277             Roo.log(target.getWidth( ));
278              dragData.ddel.style.width = target.getWidth() + 'px';
279             
280             return dragData;
281         }
282         return false;
283     }
284     
285 });
286