4 * This is BS4's Card element.. - similar to our containers probably..
8 * @class Roo.bootstrap.Card
9 * @extends Roo.bootstrap.Component
10 * @children Roo.bootstrap.Component
11 * Bootstrap Card class
14 * possible... may not be implemented..
15 * @cfg {String} header_image src url of image.
16 * @cfg {String|Object} header
17 * @cfg {Number} header_size (0|1|2|3|4|5) H1 or H2 etc.. 0 indicates default
18 * @cfg {Number} header_weight (primary|secondary|success|info|warning|danger|light|dark)
21 * @cfg {String} subtitle
22 * @cfg {String|Boolean} html -- html contents - or just use children.. use false to hide it..
23 * @cfg {String} footer
25 * @cfg {String} weight (primary|warning|info|danger|secondary|success|light|dark)
27 * @cfg {String} margin (0|1|2|3|4|5|auto)
28 * @cfg {String} margin_top (0|1|2|3|4|5|auto)
29 * @cfg {String} margin_bottom (0|1|2|3|4|5|auto)
30 * @cfg {String} margin_left (0|1|2|3|4|5|auto)
31 * @cfg {String} margin_right (0|1|2|3|4|5|auto)
32 * @cfg {String} margin_x (0|1|2|3|4|5|auto)
33 * @cfg {String} margin_y (0|1|2|3|4|5|auto)
35 * @cfg {String} padding (0|1|2|3|4|5)
36 * @cfg {String} padding_top (0|1|2|3|4|5)next_to_card
37 * @cfg {String} padding_bottom (0|1|2|3|4|5)
38 * @cfg {String} padding_left (0|1|2|3|4|5)
39 * @cfg {String} padding_right (0|1|2|3|4|5)
40 * @cfg {String} padding_x (0|1|2|3|4|5)
41 * @cfg {String} padding_y (0|1|2|3|4|5)
43 * @cfg {String} display (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
44 * @cfg {String} display_xs (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
45 * @cfg {String} display_sm (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
46 * @cfg {String} display_lg (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
47 * @cfg {String} display_xl (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
49 * @config {Boolean} dragable if this card can be dragged.
50 * @config {String} drag_group group for drag
51 * @config {Boolean} dropable if this card can recieve other cards being dropped onto it..
52 * @config {String} drop_group group for drag
54 * @config {Boolean} collapsable can the body be collapsed.
55 * @config {Boolean} collapsed is the body collapsed when rendered...
56 * @config {Boolean} rotateable can the body be rotated by clicking on it..
57 * @config {Boolean} rotated is the body rotated when rendered...
60 * Create a new Container
61 * @param {Object} config The config object
64 Roo.bootstrap.Card = function(config){
65 Roo.bootstrap.Card.superclass.constructor.call(this, config);
71 * When a element a card is dropped
72 * @param {Roo.bootstrap.Card} this
75 * @param {Roo.bootstrap.Card} move_card the card being dropped?
76 * @param {String} position 'above' or 'below'
77 * @param {Roo.bootstrap.Card} next_to_card What card position is relative to of 'false' for empty list.
83 * When a element a card is rotate
84 * @param {Roo.bootstrap.Card} this
85 * @param {Roo.Element} n the node being dropped?
86 * @param {Boolean} rotate status
91 * When a card element is dragged over ready to drop (return false to block dropable)
92 * @param {Roo.bootstrap.Card} this
93 * @param {Object} data from dragdrop
101 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component, {
106 margin: '', /// may be better in component?
145 childContainer : false,
146 dropEl : false, /// the dom placeholde element that indicates drop location.
147 containerEl: false, // body container
148 bodyEl: false, // card-body
149 headerContainerEl : false, //
151 header_imageEl : false,
154 layoutCls : function()
158 Roo.log(this.margin_bottom.length);
159 ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
160 // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
162 if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
163 cls += ' m' + (v.length ? v[0] : '') + '-' + t['margin' + (v.length ? '_' : '') + v];
165 if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
166 cls += ' p' + (v.length ? v[0] : '') + '-' + t['padding' + (v.length ? '_' : '') + v];
170 ['', 'xs', 'sm', 'lg', 'xl'].forEach(function(v) {
171 if (('' + t['display' + (v.length ? '_' : '') + v]).length) {
172 cls += ' d' + (v.length ? '-' : '') + v + '-' + t['display' + (v.length ? '_' : '') + v]
176 // more generic support?
184 // Roo.log("Call onRender: " + this.xtype);
185 /* We are looking at something like this.
187 <img src="..." class="card-img-top" alt="...">
188 <div class="card-body">
189 <h5 class="card-title">Card title</h5>
190 <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
192 >> this bit is really the body...
193 <div> << we will ad dthis in hopefully it will not break shit.
195 ** card text does not actually have any styling...
197 <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>
200 <a href="#" class="card-link">Card link</a>
203 <div class="card-footer">
204 <small class="text-muted">Last updated 3 mins ago</small>
208 getAutoCreate : function(){
216 if (this.weight.length && this.weight != 'light') {
217 cfg.cls += ' text-white';
219 cfg.cls += ' text-dark'; // need as it's nested..
221 if (this.weight.length) {
222 cfg.cls += ' bg-' + this.weight;
225 cfg.cls += ' ' + this.layoutCls();
229 if (this.header.length) {
231 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
232 cls : 'card-header ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
240 cls : 'card-header d-none ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
246 if (this.collapsable) {
249 cls : 'd-block user-select-none',
253 cls : 'roo-collapse-toggle fa fa-chevron-down float-right ' + (this.collapsed ? 'collapsed' : '')
258 hdr.cn.push(hdr_ctr);
263 cls: 'roo-card-header-ctr' + ( this.header.length ? '' : ' d-none'),
268 if (this.header_image.length) {
271 cls : 'card-img-top',
272 src: this.header_image // escape?
277 cls : 'card-img-top d-none'
283 cls : 'card-body' + (this.html === false ? ' d-none' : ''),
287 if (this.collapsable || this.rotateable) {
290 cls : 'roo-collapsable collapse ' + (this.collapsed || this.rotated ? '' : 'show'),
297 if (this.title.length) {
301 src: this.title // escape?
305 if (this.subtitle.length) {
309 src: this.subtitle // escape?
315 cls : 'roo-card-body-ctr'
318 if (this.html.length) {
324 // fixme ? handle objects?
326 if (this.footer.length) {
329 cls : 'card-footer ' + (this.rotated ? 'd-none' : ''),
334 cfg.cn.push({cls : 'card-footer d-none'});
343 getCardHeader : function()
345 var ret = this.el.select('.card-header',true).first();
346 if (ret.hasClass('d-none')) {
347 ret.removeClass('d-none');
352 getCardFooter : function()
354 var ret = this.el.select('.card-footer',true).first();
355 if (ret.hasClass('d-none')) {
356 ret.removeClass('d-none');
361 getCardImageTop : function()
363 var ret = this.header_imageEl;
364 if (ret.hasClass('d-none')) {
365 ret.removeClass('d-none');
371 getChildContainer : function()
377 return this.el.select('.roo-card-body-ctr',true).first();
380 initEvents: function()
382 this.bodyEl = this.el.select('.card-body',true).first();
383 this.containerEl = this.getChildContainer();
385 this.dragZone = new Roo.dd.DragZone(this.getEl(), {
386 containerScroll: true,
387 ddGroup: this.drag_group || 'default_card_drag_group'
389 this.dragZone.getDragData = this.getDragData.createDelegate(this);
392 this.dropZone = new Roo.dd.DropZone(this.el.select('.card-body',true).first() , {
393 containerScroll: true,
394 ddGroup: this.drop_group || 'default_card_drag_group'
396 this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
397 this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
398 this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
399 this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
400 this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
403 if (this.collapsable) {
404 this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
406 if (this.rotateable) {
407 this.el.select('.card-header',true).on('click', this.onToggleRotate, this);
409 this.collapsableEl = this.el.select('.roo-collapsable',true).first();
411 this.footerEl = this.el.select('.card-footer',true).first();
412 this.collapsableToggleEl = this.el.select('.roo-collapse-toggle',true).first();
413 this.headerContainerEl = this.el.select('.roo-card-header-ctr',true).first();
414 this.headerEl = this.el.select('.card-header',true).first();
417 this.el.addClass('roo-card-rotated');
418 this.fireEvent('rotate', this, true);
420 this.header_imageEl = this.el.select('.card-img-top',true).first();
421 this.header_imageEl.on('load', this.onHeaderImageLoad, this );
424 getDragData : function(e)
426 var target = this.getEl();
428 //this.handleSelection(e);
438 dragData.ddel = target.dom ; // the div element
439 Roo.log(target.getWidth( ));
440 dragData.ddel.style.width = target.getWidth() + 'px';
447 * Part of the Roo.dd.DropZone interface. If no target node is found, the
448 * whole Element becomes the target, and this causes the drop gesture to append.
453 position : 'below' or 'above'
454 card : relateive to card OBJECT (or true for no cards listed)
455 items_n : relative to nth item in list
456 card_n : relative to nth card in list
461 getTargetFromEvent : function(e, dragged_card_el)
463 var target = e.getTarget();
464 while ((target !== null) && (target.parentNode != this.containerEl.dom)) {
465 target = target.parentNode;
476 //Roo.log([ 'target' , target ? target.id : '--nothing--']);
477 // see if target is one of the 'cards'...
480 //Roo.log(this.items.length);
485 for (var i = 0;i< this.items.length;i++) {
487 if (!this.items[i].el.hasClass('card')) {
490 pos = this.getDropPoint(e, this.items[i].el.dom);
492 cards_len = ret.cards.length;
493 //Roo.log(this.items[i].el.dom.id);
494 ret.cards.push(this.items[i]);
496 if (ret.card_n < 0 && pos == 'above') {
497 ret.position = cards_len > 0 ? 'below' : pos;
498 ret.items_n = i > 0 ? i - 1 : 0;
499 ret.card_n = cards_len > 0 ? cards_len - 1 : 0;
500 ret.card = ret.cards[ret.card_n];
503 if (!ret.cards.length) {
505 ret.position = 'below';
509 // could not find a card.. stick it at the end..
510 if (ret.card_n < 0) {
511 ret.card_n = last_card_n;
512 ret.card = ret.cards[last_card_n];
513 ret.items_n = this.items.indexOf(ret.cards[last_card_n]);
514 ret.position = 'below';
517 if (this.items[ret.items_n].el == dragged_card_el) {
521 if (ret.position == 'below') {
522 var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
524 if (card_after && card_after.el == dragged_card_el) {
531 var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
533 if (card_before && card_before.el == dragged_card_el) {
540 onNodeEnter : function(n, dd, e, data){
543 onNodeOver : function(n, dd, e, data)
546 var target_info = this.getTargetFromEvent(e,data.source.el);
547 if (target_info === false) {
548 this.dropPlaceHolder('hide');
551 Roo.log(['getTargetFromEvent', target_info ]);
554 if (this.fireEvent('cardover', this, [ data ]) === false) {
558 this.dropPlaceHolder('show', target_info,data);
562 onNodeOut : function(n, dd, e, data){
563 this.dropPlaceHolder('hide');
566 onNodeDrop : function(n, dd, e, data)
569 // call drop - return false if
571 // this could actually fail - if the Network drops..
572 // we will ignore this at present..- client should probably reload
573 // the whole set of cards if stuff like that fails.
576 var info = this.getTargetFromEvent(e,data.source.el);
577 if (info === false) {
580 this.dropPlaceHolder('hide');
584 this.acceptCard(data.source, info.position, info.card, info.items_n);
588 firstChildCard : function()
590 for (var i = 0;i< this.items.length;i++) {
592 if (!this.items[i].el.hasClass('card')) {
595 return this.items[i];
597 return this.items.length ? this.items[this.items.length-1] : false; // don't try and put stuff after the cards...
602 * - card.acceptCard(move_card, info.position, info.card, info.items_n);
604 acceptCard : function(move_card, position, next_to_card )
606 if (this.fireEvent("drop", this, move_card, position, next_to_card) === false) {
610 var to_items_n = next_to_card ? this.items.indexOf(next_to_card) : 0;
612 move_card.parent().removeCard(move_card);
615 var dom = move_card.el.dom;
616 dom.style.width = ''; // clear with - which is set by drag.
618 if (next_to_card !== false && next_to_card !== true && next_to_card.el.dom.parentNode) {
619 var cardel = next_to_card.el.dom;
621 if (position == 'above' ) {
622 cardel.parentNode.insertBefore(dom, cardel);
623 } else if (cardel.nextSibling) {
624 cardel.parentNode.insertBefore(dom,cardel.nextSibling);
626 cardel.parentNode.append(dom);
630 this.containerEl.dom.append(dom);
633 //FIXME HANDLE card = true
635 // add this to the correct place in items.
637 // remove Card from items.
640 if (this.items.length) {
642 //Roo.log([info.items_n, info.position, this.items.length]);
643 for (var i =0; i < this.items.length; i++) {
644 if (i == to_items_n && position == 'above') {
645 nitems.push(move_card);
647 nitems.push(this.items[i]);
648 if (i == to_items_n && position == 'below') {
649 nitems.push(move_card);
655 this.items.push(move_card);
658 move_card.parentId = this.id;
664 removeCard : function(c)
666 this.items = this.items.filter(function(e) { return e != c });
669 dom.parentNode.removeChild(dom);
670 dom.style.width = ''; // clear with - which is set by drag.
675 /** Decide whether to drop above or below a View node. */
676 getDropPoint : function(e, n, dd)
681 if (n == this.containerEl.dom) {
684 var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
685 var c = t + (b - t) / 2;
686 var y = Roo.lib.Event.getPageY(e);
693 onToggleCollapse : function(e)
695 if (this.collapsed) {
696 this.el.select('.roo-collapse-toggle').removeClass('collapsed');
697 this.collapsableEl.addClass('show');
698 this.collapsed = false;
701 this.el.select('.roo-collapse-toggle').addClass('collapsed');
702 this.collapsableEl.removeClass('show');
703 this.collapsed = true;
708 onToggleRotate : function(e)
710 this.collapsableEl.removeClass('show');
711 this.footerEl.removeClass('d-none');
712 this.el.removeClass('roo-card-rotated');
713 this.el.removeClass('d-none');
716 this.collapsableEl.addClass('show');
717 this.rotated = false;
718 this.fireEvent('rotate', this, this.rotated);
721 this.el.addClass('roo-card-rotated');
722 this.footerEl.addClass('d-none');
723 this.el.select('.roo-collapsable').removeClass('show');
726 this.fireEvent('rotate', this, this.rotated);
730 dropPlaceHolder: function (action, info, data)
732 if (this.dropEl === false) {
733 this.dropEl = Roo.DomHelper.append(this.containerEl, {
737 this.dropEl.removeClass(['d-none', 'd-block']);
738 if (action == 'hide') {
740 this.dropEl.addClass('d-none');
743 // FIXME - info.card == true!!!
744 this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
746 if (info.card !== true) {
747 var cardel = info.card.el.dom;
749 if (info.position == 'above') {
750 cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
751 } else if (cardel.nextSibling) {
752 cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
754 cardel.parentNode.append(this.dropEl.dom);
758 this.containerEl.dom.append(this.dropEl.dom);
761 this.dropEl.addClass('d-block roo-card-dropzone');
763 this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
770 setHeaderText: function(html)
773 if (this.headerContainerEl) {
774 this.headerContainerEl.dom.innerHTML = html;
777 onHeaderImageLoad : function(ev, he)
779 if (!this.header_image_fit_square) {
783 var hw = he.naturalHeight / he.naturalWidth;
786 //var w = he.dom.naturalWidth;
789 he.style.position = 'relative';
791 var nw = (ww * (1/hw));
792 Roo.get(he).setSize( ww * (1/hw), ww);
793 he.style.left = ((ww - nw)/ 2) + 'px';
794 he.style.position = 'relative';