diff --git a/media/js/base.js b/media/js/base.js index 18d91738f4..dfc524967c 100644 --- a/media/js/base.js +++ b/media/js/base.js @@ -16,85 +16,62 @@ if ($('.messages')[0]) { setTimeout(function() { $('.messages').addClass('hide'); }, 10000); } -$(function(){ +$(function() { var msg_ct = $("#msg-count"); - $.ajax({ - url: msg_ct.data('cturl'), - dataType: 'json', - cache: false, - success: function(data) { - if (data['count'] > 0) { - $('.num', msg_ct).html(data['count']).removeClass('hide'); - $('.num').data('count', data['count']); + // original title + var orig_doc_title = document.title; + var reqUnreadNum = function() { + $.ajax({ + url: msg_ct.data('url'), + dataType: 'json', + cache: false, + success: function(data) { + var count = data['count'], + num = $('.num', msg_ct); + num.html(count); + if (count > 0) { + num.removeClass('hide'); + document.title = '(' + count + ')' + orig_doc_title; + } else { + num.addClass('hide'); + document.title = orig_doc_title; + } } - } - }); + }); + }; + reqUnreadNum(); + // request every 30s + setInterval(reqUnreadNum, 30*1000); $('#notice-icon').click(function() { var popup = $('#notice-popup'); popup.toggleClass('hide'); - - //to prevent duplicately send ajax - if ($('#notice-icon').data('ajax') != 1){ + if (!popup.hasClass('hide')) { + $('.con', popup).css({'max-height':$(window).height() - $('#header').outerHeight() - $('#notice-popup .hd').outerHeight() - 3}); + var loading_tip = $('.loading-tip', popup); + loading_tip.show(); $.ajax({ - url: popup.data('pop_url'), - dataType: 'json', - cache: false, + url: popup.data('url'), + dataType: 'json', success: function(data) { - popup.find('.loading-tip').remove(); - popup.html(data['notifications_popup_html']); - $('.con', popup).css({'max-height':$(window).height() - $('#header').outerHeight() - $('#notice-popup .hd').outerHeight() - 3}); - $('#notice-icon').data('ajax', 1); + loading_tip.hide(); + var notice_list = $('#notice-list'); + notice_list.html(data['notice_html']).removeClass('hide'); - $('#notice-popup .close').click(function() { - if ($('.num').data('count') > 0){ - $.ajax({ - url: popup.data('seen_url'), - dataType: 'json', - cache: false, - success: function() { - popup.addClass('hide'); - $('.num').data('count', 0); - $('.num').addClass('hide'); - $('li').each(function() { - if ($(this).hasClass('unread')){ - $(this).removeClass('unread'); - $(this).addClass('read'); - } - }); - } - }); - }else{ - popup.addClass('hide'); - } - }); - - $("a[id$='-notice']").click(function() { - var notice_id = $(this).data('notice_id'); - var url = popup.data('seen_by_id_url'); - var href = $(this).data('href'); - - if ($(this).parent().parent().hasClass('unread')){ - $.ajax({ - url: url + '?notice_id=' + notice_id, - type: 'GET', - dataType: 'json', - cache: false, - success: function() { - location.href = href; - } - }); - }else{ - location.href = href; - } + // set a notice to be read when in it is clicked + $('.unread a', notice_list).click(function() { + var notice_id = $(this).parents('.unread').data('id'); + $.ajax({ + url: notice_list.data('url') + '?notice_id=' + e(notice_id), + dataType:'json' + }); }); } }); } }); - $(window).resize(function() { var popup = $('#notice-popup'); if (!popup.hasClass('hide')) { @@ -102,6 +79,22 @@ $(function(){ } }); + $('#notice-popup .close').click(function() { + $('#notice-popup').addClass('hide'); + if ($('#notice-list .unread').length > 0) { + // set all unread notice to be read + var url = $(this).data('url'); + $.ajax({ + url: url, + dataType: 'json', + success: function() { + $('.num', msg_ct).html(0).addClass('hide'); + document.title = orig_doc_title; + } + }); + } + }); + (function () { var my_info = $('#my-info'); var popup = $('#user-info-popup'); @@ -125,6 +118,7 @@ $(function(){ return false; }); })(); + }); $(document).click(function(e) { @@ -137,15 +131,6 @@ $(document).click(function(e) { closePopup($('#user-info-popup'), $('#my-info')); closePopup($('#top-nav-grp-info'), $('#top-nav-grp')); closePopup($('#notice-popup'), $('#notice-icon')); - - var closeNum = function(num, num_switch) { - if (num.hasClass('hide') && !num.is(target) && !num_switch.is(target) && !num_switch.find('*').is(target) ) { - if ($('.num').data('count') > 0){ - num.removeClass('hide'); - } - } - }; - closeNum($('.num'), $('#msg-count')); }); // search: disable submit when input nothing diff --git a/seahub/notifications/models.py b/seahub/notifications/models.py index a1e67688f9..eba0b401c8 100644 --- a/seahub/notifications/models.py +++ b/seahub/notifications/models.py @@ -486,12 +486,11 @@ class UserNotification(models.Model): folder_link = reverse('repo', args=[repo_id]) + '?p=' + urlquote(uploaded_to) folder_name = os.path.basename(uploaded_to) - msg = _(u"A file named %(file_name)s is uploaded to your folder %(folder)s") % { + msg = _(u"A file named %(file_name)s is uploaded to your folder %(folder)s") % { 'file_link': file_link, 'file_name': filename, 'folder_link': folder_link, 'folder': folder_name, - 'notice_id': self.id, } return msg @@ -510,11 +509,10 @@ class UserNotification(models.Model): self.delete() return None - msg = _(u"%(user)s has shared a library named %(repo_name)s to you.") % { + msg = _(u"%(user)s has shared a library named %(repo_name)s to you.") % { 'user': escape(share_from), 'href': reverse('repo', args=[repo.id]), 'repo_name': repo.name, - 'notice_id': self.id, } return msg @@ -529,11 +527,10 @@ class UserNotification(models.Model): file_name = d['file_name'] priv_share_token = d['priv_share_token'] - msg = _(u"%(user)s has shared a file named %(file_name)s to you.") % { + msg = _(u"%(user)s has shared a file named %(file_name)s to you.") % { 'user': escape(share_from), 'href': reverse('view_priv_shared_file', args=[priv_share_token]), 'file_name': file_name, - 'notice_id': self.id, } return msg def format_user_message_title(self): @@ -550,7 +547,7 @@ class UserNotification(models.Model): msg_from = d.get('msg_from') nickname = email2nickname(msg_from) - msg = _(u"You have received a new message from %(user)s.") % { + msg = _(u"You have received a new message from %(user)s.") % { 'user': escape(nickname), 'href': reverse('user_msg_list', args=[msg_from]), } @@ -593,11 +590,11 @@ class UserNotification(models.Model): msg_from = d.get('msg_from') if msg_from is None: - msg = _(u"%(group_name)s has new discussion.") % { + msg = _(u"%(group_name)s has a new discussion.") % { 'href': reverse('group_discuss', args=[group.id]), 'group_name': group.group_name} else: - msg = _(u"%(user)s posted a new discussion in %(group_name)s.") % { + msg = _(u"%(user)s posted a new discussion in %(group_name)s.") % { 'href': reverse('group_discuss', args=[group.id]), 'user': escape(email2nickname(msg_from)), 'group_name': group.group_name} @@ -634,11 +631,11 @@ class UserNotification(models.Model): reply_from = d.get('reply_from') if reply_from is None: - msg = _(u"One group discussion has new reply.") % { + msg = _(u"One group discussion has a new reply.") % { 'href': reverse('msg_reply_new'), } else: - msg = _(u"%(user)s replied your group discussion.") % { + msg = _(u"%(user)s replied your group discussion.") % { 'user': escape(email2nickname(reply_from)), 'href': reverse('msg_reply_new'), } @@ -677,13 +674,12 @@ class UserNotification(models.Model): self.delete() return None - msg = _(u"User %(username)s has asked to join group %(group_name)s, verification message: %(join_request_msg)s") % { + msg = _(u"User %(username)s has asked to join group %(group_name)s, verification message: %(join_request_msg)s") % { 'user_profile': reverse('user_profile', args=[username]), 'username': username, 'href': reverse('group_members', args=[group_id]), 'group_name': group.group_name, 'join_request_msg': escape(join_request_msg), - 'notice_id': self.id, } return msg diff --git a/seahub/notifications/templates/notifications/notice_email.html b/seahub/notifications/templates/notifications/notice_email.html index b9052ed022..282e42722e 100644 --- a/seahub/notifications/templates/notifications/notice_email.html +++ b/seahub/notifications/templates/notifications/notice_email.html @@ -5,7 +5,7 @@ {% block email_con %} {% autoescape off %} -

Hi, {{ name|email2nickname }}

+

{% blocktrans with name=to_user|email2nickname %}Hi {{ name }},{% endblocktrans %}

{% blocktrans count num=notice_count %} @@ -15,8 +15,8 @@ You've got {{num}} new notices on {{ site_name }}: {% endblocktrans %}

-
- +
+
@@ -24,38 +24,38 @@ You've got {{num}} new notices on {{ site_name }}: {% for notice in notices %} {% if notice.is_group_msg %} - - + + {% elif notice.is_grpmsg_reply %} - - + + {% elif notice.is_repo_share_msg %} - - + + {% elif notice.is_priv_file_share_msg %} - - + + {% elif notice.is_user_message %} - - + + {% elif notice.is_file_uploaded_msg %} - - + + {% elif notice.is_group_join_request %} - - + + {% endif %} diff --git a/seahub/notifications/templates/notifications/user_notification_list.html b/seahub/notifications/templates/notifications/user_notification_list.html index dd6605de67..6ed90d595b 100644 --- a/seahub/notifications/templates/notifications/user_notification_list.html +++ b/seahub/notifications/templates/notifications/user_notification_list.html @@ -33,12 +33,6 @@ {% block extra_script %} {% endblock %} diff --git a/seahub/templates/base.html b/seahub/templates/base.html index e497b7c558..b4ad7e666a 100644 --- a/seahub/templates/base.html +++ b/seahub/templates/base.html @@ -45,13 +45,19 @@ {% include 'snippets/search_form.html' %} {% endif %} -
+
- 0 + 0
-
- +
+

{% trans "Notifications" %}

+ +
diff --git a/seahub/templates/snippets/notice_html.html b/seahub/templates/snippets/notice_html.html new file mode 100644 index 0000000000..d9a78ba1ff --- /dev/null +++ b/seahub/templates/snippets/notice_html.html @@ -0,0 +1,47 @@ +{% load avatar_tags seahub_tags i18n %} + +{% for notice in notices %} + {% if notice.seen %} +
  • + {% else %} +
  • + {% endif %} + + {% if notice.msg_from %} + {% avatar notice.msg_from 32 %} + {% else %} + + {% endif %} + + {% if notice.is_group_msg %} +

    {{ notice.format_group_message_title|safe }}

    + {% if notice.format_group_message_detail %} +

    {{ notice.format_group_message_detail }}

    + {% endif %} + {% elif notice.is_grpmsg_reply %} +

    {{ notice.format_grpmsg_reply_title|safe }}

    + {% if notice.format_grpmsg_reply_detail %} +

    {{ notice.format_grpmsg_reply_detail }}

    + {% endif %} + {% elif notice.is_user_message %} +

    {{ notice.format_user_message_title|safe }}

    + {% if notice.format_user_message_detail %} +

    {{ notice.format_user_message_detail }}

    + {% endif %} + + {% elif notice.is_file_uploaded_msg %} +

    {{ notice.format_file_uploaded_msg|safe }}

    + + {% elif notice.is_repo_share_msg %} +

    {{ notice.format_repo_share_msg|safe }}

    + + {% elif notice.is_priv_file_share_msg %} +

    {{ notice.format_priv_file_share_msg|safe }}

    + + {% elif notice.is_group_join_request %} +

    {{ notice.format_group_join_request|safe }}

    + {% endif %} + +

    {{ notice.timestamp|translate_seahub_time }}

    +
  • +{% endfor %} diff --git a/seahub/templates/snippets/notifications_popup.html b/seahub/templates/snippets/notifications_popup.html deleted file mode 100644 index 6c1635b3a4..0000000000 --- a/seahub/templates/snippets/notifications_popup.html +++ /dev/null @@ -1,59 +0,0 @@ -{% load avatar_tags seahub_tags i18n %} -

    {% trans "Notifications" %}

    -
    -
      - {% for notice in notices %} - - {% if not notice.seen %} -
    • - {% else %} -
    • - {% endif %} - - {% if notice.msg_from %} - {% avatar notice.msg_from 32 %} - {% else %} - - {% endif %} - - {% if notice.is_group_msg %} -

      {{ notice.format_group_message_title|safe }} - {% if notice.format_group_message_detail %}
      - {{ notice.format_group_message_detail }} - {% endif %} -

      - - {% elif notice.is_grpmsg_reply %} -

      {{ notice.format_grpmsg_reply_title|safe }} - {% if notice.format_grpmsg_reply_detail %}
      - {{ notice.format_grpmsg_reply_detail}} - {% endif %} -

      - - {% elif notice.is_file_uploaded_msg %} -

      {{ notice.format_file_uploaded_msg|safe }}

      - - {% elif notice.is_repo_share_msg %} -

      {{ notice.format_repo_share_msg|safe }}

      - - {% elif notice.is_priv_file_share_msg %} -

      {{ notice.format_priv_file_share_msg|safe }}

      - - {% elif notice.is_user_message %} -

      {{ notice.format_user_message_title|safe }} - {% if notice.format_user_message_detail %}
      - {{ notice.format_user_message_detail }} - {% endif %} -

      - - {% elif notice.is_group_join_request %} -

      {{ notice.format_group_join_request|safe }}

      - - {% endif %} -

      {{ notice.timestamp|translate_seahub_time }}

      -
    • - {% endfor %} -
    -

    {% trans "See All Notifications" %}

    -
    - diff --git a/seahub/views/ajax.py b/seahub/views/ajax.py index 04ab604b65..90fe0f0147 100644 --- a/seahub/views/ajax.py +++ b/seahub/views/ajax.py @@ -1279,7 +1279,7 @@ def get_popup_notices(request): If unseen notices > 5, return all unseen notices. If unseen notices = 0, return last 5 notices. - Otherwise return all unseen notices, pluse some seen notices to make the + Otherwise return all unseen notices, plus some seen notices to make the sum equal to 5. Arguments: @@ -1296,18 +1296,18 @@ def get_popup_notices(request): seen_notices = [] list_num = 5 - count = UserNotification.objects.count_unseen_user_notifications(username) - if count == 0: + unseen_num = UserNotification.objects.count_unseen_user_notifications(username) + if unseen_num == 0: seen_notices = UserNotification.objects.get_user_notifications( username)[:list_num] - elif count > list_num: + elif unseen_num > list_num: unseen_notices = UserNotification.objects.get_user_notifications( username, seen=False) else: unseen_notices = UserNotification.objects.get_user_notifications( username, seen=False) seen_notices = UserNotification.objects.get_user_notifications( - username, seen=True)[:list_num - count] + username, seen=True)[:list_num - unseen_num] result_notices += unseen_notices result_notices += seen_notices @@ -1345,12 +1345,12 @@ def get_popup_notices(request): pass ctx_notices = {"notices": result_notices} - notifications_popup_html = render_to_string( - 'snippets/notifications_popup.html', ctx_notices, + notice_html = render_to_string( + 'snippets/notice_html.html', ctx_notices, context_instance=RequestContext(request)) return HttpResponse(json.dumps({ - "notifications_popup_html": notifications_popup_html, + "notice_html": notice_html, }), content_type=content_type) @login_required
    {% trans "Message"%} {% trans "Time"%}
    {{notice.group_msg_from}} posted a new discussion in {{notice.group_name}}
    {% blocktrans with user=notice.group_msg_from grp_url=notice.group_url grp_name=notice.group_name%}{{user}} posted a new discussion in {{grp_name}}{% endblocktrans %} {{ notice.timestamp }}
    {{notice.group_msg_reply_from}} replied yourgroup discussion
    {% blocktrans with user=notice.group_msg_reply_from reply_url=notice.group_msg_reply_url%}{{user}} replied yourgroup discussion{% endblocktrans %} {{ notice.timestamp }}
    {{notice.repo_share_from}} has shared a library named {{notice.repo_name}} to you.
    {% blocktrans with user=notice.repo_share_from lib_url=notice.repo_url lib_name=notice.repo_name%}{{user}} has shared a library named {{lib_name}} to you.{% endblocktrans %} {{ notice.timestamp }}
    {{notice.priv_shared_file_from}} has shared a file named {{notice.priv_shared_file_name}} to you.
    {% blocktrans with user=notice.priv_shared_file_from file_url=notice.priv_shared_file_url file_name=notice.priv_shared_file_name %}{{user}} has shared a file named {{file_name}} to you.{% endblocktrans %} {{ notice.timestamp }}
    You have received a new message from {{notice.user_msg_from}}
    {% blocktrans with msg_url=notice.user_msg_url user=notice.user_msg_from %}You have received a new message from {{user}}.{% endblocktrans %} {{ notice.timestamp }}
    A file named {{notice.uploaded_file_name}} is uploaded to your folder {{notice.uploaded_folder_name}}.
    {% blocktrans with file_url=notice.uploaded_file_link file_name=notice.uploaded_file_name folder_url=notice.uploaded_folder_link folder_name=notice.uploaded_folder_name %}A file named {{file_name}} is uploaded to your folder {{folder_name}}.{% endblocktrans %} {{ notice.timestamp }}
    User {{notice.grpjoin_username}} has asked to join group {{notice.grpjoin_group_name}}, verification message: {{notice.grpjoin_request_msg}}.
    {% blocktrans with user_url=notice.grpjoin_user_profile_url user_name=notice.grpjoin_username grp_url=notice.grpjoin_group_url grp_name=notice.grpjoin_group_name msg=notice.grpjoin_request_msg%}User {{user_name}} has asked to join group {{grp_name}}, verification message: {{msg}}.{% endblocktrans %} {{ notice.timestamp }}