From ca65e4c3c3eec57227b50db1f10df0d2901fe0b0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 13 Nov 2010 00:43:58 +0100 Subject: [PATCH] Return rendered HTML for the reply element. --- .../net/pterodactylus/sone/web/WebInterface.java | 3 ++- .../sone/web/ajax/GetReplyAjaxPage.java | 25 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 1b4654b..2f10567 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -216,6 +216,7 @@ public class WebInterface { Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html")); Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html")); Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html")); + Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html")); PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/"); pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index")); @@ -245,7 +246,7 @@ public class WebInterface { pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new GetSoneStatusPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this))); - pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this))); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java index d00c7dd..292f3bf 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.web.ajax; +import java.io.StringWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -24,7 +25,10 @@ import java.util.Date; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.template.SoneAccessor; import net.pterodactylus.sone.web.WebInterface; +import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.json.JsonObject; +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateException; /** * This AJAX page returns the details of a reply. @@ -36,13 +40,20 @@ public class GetReplyAjaxPage extends JsonPage { /** Date formatter. */ private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss"); + /** The template to render. */ + private final Template replyTemplate; + /** * Creates a new “get reply” page. * * @param webInterface + * The Sone web interface + * @param replyTemplate + * The template to render */ - public GetReplyAjaxPage(WebInterface webInterface) { + public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) { super("ajax/getReply.ajax", webInterface); + this.replyTemplate = replyTemplate; } // @@ -59,8 +70,18 @@ public class GetReplyAjaxPage extends JsonPage { if ((reply == null) || (reply.getSone() == null)) { return createErrorJsonObject("invalid-reply-id"); } + replyTemplate.set("reply", reply); + replyTemplate.set("currentSone", getCurrentSone(request.getToadletContext())); + StringWriter templateWriter = new StringWriter(); + try { + replyTemplate.render(templateWriter); + } catch (TemplateException te1) { + /* TODO - shouldn’t happen. */ + } finally { + Closer.close(templateWriter); + } synchronized (dateFormat) { - return new JsonObject().put("success", true).put("sone-id", reply.getSone().getId()).put("sone-name", SoneAccessor.getNiceName(reply.getSone())).put("time", reply.getTime()).put("display-time", dateFormat.format(new Date(reply.getTime()))).put("text", reply.getText()); + return new JsonObject().put("success", true).put("soneId", reply.getSone().getId()).put("soneName", SoneAccessor.getNiceName(reply.getSone())).put("time", reply.getTime()).put("displayTime", dateFormat.format(new Date(reply.getTime()))).put("text", reply.getText()).put("html", templateWriter.toString()); } } -- 2.7.4