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.Element} this
84 * @param {Roo.Element} n the node being dropped?
85 * @param {Boolean} rotate status
93 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component, {
98 margin: '', /// may be better in component?
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, //
144 layoutCls : function()
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
152 if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
153 cls += ' m' + (v.length ? v[0] : '') + '-' + t['margin' + (v.length ? '_' : '') + v];
155 if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
156 cls += ' p' + (v.length ? v[0] : '') + '-' + t['padding' + (v.length ? '_' : '') + v];
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]
166 // more generic support?
174 // Roo.log("Call onRender: " + this.xtype);
175 /* We are looking at something like this.
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>
182 >> this bit is really the body...
183 <div> << we will ad dthis in hopefully it will not break shit.
185 ** card text does not actually have any styling...
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>
190 <a href="#" class="card-link">Card link</a>
193 <div class="card-footer">
194 <small class="text-muted">Last updated 3 mins ago</small>
198 getAutoCreate : function(){
206 if (this.weight.length && this.weight != 'light') {
207 cfg.cls += ' text-white';
209 cfg.cls += ' text-dark'; // need as it's nested..
211 if (this.weight.length) {
212 cfg.cls += ' bg-' + this.weight;
215 cfg.cls += ' ' + this.layoutCls();
219 if (this.header.length) {
221 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
222 cls : 'card-header ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
230 cls : 'card-header d-none ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
236 if (this.collapsable) {
239 cls : 'd-block user-select-none',
243 cls : 'roo-collapse-toggle fa fa-chevron-down float-right ' + (this.collapsed ? 'collapsed' : '')
248 hdr.cn.push(hdr_ctr);
253 cls: 'roo-card-header-ctr' + ( this.header.length ? '' : ' d-none'),
258 if (this.header_image.length) {
261 cls : 'card-img-top',
262 src: this.header_image // escape?
267 cls : 'card-img-top d-none'
273 cls : 'card-body' + (this.html === false ? ' d-none' : ''),
277 if (this.collapsable || this.rotateable) {
280 cls : 'roo-collapsable collapse ' + (this.collapsed || this.rotated ? '' : 'show'),
287 if (this.title.length) {
291 src: this.title // escape?
295 if (this.subtitle.length) {
299 src: this.subtitle // escape?
305 cls : 'roo-card-body-ctr'
308 if (this.html.length) {
314 // fixme ? handle objects?
316 if (this.footer.length) {
319 cls : 'card-footer ' + (this.rotated ? 'd-none' : ''),
324 cfg.cn.push({cls : 'card-footer d-none'});
333 getCardHeader : function()
335 var ret = this.el.select('.card-header',true).first();
336 if (ret.hasClass('d-none')) {
337 ret.removeClass('d-none');
342 getCardFooter : function()
344 var ret = this.el.select('.card-footer',true).first();
345 if (ret.hasClass('d-none')) {
346 ret.removeClass('d-none');
351 getCardImageTop : function()
353 var ret = this.el.select('.card-img-top',true).first();
354 if (ret.hasClass('d-none')) {
355 ret.removeClass('d-none');
361 getChildContainer : function()
367 return this.el.select('.roo-card-body-ctr',true).first();
370 initEvents: function()
372 this.bodyEl = this.el.select('.card-body',true).first();
373 this.containerEl = this.getChildContainer();
375 this.dragZone = new Roo.dd.DragZone(this.getEl(), {
376 containerScroll: true,
377 ddGroup: this.drag_group || 'default_card_drag_group'
379 this.dragZone.getDragData = this.getDragData.createDelegate(this);
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'
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);
393 if (this.collapsable) {
394 this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
396 if (this.rotateable) {
397 this.el.select('.card-header',true).on('click', this.onToggleRotate, this);
399 this.collapsableEl = this.el.select('.roo-collapsable').first();
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();
407 this.el.addClass('roo-card-rotated');
408 this.fireEvent('rotate', this, true);
412 getDragData : function(e)
414 var target = this.getEl();
416 //this.handleSelection(e);
426 dragData.ddel = target.dom ; // the div element
427 Roo.log(target.getWidth( ));
428 dragData.ddel.style.width = target.getWidth() + 'px';
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.
438 getTargetFromEvent : function(e, dragged_card_el)
440 var target = e.getTarget();
441 while ((target !== null) && (target.parentNode != this.containerEl.dom)) {
442 target = target.parentNode;
453 //Roo.log([ 'target' , target ? target.id : '--nothing--']);
454 // see if target is one of the 'cards'...
457 //Roo.log(this.items.length);
462 for (var i = 0;i< this.items.length;i++) {
464 if (!this.items[i].el.hasClass('card')) {
467 pos = this.getDropPoint(e, this.items[i].el.dom);
469 cards_len = ret.cards.length;
470 //Roo.log(this.items[i].el.dom.id);
471 ret.cards.push(this.items[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];
480 if (!ret.cards.length) {
482 ret.position = 'below';
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';
494 if (this.items[ret.items_n].el == dragged_card_el) {
498 if (ret.position == 'below') {
499 var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
501 if (card_after && card_after.el == dragged_card_el) {
508 var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
510 if (card_before && card_before.el == dragged_card_el) {
517 onNodeEnter : function(n, dd, e, data){
520 onNodeOver : function(n, dd, e, data)
523 var target_info = this.getTargetFromEvent(e,data.source.el);
524 if (target_info === false) {
525 this.dropPlaceHolder('hide');
528 Roo.log(['getTargetFromEvent', target_info ]);
531 this.dropPlaceHolder('show', target_info,data);
535 onNodeOut : function(n, dd, e, data){
536 this.dropPlaceHolder('hide');
539 onNodeDrop : function(n, dd, e, data)
542 // call drop - return false if
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.
549 var info = this.getTargetFromEvent(e,data.source.el);
550 if (info === false) {
553 this.dropPlaceHolder('hide');
559 this.acceptCard(data.source, info.position, info.card, info.items_n);
563 firstChildCard : function()
565 for (var i = 0;i< this.items.length;i++) {
567 if (!this.items[i].el.hasClass('card')) {
570 return this.items[i];
572 return this.items.length ? this.items[this.items.length-1] : false; // don't try and put stuff after the cards...
577 * - card.acceptCard(move_card, info.position, info.card, info.items_n);
579 acceptCard : function(move_card, position, next_to_card )
581 if (this.fireEvent("drop", this, move_card, position, next_to_card) === false) {
585 var to_items_n = next_to_card ? this.items.indexOf(next_to_card) : 0;
587 move_card.parent().removeCard(move_card);
590 var dom = move_card.el.dom;
591 dom.style.width = ''; // clear with - which is set by drag.
593 if (next_to_card !== false && next_to_card !== true && next_to_card.el.dom.parentNode) {
594 var cardel = next_to_card.el.dom;
596 if (position == 'above' ) {
597 cardel.parentNode.insertBefore(dom, cardel);
598 } else if (cardel.nextSibling) {
599 cardel.parentNode.insertBefore(dom,cardel.nextSibling);
601 cardel.parentNode.append(dom);
605 this.containerEl.dom.append(dom);
608 //FIXME HANDLE card = true
610 // add this to the correct place in items.
612 // remove Card from items.
615 if (this.items.length) {
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);
622 nitems.push(this.items[i]);
623 if (i == to_items_n && position == 'below') {
624 nitems.push(move_card);
630 this.items.push(move_card);
633 move_card.parentId = this.id;
639 removeCard : function(c)
641 this.items = this.items.filter(function(e) { return e != c });
644 dom.parentNode.removeChild(dom);
645 dom.style.width = ''; // clear with - which is set by drag.
650 /** Decide whether to drop above or below a View node. */
651 getDropPoint : function(e, n, dd)
656 if (n == this.containerEl.dom) {
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);
668 onToggleCollapse : function(e)
670 if (this.collapsed) {
671 this.el.select('.roo-collapse-toggle').removeClass('collapsed');
672 this.collapsableEl.addClass('show');
673 this.collapsed = false;
676 this.el.select('.roo-collapse-toggle').addClass('collapsed');
677 this.collapsableEl.removeClass('show');
678 this.collapsed = true;
683 onToggleRotate : function(e)
685 this.collapsableEl.removeClass('show');
686 this.footerEl.removeClass('d-none');
687 this.el.removeClass('roo-card-rotated');
688 this.el.removeClass('d-none');
691 this.collapsableEl.addClass('show');
692 this.rotated = false;
693 this.fireEvent('rotate', this, this.rotated);
696 this.el.addClass('roo-card-rotated');
697 this.footerEl.addClass('d-none');
698 this.el.select('.roo-collapsable').removeClass('show');
701 this.fireEvent('rotate', this, this.rotated);
705 dropPlaceHolder: function (action, info, data)
707 if (this.dropEl === false) {
708 this.dropEl = Roo.DomHelper.append(this.containerEl, {
712 this.dropEl.removeClass(['d-none', 'd-block']);
713 if (action == 'hide') {
715 this.dropEl.addClass('d-none');
718 // FIXME - info.card == true!!!
719 this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
721 if (info.card !== true) {
722 var cardel = info.card.el.dom;
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);
729 cardel.parentNode.append(this.dropEl.dom);
733 this.containerEl.dom.append(this.dropEl.dom);
736 this.dropEl.addClass('d-block roo-card-dropzone');
738 this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
745 setHeaderText: function(html)
747 this.headerContainerEl.dom.innerHTML = html;