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