@kephas/ui
Version:
Provides a common infrastructure for UI components.
115 lines (110 loc) • 3.47 kB
JavaScript
import { __decorate, __metadata } from 'tslib';
import { Logger, LogLevel, AppService, Priority, SingletonAppServiceContract } from '@kephas/core';
/**
* Notification service.
*
* @export
* @class NotificationService
*/
let NotificationService = class NotificationService {
/**
* Creates an instance of Notification.
* @param {Logger} [logger] Optional. The logger.
* @memberof NotificationService
*/
constructor(logger) {
this.logger = logger || new Logger();
}
/**
* Notifies the message at error level.
*
* @param {*} data The information to be displayed.
* @memberof NotificationService
*/
notifyError(data) {
// debugger;
const severityString = data.severity || LogLevel[LogLevel.Error];
const severity = severityString === LogLevel[LogLevel.Warning]
? LogLevel.Warning
: LogLevel.Error;
this.show(this.formatData(data), severity);
}
/**
* Notifies the message at warning level.
*
* @param {*} data The information to be displayed.
* @memberof NotificationService
*/
notifyWarning(data) {
// debugger;
const severityString = data.severity || LogLevel[LogLevel.Warning];
const severity = severityString === LogLevel[LogLevel.Warning]
? LogLevel.Warning
: LogLevel.Error;
this.show(this.formatData(data), severity);
}
/**
* Notifies the message at information level.
*
* @param {*} data The information to be displayed.
* @memberof NotificationService
*/
notifyInfo(data) {
this.show(this.formatData(data), LogLevel.Info);
}
/**
* Shows the notification.
*
* @protected
* @param {*} formattedData The formatted data.
* @param {LogLevel} severity The severity.
* @memberof NotificationService
*/
show(formattedData, severity) {
this.logger.log(severity, null, formattedData);
}
/**
* Formats the data. By default it returns a formatted string.
*
* @protected
* @param {*} data The information to be formatted.
* @returns {string}
* @memberof NotificationService
*/
formatData(data) {
if (!data) {
return 'Unknown error. Please check the client and server logs for more information.';
}
if (data.message && data.url) {
return `${data.message} (url: ${data.url}).`;
}
if (typeof (data) === 'object') {
if (data.error) {
// this is the case of Kendo data objects.
if (typeof data.error === 'object') {
if (data.error.responseStatus) {
return data.error.responseStatus.message;
}
}
return data.error;
}
if (data.message) {
return data.message;
}
}
return data;
}
};
NotificationService = __decorate([
AppService({ overridePriority: Priority.Low }),
SingletonAppServiceContract(),
__metadata("design:paramtypes", [Logger])
], NotificationService);
/*
* Public API Surface of ui
*/
/**
* Generated bundle index. Do not edit.
*/
export { NotificationService };
//# sourceMappingURL=kephas-ui.mjs.map