notification.service.ts
2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {Injectable, Inject} from "ng-forward";
import {TranslatorService} from "./translator.service";
@Injectable()
@Inject("$log", "SweetAlert", TranslatorService)
export class NotificationService {
constructor(
private $log: ng.ILogService,
private SweetAlert: any,
private translatorService: TranslatorService
) { }
public static DEFAULT_ERROR_TITLE = "notification.error.default.title";
public static DEFAULT_ERROR_MESSAGE = "notification.error.default.message";
public static DEFAULT_SUCCESS_TIMER = 1000;
error({
message = NotificationService.DEFAULT_ERROR_MESSAGE,
title = NotificationService.DEFAULT_ERROR_TITLE,
showConfirmButton = true
} = {}) {
this.showMessage({ title: title, text: message, showConfirmButton: showConfirmButton, type: "error" });
}
httpError(status: number, data: any): boolean {
this.error({ message: `notification.http_error.${status}.message` });
return false; // return true to indicate that the error was already handled
}
success({
title,
message,
timer = NotificationService.DEFAULT_SUCCESS_TIMER
}) {
this.showMessage({ title: title, text: message, timer: timer });
}
confirmation({title, message, showCancelButton = true, type = "warning"}, confirmationFunction: Function) {
this.showMessage({ title: title, text: message, showCancelButton: showCancelButton, type: type, closeOnConfirm: false }, confirmationFunction);
}
private showMessage({title, text, type = "success", timer = null, showConfirmButton = true, showCancelButton = false, closeOnConfirm = true}, confirmationFunction: Function = null) {
this.$log.debug("Notification message:", title, text, type, this.translatorService.currentLanguage());
this.SweetAlert.swal({
title: this.translatorService.translate(title),
text: this.translatorService.translate(text),
type: type,
timer: timer,
showConfirmButton: showConfirmButton,
showCancelButton: showCancelButton,
closeOnConfirm: closeOnConfirm
}, confirmationFunction ? (isConfirm: boolean) => {
if (isConfirm) confirmationFunction();
} : null);
}
}