This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnotification.js
225 lines (211 loc) · 6.99 KB
/
notification.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module ui/notification/notification
*/
/* globals window */
import ContextPlugin from '@ckeditor/ckeditor5-core/src/contextplugin';
/**
* The Notification plugin.
*
* This plugin sends a few types of notifications: `success`, `info` and `warning`. The notifications need to be
* handled and displayed by a plugin responsible for showing the UI of the notifications. Using this plugin for dispatching
* notifications makes it possible to switch the notifications UI.
*
* Note that every unhandled and not stopped `warning` notification will be displayed as a system alert.
* See {@link module:ui/notification/notification~Notification#showWarning}.
*
* @extends module:core/contextplugin~ContextPlugin
*/
export default class Notification extends ContextPlugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'Notification';
}
/**
* @inheritDoc
*/
init() {
// Each unhandled and not stopped `show:warning` event is displayed as a system alert.
this.on( 'show:warning', ( evt, data ) => {
window.alert( data.message ); // eslint-disable-line no-alert
}, { priority: 'lowest' } );
}
/**
* Shows a success notification.
*
* By default, it fires the {@link #event:show:success `show:success` event} with the given `data`. The event namespace can be extended
* using the `data.namespace` option. For example:
*
* showSuccess( 'Image is uploaded.', {
* namespace: 'upload:image'
* } );
*
* will fire the `show:success:upload:image` event.
*
* You can provide the title of the notification:
*
* showSuccess( 'Image is uploaded.', {
* title: 'Image upload success'
* } );
*
* @param {String} message The content of the notification.
* @param {Object} [data={}] Additional data.
* @param {String} [data.namespace] Additional event namespace.
* @param {String} [data.title] The title of the notification.
*/
showSuccess( message, data = {} ) {
this._showNotification( {
message,
type: 'success',
namespace: data.namespace,
title: data.title
} );
}
/**
* Shows an information notification.
*
* By default, it fires the {@link #event:show:info `show:info` event} with the given `data`. The event namespace can be extended
* using the `data.namespace` option. For example:
*
* showInfo( 'Editor is offline.', {
* namespace: 'editor:status'
* } );
*
* will fire the `show:info:editor:status` event.
*
* You can provide the title of the notification:
*
* showInfo( 'Editor is offline.', {
* title: 'Network information'
* } );
*
* @param {String} message The content of the notification.
* @param {Object} [data={}] Additional data.
* @param {String} [data.namespace] Additional event namespace.
* @param {String} [data.title] The title of the notification.
*/
showInfo( message, data = {} ) {
this._showNotification( {
message,
type: 'info',
namespace: data.namespace,
title: data.title
} );
}
/**
* Shows a warning notification.
*
* By default, it fires the {@link #event:show:warning `show:warning` event}
* with the given `data`. The event namespace can be extended using the `data.namespace` option. For example:
*
* showWarning( 'Image upload error.', {
* namespace: 'upload:image'
* } );
*
* will fire the `show:warning:upload:image` event.
*
* You can provide the title of the notification:
*
* showWarning( 'Image upload error.', {
* title: 'Upload failed'
* } );
*
* Note that each unhandled and not stopped `warning` notification will be displayed as a system alert.
* The plugin responsible for displaying warnings should `stop()` the event to prevent displaying it as an alert:
*
* notifications.on( 'show:warning', ( evt, data ) => {
* // Do something with the data.
*
* // Stop this event to prevent displaying it as an alert.
* evt.stop();
* } );
*
* You can attach many listeners to the same event and `stop()` this event in a listener with a low priority:
*
* notifications.on( 'show:warning', ( evt, data ) => {
* // Show the warning in the UI, but do not stop it.
* } );
*
* notifications.on( 'show:warning', ( evt, data ) => {
* // Log the warning to some error tracker.
*
* // Stop this event to prevent displaying it as an alert.
* evt.stop();
* }, { priority: 'low' } );
*
* @param {String} message The content of the notification.
* @param {Object} [data={}] Additional data.
* @param {String} [data.namespace] Additional event namespace.
* @param {String} [data.title] The title of the notification.
*/
showWarning( message, data = {} ) {
this._showNotification( {
message,
type: 'warning',
namespace: data.namespace,
title: data.title
} );
}
/**
* Fires the `show` event with the specified type, namespace and message.
*
* @private
* @param {Object} data The message data.
* @param {String} data.message The content of the notification.
* @param {'success'|'info'|'warning'} data.type The type of the message.
* @param {String} [data.namespace] Additional event namespace.
* @param {String} [data.title=''] The title of the notification.
*/
_showNotification( data ) {
const event = `show:${ data.type }` + ( data.namespace ? `:${ data.namespace }` : '' );
this.fire( event, {
message: data.message,
type: data.type,
title: data.title || ''
} );
}
/**
* Fired when one of the `showSuccess()`, `showInfo()`, `showWarning()` methods is called.
*
* @event show
* @param {Object} data The notification data.
* @param {String} data.message The content of the notification.
* @param {String} data.title The title of the notification.
* @param {'success'|'info'|'warning'} data.type The type of the notification.
*/
/**
* Fired when the `showSuccess()` method is called.
*
* @event show:success
* @param {Object} data The notification data.
* @param {String} data.message The content of the notification.
* @param {String} data.title The title of the notification.
* @param {'success'} data.type The type of the notification.
*/
/**
* Fired when the `showInfo()` method is called.
*
* @event show:info
* @param {Object} data The notification data.
* @param {String} data.message The content of the notification.
* @param {String} data.title The title of the notification.
* @param {'info'} data.type The type of the notification.
*/
/**
* Fired when the `showWarning()` method is called.
*
* When this event is not handled or stopped by `event.stop()`, the `data.message` of this event will
* be automatically displayed as a system alert.
*
* @event show:warning
* @param {Object} data The notification data.
* @param {String} data.message The content of the notification.
* @param {String} data.title The title of the notification.
* @param {'warning'} data.type The type of the notification.
*/
}