mirror of
https://bitbucket.org/bellord13/kit.git
synced 2026-11-13 19:06:11 +03:00
79 lines
2.9 KiB
JavaScript
79 lines
2.9 KiB
JavaScript
/** @module bsToastService */
|
|
kit.PluginManager.register('bsToastService', function(module, exports, require, global){
|
|
var support = require('support');
|
|
var bsToast = require('bsToast');
|
|
var Collection = require('Collection');
|
|
|
|
|
|
function Service() {
|
|
this.close = true;
|
|
this.option = {
|
|
animation: true,
|
|
autohide: true,
|
|
delay: 4000,
|
|
};
|
|
this.closeTemplate = '<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Закрыть"/>';
|
|
this.iconTemplate = '<svg class="bd-placeholder-img rounded me-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" preserveAspectRatio="xMidYMid slice" focusable="false"><rect width="100%" height="100%" fill="#007aff"/></svg>';
|
|
/**
|
|
* @type {HTMLElement}
|
|
*/
|
|
this.container = null;
|
|
this.onShow = new Collection();
|
|
}
|
|
|
|
|
|
Service.prototype.setContainer = function (parentNode = document.body, opClass = 'default') {
|
|
parentNode = support.getNode(parentNode);
|
|
if(opClass) {
|
|
opClass = 'toast-container--'+opClass;
|
|
}
|
|
|
|
var opClassSelector = (opClass !== '')?('.'+opClass):'';
|
|
var container = parentNode.querySelector('.toast-container'+opClassSelector);
|
|
if(container) {
|
|
this.container = container;
|
|
return;
|
|
}
|
|
|
|
var toastContainerTemplate = '<div class="toast-container '+opClass+'"></div>';
|
|
this.container = support.makeNode(toastContainerTemplate);
|
|
if(support.isElement(parentNode)) {
|
|
parentNode.appendChild(this.container);
|
|
}
|
|
};
|
|
|
|
|
|
Service.prototype.getToastNode = function (title, message, desc = '') {
|
|
var closeTemplate = this.close?this.closeTemplate:'';
|
|
var toastTemplate = '<div class="toast fade hide" role="alert" aria-live="assertive" aria-atomic="true"><div class="toast-header"><div class="toast-icon">'+this.iconTemplate+'</div><strong class="toast-title">'+title+'</strong><small class="toast-desc">' + desc+ '</small>'+closeTemplate+'</div><div class="toast-body">'+message+'</div></div>';
|
|
return support.makeNode(toastTemplate);
|
|
};
|
|
|
|
Service.prototype.info = function (title, message, desc = '') {
|
|
var toastNode = this.getToastNode(title, message, desc);
|
|
var toast = new bsToast(toastNode, this.option);
|
|
toast.data = {
|
|
title: title,
|
|
message: message,
|
|
desc: desc,
|
|
date: new Date()
|
|
};
|
|
support.setKit(toastNode, 'toast', toast);
|
|
this.container.appendChild(toastNode);
|
|
toast.show();
|
|
this.onShow.eachCall(this, toast, toastNode);
|
|
};
|
|
|
|
|
|
/**
|
|
* @memberOf kit.service
|
|
* @class toast
|
|
**/
|
|
var toast = new Service();
|
|
toast.setContainer();
|
|
|
|
global.service.toast = toast;
|
|
|
|
module.exports = toast;
|
|
});
|