4 * This is BS4's Card element.. - similar to our containers probably..
8 * @class Roo.bootstrap.Card
9 * @extends Roo.bootstrap.Component
10 * Bootstrap Card class
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)
20 * @cfg {String} subtitle
21 * @cfg {String|Boolean} html -- html contents - or just use children.. use false to hide it..
22 * @cfg {String} footer
24 * @cfg {String} weight (primary|warning|info|danger|secondary|success|light|dark)
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)
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)
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)
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
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...
59 * Create a new Container
60 * @param {Object} config The config object
63 Roo.bootstrap.Card = function(config){
64 Roo.bootstrap.Card.superclass.constructor.call(this, config);
70 * When a element a card is dropped
71 * @param {Roo.bootstrap.Card} this
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.
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
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
100 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component, {
105 margin: '', /// may be better in component?
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, //
150 header_imageEl : false,
153 layoutCls : function()
157 Roo.log(this.margin_bottom.length);
158 ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
159 // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
161 if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
162 cls += ' m' + (v.length ? v[0] : '') + '-' + t['margin' + (v.length ? '_' : '') + v];
164 if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
165 cls += ' p' + (v.length ? v[0] : '') + '-' + t['padding' + (v.length ? '_' : '') + v];
169 ['', 'xs', 'sm', 'lg', 'xl'].forEach(function(v) {
170 if (('' + t['display' + (v.length ? '_' : '') + v]).length) {
171 cls += ' d' + (v.length ? '-' : '') + v + '-' + t['display' + (v.length ? '_' : '') + v]
175 // more generic support?
183 // Roo.log("Call onRender: " + this.xtype);
184 /* We are looking at something like this.
186 <img src="..." class="card-img-top" alt="...">
187 <div class="card-body">
188 <h5 class="card-title">Card title</h5>
189 <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
191 >> this bit is really the body...
192 <div> << we will ad dthis in hopefully it will not break shit.
194 ** card text does not actually have any styling...
196 <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>
199 <a href="#" class="card-link">Card link</a>
202 <div class="card-footer">
203 <small class="text-muted">Last updated 3 mins ago</small>
207 getAutoCreate : function(){
215 if (this.weight.length && this.weight != 'light') {
216 cfg.cls += ' text-white';
218 cfg.cls += ' text-dark'; // need as it's nested..
220 if (this.weight.length) {
221 cfg.cls += ' bg-' + this.weight;
224 cfg.cls += ' ' + this.layoutCls();
228 if (this.header.length) {
230 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
231 cls : 'card-header ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
239 cls : 'card-header d-none ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
245 if (this.collapsable) {
248 cls : 'd-block user-select-none',
252 cls : 'roo-collapse-toggle fa fa-chevron-down float-right ' + (this.collapsed ? 'collapsed' : '')
257 hdr.cn.push(hdr_ctr);
262 cls: 'roo-card-header-ctr' + ( this.header.length ? '' : ' d-none'),
267 if (this.header_image.length) {
270 cls : 'card-img-top',
271 src: this.header_image // escape?
276 cls : 'card-img-top d-none'
282 cls : 'card-body' + (this.html === false ? ' d-none' : ''),
286 if (this.collapsable || this.rotateable) {
289 cls : 'roo-collapsable collapse ' + (this.collapsed || this.rotated ? '' : 'show'),
296 if (this.title.length) {
300 src: this.title // escape?
304 if (this.subtitle.length) {
308 src: this.subtitle // escape?
314 cls : 'roo-card-body-ctr'
317 if (this.html.length) {
323 // fixme ? handle objects?
325 if (this.footer.length) {
328 cls : 'card-footer ' + (this.rotated ? 'd-none' : ''),
333 cfg.cn.push({cls : 'card-footer d-none'});
342 getCardHeader : function()
344 var ret = this.el.select('.card-header',true).first();
345 if (ret.hasClass('d-none')) {
346 ret.removeClass('d-none');
351 getCardFooter : function()
353 var ret = this.el.select('.card-footer',true).first();
354 if (ret.hasClass('d-none')) {
355 ret.removeClass('d-none');
360 getCardImageTop : function()
362 var ret = this.header_imageEl;
363 if (ret.hasClass('d-none')) {
364 ret.removeClass('d-none');
370 getChildContainer : function()
376 return this.el.select('.roo-card-body-ctr',true).first();
379 initEvents: function()
381 this.bodyEl = this.el.select('.card-body',true).first();
382 this.containerEl = this.getChildContainer();
384 this.dragZone = new Roo.dd.DragZone(this.getEl(), {
385 containerScroll: true,
386 ddGroup: this.drag_group || 'default_card_drag_group'
388 this.dragZone.getDragData = this.getDragData.createDelegate(this);
391 this.dropZone = new Roo.dd.DropZone(this.el.select('.card-body',true).first() , {
392 containerScroll: true,
393 ddGroup: this.drop_group || 'default_card_drag_group'
395 this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
396 this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
397 this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
398 this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
399 this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
402 if (this.collapsable) {
403 this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
405 if (this.rotateable) {
406 this.el.select('.card-header',true).on('click', this.onToggleRotate, this);
408 this.collapsableEl = this.el.select('.roo-collapsable',true).first();
410 this.footerEl = this.el.select('.card-footer',true).first();
411 this.collapsableToggleEl = this.el.select('.roo-collapse-toggle',true).first();
412 this.headerContainerEl = this.el.select('.roo-card-header-ctr',true).first();
413 this.headerEl = this.el.select('.card-header',true).first();
416 this.el.addClass('roo-card-rotated');
417 this.fireEvent('rotate', this, true);
419 this.header_imageEl = this.el.select('.card-img-top',true).first();
420 this.header_imageEl.on('load', this.onHeaderImageLoad, this );
423 getDragData : function(e)
425 var target = this.getEl();
427 //this.handleSelection(e);
437 dragData.ddel = target.dom ; // the div element
438 Roo.log(target.getWidth( ));
439 dragData.ddel.style.width = target.getWidth() + 'px';
446 * Part of the Roo.dd.DropZone interface. If no target node is found, the
447 * whole Element becomes the target, and this causes the drop gesture to append.
452 position : 'below' or 'above'
453 card : relateive to card OBJECT (or true for no cards listed)
454 items_n : relative to nth item in list
455 card_n : relative to nth card in list
460 getTargetFromEvent : function(e, dragged_card_el)
462 var target = e.getTarget();
463 while ((target !== null) && (target.parentNode != this.containerEl.dom)) {
464 target = target.parentNode;
475 //Roo.log([ 'target' , target ? target.id : '--nothing--']);
476 // see if target is one of the 'cards'...
479 //Roo.log(this.items.length);
484 for (var i = 0;i< this.items.length;i++) {
486 if (!this.items[i].el.hasClass('card')) {
489 pos = this.getDropPoint(e, this.items[i].el.dom);
491 cards_len = ret.cards.length;
492 //Roo.log(this.items[i].el.dom.id);
493 ret.cards.push(this.items[i]);
495 if (ret.card_n < 0 && pos == 'above') {
496 ret.position = cards_len > 0 ? 'below' : pos;
497 ret.items_n = i > 0 ? i - 1 : 0;
498 ret.card_n = cards_len > 0 ? cards_len - 1 : 0;
499 ret.card = ret.cards[ret.card_n];
502 if (!ret.cards.length) {
504 ret.position = 'below';
508 // could not find a card.. stick it at the end..
509 if (ret.card_n < 0) {
510 ret.card_n = last_card_n;
511 ret.card = ret.cards[last_card_n];
512 ret.items_n = this.items.indexOf(ret.cards[last_card_n]);
513 ret.position = 'below';
516 if (this.items[ret.items_n].el == dragged_card_el) {
520 if (ret.position == 'below') {
521 var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
523 if (card_after && card_after.el == dragged_card_el) {
530 var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
532 if (card_before && card_before.el == dragged_card_el) {
539 onNodeEnter : function(n, dd, e, data){
542 onNodeOver : function(n, dd, e, data)
545 var target_info = this.getTargetFromEvent(e,data.source.el);
546 if (target_info === false) {
547 this.dropPlaceHolder('hide');
550 Roo.log(['getTargetFromEvent', target_info ]);
553 if (this.fireEvent('cardover', this, [ data ]) === false) {
557 this.dropPlaceHolder('show', target_info,data);
561 onNodeOut : function(n, dd, e, data){
562 this.dropPlaceHolder('hide');
565 onNodeDrop : function(n, dd, e, data)
568 // call drop - return false if
570 // this could actually fail - if the Network drops..
571 // we will ignore this at present..- client should probably reload
572 // the whole set of cards if stuff like that fails.
575 var info = this.getTargetFromEvent(e,data.source.el);
576 if (info === false) {
579 this.dropPlaceHolder('hide');
583 this.acceptCard(data.source, info.position, info.card, info.items_n);
587 firstChildCard : function()
589 for (var i = 0;i< this.items.length;i++) {
591 if (!this.items[i].el.hasClass('card')) {
594 return this.items[i];
596 return this.items.length ? this.items[this.items.length-1] : false; // don't try and put stuff after the cards...
601 * - card.acceptCard(move_card, info.position, info.card, info.items_n);
603 acceptCard : function(move_card, position, next_to_card )
605 if (this.fireEvent("drop", this, move_card, position, next_to_card) === false) {
609 var to_items_n = next_to_card ? this.items.indexOf(next_to_card) : 0;
611 move_card.parent().removeCard(move_card);
614 var dom = move_card.el.dom;
615 dom.style.width = ''; // clear with - which is set by drag.
617 if (next_to_card !== false && next_to_card !== true && next_to_card.el.dom.parentNode) {
618 var cardel = next_to_card.el.dom;
620 if (position == 'above' ) {
621 cardel.parentNode.insertBefore(dom, cardel);
622 } else if (cardel.nextSibling) {
623 cardel.parentNode.insertBefore(dom,cardel.nextSibling);
625 cardel.parentNode.append(dom);
629 this.containerEl.dom.append(dom);
632 //FIXME HANDLE card = true
634 // add this to the correct place in items.
636 // remove Card from items.
639 if (this.items.length) {
641 //Roo.log([info.items_n, info.position, this.items.length]);
642 for (var i =0; i < this.items.length; i++) {
643 if (i == to_items_n && position == 'above') {
644 nitems.push(move_card);
646 nitems.push(this.items[i]);
647 if (i == to_items_n && position == 'below') {
648 nitems.push(move_card);
654 this.items.push(move_card);
657 move_card.parentId = this.id;
663 removeCard : function(c)
665 this.items = this.items.filter(function(e) { return e != c });
668 dom.parentNode.removeChild(dom);
669 dom.style.width = ''; // clear with - which is set by drag.
674 /** Decide whether to drop above or below a View node. */
675 getDropPoint : function(e, n, dd)
680 if (n == this.containerEl.dom) {
683 var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
684 var c = t + (b - t) / 2;
685 var y = Roo.lib.Event.getPageY(e);
692 onToggleCollapse : function(e)
694 if (this.collapsed) {
695 this.el.select('.roo-collapse-toggle').removeClass('collapsed');
696 this.collapsableEl.addClass('show');
697 this.collapsed = false;
700 this.el.select('.roo-collapse-toggle').addClass('collapsed');
701 this.collapsableEl.removeClass('show');
702 this.collapsed = true;
707 onToggleRotate : function(e)
709 this.collapsableEl.removeClass('show');
710 this.footerEl.removeClass('d-none');
711 this.el.removeClass('roo-card-rotated');
712 this.el.removeClass('d-none');
715 this.collapsableEl.addClass('show');
716 this.rotated = false;
717 this.fireEvent('rotate', this, this.rotated);
720 this.el.addClass('roo-card-rotated');
721 this.footerEl.addClass('d-none');
722 this.el.select('.roo-collapsable').removeClass('show');
725 this.fireEvent('rotate', this, this.rotated);
729 dropPlaceHolder: function (action, info, data)
731 if (this.dropEl === false) {
732 this.dropEl = Roo.DomHelper.append(this.containerEl, {
736 this.dropEl.removeClass(['d-none', 'd-block']);
737 if (action == 'hide') {
739 this.dropEl.addClass('d-none');
742 // FIXME - info.card == true!!!
743 this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
745 if (info.card !== true) {
746 var cardel = info.card.el.dom;
748 if (info.position == 'above') {
749 cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
750 } else if (cardel.nextSibling) {
751 cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
753 cardel.parentNode.append(this.dropEl.dom);
757 this.containerEl.dom.append(this.dropEl.dom);
760 this.dropEl.addClass('d-block roo-card-dropzone');
762 this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
769 setHeaderText: function(html)
772 if (this.headerContainerEl) {
773 this.headerContainerEl.dom.innerHTML = html;
776 onHeaderImageLoad : function(ev, he)
778 if (!this.header_image_fit_square) {
782 var hw = he.naturalHeight / he.naturalWidth;
785 //var w = he.dom.naturalWidth;
788 he.style.position = 'relative';
790 var nw = (ww * (1/hw));
791 Roo.get(he).setSize( ww * (1/hw), ww);
792 he.style.left = ((ww - nw)/ 2) + 'px';
793 he.style.position = 'relative';