Hide 0 likes.
[Sone.git] / src / main / resources / static / javascript / sone.js
index 63b8361..be7deaa 100644 (file)
@@ -1,5 +1,9 @@
 /* Sone JavaScript functions. */
 
+function isOnline() {
+       return $("#sone").hasClass("online");
+}
+
 function registerInputTextareaSwap(inputSelector, defaultText, inputFieldName, optional) {
        $(inputSelector).each(function() {
                textarea = $("<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
@@ -42,6 +46,9 @@ function registerInputTextareaSwap(inputSelector, defaultText, inputFieldName, o
 
 /* hide all the “create reply” forms until a link is clicked. */
 function addCommentLinks() {
+       if (!isOnline()) {
+               return;
+       }
        $("#sone .post").each(function() {
                postId = $(this).attr("id");
                commentElement = (function(postId) {
@@ -238,7 +245,7 @@ function getFormPassword() {
 }
 
 function getSoneElement(element) {
-       return $(element).parent(".sone");
+       return $(element).parents(".sone");
 }
 
 /**
@@ -251,3 +258,36 @@ function getSoneElement(element) {
 function getSoneId(element) {
        return getSoneElement(element).find(".id").text();
 }
+
+function getPostElement(element) {
+       return $(element).parents(".post");
+}
+
+function getPostId(element) {
+       return getPostElement(element).attr("id");
+}
+
+function likePost(postId) {
+       $.getJSON("ajax/likePost.ajax", { "post" : postId, "formPassword": getFormPassword() }, function() {
+               $("#sone .post#" + postId + " > .status-line .like").addClass("hidden");
+               $("#sone .post#" + postId + " > .status-line .unlike").removeClass("hidden");
+               updatePostLikes(postId);
+       });
+}
+
+function unlikePost(postId) {
+       $.getJSON("ajax/unlikePost.ajax", { "post" : postId, "formPassword": getFormPassword() }, function() {
+               $("#sone .post#" + postId + " > .status-line .unlike").addClass("hidden");
+               $("#sone .post#" + postId + " > .status-line .like").removeClass("hidden");
+               updatePostLikes(postId);
+       });
+}
+
+function updatePostLikes(postId) {
+       $.getJSON("ajax/getPostLikes.ajax", { "post": postId }, function(data, textStatus) {
+               if (data.success) {
+                       $("#sone .post#" + postId + " > .status-line .likes").toggleClass("hidden", data.likes == 0)
+                       $("#sone .post#" + postId + " > .status-line .likes span.like-count").text(data.likes);
+               }
+       });
+}