/
notifications-badge.js
112 lines (98 loc) · 3.68 KB
/
notifications-badge.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
(function() {
var NotificationDropdown = function() {
var self = this;
var currentPage = 2;
var notificationsLoaded = 10;
var isLoading = false;
this.subscribe("widget/ready",function(evt, badge, dropdown) {
$.extend(self, {
badge: badge,
badgeLink: badge.find("a"),
documentBody: $(document.body),
dropdown: dropdown,
dropdownNotifications: dropdown.find(".notifications"),
ajaxLoader: dropdown.find(".ajax_loader")
});
if( ! $.browser.msie ) {
self.badge.on('click', self.badgeLink, function(evt){
evt.preventDefault();
evt.stopPropagation();
if (self.dropdownShowing()){
self.hideDropdown();
} else {
self.showDropdown();
}
});
}
self.documentBody.click(function(evt) {
var inDropdown = $(evt.target).parents().is(self.dropdown);
var inHovercard = $.contains(app.hovercard.el, evt.target);
if(!inDropdown && !inHovercard && self.dropdownShowing()) {
self.badgeLink.click();
}
});
});
this.dropdownShowing = function() {
return this.dropdown.css("display") === "block";
};
this.showDropdown = function() {
self.ajaxLoader.show();
self.badge.addClass("active");
self.dropdown.css("display", "block");
$('.notifications').addClass("loading");
self.getNotifications();
};
this.hideDropdown = function() {
self.badge.removeClass("active");
self.dropdown.css("display", "none");
$('.notifications').perfectScrollbar('destroy');
};
this.getMoreNotifications = function() {
$.getJSON("/notifications?per_page=5&page="+currentPage, function(notifications) {
for(var i = 0; i < notifications.length; ++i)
self.notifications.push(notifications[i]);
notificationsLoaded += 5;
self.renderNotifications();
});
};
this.getNotifications = function() {
$.getJSON("/notifications?per_page="+notificationsLoaded, function(notifications) {
self.notifications = notifications;
self.renderNotifications();
});
};
this.renderNotifications = function() {
self.dropdownNotifications.empty();
$.each(self.notifications, function(index, notifications) {
$.each(notifications, function(index, notification) {
if($.inArray(notification, notifications) === -1)
self.dropdownNotifications.append(notification.note_html);
});
});
self.dropdownNotifications.find("time.timeago").timeago();
self.dropdownNotifications.find('.unread').each(function(index) {
Diaspora.page.header.notifications.setUpUnread( $(this) );
});
self.dropdownNotifications.find('.read').each(function(index) {
Diaspora.page.header.notifications.setUpRead( $(this) );
});
$('.notifications').perfectScrollbar('destroy');
$('.notifications').perfectScrollbar();
self.ajaxLoader.hide();
isLoading = false;
$('.notifications').removeClass("loading");
//Infinite Scrolling
$('.notifications').scroll(function(e) {
var bottom = $('.notifications').prop('scrollHeight') - $('.notifications').height();
var currentPosition = $('.notifications').scrollTop();
isLoading = ($('.loading').length == 1);
if (currentPosition + 50 >= bottom && notificationsLoaded <= self.notifications.length && !isLoading) {
$('.notifications').addClass("loading");
++currentPage;
self.getMoreNotifications();
}
});
};
};
Diaspora.Widgets.NotificationsDropdown = NotificationDropdown;
})();