X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=be7deaa074a21800c90274564c5609039fca55e8;hb=c80d8668e387886c7fbda86e4196f65ba2e3a49c;hp=45a678f21001c823d130bc8fc3b4e0c61e997b2a;hpb=08a2857c3afa6c39464fd710d4ddd55cf7c19bca;p=Sone.git diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 45a678f..be7deaa 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -1,5 +1,9 @@ /* Sone JavaScript functions. */ +function isOnline() { + return $("#sone").hasClass("online"); +} + function registerInputTextareaSwap(inputSelector, defaultText, inputFieldName, optional) { $(inputSelector).each(function() { textarea = $("").blur(function() { @@ -42,6 +46,9 @@ function registerInputTextareaSwap(inputSelector, defaultText, inputFieldName, o /* hide all the “create reply” forms until a link is clicked. */ function addCommentLinks() { + if (!isOnline()) { + return; + } $("#sone .post").each(function() { postId = $(this).attr("id"); commentElement = (function(postId) { @@ -232,3 +239,55 @@ function enhanceDeleteReplyButton(buttonId, replyId) { }); }); } + +function getFormPassword() { + return $("#sone #formPassword").text(); +} + +function getSoneElement(element) { + return $(element).parents(".sone"); +} + +/** + * Returns the ID of the Sone that this element belongs to. + * + * @param element + * The element to locate the matching Sone ID for + * @returns The ID of the Sone, or undefined + */ +function getSoneId(element) { + return getSoneElement(element).find(".id").text(); +} + +function getPostElement(element) { + return $(element).parents(".post"); +} + +function getPostId(element) { + return getPostElement(element).attr("id"); +} + +function likePost(postId) { + $.getJSON("ajax/likePost.ajax", { "post" : postId, "formPassword": getFormPassword() }, function() { + $("#sone .post#" + postId + " > .status-line .like").addClass("hidden"); + $("#sone .post#" + postId + " > .status-line .unlike").removeClass("hidden"); + updatePostLikes(postId); + }); +} + +function unlikePost(postId) { + $.getJSON("ajax/unlikePost.ajax", { "post" : postId, "formPassword": getFormPassword() }, function() { + $("#sone .post#" + postId + " > .status-line .unlike").addClass("hidden"); + $("#sone .post#" + postId + " > .status-line .like").removeClass("hidden"); + updatePostLikes(postId); + }); +} + +function updatePostLikes(postId) { + $.getJSON("ajax/getPostLikes.ajax", { "post": postId }, function(data, textStatus) { + if (data.success) { + $("#sone .post#" + postId + " > .status-line .likes").toggleClass("hidden", data.likes == 0) + $("#sone .post#" + postId + " > .status-line .likes span.like-count").text(data.likes); + } + }); +}