From: David ‘Bombe’ Roden Date: Sat, 23 Oct 2010 13:16:14 +0000 (+0200) Subject: Enhance javascript “like” functionality to include replies. X-Git-Tag: 0.1-RC1~58 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=16a69fdf1e4d4b7852ca8e2abdfd09470207ec6b;p=Sone.git Enhance javascript “like” functionality to include replies. --- diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 78a03b5..f9d6f64 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -44,13 +44,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.GetLikesAjaxPage; 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.LikeAjaxPage; 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.ajax.UnlikeAjaxPage; import net.pterodactylus.sone.web.page.PageToadlet; import net.pterodactylus.sone.web.page.PageToadletFactory; import net.pterodactylus.sone.web.page.StaticPage; @@ -250,9 +250,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))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(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/GetLikesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java new file mode 100644 index 0000000..621abe8 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java @@ -0,0 +1,74 @@ +/* + * Sone - GetLikesAjaxPage.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 . + */ + +package net.pterodactylus.sone.web.ajax; + +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.Reply; +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 David ‘Bombe’ Roden + */ +public class GetLikesAjaxPage extends JsonPage { + + /** + * Creates a new “get post likes” AJAX page. + * + * @param webInterface + * The Sone web interface + */ + public GetLikesAjaxPage(WebInterface webInterface) { + super("ajax/getLikes.ajax", webInterface); + } + + // + // JSONPAGE METHODS + // + + /** + * {@inheritDoc} + */ + @Override + protected JsonObject createJsonObject(Request request) { + String type = request.getHttpRequest().getParam("type", null); + String id = request.getHttpRequest().getParam(type, null); + if ((id == null) || (id.length() == 0)) { + return new JsonObject().put("success", false).put("error", "invalid-" + type + "-id"); + } + if ("post".equals(type)) { + Post post = webInterface.core().getPost(id); + return new JsonObject().put("success", true).put("likes", webInterface.core().getLikes(post).size()); + } else if ("reply".equals(type)) { + Reply reply = webInterface.core().getReply(id); + return new JsonObject().put("success", true).put("likes", webInterface.core().getLikes(reply).size()); + } + return new JsonObject().put("success", false).put("error", "invalid-type"); + } + + /** + * {@inheritDoc} + */ + @Override + protected boolean needsFormPassword() { + return false; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostLikesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetPostLikesAjaxPage.java deleted file mode 100644 index 69e387f..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostLikesAjaxPage.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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 . - */ - -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 David ‘Bombe’ Roden - */ -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/LikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java new file mode 100644 index 0000000..fce9bf9 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java @@ -0,0 +1,65 @@ +/* + * Sone - LikeAjaxPage.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 . + */ + +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 David ‘Bombe’ Roden + */ +public class LikeAjaxPage extends JsonPage { + + /** + * Creates a new “like post” AJAX page. + * + * @param webInterface + */ + public LikeAjaxPage(WebInterface webInterface) { + super("ajax/like.ajax", webInterface); + } + + /** + * {@inheritDoc} + */ + @Override + protected JsonObject createJsonObject(Request request) { + String type = request.getHttpRequest().getParam("type", null); + String id = request.getHttpRequest().getParam(type, null); + if ((id == null) || (id.length() == 0)) { + return new JsonObject().put("success", false).put("error", "invalid-" + type + "-id"); + } + Sone currentSone = getCurrentSone(request.getToadletContext()); + if (currentSone == null) { + return new JsonObject().put("success", false).put("error", "auth-required"); + } + if ("post".equals(type)) { + currentSone.addLikedPostId(id); + } else if ("reply".equals(type)) { + currentSone.addLikedReplyId(id); + } else { + return new JsonObject().put("success", false).put("error", "invalid-type"); + } + return new JsonObject().put("success", true); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/LikePostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/LikePostAjaxPage.java deleted file mode 100644 index d84380c..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/LikePostAjaxPage.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 . - */ - -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 David ‘Bombe’ Roden - */ -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/UnlikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java new file mode 100644 index 0000000..a6a325a --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java @@ -0,0 +1,65 @@ +/* + * Sone - UnlikeAjaxPage.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 . + */ + +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 David ‘Bombe’ Roden + */ +public class UnlikeAjaxPage extends JsonPage { + + /** + * Creates a new “unlike post” AJAX page. + * + * @param webInterface + */ + public UnlikeAjaxPage(WebInterface webInterface) { + super("ajax/unlike.ajax", webInterface); + } + + /** + * {@inheritDoc} + */ + @Override + protected JsonObject createJsonObject(Request request) { + String type = request.getHttpRequest().getParam("type", null); + String id = request.getHttpRequest().getParam(type, null); + if ((id == null) || (id.length() == 0)) { + return new JsonObject().put("success", false).put("error", "invalid-" + type + "-id"); + } + Sone currentSone = getCurrentSone(request.getToadletContext()); + if (currentSone == null) { + return new JsonObject().put("success", false).put("error", "auth-required"); + } + if ("post".equals(type)) { + currentSone.removeLikedPostId(id); + } else if ("reply".equals(type)) { + currentSone.removeLikedReplyId(id); + } else { + return new JsonObject().put("success", false).put("error", "invalid-type"); + } + 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 deleted file mode 100644 index 293370d..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikePostAjaxPage.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 . - */ - -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 David ‘Bombe’ Roden - */ -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); - } - -} diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index be7deaa..dedb510 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -267,8 +267,16 @@ function getPostId(element) { return getPostElement(element).attr("id"); } +function getReplyElement(element) { + return $(element).parents(".reply"); +} + +function getReplyId(element) { + return getReplyElement(element).attr("id"); +} + function likePost(postId) { - $.getJSON("ajax/likePost.ajax", { "post" : postId, "formPassword": getFormPassword() }, function() { + $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function() { $("#sone .post#" + postId + " > .status-line .like").addClass("hidden"); $("#sone .post#" + postId + " > .status-line .unlike").removeClass("hidden"); updatePostLikes(postId); @@ -276,7 +284,7 @@ function likePost(postId) { } function unlikePost(postId) { - $.getJSON("ajax/unlikePost.ajax", { "post" : postId, "formPassword": getFormPassword() }, function() { + $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function() { $("#sone .post#" + postId + " > .status-line .unlike").addClass("hidden"); $("#sone .post#" + postId + " > .status-line .like").removeClass("hidden"); updatePostLikes(postId); @@ -284,10 +292,35 @@ function unlikePost(postId) { } function updatePostLikes(postId) { - $.getJSON("ajax/getPostLikes.ajax", { "post": postId }, function(data, textStatus) { + $.getJSON("ajax/getLikes.ajax", { "type": "post", "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); } }); } + +function likeReply(replyId) { + $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function() { + $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden"); + $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden"); + updateReplyLikes(replyId); + }); +} + +function unlikeReply(replyId) { + $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function() { + $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden"); + $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden"); + updateReplyLikes(replyId); + }); +} + +function updateReplyLikes(replyId) { + $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) { + if (data.success) { + $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0) + $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes); + } + }); +} diff --git a/src/main/resources/templates/include/head.html b/src/main/resources/templates/include/head.html index 233a9cd..a43131a 100644 --- a/src/main/resources/templates/include/head.html +++ b/src/main/resources/templates/include/head.html @@ -98,6 +98,14 @@ unlikePost(getPostId(this)); return false; }); + $("#sone .post .reply .status-line .like").submit(function() { + likeReply(getReplyId(this)); + return false; + }); + $("#sone .post .reply .status-line .unlike").submit(function() { + unlikeReply(getReplyId(this)); + return false; + }); });