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.Element} this
71 * @param {Roo.Element} n the node being dropped?
72 * @param {Object} dd Drag and drop data
73 * @param {Roo.EventObject} e
74 * @param {Roo.EventObject} data the data passed via getDragData
79 * When a element a card is rotate
80 * @param {Roo.bootstrap.Element} this
81 * @param {Roo.Element} n the node being dropped?
82 * @param {Boolean} rotate status
90 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component, {
95 margin: '', /// may be better in component?
134 childContainer : false,
135 dropEl : false, /// the dom placeholde element that indicates drop location.
137 layoutCls : function()
141 Roo.log(this.margin_bottom.length);
142 ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
143 // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
145 if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
146 cls += ' m' + (v.length ? v[0] : '') + '-' + t['margin' + (v.length ? '_' : '') + v];
148 if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
149 cls += ' p' + (v.length ? v[0] : '') + '-' + t['padding' + (v.length ? '_' : '') + v];
153 ['', 'xs', 'sm', 'lg', 'xl'].forEach(function(v) {
154 if (('' + t['display' + (v.length ? '_' : '') + v]).length) {
155 cls += ' d' + (v.length ? '-' : '') + v + '-' + t['margin' + (v.length ? '_' : '') + v]
159 // more generic support?
167 // Roo.log("Call onRender: " + this.xtype);
168 /* We are looking at something like this.
170 <img src="..." class="card-img-top" alt="...">
171 <div class="card-body">
172 <h5 class="card-title">Card title</h5>
173 <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
175 >> this bit is really the body...
176 <div> << we will ad dthis in hopefully it will not break shit.
178 ** card text does not actually have any styling...
180 <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>
183 <a href="#" class="card-link">Card link</a>
186 <div class="card-footer">
187 <small class="text-muted">Last updated 3 mins ago</small>
191 getAutoCreate : function(){
199 if (this.weight.length && this.weight != 'light') {
200 cfg.cls += ' text-white';
202 cfg.cls += ' text-dark'; // need as it's nested..
204 if (this.weight.length) {
205 cfg.cls += ' bg-' + this.weight;
208 cfg.cls += this.layoutCls();
212 if (this.header.length) {
214 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
223 cls : 'card-header d-none',
229 if (this.collapsable) {
232 cls : 'd-block user-select-none',
236 cls : 'roo-collapse-toggle fa fa-chevron-down float-right ' + (this.collapsed ? 'collapsed' : '')
241 hdr.cn.push(hdr_ctr);
246 cls: 'roo-card-header-ctr' + ( this.header.length ? '' : ' d-none'),
251 if (this.header_image.length) {
254 cls : 'card-img-top',
255 src: this.header_image // escape?
260 cls : 'card-img-top d-none'
266 cls : 'card-body' + (this.html === false ? ' d-none' : ''),
270 if (this.collapsable || this.rotateable) {
273 cls : 'roo-collapsable collapse ' + (this.collapsed || this.rotated ? '' : 'show'),
280 if (this.title.length) {
284 src: this.title // escape?
288 if (this.subtitle.length) {
292 src: this.subtitle // escape?
298 cls : 'roo-card-body-ctr'
301 if (this.html.length) {
307 // fixme ? handle objects?
309 if (this.footer.length) {
312 cls : 'card-footer ' + (this.rotated ? 'd-none' : ''),
317 cfg.cn.push({cls : 'card-footer d-none'});
326 getCardHeader : function()
328 var ret = this.el.select('.card-header',true).first();
329 if (ret.hasClass('d-none')) {
330 ret.removeClass('d-none');
335 getCardFooter : function()
337 var ret = this.el.select('.card-footer',true).first();
338 if (ret.hasClass('d-none')) {
339 ret.removeClass('d-none');
344 getCardImageTop : function()
346 var ret = this.el.select('.card-img-top',true).first();
347 if (ret.hasClass('d-none')) {
348 ret.removeClass('d-none');
354 getChildContainer : function()
360 return this.el.select('.roo-card-body-ctr',true).first();
363 initEvents: function()
366 this.bodyEl = this.getChildContainer();
368 this.dragZone = new Roo.dd.DragZone(this.getEl(), {
369 containerScroll: true,
370 ddGroup: this.drag_group || 'default_card_drag_group'
372 this.dragZone.getDragData = this.getDragData.createDelegate(this);
375 this.dropZone = new Roo.dd.DropZone(this.el.select('.card-body',true).first() , {
376 containerScroll: true,
377 ddGroup: this.drop_group || 'default_card_drag_group'
379 this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
380 this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
381 this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
382 this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
383 this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
386 if (this.collapsable) {
387 this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
389 if (this.rotateable) {
390 this.el.select('.card-header',true).on('click', this.onToggleRotate, this);
392 this.collapsableEl = this.el.select('.roo-collapsable').first();
394 this.footerEl = this.el.select('.card-footer').first();
395 this.collapsableToggleEl = this.el.select('.roo-collapse-toggle');
396 this.headerEl = this.el.select('.roo-card-header-ctr').first();
399 this.el.addClass('roo-card-rotated');
400 this.fireEvent('rotate', this, true);
404 getDragData : function(e)
406 var target = this.getEl();
408 //this.handleSelection(e);
418 dragData.ddel = target.dom ; // the div element
419 Roo.log(target.getWidth( ));
420 dragData.ddel.style.width = target.getWidth() + 'px';
427 * Part of the Roo.dd.DropZone interface. If no target node is found, the
428 * whole Element becomes the target, and this causes the drop gesture to append.
430 getTargetFromEvent : function(e, dragged_card_el)
432 var target = e.getTarget();
433 while ((target !== null) && (target.parentNode != this.bodyEl.dom)) {
434 target = target.parentNode;
445 //Roo.log([ 'target' , target ? target.id : '--nothing--']);
446 // see if target is one of the 'cards'...
449 //Roo.log(this.items.length);
454 for (var i = 0;i< this.items.length;i++) {
456 if (!this.items[i].el.hasClass('card')) {
459 pos = this.getDropPoint(e, this.items[i].el.dom);
461 cards_len = ret.cards.length;
462 //Roo.log(this.items[i].el.dom.id);
463 ret.cards.push(this.items[i]);
465 if (ret.card_n < 0 && pos == 'above') {
466 ret.position = cards_len > 0 ? 'below' : pos;
467 ret.items_n = i > 0 ? i - 1 : 0;
468 ret.card_n = cards_len > 0 ? cards_len - 1 : 0;
469 ret.card = ret.cards[ret.card_n];
472 if (!ret.cards.length) {
474 ret.position = 'below';
478 // could not find a card.. stick it at the end..
479 if (ret.card_n < 0) {
480 ret.card_n = last_card_n;
481 ret.card = ret.cards[last_card_n];
482 ret.items_n = this.items.indexOf(ret.cards[last_card_n]);
483 ret.position = 'below';
486 if (this.items[ret.items_n].el == dragged_card_el) {
490 if (ret.position == 'below') {
491 var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
493 if (card_after && card_after.el == dragged_card_el) {
500 var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
502 if (card_before && card_before.el == dragged_card_el) {
509 onNodeEnter : function(n, dd, e, data){
512 onNodeOver : function(n, dd, e, data)
515 var target_info = this.getTargetFromEvent(e,data.source.el);
516 if (target_info === false) {
517 this.dropPlaceHolder('hide');
520 Roo.log(['getTargetFromEvent', target_info ]);
523 this.dropPlaceHolder('show', target_info,data);
527 onNodeOut : function(n, dd, e, data){
528 this.dropPlaceHolder('hide');
531 onNodeDrop : function(n, dd, e, data)
534 // call drop - return false if
536 // this could actually fail - if the Network drops..
537 // we will ignore this at present..- client should probably reload
538 // the whole set of cards if stuff like that fails.
541 var info = this.getTargetFromEvent(e,data.source.el);
542 if (info === false) {
546 if (this.fireEvent("drop", this, n, dd, e, data) === false) {
550 this.dropPlaceHolder('hide');
552 // do the dom manipulation first..
553 var dom = data.source.el.dom;
554 dom.parentNode.removeChild(dom);
557 if (info.card !== true) {
558 var cardel = info.card.el.dom;
560 if (info.position == 'above') {
561 cardel.parentNode.insertBefore(dom, cardel);
562 } else if (cardel.nextSibling) {
563 cardel.parentNode.insertBefore(dom,cardel.nextSibling);
565 cardel.parentNode.append(dom);
569 this.bodyEl.dom.append(dom);
572 //FIXME HANDLE card = true
574 // add this to the correct place in items.
578 // remove Card from items.
580 var old_parent = data.source.parent();
582 old_parent.items = old_parent.items.filter(function(e) { return e != data.source });
584 if (this.items.length) {
586 //Roo.log([info.items_n, info.position, this.items.length]);
587 for (var i =0; i < this.items.length; i++) {
588 if (i == info.items_n && info.position == 'above') {
589 nitems.push(data.source);
591 nitems.push(this.items[i]);
592 if (i == info.items_n && info.position == 'below') {
593 nitems.push(data.source);
599 this.items.push(data.source);
602 data.source.parentId = this.id;
607 /** Decide whether to drop above or below a View node. */
608 getDropPoint : function(e, n, dd)
613 if (n == this.bodyEl.dom) {
616 var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
617 var c = t + (b - t) / 2;
618 var y = Roo.lib.Event.getPageY(e);
625 onToggleCollapse : function(e)
627 if (this.collapsed) {
628 this.el.select('.roo-collapse-toggle').removeClass('collapsed');
629 this.collapsableEl.addClass('show');
630 this.collapsed = false;
633 this.el.select('.roo-collapse-toggle').addClass('collapsed');
634 this.collapsableEl.removeClass('show');
635 this.collapsed = true;
640 onToggleRotate : function(e)
642 this.collapsableEl.removeClass('show');
643 this.footerEl.removeClass('d-none');
644 this.el.removeClass('roo-card-rotated');
645 this.el.removeClass('d-none');
648 this.collapsableEl.addClass('show');
649 this.rotated = false;
650 this.fireEvent('rotate', this, this.rotated);
653 this.el.addClass('roo-card-rotated');
654 this.footerEl.addClass('d-none');
655 this.el.select('.roo-collapsable').removeClass('show');
658 this.fireEvent('rotate', this, this.rotated);
662 dropPlaceHolder: function (action, info, data)
664 if (this.dropEl === false) {
665 this.dropEl = Roo.DomHelper.append(this.bodyEl, {
669 this.dropEl.removeClass(['d-none', 'd-block']);
670 if (action == 'hide') {
672 this.dropEl.addClass('d-none');
675 // FIXME - info.card == true!!!
676 this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
678 if (info.card !== true) {
679 var cardel = info.card.el.dom;
681 if (info.position == 'above') {
682 cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
683 } else if (cardel.nextSibling) {
684 cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
686 cardel.parentNode.append(this.dropEl.dom);
690 this.bodyEl.dom.append(this.dropEl.dom);
693 this.dropEl.addClass('d-block roo-card-dropzone');
695 this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
702 setHeaderText: function(html)
704 this.headerEl.dom.innerHTML = html;