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
|
import Favico from 'favico.js';
import Cache from './cache.js';
import PageLayout from './page_layout.js';
import { $ngettext } from './gettext';
let stack = {};
let audio_notification = false;
let directlydeleted = [];
let favicon = null;
function updateFavicon(text) {
if (favicon === null) {
const valid = $('head')
.find('link[rel=icon]')
.first();
$('head')
.find('link[rel*=icon]')
.not(valid)
.remove();
favicon = new Favico({
bgColor: '#d60000',
textColor: '#fff',
fontStyle: 'normal',
fontFamily: 'Lato',
position: 'right',
type: 'rectangle'
});
}
favicon.badge(text);
}
// Wrapper function that creates a desktop notification from given data
function create_desktop_notification(data) {
var notification = new Notification(STUDIP.STUDIP_SHORT_NAME, {
body: data.text,
icon: data.avatar,
tag: data.id
});
notification.addEventListener('click', () => {
location.href = STUDIP.URLHelper.getURL(`dispatch.php/jsupdater/mark_notification_read/${notification.tag}`);
});
}
// Handler for all notifications received by an ajax request
function process_notifications({ notifications }) {
var cache = Cache.getInstance('desktop.notifications');
var ul = $('<ul/>');
var changed = false;
var new_stack = {};
notifications.forEach(notification => {
if (directlydeleted.indexOf(notification.personal_notification_id) !== -1) {
return;
}
ul.append(notification.html);
var id = $('.notification:last', ul).data().id;
new_stack[id] = notification;
if (notification.html_id) {
$(`#${notification.html_id}`).on('mouseenter', PersonalNotifications.isVisited);
}
changed = changed || stack[id] === undefined;
// Check if notifications should be sent (depends on the
// Notification itself and session storage)
if (
window.Notification === undefined
|| Notification.permission !== 'granted'
|| cache.has(notification.id)
) {
return;
}
// If it's okay let's create a notification
create_desktop_notification(notification);
cache.set(id, true);
});
// Anything changed? Replace stack and display
if (changed || Object.keys(stack).length !== Object.keys(new_stack).length) {
stack = new_stack;
$('#notification_list > ul').replaceWith(ul);
}
PersonalNotifications.update();
directlydeleted = [];
}
const PersonalNotifications = {
initialize () {
if ($('#notification_marker .count').length > 0) {
$('#notification_list .notification').map(function() {
var data = $(this).data();
stack[data.id] = data;
});
STUDIP.JSUpdater.register(
'personalnotifications',
process_notifications,
null,
60000
);
if ($('#audio_notification').length > 0) {
audio_notification = $('#audio_notification').get(0);
audio_notification.load();
}
if ('Notification' in window) {
$('#notification_list .enable-desktop-notifications')
.toggle(Notification.permission === 'default')
.click(STUDIP.PersonalNotifications.activate);
}
}
// Special handling for personal notifications:
$('#notification-container').on('mouseover mouseout', function () {
$(this).attr('aria-expanded', $(this).attr('aria-expanded') === 'true' ? 'false' : 'true');
});
},
activate () {
Promise.resolve(Notification.requestPermission()).then(permission => {
$('#notification_list .enable-desktop-notifications')
.toggle(permission === 'default');
});
},
markAsRead () {
const notification = $(this).closest('.notification');
const id = notification.data().id;
PersonalNotifications.sendReadInfo(id, notification);
return false;
},
markAllAsRead () {
const notifications = $(this)
.parent()
.find('.notification');
PersonalNotifications.sendReadInfo('all', notifications);
return false;
},
sendReadInfo (id, notification) {
$.get(STUDIP.URLHelper.getURL(`dispatch.php/jsupdater/mark_notification_read/${id}`)).done(() => {
if (notification) {
var count = notification.length;
notification.toggle('blind', 'fast', function() {
var data = $(this).data();
delete stack[data.id];
$(this).remove();
count -= 1;
if (count === 0) {
PersonalNotifications.update();
}
});
}
});
},
update () {
var count = _.values(stack).length;
var old_count = parseInt($('#notification_marker .count').text(), 10);
var really_new = 0;
$('#notification_list > ul > li').each(function() {
if (parseInt($(this).data('timestamp'), 10) > parseInt($('#notification_marker').data('lastvisit'), 10)) {
really_new += 1;
}
});
if (really_new > 0) {
$('#notification_marker')
.data('seen', false)
.addClass('alert');
PageLayout.title_prefix = '(!) ';
} else {
$('#notification_marker').removeClass('alert');
PageLayout.title_prefix = '';
}
if (count) {
$('#notification-container').addClass('hoverable');
$('#notification_marker').prop('disabled', false);
if (count > old_count && audio_notification !== false) {
audio_notification.play();
}
} else {
$('#notification-container').removeClass('hoverable');
$('#notification_marker').prop('disabled', true);
}
if (old_count !== count) {
$('#notification_marker .count').text(count);
let notification_text = $ngettext(
'%{ count } Benachrichtigung',
'%{ count } Benachrichtigungen',
count,
{ count }
);
$('#notification_marker').attr('title', notification_text);
updateFavicon(count);
$('#notification-container .mark-all-as-read').toggleClass('invisible', count < 2);
}
},
isVisited () {
const id = this.id;
$.each(stack, (index, notification) => {
if (notification.html_id === id) {
PersonalNotifications.sendReadInfo(notification.personal_notification_id);
delete stack[index];
$(`.notification[data-id=${notification.personal_notification_id}]`).fadeOut(function () {
$(this).remove();
});
directlydeleted.push(notification.personal_notification_id);
PersonalNotifications.update();
}
});
},
setSeen () {
if ($('#notification_marker').data('seen')) {
return;
}
$('#notification_marker').data('seen', true);
$.get(STUDIP.URLHelper.getURL('dispatch.php/jsupdater/notifications_seen')).then(time => {
$('#notification_marker')
.data('lastvisit', time)
.removeClass('alert');
});
}
};
export default PersonalNotifications;
|