X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=9d00c5aeb6754e23892dea9b8df102db685ec358;hb=d7b6e638e17b74e3f0a074ba4f36bab1774e3701;hp=898c3c052ffb714865c35e2af37adbdb85a181a2;hpb=4a2fdf3b0b3dfcfc761c83797018237ec4c82a4b;p=Sone.git diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 898c3c0..9d00c5a 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -47,6 +47,7 @@ function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, op textarea.show(); } $(inputField.get(0).form).submit(function() { + inputField.attr("disabled", "disabled"); if (!optional && (textarea.val() == "")) { return false; } @@ -64,10 +65,11 @@ function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, op * The element to add a “comment” link to */ function addCommentLink(postId, element, insertAfterThisElement) { - if ($(element).find(".show-reply-form").length > 0) { + if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) { 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 +89,7 @@ function addCommentLink(postId, element, insertAfterThisElement) { return commentElement; })(postId); $(insertAfterThisElement).after(commentElement.clone(true)); + $(insertAfterThisElement).after(separator); } var translations = {}; @@ -150,7 +153,13 @@ function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) { toggleClass("modified", modified); $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked); $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked); - $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated); + if (lastUpdated != null) { + $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated); + } else { + getTranslation("View.Sone.Text.UnknownDate", function(unknown) { + $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(unknown); + }); + } $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name); } @@ -301,6 +310,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 +333,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 +408,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); + 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); + 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) { @@ -453,7 +552,7 @@ function ajaxifyPost(postElement) { postReply(postId, text, function(success, error, replyId) { if (success) { $(inputField).val(""); - loadNewReply(replyId); + loadNewReply(replyId, getCurrentSoneId(), postId); markPostAsKnown(getPostElement(inputField)); $("#sone .post#" + postId + " .create-reply").addClass("hidden"); } else { @@ -484,6 +583,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")); @@ -500,7 +613,10 @@ function ajaxifyPost(postElement) { }); /* mark everything as known on click. */ - $(postElement).click(function() { + $(postElement).click(function(event) { + if ($(event.target).hasClass("click-to-show")) { + return false; + } markPostAsKnown(this); }); @@ -534,6 +650,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)); @@ -566,7 +696,7 @@ function getStatus() { if ((data != null) && data.success) { /* process Sone information. */ $.each(data.sones, function(index, value) { - updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated); + updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated); }); /* process notifications. */ $.each(data.notifications, function(index, value) { @@ -585,11 +715,11 @@ function getStatus() { }); /* process new posts. */ $.each(data.newPosts, function(index, value) { - loadNewPost(value); + loadNewPost(value.id, value.sone, value.recipient, value.time); }); /* 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); @@ -604,6 +734,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 @@ -672,19 +812,49 @@ 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; +} + +/** + * 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) { +function loadNewPost(postId, soneId, recipientId, time) { + if (hasPost(postId)) { + return; + } + if (!isIndexPage()) { + if (!isViewPostPage() || (getShownPostId() != postId)) { + if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) { + return; + } + } + } + if (getPostTime($("#sone .post").last()) > time) { return; } $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) { if ((data != null) && data.success) { - if (data.post.id in loadedPosts) { + if (hasPost(data.post.id)) { return; } - loadedPosts[data.post.id] = true; if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) { return; } @@ -698,8 +868,6 @@ function loadNewPost(postId) { newPost = $(data.post.html).addClass("hidden"); if (firstOlderPost != null) { newPost.insertBefore(firstOlderPost); - } else { - $("#sone #posts").append(newPost); } ajaxifyPost(newPost); newPost.slideDown(); @@ -708,17 +876,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; } $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) { /* find post. */ if ((data != null) && data.success) { - if (data.reply.id in loadedReplies) { + if (hasReply(data.reply.id)) { return; } - loadedReplies[data.reply.id] = true; $("#sone .post#" + data.reply.postId).each(function() { var firstNewerReply = null; $(this).find(".replies .reply").each(function() { @@ -753,6 +923,7 @@ function markPostAsKnown(postElements) { (function(postElement) { $.getJSON("markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) { $(postElement).removeClass("new"); + $(".click-to-show", postElement).removeClass("new"); }); })(postElement); } @@ -811,6 +982,91 @@ 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(); +} + +/** + * Deletes the field with the given ID from the profile. + * + * @param fieldId + * The ID of the field to delete + */ +function deleteProfileField(fieldId) { + $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) { + if (data && data.success) { + $("#sone .profile-field#" + data.field.id).slideUp(); + } + }); +} + +/** + * Renames a profile field. + * + * @param fieldId + * The ID of the field to rename + * @param newName + * The new name of the field + * @param successFunction + * Called when the renaming was successful + */ +function editProfileField(fieldId, newName, successFunction) { + $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) { + if (data && data.success) { + successFunction(); + } + }); +} + +/** + * Moves the profile field with the given ID one slot in the given direction. + * + * @param fieldId + * The ID of the field to move + * @param direction + * The direction to move in (“up” or “down”) + * @param successFunction + * Function to call on success + */ +function moveProfileField(fieldId, direction, successFunction) { + $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) { + if (data && data.success) { + successFunction(); + } + }); +} + +/** + * Moves the profile field with the given ID up one slot. + * + * @param fieldId + * The ID of the field to move + * @param successFunction + * Function to call on success + */ +function moveProfileFieldUp(fieldId, successFunction) { + moveProfileField(fieldId, "up", successFunction); +} + +/** + * Moves the profile field with the given ID down one slot. + * + * @param fieldId + * The ID of the field to move + * @param successFunction + * Function to call on success + */ +function moveProfileFieldDown(fieldId, successFunction) { + moveProfileField(fieldId, "down", successFunction); +} + // // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE // @@ -823,13 +1079,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").hasClass("default")) { + 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(); @@ -844,7 +1100,7 @@ $(document).ready(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); + loadNewPost(data.postId, getCurrentSoneId()); } }); $(this).find(":input:enabled").val("").blur();