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();
211 if (this.header.length) {
213 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
222 cls : 'card-header d-none',
227 if (this.collapsable) {
230 cls : 'd-block user-select-none',
234 cls : 'roo-collapse-toggle fa fa-chevron-down float-right ' + (this.collapsed ? 'collapsed' : '')
239 hdr.cn.push(hdr_ctr);
244 cls: 'roo-card-header-ctr' + ( this.header.length ? '' : ' d-none'),
249 if (this.header_image.length) {
252 cls : 'card-img-top',
253 src: this.header_image // escape?
258 cls : 'card-img-top d-none'
264 cls : 'card-body' + (this.html === false ? ' d-none' : ''),
268 if (this.collapsable || this.rotateable) {
271 cls : 'roo-collapsable collapse ' + (this.collapsed || this.rotated ? '' : 'show'),
278 if (this.title.length) {
282 src: this.title // escape?
286 if (this.subtitle.length) {
290 src: this.subtitle // escape?
296 cls : 'roo-card-body-ctr'
299 if (this.html.length) {
305 // fixme ? handle objects?
307 if (this.footer.length) {
310 cls : 'card-footer ' + (this.rotated ? 'd-none' : ''),
315 cfg.cn.push({cls : 'card-footer d-none'});
324 getCardHeader : function()
326 var ret = this.el.select('.card-header',true).first();
327 if (ret.hasClass('d-none')) {
328 ret.removeClass('d-none');
333 getCardFooter : function()
335 var ret = this.el.select('.card-footer',true).first();
336 if (ret.hasClass('d-none')) {
337 ret.removeClass('d-none');
342 getCardImageTop : function()
344 var ret = this.el.select('.card-img-top',true).first();
345 if (ret.hasClass('d-none')) {
346 ret.removeClass('d-none');
352 getChildContainer : function()
358 return this.el.select('.roo-card-body-ctr',true).first();
361 initEvents: function()
364 this.bodyEl = this.getChildContainer();
366 this.dragZone = new Roo.dd.DragZone(this.getEl(), {
367 containerScroll: true,
368 ddGroup: this.drag_group || 'default_card_drag_group'
370 this.dragZone.getDragData = this.getDragData.createDelegate(this);
373 this.dropZone = new Roo.dd.DropZone(this.el.select('.card-body',true).first() , {
374 containerScroll: true,
375 ddGroup: this.drop_group || 'default_card_drag_group'
377 this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
378 this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
379 this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
380 this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
381 this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
384 if (this.collapsable) {
385 this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
387 if (this.rotateable) {
388 this.el.select('.card-header',true).on('click', this.onToggleRotate, this);
390 this.collapsableEl = this.el.select('.roo-collapsable').first();
392 this.footerEl = this.el.select('.card-footer').first();
393 this.collapsableToggleEl = this.el.select('.roo-collapse-toggle');
394 this.headerEl = this.el.select('.roo-card-header-ctr').first();
397 this.el.addClass('roo-card-rotated');
398 this.fireEvent('rotate', this, true);
402 getDragData : function(e)
404 var target = this.getEl();
406 //this.handleSelection(e);
416 dragData.ddel = target.dom ; // the div element
417 Roo.log(target.getWidth( ));
418 dragData.ddel.style.width = target.getWidth() + 'px';
425 * Part of the Roo.dd.DropZone interface. If no target node is found, the
426 * whole Element becomes the target, and this causes the drop gesture to append.
428 getTargetFromEvent : function(e, dragged_card_el)
430 var target = e.getTarget();
431 while ((target !== null) && (target.parentNode != this.bodyEl.dom)) {
432 target = target.parentNode;
443 //Roo.log([ 'target' , target ? target.id : '--nothing--']);
444 // see if target is one of the 'cards'...
447 //Roo.log(this.items.length);
452 for (var i = 0;i< this.items.length;i++) {
454 if (!this.items[i].el.hasClass('card')) {
457 pos = this.getDropPoint(e, this.items[i].el.dom);
459 cards_len = ret.cards.length;
460 //Roo.log(this.items[i].el.dom.id);
461 ret.cards.push(this.items[i]);
463 if (ret.card_n < 0 && pos == 'above') {
464 ret.position = cards_len > 0 ? 'below' : pos;
465 ret.items_n = i > 0 ? i - 1 : 0;
466 ret.card_n = cards_len > 0 ? cards_len - 1 : 0;
467 ret.card = ret.cards[ret.card_n];
470 if (!ret.cards.length) {
472 ret.position = 'below';
476 // could not find a card.. stick it at the end..
477 if (ret.card_n < 0) {
478 ret.card_n = last_card_n;
479 ret.card = ret.cards[last_card_n];
480 ret.items_n = this.items.indexOf(ret.cards[last_card_n]);
481 ret.position = 'below';
484 if (this.items[ret.items_n].el == dragged_card_el) {
488 if (ret.position == 'below') {
489 var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
491 if (card_after && card_after.el == dragged_card_el) {
498 var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
500 if (card_before && card_before.el == dragged_card_el) {
507 onNodeEnter : function(n, dd, e, data){
510 onNodeOver : function(n, dd, e, data)
513 var target_info = this.getTargetFromEvent(e,data.source.el);
514 if (target_info === false) {
515 this.dropPlaceHolder('hide');
518 Roo.log(['getTargetFromEvent', target_info ]);
521 this.dropPlaceHolder('show', target_info,data);
525 onNodeOut : function(n, dd, e, data){
526 this.dropPlaceHolder('hide');
529 onNodeDrop : function(n, dd, e, data)
532 // call drop - return false if
534 // this could actually fail - if the Network drops..
535 // we will ignore this at present..- client should probably reload
536 // the whole set of cards if stuff like that fails.
539 var info = this.getTargetFromEvent(e,data.source.el);
540 if (info === false) {
544 if (this.fireEvent("drop", this, n, dd, e, data) === false) {
548 this.dropPlaceHolder('hide');
550 // do the dom manipulation first..
551 var dom = data.source.el.dom;
552 dom.parentNode.removeChild(dom);
555 if (info.card !== true) {
556 var cardel = info.card.el.dom;
558 if (info.position == 'above') {
559 cardel.parentNode.insertBefore(dom, cardel);
560 } else if (cardel.nextSibling) {
561 cardel.parentNode.insertBefore(dom,cardel.nextSibling);
563 cardel.parentNode.append(dom);
567 this.bodyEl.dom.append(dom);
570 //FIXME HANDLE card = true
572 // add this to the correct place in items.
576 // remove Card from items.
578 var old_parent = data.source.parent();
580 old_parent.items = old_parent.items.filter(function(e) { return e != data.source });
582 if (this.items.length) {
584 Roo.log([info.items_n, info.position, this.items.length])
585 for (var i =0; i < this.items.length; i++) {
586 if (i == info.items_n && info.position == 'above') {
587 nitems.push(data.source);
589 nitems.push(this.items[i]);
590 if (i == info.items_n && info.position == 'below') {
591 nitems.push(data.source);
597 this.items.push(data.source);
600 data.source.parentId = this.id;
605 /** Decide whether to drop above or below a View node. */
606 getDropPoint : function(e, n, dd)
611 if (n == this.bodyEl.dom) {
614 var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
615 var c = t + (b - t) / 2;
616 var y = Roo.lib.Event.getPageY(e);
623 onToggleCollapse : function(e)
625 if (this.collapsed) {
626 this.el.select('.roo-collapse-toggle').removeClass('collapsed');
627 this.collapsableEl.addClass('show');
628 this.collapsed = false;
631 this.el.select('.roo-collapse-toggle').addClass('collapsed');
632 this.collapsableEl.removeClass('show');
633 this.collapsed = true;
638 onToggleRotate : function(e)
640 this.collapsableEl.removeClass('show');
641 this.footerEl.removeClass('d-none');
642 this.el.removeClass('roo-card-rotated');
643 this.el.removeClass('d-none');
646 this.collapsableEl.addClass('show');
647 this.rotated = false;
648 this.fireEvent('rotate', this, this.rotated);
651 this.el.addClass('roo-card-rotated');
652 this.footerEl.addClass('d-none');
653 this.el.select('.roo-collapsable').removeClass('show');
656 this.fireEvent('rotate', this, this.rotated);
660 dropPlaceHolder: function (action, info, data)
662 if (this.dropEl === false) {
663 this.dropEl = Roo.DomHelper.append(this.bodyEl, {
667 this.dropEl.removeClass(['d-none', 'd-block']);
668 if (action == 'hide') {
670 this.dropEl.addClass('d-none');
673 // FIXME - info.card == true!!!
674 this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
676 if (info.card !== true) {
677 var cardel = info.card.el.dom;
679 if (info.position == 'above') {
680 cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
681 } else if (cardel.nextSibling) {
682 cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
684 cardel.parentNode.append(this.dropEl.dom);
688 this.bodyEl.dom.append(this.dropEl.dom);
691 this.dropEl.addClass('d-block roo-card-dropzone');
693 this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
700 setHeaderText: function(html)
702 this.headerEl.dom.innerHTML = html;