X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=16eb806a014eeeb60e1d7464fd98479826318e3a;hb=bf40f8d978e4564acece0bf68b645b0e705bd714;hp=b7f323e95e9ab1405c3f9e385ebffaf4cd35be82;hpb=8454806e48890675178c1b4b6cbe4bb9c42b68a3;p=Sone.git diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index b7f323e..16eb806 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); + 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)); }); } @@ -560,7 +562,7 @@ function ajaxifyNotification(notification) { } function getStatus() { - $.getJSON("getStatus.ajax", {}, function(data, textStatus) { + $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) { if ((data != null) && data.success) { /* process Sone information. */ $.each(data.sones, function(index, value) { @@ -636,7 +638,7 @@ function isViewSonePage() { * * @returns The ID of the currently shown Sone */ -function getSoneId() { +function getShownSoneId() { return $("#sone .sone-id").text(); } @@ -656,21 +658,54 @@ function isViewPostPage() { * * @returns The ID of the currently shown post */ -function getPostId() { +function getShownPostId() { return $("#sone .post-id").text(); } -var loadedPosts = {}; -var loadedReplies = {}; +/** + * Returns whether the current page is the “known Sones” page. + * + * @returns {Boolean} true if the current page is the “known + * Sones” page, false otherwise + */ +function isKnownSonesPage() { + return getPageId() == "known-sones"; +} + +/** + * 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; +} + +/** + * 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) { - if (postId in loadedPosts) { + if (hasPost(postId)) { return; } - loadedPosts[postId] = true; $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) { if ((data != null) && data.success) { - if (!isIndexPage() && !(isViewSonePage() && (getSoneId() == data.post.sone))) { + if (hasPost(data.post.id)) { + return; + } + if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) { return; } var firstOlderPost = null; @@ -684,7 +719,7 @@ function loadNewPost(postId) { if (firstOlderPost != null) { newPost.insertBefore(firstOlderPost); } else { - $("#sone .post:last").append(newPost); + $("#sone #posts").append(newPost); } ajaxifyPost(newPost); newPost.slideDown(); @@ -694,13 +729,15 @@ function loadNewPost(postId) { } function loadNewReply(replyId) { - if (replyId in loadedReplies) { + if (hasReply(replyId)) { 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() { @@ -722,6 +759,7 @@ function loadNewReply(replyId) { ajaxifyReply(newReply); newReply.slideDown(); setActivity(); + return false; }); } }); @@ -792,6 +830,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 // @@ -804,6 +853,9 @@ $(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) { @@ -815,6 +867,28 @@ $(document).ready(function() { }); }); + /* ajaxify input field on “view Sone” page. */ + getTranslation("WebInterface.DefaultText.Message", function(defaultText) { + registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false); + $("#sone #post-message").submit(function() { + text = $(this).find(":input:enabled").val(); + $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "text": text }, function(data, textStatus) { + if ((data != null) && data.success) { + loadNewPost(data.postId); + } + }); + $(this).find(":input:enabled").val("").blur(); + return false; + }); + }); + + /* ajaxify album creation input field. */ + getTranslation("WebInterface.DefaultText.Reply", function(text) { + $("#create-album input[type=text]".each(function() { + registerInputTextareaSwap(this, text, "name", false, true); + }); + }); + /* Ajaxifies all posts. */ /* calling getTranslation here will cache the necessary values. */ getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {