Add javascript version of “like” button.
[Sone.git] / src / main / resources / static / javascript / sone.js
index 45a678f..f19b0d1 100644 (file)
@@ -232,3 +232,54 @@ function enhanceDeleteReplyButton(buttonId, replyId) {
                });
        });
 }
+
+function getFormPassword() {
+       return $("#sone #formPassword").text();
+}
+
+function getSoneElement(element) {
+       return $(element).parent(".sone");
+}
+
+/**
+ * Returns the ID of the Sone that this element belongs to.
+ *
+ * @param element
+ *            The element to locate the matching Sone ID for
+ * @returns The ID of the Sone, or undefined
+ */
+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 span.like-count").text(data.likes);
+               }
+       });
+}