X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=bded3ce1a8031351f1d76c4c5c586a938eebe986;hb=28ab2bd3f3e4991d09682649e6b0456e6acfe016;hp=32ad68320bdbded8a597147233f2bedd886d7a4b;hpb=2beccb575d2baf1d0e9e41acc77fe742b8517ecd;p=Sone.git diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 32ad683..bded3ce 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 = {}; @@ -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,12 +494,13 @@ 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); + /* mark everything as known on click. */ + $(postElement).click(function() { + markPostAsKnown(this); + }); /* hide reply input field. */ $(postElement).find(".create-reply").addClass("hidden"); @@ -528,7 +530,12 @@ function ajaxifyReply(replyElement) { }); }); })(replyElement); - addCommentLink(getPostId(replyElement), replyElement); + addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time")); + + /* mark post and all replies as known on click. */ + $(replyElement).click(function() { + markPostAsKnown(getPostElement(replyElement)); + }); } /** @@ -569,6 +576,7 @@ function getStatus() { $("#sone #notification-area").append(notification); notification.slideDown(); } + setActivity(); }); $.each(data.removedNotifications, function(index, value) { $("#sone #notification-area .notification#" + value.id).slideUp(); @@ -593,6 +601,65 @@ function getStatus() { }) } +/** + * Returns the content of the page-id attribute. + * + * @returns The page ID + */ +function getPageId() { + return $("#sone .page-id").text(); +} + +/** + * Returns whether the current page is the index page. + * + * @returns {Boolean} true if the current page is the index page, + * false otherwise + */ +function isIndexPage() { + return getPageId() == "index"; +} + +/** + * Returns whether the current page is a “view Sone” page. + * + * @returns {Boolean} true if the current page is a “view Sone” + * page, false otherwise + */ +function isViewSonePage() { + return getPageId() == "view-sone"; +} + +/** + * Returns the ID of the currently shown Sone. This will only return a sensible + * value if isViewSonePage() returns true. + * + * @returns The ID of the currently shown Sone + */ +function getSoneId() { + return $("#sone .sone-id").text(); +} + +/** + * Returns whether the current page is a “view post” page. + * + * @returns {Boolean} true if the current page is a “view post” + * page, false otherwise + */ +function isViewPostPage() { + return getPageId() == "view-post"; +} + +/** + * Returns the ID of the currently shown post. This will only return a sensible + * value if isViewPostPage() returns true. + * + * @returns The ID of the currently shown post + */ +function getPostId() { + return $("#sone .post-id").text(); +} + var loadedPosts = {}; var loadedReplies = {}; @@ -603,6 +670,9 @@ function loadNewPost(postId) { loadedPosts[postId] = true; $.getJSON("ajax/getPost.ajax", { "post" : postId }, function(data, textStatus) { if ((data != null) && data.success) { + if (!isIndexPage() && !(isViewSonePage() && (getSoneId() == data.post.sone))) { + return; + } var firstOlderPost = null; $("#sone .post").each(function() { if (getPostTime(this) < data.post.time) { @@ -614,10 +684,11 @@ function loadNewPost(postId) { if (firstOlderPost != null) { newPost.insertBefore(firstOlderPost); } else { - $("#sone #posts").append(newPost); + $("#sone .post:last").append(newPost); } ajaxifyPost(newPost); newPost.slideDown(); + setActivity(); } }); } @@ -650,6 +721,7 @@ function loadNewReply(replyId) { } ajaxifyReply(newReply); newReply.slideDown(); + setActivity(); }); } }); @@ -659,9 +731,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")); @@ -671,13 +745,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. * @@ -704,6 +796,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. */ @@ -734,30 +828,32 @@ $(document).ready(function() { }); /* hides all replies but the latest two. */ - getTranslation("WebInterface.ClickToShow.Replies", function(text) { - $("#sone .post .replies").each(function() { - allReplies = $(this).find(".reply"); - if (allReplies.length > 2) { - newHidden = false; - for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) { - $(allReplies[replyIndex]).addClass("hidden"); - newHidden |= $(allReplies[replyIndex]).hasClass("new"); - } - clickToShowElement = $("
").addClass("click-to-show"); - if (newHidden) { - clickToShowElement.addClass("new"); + if (!isViewPostPage()) { + getTranslation("WebInterface.ClickToShow.Replies", function(text) { + $("#sone .post .replies").each(function() { + allReplies = $(this).find(".reply"); + if (allReplies.length > 2) { + newHidden = false; + for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) { + $(allReplies[replyIndex]).addClass("hidden"); + newHidden |= $(allReplies[replyIndex]).hasClass("new"); + } + clickToShowElement = $("
").addClass("click-to-show"); + if (newHidden) { + clickToShowElement.addClass("new"); + } + (function(clickToShowElement, allReplies, text) { + clickToShowElement.text(text); + clickToShowElement.click(function() { + allReplies.removeClass("hidden"); + clickToShowElement.addClass("hidden"); + }); + })(clickToShowElement, allReplies, text); + $(allReplies[0]).before(clickToShowElement); } - (function(clickToShowElement, allReplies, text) { - clickToShowElement.text(text); - clickToShowElement.click(function() { - allReplies.removeClass("hidden"); - clickToShowElement.addClass("hidden"); - }); - })(clickToShowElement, allReplies, text); - $(allReplies[0]).before(clickToShowElement); - } + }); }); - }); + } /* * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something @@ -803,4 +899,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; + }) + });