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
19 * @cfg {String} subtitle
20 * @cfg {String|Boolean} html -- html contents - or just use children.. use false to hide it..
21 * @cfg {String} footer
23 * @cfg {String} weight (primary|warning|info|danger|secondary|success|light|dark)
25 * @cfg {String} margin (0|1|2|3|4|5|auto)
26 * @cfg {String} margin_top (0|1|2|3|4|5|auto)
27 * @cfg {String} margin_bottom (0|1|2|3|4|5|auto)
28 * @cfg {String} margin_left (0|1|2|3|4|5|auto)
29 * @cfg {String} margin_right (0|1|2|3|4|5|auto)
30 * @cfg {String} margin_x (0|1|2|3|4|5|auto)
31 * @cfg {String} margin_y (0|1|2|3|4|5|auto)
33 * @cfg {String} padding (0|1|2|3|4|5)
34 * @cfg {String} padding_top (0|1|2|3|4|5)
35 * @cfg {String} padding_bottom (0|1|2|3|4|5)
36 * @cfg {String} padding_left (0|1|2|3|4|5)
37 * @cfg {String} padding_right (0|1|2|3|4|5)
38 * @cfg {String} padding_x (0|1|2|3|4|5)
39 * @cfg {String} padding_y (0|1|2|3|4|5)
41 * @cfg {String} display (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
42 * @cfg {String} display_xs (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
43 * @cfg {String} display_sm (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
44 * @cfg {String} display_lg (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
45 * @cfg {String} display_xl (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
47 * @config {Boolean} dragable if this card can be dragged.
48 * @config {String} drag_group group for drag
49 * @config {Boolean} dropable if this card can recieve other cards being dropped onto it..
50 * @config {String} drop_group group for drag
52 * @config {Boolean} collapsable can the body be collapsed.
53 * @config {Boolean} collapsed is the body collapsed when rendered...
54 * @config {Boolean} rotateable can the body be rotated by clicking on it..
55 * @config {Boolean} rotated is the body rotated when rendered...
58 * Create a new Container
59 * @param {Object} config The config object
62 Roo.bootstrap.Card = function(config){
63 Roo.bootstrap.Card.superclass.constructor.call(this, config);
69 * When a element a card is dropped
70 * @param {Roo.bootstrap.Card} this
73 * @param {Roo.bootstrap.Card} move_card the card being dropped?
74 * @param {String} position 'above' or 'below'
75 * @param {Roo.bootstrap.Card} next_to_card What card position is relative to of 'false' for empty list.
81 * When a element a card is rotate
82 * @param {Roo.bootstrap.Element} this
83 * @param {Roo.Element} n the node being dropped?
84 * @param {Boolean} rotate status
92 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component, {
97 margin: '', /// may be better in component?
136 childContainer : false,
137 dropEl : false, /// the dom placeholde element that indicates drop location.
138 containerEl: false, // body container
139 bodyEl: false, // card-body
140 headerContainerEl : false, //
143 layoutCls : function()
147 Roo.log(this.margin_bottom.length);
148 ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
149 // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
151 if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
152 cls += ' m' + (v.length ? v[0] : '') + '-' + t['margin' + (v.length ? '_' : '') + v];
154 if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
155 cls += ' p' + (v.length ? v[0] : '') + '-' + t['padding' + (v.length ? '_' : '') + v];
159 ['', 'xs', 'sm', 'lg', 'xl'].forEach(function(v) {
160 if (('' + t['display' + (v.length ? '_' : '') + v]).length) {
161 cls += ' d' + (v.length ? '-' : '') + v + '-' + t['margin' + (v.length ? '_' : '') + v]
165 // more generic support?
173 // Roo.log("Call onRender: " + this.xtype);
174 /* We are looking at something like this.
176 <img src="..." class="card-img-top" alt="...">
177 <div class="card-body">
178 <h5 class="card-title">Card title</h5>
179 <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
181 >> this bit is really the body...
182 <div> << we will ad dthis in hopefully it will not break shit.
184 ** card text does not actually have any styling...
186 <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>
189 <a href="#" class="card-link">Card link</a>
192 <div class="card-footer">
193 <small class="text-muted">Last updated 3 mins ago</small>
197 getAutoCreate : function(){
205 if (this.weight.length && this.weight != 'light') {
206 cfg.cls += ' text-white';
208 cfg.cls += ' text-dark'; // need as it's nested..
210 if (this.weight.length) {
211 cfg.cls += ' bg-' + this.weight;
214 cfg.cls += this.layoutCls();
218 if (this.header.length) {
220 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
229 cls : 'card-header d-none',
235 if (this.collapsable) {
238 cls : 'd-block user-select-none',
242 cls : 'roo-collapse-toggle fa fa-chevron-down float-right ' + (this.collapsed ? 'collapsed' : '')
247 hdr.cn.push(hdr_ctr);
252 cls: 'roo-card-header-ctr' + ( this.header.length ? '' : ' d-none'),
257 if (this.header_image.length) {
260 cls : 'card-img-top',
261 src: this.header_image // escape?
266 cls : 'card-img-top d-none'
272 cls : 'card-body' + (this.html === false ? ' d-none' : ''),
276 if (this.collapsable || this.rotateable) {
279 cls : 'roo-collapsable collapse ' + (this.collapsed || this.rotated ? '' : 'show'),
286 if (this.title.length) {
290 src: this.title // escape?
294 if (this.subtitle.length) {
298 src: this.subtitle // escape?
304 cls : 'roo-card-body-ctr'
307 if (this.html.length) {
313 // fixme ? handle objects?
315 if (this.footer.length) {
318 cls : 'card-footer ' + (this.rotated ? 'd-none' : ''),
323 cfg.cn.push({cls : 'card-footer d-none'});
332 getCardHeader : function()
334 var ret = this.el.select('.card-header',true).first();
335 if (ret.hasClass('d-none')) {
336 ret.removeClass('d-none');
341 getCardFooter : function()
343 var ret = this.el.select('.card-footer',true).first();
344 if (ret.hasClass('d-none')) {
345 ret.removeClass('d-none');
350 getCardImageTop : function()
352 var ret = this.el.select('.card-img-top',true).first();
353 if (ret.hasClass('d-none')) {
354 ret.removeClass('d-none');
360 getChildContainer : function()
366 return this.el.select('.roo-card-body-ctr',true).first();
369 initEvents: function()
371 this.bodyEl = this.el.select('.card-body',true).first();
372 this.containerEl = this.getChildContainer();
374 this.dragZone = new Roo.dd.DragZone(this.getEl(), {
375 containerScroll: true,
376 ddGroup: this.drag_group || 'default_card_drag_group'
378 this.dragZone.getDragData = this.getDragData.createDelegate(this);
381 this.dropZone = new Roo.dd.DropZone(this.el.select('.card-body',true).first() , {
382 containerScroll: true,
383 ddGroup: this.drop_group || 'default_card_drag_group'
385 this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
386 this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
387 this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
388 this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
389 this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
392 if (this.collapsable) {
393 this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
395 if (this.rotateable) {
396 this.el.select('.card-header',true).on('click', this.onToggleRotate, this);
398 this.collapsableEl = this.el.select('.roo-collapsable').first();
400 this.footerEl = this.el.select('.card-footer').first();
401 this.collapsableToggleEl = this.el.select('.roo-collapse-toggle');
402 this.headerContainerEl = this.el.select('.roo-card-header-ctr').first();
403 this.headerEl = this.el.select('.card-header',true).first();
406 this.el.addClass('roo-card-rotated');
407 this.fireEvent('rotate', this, true);
411 getDragData : function(e)
413 var target = this.getEl();
415 //this.handleSelection(e);
425 dragData.ddel = target.dom ; // the div element
426 Roo.log(target.getWidth( ));
427 dragData.ddel.style.width = target.getWidth() + 'px';
434 * Part of the Roo.dd.DropZone interface. If no target node is found, the
435 * whole Element becomes the target, and this causes the drop gesture to append.
437 getTargetFromEvent : function(e, dragged_card_el)
439 var target = e.getTarget();
440 while ((target !== null) && (target.parentNode != this.containerEl.dom)) {
441 target = target.parentNode;
452 //Roo.log([ 'target' , target ? target.id : '--nothing--']);
453 // see if target is one of the 'cards'...
456 //Roo.log(this.items.length);
461 for (var i = 0;i< this.items.length;i++) {
463 if (!this.items[i].el.hasClass('card')) {
466 pos = this.getDropPoint(e, this.items[i].el.dom);
468 cards_len = ret.cards.length;
469 //Roo.log(this.items[i].el.dom.id);
470 ret.cards.push(this.items[i]);
472 if (ret.card_n < 0 && pos == 'above') {
473 ret.position = cards_len > 0 ? 'below' : pos;
474 ret.items_n = i > 0 ? i - 1 : 0;
475 ret.card_n = cards_len > 0 ? cards_len - 1 : 0;
476 ret.card = ret.cards[ret.card_n];
479 if (!ret.cards.length) {
481 ret.position = 'below';
485 // could not find a card.. stick it at the end..
486 if (ret.card_n < 0) {
487 ret.card_n = last_card_n;
488 ret.card = ret.cards[last_card_n];
489 ret.items_n = this.items.indexOf(ret.cards[last_card_n]);
490 ret.position = 'below';
493 if (this.items[ret.items_n].el == dragged_card_el) {
497 if (ret.position == 'below') {
498 var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
500 if (card_after && card_after.el == dragged_card_el) {
507 var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
509 if (card_before && card_before.el == dragged_card_el) {
516 onNodeEnter : function(n, dd, e, data){
519 onNodeOver : function(n, dd, e, data)
522 var target_info = this.getTargetFromEvent(e,data.source.el);
523 if (target_info === false) {
524 this.dropPlaceHolder('hide');
527 Roo.log(['getTargetFromEvent', target_info ]);
530 this.dropPlaceHolder('show', target_info,data);
534 onNodeOut : function(n, dd, e, data){
535 this.dropPlaceHolder('hide');
538 onNodeDrop : function(n, dd, e, data)
541 // call drop - return false if
543 // this could actually fail - if the Network drops..
544 // we will ignore this at present..- client should probably reload
545 // the whole set of cards if stuff like that fails.
548 var info = this.getTargetFromEvent(e,data.source.el);
549 if (info === false) {
552 this.dropPlaceHolder('hide');
558 this.acceptCard(data.source, info.position, info.card, info.items_n);
562 firstChildCard : function()
564 for (var i = 0;i< this.items.length;i++) {
566 if (!this.items[i].el.hasClass('card')) {
569 return this.items[i];
571 return this.items.length ? this.items[this.items.length-1] : false; // don't try and put stuff after the cards...
576 * - 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;
588 var dom = move_card.el.dom;
589 dom.parentNode.removeChild(dom);
592 if (next_to_card !== false) {
593 var cardel = next_to_card.el.dom;
595 if (position == 'above') {
596 cardel.parentNode.insertBefore(dom, cardel);
597 } else if (cardel.nextSibling) {
598 cardel.parentNode.insertBefore(dom,cardel.nextSibling);
600 cardel.parentNode.append(dom);
604 this.containerEl.dom.append(dom);
607 //FIXME HANDLE card = true
609 // add this to the correct place in items.
613 // remove Card from items.
615 var old_parent = move_card.parent();
617 old_parent.items = old_parent.items.filter(function(e) { return e != move_card });
619 if (this.items.length) {
621 //Roo.log([info.items_n, info.position, this.items.length]);
622 for (var i =0; i < this.items.length; i++) {
623 if (i == to_items_n && position == 'above') {
624 nitems.push(move_card);
626 nitems.push(this.items[i]);
627 if (i == to_items_n && position == 'below') {
628 nitems.push(move_card);
634 this.items.push(move_card);
637 move_card.parentId = this.id;
645 /** Decide whether to drop above or below a View node. */
646 getDropPoint : function(e, n, dd)
651 if (n == this.containerEl.dom) {
654 var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
655 var c = t + (b - t) / 2;
656 var y = Roo.lib.Event.getPageY(e);
663 onToggleCollapse : function(e)
665 if (this.collapsed) {
666 this.el.select('.roo-collapse-toggle').removeClass('collapsed');
667 this.collapsableEl.addClass('show');
668 this.collapsed = false;
671 this.el.select('.roo-collapse-toggle').addClass('collapsed');
672 this.collapsableEl.removeClass('show');
673 this.collapsed = true;
678 onToggleRotate : function(e)
680 this.collapsableEl.removeClass('show');
681 this.footerEl.removeClass('d-none');
682 this.el.removeClass('roo-card-rotated');
683 this.el.removeClass('d-none');
686 this.collapsableEl.addClass('show');
687 this.rotated = false;
688 this.fireEvent('rotate', this, this.rotated);
691 this.el.addClass('roo-card-rotated');
692 this.footerEl.addClass('d-none');
693 this.el.select('.roo-collapsable').removeClass('show');
696 this.fireEvent('rotate', this, this.rotated);
700 dropPlaceHolder: function (action, info, data)
702 if (this.dropEl === false) {
703 this.dropEl = Roo.DomHelper.append(this.containerEl, {
707 this.dropEl.removeClass(['d-none', 'd-block']);
708 if (action == 'hide') {
710 this.dropEl.addClass('d-none');
713 // FIXME - info.card == true!!!
714 this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
716 if (info.card !== true) {
717 var cardel = info.card.el.dom;
719 if (info.position == 'above') {
720 cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
721 } else if (cardel.nextSibling) {
722 cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
724 cardel.parentNode.append(this.dropEl.dom);
728 this.containerEl.dom.append(this.dropEl.dom);
731 this.dropEl.addClass('d-block roo-card-dropzone');
733 this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
740 setHeaderText: function(html)
742 this.headerContainerEl.dom.innerHTML = html;