diaspora/app/assets/javascripts/app/views/notifications_view.js
2015-02-08 23:41:32 +01:00

95 lines
2.8 KiB
JavaScript

// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
app.views.Notifications = Backbone.View.extend({
events: {
"click .unread-toggle" : "toggleUnread"
},
initialize: function() {
Diaspora.page.header.notifications.setUpNotificationPage(this);
$(".unread-toggle .entypo").tooltip();
},
toggleUnread: function(evt) {
var note = $(evt.target).closest(".stream_element");
var unread = note.hasClass("unread");
if (unread) {
this.setRead(note.data("guid"));
}
else {
this.setUnread(note.data("guid"));
}
},
setRead: function(guid) {
$.ajax({
url: "/notifications/" + guid,
data: { set_unread: false },
type: "PUT",
context: this,
success: this.clickSuccess
});
},
setUnread: function(guid) {
$.ajax({
url: "/notifications/" + guid,
data: { set_unread: true },
type: "PUT",
context: this,
success: this.clickSuccess
});
},
clickSuccess: function(data) {
var type = $('.stream_element[data-guid=' + data["guid"] + ']').data('type');
this.updateView(data["guid"], type, data["unread"]);
},
updateView: function(guid, type, unread) {
var change = unread ? 1 : -1,
all_notes = $('ul.nav > li:eq(0) .badge'),
type_notes = $('ul.nav > li[data-type=' + type + '] .badge'),
header_badge = $('#notification_badge .badge_count'),
note = $('.stream_element[data-guid=' + guid + ']');
if(unread) {
note.removeClass("read").addClass("unread");
$(".unread-toggle .entypo", note)
.tooltip('destroy')
.removeAttr("data-original-title")
.attr('title',Diaspora.I18n.t('notifications.mark_read'))
.tooltip();
}
else {
note.removeClass("unread").addClass("read");
$(".unread-toggle .entypo", note)
.tooltip('destroy')
.removeAttr("data-original-title")
.attr('title',Diaspora.I18n.t('notifications.mark_unread'))
.tooltip();
}
all_notes.text( function(i,text) { return parseInt(text) + change });
type_notes.text( function(i,text) { return parseInt(text) + change });
header_badge.text( function(i,text) { return parseInt(text) + change });
if(all_notes.text()>0){
all_notes.addClass('badge-important').removeClass('badge-default');
} else {
all_notes.removeClass('badge-important').addClass('badge-default');
}
if(type_notes.text()>0){
type_notes.addClass('badge-important').removeClass('badge-default');
} else {
type_notes.removeClass('badge-important').addClass('badge-default');
}
if(header_badge.text()>0){
header_badge.removeClass('hidden');
} else {
header_badge.addClass('hidden');
}
}
});
// @license-end