Roo/bootstrap/Card.js
[roojs1] / Roo / bootstrap / Card.js
1 /*
2  * This is BS4's Card element.. - similar to our containers probably..
3  * 
4  */
5 /**
6  * @class Roo.bootstrap.Card
7  * @extends Roo.bootstrap.Component
8  * Bootstrap Card class
9  *
10  *
11  * possible... may not be implemented..
12  * @cfg {String} header_image  src url of image.
13  * @cfg {String} header
14  * @cfg {Number} header_size (0|1|2|3|4|5) H1 or H2 etc.. 0 indicates default
15  * 
16  * @cfg {String} title
17  * @cfg {String} subtitle
18  * @cfg {String} html -- html contents - or just use children..
19  * @cfg {String} footer
20  
21  * @cfg {String} weight (primary|warning|info|danger|secondary|success|light|dark)
22  * 
23  * @cfg {String} margin (0|1|2|3|4|5|auto)
24  * @cfg {String} margin_top (0|1|2|3|4|5|auto)
25  * @cfg {String} margin_bottom (0|1|2|3|4|5|auto)
26  * @cfg {String} margin_left (0|1|2|3|4|5|auto)
27  * @cfg {String} margin_right (0|1|2|3|4|5|auto)
28  * @cfg {String} margin_x (0|1|2|3|4|5|auto)
29  * @cfg {String} margin_y (0|1|2|3|4|5|auto)
30  *
31  * @cfg {String} padding (0|1|2|3|4|5)
32  * @cfg {String} padding_top (0|1|2|3|4|5)
33  * @cfg {String} padding_bottom (0|1|2|3|4|5)
34  * @cfg {String} padding_left (0|1|2|3|4|5)
35  * @cfg {String} padding_right (0|1|2|3|4|5)
36  * @cfg {String} padding_x (0|1|2|3|4|5)
37  * @cfg {String} padding_y (0|1|2|3|4|5)
38  *
39  * @cfg {String} display (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
40  * @cfg {String} display_xs (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
41  * @cfg {String} display_sm (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
42  * @cfg {String} display_lg (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
43  * @cfg {String} display_xl (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
44  
45  * @constructor
46  * Create a new Container
47  * @param {Object} config The config object
48  */
49
50 Roo.bootstrap.Card = function(config){
51     Roo.bootstrap.Card.superclass.constructor.call(this, config);
52     
53     this.addEvents({
54         
55     });
56 };
57
58
59 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component,  {
60     
61     
62     weight : '',
63     
64     margin: '', /// may be better in component?
65     margin_top: '', 
66     margin_bottom: '', 
67     margin_left: '',
68     margin_right: '',
69     margin_x: '',
70     margin_y: '',
71     
72     padding : '',
73     padding_top: '', 
74     padding_bottom: '', 
75     padding_left: '',
76     padding_right: '',
77     padding_x: '',
78     padding_y: '',
79     
80     display: '', 
81     display_xs: '', 
82     display_sm: '', 
83     display_lg: '',
84     display_xl: '',
85  
86     header_image  : '',
87     header : '',
88     header_size : 0,
89     title : '',
90     subtitle : '',
91     html : '',
92     
93     
94     childContainer : false,
95
96     layoutCls : function()
97     {
98         var cls = '';
99         var t = this;
100         
101         ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
102             // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
103             
104             if (t['margin' + (v.length ? '_' : '') + v].length) {
105                 cls += ' m' +  (v.length ? v[0]  : '') + '-' +  t['margin' + (v.length ? '_' : '') + v];
106             }
107             if (t['padding' + (v.length ? '_' : '') + v].length) {
108                 cls += ' p' +  (v.length ? v[0]  : '') + '-' +  t['padding' + (v.length ? '_' : '') + v];
109             }
110         });
111         
112         ['', 'xs', 'sm', 'lg', 'xl',   ].forEach(function(v) {
113             if (t['display' + (v.length ? '_' : '') + v].length) {
114                 cls += ' d' +  (v.length ? '-' : '') + v + '-' + t['margin' + (v.length ? '_' : '') + v].length
115             }
116         });
117         
118         // more generic support?
119         if (this.hidden) {
120             cls += ' d-none';
121         }
122         
123         return cls;
124     },
125  
126        // Roo.log("Call onRender: " + this.xtype);
127         /*  We are looking at something like this.
128 <div class="card">
129     <img src="..." class="card-img-top" alt="...">
130     <div class="card-body">
131         <h5 class="card-title">Card title</h5>
132          <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
133
134         >> this bit is really the body...
135         <div> << we will ad dthis in hopefully it will not break shit.
136         
137             <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>
138         
139         </div> <<
140           <a href="#" class="card-link">Card link</a>
141           
142     </div>
143     <div class="card-footer">
144         <small class="text-muted">Last updated 3 mins ago</small>
145     </div>
146 </div>
147          */
148     getAutoCreate : function(){
149         
150         var cfg = {
151             tag : 'div',
152             cls : 'card',
153             cn : [ ]
154         };
155         
156         if (this.weight.length && this.weight != 'light') {
157             cfg.cls += ' text-white'
158         }
159         if (this.weight.length) {
160             cfg.cls += ' bg-' + this.weight;
161         }
162         
163         cfg.cls += this.layoutCls(); 
164         
165         if (this.header.length) {
166             cfg.cn.push({
167                 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
168                 cls : 'card-header',
169                 html : this.header // escape?
170             });
171         }
172         if (this.header_image.length) {
173             cfg.cn.push({
174                 tag : 'img',
175                 cls : 'card-img-top',
176                 src: this.header_image // escape?
177             });
178         }
179         
180         var body = {
181             tag : 'div',
182             cls : 'card-body',
183             cn : []
184         };
185         cfg.push(body);
186         
187         if (this.title.length) {
188             body.cn.push({
189                 tag : 'div',
190                 cls : 'card-title',
191                 src: this.title // escape?
192             });
193         }
194         
195         if (this.subtitle.length) {
196             body.cn.push({
197                 tag : 'div',
198                 cls : 'card-title',
199                 src: this.subtitle // escape?
200             });
201         }
202         
203         body.cn.push({
204             tag : 'div',
205             cls : 'roo-card-body-ctr'
206         });
207         
208         // fixme ? handle objects?
209         if (this.footer.length) {
210             body.cn.push({
211                 tag : 'div',
212                 cls : 'card-footer',
213                 html: this.footer // escape?
214             });
215         }
216         // footer...
217         
218         return cfg;
219     },
220     
221     
222     getChildContainer : function()
223     {
224         
225         if(!this.el){
226             return false;
227         }
228         return this.el.select('.roo-card-body-ctr',true).first();    
229     }
230     
231 }