-
Notifications
You must be signed in to change notification settings - Fork 27
/
notifications.js
74 lines (74 loc) · 2.11 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
(function() {
$.jGrowl.defaults.position='bottom-right';
$.jGrowl.defaults.life=6000;
var notifications = null,
NotificationHandler = {
// New ticket
'0': function(data) {
$.jGrowl(
'New <a href="' + data.ticket_url + '" target="_blank">Ticket #'
+ data.ticket_number + '</a> assigned to you'
);
},
// Ticket changed
'1': function(data) {
$.jGrowl(
'<a href="' + data.ticket_url + '" target="_blank">Ticket #'
+ data.ticket_number + '</a> has been changed'
);
},
// Ticket deadline reached
'2': function(data) {
$.jGrowl(
'The deadline passed for <a href="'
+ data.ticket_url + '" target="_blank">Ticket #'
+ data.ticket_number + '</a>'
);
},
// Tickets order changed
'3': function(data) {
$('#bug-list').yiiListView.update('bug-list');
},
// New comment on ticket
'4': function(data) {
$.jGrowl(
'New comment created on <a href="'
+ data.ticket_url + '" target="_blank">Ticket #'
+ data.ticket_number + '</a>.'
);
}
};
function initSocket() {
//var baseUrl = window.location.protocol + '//' + window.location.host + ':27000';
var baseUrl = 'https:' + '//' + window.location.host + ':' + (window.notificationsPort || '27000');
var userData=JSON.parse($('#user_data').text());
var user_id = parseInt(userData.user_id, 10);
var project_id = parseInt(userData.project_id, 10);
notifications = io.connect(baseUrl + '/notifications', {secure: true});
notifications.on('connect', function() {
if(!isNaN(user_id)) {
notifications.on('setUserDataResponse', function(data) {
// The user is added to collection if data.success === true
// console.log(data);
});
notifications.emit('setUserData', {
'user_id': user_id,
'project_id': project_id
});
}
});
notifications.on('notification', function(data) {
data.forEach(function(dataItem) {
if(dataItem.message_type
&& NotificationHandler[dataItem.message_type]) {
NotificationHandler[dataItem.message_type](dataItem);
}
});
});
}
$(window).load(function () {
if(typeof io != 'undefined') {
initSocket();
}
});
})();