From d8a5000f215fa4ac346cea76c21a716c1d69b978 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 23 Sep 2011 21:06:29 +0200 Subject: [PATCH] Allow user to edit image data inline. --- .../net/pterodactylus/sone/web/WebInterface.java | 2 + .../sone/web/ajax/EditImageAjaxPage.java | 76 ++++++++++++++++++++++ src/main/resources/templates/imageBrowser.html | 66 +++++++++++++++++-- 3 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 4572497..9dd9954 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -75,6 +75,7 @@ import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage; import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage; import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage; import net.pterodactylus.sone.web.ajax.DistrustAjaxPage; +import net.pterodactylus.sone.web.ajax.EditImageAjaxPage; import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage; import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage; import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage; @@ -664,6 +665,7 @@ public class WebInterface implements CoreListener { pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this))); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java new file mode 100644 index 0000000..17d171b --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java @@ -0,0 +1,76 @@ +/* + * Sone - EditImageAjaxPage.java - Copyright © 2011 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.Image; +import net.pterodactylus.sone.web.WebInterface; +import net.pterodactylus.sone.web.page.FreenetRequest; +import net.pterodactylus.util.json.JsonObject; + +/** + * Page that stores a user’s image modifications. + * + * @author David ‘Bombe’ Roden + */ +public class EditImageAjaxPage extends JsonPage { + + /** + * Creates a new edit image AJAX page. + * + * @param webInterface + * The Sone web interface + */ + public EditImageAjaxPage(WebInterface webInterface) { + super("editImage.ajax", webInterface); + } + + // + // JSONPAGE METHODS + // + + /** + * {@inheritDoc} + */ + @Override + protected JsonObject createJsonObject(FreenetRequest request) { + String imageId = request.getHttpRequest().getParam("image"); + Image image = webInterface.getCore().getImage(imageId, false); + if (image == null) { + return createErrorJsonObject("invalid-image-id"); + } + if (!webInterface.getCore().isLocalSone(image.getSone())) { + return createErrorJsonObject("not-authorized"); + } + if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) { + Image swappedImage = image.getAlbum().moveImageUp(image); + webInterface.getCore().touchConfiguration(); + return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId()); + } + if ("true".equals(request.getHttpRequest().getParam("moveRight"))) { + Image swappedImage = image.getAlbum().moveImageDown(image); + webInterface.getCore().touchConfiguration(); + return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId()); + } + String title = request.getHttpRequest().getParam("title").trim(); + String description = request.getHttpRequest().getParam("description").trim(); + image.setTitle(title).setDescription(description); + webInterface.getCore().touchConfiguration(); + return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()); + } + +} diff --git a/src/main/resources/templates/imageBrowser.html b/src/main/resources/templates/imageBrowser.html index 74dac80..987b97a 100644 --- a/src/main/resources/templates/imageBrowser.html +++ b/src/main/resources/templates/imageBrowser.html @@ -75,6 +75,43 @@ editingImageId = null; } + /** + * Returns the image element with the given ID. + * + * @param imageId The ID of the image + * @return The image element + */ + function getImage(imageId) { + return $("#sone .image .image-id:contains('" + imageId + "')").closest(".image"); + } + + /** + * Swaps two images. + * + * @param sourceId The ID of the source image + * @param destinationId The ID of the destionation image + */ + function swapImage(sourceId, destinationId) { + sourceElement = getImage(sourceId); + destinationElement = getImage(destinationId); + sourceParent = sourceElement.closest(".image-row"); + sourcePrevSibling = sourceElement.prev(); + sourceElement.detach(); + destinationElement.before(sourceElement); + if (sourcePrevSibling.get(0) != destinationElement.get(0)) { + destinationElement.detach(); + (sourcePrevSibling.size() > 0) ? sourcePrevSibling.after(destinationElement) : sourceParent.prepend(destinationElement); + } + if ($("button[name='moveLeft']", sourceElement).hasClass("hidden") != $("button[name='moveLeft']", destinationElement).hasClass("hidden")) { + $("button[name='moveLeft']", sourceElement).toggleClass("hidden"); + $("button[name='moveLeft']", destinationElement).toggleClass("hidden"); + } + if ($("button[name='moveRight']", sourceElement).hasClass("hidden") != $("button[name='moveRight']", destinationElement).hasClass("hidden")) { + $("button[name='moveRight']", sourceElement).toggleClass("hidden"); + $("button[name='moveRight']", destinationElement).toggleClass("hidden"); + } + } + $(function() { getTranslation("WebInterface.DefaultText.UploadImage.Title", function(text) { $("#upload-image :input[name='title']").each(function() { @@ -121,8 +158,29 @@ $(".show-data", element).click(function() { editImage(imageId); }); + $("button[name='moveLeft'], button[name='moveRight']", element).click(function() { + ajaxGet("editImage.ajax", { "formPassword": getFormPassword(), "image": imageId, "moveLeft": this.name == "moveLeft", "moveRight": this.name == "moveRight" }, function(data) { + if (data && data.success) { + swapImage(data.sourceImageId, data.destinationImageId); + } + }); + return false; + }); + $("button[name='submit']", element).click(function() { + title = $(":input[name='title']:enabled", this.form).val(); + description = $(":input[name='description']:enabled", this.form).val(); + ajaxGet("editImage.ajax", { "formPassword": getFormPassword(), "image": imageId, "title": title, "description": description }, function(data) { + if (data && data.success) { + getImage(data.imageId).find(".image-title").text(data.title); + getImage(data.imageId).find(".image-description").text(data.description); + getImage(data.imageId).find(":input[name='title']").attr("defaultValue", title); + getImage(data.imageId).find(":input[name='description']").attr("defaultValue", description); + cancelEditing(); + } + }); + return false; + }); })(this, imageId); - }); }); }); @@ -235,9 +293,9 @@
- <%notfirst><%/notfirst> - - <%notlast><%/notlast> + + +
-- 2.7.4