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                 html : this.header // escape?
192             };
193             cfg.cn.push(hdr);
194             
195         } else {
196             hdr = {
197                 tag : 'div',
198                 cls : 'card-header d-none',
199                 cn : []
200             }
201             cfg.cn.push(hdr);
202         }
203         if (this.collapsable) {
204             hdr.cn.push({
205                 tag: 'i',
206                 cls : 'fa fa-chevron-down pull-right'
207             });
208         }
209         
210         if (this.header_image.length) {
211             cfg.cn.push({
212                 tag : 'img',
213                 cls : 'card-img-top',
214                 src: this.header_image // escape?
215             });
216         } else {
217             cfg.cn.push({
218                 tag : 'div',
219                 cls : 'card-img-top d-none' 
220             });
221         }
222         
223         var body = {
224             tag : 'div',
225             cls : 'card-body',
226             cn : []
227         };
228         
229         if (this.collapsable) {
230             body = {
231                 tag: 'div',
232                 cls : 'roo-collapsable collapse ' + (this.collapsed ? '' : 'show'),
233                 cn : [  body ]
234             };
235         }
236         
237         cfg.cn.push(body);
238         
239         if (this.title.length) {
240             body.cn.push({
241                 tag : 'div',
242                 cls : 'card-title',
243                 src: this.title // escape?
244             });
245         }
246         
247         if (this.subtitle.length) {
248             body.cn.push({
249                 tag : 'div',
250                 cls : 'card-title',
251                 src: this.subtitle // escape?
252             });
253         }
254         
255         body.cn.push({
256             tag : 'div',
257             cls : 'roo-card-body-ctr'
258         });
259         
260         if (this.html.length) {
261             body.cn.push({
262                 tag: 'div',
263                 html : this.html
264             });
265         }
266         // fixme ? handle objects?
267         if (this.footer.length) {
268             cfg.cn.push({
269                 tag : 'div',
270                 cls : 'card-footer',
271                 html: this.footer // escape?
272             });
273         }
274         // footer...
275         
276         return cfg;
277     },
278     
279     
280     getCardHeader : function()
281     {
282         var  ret = this.el.select('.card-header',true).first();
283         if (ret.hasClass('d-none')) {
284             ret.removeClass('d-none');
285         }
286         
287         return ret;
288     },
289     
290     getCardImageTop : function()
291     {
292         var  ret = this.el.select('.card-img-top',true).first();
293         if (ret.hasClass('d-none')) {
294             ret.removeClass('d-none');
295         }
296         
297         return ret;
298     },
299     
300     getChildContainer : function()
301     {
302         
303         if(!this.el){
304             return false;
305         }
306         return this.el.select('.roo-card-body-ctr',true).first();    
307     },
308     
309     initEvents: function() 
310     {
311         
312         this.bodyEl = this.getChildContainer();
313         if(this.dragable){
314             this.dragZone = new Roo.dd.DragZone(this.getEl(), {
315                     containerScroll: true,
316                     ddGroup: this.drag_group || 'default_card_drag_group'
317             });
318             this.dragZone.getDragData = this.getDragData.createDelegate(this);
319         }
320         if (this.dropable) {
321             this.dropZone = new Roo.dd.DropZone(this.getChildContainer(), {
322                     containerScroll: true,
323                     ddGroup: this.drop_group || 'default_card_drag_group'
324             });
325             this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
326             this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
327             this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
328             this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
329             this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
330         }
331         
332         if (this.collapsable) {
333             this.el.select('.card-body .pull-right',true).on('click', this.onToggleCollapse, this);
334         }
335     },
336     getDragData : function(e) {
337         var target = this.getEl();
338         if (target) {
339             //this.handleSelection(e);
340             
341             var dragData = {
342                 source: this,
343                 copy: false,
344                 nodes: this.getEl(),
345                 records: []
346             };
347             
348             
349             dragData.ddel = target.dom ;        // the div element
350             Roo.log(target.getWidth( ));
351              dragData.ddel.style.width = target.getWidth() + 'px';
352             
353             return dragData;
354         }
355         return false;
356     },
357     /**
358  *      Part of the Roo.dd.DropZone interface. If no target node is found, the
359  *      whole Element becomes the target, and this causes the drop gesture to append.
360  */
361     getTargetFromEvent : function(e)
362     {
363         var target = e.getTarget();
364         while ((target !== null) && (target.parentNode != this.bodyEl.dom)) {
365             target = target.parentNode;
366         }
367         // see if target is one of the 'cards'...
368         var ctarget = false;
369         var cards = [];
370         for (var i = 0;i< this.items.length;i++) {
371             if (this.items[i].xtype != 'Card') {
372                 continue;
373             }
374             cards.push(this.items[i].el.dom);
375             if (target == this.items[i].el.dom) {
376                 ctarget = target;
377             }
378         }
379         
380         if (!ctarget) {
381             ctarget = cards[cards.length-1] || this.el.dom;
382         }
383         
384         
385         Roo.log(['getTargetFromEvent', ctarget]);
386         return ctarget;
387     },
388     
389     onNodeEnter : function(n, dd, e, data){
390         return false;
391     },
392     onNodeOver : function(n, dd, e, data)
393     {
394         var pt = this.getDropPoint(e, n, dd);
395         // set the insert point style on the target node
396         //var dragElClass = this.dropNotAllowed;
397         if (pt) {
398             Roo.log(pt);
399         }
400         return false; //dragElClass;
401     },
402     onNodeOut : function(n, dd, e, data){
403         //this.removeDropIndicators(n);
404     },
405     onNodeDrop : function(n, dd, e, data)
406     {
407         return false;
408         
409         if (this.fireEvent("drop", this, n, dd, e, data) === false) {
410                 return false;
411         }
412         var pt = this.getDropPoint(e, n, dd);
413         var insertAt = (n == this.bodyEl.dom) ? this.items.length : n.nodeIndex;
414         if (pt == "below") {
415             insertAt++;
416         }
417         for (var i = 0; i < this.items.length; i++) {
418             var r = this.items[i];
419             //var dup = this.store.getById(r.id);
420             if (dup && (dd != this.dragZone)) {
421                     Roo.fly(this.getNode(this.store.indexOf(dup))).frame("red", 1);
422             } else {
423                 if (data.copy) {
424                     this.store.insert(insertAt++, r.copy());
425                 } else {
426                     data.source.isDirtyFlag = true;
427                     r.store.remove(r);
428                     this.store.insert(insertAt++, r);
429                 }
430                 this.isDirtyFlag = true;
431             }
432         }
433         this.dragZone.cachedTarget = null;
434         return true;
435     },
436     
437     /** Decide whether to drop above or below a View node. */
438     getDropPoint : function(e, n, dd)
439     {
440         if (n == this.bodyEl.dom) {
441                 return "above";
442         }
443         var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
444         var c = t + (b - t) / 2;
445         var y = Roo.lib.Event.getPageY(e);
446         if(y <= c) {
447                 return "above";
448         }else{
449                 return "below";
450         }
451     },
452     onToggleCollapse : function(e)
453     {
454         if (this.collapsed) {
455             this.el.select('.roo-collapse-toggle').removeClass('collapsed');
456             this.el.select('.roo-collapse').addClass('show');
457             this.collapsed = false;
458             return;
459         }
460         this.el.select('.roo-collapse-toggle').addClass('collapsed');
461         this.el.select('.roo-collapse').removeClass('show');
462         this.collapsed = true;
463         
464         
465     }
466
467     
468 });
469