clean up notifications-badge.js

This commit is contained in:
Dan Hansen 2011-07-02 14:57:17 -07:00
parent b48b182c73
commit 3941d562a8

View file

@ -1,75 +1,64 @@
(function() { (function() {
var NotificationDropdown = function() { var NotificationDropdown = function() {
var self = this;
this.start = function() { this.start = function() {
this.badge = $("#notification_badge a"); this.badge = $("#notification_badge");
this.badgeLink = this.badge.find("a");
this.documentBody = $(document.body); this.documentBody = $(document.body);
this.dropdown = $("#notification_dropdown"); this.dropdown = $("#notification_dropdown");
this.dropdownNotifications = this.dropdown.find(".notifications");
this.badge.click($.proxy(function(evt) { this.badgeLink.toggle(function(evt) {
evt.preventDefault(); evt.preventDefault();
evt.stopPropagation(); evt.stopPropagation();
if(!this.dropdownShowing()) { self.getNotifications(function() {
this.getNotifications(function() { self.renderNotifications();
this.toggleDropdown(); self.dropdown.css("display", "block");
}); });
} }, function(evt) {
else { evt.preventDefault();
this.toggleDropdown(); evt.stopPropagation();
}
}, this));
this.documentBody.click($.proxy(function(evt) { self.dropdown.css("display", "none");
if(this.dropdownShowing()) { });
this.toggleDropdown(evt);
this.dropdown.click(function(evt) {
evt.stopPropagation();
});
this.documentBody.click(function(evt) {
if(self.dropdownShowing()) {
self.badgeLink.click();
} }
}, this)); });
}; };
};
NotificationDropdown.prototype.dropdownShowing = function() {
return this.dropdown.css("display") === "block";
}
NotificationDropdown.prototype.toggleDropdown = function() { this.dropdownShowing = function() {
if(!this.dropdownShowing()) { return this.dropdown.css("display") === "block";
this.renderNotifications(); };
this.showDropdown();
} else {
this.hideDropdown();
}
}
NotificationDropdown.prototype.showDropdown = function() { this.getNotifications = function(callback) {
this.badge.parent().addClass("active"); $.getJSON("/notifications", function(notifications) {
this.dropdown.css("display", "block"); self.notifications = notifications;
} callback.apply(self, []);
});
};
NotificationDropdown.prototype.hideDropdown = function() { this.renderNotifications = function() {
this.badge.parent().removeClass("active"); self.dropdownNotifications.empty();
this.dropdown.css("display", "none");
}
NotificationDropdown.prototype.getNotifications = function(callback) { $.each(self.notifications.notifications, function(index, notifications) {
$.getJSON("/notifications", $.proxy(function(notifications) { $.each(notifications, function(index, notification) {
this.notifications = notifications; $("<div/>", { "class": "notification_element" })
callback.apply(this, [notifications]);
}, this));
};
NotificationDropdown.prototype.renderNotifications = function() {
this.dropdown.find(".notifications").html("");
$.each(this.notifications.notifications, $.proxy(function(index, notifications) {
$.each(notifications, $.proxy(function(index, notification) {
$("<div/>", {
"class": "notification_element"
})
.html(notification.translation) .html(notification.translation)
.prepend($("<img/>", { src: notification.actors[0].avatar })) .prepend($("<img/>", { src: notification.actors[0].avatar }))
.prependTo(this.dropdown.find(".notifications")); .prependTo(self.dropdownNotifications);
}, this)); });
}, this)); });
};
}; };
Diaspora.widgets.add("notificationDropdown", NotificationDropdown); Diaspora.widgets.add("notificationDropdown", NotificationDropdown);