-
Notifications
You must be signed in to change notification settings - Fork 987
/
notifications.js
186 lines (167 loc) · 5.84 KB
/
notifications.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
/**
* Notifcation system for admin
*
* @since 2017-08-02
* @author Olle Haerstedt
*/
// Namespace
var LS = LS || {};
$(document).ready(function() {
/**
* Log stuff
*/
function log(a, b) {
return; // Remove to enable logging
if (b == undefined) {
console.log(a);
}
else {
console.log(a, b);
}
}
/**
* Load widget HTML and inject it
* @param {string} URL to call
* @return
*/
function updateNotificationWidget(updateUrl) {
log('updateNotificationWidget');
// Update notification widget
return $.ajax({
url: updateUrl,
method: 'GET',
success: function (response) {
$('#notification-li').replaceWith(response);
// Re-bind onclick
initNotification();
// Adapt style to window size
styleNotificationMenu();
},
error: showError
});
}
// Called from outside (update notifications when click
LS.updateNotificationWidget = function(url) {
// Make sure menu is open after load
updateNotificationWidget(url).then(function() {
$('#notification-li').addClass('open');
});
// Only update once
LS.updateNotificationWidget = function() {};
}
/**
* Tell system that notification is read
* @param {object} that The notification link
* @return
*/
function notificationIsRead(that) {
log('notificationIsRead');
$.ajax({
url: $(that).data('read-url'),
method: 'GET',
success : function(response) {
// Fetch new HTML for menu widget
updateNotificationWidget($(that).data('update-url'));
},
error: showError
});
}
/**
* Fetch notification as JSON and show modal
* @param {object} that The notification link
* @param {url} URL to fetch notification as JSON
* @return
*/
function showNotificationModal(that, url) {
log('showNotificationModal');
$.ajax({
url: url,
method: 'GET',
dataType: 'json',
success : function(response) {
var not = response.result;
$('#admin-notification-modal .modal-title').html(not.title);
$('#admin-notification-modal .modal-body-text').html(not.message);
$('#admin-notification-modal .modal-content').removeClass('panel-danger').addClass('panel-' + not.display_class);
$('#admin-notification-modal .notification-date').html(not.created.substr(0, 16));
$('#admin-notification-modal').modal();
// TODO: Will this work in message includes a link that is clicked?
$('#admin-notification-modal').unbind('hidden.bs.modal');
$('#admin-notification-modal').on('hidden.bs.modal', function(e) {
notificationIsRead(that);
$('#admin-notification-modal .modal-content').removeClass('panel-' + not.display_class);
});
},
error: showError
});
}
/**
* Bind onclick and stuff
* @return
*/
function initNotification() {
log('initNotification');
$('.admin-notification-link').each(function(nr, that) {
log('nr', nr);
var url = $(that).data('url');
var importance = $(that).data('importance');
var status = $(that).data('status');
// Important notifications are shown as pop-up on load
if (importance == 3 && status == 'new') {
showNotificationModal(that, url);
log('stoploop');
return false; // Stop loop
}
// Bind click to notification in drop-down
$(that).unbind('click');
$(that).on('click', function() {
showNotificationModal(that, url);
});
});
}
/**
* Apply styling
* @return
*/
function styleNotificationMenu()
{
log('styleNotificationMenu');
var height = window.innerHeight - 70;
$('#notification-outer-ul').css('height', height + 'px');
$('#notification-inner-ul').css('height', (height - 60) + 'px');
$('#notification-inner-li').css('height', (height - 60) + 'px');
}
LS.styleNotificationMenu = styleNotificationMenu;
/**
* Called when user clicks "Delete all notifications"
* @param {string} url
* @return
*/
function deleteAllNotifications(url, updateUrl) {
return $.ajax({
url: url,
method: 'GET',
success: function (response) {
log('response', response);
},
error: showError
}).then(function() {
updateNotificationWidget(updateUrl);
});
}
LS.deleteAllNotifications = deleteAllNotifications;
initNotification();
function showError(response) {
var status= response.status || LS.lang.errorUnknow;
var responseText= response.responseText || LS.lang.unknowText;
$('#admin-notification-modal .modal-title').html(LS.lang.errorTitle.replace("%s",status));
$('#admin-notification-modal .modal-body-text').html(responseText);
$('#admin-notification-modal .modal-content').addClass('panel-danger');
//$('#admin-notification-modal .notification-date').html(not.created.substr(0, 16));
$('#admin-notification-modal').modal();
$('#admin-notification-modal').unbind('hidden.bs.modal');
$('#admin-notification-modal').on('hidden.bs.modal', function(e) {
$('#admin-notification-modal .modal-content').removeClass('panel-danger');
});
}
});