gd-bs
Version:
Bootstrap JavaScript, TypeScript and Web Components library.
120 lines (119 loc) • 4.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CardBody = void 0;
var common_1 = require("../common");
var action_1 = require("./action");
var templates_1 = require("./templates");
/**
* Card Body
*/
var CardBody = /** @class */ (function () {
// Constructor
function CardBody(props) {
this._el = null;
this._props = null;
// Save the properties
this._props = props;
// Create the item
var elItem = document.createElement("div");
elItem.innerHTML = templates_1.HTMLItem;
this._el = elItem.firstChild;
// Configure the item
this.configure();
// Configure the events
this.configureEvents();
}
// Configure the body
CardBody.prototype.configure = function () {
// Set the class names
if (this._props.className) {
// Set the class names
(0, common_1.setClassNames)(this._el, this._props.className);
}
// Update the title
var elTitle = this._el.querySelector(".card-title");
if (this._props.title || this._props.onRenderTitle) {
// Append the title
(0, common_1.appendContent)(elTitle, this._props.title);
// Call the render event
this._props.onRenderTitle ? this._props.onRenderTitle(elTitle, this._props) : null;
}
else {
// Remove the title
this._el.removeChild(elTitle);
}
// Update the sub-title
var subTitle = this._el.querySelector(".card-subtitle");
if (this._props.subTitle) {
// Set the sub-title
subTitle.innerHTML = this._props.subTitle;
}
else {
// Remove the sub-title
this._el.removeChild(subTitle);
}
// Update the text
var text = this._el.querySelector(".card-text");
if (this._props.text) {
// Set the text
text.innerHTML = this._props.text;
}
// Else, see if there is content
else if (this._props.content && typeof (this._props.content) === "string") {
// Set the text
text.innerHTML = this._props.content;
}
else {
// Remove the text
this._el.removeChild(text);
}
// See if the content is an element
if (this._props.content && typeof (this._props.content) !== "string") {
// Append the content
(0, common_1.appendContent)(this._el, this._props.content);
}
// Render the actions
this.renderActions();
};
// Configure the events
CardBody.prototype.configureEvents = function () {
var _this = this;
// Call the render event if it exists
this._props.onRender ? this._props.onRender(this._el, this._props) : null;
// See if there is a click event
if (this._props.onClick) {
// Set the click event
this._el.addEventListener("click", function (ev) {
// Execute the event
_this._props.onClick(_this._props, ev);
});
}
};
// Render the card actions
CardBody.prototype.renderActions = function () {
// Parse the actions
var actions = this._props.actions || [];
for (var i = 0; i < actions.length; i++) {
// Add the action
var action = new action_1.CardAction(actions[i], this);
this._el.appendChild(action.el);
}
};
Object.defineProperty(CardBody.prototype, "el", {
/**
* Public Interface
*/
// The component HTML element
get: function () { return this._el; },
enumerable: false,
configurable: true
});
Object.defineProperty(CardBody.prototype, "props", {
// The component properties
get: function () { return this._props; },
enumerable: false,
configurable: true
});
return CardBody;
}());
exports.CardBody = CardBody;