X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=100e9db33b1033d3da5b07c658922832ac3cb06d;hp=591afa022e039890d5ded16e4a53866c12a1b694;hb=a02811cd7b38393e0d59f8a90a8a617fd8ca160a;hpb=67c3d6cf09d8e7784d6cc5e0c4116b12fe15dd00 diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 591afa0..100e9db 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -67,30 +67,31 @@ function addCommentLink(postId, author, element, insertAfterThisElement) { if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) { return; } - commentElement = (function(postId, author) { + (function(postId, author, insertAfterThisElement) { separator = $(" · ").addClass("separator"); - var commentElement = $("
Comment
").addClass("show-reply-form").click(function() { - replyElement = $("#sone .post#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); - textArea = replyElement.find("input.reply-input").focus().data("textarea"); - if (author != getCurrentSoneId()) { - textArea.val(textArea.val() + "@sone://" + author + " "); - } + getTranslation("WebInterface.Button.Comment", function(text) { + commentElement = $("
" + text + "
").addClass("show-reply-form").click(function() { + replyElement = $("#sone .post#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); + textArea = replyElement.find("input.reply-input").focus().data("textarea"); + if (author != getCurrentSoneId()) { + textArea.val(textArea.val() + "@sone://" + author + " "); + } + }); + $(insertAfterThisElement).after(commentElement.clone(true)); + $(insertAfterThisElement).after(separator); }); - return commentElement; - })(postId, author); - $(insertAfterThisElement).after(commentElement.clone(true)); - $(insertAfterThisElement).after(separator); + })(postId, author, insertAfterThisElement); } var translations = {}; @@ -874,7 +875,7 @@ function ajaxifyPost(postElement) { } }); (function(postElement) { - var soneId = $(".sone-menu-id", postElement).text(); + var soneId = $(".sone-menu-id:first", postElement).text(); $(".sone-post-menu .follow", postElement).click(function() { var followElement = this; ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() { @@ -1091,7 +1092,7 @@ function ajaxifyNotification(notification) { * Returns the notification hash. This hash is used in {@link #getStatus()} to * determine whether the notifications changed and need to be reloaded. */ -public getNotificationHash() { +function getNotificationHash() { return $("#sone #notification-area #notification-hash").text(); } @@ -1101,7 +1102,7 @@ public getNotificationHash() { * @param notificationHash * The new notification hash */ -public setNotificationHash(notificationHash) { +function setNotificationHash(notificationHash) { $("#sone #notification-area #notification-hash").text(notificationHash); } @@ -1200,6 +1201,33 @@ function getStatus() { if (!notLoggedIn) { showOfflineMarker(!online); } + if (data.notificationHash != getNotificationHash()) { + console.log("Old hash: ", getNotificationHash(), ", new hash: ", data.notificationHash); + requestNotifications(); + } + /* process new posts. */ + $.each(data.newPosts, function(index, value) { + loadNewPost(value.id, value.sone, value.recipient, value.time); + }); + /* process new replies. */ + $.each(data.newReplies, function(index, value) { + loadNewReply(value.id, value.sone, value.post, value.postSone); + }); + /* do it again in 5 seconds. */ + setTimeout(getStatus, 5000); + } else { + /* data.success was false, wait 30 seconds. */ + setTimeout(getStatus, 30000); + } + }, function() { + statusRequestQueued = false; + ajaxError(); + }); +} + +function requestNotifications() { + ajaxGet("getNotifications.ajax", {}, function(data, textStatus) { + if (data && data.success) { /* search for removed notifications. */ $("#sone #notification-area .notification").each(function() { notificationId = $(this).attr("id"); @@ -1237,69 +1265,29 @@ function getStatus() { } }); /* process notifications. */ - notificationIds = []; $.each(data.notifications, function(index, value) { oldNotification = getNotification(value.id); - if ((oldNotification.length == 0) || (value.lastUpdatedTime > getNotificationLastUpdatedTime(oldNotification))) { - notificationIds.push(value.id); + notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide(); + if (oldNotification.length != 0) { + if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) { + opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden"); + notification.find(".short-text").toggleClass("hidden", opened); + notification.find(".text").toggleClass("hidden", !opened); + } + checkForRemovedSones(oldNotification, notification); + checkForRemovedPosts(oldNotification, notification); + checkForRemovedReplies(oldNotification, notification); + oldNotification.replaceWith(notification.show()); + } else { + $("#sone #notification-area").append(notification); + if (value.id.substring(0, 5) != "local") { + notification.slideDown(); + setActivity(); + } } }); - if (notificationIds.length > 0) { - loadNotifications(notificationIds); - } - /* process new posts. */ - $.each(data.newPosts, function(index, value) { - loadNewPost(value.id, value.sone, value.recipient, value.time); - }); - /* process new replies. */ - $.each(data.newReplies, function(index, value) { - loadNewReply(value.id, value.sone, value.post, value.postSone); - }); - /* do it again in 5 seconds. */ - setTimeout(getStatus, 5000); - } else { - /* data.success was false, wait 30 seconds. */ - setTimeout(getStatus, 30000); - } - }, function() { - statusRequestQueued = false; - ajaxError(); - }); -} - -/** - * Requests multiple notifications from Sone and displays them. - * - * @param notificationIds - * Array of IDs of the notifications to load - */ -function loadNotifications(notificationIds) { - ajaxGet("getNotification.ajax", {"notifications": notificationIds.join(",")}, function(data, textStatus) { - if (!data || !data.success) { - // TODO - show error - return; + setNotificationHash(data.notificationHash); } - $.each(data.notifications, function(index, value) { - oldNotification = getNotification(value.id); - notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide(); - if (oldNotification.length != 0) { - if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) { - opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden"); - notification.find(".short-text").toggleClass("hidden", opened); - notification.find(".text").toggleClass("hidden", !opened); - } - checkForRemovedSones(oldNotification, notification); - checkForRemovedPosts(oldNotification, notification); - checkForRemovedReplies(oldNotification, notification); - oldNotification.replaceWith(notification.show()); - } else { - $("#sone #notification-area").append(notification); - if (value.id.substring(0, 5) != "local") { - notification.slideDown(); - setActivity(); - } - } - }); }); } @@ -1529,6 +1517,7 @@ function markSoneAsKnown(soneElement, skipRequest) { $(soneElement).removeClass("new"); if ((typeof skipRequest == "undefined") || !skipRequest) { ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}); + requestNotifications(); } } } @@ -1541,6 +1530,7 @@ function markPostAsKnown(postElements, skipRequest) { $(postElement).removeClass("new"); if ((typeof skipRequest == "undefined") || !skipRequest) { ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)}); + requestNotifications(); } })(postElement); } @@ -1557,6 +1547,7 @@ function markReplyAsKnown(replyElements, skipRequest) { $(replyElement).removeClass("new"); if ((typeof skipRequest == "undefined") || !skipRequest) { ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)}); + requestNotifications(); } })(replyElement); } @@ -1933,8 +1924,10 @@ $(document).ready(function() { getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) { getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) { getTranslation("WebInterface.DefaultText.Reply", function(text) { - $("#sone .post").each(function() { - ajaxifyPost(this); + getTranslation("WebInterface.Button.Comment", function(text) { + $("#sone .post").each(function() { + ajaxifyPost(this); + }); }); }); });