header text update
[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  * @cfg {Number} header_weight  (primary|secondary|success|info|warning|danger|light|dark)
18  * 
19  * @cfg {String} title
20  * @cfg {String} subtitle
21  * @cfg {String|Boolean} html -- html contents - or just use children.. use false to hide it..
22  * @cfg {String} footer
23  
24  * @cfg {String} weight (primary|warning|info|danger|secondary|success|light|dark)
25  * 
26  * @cfg {String} margin (0|1|2|3|4|5|auto)
27  * @cfg {String} margin_top (0|1|2|3|4|5|auto)
28  * @cfg {String} margin_bottom (0|1|2|3|4|5|auto)
29  * @cfg {String} margin_left (0|1|2|3|4|5|auto)
30  * @cfg {String} margin_right (0|1|2|3|4|5|auto)
31  * @cfg {String} margin_x (0|1|2|3|4|5|auto)
32  * @cfg {String} margin_y (0|1|2|3|4|5|auto)
33  *
34  * @cfg {String} padding (0|1|2|3|4|5)
35  * @cfg {String} padding_top (0|1|2|3|4|5)next_to_card
36  * @cfg {String} padding_bottom (0|1|2|3|4|5)
37  * @cfg {String} padding_left (0|1|2|3|4|5)
38  * @cfg {String} padding_right (0|1|2|3|4|5)
39  * @cfg {String} padding_x (0|1|2|3|4|5)
40  * @cfg {String} padding_y (0|1|2|3|4|5)
41  *
42  * @cfg {String} display (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
43  * @cfg {String} display_xs (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
44  * @cfg {String} display_sm (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
45  * @cfg {String} display_lg (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
46  * @cfg {String} display_xl (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
47  
48  * @config {Boolean} dragable  if this card can be dragged.
49  * @config {String} drag_group  group for drag
50  * @config {Boolean} dropable  if this card can recieve other cards being dropped onto it..
51  * @config {String} drop_group  group for drag
52  * 
53  * @config {Boolean} collapsable can the body be collapsed.
54  * @config {Boolean} collapsed is the body collapsed when rendered...
55  * @config {Boolean} rotateable can the body be rotated by clicking on it..
56  * @config {Boolean} rotated is the body rotated when rendered...
57  * 
58  * @constructor
59  * Create a new Container
60  * @param {Object} config The config object
61  */
62
63 Roo.bootstrap.Card = function(config){
64     Roo.bootstrap.Card.superclass.constructor.call(this, config);
65     
66     this.addEvents({
67          // raw events
68         /**
69          * @event drop
70          * When a element a card is dropped
71          * @param {Roo.bootstrap.Card} this
72          *
73          * 
74          * @param {Roo.bootstrap.Card} move_card the card being dropped?
75          * @param {String} position 'above' or 'below'
76          * @param {Roo.bootstrap.Card} next_to_card What card position is relative to of 'false' for empty list.
77         
78          */
79         'drop' : true,
80          /**
81          * @event rotate
82          * When a element a card is rotate
83          * @param {Roo.bootstrap.Element} this
84          * @param {Roo.Element} n the node being dropped?
85          * @param {Boolean} rotate status
86          */
87         'rotate' : true
88         
89     });
90 };
91
92
93 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component,  {
94     
95     
96     weight : '',
97     
98     margin: '', /// may be better in component?
99     margin_top: '', 
100     margin_bottom: '', 
101     margin_left: '',
102     margin_right: '',
103     margin_x: '',
104     margin_y: '',
105     
106     padding : '',
107     padding_top: '', 
108     padding_bottom: '', 
109     padding_left: '',
110     padding_right: '',
111     padding_x: '',
112     padding_y: '',
113     
114     display: '', 
115     display_xs: '', 
116     display_sm: '', 
117     display_lg: '',
118     display_xl: '',
119  
120     header_image  : '',
121     header : '',
122     header_size : 0,
123     title : '',
124     subtitle : '',
125     html : '',
126     footer: '',
127
128     collapsable : false,
129     collapsed : false,
130     rotateable : false,
131     rotated : false,
132     
133     dragable : false,
134     drag_group : false,
135     dropable : false,
136     drop_group : false,
137     childContainer : false,
138     dropEl : false, /// the dom placeholde element that indicates drop location.
139     containerEl: false, // body container
140     bodyEl: false, // card-body
141     headerContainerEl : false, //
142     headerEl : false,
143     
144     layoutCls : function()
145     {
146         var cls = '';
147         var t = this;
148         Roo.log(this.margin_bottom.length);
149         ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
150             // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
151             
152             if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
153                 cls += ' m' +  (v.length ? v[0]  : '') + '-' +  t['margin' + (v.length ? '_' : '') + v];
154             }
155             if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
156                 cls += ' p' +  (v.length ? v[0]  : '') + '-' +  t['padding' + (v.length ? '_' : '') + v];
157             }
158         });
159         
160         ['', 'xs', 'sm', 'lg', 'xl'].forEach(function(v) {
161             if (('' + t['display' + (v.length ? '_' : '') + v]).length) {
162                 cls += ' d' +  (v.length ? '-' : '') + v + '-' + t['display' + (v.length ? '_' : '') + v]
163             }
164         });
165         
166         // more generic support?
167         if (this.hidden) {
168             cls += ' d-none';
169         }
170         
171         return cls;
172     },
173  
174        // Roo.log("Call onRender: " + this.xtype);
175         /*  We are looking at something like this.
176 <div class="card">
177     <img src="..." class="card-img-top" alt="...">
178     <div class="card-body">
179         <h5 class="card-title">Card title</h5>
180          <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
181
182         >> this bit is really the body...
183         <div> << we will ad dthis in hopefully it will not break shit.
184         
185         ** card text does not actually have any styling...
186         
187             <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>
188         
189         </div> <<
190           <a href="#" class="card-link">Card link</a>
191           
192     </div>
193     <div class="card-footer">
194         <small class="text-muted">Last updated 3 mins ago</small>
195     </div>
196 </div>
197          */
198     getAutoCreate : function(){
199         
200         var cfg = {
201             tag : 'div',
202             cls : 'card',
203             cn : [ ]
204         };
205         
206         if (this.weight.length && this.weight != 'light') {
207             cfg.cls += ' text-white';
208         } else {
209             cfg.cls += ' text-dark'; // need as it's nested..
210         }
211         if (this.weight.length) {
212             cfg.cls += ' bg-' + this.weight;
213         }
214         
215         cfg.cls += ' ' + this.layoutCls(); 
216         
217         var hdr = false;
218         var hdr_ctr = false;
219         if (this.header.length) {
220             hdr = {
221                 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
222                 cls : 'card-header ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
223                 cn : []
224             };
225             cfg.cn.push(hdr);
226             hdr_ctr = hdr;
227         } else {
228             hdr = {
229                 tag : 'div',
230                 cls : 'card-header d-none ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
231                 cn : []
232             };
233             cfg.cn.push(hdr);
234             hdr_ctr = hdr;
235         }
236         if (this.collapsable) {
237             hdr_ctr = {
238             tag : 'a',
239             cls : 'd-block user-select-none',
240             cn: [
241                     {
242                         tag: 'i',
243                         cls : 'roo-collapse-toggle fa fa-chevron-down float-right ' + (this.collapsed ? 'collapsed' : '')
244                     }
245                    
246                 ]
247             };
248             hdr.cn.push(hdr_ctr);
249         }
250         
251         hdr_ctr.cn.push(        {
252             tag: 'span',
253             cls: 'roo-card-header-ctr' + ( this.header.length ? '' : ' d-none'),
254             html : this.header
255         });
256         
257         
258         if (this.header_image.length) {
259             cfg.cn.push({
260                 tag : 'img',
261                 cls : 'card-img-top',
262                 src: this.header_image // escape?
263             });
264         } else {
265             cfg.cn.push({
266                     tag : 'div',
267                     cls : 'card-img-top d-none' 
268                 });
269         }
270             
271         var body = {
272             tag : 'div',
273             cls : 'card-body' + (this.html === false  ? ' d-none' : ''),
274             cn : []
275         };
276         var obody = body;
277         if (this.collapsable || this.rotateable) {
278             obody = {
279                 tag: 'div',
280                 cls : 'roo-collapsable collapse ' + (this.collapsed || this.rotated ? '' : 'show'),
281                 cn : [  body ]
282             };
283         }
284         
285         cfg.cn.push(obody);
286         
287         if (this.title.length) {
288             body.cn.push({
289                 tag : 'div',
290                 cls : 'card-title',
291                 src: this.title // escape?
292             });
293         }  
294         
295         if (this.subtitle.length) {
296             body.cn.push({
297                 tag : 'div',
298                 cls : 'card-title',
299                 src: this.subtitle // escape?
300             });
301         }
302         
303         body.cn.push({
304             tag : 'div',
305             cls : 'roo-card-body-ctr'
306         });
307         
308         if (this.html.length) {
309             body.cn.push({
310                 tag: 'div',
311                 html : this.html
312             });
313         }
314         // fixme ? handle objects?
315         
316         if (this.footer.length) {
317            
318             cfg.cn.push({
319                 cls : 'card-footer ' + (this.rotated ? 'd-none' : ''),
320                 html : this.footer
321             });
322             
323         } else {
324             cfg.cn.push({cls : 'card-footer d-none'});
325         }
326         
327         // footer...
328         
329         return cfg;
330     },
331     
332     
333     getCardHeader : function()
334     {
335         var  ret = this.el.select('.card-header',true).first();
336         if (ret.hasClass('d-none')) {
337             ret.removeClass('d-none');
338         }
339         
340         return ret;
341     },
342     getCardFooter : function()
343     {
344         var  ret = this.el.select('.card-footer',true).first();
345         if (ret.hasClass('d-none')) {
346             ret.removeClass('d-none');
347         }
348         
349         return ret;
350     },
351     getCardImageTop : function()
352     {
353         var  ret = this.el.select('.card-img-top',true).first();
354         if (ret.hasClass('d-none')) {
355             ret.removeClass('d-none');
356         }
357             
358         return ret;
359     },
360     
361     getChildContainer : function()
362     {
363         
364         if(!this.el){
365             return false;
366         }
367         return this.el.select('.roo-card-body-ctr',true).first();    
368     },
369     
370     initEvents: function() 
371     {
372         this.bodyEl = this.el.select('.card-body',true).first(); 
373         this.containerEl = this.getChildContainer();
374         if(this.dragable){
375             this.dragZone = new Roo.dd.DragZone(this.getEl(), {
376                     containerScroll: true,
377                     ddGroup: this.drag_group || 'default_card_drag_group'
378             });
379             this.dragZone.getDragData = this.getDragData.createDelegate(this);
380         }
381         if (this.dropable) {
382             this.dropZone = new Roo.dd.DropZone(this.el.select('.card-body',true).first() , {
383                 containerScroll: true,
384                 ddGroup: this.drop_group || 'default_card_drag_group'
385             });
386             this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
387             this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
388             this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
389             this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
390             this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
391         }
392         
393         if (this.collapsable) {
394             this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
395         }
396         if (this.rotateable) {
397             this.el.select('.card-header',true).on('click', this.onToggleRotate, this);
398         }
399         this.collapsableEl = this.el.select('.roo-collapsable').first();
400          
401         this.footerEl = this.el.select('.card-footer').first();
402         this.collapsableToggleEl = this.el.select('.roo-collapse-toggle');
403         this.headerContainerEl = this.el.select('.roo-card-header-ctr').first();
404         this.headerEl = this.el.select('.card-header',true).first();
405         
406         if (this.rotated) {
407             this.el.addClass('roo-card-rotated');
408             this.fireEvent('rotate', this, true);
409         }
410         
411     },
412     getDragData : function(e)
413     {
414         var target = this.getEl();
415         if (target) {
416             //this.handleSelection(e);
417             
418             var dragData = {
419                 source: this,
420                 copy: false,
421                 nodes: this.getEl(),
422                 records: []
423             };
424             
425             
426             dragData.ddel = target.dom ;    // the div element
427             Roo.log(target.getWidth( ));
428             dragData.ddel.style.width = target.getWidth() + 'px';
429             
430             return dragData;
431         }
432         return false;
433     },
434     /**
435     *    Part of the Roo.dd.DropZone interface. If no target node is found, the
436     *    whole Element becomes the target, and this causes the drop gesture to append.
437     */
438     getTargetFromEvent : function(e, dragged_card_el)
439     {
440         var target = e.getTarget();
441         while ((target !== null) && (target.parentNode != this.containerEl.dom)) {
442             target = target.parentNode;
443         }
444         
445         var ret = {
446             position: '',
447             cards : [],
448             card_n : -1,
449             items_n : -1,
450             card : false 
451         };
452         
453         //Roo.log([ 'target' , target ? target.id : '--nothing--']);
454         // see if target is one of the 'cards'...
455         
456         
457         //Roo.log(this.items.length);
458         var pos = false;
459         
460         var last_card_n = 0;
461         var cards_len  = 0;
462         for (var i = 0;i< this.items.length;i++) {
463             
464             if (!this.items[i].el.hasClass('card')) {
465                  continue;
466             }
467             pos = this.getDropPoint(e, this.items[i].el.dom);
468             
469             cards_len = ret.cards.length;
470             //Roo.log(this.items[i].el.dom.id);
471             ret.cards.push(this.items[i]);
472             last_card_n  = i;
473             if (ret.card_n < 0 && pos == 'above') {
474                 ret.position = cards_len > 0 ? 'below' : pos;
475                 ret.items_n = i > 0 ? i - 1 : 0;
476                 ret.card_n  = cards_len  > 0 ? cards_len - 1 : 0;
477                 ret.card = ret.cards[ret.card_n];
478             }
479         }
480         if (!ret.cards.length) {
481             ret.card = true;
482             ret.position = 'below';
483             ret.items_n;
484             return ret;
485         }
486         // could not find a card.. stick it at the end..
487         if (ret.card_n < 0) {
488             ret.card_n = last_card_n;
489             ret.card = ret.cards[last_card_n];
490             ret.items_n = this.items.indexOf(ret.cards[last_card_n]);
491             ret.position = 'below';
492         }
493         
494         if (this.items[ret.items_n].el == dragged_card_el) {
495             return false;
496         }
497         
498         if (ret.position == 'below') {
499             var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
500             
501             if (card_after  && card_after.el == dragged_card_el) {
502                 return false;
503             }
504             return ret;
505         }
506         
507         // its's after ..
508         var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
509         
510         if (card_before  && card_before.el == dragged_card_el) {
511             return false;
512         }
513         
514         return ret;
515     },
516     
517     onNodeEnter : function(n, dd, e, data){
518         return false;
519     },
520     onNodeOver : function(n, dd, e, data)
521     {
522        
523         var target_info = this.getTargetFromEvent(e,data.source.el);
524         if (target_info === false) {
525             this.dropPlaceHolder('hide');
526             return false;
527         }
528         Roo.log(['getTargetFromEvent', target_info ]);
529         
530          
531         this.dropPlaceHolder('show', target_info,data);
532         
533         return false; 
534     },
535     onNodeOut : function(n, dd, e, data){
536         this.dropPlaceHolder('hide');
537      
538     },
539     onNodeDrop : function(n, dd, e, data)
540     {
541         
542         // call drop - return false if
543         
544         // this could actually fail - if the Network drops..
545         // we will ignore this at present..- client should probably reload
546         // the whole set of cards if stuff like that fails.
547         
548         
549         var info = this.getTargetFromEvent(e,data.source.el);
550         if (info === false) {
551             return false;
552         }
553         this.dropPlaceHolder('hide');
554   
555          
556     
557     
558     
559         this.acceptCard(data.source, info.position, info.card, info.items_n);
560         return true;
561          
562     },
563     firstChildCard : function()
564     {
565         for (var i = 0;i< this.items.length;i++) {
566             
567             if (!this.items[i].el.hasClass('card')) {
568                  continue;
569             }
570             return this.items[i];
571         }
572         return this.items.length ? this.items[this.items.length-1] : false; // don't try and put stuff after the cards...
573     },
574     /**
575      * accept card
576      *
577      * -        card.acceptCard(move_card, info.position, info.card, info.items_n);
578      */
579     acceptCard : function(move_card,  position, next_to_card )
580     {
581         if (this.fireEvent("drop", this, move_card, position, next_to_card) === false) {
582             return false;
583         }
584         
585         var to_items_n = next_to_card ? this.items.indexOf(next_to_card) : 0;
586         
587         move_card.parent().removeCard(move_card);
588         
589         
590         var dom = move_card.el.dom;
591         dom.style.width = ''; // clear with - which is set by drag.
592         
593         if (next_to_card !== false && next_to_card !== true && next_to_card.el.dom.parentNode) {
594             var cardel = next_to_card.el.dom;
595             
596             if (position == 'above' ) {
597                 cardel.parentNode.insertBefore(dom, cardel);
598             } else if (cardel.nextSibling) {
599                 cardel.parentNode.insertBefore(dom,cardel.nextSibling);
600             } else {
601                 cardel.parentNode.append(dom);
602             }
603         } else {
604             // card container???
605             this.containerEl.dom.append(dom);
606         }
607         
608         //FIXME HANDLE card = true 
609         
610         // add this to the correct place in items.
611         
612         // remove Card from items.
613         
614        
615         if (this.items.length) {
616             var nitems = [];
617             //Roo.log([info.items_n, info.position, this.items.length]);
618             for (var i =0; i < this.items.length; i++) {
619                 if (i == to_items_n && position == 'above') {
620                     nitems.push(move_card);
621                 }
622                 nitems.push(this.items[i]);
623                 if (i == to_items_n && position == 'below') {
624                     nitems.push(move_card);
625                 }
626             }
627             this.items = nitems;
628             Roo.log(this.items);
629         } else {
630             this.items.push(move_card);
631         }
632         
633         move_card.parentId = this.id;
634         
635         return true;
636         
637         
638     },
639     removeCard : function(c)
640     {
641         this.items = this.items.filter(function(e) { return e != c });
642  
643         var dom = c.el.dom;
644         dom.parentNode.removeChild(dom);
645         dom.style.width = ''; // clear with - which is set by drag.
646         c.parentId = false;
647         
648     },
649     
650     /**    Decide whether to drop above or below a View node. */
651     getDropPoint : function(e, n, dd)
652     {
653         if (dd) {
654              return false;
655         }
656         if (n == this.containerEl.dom) {
657             return "above";
658         }
659         var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
660         var c = t + (b - t) / 2;
661         var y = Roo.lib.Event.getPageY(e);
662         if(y <= c) {
663             return "above";
664         }else{
665             return "below";
666         }
667     },
668     onToggleCollapse : function(e)
669         {
670         if (this.collapsed) {
671             this.el.select('.roo-collapse-toggle').removeClass('collapsed');
672             this.collapsableEl.addClass('show');
673             this.collapsed = false;
674             return;
675         }
676         this.el.select('.roo-collapse-toggle').addClass('collapsed');
677         this.collapsableEl.removeClass('show');
678         this.collapsed = true;
679         
680     
681     },
682     
683     onToggleRotate : function(e)
684     {
685         this.collapsableEl.removeClass('show');
686         this.footerEl.removeClass('d-none');
687         this.el.removeClass('roo-card-rotated');
688         this.el.removeClass('d-none');
689         if (this.rotated) {
690             
691             this.collapsableEl.addClass('show');
692             this.rotated = false;
693             this.fireEvent('rotate', this, this.rotated);
694             return;
695         }
696         this.el.addClass('roo-card-rotated');
697         this.footerEl.addClass('d-none');
698         this.el.select('.roo-collapsable').removeClass('show');
699         
700         this.rotated = true;
701         this.fireEvent('rotate', this, this.rotated);
702     
703     },
704     
705     dropPlaceHolder: function (action, info, data)
706     {
707         if (this.dropEl === false) {
708             this.dropEl = Roo.DomHelper.append(this.containerEl, {
709             cls : 'd-none'
710             },true);
711         }
712         this.dropEl.removeClass(['d-none', 'd-block']);        
713         if (action == 'hide') {
714             
715             this.dropEl.addClass('d-none');
716             return;
717         }
718         // FIXME - info.card == true!!!
719         this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
720         
721         if (info.card !== true) {
722             var cardel = info.card.el.dom;
723             
724             if (info.position == 'above') {
725                 cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
726             } else if (cardel.nextSibling) {
727                 cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
728             } else {
729                 cardel.parentNode.append(this.dropEl.dom);
730             }
731         } else {
732             // card container???
733             this.containerEl.dom.append(this.dropEl.dom);
734         }
735         
736         this.dropEl.addClass('d-block roo-card-dropzone');
737         
738         this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
739         
740         
741     
742     
743     
744     },
745     setHeaderText: function(html)
746     {
747         this.header = html;
748         if (this.headerContainerEl) {
749             this.headerContainerEl.dom.innerHTML = html;
750         }
751     }
752
753     
754 });
755