Skip to content

Commit 1859668

Browse files
committed
Add proper handling of the close event handler in UINotification
1 parent 7015cfc commit 1859668

File tree

2 files changed

+78
-41
lines changed

2 files changed

+78
-41
lines changed

src/UI/UIDesktop.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async function UIDesktop(options){
117117
title: notification.title,
118118
text: notification.text,
119119
icon: icon,
120-
click: async () => {
120+
close: async () => {
121121
await fetch(`${window.api_origin}/notif/mark-ack`, {
122122
method: 'POST',
123123
headers: {
@@ -143,7 +143,7 @@ async function UIDesktop(options){
143143
icon,
144144
title: notification.title,
145145
text: notification.text ?? notification.title,
146-
click: async () => {
146+
close: async () => {
147147
await fetch(`${window.api_origin}/notif/mark-ack`, {
148148
method: 'POST',
149149
headers: {
@@ -1085,6 +1085,28 @@ async function UIDesktop(options){
10851085
}
10861086
})
10871087
}
1088+
1089+
// fetch notifications
1090+
fetch(puter.APIOrigin + "/drivers/call", {
1091+
"headers": {
1092+
"Content-Type": "application/json",
1093+
"Authorization": `Bearer ${puter.authToken}`,
1094+
},
1095+
"body": JSON.stringify({
1096+
interface: 'puter-notifications',
1097+
method: 'select',
1098+
args: {}
1099+
}),
1100+
"method": "POST",
1101+
})
1102+
.then(response => response.json())
1103+
.then(data => {
1104+
if(data && data.result && data.result.length > 0){
1105+
data.data?.forEach(notification => {
1106+
UINotification(notification);
1107+
})
1108+
}
1109+
})
10881110
}
10891111

10901112
$(document).on('contextmenu taphold', '.taskbar', function(event){
@@ -1303,7 +1325,7 @@ $(document).on('click', '.user-options-menu-btn', async function(e){
13031325
}
13041326
},
13051327
]
1306-
});
1328+
});
13071329
})
13081330

13091331
$(document).on('click', '.fullscreen-btn', async function (e) {

src/UI/UINotification.js

+53-38
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function UINotification(options){
4848
}
4949
})
5050

51+
// Notification Clicked
5152
$(el_notification).on('click', function(e){
5253
// close button clicked
5354
if($(e.target).hasClass('notification-close')){
@@ -58,8 +59,55 @@ function UINotification(options){
5859
if(options.click && typeof options.click === 'function'){
5960
options.click(options.value);
6061
}
62+
63+
// close notification
64+
close_notification(el_notification);
6165
})
6266

67+
// Close Button Clicked
68+
$(el_notification).find('.notification-close').on('click', function(e, data){
69+
let closingMultiple = false;
70+
if(data?.closingAll)
71+
closingMultiple = true;
72+
73+
close_notification(el_notification, closingMultiple);
74+
e.stopPropagation();
75+
e.preventDefault();
76+
return false;
77+
});
78+
79+
const close_notification = function(el_notification, closingMultiple = false){
80+
// hide notification wrapper by animating height and opacity
81+
// only if closing one notification and there are multiple notifications
82+
// otherwise the animation is not needed
83+
if(!closingMultiple && $('.notification').length > 1){
84+
$(el_notification).closest('.notification-wrapper').animate({
85+
height: 0,
86+
opacity: 0
87+
}, 300);
88+
}
89+
90+
// hide notification by fading out to the right
91+
$(el_notification).addClass('animate__fadeOutRight');
92+
93+
// close callback
94+
if(options.close && typeof options.close === 'function'){
95+
options.close(options.value);
96+
}
97+
98+
// remove notification and wrapper after animation
99+
setTimeout(function(){
100+
$(el_notification).closest('.notification-wrapper').remove();
101+
$(el_notification).remove();
102+
// count notifications
103+
let count = $('.notification-container').find('.notification-wrapper').length;
104+
if(count <= 1){
105+
$('.notification-container').removeClass('has-multiple');
106+
}else{
107+
$('.notification-container').addClass('has-multiple');
108+
}
109+
}, 500);
110+
}
63111
// Show Notification
64112
$(el_notification).delay(100).show(0);
65113

@@ -74,52 +122,19 @@ function UINotification(options){
74122
return el_notification;
75123
}
76124

77-
$(document).on('click', '.notification', function(e){
78-
if($(e.target).hasClass('notification')){
79-
window.close_notification(e.target);
80-
}else{
81-
window.close_notification($(e.target).closest('.notification'));
82-
}
83-
});
84-
85-
$(document).on('click', '.notification-close', function(e){
86-
window.close_notification($(e.target).closest('.notification'));
87-
e.stopPropagation();
88-
e.preventDefault();
89-
return false;
90-
});
91-
92125
$(document).on('click', '.notifications-close-all', function(e){
93-
$('.notification-container').find('.notification-wrapper').each(function(){
94-
window.close_notification(this);
95-
});
126+
// close all notifications
127+
$('.notification-container').find('.notification-close').trigger('click', {closingAll: true});
128+
// hide 'Close all' button
96129
$('.notifications-close-all').animate({
97130
opacity: 0
98131
}, 300);
132+
// remove the 'has-multiple' class
99133
$('.notification-container').removeClass('has-multiple');
134+
// prevent default
100135
e.stopPropagation();
101136
e.preventDefault();
102137
return false;
103138
})
104-
window.close_notification = function(el_notification){
105-
$(el_notification).closest('.notification-wrapper').animate({
106-
height: 0,
107-
opacity: 0
108-
}, 300);
109-
$(el_notification).addClass('animate__fadeOutRight');
110-
111-
// remove notification
112-
setTimeout(function(){
113-
$(el_notification).closest('.notification-wrapper').remove();
114-
$(el_notification).remove();
115-
// count notifications
116-
let count = $('.notification-container').find('.notification-wrapper').length;
117-
if(count <= 1){
118-
$('.notification-container').removeClass('has-multiple');
119-
}else{
120-
$('.notification-container').addClass('has-multiple');
121-
}
122-
}, 500);
123-
}
124139

125140
export default UINotification;

0 commit comments

Comments
 (0)