Mark the document title with a “(!)” when activity is detected, clear on focus.
[Sone.git] / src / main / resources / static / javascript / sone.js
index 6cb227c..8be0282 100644 (file)
@@ -69,6 +69,7 @@ function addCommentLink(postId, element) {
        }
        commentElement = (function(postId) {
                var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
+                       markPostAsKnown(getPostElement(this));
                        replyElement = $("#sone .post#" + postId + " .create-reply");
                        replyElement.removeClass("hidden");
                        replyElement.removeClass("light");
@@ -299,7 +300,7 @@ function getPostId(element) {
 }
 
 function getPostTime(element) {
-       return getPostElement(element).find(".storage-time").text();
+       return getPostElement(element).find(".post-time").text();
 }
 
 function getReplyElement(element) {
@@ -311,7 +312,7 @@ function getReplyId(element) {
 }
 
 function getReplyTime(element) {
-       return getReplyElement(element).find(".storage-time").text();
+       return getReplyElement(element).find(".reply-time").text();
 }
 
 function likePost(postId) {
@@ -454,6 +455,7 @@ function ajaxifyPost(postElement) {
                postReply(postId, text, function(success, error, replyId) {
                        if (success) {
                                loadNewReply(replyId);
+                               markPostAsKnown(getPostElement(inputField));
                                $("#sone .post#" + postId + " .create-reply").addClass("hidden");
                        } else {
                                alert(error);
@@ -473,10 +475,12 @@ function ajaxifyPost(postElement) {
        /* convert all “like” buttons to javascript functions. */
        $(postElement).find(".like-post").submit(function() {
                likePost(getPostId(this));
+               markPostAsKnown(getPostElement(this));
                return false;
        });
        $(postElement).find(".unlike-post").submit(function() {
                unlikePost(getPostId(this));
+               markPostAsKnown(getPostElement(this));
                return false;
        });
 
@@ -509,10 +513,12 @@ function ajaxifyPost(postElement) {
 function ajaxifyReply(replyElement) {
        $(replyElement).find(".like-reply").submit(function() {
                likeReply(getReplyId(this));
+               markPostAsKnown(getPostElement(this));
                return false;
        });
        $(replyElement).find(".unlike-reply").submit(function() {
                unlikeReply(getReplyId(this));
+               markPostAsKnown(getPostElement(this));
                return false;
        });
        (function(replyElement) {
@@ -563,6 +569,7 @@ function getStatus() {
                                        $("#sone #notification-area").append(notification);
                                        notification.slideDown();
                                }
+                               setActivity();
                        });
                        $.each(data.removedNotifications, function(index, value) {
                                $("#sone #notification-area .notification#" + value.id).slideUp();
@@ -612,6 +619,7 @@ function loadNewPost(postId) {
                        }
                        ajaxifyPost(newPost);
                        newPost.slideDown();
+                       setActivity();
                }
        });
 }
@@ -644,6 +652,7 @@ function loadNewReply(replyId) {
                                }
                                ajaxifyReply(newReply);
                                newReply.slideDown();
+                               setActivity();
                        });
                }
        });
@@ -652,21 +661,44 @@ function loadNewReply(replyId) {
 function markPostAsKnown(postElements) {
        $(postElements).each(function() {
                postElement = this;
-               $.getJSON("ajax/markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function() {
-                       $(postElement).removeClass("new");
-               });
+               if ($(postElement).hasClass("new")) {
+                       (function(postElement) {
+                               $.getJSON("ajax/markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) {
+                                       $(postElement).removeClass("new");
+                               });
+                       })(postElement);
+               }
        });
+       markReplyAsKnown($(postElements).find(".reply"));
 }
 
 function markReplyAsKnown(replyElements) {
        $(replyElements).each(function() {
                replyElement = this;
-               $.getJSON("ajax/markReplyAsKnown.ajax", {"formPassword": getFormPassword(), "reply": getReplyId(replyElement)}, function() {
-                       $(replyElement).removeClass("new");
-               });
+               if ($(replyElement).hasClass("new")) {
+                       (function(replyElement) {
+                               $.getJSON("ajax/markReplyAsKnown.ajax", {"formPassword": getFormPassword(), "reply": getReplyId(replyElement)}, function(data, textStatus) {
+                                       $(replyElement).removeClass("new");
+                               });
+                       })(replyElement);
+               }
        });
 }
 
+function resetActivity() {
+       title = document.title;
+       if (title.indexOf('(') == 0) {
+               document.title = title.substr(title.indexOf(' ') + 1);
+       }
+}
+
+function setActivity() {
+       title = document.title;
+       if (title.indexOf('(') != 0) {
+               document.title = "(!) " + title;
+       }
+}
+
 /**
  * Creates a new notification.
  *
@@ -792,4 +824,10 @@ $(document).ready(function() {
 
        /* activate status polling. */
        setTimeout(getStatus, 5000);
+
+       /* reset activity counter when the page has focus. */
+       $(window).focus(function() {
+               resetActivity();
+       });
+
 });