@igo2/core
Version:
284 lines (277 loc) • 13.2 kB
JavaScript
import * as i0 from '@angular/core';
import { makeEnvironmentProviders, NgModule, Injector, Inject, Injectable } from '@angular/core';
import { provideToastr, ToastrService } from 'ngx-toastr';
import * as i1 from '@igo2/core/config';
import * as i2 from '@igo2/core/language';
import { BehaviorSubject, forkJoin } from 'rxjs';
import { debounceTime, first } from 'rxjs/operators';
const TOASTR_CONFIG = {
positionClass: 'toast-bottom-right',
timeOut: 10000,
extendedTimeOut: 10000,
titleClass: 'toastr-message-title',
messageClass: 'toast-message',
closeButton: true,
progressBar: true,
enableHtml: true,
tapToDismiss: true,
maxOpened: 4,
preventDuplicates: true,
resetTimeoutOnDuplicate: true,
countDuplicates: false,
includeTitleDuplicates: true
};
function provideMessage() {
return makeEnvironmentProviders([provideToastr(TOASTR_CONFIG)]);
}
/**
* @deprecated import the provideMessage directly
*/
class IgoMessageModule {
static forRoot() {
return {
ngModule: IgoMessageModule,
providers: []
};
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.15", ngImport: i0, type: IgoMessageModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.15", ngImport: i0, type: IgoMessageModule });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.15", ngImport: i0, type: IgoMessageModule, providers: [provideMessage()] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.15", ngImport: i0, type: IgoMessageModule, decorators: [{
type: NgModule,
args: [{
imports: [],
providers: [provideMessage()],
exports: []
}]
}] });
var MessageType;
(function (MessageType) {
MessageType["ERROR"] = "error";
MessageType["ALERT"] = "warning";
// eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values
MessageType["WARNING"] = "warning";
MessageType["INFO"] = "info";
MessageType["SUCCESS"] = "success";
MessageType["SHOW"] = "show";
})(MessageType || (MessageType = {}));
class MessageService {
injector;
configService;
languageService;
messages$ = new BehaviorSubject([]);
options;
activeMessageTranslations = [];
constructor(injector, configService, languageService) {
this.injector = injector;
this.configService = configService;
this.languageService = languageService;
this.options = this.configService.getConfig('message');
this.languageService.language$.pipe(debounceTime(500)).subscribe(() => {
if (this.toastr.toasts.length === 0) {
this.activeMessageTranslations = [];
}
this.toastr.toasts.map((toast) => {
const activeMessageTranslation = this.activeMessageTranslations.find((amt) => amt.id === toast.toastId);
if (activeMessageTranslation) {
const translatedTextInterpolateParams = {
...activeMessageTranslation.textInterpolateParams
};
const translatedTitleInterpolateParams = {
...activeMessageTranslation.titleInterpolateParams
};
if (activeMessageTranslation.textInterpolateParams) {
Object.keys(activeMessageTranslation.textInterpolateParams).map((k) => {
if (k) {
translatedTextInterpolateParams[k] =
this.languageService.translate.instant(activeMessageTranslation.textInterpolateParams[k]);
}
});
}
if (activeMessageTranslation.titleInterpolateParams) {
Object.keys(activeMessageTranslation.titleInterpolateParams).map((k) => {
if (k) {
translatedTitleInterpolateParams[k] =
this.languageService.translate.instant(activeMessageTranslation.titleInterpolateParams[k]);
}
});
}
forkJoin([
this.languageService.translate.get(activeMessageTranslation.textKey, translatedTextInterpolateParams),
this.languageService.translate.get(activeMessageTranslation.titleKey, translatedTitleInterpolateParams)
])
.pipe(first())
.subscribe((res) => {
toast.toastRef.componentInstance.message = res[0];
toast.toastRef.componentInstance.title = res[1];
});
}
});
});
}
get toastr() {
return this.injector.get(ToastrService);
}
showError(httpError) {
httpError.error.caught = true;
return this.error(httpError.error.message, httpError.error.title);
}
message(message) {
const messageType = message.type;
this.toastr.toastrConfig.iconClasses[messageType] = `toast-${messageType}`;
this.messages$.next(this.messages$.value.concat([message]));
message.options = message.options || {};
const currentDate = new Date();
message.options.from = message.options.from
? message.options.from
: new Date('1 jan 1900');
message.options.to = message.options.to
? message.options.to
: new Date('1 jan 3000');
if (typeof message.options.from === 'string') {
message.options.from = new Date(Date.parse(message.options.from.replace(/-/g, ' ')));
}
if (typeof message.options.to === 'string') {
message.options.to = new Date(Date.parse(message.options.to.replace(/-/g, ' ')));
}
if (currentDate > message.options.from &&
currentDate < message.options.to) {
if (message.showIcon === false) {
this.toastr.toastrConfig.iconClasses[messageType] =
`toast-${messageType} toast-no-icon`;
}
message = this.handleTemplate(message);
if (message.text) {
let messageShown;
switch (message.type) {
case MessageType.SUCCESS:
messageShown = this.success(message.text, message.title, message.options, message.textInterpolateParams, message.titleInterpolateParams);
break;
case MessageType.ERROR:
messageShown = this.error(message.text, message.title, message.options, message.textInterpolateParams, message.titleInterpolateParams);
break;
case MessageType.INFO:
messageShown = this.info(message.text, message.title, message.options, message.textInterpolateParams, message.titleInterpolateParams);
break;
case MessageType.SHOW:
messageShown = this.show(message.text, message.title, message.options, message.textInterpolateParams, message.titleInterpolateParams);
break;
case MessageType.ALERT:
case MessageType.WARNING:
messageShown = this.alert(message.text, message.title, message.options, message.textInterpolateParams, message.titleInterpolateParams);
break;
default:
messageShown = this.info(message.text, message.title, message.options, message.textInterpolateParams, message.titleInterpolateParams);
break;
}
message.options.id = messageShown.toastId;
}
}
}
success(text, title = 'igo.core.message.success', options = {}, textInterpolateParams, titleInterpolateParams) {
return this.handleNgxToastr('success', text, title, options, textInterpolateParams, titleInterpolateParams);
}
error(text, title = 'igo.core.message.error', options = {}, textInterpolateParams, titleInterpolateParams) {
return this.handleNgxToastr('error', text, title, options, textInterpolateParams, titleInterpolateParams);
}
info(text, title = 'igo.core.message.info', options = {}, textInterpolateParams, titleInterpolateParams) {
return this.handleNgxToastr('info', text, title, options, textInterpolateParams, titleInterpolateParams);
}
alert(text, title = 'igo.core.message.alert', options = {}, textInterpolateParams, titleInterpolateParams) {
return this.handleNgxToastr('alert', text, title, options, textInterpolateParams, titleInterpolateParams);
}
show(text, title = 'igo.core.message.info', options = {}, textInterpolateParams, titleInterpolateParams) {
return this.handleNgxToastr('show', text, title, options, textInterpolateParams, titleInterpolateParams);
}
handleNgxToastr(type, text, title, options = {}, textInterpolateParams, titleInterpolateParams) {
const translatedTextInterpolateParams = { ...textInterpolateParams };
const translatedTitlenterpolateParams = { ...titleInterpolateParams };
if (textInterpolateParams) {
Object.keys(textInterpolateParams).map((k) => {
const value = textInterpolateParams[k];
if (value) {
translatedTextInterpolateParams[k] =
typeof value === 'string'
? this.languageService.translate.instant(value)
: value;
}
});
}
if (titleInterpolateParams) {
Object.keys(titleInterpolateParams).map((k) => {
if (k) {
const value = titleInterpolateParams[k];
translatedTitlenterpolateParams[k] =
typeof value === 'string'
? this.languageService.translate.instant(value)
: value;
}
});
}
const message = this.languageService.translate.instant(text, translatedTextInterpolateParams);
const translatedTitle = this.languageService.translate.instant(title, translatedTitlenterpolateParams);
let activeToast;
switch (type) {
case 'success':
activeToast = this.toastr.success(message, translatedTitle, options);
break;
case 'error':
activeToast = this.toastr.error(message, translatedTitle, options);
break;
case 'show':
case 'info':
activeToast = this.toastr.info(message, translatedTitle, options);
break;
case 'alert':
activeToast = this.toastr.warning(message, translatedTitle, options);
break;
}
this.activeMessageTranslations.push({
id: activeToast.toastId,
titleKey: title,
textKey: text,
textInterpolateParams,
titleInterpolateParams
});
return activeToast;
}
remove(id) {
this.toastr.remove(id);
}
removeAllAreNotError() {
for (const mess of this.messages$.value) {
if (mess.type !== MessageType.ERROR) {
this.remove(mess.options.id);
}
}
}
handleTemplate(message) {
if (!this.options?.template || message.html) {
return message;
}
let html = this.options?.template;
html = html.replace('${text}', message.text);
html = html.replace('${title}', message.title);
message.html = undefined;
message.text = html;
message.title = undefined;
return message;
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.15", ngImport: i0, type: MessageService, deps: [{ token: Injector }, { token: i1.ConfigService }, { token: i2.LanguageService }], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.15", ngImport: i0, type: MessageService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.15", ngImport: i0, type: MessageService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: () => [{ type: i0.Injector, decorators: [{
type: Inject,
args: [Injector]
}] }, { type: i1.ConfigService }, { type: i2.LanguageService }] });
/**
* Generated bundle index. Do not edit.
*/
export { IgoMessageModule, MessageService, MessageType, provideMessage };
//# sourceMappingURL=igo2-core-message.mjs.map