X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=e07c51ef775c99d0cf9e46edbb8e83463f561fd3;hb=39788815bb8b499128d895304b5aee707dfc3b9a;hp=d5c4461080789aa6e2af49b63720a98bbca6b654;hpb=221c7e9749a57e778ca778f231923f17da2f200d;p=Sone.git diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index d5c4461..e07c51e 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -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 = {}; @@ -475,13 +473,18 @@ 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; }); + /* add “comment” link. */ + addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time")); + /* process all replies. */ $(postElement).find(".reply").each(function() { ajaxifyReply(this); @@ -491,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"); } @@ -511,10 +510,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) { @@ -524,7 +525,7 @@ function ajaxifyReply(replyElement) { }); }); })(replyElement); - addCommentLink(getPostId(replyElement), replyElement); + addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time")); } /** @@ -565,6 +566,7 @@ function getStatus() { $("#sone #notification-area").append(notification); notification.slideDown(); } + setActivity(); }); $.each(data.removedNotifications, function(index, value) { $("#sone #notification-area .notification#" + value.id).slideUp(); @@ -614,6 +616,7 @@ function loadNewPost(postId) { } ajaxifyPost(newPost); newPost.slideDown(); + setActivity(); } }); } @@ -646,6 +649,7 @@ function loadNewReply(replyId) { } ajaxifyReply(newReply); newReply.slideDown(); + setActivity(); }); } }); @@ -655,9 +659,11 @@ function markPostAsKnown(postElements) { $(postElements).each(function() { postElement = this; if ($(postElement).hasClass("new")) { - $.getJSON("ajax/markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) { - $(postElement).removeClass("new"); - }); + (function(postElement) { + $.getJSON("ajax/markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) { + $(postElement).removeClass("new"); + }); + })(postElement); } }); markReplyAsKnown($(postElements).find(".reply")); @@ -667,13 +673,31 @@ function markReplyAsKnown(replyElements) { $(replyElements).each(function() { replyElement = this; if ($(replyElement).hasClass("new")) { - $.getJSON("ajax/markReplyAsKnown.ajax", {"formPassword": getFormPassword(), "reply": getReplyId(replyElement)}, function(data, textStatus) { - $(replyElement).removeClass("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() { + if (!focus) { + title = document.title; + if (title.indexOf('(') != 0) { + document.title = "(!) " + title; + } + } +} + /** * Creates a new notification. * @@ -700,6 +724,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. */ @@ -799,4 +825,13 @@ $(document).ready(function() { /* activate status polling. */ setTimeout(getStatus, 5000); + + /* reset activity counter when the page has focus. */ + $(window).focus(function() { + focus = true; + resetActivity(); + }).blur(function() { + focus = false; + }) + });