HM-101. Добавлен сервис для вывода уведомлений (#41)

This commit is contained in:
Nikolay
2020-08-01 17:46:08 +03:00
committed by GitHub
parent 8c1daa5771
commit f7ddce5bb0
12 changed files with 185 additions and 0 deletions

View File

@ -1,5 +1,6 @@
/**
* Класс для создвания объектов с генерацией событий
* @class EmitService
*/
class EmitService {
/**

View File

@ -0,0 +1,94 @@
import * as bootstrap from 'bootstrap';
import NotifyMessage from '../components/notify-message';
import InfoIcon from '../img/notify_icon_info.svg';
import WarnIcon from '../img/notify_icon_warn.svg';
import SuccessIcon from '../img/notify_icon_success.svg';
/**
* @interface NotifyConfig
* @type {Object}
* @property {string} title - если надо установить свой заголовок
* @property {boolean} notHide - установить true, чтобы сообщение не закрывалось автоматически
* @property {number} delay - стандартно 3000 мс, можно установить свое значение
*/
/**
* Класс для работы с уведомлениями
* @class NotifyService
*/
class NotifyService {
/**
* Общий метод для отображения уведомлений
* @private
*/
_show = ({icon, title, message, autohide = false, delay = 3000}) => {
const messageElement = new NotifyMessage({
title,
message,
icon,
autohide,
});
const toast = new bootstrap.Toast(messageElement.mainNode, {
autohide,
delay,
});
if (autohide) {
setTimeout(() => {
messageElement.mainNode.remove();
}, delay + 500);
}
toast.show();
}
/**
* Сообщение со значкем Warning и заголовком "Внимание!", использовать для ошибок и предупреждений
* @param {string} message - сообщение уведомления
* @param {NotifyConfig} config - дополнительные настройки
*/
warn = (message, config = {}) => {
const {notHide, delay, title} = config;
this._show({
icon: WarnIcon,
title: title || 'Внимание!',
message,
autohide: !notHide,
delay,
});
}
/**
* Сообщение со значкем Info и заголовком "Сообщение", использовать для информационных сообщений
* @param {string} message - сообщение уведомления
* @param {NotifyConfig} config - дополнительные настройки
*/
info = (message, config = {}) => {
const {notHide, delay, title} = config;
this._show({
icon: InfoIcon,
title: title || 'Сообщение',
message,
autohide: !notHide,
delay,
});
}
/**
* Сообщение со значкем Success и заголовком "Выполнено!", использовать для сообщений об успешном действии
* @param {string} message - сообщение уведомления
* @param {NotifyConfig} config - дополнительные настройки
*/
success = (message, config = {}) => {
const {notHide, delay, title} = config;
this._show({
icon: SuccessIcon,
title: title || 'Выполнено!',
message,
autohide: !notHide,
delay,
});
}
}
const notify = new NotifyService();
export default notify;