X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=d9f3bf112ec43537145743e514d7eebb76a37aa1;hb=8f4493c5b704b878c5aedc27a1dcdb906d9b3c3d;hp=efcf7ccabb902153850b32969506a16f76105d70;hpb=7cba88f458cf0fe8831e5e50fd2a848cdadf5492;p=Sone.git diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index efcf7cc..d9f3bf1 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -495,74 +495,6 @@ function unlikeReply(replyId) { } /** - * Trusts the Sone with the given ID. - * - * @param soneId - * The ID of the Sone to trust - */ -function trustSone(soneId) { - ajaxGet("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data) { - 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) { - ajaxGet("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data) { - 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) { - ajaxGet("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data) { - 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.find(".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.find(".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); - } - }); -} - -/** * Bookmarks the post with the given ID. * * @param postId @@ -685,6 +617,40 @@ function ajaxifySone(soneElement) { }); } +function followSone(soneId) { + return function() { + const followElement = this; + ajaxGet("followSone.ajax", {"sone": soneId, "formPassword": getFormPassword()}, function () { + $(followElement).addClass("hidden"); + $(followElement).parent().find(".unfollow").removeClass("hidden"); + sone.find(".sone-menu").each(function () { + if (getMenuSone(this) === soneId) { + $(".follow", this).toggleClass("hidden", true); + $(".unfollow", this).toggleClass("hidden", false); + } + }); + }); + return false; + } +} + +function unfollowSone(soneId) { + return function() { + const unfollowElement = this; + ajaxGet("unfollowSone.ajax", {"sone": soneId, "formPassword": getFormPassword()}, function () { + $(unfollowElement).addClass("hidden"); + $(unfollowElement).parent().find(".follow").removeClass("hidden"); + sone.find(".sone-menu").each(function () { + if (getMenuSone(this) === soneId) { + $(".follow", this).toggleClass("hidden", false); + $(".unfollow", this).toggleClass("hidden", true); + } + }); + }); + return false; + } +}; + /** * Ajaxifies the given post by enhancing all eligible elements with AJAX. * @@ -739,20 +705,6 @@ 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; - }); - /* convert bookmark/unbookmark buttons to javascript functions. */ $(postElement).find(".bookmark").submit(function() { bookmarkPost(getPostId(this)); @@ -785,7 +737,7 @@ function ajaxifyPost(postElement) { }); /* convert “show more” link into javascript function. */ - $(postElement).find(".expand-post-text").each(function() { + const toggleShowMore = function() { $(this).click(function() { $(".post-text.text", getPostElement(this)).toggleClass("hidden"); $(".post-text.short-text", getPostElement(this)).toggleClass("hidden"); @@ -793,16 +745,9 @@ function ajaxifyPost(postElement) { $(".shrink-post-text", getPostElement(this)).toggleClass("hidden"); return false; }); - }); - $(postElement).find(".shrink-post-text").each(function() { - $(this).click(function() { - $(".post-text.text", getPostElement(this)).toggleClass("hidden"); - $(".post-text.short-text", getPostElement(this)).toggleClass("hidden"); - $(".expand-post-text", getPostElement(this)).toggleClass("hidden"); - $(".shrink-post-text", getPostElement(this)).toggleClass("hidden"); - return false; - }); - }); + }; + $(postElement).find(".expand-post-text").each(toggleShowMore); + $(postElement).find(".shrink-post-text").each(toggleShowMore); /* ajaxify author/post links */ $(".post-status-line .permalink a", postElement).click(function() { @@ -872,34 +817,8 @@ function ajaxifyPost(postElement) { }); (function(postElement) { const soneId = $(".sone-menu-id:first", postElement).text(); - $(".sone-post-menu .follow", postElement).click(function() { - const followElement = this; - ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() { - $(followElement).addClass("hidden"); - $(followElement).parent().find(".unfollow").removeClass("hidden"); - sone.find(".sone-menu").each(function() { - if (getMenuSone(this) === soneId) { - $(".follow", this).toggleClass("hidden", true); - $(".unfollow", this).toggleClass("hidden", false); - } - }); - }); - return false; - }); - $(".sone-post-menu .unfollow", postElement).click(function() { - const unfollowElement = this; - ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() { - $(unfollowElement).addClass("hidden"); - $(unfollowElement).parent().find(".follow").removeClass("hidden"); - sone.find(".sone-menu").each(function() { - if (getMenuSone(this) === soneId) { - $(".follow", this).toggleClass("hidden", false); - $(".unfollow", this).toggleClass("hidden", true); - } - }); - }); - return false; - }); + $(".sone-post-menu .follow", postElement).click(followSone(soneId)); + $(".sone-post-menu .unfollow", postElement).click(unfollowSone(soneId)); })(postElement); } @@ -959,16 +878,7 @@ function ajaxifyReply(replyElement) { }); /* convert “show more” link into javascript function. */ - $(replyElement).find(".expand-reply-text").each(function() { - $(this).click(function() { - $(".reply-text.text", getReplyElement(this)).toggleClass("hidden"); - $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden"); - $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden"); - $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden"); - return false; - }); - }); - $(replyElement).find(".shrink-reply-text").each(function() { + const toggleShowMore = function() { $(this).click(function() { $(".reply-text.text", getReplyElement(this)).toggleClass("hidden"); $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden"); @@ -976,21 +886,9 @@ function ajaxifyReply(replyElement) { $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden"); return false; }); - }); - - /* 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; - }); + }; + $(replyElement).find(".expand-reply-text").each(toggleShowMore); + $(replyElement).find(".shrink-reply-text").each(toggleShowMore); /* show Sone menu when hovering over the avatar. */ $(replyElement).find(".reply-avatar").mouseover(function() { @@ -1011,34 +909,8 @@ function ajaxifyReply(replyElement) { }); (function(replyElement) { const soneId = $(".sone-menu-id", replyElement).text(); - $(".sone-menu .follow", replyElement).click(function() { - const followElement = this; - ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() { - $(followElement).addClass("hidden"); - $(followElement).parent().find(".unfollow").removeClass("hidden"); - sone.find(".sone-menu").each(function() { - if (getMenuSone(this) === soneId) { - $(".follow", this).toggleClass("hidden", true); - $(".unfollow", this).toggleClass("hidden", false); - } - }); - }); - return false; - }); - $(".sone-menu .unfollow", replyElement).click(function() { - const followElement = this; - ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() { - $(unfollowElement).addClass("hidden"); - $(unfollowElement).parent().find(".follow").removeClass("hidden"); - sone.find(".sone-menu").each(function() { - if (getMenuSone(this) === soneId) { - $(".follow", this).toggleClass("hidden", false); - $(".unfollow", this).toggleClass("hidden", true); - } - }); - }); - return false; - }); + $(".sone-menu .follow", replyElement).click(followSone(soneId)); + $(".sone-menu .unfollow", replyElement).click(unfollowSone(soneId)); })(replyElement); } @@ -1186,12 +1058,47 @@ function checkForRemovedReplies(oldNotification, newNotification) { }); } +/** + * The URLs of not-loaded elements are part of the GET request’s URL. As + * both browsers and HTTP servers do have differing limits on URL length + * (the HTTP 1.1 RFC states 8000 bytes but most browsers only support up + * to 2000 bytes) we will return a random selection of not-loaded URLs we + * want to refresh the status from up until we are at approximately 1000 + * bytes (as the rest of the URL also needs some space). + * + * @return An array of not-loaded element URLs that will have a total length + * of 1000 bytes or fewer + */ +function getRandomSelectionOfElementsToUpdate() { + const notLoadedElementUrls = $(".linked-element.not-loaded").map(function(_, element) { + return $(element).prop("title"); + }).toArray(); + shuffleArray(notLoadedElementUrls); + const selectedElementUrls = Array(); + let currentCombinedStringLength = 0; + $(notLoadedElementUrls).each(function(_, elementUrl) { + if ((currentCombinedStringLength + elementUrl.length) <= 1000) { + selectedElementUrls.push(elementUrl); + currentCombinedStringLength += elementUrl.length; + } + }); + return selectedElementUrls; +} + +// shamelessly stolen from https://stackoverflow.com/a/12646864/43582 +function shuffleArray(array) { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + const temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } +} + function getStatus() { const parameters = isViewSonePage() ? {"soneIds": getShownSoneId()} : isKnownSonesPage() ? {"soneIds": getShownSoneIds()} : {}; $.extend(parameters, { - "elements": JSON.stringify($(".linked-element.not-loaded").map(function () { - return $(this).prop("title"); - }).toArray()) + "elements": JSON.stringify(getRandomSelectionOfElementsToUpdate()) }); ajaxGet("getStatus.ajax", parameters, function(data) { if ((data != null) && data.success) { @@ -1309,7 +1216,7 @@ function getCurrentSoneId() { /** * Returns the content of the page-id attribute. * - * @returns The page ID + * @returns String The page ID */ function getPageId() { return sone.find(".page-id").text(); @@ -1653,7 +1560,7 @@ function updatePostTimes(postIds) { /** * Updates the time of the reply with the given ID. * - * @param postId + * @param replyId * The ID of the reply to update * @param timeText * The text of the time to show @@ -1674,7 +1581,7 @@ function updateReplyTime(replyId, timeText, refreshTime, tooltip) { /** * Requests new rendered times for the posts with the given IDs. * - * @param postIds + * @param replyIds * Comma-separated post IDs */ function updateReplyTimes(replyIds) {