X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=ad23acdb08223e4dd6d8828771e5e0a77d201c72;hb=dd744d086e479089ff12c3e53d9d7e8a07e2f139;hp=8a6411825db453d557a9f49e16d41b3818470304;hpb=faa26c42ce692d20bd5c22c3e1797729d7dea9e1;p=Sone.git diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 8a64118..ad23acd 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -63,12 +63,13 @@ function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, op * @param element * The element to add a âcommentâ link to */ -function addCommentLink(postId, element) { +function addCommentLink(postId, element, insertAfterThisElement) { if ($(element).find(".show-reply-form").length > 0) { return; } commentElement = (function(postId) { var commentElement = $("
true
if the current page is the index page,
+ * false
otherwise
+ */
+function isIndexPage() {
+ return getPageId() == "index";
+}
+
+/**
+ * Returns whether the current page is a âview Soneâ page.
+ *
+ * @returns {Boolean} true
if the current page is a âview Soneâ
+ * page, false
otherwise
+ */
+function isViewSonePage() {
+ return getPageId() == "view-sone";
+}
+
+/**
+ * Returns the ID of the currently shown Sone. This will only return a sensible
+ * value if isViewSonePage() returns true
.
+ *
+ * @returns The ID of the currently shown Sone
+ */
+function getShownSoneId() {
+ return $("#sone .sone-id").text();
+}
+
+/**
+ * Returns whether the current page is a âview postâ page.
+ *
+ * @returns {Boolean} true
if the current page is a âview postâ
+ * page, false
otherwise
+ */
+function isViewPostPage() {
+ return getPageId() == "view-post";
+}
+
+/**
+ * Returns the ID of the currently shown post. This will only return a sensible
+ * value if isViewPostPage() returns true
.
+ *
+ * @returns The ID of the currently shown post
+ */
+function getShownPostId() {
+ return $("#sone .post-id").text();
+}
+
+/**
+ * Returns whether the current page is the âknown Sonesâ page.
+ *
+ * @returns {Boolean} true
if the current page is the âknown
+ * Sonesâ page, false
otherwise
+ */
+function isKnownSonesPage() {
+ return getPageId() == "known-sones";
+}
+
+/**
+ * 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) {
+ if (hasPost(postId)) {
return;
}
- loadedPosts[postId] = true;
- $.getJSON("ajax/getPost.ajax", { "post" : postId }, function(data, textStatus) {
+ $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
if ((data != null) && data.success) {
+ if (hasPost(data.post.id)) {
+ return;
+ }
+ if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
+ return;
+ }
var firstOlderPost = null;
- $("#sone #posts .post").each(function() {
+ $("#sone .post").each(function() {
if (getPostTime(this) < data.post.time) {
firstOlderPost = $(this);
return false;
@@ -608,38 +719,93 @@ function loadNewPost(postId) {
if (firstOlderPost != null) {
newPost.insertBefore(firstOlderPost);
} else {
- $("#sone #posts .post:last").after(newPost);
+ $("#sone #posts").append(newPost);
}
ajaxifyPost(newPost);
newPost.slideDown();
+ setActivity();
}
});
}
function loadNewReply(replyId) {
- if (replyId in loadedReplies) {
+ if (hasReply(replyId)) {
return;
}
- loadedReplies[replyId] = true;
- $.getJSON("ajax/getReply.ajax", { "reply": replyId }, function(data, textStatus) {
+ $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
/* find post. */
- $("#sone #posts .post#" + data.reply.postId).each(function() {
- var firstNewerReply = null;
- $(this).find(".replies .reply").each(function() {
- if (getReplyTime(this) > data.reply.time) {
- firstNewerReply = $(this);
- return false;
+ if ((data != null) && data.success) {
+ if (hasReply(data.reply.id)) {
+ return;
+ }
+ $("#sone .post#" + data.reply.postId).each(function() {
+ var firstNewerReply = null;
+ $(this).find(".replies .reply").each(function() {
+ if (getReplyTime(this) > data.reply.time) {
+ firstNewerReply = $(this);
+ return false;
+ }
+ });
+ newReply = $(data.reply.html).addClass("hidden");
+ if (firstNewerReply != null) {
+ newReply.insertBefore(firstNewerReply);
+ } else {
+ if ($(this).find(".replies .create-reply")) {
+ $(this).find(".replies .create-reply").before(newReply);
+ } else {
+ $(this).find(".replies").append(newReply);
+ }
}
+ ajaxifyReply(newReply);
+ newReply.slideDown();
+ setActivity();
+ return false;
});
- newReply = $(data.reply.html);
- if (firstNewerReply != null) {
- newReply.insertAfter(firstNewerReply);
- } else {
- $(this).find(".replies .reply:last").after(newReply);
- }
- ajaxifyReply(newReply);
- });
+ }
+ });
+}
+
+function markPostAsKnown(postElements) {
+ $(postElements).each(function() {
+ postElement = this;
+ if ($(postElement).hasClass("new")) {
+ (function(postElement) {
+ $.getJSON("markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) {
+ $(postElement).removeClass("new");
+ });
+ })(postElement);
+ }
});
+ markReplyAsKnown($(postElements).find(".reply"));
+}
+
+function markReplyAsKnown(replyElements) {
+ $(replyElements).each(function() {
+ replyElement = this;
+ if ($(replyElement).hasClass("new")) {
+ (function(replyElement) {
+ $.getJSON("markReplyAsKnown.ajax", {"formPassword": getFormPassword(), "reply": getReplyId(replyElement)}, function(data, textStatus) {
+ $(replyElement).removeClass("new");
+ });
+ })(replyElement);
+ }
+ });
+}
+
+function resetActivity() {
+ title = document.title;
+ if (title.indexOf('(') == 0) {
+ document.title = title.substr(title.indexOf(' ') + 1);
+ }
+}
+
+function setActivity() {
+ if (!focus) {
+ title = document.title;
+ if (title.indexOf('(') != 0) {
+ document.title = "(!) " + title;
+ }
+ }
}
/**
@@ -668,14 +834,34 @@ function createNotification(id, text, dismissable) {
// EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
//
+var focus = true;
+
$(document).ready(function() {
/* this initializes the status update input field. */
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.default").hasAttr("disabled")) {
+ return false;
+ }
text = $(this).find(":input:enabled").val();
- $.getJSON("ajax/createPost.ajax", { "formPassword": getFormPassword(), "text": text }, function(data, textStatus) {
+ $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "text": text }, function(data, textStatus) {
+ if ((data != null) && data.success) {
+ loadNewPost(data.postId);
+ }
+ });
+ $(this).find(":input:enabled").val("").blur();
+ return false;
+ });
+ });
+
+ /* ajaxify input field on âview Soneâ page. */
+ getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
+ registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
+ $("#sone #post-message").submit(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);
}
@@ -698,30 +884,32 @@ $(document).ready(function() {
});
/* hides all replies but the latest two. */
- getTranslation("WebInterface.ClickToShow.Replies", function(text) {
- $("#sone .post .replies").each(function() {
- allReplies = $(this).find(".reply");
- if (allReplies.length > 2) {
- newHidden = false;
- for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
- $(allReplies[replyIndex]).addClass("hidden");
- newHidden |= $(allReplies[replyIndex]).hasClass("new");
- }
- clickToShowElement = $("").addClass("click-to-show");
- if (newHidden) {
- clickToShowElement.addClass("new");
+ if (!isViewPostPage()) {
+ getTranslation("WebInterface.ClickToShow.Replies", function(text) {
+ $("#sone .post .replies").each(function() {
+ allReplies = $(this).find(".reply");
+ if (allReplies.length > 2) {
+ newHidden = false;
+ for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
+ $(allReplies[replyIndex]).addClass("hidden");
+ newHidden |= $(allReplies[replyIndex]).hasClass("new");
+ }
+ clickToShowElement = $("").addClass("click-to-show");
+ if (newHidden) {
+ clickToShowElement.addClass("new");
+ }
+ (function(clickToShowElement, allReplies, text) {
+ clickToShowElement.text(text);
+ clickToShowElement.click(function() {
+ allReplies.removeClass("hidden");
+ clickToShowElement.addClass("hidden");
+ });
+ })(clickToShowElement, allReplies, text);
+ $(allReplies[0]).before(clickToShowElement);
}
- (function(clickToShowElement, allReplies, text) {
- clickToShowElement.text(text);
- clickToShowElement.click(function() {
- allReplies.removeClass("hidden");
- clickToShowElement.addClass("hidden");
- });
- })(clickToShowElement, allReplies, text);
- $(allReplies[0]).before(clickToShowElement);
- }
+ });
});
- });
+ }
/*
* convert all âfollowâ, âunfollowâ, âlockâ, and âunlockâ links to something
@@ -729,7 +917,7 @@ $(document).ready(function() {
*/
$("#sone .follow").submit(function() {
var followElement = this;
- $.getJSON("ajax/followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
+ $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
$(followElement).addClass("hidden");
$(followElement).parent().find(".unfollow").removeClass("hidden");
});
@@ -737,7 +925,7 @@ $(document).ready(function() {
});
$("#sone .unfollow").submit(function() {
var unfollowElement = this;
- $.getJSON("ajax/unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
+ $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
$(unfollowElement).addClass("hidden");
$(unfollowElement).parent().find(".follow").removeClass("hidden");
});
@@ -745,7 +933,7 @@ $(document).ready(function() {
});
$("#sone .lock").submit(function() {
var lockElement = this;
- $.getJSON("ajax/lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
+ $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
$(lockElement).addClass("hidden");
$(lockElement).parent().find(".unlock").removeClass("hidden");
});
@@ -753,7 +941,7 @@ $(document).ready(function() {
});
$("#sone .unlock").submit(function() {
var unlockElement = this;
- $.getJSON("ajax/unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
+ $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
$(unlockElement).addClass("hidden");
$(unlockElement).parent().find(".lock").removeClass("hidden");
});
@@ -767,4 +955,13 @@ $(document).ready(function() {
/* activate status polling. */
setTimeout(getStatus, 5000);
+
+ /* reset activity counter when the page has focus. */
+ $(window).focus(function() {
+ focus = true;
+ resetActivity();
+ }).blur(function() {
+ focus = false;
+ })
+
});