X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=75fa965184c907e5cf63393c80c7dee3f925e6cc;hb=5ac2eeb4296b651f380e2d35ad35cf4bfc70c766;hp=01f13e3c4fc73a6e1c7b4eba1156576849d4805e;hpb=fa7f3a80a777bf743bcc5d7b95bfd811a417651f;p=Sone.git diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 01f13e3..75fa965 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -43,28 +43,40 @@ function addCommentLinks() { } $("#sone .post").each(function() { postId = $(this).attr("id"); - commentElement = (function(postId) { - var commentElement = $("
Comment
").addClass("show-reply-form").click(function() { - replyElement = $("#sone .post#" + postId + " .create-reply"); - replyElement.removeClass("hidden"); - replyElement.removeClass("light"); - (function(replyElement) { - replyElement.find("input.reply-input").blur(function() { - if ($(this).hasClass("default")) { - replyElement.addClass("light"); - } - }).focus(function() { - replyElement.removeClass("light"); - }); - })(replyElement); - replyElement.find("input.reply-input").focus(); - }); - return commentElement; - })(postId); - $(this).find(".create-reply").addClass("hidden"); - $(this).find(".status-line .time").each(function() { - $(this).after(commentElement.clone(true)); + addCommentLink(postId, $(this)); + }); +} + +/** + * Adds a “comment” link to all status lines contained in the given element. + * + * @param postId + * The ID of the post + * @param element + * The element to add a “comment” link to + */ +function addCommentLink(postId, element) { + commentElement = (function(postId) { + var commentElement = $("
Comment
").addClass("show-reply-form").click(function() { + replyElement = $("#sone .post#" + postId + " .create-reply"); + replyElement.removeClass("hidden"); + replyElement.removeClass("light"); + (function(replyElement) { + replyElement.find("input.reply-input").blur(function() { + if ($(this).hasClass("default")) { + replyElement.addClass("light"); + } + }).focus(function() { + replyElement.removeClass("light"); + }); + })(replyElement); + replyElement.find("input.reply-input").focus(); }); + return commentElement; + })(postId); + element.find(".create-reply").addClass("hidden"); + element.find(".status-line .time").each(function() { + $(this).after(commentElement.clone(true)); }); } @@ -88,17 +100,20 @@ function getTranslation(key, callback) { * * @param soneId * The ID of the Sone + * @param local + * true if the Sone is local, false + * otherwise */ -function getSoneStatus(soneId) { +function getSoneStatus(soneId, local) { $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) { updateSoneStatus(soneId, data.name, data.status, data.modified, data.lastUpdated); /* seconds! */ updateInterval = 60; - if (data.modified || (data.status == "downloading") || (data.status == "inserting")) { + if (local || data.modified || (data.status == "downloading") || (data.status == "inserting")) { updateInterval = 5; } setTimeout(function() { - getSoneStatus(soneId); + getSoneStatus(soneId, local); }, updateInterval * 1000); }); } @@ -145,15 +160,18 @@ var watchedSones = {}; * * @param soneId * The ID of the Sone to watch + * @param local + * true if the Sone is local, false + * otherwise */ -function watchSone(soneId) { +function watchSone(soneId, local) { if (watchedSones[soneId]) { return; } watchedSones[soneId] = true; (function(soneId) { setTimeout(function() { - getSoneStatus(soneId); + getSoneStatus(soneId, local); }, 5000); })(soneId); } @@ -351,3 +369,60 @@ function updateReplyLikes(replyId) { } }); } + +/** + * Posts a reply and calls the given callback when the request finishes. + * + * @param postId + * The ID of the post the reply refers to + * @param text + * The text to post + * @param callbackFunction + * The callback function to call when the request finishes (takes 3 + * parameters: success, error, replyId) + */ +function postReply(postId, text, callbackFunction) { + $.getJSON("ajax/createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) { + if (data.success) { + callbackFunction(true, null, data.reply); + } else { + callbackFunction(false, data.error); + } + }); +} + +/** + * Requests information about the reply with the given ID. + * + * @param replyId + * The ID of the reply + * @param callbackFunction + * A callback function (parameters soneId, soneName, replyTime, + * replyDisplayTime, text, html) + */ +function getReply(replyId, callbackFunction) { + $.getJSON("ajax/getReply.ajax", { "reply" : replyId }, function(data, textStatus) { + if (data.success) { + callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html); + } + }); +} + +/** + * Ajaxifies the given notification by replacing the form with AJAX. + * + * @param notification + * jQuery object representing the notification. + */ +function ajaxifyNotification(notification) { + notification.find("form.dismiss").submit(function() { + return false; + }); + notification.find("form.dismiss button").click(function() { + $.getJSON("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) { + if (data.success) { + notification.slideUp(); + } + }); + }); +}