Add function to detect if the current page is a “view post” page.
[Sone.git] / src / main / resources / static / javascript / sone.js
index e07c51e..fa2b1cf 100644 (file)
@@ -497,6 +497,11 @@ function ajaxifyPost(postElement) {
                });
        });
 
+       /* mark everything as known on click. */
+       $(postElement).click(function() {
+               markPostAsKnown(this);
+       });
+
        /* hide reply input field. */
        $(postElement).find(".create-reply").addClass("hidden");
 }
@@ -526,6 +531,11 @@ function ajaxifyReply(replyElement) {
                });
        })(replyElement);
        addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
+
+       /* mark post and all replies as known on click. */
+       $(replyElement).click(function() {
+               markPostAsKnown(getPostElement(replyElement));
+       });
 }
 
 /**
@@ -591,6 +601,65 @@ function getStatus() {
        })
 }
 
+/**
+ * Returns the content of the page-id attribute.
+ *
+ * @returns The page ID
+ */
+function getPageId() {
+       return $("#sone .page-id").text();
+}
+
+/**
+ * Returns whether the current page is the index page.
+ *
+ * @returns {Boolean} <code>true</code> if the current page is the index page,
+ *          <code>false</code> otherwise
+ */
+function isIndexPage() {
+       return getPageId() == "index";
+}
+
+/**
+ * Returns whether the current page is a “view Sone” page.
+ *
+ * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
+ *          page, <code>false</code> 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 <code>true</code>.
+ *
+ * @returns The ID of the currently shown Sone
+ */
+function getSoneId() {
+       return $("#sone .sone-id").text();
+}
+
+/**
+ * Returns whether the current page is a “view post” page.
+ *
+ * @returns {Boolean} <code>true</code> if the current page is a “view post”
+ *          page, <code>false</code> 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 <code>true</code>.
+ *
+ * @returns The ID of the currently shown post
+ */
+function getPostId() {
+       return $("#sone .post-id").text();
+}
+
 var loadedPosts = {};
 var loadedReplies = {};
 
@@ -601,6 +670,9 @@ function loadNewPost(postId) {
        loadedPosts[postId] = true;
        $.getJSON("ajax/getPost.ajax", { "post" : postId }, function(data, textStatus) {
                if ((data != null) && data.success) {
+                       if (!isIndexPage() && !(isViewSonePage() && (getSoneId() == data.post.sone))) {
+                               return;
+                       }
                        var firstOlderPost = null;
                        $("#sone .post").each(function() {
                                if (getPostTime(this) < data.post.time) {
@@ -612,7 +684,7 @@ function loadNewPost(postId) {
                        if (firstOlderPost != null) {
                                newPost.insertBefore(firstOlderPost);
                        } else {
-                               $("#sone #posts").append(newPost);
+                               $("#sone .post:last").append(newPost);
                        }
                        ajaxifyPost(newPost);
                        newPost.slideDown();