interface INotifyOptions { globalPosition?: string, autoHideDelay?: number, className?: string showAnimation?: string, showDuration?: number, hideAnimation?: string, hideDuration?: number, autoHide?: boolean, } abstract class MessageShower { static readonly singletonContentSelector = "span[data-notify-text]"; static singletonObject = null; static hideSingletonTimeout?: number; static readonly defaultOptions: any = { globalPosition: 'right top', autoHideDelay: 5000, showAnimation: 'slideDown', // show animation duration showDuration: 400, // hide animation hideAnimation: 'slideUp', // hide animation duration hideDuration: 200, } public static showMessageParams(message: string, options?: INotifyOptions) { ($ as any).notify(message, Object.assign(options, this.defaultOptions)); } public static showError(message) { this.showMessageParams(message, { className: "error" }); } /** * Показывает сообщение об ошибке, которое выкинул new BadUserInputException() или Конкарренси * Парсит нужное поле автоматом. * @param text * @param newClass */ public static showErrorBadUserInput(data) { try { if (data.statusText == "Conflict") { this.showMessageParams("Пожалуйста, обновите страницу. Данные на странице устарели. Внимание! Последние изменения могут быть утеряны.", { className: "error", autoHide: false }); } else { this.showMessageParams(data.responseJSON.error.Message, { className: "error" }); } } catch (e) { this.showSaveDataError(); } } public static showMessage(message) { this.showMessageParams(message, { className: "info" }); } public static showSuccess(message) { this.showMessageParams(message, { className: "success" }); } /** * Показывает сообщение в 1м экземпляре. * При измении повторном вызове - заменяет текст у текущего сообщения на новый. * Возможно менять стили на лету. * @param text * @param newClass */ public static showSingleton(text: string, newClass: string = "info") { if (MessageShower.singletonObject && MessageShower.singletonObject.length > 0 || $(MessageShower.singletonContentSelector).length > 0) { MessageShower.changeTextInSingleton(text, newClass); return; } this.showMessageParams(MessageShower.singletonContentSelector, { className: "info", autoHide: false }); this.singletonObject = $(`${MessageShower.singletonContentSelector}:contains(${MessageShower.singletonContentSelector})`).parents(".notifyjs-corner"); MessageShower.changeTextInSingleton(text, newClass); } public static showSaveDataError() { MessageShower.showError("Не удалось сохранить изменения. Пожалуйста, обновите страницу"); } private static changeTextInSingleton(text: string, newClass?: string) { let textItem = $(MessageShower.singletonContentSelector); if (textItem && textItem.length) { textItem.text(text); if (newClass) { let container = textItem.parents(".notifyjs-bootstrap-base"); container.removeClass("notifyjs-bootstrap-info"); container.removeClass("notifyjs-bootstrap-success"); container.removeClass("notifyjs-bootstrap-error"); container.addClass("notifyjs-bootstrap-" + newClass); } } if (MessageShower.hideSingletonTimeout) { clearTimeout(MessageShower.hideSingletonTimeout); } MessageShower.hideSingletonTimeout = setTimeout(() => { if (MessageShower.singletonObject && MessageShower.singletonObject.length > 0) { MessageShower.singletonObject.find(MessageShower.singletonContentSelector).click(); MessageShower.singletonObject = null; MessageShower.hideSingletonTimeout = null; } }, 4000); } }