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
82 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component, {
87 margin: '', /// may be better in component?
126 childContainer : false,
127 dropEl : false, /// the dom placeholde element that indicates drop location.
129 layoutCls : function()
133 Roo.log(this.margin_bottom.length);
134 ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
135 // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
137 if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
138 cls += ' m' + (v.length ? v[0] : '') + '-' + t['margin' + (v.length ? '_' : '') + v];
140 if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
141 cls += ' p' + (v.length ? v[0] : '') + '-' + t['padding' + (v.length ? '_' : '') + v];
145 ['', 'xs', 'sm', 'lg', 'xl'].forEach(function(v) {
146 if (('' + t['display' + (v.length ? '_' : '') + v]).length) {
147 cls += ' d' + (v.length ? '-' : '') + v + '-' + t['margin' + (v.length ? '_' : '') + v]
151 // more generic support?
159 // Roo.log("Call onRender: " + this.xtype);
160 /* We are looking at something like this.
162 <img src="..." class="card-img-top" alt="...">
163 <div class="card-body">
164 <h5 class="card-title">Card title</h5>
165 <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
167 >> this bit is really the body...
168 <div> << we will ad dthis in hopefully it will not break shit.
170 ** card text does not actually have any styling...
172 <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>
175 <a href="#" class="card-link">Card link</a>
178 <div class="card-footer">
179 <small class="text-muted">Last updated 3 mins ago</small>
183 getAutoCreate : function(){
191 if (this.weight.length && this.weight != 'light') {
192 cfg.cls += ' text-white';
194 cfg.cls += ' text-dark'; // need as it's nested..
196 if (this.weight.length) {
197 cfg.cls += ' bg-' + this.weight;
200 cfg.cls += this.layoutCls();
203 if (this.header.length) {
205 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
214 cls : 'card-header d-none',
219 if (this.collapsable) {
222 cls : 'd-block user-select-none',
226 cls : 'roo-collapse-toggle fa fa-chevron-down float-right ' + (this.collapsed ? 'collapsed' : '')
231 hdr.cn.push(hdr_ctr);
233 if (this.header.length) {
236 cls: 'roo-card-header-ctr',
241 if (this.header_image.length) {
244 cls : 'card-img-top',
245 src: this.header_image // escape?
250 cls : 'card-img-top d-none'
256 cls : 'card-body' + (this.html === false ? ' d-none' : ''),
260 if (this.collapsable || this.rotateable) {
263 cls : 'roo-collapsable collapse ' + (this.collapsed || this.rotated ? '' : 'show'),
270 if (this.title.length) {
274 src: this.title // escape?
278 if (this.subtitle.length) {
282 src: this.subtitle // escape?
288 cls : 'roo-card-body-ctr'
291 if (this.html.length) {
297 // fixme ? handle objects?
299 if (this.footer.length) {
302 cls : 'card-footer ' + (this.rotated ? 'd-none' : ''),
307 cfg.cn.push({cls : 'card-footer d-none'});
316 getCardHeader : function()
318 var ret = this.el.select('.card-header',true).first();
319 if (ret.hasClass('d-none')) {
320 ret.removeClass('d-none');
325 getCardFooter : function()
327 var ret = this.el.select('.card-footer',true).first();
328 if (ret.hasClass('d-none')) {
329 ret.removeClass('d-none');
334 getCardImageTop : function()
336 var ret = this.el.select('.card-img-top',true).first();
337 if (ret.hasClass('d-none')) {
338 ret.removeClass('d-none');
344 getChildContainer : function()
350 return this.el.select('.roo-card-body-ctr',true).first();
353 initEvents: function()
356 this.bodyEl = this.getChildContainer();
358 this.dragZone = new Roo.dd.DragZone(this.getEl(), {
359 containerScroll: true,
360 ddGroup: this.drag_group || 'default_card_drag_group'
362 this.dragZone.getDragData = this.getDragData.createDelegate(this);
365 this.dropZone = new Roo.dd.DropZone(this.el.select('.card-body',true).first() , {
366 containerScroll: true,
367 ddGroup: this.drop_group || 'default_card_drag_group'
369 this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
370 this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
371 this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
372 this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
373 this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
376 if (this.collapsable) {
377 this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
379 if (this.rotateable) {
380 this.el.select('.card-header',true).on('click', this.onToggleRotate, this);
382 this.collapsableEl = this.el.select('.roo-collapsable').first();
383 this.footerEl = this.el.select('.card-footer').first();
384 this.collapsableToggleEl = this.el.select('.roo-collapse-toggle');
386 getDragData : function(e)
388 var target = this.getEl();
390 //this.handleSelection(e);
400 dragData.ddel = target.dom ; // the div element
401 Roo.log(target.getWidth( ));
402 dragData.ddel.style.width = target.getWidth() + 'px';
409 * Part of the Roo.dd.DropZone interface. If no target node is found, the
410 * whole Element becomes the target, and this causes the drop gesture to append.
412 getTargetFromEvent : function(e, dragged_card_el)
414 var target = e.getTarget();
415 while ((target !== null) && (target.parentNode != this.bodyEl.dom)) {
416 target = target.parentNode;
427 //Roo.log([ 'target' , target ? target.id : '--nothing--']);
428 // see if target is one of the 'cards'...
431 //Roo.log(this.items.length);
436 for (var i = 0;i< this.items.length;i++) {
438 if (!this.items[i].el.hasClass('card')) {
441 pos = this.getDropPoint(e, this.items[i].el.dom);
443 cards_len = ret.cards.length;
444 //Roo.log(this.items[i].el.dom.id);
445 ret.cards.push(this.items[i]);
447 if (ret.card_n < 0 && pos == 'above') {
448 ret.position = cards_len > 0 ? 'below' : pos;
449 ret.items_n = i > 0 ? i - 1 : 0;
450 ret.card_n = cards_len > 0 ? cards_len - 1 : 0;
451 ret.card = ret.cards[ret.card_n];
454 if (!ret.cards.length) {
456 ret.position = 'below';
460 // could not find a card.. stick it at the end..
461 if (ret.card_n < 0) {
462 ret.card_n = last_card_n;
463 ret.card = ret.cards[last_card_n];
464 ret.items_n = this.items.indexOf(ret.cards[last_card_n]);
465 ret.position = 'below';
468 if (this.items[ret.items_n].el == dragged_card_el) {
472 if (ret.position == 'below') {
473 var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
475 if (card_after && card_after.el == dragged_card_el) {
482 var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
484 if (card_before && card_before.el == dragged_card_el) {
491 onNodeEnter : function(n, dd, e, data){
494 onNodeOver : function(n, dd, e, data)
497 var target_info = this.getTargetFromEvent(e,data.source.el);
498 if (target_info === false) {
499 this.dropPlaceHolder('hide');
502 Roo.log(['getTargetFromEvent', target_info ]);
505 this.dropPlaceHolder('show', target_info,data);
509 onNodeOut : function(n, dd, e, data){
510 this.dropPlaceHolder('hide');
513 onNodeDrop : function(n, dd, e, data)
516 // call drop - return false if
518 // this could actually fail - if the Network drops..
519 // we will ignore this at present..- client should probably reload
520 // the whole set of cards if stuff like that fails.
523 var info = this.getTargetFromEvent(e,data.source.el);
524 if (info === false) {
528 if (this.fireEvent("drop", this, n, dd, e, data) === false) {
532 this.dropPlaceHolder('hide');
534 // do the dom manipulation first..
535 var dom = data.source.el.dom;
536 dom.parentNode.removeChild(dom);
539 if (info.card !== true) {
540 var cardel = info.card.el.dom;
542 if (info.position == 'above') {
543 cardel.parentNode.insertBefore(dom, cardel);
544 } else if (cardel.nextSibling) {
545 cardel.parentNode.insertBefore(dom,cardel.nextSibling);
547 cardel.parentNode.append(dom);
551 this.bodyEl.dom.append(dom);
554 //FIXME HANDLE card = true
556 // add this to the correct place in items.
560 // remove Card from items.
562 var old_parent = data.source.parent();
564 old_parent.items = old_parent.items.filter(function(e) { return e != data.source });
566 if (this.items.length) {
568 Roo.log([info.items_n, info.position, this.items.length])
569 for (var i =0; i < this.items.length; i++) {
570 if (i == info.items_n && info.position == 'above') {
571 nitems.push(data.source);
573 nitems.push(this.items[i]);
574 if (i == info.items_n && info.position == 'below') {
575 nitems.push(data.source);
581 this.items.push(data.source);
589 /** Decide whether to drop above or below a View node. */
590 getDropPoint : function(e, n, dd)
595 if (n == this.bodyEl.dom) {
598 var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
599 var c = t + (b - t) / 2;
600 var y = Roo.lib.Event.getPageY(e);
607 onToggleCollapse : function(e)
609 if (this.collapsed) {
610 this.el.select('.roo-collapse-toggle').removeClass('collapsed');
611 this.collapsableEl.addClass('show');
612 this.collapsed = false;
615 this.el.select('.roo-collapse-toggle').addClass('collapsed');
616 this.collapsableEl.removeClass('show');
617 this.collapsed = true;
622 onToggleRotate : function(e)
624 this.collapsableEl.removeClass('show');
625 this.footerEl.removeClass('d-none');
628 this.collapsableEl.addClass('show');
629 this.rotated = false;
632 this.footerEl.addClass('d-none');
633 this.el.select('.roo-collapsable').removeClass('show');
639 dropPlaceHolder: function (action, info, data)
641 if (this.dropEl === false) {
642 this.dropEl = Roo.DomHelper.append(this.bodyEl, {
646 this.dropEl.removeClass(['d-none', 'd-block']);
647 if (action == 'hide') {
649 this.dropEl.addClass('d-none');
652 // FIXME - info.card == true!!!
653 this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
655 if (info.card !== true) {
656 var cardel = info.card.el.dom;
658 if (info.position == 'above') {
659 cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
660 } else if (cardel.nextSibling) {
661 cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
663 cardel.parentNode.append(this.dropEl.dom);
667 this.bodyEl.dom.append(this.dropEl.dom);
670 this.dropEl.addClass('d-block roo-card-dropzone');
672 this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );