Add javascript version of “like” button.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 23 Oct 2010 01:06:22 +0000 (03:06 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 23 Oct 2010 01:06:22 +0000 (03:06 +0200)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/ajax/GetPostLikesAjaxPage.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/web/ajax/LikePostAjaxPage.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/web/ajax/UnlikePostAjaxPage.java [new file with mode: 0644]
src/main/resources/static/javascript/sone.js
src/main/resources/templates/include/head.html

index 388e64f..bd640bc 100644 (file)
@@ -42,10 +42,13 @@ import net.pterodactylus.sone.web.ajax.BlockSoneAjaxPage;
 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
+import net.pterodactylus.sone.web.ajax.GetPostLikesAjaxPage;
 import net.pterodactylus.sone.web.ajax.GetSoneStatusPage;
 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
+import net.pterodactylus.sone.web.ajax.LikePostAjaxPage;
 import net.pterodactylus.sone.web.ajax.UnblockSoneAjaxPage;
 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
+import net.pterodactylus.sone.web.ajax.UnlikePostAjaxPage;
 import net.pterodactylus.sone.web.page.PageToadlet;
 import net.pterodactylus.sone.web.page.PageToadletFactory;
 import net.pterodactylus.sone.web.page.StaticPage;
@@ -242,6 +245,9 @@ public class WebInterface extends AbstractService {
                pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new BlockSoneAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new UnblockSoneAjaxPage(this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePostAjaxPage(this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePostAjaxPage(this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostLikesAjaxPage(this)));
 
                ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
                toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostLikesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetPostLikesAjaxPage.java
new file mode 100644 (file)
index 0000000..69e387f
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Sone - GetPostLikesAjaxPage.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.ajax;
+
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.web.WebInterface;
+import net.pterodactylus.util.json.JsonObject;
+
+/**
+ * AJAX page that retrieves the number of “likes” a {@link Post} has.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class GetPostLikesAjaxPage extends JsonPage {
+
+       /**
+        * Creates a new “get post likes” AJAX page.
+        *
+        * @param webInterface
+        *            The Sone web interface
+        */
+       public GetPostLikesAjaxPage(WebInterface webInterface) {
+               super("ajax/getPostLikes.ajax", webInterface);
+       }
+
+       //
+       // JSONPAGE METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected JsonObject createJsonObject(Request request) {
+               String postId = request.getHttpRequest().getParam("post", null);
+               if ((postId == null) || (postId.length() == 0)) {
+                       return new JsonObject().put("success", false).put("error", "invalid-post-id");
+               }
+               Post post = webInterface.core().getPost(postId);
+               return new JsonObject().put("success", true).put("likes", webInterface.core().getLikes(post).size());
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected boolean needsFormPassword() {
+               return false;
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/LikePostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/LikePostAjaxPage.java
new file mode 100644 (file)
index 0000000..d84380c
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Sone - LikePostAjaxPage.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.ajax;
+
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.web.WebInterface;
+import net.pterodactylus.util.json.JsonObject;
+
+/**
+ * AJAX page that lets the user like a {@link Post}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class LikePostAjaxPage extends JsonPage {
+
+       /**
+        * Creates a new “like post” AJAX page.
+        *
+        * @param webInterface
+        */
+       public LikePostAjaxPage(WebInterface webInterface) {
+               super("ajax/likePost.ajax", webInterface);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected JsonObject createJsonObject(Request request) {
+               String postId = request.getHttpRequest().getParam("post", null);
+               if ((postId == null) || (postId.length() == 0)) {
+                       return new JsonObject().put("success", false).put("error", "invalid-post-id");
+               }
+               Sone currentSone = getCurrentSone(request.getToadletContext());
+               if (currentSone == null) {
+                       return new JsonObject().put("success", false).put("error", "auth-required");
+               }
+               currentSone.addLikedPostId(postId);
+               return new JsonObject().put("success", true);
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikePostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikePostAjaxPage.java
new file mode 100644 (file)
index 0000000..293370d
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Sone - UnlikePostAjaxPage.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.ajax;
+
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.web.WebInterface;
+import net.pterodactylus.util.json.JsonObject;
+
+/**
+ * AJAX page that lets the user unlike a {@link Post}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class UnlikePostAjaxPage extends JsonPage {
+
+       /**
+        * Creates a new “unlike post” AJAX page.
+        *
+        * @param webInterface
+        */
+       public UnlikePostAjaxPage(WebInterface webInterface) {
+               super("ajax/unlikePost.ajax", webInterface);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected JsonObject createJsonObject(Request request) {
+               String postId = request.getHttpRequest().getParam("post", null);
+               if ((postId == null) || (postId.length() == 0)) {
+                       return new JsonObject().put("success", false).put("error", "invalid-post-id");
+               }
+               Sone currentSone = getCurrentSone(request.getToadletContext());
+               if (currentSone == null) {
+                       return new JsonObject().put("success", false).put("error", "auth-required");
+               }
+               currentSone.removeLikedPostId(postId);
+               return new JsonObject().put("success", true);
+       }
+
+}
index 63b8361..f19b0d1 100644 (file)
@@ -251,3 +251,35 @@ 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 span.like-count").text(data.likes);
+               }
+       });
+}
index ee4278c..797e7a9 100644 (file)
                });
        </script>
 
+       <script language="javascript">
+               /* convert all “like” buttons to javascript functions. */
+               $(document).ready(function() {
+                       $("#sone .post > .status-line .like").submit(function() {
+                               likePost(getPostId(this));
+                               return false;
+                       });
+                       $("#sone .post > .status-line .unlike").submit(function() {
+                               unlikePost(getPostId(this));
+                               return false;
+                       });
+               });
+       </script>
+
        <div id="main">
 
                <div id="profile" class="<%ifnull currentSone>offline<%else>online<%/if>">