-
Notifications
You must be signed in to change notification settings - Fork 639
/
Notification.ts
201 lines (185 loc) · 5.54 KB
/
Notification.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
import {SeverityEnum} from './Enum/Severity';
import Severity = require('./Severity');
/**
* Module: TYPO3/CMS/Backend/Notification
* Notification API for the TYPO3 backend
*/
class Notification {
private static duration: number = 5;
private static messageContainer: JQuery = null;
/**
* Show a notice notification
*
* @param {string} title
* @param {string} message
* @param {number} duration
*/
public static notice(title: string, message: string, duration: number): void {
Notification.showMessage(title, message, SeverityEnum.notice, duration);
}
/**
* Show a info notification
*
* @param {string} title
* @param {string} message
* @param {number} duration
*/
public static info(title: string, message: string, duration: number): void {
Notification.showMessage(title, message, SeverityEnum.info, duration);
}
/**
* Show a success notification
*
* @param {string} title
* @param {string} message
* @param {number} duration
*/
public static success(title: string, message: string, duration: number): void {
Notification.showMessage(title, message, SeverityEnum.ok, duration);
}
/**
* Show a warning notification
*
* @param {string} title
* @param {string} message
* @param {number} duration
*/
public static warning(title: string, message: string, duration: number): void {
Notification.showMessage(title, message, SeverityEnum.warning, duration);
}
/**
* Show a error notification
*
* @param {string} title
* @param {string} message
* @param {number} duration
*/
public static error(title: string, message: string, duration: number = 0): void {
Notification.showMessage(title, message, SeverityEnum.error, duration);
}
/**
* @param {string} title
* @param {string} message
* @param {SeverityEnum} severity
* @param {number} duration
*/
public static showMessage(title: string,
message: string,
severity: SeverityEnum,
duration: number | string = this.duration): void {
const className = Severity.getCssClass(severity);
let icon = '';
switch (severity) {
case SeverityEnum.notice:
icon = 'lightbulb-o';
break;
case SeverityEnum.ok:
icon = 'check';
break;
case SeverityEnum.warning:
icon = 'exclamation';
break;
case SeverityEnum.error:
icon = 'times';
break;
case SeverityEnum.info:
default:
icon = 'info';
}
duration = (typeof duration === 'undefined')
? this.duration
: (
typeof duration === 'string'
? parseFloat(duration)
: duration
);
if (this.messageContainer === null) {
this.messageContainer = $('<div id="alert-container"></div>').appendTo('body');
}
const $box = $(
'<div class="alert alert-' + className + ' alert-dismissible fade" role="alert">' +
'<button type="button" class="close" data-dismiss="alert">' +
'<span aria-hidden="true"><i class="fa fa-times-circle"></i></span>' +
'<span class="sr-only">Close</span>' +
'</button>' +
'<div class="media">' +
'<div class="media-left">' +
'<span class="fa-stack fa-lg">' +
'<i class="fa fa-circle fa-stack-2x"></i>' +
'<i class="fa fa-' + icon + ' fa-stack-1x"></i>' +
'</span>' +
'</div>' +
'<div class="media-body">' +
'<h4 class="alert-title"></h4>' +
'<p class="alert-message text-pre-wrap"></p>' +
'</div>' +
'</div>' +
'</div>'
);
$box.find('.alert-title').text(title);
$box.find('.alert-message').text(message);
$box.on('close.bs.alert', (e: Event) => {
e.preventDefault();
const $me = $(e.currentTarget);
$me
.clearQueue()
.queue((next: any): void => {
$me.removeClass('in');
next();
})
.slideUp(() => {
$me.remove();
});
});
$box.appendTo(this.messageContainer);
$box.delay(200)
.queue((next: any): void => {
$box.addClass('in');
next();
});
if (duration > 0) {
// if duration > 0 dismiss alert
$box.delay(duration * 1000)
.queue((next: any): void => {
$box.alert('close');
next();
});
}
}
}
let notificationObject;
try {
// fetch from parent
if (parent && parent.window.TYPO3 && parent.window.TYPO3.Notification) {
notificationObject = parent.window.TYPO3.Notification;
}
// fetch object from outer frame
if (top && top.TYPO3.Notification) {
notificationObject = top.TYPO3.Notification;
}
} catch (e) {
// This only happens if the opener, parent or top is some other url (eg a local file)
// which loaded the current window. Then the browser's cross domain policy jumps in
// and raises an exception.
// For this case we are safe and we can create our global object below.
}
if (!notificationObject) {
notificationObject = Notification;
// attach to global frame
if (typeof TYPO3 !== 'undefined') {
TYPO3.Notification = notificationObject;
}
}
export = notificationObject;