X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=8be028279d2b549a4a15e9eef9c57cd3d88b1f2d;hb=0c793f7f21339606808799d513d28f86f7fa6bfd;hp=133e1d4c295a2d39bb769c6d40211ea20df32ac6;hpb=e0e0192f88d5b5c27b7d29bcb3778d53e38a9897;p=Sone.git diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 133e1d4..8be0282 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -69,6 +69,7 @@ function addCommentLink(postId, element) { } commentElement = (function(postId) { var commentElement = $("
Comment
").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) { @@ -453,11 +454,9 @@ function ajaxifyPost(postElement) { $(inputField).val(""); postReply(postId, text, function(success, error, replyId) { if (success) { - getReply(replyId, function(soneId, soneName, replyTime, replyDisplayTime, text, html) { - newReply = $(html).insertBefore("#sone .post#" + postId + " .create-reply"); - $("#sone .post#" + postId + " .create-reply").addClass("hidden"); - ajaxifyReply(newReply); - }); + loadNewReply(replyId); + markPostAsKnown(getPostElement(inputField)); + $("#sone .post#" + postId + " .create-reply").addClass("hidden"); } else { alert(error); } @@ -476,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; }); @@ -512,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) { @@ -566,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(); @@ -601,7 +605,7 @@ function loadNewPost(postId) { $.getJSON("ajax/getPost.ajax", { "post" : postId }, function(data, textStatus) { if ((data != null) && data.success) { var firstOlderPost = null; - $("#sone #posts .post").each(function() { + $("#sone .post").each(function() { if (getPostTime(this) < data.post.time) { firstOlderPost = $(this); return false; @@ -611,10 +615,11 @@ function loadNewPost(postId) { if (firstOlderPost != null) { newPost.insertBefore(firstOlderPost); } else { - $("#sone #posts .post:last").after(newPost); + $("#sone #posts").append(newPost); } ajaxifyPost(newPost); newPost.slideDown(); + setActivity(); } }); } @@ -626,23 +631,72 @@ function loadNewReply(replyId) { loadedReplies[replyId] = true; $.getJSON("ajax/getReply.ajax", { "reply": replyId }, function(data, textStatus) { /* find post. */ - $("#sone #posts .post#" + data.reply.postId).each(function() { - var firstNewerReply = null; - $(this).find(".replies .reply").each(function() { - if (getReplyTime(this) > data.reply.time) { - firstNewerReply = $(this); - return false; + if ((data != null) && data.success) { + $("#sone .post#" + data.reply.postId).each(function() { + var firstNewerReply = null; + $(this).find(".replies .reply").each(function() { + if (getReplyTime(this) > data.reply.time) { + firstNewerReply = $(this); + return false; + } + }); + newReply = $(data.reply.html).addClass("hidden"); + if (firstNewerReply != null) { + newReply.insertBefore(firstNewerReply); + } else { + if ($(this).find(".replies .create-reply")) { + $(this).find(".replies .create-reply").before(newReply); + } else { + $(this).find(".replies").append(newReply); + } } + ajaxifyReply(newReply); + newReply.slideDown(); + setActivity(); }); - newReply = $(data.reply.html); - if (firstNewerReply != null) { - newReply.insertAfter(firstNewerReply); - } else { - $(this).find(".replies .reply:last").after(newReply); - } - ajaxifyReply(newReply); - }); + } + }); +} + +function markPostAsKnown(postElements) { + $(postElements).each(function() { + postElement = this; + 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; + 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; + } } /** @@ -770,4 +824,10 @@ $(document).ready(function() { /* activate status polling. */ setTimeout(getStatus, 5000); + + /* reset activity counter when the page has focus. */ + $(window).focus(function() { + resetActivity(); + }); + });