Marks posts as known when clicking a new post or reply.
[Sone.git] / src / main / resources / static / javascript / sone.js
index 8be0282..4b60dd9 100644 (file)
@@ -63,7 +63,7 @@ function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, op
  * @param element
  *            The element to add a “comment” link to
  */
-function addCommentLink(postId, element) {
+function addCommentLink(postId, element, insertAfterThisElement) {
        if ($(element).find(".show-reply-form").length > 0) {
                return;
        }
@@ -86,9 +86,7 @@ function addCommentLink(postId, element) {
                });
                return commentElement;
        })(postId);
-       $(element).find(".status-line .time").each(function() {
-               $(this).after(commentElement.clone(true));
-       });
+       $(insertAfterThisElement).after(commentElement.clone(true));
 }
 
 var translations = {};
@@ -484,6 +482,9 @@ function ajaxifyPost(postElement) {
                return false;
        });
 
+       /* add “comment” link. */
+       addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
+
        /* process all replies. */
        $(postElement).find(".reply").each(function() {
                ajaxifyReply(this);
@@ -493,13 +494,9 @@ function ajaxifyPost(postElement) {
        getTranslation("WebInterface.DefaultText.Reply", function(text) {
                $(postElement).find("input.reply-input").each(function() {
                        registerInputTextareaSwap(this, text, "text", false, false);
-                       addCommentLink(getPostId(postElement), postElement);
                });
        });
 
-       /* add “comment” link. */
-       addCommentLink(getPostId(postElement), postElement);
-
        /* hide reply input field. */
        $(postElement).find(".create-reply").addClass("hidden");
 }
@@ -528,7 +525,7 @@ function ajaxifyReply(replyElement) {
                        });
                });
        })(replyElement);
-       addCommentLink(getPostId(replyElement), replyElement);
+       addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
 }
 
 /**
@@ -611,7 +608,9 @@ function loadNewPost(postId) {
                                        return false;
                                }
                        });
-                       newPost = $(data.post.html).addClass("hidden");
+                       newPost = $(data.post.html).addClass("hidden").click(function() {
+                               markPostAsKnown(this);
+                       });
                        if (firstOlderPost != null) {
                                newPost.insertBefore(firstOlderPost);
                        } else {
@@ -640,7 +639,9 @@ function loadNewReply(replyId) {
                                                return false;
                                        }
                                });
-                               newReply = $(data.reply.html).addClass("hidden");
+                               newReply = $(data.reply.html).addClass("hidden").click(function() {
+                                       markPostAsKnown(getPostElement(this));
+                               });
                                if (firstNewerReply != null) {
                                        newReply.insertBefore(firstNewerReply);
                                } else {
@@ -693,9 +694,11 @@ function resetActivity() {
 }
 
 function setActivity() {
-       title = document.title;
-       if (title.indexOf('(') != 0) {
-               document.title = "(!) " + title;
+       if (!focus) {
+               title = document.title;
+               if (title.indexOf('(') != 0) {
+                       document.title = "(!) " + title;
+               }
        }
 }
 
@@ -725,6 +728,8 @@ function createNotification(id, text, dismissable) {
 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
 //
 
+var focus = true;
+
 $(document).ready(function() {
 
        /* this initializes the status update input field. */
@@ -827,7 +832,10 @@ $(document).ready(function() {
 
        /* reset activity counter when the page has focus. */
        $(window).focus(function() {
+               focus = true;
                resetActivity();
-       });
+       }).blur(function() {
+               focus = false;
+       })
 
 });