X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=6afb2322d6e32c655aaf290f219085fb6cf4e7d2;hp=29c5420607c08ae48567723b37b4e64c9e564379;hb=8d7400f3774c9f7881f7d3cbc971abe4b97d5121;hpb=74af78bee7dda61c9244b71e170c753bc15f80e4 diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 29c5420..6afb232 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -70,7 +70,7 @@ function addCommentLink(postId, author, element, insertAfterThisElement) { commentElement = (function(postId, author) { separator = $(" · ").addClass("separator"); var commentElement = $("
Comment
").addClass("show-reply-form").click(function() { - replyElement = $("#sone .post#" + postId + " .create-reply"); + replyElement = $("#sone .post#post-" + postId + " .create-reply"); replyElement.removeClass("hidden"); replyElement.removeClass("light"); (function(replyElement) { @@ -214,7 +214,7 @@ function enhanceDeletePostButton(button, postId, text) { return; } if (data.success) { - $("#sone .post#" + postId).slideUp(); + $("#sone .post#post-" + postId).slideUp(); } else if (data.error == "invalid-post-id") { /* pretend the post is already gone. */ getPost(postId).slideUp(); @@ -246,7 +246,7 @@ function enhanceDeleteReplyButton(button, replyId, text) { return; } if (data.success) { - $("#sone .reply#" + replyId).slideUp(); + $("#sone .reply#reply-" + replyId).slideUp(); } else if (data.error == "invalid-reply-id") { /* pretend the reply is already gone. */ getReply(replyId).slideUp(); @@ -291,7 +291,7 @@ function getSoneElement(element) { * @return The Sone ID */ function getMenuSone(element) { - return $(element).closest(".sone-menu").find(".sone-id").text(); + return $(element).closest(".sone-menu").find(".sone-menu-id").text(); } /** @@ -332,7 +332,7 @@ function getSoneId(element) { * @returns The element of the post */ function getPost(postId) { - return $("#sone .post#" + postId); + return $("#sone .post#post-" + postId); } function getPostElement(element) { @@ -340,7 +340,7 @@ function getPostElement(element) { } function getPostId(element) { - return getPostElement(element).attr("id"); + return getPostElement(element).attr("id").substr(5); } function getPostTime(element) { @@ -366,7 +366,7 @@ function getPostAuthor(element) { * @returns The element of the reply */ function getReply(replyId) { - return $("#sone .reply#" + replyId); + return $("#sone .reply#reply-" + replyId); } function getReplyElement(element) { @@ -374,7 +374,7 @@ function getReplyElement(element) { } function getReplyId(element) { - return getReplyElement(element).attr("id"); + return getReplyElement(element).attr("id").substr(6); } function getReplyTime(element) { @@ -441,8 +441,8 @@ function likePost(postId) { if ((data == null) || !data.success) { return; } - $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden"); - $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden"); + $("#sone .post#post-" + postId + " > .inner-part > .status-line .like").addClass("hidden"); + $("#sone .post#post-" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden"); updatePostLikes(postId); }, function(xmlHttpRequest, textStatus, error) { /* ignore error. */ @@ -454,8 +454,8 @@ function unlikePost(postId) { if ((data == null) || !data.success) { return; } - $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden"); - $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden"); + $("#sone .post#post-" + postId + " > .inner-part > .status-line .unlike").addClass("hidden"); + $("#sone .post#post-" + postId + " > .inner-part > .status-line .like").removeClass("hidden"); updatePostLikes(postId); }, function(xmlHttpRequest, textStatus, error) { /* ignore error. */ @@ -465,9 +465,9 @@ function unlikePost(postId) { function updatePostLikes(postId) { ajaxGet("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) { if ((data != null) && data.success) { - $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0); - $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes); - $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones)); + $("#sone .post#post-" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0); + $("#sone .post#post-" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes); + $("#sone .post#post-" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones)); } }, function(xmlHttpRequest, textStatus, error) { /* ignore error. */ @@ -479,8 +479,8 @@ function likeReply(replyId) { if ((data == null) || !data.success) { return; } - $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden"); - $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden"); + $("#sone .reply#reply-" + replyId + " .status-line .like").addClass("hidden"); + $("#sone .reply#reply-" + replyId + " .status-line .unlike").removeClass("hidden"); updateReplyLikes(replyId); }, function(xmlHttpRequest, textStatus, error) { /* ignore error. */ @@ -492,8 +492,8 @@ function unlikeReply(replyId) { if ((data == null) || !data.success) { return; } - $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden"); - $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden"); + $("#sone .reply#reply-" + replyId + " .status-line .unlike").addClass("hidden"); + $("#sone .reply#reply-" + replyId + " .status-line .like").removeClass("hidden"); updateReplyLikes(replyId); }, function(xmlHttpRequest, textStatus, error) { /* ignore error. */ @@ -603,9 +603,9 @@ function unbookmarkPost(postId) { function updateReplyLikes(replyId) { ajaxGet("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) { if ((data != null) && data.success) { - $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0); - $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes); - $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones)); + $("#sone .reply#reply-" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0); + $("#sone .reply#reply-" + replyId + " .status-line .likes span.like-count").text(data.likes); + $("#sone .reply#reply-" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones)); } }, function(xmlHttpRequest, textStatus, error) { /* ignore error. */ @@ -713,10 +713,10 @@ function ajaxifyPost(postElement) { if (success) { $(inputField).val(""); loadNewReply(replyId, soneId, postId); - $("#sone .post#" + postId + " .create-reply").addClass("hidden"); - $("#sone .post#" + postId + " .create-reply .sender").hide(); - $("#sone .post#" + postId + " .create-reply .select-sender").show(); - $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId()); + $("#sone .post#post-" + postId + " .create-reply").addClass("hidden"); + $("#sone .post#post-" + postId + " .create-reply .sender").hide(); + $("#sone .post#post-" + postId + " .create-reply .select-sender").show(); + $("#sone .post#post-" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId()); } else { alert(error); } @@ -858,14 +858,23 @@ function ajaxifyPost(postElement) { /* show Sone menu when hovering over the avatar. */ $(postElement).find(".post-avatar").mouseover(function() { - $(".sone-menu:visible").fadeOut(); - $(".sone-post-menu", postElement).mouseleave(function() { - $(this).fadeOut(); - }).fadeIn(); - return false; + if (typeof currentSoneMenuTimeoutHandler != undefined) { + clearTimeout(currentSoneMenuTimeoutHandler); + } + currentSoneMenuId = getPostId(this); + currentSoneMenuTimeoutHandler = setTimeout(function() { + $(".sone-menu:visible").fadeOut(); + $(".sone-post-menu", postElement).mouseleave(function() { + $(this).fadeOut(); + }).fadeIn(); + }, 1000); + }).mouseleave(function() { + if (currentSoneMenuId = getPostId(this)) { + clearTimeout(currentSoneMenuTimeoutHandler); + } }); (function(postElement) { - var soneId = $(".sone-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() { @@ -988,14 +997,23 @@ function ajaxifyReply(replyElement) { /* show Sone menu when hovering over the avatar. */ $(replyElement).find(".reply-avatar").mouseover(function() { - $(".sone-menu:visible").fadeOut(); - $(".sone-reply-menu", replyElement).mouseleave(function() { - $(this).fadeOut(); - }).fadeIn(); - return false; + if (typeof currentSoneMenuTimeoutHandler != undefined) { + clearTimeout(currentSoneMenuTimeoutHandler); + } + currentSoneMenuId = getPostId(this) + "-" + getReplyId(this); + currentSoneMenuTimeoutHandler = setTimeout(function() { + $(".sone-menu:visible").fadeOut(); + $(".sone-reply-menu", replyElement).mouseleave(function() { + $(this).fadeOut(); + }).fadeIn(); + }, 1000); + }).mouseleave(function() { + if (currentSoneMenuId = getPostId(this) + "-" + getReplyId(this)) { + clearTimeout(currentSoneMenuTimeoutHandler); + } }); (function(replyElement) { - var soneId = $(".sone-id", replyElement).text(); + var soneId = $(".sone-menu-id", replyElement).text(); $(".sone-menu .follow", replyElement).click(function() { var followElement = this; ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() { @@ -1070,6 +1088,24 @@ 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. + */ +function getNotificationHash() { + return $("#sone #notification-area #notification-hash").text(); +} + +/** + * Sets the notification hash. + * + * @param notificationHash + * The new notification hash + */ +function setNotificationHash(notificationHash) { + $("#sone #notification-area #notification-hash").text(notificationHash); +} + +/** * Retrieves element IDs from notification elements. * * @param notification @@ -1141,7 +1177,7 @@ function checkForRemovedPosts(oldNotification, newNotification) { * The new notification element */ function checkForRemovedReplies(oldNotification, newNotification) { - if (getNotificationId(oldNotification) != "new-replies-notification") { + if (getNotificationId(oldNotification) != "new-reply-notification") { return; } oldIds = getElementIds(oldNotification, ".reply-id"); @@ -1164,6 +1200,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"); @@ -1175,17 +1238,17 @@ function getStatus() { } }); if (!foundNotification) { - if (notificationId == "new-sone-notification") { + if (notificationId == "new-sone-notification" && (data.options["ShowNotification/NewSones"] == true)) { $(".new-sone-id", this).each(function(index, element) { soneId = $(this).text(); markSoneAsKnown(getSone(soneId), true); }); - } else if (notificationId == "new-post-notification") { + } else if (notificationId == "new-post-notification" && (data.options["ShowNotification/NewPosts"] == true)) { $(".post-id", this).each(function(index, element) { postId = $(this).text(); markPostAsKnown(getPost(postId), true); }); - } else if (notificationId == "new-replies-notification") { + } else if (notificationId == "new-reply-notification" && (data.options["ShowNotification/NewReplies"] == true)) { $(".reply-id", this).each(function(index, element) { replyId = $(this).text(); markReplyAsKnown(getReply(replyId), true); @@ -1201,69 +1264,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); + setNotificationHash(data.notificationHash); } - }, 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; - } - $.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(); - } - } - }); }); } @@ -1329,7 +1352,7 @@ function isViewSonePage() { * @returns The ID of the currently shown Sone */ function getShownSoneId() { - return $("#sone .sone-id").text(); + return $("#sone .sone-id").first().text(); } /** @@ -1371,7 +1394,7 @@ function isKnownSonesPage() { * exists on the page, false otherwise */ function hasPost(postId) { - return $(".post#" + postId).length > 0; + return $(".post#post-" + postId).length > 0; } /** @@ -1383,7 +1406,7 @@ function hasPost(postId) { * exists on the page, false otherwise */ function hasReply(replyId) { - return $("#sone .reply#" + replyId).length > 0; + return $("#sone .reply#reply-" + replyId).length > 0; } function loadNewPost(postId, soneId, recipientId, time) { @@ -1443,7 +1466,7 @@ function loadNewReply(replyId, soneId, postId, postSoneId) { if (hasReply(data.reply.id)) { return; } - $("#sone .post#" + data.reply.postId).each(function() { + $("#sone .post#post-" + data.reply.postId).each(function() { var firstNewerReply = null; $(this).find(".replies .reply").each(function() { if (getReplyTime(this) > data.reply.time) { @@ -1493,6 +1516,7 @@ function markSoneAsKnown(soneElement, skipRequest) { $(soneElement).removeClass("new"); if ((typeof skipRequest == "undefined") || !skipRequest) { ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}); + requestNotifications(); } } } @@ -1505,6 +1529,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); } @@ -1521,6 +1546,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); } @@ -1828,6 +1854,12 @@ var online = true; var initiallyLoggedIn = $("#sone #loggedIn").text() == "true"; var notLoggedIn = !initiallyLoggedIn; +/** ID of the next-to-show Sone context menu. */ +var currentSoneMenuId; + +/** Timeout handler for the next-to-show Sone context menu. */ +var currentSoneMenuTimeoutHandler; + $(document).ready(function() { /* this initializes the status update input field. */