X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=ca087fadab83f6f8860225f2e126f74bbdec4b65;hb=edc855dd691b472f36007fc8492e0f70e3301c60;hp=02a02a8f51fbde70879d280d248968aa6d38f926;hpb=6db033602e7c17efc40a4a19efe705d67832fc1a;p=Sone.git diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 02a02a8..ca087fa 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -256,7 +256,7 @@ function getFormPassword() { } function getSoneElement(element) { - return $(element).parents(".sone"); + return $(element).closest(".sone"); } /** @@ -449,16 +449,18 @@ function ajaxifyPost(postElement) { inputField = $(this.form).find(":input:enabled").get(0); postId = getPostId(this); text = $(inputField).val(); - $(inputField).val(""); - postReply(postId, text, function(success, error, replyId) { - if (success) { - loadNewReply(replyId); - markPostAsKnown(getPostElement(inputField)); - $("#sone .post#" + postId + " .create-reply").addClass("hidden"); - } else { - alert(error); - } - }); + (function(postId, text, inputField) { + postReply(postId, text, function(success, error, replyId) { + if (success) { + $(inputField).val(""); + loadNewReply(replyId, getCurrentSoneId(), postId); + markPostAsKnown(getPostElement(inputField)); + $("#sone .post#" + postId + " .create-reply").addClass("hidden"); + } else { + alert(error); + } + }); + })(postId, text, inputField); return false; }); @@ -534,7 +536,7 @@ function ajaxifyReply(replyElement) { /* mark post and all replies as known on click. */ $(replyElement).click(function() { - markPostAsKnown(getPostElement(replyElement)); + markPostAsKnown(getPostElement(this)); }); } @@ -583,11 +585,11 @@ function getStatus() { }); /* process new posts. */ $.each(data.newPosts, function(index, value) { - loadNewPost(value); + loadNewPost(value.id, value.sone, value.recipient); }); /* process new replies. */ $.each(data.newReplies, function(index, value) { - loadNewReply(value); + loadNewReply(value.id, value.sone, value.post, value.postSone); }); /* do it again in 5 seconds. */ setTimeout(getStatus, 5000); @@ -602,6 +604,16 @@ function getStatus() { } /** + * Returns the ID of the currently logged in Sone. + * + * @return The ID of the current Sone, or an empty string if no Sone is logged + * in + */ +function getCurrentSoneId() { + return $("#currentSoneId").text(); +} + +/** * Returns the content of the page-id attribute. * * @returns The page ID @@ -670,17 +682,47 @@ function isKnownSonesPage() { return getPageId() == "known-sones"; } -var loadedPosts = {}; -var loadedReplies = {}; +/** + * Returns whether a post with the given ID exists on the current page. + * + * @param postId + * The post ID to check for + * @returns {Boolean} true if a post with the given ID already + * exists on the page, false otherwise + */ +function hasPost(postId) { + return $(".post#" + postId).length > 0; +} -function loadNewPost(postId) { - if (postId in loadedPosts) { +/** + * Returns whether a reply with the given ID exists on the current page. + * + * @param replyId + * The reply ID to check for + * @returns {Boolean} true if a reply with the given ID already + * exists on the page, false otherwise + */ +function hasReply(replyId) { + return $("#sone .reply#" + replyId).length > 0; +} + +function loadNewPost(postId, soneId, recipientId) { + if (hasPost(postId)) { return; } - loadedPosts[postId] = true; + if (!isIndexPage()) { + if (!isViewPostPage() || (getShownPostId() != postId)) { + if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) { + return; + } + } + } $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) { if ((data != null) && data.success) { - if (!isIndexPage() && !(isViewSonePage() && (getShownSoneId() == data.post.sone))) { + if (hasPost(data.post.id)) { + return; + } + if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) { return; } var firstOlderPost = null; @@ -694,7 +736,7 @@ function loadNewPost(postId) { if (firstOlderPost != null) { newPost.insertBefore(firstOlderPost); } else { - $("#sone .post:last").append(newPost); + $("#sone #posts").append(newPost); } ajaxifyPost(newPost); newPost.slideDown(); @@ -703,14 +745,19 @@ function loadNewPost(postId) { }); } -function loadNewReply(replyId) { - if (replyId in loadedReplies) { +function loadNewReply(replyId, soneId, postId, postSoneId) { + if (hasReply(replyId)) { + return; + } + if (!hasPost(postId)) { return; } - loadedReplies[replyId] = true; $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) { /* find post. */ if ((data != null) && data.success) { + if (hasReply(data.reply.id)) { + return; + } $("#sone .post#" + data.reply.postId).each(function() { var firstNewerReply = null; $(this).find(".replies .reply").each(function() { @@ -732,6 +779,7 @@ function loadNewReply(replyId) { ajaxifyReply(newReply); newReply.slideDown(); setActivity(); + return false; }); } }); @@ -802,6 +850,17 @@ function createNotification(id, text, dismissable) { return notification; } +/** + * Shows the details of the notification with the given ID. + * + * @param notificationId + * The ID of the notification + */ +function showNotificationDetails(notificationId) { + $("#sone .notification#" + notificationId + " .text").show(); + $("#sone .notification#" + notificationId + " .short-text").hide(); +} + // // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE // @@ -814,10 +873,13 @@ $(document).ready(function() { getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) { registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false); $("#sone #update-status").submit(function() { + if ($(this).find(":input.default:enabled").length > 0) { + return false; + } text = $(this).find(":input:enabled").val(); $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "text": text }, function(data, textStatus) { if ((data != null) && data.success) { - loadNewPost(data.postId); + loadNewPost(data.postId, getCurrentSoneId()); } }); $(this).find(":input:enabled").val("").blur(); @@ -830,9 +892,9 @@ $(document).ready(function() { registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false); $("#sone #post-message").submit(function() { text = $(this).find(":input:enabled").val(); - $.getJSON("ajax/createPost.ajax", { "formPassword": getFormPassword(), "recipient": $("#sone #sone-id").text(), "text": text }, function(data, textStatus) { + $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "text": text }, function(data, textStatus) { if ((data != null) && data.success) { - loadNewPost(data.postId); + loadNewPost(data.postId, getCurrentSoneId()); } }); $(this).find(":input:enabled").val("").blur();