1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-30 00:42:53 +00:00
seahub/static/scripts/app/views/notifications.js

166 lines
5.5 KiB
JavaScript
Raw Normal View History

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',
template: _.template($('#notice-popover-tmpl').html()),
initialize: function(options) {
PopoverView.prototype.initialize.call(this);
this.render();
2016-04-23 04:28:26 +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);
this.orig_doc_title = document.title;
2016-04-23 04:28:26 +00:00
var _this = this;
2016-04-23 04:28:26 +00:00
var reqUnreadNum = function() {
$.ajax({
url: Common.getUrl({name: 'get_unseen_notices_num'}),
dataType: 'json',
cache: false,
success: function(data) {
var count = data['count'],
2016-04-23 04:28:26 +00:00
$num = _this.$num;
$num.html(count);
if (count > 0) {
2016-04-23 04:28:26 +00:00
$num.removeClass('hide');
document.title = '(' + count + ')' + _this.orig_doc_title;
} else {
2016-04-23 04:28:26 +00:00
$num.addClass('hide');
document.title = _this.orig_doc_title;
}
2016-07-20 06:20:19 +00:00
},
2016-11-18 03:56:11 +00:00
error: function(xhr) { // e.g. 401 UNAUTHORIZED
2016-07-20 06:20:19 +00:00
clearInterval(reqInterval); // stop sending requests
2016-11-18 03:56:11 +00:00
var $el;
if (xhr.responseText) {
if (xhr.status == 401) {
$el = $('<p class="top-bar fixed-top-bar">' + gettext("You have logged out.") + '<span class="top-bar-click">' + gettext("Log in") + '</span></p>');
}
} else {
$el = $('<p class="top-bar fixed-top-bar">' + gettext("Please check the network.") + '</p>');
}
$('#wrapper').prepend($el);
$('.top-bar-click', $el).click(function() {
location.reload(true);
});
}
});
};
reqUnreadNum();
// request every 30s
2016-07-20 06:20:19 +00:00
var reqInterval = setInterval(reqUnreadNum, 30*1000);
$('#notice-icon').click(function() {
_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({
url: Common.getUrl({name: 'set_notice_seen_by_id'}) + '?notice_id=' + encodeURIComponent(notice_id),
type: 'POST',
dataType: 'json',
beforeSend: Common.prepareCSRFToken,
success: function(data) {
location.href = link_href;
},
error: function() {
location.href = link_href;
}
});
return false;
},
// 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({
url: Common.getUrl({name: 'set_notices_seen'}),
type: 'POST',
dataType: 'json',
beforeSend: Common.prepareCSRFToken,
success: function() {
2016-04-23 04:28:26 +00:00
_this.$num.html(0).addClass('hide');
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();
var err_msg;
if (xhr.responseText) {
err_msg = $.parseJSON(xhr.responseText).error;
} else {
err_msg = gettext('Please check the network.');
}
_this.$error.html(err_msg).show();
}
});
this.$notifications.append(this.$el);
}
});
return View;
});