2016-04-18 09:46:06 +00:00
|
|
|
define([
|
|
|
|
'jquery',
|
|
|
|
'underscore',
|
|
|
|
'backbone',
|
|
|
|
'common',
|
|
|
|
'app/views/widgets/popover'
|
|
|
|
], function($, _, Backbone, Common, PopoverView) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var View = PopoverView.extend({
|
|
|
|
id: 'notice-popover',
|
2016-04-27 06:48:25 +00:00
|
|
|
className: 'sf-popover',
|
2016-04-18 09:46:06 +00:00
|
|
|
|
|
|
|
template: _.template($('#notice-popover-tmpl').html()),
|
|
|
|
|
|
|
|
initialize: function(options) {
|
|
|
|
PopoverView.prototype.initialize.call(this);
|
|
|
|
|
|
|
|
this.render();
|
2016-04-23 04:28:26 +00:00
|
|
|
|
2016-04-18 09:46:06 +00:00
|
|
|
this.$loadingTip = this.$('.loading-tip');
|
|
|
|
this.$error = this.$('.error');
|
|
|
|
this.$noticeList = this.$('.notice-list');
|
|
|
|
|
|
|
|
this.$notifications = $("#notifications");
|
2016-04-23 04:28:26 +00:00
|
|
|
this.$num = $('.num', this.$notifications);
|
2016-04-18 09:46:06 +00:00
|
|
|
this.orig_doc_title = document.title;
|
2016-04-23 04:28:26 +00:00
|
|
|
|
2016-04-18 09:46:06 +00:00
|
|
|
var _this = this;
|
2016-04-23 04:28:26 +00:00
|
|
|
|
2016-04-18 09:46:06 +00:00
|
|
|
var reqUnreadNum = function() {
|
|
|
|
$.ajax({
|
2016-08-19 04:00:05 +00:00
|
|
|
url: Common.getUrl({name: 'notifications'}),
|
2016-04-18 09:46:06 +00:00
|
|
|
dataType: 'json',
|
|
|
|
cache: false,
|
|
|
|
success: function(data) {
|
2016-08-19 04:00:05 +00:00
|
|
|
var count = data['unseen_count'],
|
2016-04-23 04:28:26 +00:00
|
|
|
$num = _this.$num;
|
|
|
|
$num.html(count);
|
2016-04-18 09:46:06 +00:00
|
|
|
if (count > 0) {
|
2016-04-23 04:28:26 +00:00
|
|
|
$num.removeClass('hide');
|
2016-04-18 09:46:06 +00:00
|
|
|
document.title = '(' + count + ')' + _this.orig_doc_title;
|
|
|
|
} else {
|
2016-04-23 04:28:26 +00:00
|
|
|
$num.addClass('hide');
|
2016-04-18 09:46:06 +00:00
|
|
|
document.title = _this.orig_doc_title;
|
|
|
|
}
|
2017-01-09 06:17:22 +00:00
|
|
|
|
|
|
|
var $networkNotice = $('#network-top-notice');
|
|
|
|
if ($networkNotice.is(':visible')) {
|
|
|
|
$networkNotice.remove();
|
|
|
|
}
|
2016-07-20 06:20:19 +00:00
|
|
|
},
|
2016-11-18 03:56:11 +00:00
|
|
|
error: function(xhr) { // e.g. 401 UNAUTHORIZED
|
|
|
|
var $el;
|
|
|
|
if (xhr.responseText) {
|
|
|
|
if (xhr.status == 401) {
|
2017-01-09 06:17:22 +00:00
|
|
|
clearInterval(reqInterval); // stop sending requests
|
2016-11-18 03:56:11 +00:00
|
|
|
$el = $('<p class="top-bar fixed-top-bar">' + gettext("You have logged out.") + '<span class="top-bar-click">' + gettext("Log in") + '</span></p>');
|
2017-01-09 06:17:22 +00:00
|
|
|
$('#wrapper').prepend($el);
|
2016-11-18 03:56:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-09 06:17:22 +00:00
|
|
|
if ($('#network-top-notice').length == 0) {
|
|
|
|
$el = $('<p class="top-bar fixed-top-bar" id="network-top-notice">' + gettext("Please check the network.") + '<span class="top-bar-click">' + gettext("Refresh") + '</span></p>');
|
|
|
|
$('#wrapper').prepend($el);
|
|
|
|
}
|
2016-11-18 03:56:11 +00:00
|
|
|
}
|
2018-02-05 08:47:56 +00:00
|
|
|
$('.top-bar-click', $el).on('click', function() {
|
2016-11-18 03:56:11 +00:00
|
|
|
location.reload(true);
|
|
|
|
});
|
2016-04-18 09:46:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
reqUnreadNum();
|
2017-07-15 03:05:58 +00:00
|
|
|
// request every `unread_notifications_request_interval` seconds
|
|
|
|
var reqInterval = setInterval(reqUnreadNum, (app.pageOptions.unread_notifications_request_interval || 3*60)*1000);
|
2016-04-18 09:46:06 +00:00
|
|
|
|
2018-02-05 08:47:56 +00:00
|
|
|
$('#notice-icon').on('click', function() {
|
2016-04-18 09:46:06 +00:00
|
|
|
_this.toggle();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
this.$el.html(this.template());
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2016-04-23 04:28:26 +00:00
|
|
|
events: {
|
|
|
|
'click .detail': 'viewDetail',
|
|
|
|
'click .unread a': 'visitUnread'
|
|
|
|
},
|
|
|
|
|
|
|
|
viewDetail: function(e) {
|
|
|
|
var $el = $(e.currentTarget);
|
|
|
|
location.href = $('.brief a', $el.parent()).attr('href');
|
|
|
|
},
|
|
|
|
|
|
|
|
visitUnread: function(e) {
|
|
|
|
var $el = $(e.currentTarget);
|
|
|
|
var notice_id = $el.closest('.unread').data('id');
|
|
|
|
var link_href = $el.attr('href');
|
|
|
|
$.ajax({
|
2017-02-07 08:03:46 +00:00
|
|
|
// set unread notice to be read
|
2016-08-19 07:17:46 +00:00
|
|
|
url: Common.getUrl({name: 'notification'}),
|
|
|
|
type: 'PUT',
|
2016-04-23 04:28:26 +00:00
|
|
|
dataType: 'json',
|
2017-02-07 08:03:46 +00:00
|
|
|
data:{'notice_id': notice_id},
|
2016-04-23 04:28:26 +00:00
|
|
|
beforeSend: Common.prepareCSRFToken,
|
|
|
|
success: function(data) {
|
|
|
|
location.href = link_href;
|
2017-02-07 08:03:46 +00:00
|
|
|
$el.closest('.unread').removeClass('unread').addClass('read');
|
2016-04-23 04:28:26 +00:00
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
location.href = link_href;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2016-04-18 09:46:06 +00:00
|
|
|
// override hide function
|
|
|
|
hide: function() {
|
|
|
|
var _this = this;
|
|
|
|
app.ui.currentPopover = null;
|
|
|
|
this.$el.detach();
|
|
|
|
|
|
|
|
if (this.$(".unread").length > 0) {
|
|
|
|
// set all unread notice to be read
|
|
|
|
$.ajax({
|
2016-08-19 07:00:10 +00:00
|
|
|
url: Common.getUrl({name: 'notifications'}),
|
|
|
|
type: 'PUT',
|
2016-04-18 09:46:06 +00:00
|
|
|
dataType: 'json',
|
|
|
|
beforeSend: Common.prepareCSRFToken,
|
|
|
|
success: function() {
|
2016-04-23 04:28:26 +00:00
|
|
|
_this.$num.html(0).addClass('hide');
|
2016-04-18 09:46:06 +00:00
|
|
|
document.title = _this.orig_doc_title;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
showContent: function() {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
this.$noticeList.addClass('hide');
|
|
|
|
this.$error.hide();
|
|
|
|
this.$loadingTip.show();
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: Common.getUrl({name: 'get_popup_notices'}),
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(data) {
|
|
|
|
_this.$loadingTip.hide();
|
|
|
|
_this.$noticeList.html(data['notice_html']).show();
|
|
|
|
},
|
|
|
|
error: function (xhr, textStatus, errorThrown) {
|
|
|
|
_this.$loadingTip.hide();
|
2018-07-31 10:15:44 +00:00
|
|
|
var error_msg = Common.prepareAjaxErrorMsg(xhr);
|
|
|
|
_this.$error.html(error_msg).show();
|
2016-04-18 09:46:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$notifications.append(this.$el);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return View;
|
|
|
|
});
|