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, //
143 header_imageEl : false,
145 layoutCls : function()
149 Roo.log(this.margin_bottom.length);
150 ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
151 // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
153 if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
154 cls += ' m' + (v.length ? v[0] : '') + '-' + t['margin' + (v.length ? '_' : '') + v];
156 if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
157 cls += ' p' + (v.length ? v[0] : '') + '-' + t['padding' + (v.length ? '_' : '') + v];
161 ['', 'xs', 'sm', 'lg', 'xl'].forEach(function(v) {
162 if (('' + t['display' + (v.length ? '_' : '') + v]).length) {
163 cls += ' d' + (v.length ? '-' : '') + v + '-' + t['display' + (v.length ? '_' : '') + v]
167 // more generic support?
175 // Roo.log("Call onRender: " + this.xtype);
176 /* We are looking at something like this.
178 <img src="..." class="card-img-top" alt="...">
179 <div class="card-body">
180 <h5 class="card-title">Card title</h5>
181 <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
183 >> this bit is really the body...
184 <div> << we will ad dthis in hopefully it will not break shit.
186 ** card text does not actually have any styling...
188 <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>
191 <a href="#" class="card-link">Card link</a>
194 <div class="card-footer">
195 <small class="text-muted">Last updated 3 mins ago</small>
199 getAutoCreate : function(){
207 if (this.weight.length && this.weight != 'light') {
208 cfg.cls += ' text-white';
210 cfg.cls += ' text-dark'; // need as it's nested..
212 if (this.weight.length) {
213 cfg.cls += ' bg-' + this.weight;
216 cfg.cls += ' ' + this.layoutCls();
220 if (this.header.length) {
222 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
223 cls : 'card-header ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
231 cls : 'card-header d-none ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
237 if (this.collapsable) {
240 cls : 'd-block user-select-none',
244 cls : 'roo-collapse-toggle fa fa-chevron-down float-right ' + (this.collapsed ? 'collapsed' : '')
249 hdr.cn.push(hdr_ctr);
254 cls: 'roo-card-header-ctr' + ( this.header.length ? '' : ' d-none'),
259 if (this.header_image.length) {
262 cls : 'card-img-top',
263 src: this.header_image // escape?
268 cls : 'card-img-top d-none'
274 cls : 'card-body' + (this.html === false ? ' d-none' : ''),
278 if (this.collapsable || this.rotateable) {
281 cls : 'roo-collapsable collapse ' + (this.collapsed || this.rotated ? '' : 'show'),
288 if (this.title.length) {
292 src: this.title // escape?
296 if (this.subtitle.length) {
300 src: this.subtitle // escape?
306 cls : 'roo-card-body-ctr'
309 if (this.html.length) {
315 // fixme ? handle objects?
317 if (this.footer.length) {
320 cls : 'card-footer ' + (this.rotated ? 'd-none' : ''),
325 cfg.cn.push({cls : 'card-footer d-none'});
334 getCardHeader : function()
336 var ret = this.el.select('.card-header',true).first();
337 if (ret.hasClass('d-none')) {
338 ret.removeClass('d-none');
343 getCardFooter : function()
345 var ret = this.el.select('.card-footer',true).first();
346 if (ret.hasClass('d-none')) {
347 ret.removeClass('d-none');
352 getCardImageTop : function()
354 var ret = this.header_imageEl;
355 if (ret.hasClass('d-none')) {
356 ret.removeClass('d-none');
362 getChildContainer : function()
368 return this.el.select('.roo-card-body-ctr',true).first();
371 initEvents: function()
373 this.bodyEl = this.el.select('.card-body',true).first();
374 this.containerEl = this.getChildContainer();
376 this.dragZone = new Roo.dd.DragZone(this.getEl(), {
377 containerScroll: true,
378 ddGroup: this.drag_group || 'default_card_drag_group'
380 this.dragZone.getDragData = this.getDragData.createDelegate(this);
383 this.dropZone = new Roo.dd.DropZone(this.el.select('.card-body',true).first() , {
384 containerScroll: true,
385 ddGroup: this.drop_group || 'default_card_drag_group'
387 this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
388 this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
389 this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
390 this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
391 this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
394 if (this.collapsable) {
395 this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
397 if (this.rotateable) {
398 this.el.select('.card-header',true).on('click', this.onToggleRotate, this);
400 this.collapsableEl = this.el.select('.roo-collapsable').first();
402 this.footerEl = this.el.select('.card-footer').first();
403 this.collapsableToggleEl = this.el.select('.roo-collapse-toggle');
404 this.headerContainerEl = this.el.select('.roo-card-header-ctr').first();
405 this.headerEl = this.el.select('.card-header',true).first();
408 this.el.addClass('roo-card-rotated');
409 this.fireEvent('rotate', this, true);
411 this.header_imageEl = this.el.select('.card-img-top',true).first();
412 this.header_imageEl.on('load', this.onHeaderImageLoad, this );
415 getDragData : function(e)
417 var target = this.getEl();
419 //this.handleSelection(e);
429 dragData.ddel = target.dom ; // the div element
430 Roo.log(target.getWidth( ));
431 dragData.ddel.style.width = target.getWidth() + 'px';
438 * Part of the Roo.dd.DropZone interface. If no target node is found, the
439 * whole Element becomes the target, and this causes the drop gesture to append.
441 getTargetFromEvent : function(e, dragged_card_el)
443 var target = e.getTarget();
444 while ((target !== null) && (target.parentNode != this.containerEl.dom)) {
445 target = target.parentNode;
456 //Roo.log([ 'target' , target ? target.id : '--nothing--']);
457 // see if target is one of the 'cards'...
460 //Roo.log(this.items.length);
465 for (var i = 0;i< this.items.length;i++) {
467 if (!this.items[i].el.hasClass('card')) {
470 pos = this.getDropPoint(e, this.items[i].el.dom);
472 cards_len = ret.cards.length;
473 //Roo.log(this.items[i].el.dom.id);
474 ret.cards.push(this.items[i]);
476 if (ret.card_n < 0 && pos == 'above') {
477 ret.position = cards_len > 0 ? 'below' : pos;
478 ret.items_n = i > 0 ? i - 1 : 0;
479 ret.card_n = cards_len > 0 ? cards_len - 1 : 0;
480 ret.card = ret.cards[ret.card_n];
483 if (!ret.cards.length) {
485 ret.position = 'below';
489 // could not find a card.. stick it at the end..
490 if (ret.card_n < 0) {
491 ret.card_n = last_card_n;
492 ret.card = ret.cards[last_card_n];
493 ret.items_n = this.items.indexOf(ret.cards[last_card_n]);
494 ret.position = 'below';
497 if (this.items[ret.items_n].el == dragged_card_el) {
501 if (ret.position == 'below') {
502 var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
504 if (card_after && card_after.el == dragged_card_el) {
511 var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
513 if (card_before && card_before.el == dragged_card_el) {
520 onNodeEnter : function(n, dd, e, data){
523 onNodeOver : function(n, dd, e, data)
526 var target_info = this.getTargetFromEvent(e,data.source.el);
527 if (target_info === false) {
528 this.dropPlaceHolder('hide');
531 Roo.log(['getTargetFromEvent', target_info ]);
534 this.dropPlaceHolder('show', target_info,data);
538 onNodeOut : function(n, dd, e, data){
539 this.dropPlaceHolder('hide');
542 onNodeDrop : function(n, dd, e, data)
545 // call drop - return false if
547 // this could actually fail - if the Network drops..
548 // we will ignore this at present..- client should probably reload
549 // the whole set of cards if stuff like that fails.
552 var info = this.getTargetFromEvent(e,data.source.el);
553 if (info === false) {
556 this.dropPlaceHolder('hide');
562 this.acceptCard(data.source, info.position, info.card, info.items_n);
566 firstChildCard : function()
568 for (var i = 0;i< this.items.length;i++) {
570 if (!this.items[i].el.hasClass('card')) {
573 return this.items[i];
575 return this.items.length ? this.items[this.items.length-1] : false; // don't try and put stuff after the cards...
580 * - card.acceptCard(move_card, info.position, info.card, info.items_n);
582 acceptCard : function(move_card, position, next_to_card )
584 if (this.fireEvent("drop", this, move_card, position, next_to_card) === false) {
588 var to_items_n = next_to_card ? this.items.indexOf(next_to_card) : 0;
590 move_card.parent().removeCard(move_card);
593 var dom = move_card.el.dom;
594 dom.style.width = ''; // clear with - which is set by drag.
596 if (next_to_card !== false && next_to_card !== true && next_to_card.el.dom.parentNode) {
597 var cardel = next_to_card.el.dom;
599 if (position == 'above' ) {
600 cardel.parentNode.insertBefore(dom, cardel);
601 } else if (cardel.nextSibling) {
602 cardel.parentNode.insertBefore(dom,cardel.nextSibling);
604 cardel.parentNode.append(dom);
608 this.containerEl.dom.append(dom);
611 //FIXME HANDLE card = true
613 // add this to the correct place in items.
615 // remove Card from items.
618 if (this.items.length) {
620 //Roo.log([info.items_n, info.position, this.items.length]);
621 for (var i =0; i < this.items.length; i++) {
622 if (i == to_items_n && position == 'above') {
623 nitems.push(move_card);
625 nitems.push(this.items[i]);
626 if (i == to_items_n && position == 'below') {
627 nitems.push(move_card);
633 this.items.push(move_card);
636 move_card.parentId = this.id;
642 removeCard : function(c)
644 this.items = this.items.filter(function(e) { return e != c });
647 dom.parentNode.removeChild(dom);
648 dom.style.width = ''; // clear with - which is set by drag.
653 /** Decide whether to drop above or below a View node. */
654 getDropPoint : function(e, n, dd)
659 if (n == this.containerEl.dom) {
662 var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
663 var c = t + (b - t) / 2;
664 var y = Roo.lib.Event.getPageY(e);
671 onToggleCollapse : function(e)
673 if (this.collapsed) {
674 this.el.select('.roo-collapse-toggle').removeClass('collapsed');
675 this.collapsableEl.addClass('show');
676 this.collapsed = false;
679 this.el.select('.roo-collapse-toggle').addClass('collapsed');
680 this.collapsableEl.removeClass('show');
681 this.collapsed = true;
686 onToggleRotate : function(e)
688 this.collapsableEl.removeClass('show');
689 this.footerEl.removeClass('d-none');
690 this.el.removeClass('roo-card-rotated');
691 this.el.removeClass('d-none');
694 this.collapsableEl.addClass('show');
695 this.rotated = false;
696 this.fireEvent('rotate', this, this.rotated);
699 this.el.addClass('roo-card-rotated');
700 this.footerEl.addClass('d-none');
701 this.el.select('.roo-collapsable').removeClass('show');
704 this.fireEvent('rotate', this, this.rotated);
708 dropPlaceHolder: function (action, info, data)
710 if (this.dropEl === false) {
711 this.dropEl = Roo.DomHelper.append(this.containerEl, {
715 this.dropEl.removeClass(['d-none', 'd-block']);
716 if (action == 'hide') {
718 this.dropEl.addClass('d-none');
721 // FIXME - info.card == true!!!
722 this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
724 if (info.card !== true) {
725 var cardel = info.card.el.dom;
727 if (info.position == 'above') {
728 cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
729 } else if (cardel.nextSibling) {
730 cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
732 cardel.parentNode.append(this.dropEl.dom);
736 this.containerEl.dom.append(this.dropEl.dom);
739 this.dropEl.addClass('d-block roo-card-dropzone');
741 this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
748 setHeaderText: function(html)
751 if (this.headerContainerEl) {
752 this.headerContainerEl.dom.innerHTML = html;
755 onHeaderImageLoad : function(ev, he)
757 if (!this.header_image_fit_square) {
761 var hw = he.naturalHeight / he.naturalWidth;
764 //var w = he.dom.naturalWidth;
767 he.style.position = 'relative';
769 var nw = (ww * (1/hw));
770 Roo.get(he).setSize( ww * (1/hw), ww);
771 he.style.left = ((ww - nw)/ 2) + 'px';
772 he.style.position = 'relative';