-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathNotificationUI.js
More file actions
402 lines (371 loc) · 16 KB
/
NotificationUI.js
File metadata and controls
402 lines (371 loc) · 16 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global FloatingUIDOM*/
// @INCLUDE_IN_API_DOCS
/**
* The global NotificationUI can be used to create popup notifications over dom elements or generics app notifications.
*
* A global `window.EventManager` object is made available in phoenix that can be called anytime after AppStart.
* This global can be triggered from anywhere without using require context.
*
* ## Usage
* ### Simple example
* For Eg. Let's say we have to create a popup notification over the HTML element with ID `showInfileTree`.
* We can do this with the following
* @example
* ```js
* const NotificationUI = brackets.getModule("widgets/NotificationUI");
* // or use window.NotificationUI global object has the same effect.
* let notification = NotificationUI.createFromTemplate("Click me to locate the file in file tree", "showInfileTree",{});
* notification.done(()=>{
* console.log("notification is closed in ui.");
* })
* ```
* ### Advanced example
* Another advanced example where you can specify html and interactive components in the notification
* @example
* ```js
* // note that you can even provide an HTML Element node with
* // custom event handlers directly here instead of HTML text.
* let notification1 = NotificationUI.createFromTemplate(
* "<div>Click me to locate the file in file tree</div>", "showInfileTree",{
* allowedPlacements: ['top', 'bottom'],
* dismissOnClick: false,
* autoCloseTimeS: 300 // auto close the popup after 5 minutes
* });
* // do stuff
* notification1.done((closeReason)=>{
* console.log("notification is closed in ui reason:", closeReason);
* })
* ```
* The `createFromTemplate` API can be configured with numerous options. See API options below.
* @module widgets/NotificationUI
*/
define(function (require, exports, module) {
const WorkspaceManager = require("view/WorkspaceManager"),
Mustache = require("thirdparty/mustache/mustache"),
ToastPopupHtml = require("text!widgets/html/toast-popup.html"),
Dialogs = require("widgets/Dialogs"),
MainViewManager = require("view/MainViewManager");
const NOTIFICATION_TYPE_ARROW = "arrow",
NOTIFICATION_TYPE_TOAST = "toast";
/**
* CSS class names for notification styles.
* @enum {string}
* @const
*/
const NOTIFICATION_STYLES_CSS_CLASS = {
INFO: "style-info",
WARNING: "style-warning",
SUCCESS: "style-success",
ERROR: "style-error",
DANGER: "style-danger"
};
/**
* Closing notification reason.
* @enum {string}
* @const
*/
const CLOSE_REASON ={
TIMEOUT: 'closeTimeout',
CLICK_DISMISS: 'clickDismiss',
CLOSE_BTN_CLICK: 'closeBtnClick'
};
/**
* This section outlines the properties and methods available in this module
* @name API
*/
/**
* This is an instance of the notification returned by the `createFromTemplate` call. The object can be used to
* control the created notification. See Notification docs below.
* @type {Object}
*/
/**
* @constructor
* @private
*/
function Notification($notification, type) {
this.$notification = $notification;
this.type = type;
this._result = new $.Deferred();
this._promise = this._result.promise();
}
function _closeToastNotification($NotificationPopup, endCB) {
// Animate out
function cleanup() {
$NotificationPopup.removeClass("animateClose");
$NotificationPopup.remove();
endCB && endCB();
}
$NotificationPopup.removeClass("animateOpen");
$NotificationPopup
.addClass("animateClose")
.one("transitionend", cleanup)
.one("transitioncancel", cleanup);
}
function _closeArrowNotification($NotificationPopup, endCB) {
function cleanup() {
// wait for the animation to complete before removal
$NotificationPopup.remove();
WorkspaceManager.off(WorkspaceManager.EVENT_WORKSPACE_UPDATE_LAYOUT, $NotificationPopup[0].update);
endCB && endCB();
}
$NotificationPopup.removeClass('notification-ui-visible')
.addClass('notification-ui-hidden')
.one("transitionend", cleanup)
.one("transitioncancel", cleanup);
}
/**
* Closes the Notification if is visible and destroys then dom nodes
* @param {string} closeType - an optional reason as to why the notification is closed.
* @type {function}
* @name Notification.close
*/
Notification.prototype.close = function (closeType) {
let self = this,
$notification = this.$notification;
if(!$notification){
return this; // if already closed
}
this.$notification = null;
function doneCB() {
self._result.resolve(closeType);
}
this.type === NOTIFICATION_TYPE_TOAST ?
_closeToastNotification($notification, doneCB) :
_closeArrowNotification($notification, doneCB);
return this;
};
/**
* Adds a done callback to the Notification promise. The promise will be resolved
* when the Notification is dismissed. Never rejected.
* Print the close reason on console when the notification closes
* notificationInstance.done((closeReason)=>{
* console.log(closeReason)
* })
* @type {function}
* @name Notification.done
*/
Notification.prototype.done = function (callback) {
this._promise.done(callback);
};
/**
* Creates a new notification popup from given template.
* The template can either be a string or a jQuery object representing a DOM node that is *not* in the current DOM.
*
* Creating a notification popup
* // note that you can even provide an HTML Element node with
* // custom event handlers directly here instead of HTML text.
* let notification1 = NotificationUI.createFromTemplate(
* ```js
* "<div>Click me to locate the file in file tree</div>", "showInfileTree",{
* allowedPlacements: ['top', 'bottom'],
* dismissOnClick: false,
* autoCloseTimeS: 300 // auto close the popup after 5 minutes
* });
* ```
*
* @param {string} title The title for the notification.
* @param {string|Element} template A string template or HTML Element to use as the dialog HTML.
* @param {String} [elementID] optional id string if provided will show the notification pointing to the element.
* If no element is specified, it will be managed as a generic notification.
* @param {Object} [options] optional, supported
* * options are:
* * `allowedPlacements` - Optional String array with values restricting where the notification will be shown.
* Values can be a mix of `['top', 'bottom', 'left', 'right']`
* * `autoCloseTimeS` - Time in seconds after which the notification should be auto closed. Default is never.
* * `dismissOnClick` - when clicked, the notification is closed. Default is true(dismiss).
* * `toastStyle` - To style the toast notification for error, warning, info etc. Can be
* one of `NotificationUI.NOTIFICATION_STYLES_CSS_CLASS.*` or your own css class name.
* @return {Notification} Object with a done handler that resolves when the notification closes.
* @type {function}
*/
function createFromTemplate(title, template, elementID, options= {}) {
// https://floating-ui.com/docs/tutorial
options.allowedPlacements = options.allowedPlacements || ['top', 'bottom', 'left', 'right'];
options.dismissOnClick = options.dismissOnClick === undefined ? true : options.dismissOnClick;
if(!elementID){
elementID = 'notificationUIDefaultAnchor';
}
const $tooltip = _createDomElementWithArrowElement(title, template, elementID, options);
$tooltip.addClass('notification-ui-visible');
let notification = (new Notification($tooltip, NOTIFICATION_TYPE_ARROW));
if(options.autoCloseTimeS){
setTimeout(()=>{
notification.close(CLOSE_REASON.TIMEOUT);
}, options.autoCloseTimeS * 1000);
}
if(options.dismissOnClick){
$tooltip.click(()=>{
notification.close(CLOSE_REASON.CLICK_DISMISS);
});
}
$tooltip.find(".notification-popup-close-button").click(()=>{
notification.close(CLOSE_REASON.CLICK_DISMISS);
});
return notification;
}
let notificationWidgetCount = 0;
function _computePlacementWithArrowElement(tooltip, arrowElement, {x, y, placement, middlewareData}) {
Object.assign(tooltip.style, {
left: `${x}px`,
top: `${y}px`
});
if(arrowElement){
const {x: arrowX, y: arrowY} = middlewareData.arrow;
const staticSide = {
top: 'bottom',
right: 'left',
bottom: 'top',
left: 'right'
}[placement.split('-')[0]];
Object.assign(arrowElement.style, {
left: arrowX != null ? `${arrowX}px` : '',
top: arrowY != null ? `${arrowY}px` : '',
right: '',
bottom: '',
[staticSide]: '-4px'
});
}
}
function _updatePositions(tooltip, onElement, arrowElement, options) {
let middleWare= [
FloatingUIDOM.offset(6),
FloatingUIDOM.autoPlacement({
// 'right' and 'left' won't be chosen
allowedPlacements: options.allowedPlacements
}),
FloatingUIDOM.shift({padding: 5})
];
if(arrowElement){
middleWare.push(FloatingUIDOM.arrow({element: arrowElement}));
}
tooltip.update = function () {
FloatingUIDOM.computePosition(onElement, tooltip, {
placement: 'top',
middleware: middleWare
}).then(({x, y, placement, middlewareData}) => {
_computePlacementWithArrowElement(tooltip, arrowElement,
{x, y, placement, middlewareData});
});
};
tooltip.update();
WorkspaceManager.on(WorkspaceManager.EVENT_WORKSPACE_UPDATE_LAYOUT, tooltip.update);
}
function _createDomElementWithArrowElement(title, domTemplate, elementID, options) {
notificationWidgetCount++;
const onElement = document.getElementById(elementID);
let arrowElement;
let widgetID = `notification-ui-widget-${notificationWidgetCount}`;
let arrowID = `notification-ui-arrow-${notificationWidgetCount}`;
let textTemplate = null;
if (typeof domTemplate === 'string' || domTemplate instanceof String){
textTemplate = domTemplate;
}
const styleClass = NOTIFICATION_STYLES_CSS_CLASS[options.toastStyle]
|| options.toastStyle;
let $floatingDom = $(`<div id="${widgetID}" class="notification-ui-tooltip ${styleClass}" role="tooltip">
<div>
<p class='notification-popup-close-button arrow'>×</p>
</div>
<div >
<p class="notification-dialog-title">${title}</p>
</div>
<div>
<p class="notification-dialog-content">${textTemplate||''}</p>
</div></div>`);
if(!textTemplate && domTemplate){
$floatingDom.find(".notification-dialog-content").append($(domTemplate));
}
if(onElement){
arrowElement = $(`<div id="${arrowID}" class="notification-ui-arrow ${
NOTIFICATION_STYLES_CSS_CLASS[options.toastStyle] || ''}"></div>`);
$floatingDom.append(arrowElement);
}
$("body").append($floatingDom);
_updatePositions($floatingDom[0], onElement, arrowElement[0], options);
return $floatingDom;
}
/**
* Creates a new toast notification popup from given title and html message.
* The message can either be a string or a jQuery object representing a DOM node that is *not* in the current DOM.
*
* Creating a toast notification popup
*
* ```js
* // note that you can even provide an HTML Element node with
* // custom event handlers directly here instead of HTML text.
* let notification1 = NotificationUI.createToastFromTemplate( "Title here",
* "<div>Click me to locate the file in file tree</div>", {
* dismissOnClick: false,
* autoCloseTimeS: 300 // auto close the popup after 5 minutes
* });
* ```
* @param {string} title The title for the notification.
* @param {string|Element} template A string template or HTML Element to use as the dialog HTML.
* @param {{dismissOnClick, autoCloseTimeS, toastStyle}} [options] optional, supported
* * options are:
* * `autoCloseTimeS` - Time in seconds after which the notification should be auto closed. Default is never.
* * `dismissOnClick` - when clicked, the notification is closed. Default is true(dismiss).
* * `toastStyle` - To style the toast notification for error, warning, info etc. Can be
* one of `NotificationUI.NOTIFICATION_STYLES_CSS_CLASS.*` or your own css class name.
* @return {Notification} Object with a done handler that resolves when the notification closes.
* @type {function}
*/
function createToastFromTemplate(title, template, options = {}) {
options.dismissOnClick = options.dismissOnClick === undefined ? true : options.dismissOnClick;
notificationWidgetCount++;
const styleClass = NOTIFICATION_STYLES_CSS_CLASS[options.toastStyle]
|| options.toastStyle || NOTIFICATION_STYLES_CSS_CLASS.INFO;
const widgetID = `notification-toast-${notificationWidgetCount}`,
$NotificationPopup = $(Mustache.render(ToastPopupHtml, {id: widgetID, title: title,
containerStyle: styleClass}));
$NotificationPopup.find(".notification-dialog-content")
.append($(template));
Dialogs.addLinkTooltips($NotificationPopup);
let notification = (new Notification($NotificationPopup, NOTIFICATION_TYPE_TOAST));
$NotificationPopup.appendTo("#toast-notification-container").hide()
.find(".notification-popup-close-button").click(function () {
notification.close(CLOSE_REASON.CLOSE_BTN_CLICK);
MainViewManager.focusActivePane();
});
$NotificationPopup.show();
// Animate in
// Must wait a cycle for the "display: none" to drop out before CSS transitions will work
setTimeout(function () {
$NotificationPopup.addClass("animateOpen");
}, 0);
if(options.autoCloseTimeS){
setTimeout(()=>{
notification.close(CLOSE_REASON.TIMEOUT);
}, options.autoCloseTimeS * 1000);
}
if(options.dismissOnClick){
$NotificationPopup.click(()=>{
notification.close(CLOSE_REASON.CLICK_DISMISS);
});
}
return notification;
}
exports.createFromTemplate = createFromTemplate;
exports.createToastFromTemplate = createToastFromTemplate;
exports.CLOSE_REASON = CLOSE_REASON;
exports.NOTIFICATION_STYLES_CSS_CLASS = NOTIFICATION_STYLES_CSS_CLASS;
});