2 * @class Roo.bootstrap.Card
3 * @extends Roo.bootstrap.Component
4 * @children Roo.bootstrap.Component
6 * Bootstrap Card class - note this has children as CardHeader/ImageTop/Footer.. - which should really be listed properties?
9 * possible... may not be implemented..
10 * @cfg {String} header_image src url of image.
11 * @cfg {String|Object} header
12 * @cfg {Number} header_size (0|1|2|3|4|5) H1 or H2 etc.. 0 indicates default
13 * @cfg {Number} header_weight (primary|secondary|success|info|warning|danger|light|dark)
16 * @cfg {String} subtitle
17 * @cfg {String|Boolean} html -- html contents - or just use children.. use false to hide it..
18 * @cfg {String} footer
20 * @cfg {String} weight (primary|warning|info|danger|secondary|success|light|dark)
22 * @cfg {String} margin (0|1|2|3|4|5|auto)
23 * @cfg {String} margin_top (0|1|2|3|4|5|auto)
24 * @cfg {String} margin_bottom (0|1|2|3|4|5|auto)
25 * @cfg {String} margin_left (0|1|2|3|4|5|auto)
26 * @cfg {String} margin_right (0|1|2|3|4|5|auto)
27 * @cfg {String} margin_x (0|1|2|3|4|5|auto)
28 * @cfg {String} margin_y (0|1|2|3|4|5|auto)
30 * @cfg {String} padding (0|1|2|3|4|5)
31 * @cfg {String} padding_top (0|1|2|3|4|5)next_to_card
32 * @cfg {String} padding_bottom (0|1|2|3|4|5)
33 * @cfg {String} padding_left (0|1|2|3|4|5)
34 * @cfg {String} padding_right (0|1|2|3|4|5)
35 * @cfg {String} padding_x (0|1|2|3|4|5)
36 * @cfg {String} padding_y (0|1|2|3|4|5)
38 * @cfg {String} display (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
39 * @cfg {String} display_xs (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
40 * @cfg {String} display_sm (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
41 * @cfg {String} display_lg (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
42 * @cfg {String} display_xl (none|inline|inline-block|block|table|table-cell|table-row|flex|inline-flex)
44 * @config {Boolean} dragable if this card can be dragged.
45 * @config {String} drag_group group for drag
46 * @config {Boolean} dropable if this card can recieve other cards being dropped onto it..
47 * @config {String} drop_group group for drag
49 * @config {Boolean} collapsable can the body be collapsed.
50 * @config {Boolean} collapsed is the body collapsed when rendered...
51 * @config {Boolean} rotateable can the body be rotated by clicking on it..
52 * @config {Boolean} rotated is the body rotated when rendered...
55 * Create a new Container
56 * @param {Object} config The config object
59 Roo.bootstrap.Card = function(config){
60 Roo.bootstrap.Card.superclass.constructor.call(this, config);
66 * When a element a card is dropped
67 * @param {Roo.bootstrap.Card} this
70 * @param {Roo.bootstrap.Card} move_card the card being dropped?
71 * @param {String} position 'above' or 'below'
72 * @param {Roo.bootstrap.Card} next_to_card What card position is relative to of 'false' for empty list.
78 * When a element a card is rotate
79 * @param {Roo.bootstrap.Card} this
80 * @param {Roo.Element} n the node being dropped?
81 * @param {Boolean} rotate status
86 * When a card element is dragged over ready to drop (return false to block dropable)
87 * @param {Roo.bootstrap.Card} this
88 * @param {Object} data from dragdrop
96 Roo.extend(Roo.bootstrap.Card, Roo.bootstrap.Component, {
101 margin: '', /// may be better in component?
140 childContainer : false,
141 dropEl : false, /// the dom placeholde element that indicates drop location.
142 containerEl: false, // body container
143 bodyEl: false, // card-body
144 headerContainerEl : false, //
146 header_imageEl : false,
149 layoutCls : function()
153 Roo.log(this.margin_bottom.length);
154 ['', 'top', 'bottom', 'left', 'right', 'x', 'y' ].forEach(function(v) {
155 // in theory these can do margin_top : ml-xs-3 ??? but we don't support that yet
157 if (('' + t['margin' + (v.length ? '_' : '') + v]).length) {
158 cls += ' m' + (v.length ? v[0] : '') + '-' + t['margin' + (v.length ? '_' : '') + v];
160 if (('' + t['padding' + (v.length ? '_' : '') + v]).length) {
161 cls += ' p' + (v.length ? v[0] : '') + '-' + t['padding' + (v.length ? '_' : '') + v];
165 ['', 'xs', 'sm', 'lg', 'xl'].forEach(function(v) {
166 if (('' + t['display' + (v.length ? '_' : '') + v]).length) {
167 cls += ' d' + (v.length ? '-' : '') + v + '-' + t['display' + (v.length ? '_' : '') + v]
171 // more generic support?
179 // Roo.log("Call onRender: " + this.xtype);
180 /* We are looking at something like this.
182 <img src="..." class="card-img-top" alt="...">
183 <div class="card-body">
184 <h5 class="card-title">Card title</h5>
185 <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
187 >> this bit is really the body...
188 <div> << we will ad dthis in hopefully it will not break shit.
190 ** card text does not actually have any styling...
192 <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>
195 <a href="#" class="card-link">Card link</a>
198 <div class="card-footer">
199 <small class="text-muted">Last updated 3 mins ago</small>
203 getAutoCreate : function(){
211 if (this.weight.length && this.weight != 'light') {
212 cfg.cls += ' text-white';
214 cfg.cls += ' text-dark'; // need as it's nested..
216 if (this.weight.length) {
217 cfg.cls += ' bg-' + this.weight;
220 cfg.cls += ' ' + this.layoutCls();
224 if (this.header.length) {
226 tag : this.header_size > 0 ? 'h' + this.header_size : 'div',
227 cls : 'card-header ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
235 cls : 'card-header d-none ' + (this.header_weight ? 'bg-' + this.header_weight : ''),
241 if (this.collapsable) {
244 cls : 'd-block user-select-none',
248 cls : 'roo-collapse-toggle fa fa-chevron-down float-right ' + (this.collapsed ? 'collapsed' : '')
253 hdr.cn.push(hdr_ctr);
258 cls: 'roo-card-header-ctr' + ( this.header.length ? '' : ' d-none'),
263 if (this.header_image.length) {
266 cls : 'card-img-top',
267 src: this.header_image // escape?
272 cls : 'card-img-top d-none'
278 cls : 'card-body' + (this.html === false ? ' d-none' : ''),
282 if (this.collapsable || this.rotateable) {
285 cls : 'roo-collapsable collapse ' + (this.collapsed || this.rotated ? '' : 'show'),
292 if (this.title.length) {
296 src: this.title // escape?
300 if (this.subtitle.length) {
304 src: this.subtitle // escape?
310 cls : 'roo-card-body-ctr'
313 if (this.html.length) {
319 // fixme ? handle objects?
321 if (this.footer.length) {
324 cls : 'card-footer ' + (this.rotated ? 'd-none' : ''),
329 cfg.cn.push({cls : 'card-footer d-none'});
338 getCardHeader : function()
340 var ret = this.el.select('.card-header',true).first();
341 if (ret.hasClass('d-none')) {
342 ret.removeClass('d-none');
347 getCardFooter : function()
349 var ret = this.el.select('.card-footer',true).first();
350 if (ret.hasClass('d-none')) {
351 ret.removeClass('d-none');
356 getCardImageTop : function()
358 var ret = this.header_imageEl;
359 if (ret.hasClass('d-none')) {
360 ret.removeClass('d-none');
366 getChildContainer : function()
372 return this.el.select('.roo-card-body-ctr',true).first();
375 initEvents: function()
377 this.bodyEl = this.el.select('.card-body',true).first();
378 this.containerEl = this.getChildContainer();
380 this.dragZone = new Roo.dd.DragZone(this.getEl(), {
381 containerScroll: true,
382 ddGroup: this.drag_group || 'default_card_drag_group'
384 this.dragZone.getDragData = this.getDragData.createDelegate(this);
387 this.dropZone = new Roo.dd.DropZone(this.el.select('.card-body',true).first() , {
388 containerScroll: true,
389 ddGroup: this.drop_group || 'default_card_drag_group'
391 this.dropZone.getTargetFromEvent = this.getTargetFromEvent.createDelegate(this);
392 this.dropZone.onNodeEnter = this.onNodeEnter.createDelegate(this);
393 this.dropZone.onNodeOver = this.onNodeOver.createDelegate(this);
394 this.dropZone.onNodeOut = this.onNodeOut.createDelegate(this);
395 this.dropZone.onNodeDrop = this.onNodeDrop.createDelegate(this);
398 if (this.collapsable) {
399 this.el.select('.card-header',true).on('click', this.onToggleCollapse, this);
401 if (this.rotateable) {
402 this.el.select('.card-header',true).on('click', this.onToggleRotate, this);
404 this.collapsableEl = this.el.select('.roo-collapsable',true).first();
406 this.footerEl = this.el.select('.card-footer',true).first();
407 this.collapsableToggleEl = this.el.select('.roo-collapse-toggle',true).first();
408 this.headerContainerEl = this.el.select('.roo-card-header-ctr',true).first();
409 this.headerEl = this.el.select('.card-header',true).first();
412 this.el.addClass('roo-card-rotated');
413 this.fireEvent('rotate', this, true);
415 this.header_imageEl = this.el.select('.card-img-top',true).first();
416 this.header_imageEl.on('load', this.onHeaderImageLoad, this );
419 getDragData : function(e)
421 var target = this.getEl();
423 //this.handleSelection(e);
433 dragData.ddel = target.dom ; // the div element
434 Roo.log(target.getWidth( ));
435 dragData.ddel.style.width = target.getWidth() + 'px';
442 * Part of the Roo.dd.DropZone interface. If no target node is found, the
443 * whole Element becomes the target, and this causes the drop gesture to append.
448 position : 'below' or 'above'
449 card : relateive to card OBJECT (or true for no cards listed)
450 items_n : relative to nth item in list
451 card_n : relative to nth card in list
456 getTargetFromEvent : function(e, dragged_card_el)
458 var target = e.getTarget();
459 while ((target !== null) && (target.parentNode != this.containerEl.dom)) {
460 target = target.parentNode;
471 //Roo.log([ 'target' , target ? target.id : '--nothing--']);
472 // see if target is one of the 'cards'...
475 //Roo.log(this.items.length);
480 for (var i = 0;i< this.items.length;i++) {
482 if (!this.items[i].el.hasClass('card')) {
485 pos = this.getDropPoint(e, this.items[i].el.dom);
487 cards_len = ret.cards.length;
488 //Roo.log(this.items[i].el.dom.id);
489 ret.cards.push(this.items[i]);
491 if (ret.card_n < 0 && pos == 'above') {
492 ret.position = cards_len > 0 ? 'below' : pos;
493 ret.items_n = i > 0 ? i - 1 : 0;
494 ret.card_n = cards_len > 0 ? cards_len - 1 : 0;
495 ret.card = ret.cards[ret.card_n];
498 if (!ret.cards.length) {
500 ret.position = 'below';
504 // could not find a card.. stick it at the end..
505 if (ret.card_n < 0) {
506 ret.card_n = last_card_n;
507 ret.card = ret.cards[last_card_n];
508 ret.items_n = this.items.indexOf(ret.cards[last_card_n]);
509 ret.position = 'below';
512 if (this.items[ret.items_n].el == dragged_card_el) {
516 if (ret.position == 'below') {
517 var card_after = ret.card_n+1 == ret.cards.length ? false : ret.cards[ret.card_n+1];
519 if (card_after && card_after.el == dragged_card_el) {
526 var card_before = ret.card_n > 0 ? ret.cards[ret.card_n-1] : false;
528 if (card_before && card_before.el == dragged_card_el) {
535 onNodeEnter : function(n, dd, e, data){
538 onNodeOver : function(n, dd, e, data)
541 var target_info = this.getTargetFromEvent(e,data.source.el);
542 if (target_info === false) {
543 this.dropPlaceHolder('hide');
546 Roo.log(['getTargetFromEvent', target_info ]);
549 if (this.fireEvent('cardover', this, [ data ]) === false) {
553 this.dropPlaceHolder('show', target_info,data);
557 onNodeOut : function(n, dd, e, data){
558 this.dropPlaceHolder('hide');
561 onNodeDrop : function(n, dd, e, data)
564 // call drop - return false if
566 // this could actually fail - if the Network drops..
567 // we will ignore this at present..- client should probably reload
568 // the whole set of cards if stuff like that fails.
571 var info = this.getTargetFromEvent(e,data.source.el);
572 if (info === false) {
575 this.dropPlaceHolder('hide');
579 this.acceptCard(data.source, info.position, info.card, info.items_n);
583 firstChildCard : function()
585 for (var i = 0;i< this.items.length;i++) {
587 if (!this.items[i].el.hasClass('card')) {
590 return this.items[i];
592 return this.items.length ? this.items[this.items.length-1] : false; // don't try and put stuff after the cards...
597 * - card.acceptCard(move_card, info.position, info.card, info.items_n);
599 acceptCard : function(move_card, position, next_to_card )
601 if (this.fireEvent("drop", this, move_card, position, next_to_card) === false) {
605 var to_items_n = next_to_card ? this.items.indexOf(next_to_card) : 0;
607 move_card.parent().removeCard(move_card);
610 var dom = move_card.el.dom;
611 dom.style.width = ''; // clear with - which is set by drag.
613 if (next_to_card !== false && next_to_card !== true && next_to_card.el.dom.parentNode) {
614 var cardel = next_to_card.el.dom;
616 if (position == 'above' ) {
617 cardel.parentNode.insertBefore(dom, cardel);
618 } else if (cardel.nextSibling) {
619 cardel.parentNode.insertBefore(dom,cardel.nextSibling);
621 cardel.parentNode.append(dom);
625 this.containerEl.dom.append(dom);
628 //FIXME HANDLE card = true
630 // add this to the correct place in items.
632 // remove Card from items.
635 if (this.items.length) {
637 //Roo.log([info.items_n, info.position, this.items.length]);
638 for (var i =0; i < this.items.length; i++) {
639 if (i == to_items_n && position == 'above') {
640 nitems.push(move_card);
642 nitems.push(this.items[i]);
643 if (i == to_items_n && position == 'below') {
644 nitems.push(move_card);
650 this.items.push(move_card);
653 move_card.parentId = this.id;
659 removeCard : function(c)
661 this.items = this.items.filter(function(e) { return e != c });
664 dom.parentNode.removeChild(dom);
665 dom.style.width = ''; // clear with - which is set by drag.
670 /** Decide whether to drop above or below a View node. */
671 getDropPoint : function(e, n, dd)
676 if (n == this.containerEl.dom) {
679 var t = Roo.lib.Dom.getY(n), b = t + n.offsetHeight;
680 var c = t + (b - t) / 2;
681 var y = Roo.lib.Event.getPageY(e);
688 onToggleCollapse : function(e)
690 if (this.collapsed) {
691 this.el.select('.roo-collapse-toggle').removeClass('collapsed');
692 this.collapsableEl.addClass('show');
693 this.collapsed = false;
696 this.el.select('.roo-collapse-toggle').addClass('collapsed');
697 this.collapsableEl.removeClass('show');
698 this.collapsed = true;
703 onToggleRotate : function(e)
705 this.collapsableEl.removeClass('show');
706 this.footerEl.removeClass('d-none');
707 this.el.removeClass('roo-card-rotated');
708 this.el.removeClass('d-none');
711 this.collapsableEl.addClass('show');
712 this.rotated = false;
713 this.fireEvent('rotate', this, this.rotated);
716 this.el.addClass('roo-card-rotated');
717 this.footerEl.addClass('d-none');
718 this.el.select('.roo-collapsable').removeClass('show');
721 this.fireEvent('rotate', this, this.rotated);
725 dropPlaceHolder: function (action, info, data)
727 if (this.dropEl === false) {
728 this.dropEl = Roo.DomHelper.append(this.containerEl, {
732 this.dropEl.removeClass(['d-none', 'd-block']);
733 if (action == 'hide') {
735 this.dropEl.addClass('d-none');
738 // FIXME - info.card == true!!!
739 this.dropEl.dom.parentNode.removeChild(this.dropEl.dom);
741 if (info.card !== true) {
742 var cardel = info.card.el.dom;
744 if (info.position == 'above') {
745 cardel.parentNode.insertBefore(this.dropEl.dom, cardel);
746 } else if (cardel.nextSibling) {
747 cardel.parentNode.insertBefore(this.dropEl.dom,cardel.nextSibling);
749 cardel.parentNode.append(this.dropEl.dom);
753 this.containerEl.dom.append(this.dropEl.dom);
756 this.dropEl.addClass('d-block roo-card-dropzone');
758 this.dropEl.setHeight( Roo.get(data.ddel).getHeight() );
765 setHeaderText: function(html)
768 if (this.headerContainerEl) {
769 this.headerContainerEl.dom.innerHTML = html;
772 onHeaderImageLoad : function(ev, he)
774 if (!this.header_image_fit_square) {
778 var hw = he.naturalHeight / he.naturalWidth;
781 //var w = he.dom.naturalWidth;
784 he.style.position = 'relative';
786 var nw = (ww * (1/hw));
787 Roo.get(he).setSize( ww * (1/hw), ww);
788 he.style.left = ((ww - nw)/ 2) + 'px';
789 he.style.position = 'relative';