laravel-jstools
Version:
JS tools for building front-side of Laravel applications
38 lines (37 loc) • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToastNotyService = void 0;
const laravel_jstools_di_1 = require("laravel-jstools-di");
class ToastNotyService extends laravel_jstools_di_1.Service {
constructor(containerId, tools) {
super();
this.containerId = containerId;
this.tools = tools;
this.containerEl = document.getElementById(containerId);
}
show(data) {
if (data.message) {
const toastId = this.generateId();
this.containerEl.innerHTML += this.html(toastId, data.context, data.message);
const toastEl = document.getElementById(toastId);
toastEl.addEventListener('hidden.bs.toast', () => {
this.containerEl.removeChild(toastEl);
});
new this.tools.bootstrap.Toast(`#${toastId}`, { delay: data.delay || 5000 }).show();
}
}
generateId() {
return `toast-${+new Date()}`;
}
html(id, context, message) {
return `
<div id="${id}" class="toast align-items-center text-bg-${context} border-0" role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div class="toast-body">${message}</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
`;
}
}
exports.ToastNotyService = ToastNotyService;