X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=80d039c95546e522b9824b9015443af90faeaec1;hp=2d6c8ecbda64eb1bc08bb9be594b16454a01862d;hb=f4ea1e1b3526175e255831c975d6eea813589f25;hpb=76e7a20f5212d9c202dbe92e958834ce3f99bd0e diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 2d6c8ec..80d039c 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -68,6 +68,7 @@ function addCommentLink(postId, element, insertAfterThisElement) { return; } commentElement = (function(postId) { + separator = $(" · ").addClass("separator"); var commentElement = $("
Comment
").addClass("show-reply-form").click(function() { markPostAsKnown(getPostElement(this)); replyElement = $("#sone .post#" + postId + " .create-reply"); @@ -87,6 +88,7 @@ function addCommentLink(postId, element, insertAfterThisElement) { return commentElement; })(postId); $(insertAfterThisElement).after(commentElement.clone(true)); + $(insertAfterThisElement).after(separator); } var translations = {}; @@ -301,6 +303,17 @@ function getPostTime(element) { return getPostElement(element).find(".post-time").text(); } +/** + * Returns the author of the post the given element belongs to. + * + * @param element + * The element whose post to get the author for + * @returns The ID of the authoring Sone + */ +function getPostAuthor(element) { + return getPostElement(element).find(".post-author").text(); +} + function getReplyElement(element) { return $(element).closest(".reply"); } @@ -313,6 +326,17 @@ function getReplyTime(element) { return getReplyElement(element).find(".reply-time").text(); } +/** + * Returns the author of the reply the given element belongs to. + * + * @param element + * The element whose reply to get the author for + * @returns The ID of the authoring Sone + */ +function getReplyAuthor(element) { + return getReplyElement(element).find(".reply-author").text(); +} + function likePost(postId) { $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) { if ((data == null) || !data.success) { @@ -377,6 +401,74 @@ function unlikeReply(replyId) { }); } +/** + * Trusts the Sone with the given ID. + * + * @param soneId + * The ID of the Sone to trust + */ +function trustSone(soneId) { + $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) { + if ((data != null) && data.success) { + updateTrustControls(soneId, data.trustValue); + } + }); +} + +/** + * Distrusts the Sone with the given ID, i.e. assigns a negative trust value. + * + * @param soneId + * The ID of the Sone to distrust + */ +function distrustSone(soneId) { + $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) { + if ((data != null) && data.success) { + updateTrustControls(soneId, data.trustValue); + } + }); +} + +/** + * Untrusts the Sone with the given ID, i.e. removes any trust assignment. + * + * @param soneId + * The ID of the Sone to untrust + */ +function untrustSone(soneId) { + $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) { + if ((data != null) && data.success) { + updateTrustControls(soneId, data.trustValue); + } + }); +} + +/** + * Updates the trust controls for all posts and replies of the given Sone, + * according to the given trust value. + * + * @param soneId + * The ID of the Sone to update all trust controls for + * @param trustValue + * The trust value for the Sone + */ +function updateTrustControls(soneId, trustValue) { + $("#sone .post").each(function() { + if (getPostAuthor(this) == soneId) { + getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null); + getPostElement(this).find(".post-distrust").toggleClass("hidden", (trustValue != null) && (trustValue < 0)); + getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null); + } + }); + $("#sone .reply").each(function() { + if (getReplyAuthor(this) == soneId) { + getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null); + getReplyElement(this).find(".reply-distrust").toggleClass("hidden", (trustValue != null) && (trustValue < 0)); + getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null); + } + }); +} + function updateReplyLikes(replyId) { $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) { if ((data != null) && data.success) { @@ -484,6 +576,20 @@ function ajaxifyPost(postElement) { return false; }); + /* convert trust control buttons to javascript functions. */ + $(postElement).find(".post-trust").submit(function() { + trustSone(getPostAuthor(this)); + return false; + }); + $(postElement).find(".post-distrust").submit(function() { + distrustSone(getPostAuthor(this)); + return false; + }); + $(postElement).find(".post-untrust").submit(function() { + untrustSone(getPostAuthor(this)); + return false; + }); + /* add “comment” link. */ addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time")); @@ -537,6 +643,20 @@ function ajaxifyReply(replyElement) { })(replyElement); addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time")); + /* convert trust control buttons to javascript functions. */ + $(replyElement).find(".reply-trust").submit(function() { + trustSone(getReplyAuthor(this)); + return false; + }); + $(replyElement).find(".reply-distrust").submit(function() { + distrustSone(getReplyAuthor(this)); + return false; + }); + $(replyElement).find(".reply-untrust").submit(function() { + untrustSone(getReplyAuthor(this)); + return false; + }); + /* mark post and all replies as known on click. */ $(replyElement).click(function() { markPostAsKnown(getPostElement(this));