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