From 2cc71500fdfee9face7bd52889be29e5a21489ce Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sun, 3 Jul 2022 16:42:16 +0200 Subject: [PATCH] add style, improve example (mobile support through live editing) --- index.html | 47 ++++++++++--------- .../tudbut/tryumph/events/EventListener.java | 5 +- src/de/tudbut/tryumph/example/Main.java | 25 +++++++++- .../tudbut/tryumph/server/BrowserContext.java | 6 +-- src/de/tudbut/tryumph/server/Response.java | 4 +- style.css | 9 ++++ 6 files changed, 65 insertions(+), 31 deletions(-) create mode 100644 style.css diff --git a/index.html b/index.html index 3859720..c0e16e5 100644 --- a/index.html +++ b/index.html @@ -3,34 +3,37 @@ Tryumph example page -

This is the Tryumph example page

- - Here, you will be able to test out some functions of Tryumph. +
+

This is the Tryumph example page

+ Here, you will be able to test out some functions of Tryumph. +


-

Get:

-
- = - - -
+
+

Get:

+
+ = + + +
-

Server-side set:

-
- = - - -
+

Server-side set:

+
+ = + + +
-

Client-side set:

-
- = - - - -
+

Client-side set:

+
+ = + + + +
+
diff --git a/src/de/tudbut/tryumph/events/EventListener.java b/src/de/tudbut/tryumph/events/EventListener.java index fa8f963..ffbbe68 100644 --- a/src/de/tudbut/tryumph/events/EventListener.java +++ b/src/de/tudbut/tryumph/events/EventListener.java @@ -4,15 +4,14 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import de.tudbut.async.Callback; -import de.tudbut.tryumph.config.IRequestCatcher; import de.tudbut.tryumph.server.Request; import de.tudbut.tryumph.server.Response; public class EventListener { - private IRequestCatcher catcher; + private Object catcher; - public EventListener(IRequestCatcher catcher) { + public EventListener(Object catcher) { this.catcher = catcher; } diff --git a/src/de/tudbut/tryumph/example/Main.java b/src/de/tudbut/tryumph/example/Main.java index 9b52695..20c360a 100644 --- a/src/de/tudbut/tryumph/example/Main.java +++ b/src/de/tudbut/tryumph/example/Main.java @@ -2,6 +2,8 @@ package de.tudbut.tryumph.example; import java.net.Socket; +import org.w3c.dom.Document; +import org.w3c.dom.Element; import de.tudbut.async.Callback; import de.tudbut.async.ComposeCallback; import de.tudbut.async.TaskCallable; @@ -22,13 +24,34 @@ public class Main implements IRequestCatcher { public TaskCallable> onConnect(Socket socket) { return (tres, trej) -> tres.call((resp, res, rej) -> { System.out.println(resp.toString()); - listener.handle(resp, res, rej); + listener.handle(resp, r -> { + if(r.isHTML) { + Document html = r.getHTML(); + Element element; + element = html.createElement("meta"); + element.setAttribute("name", "viewport"); + element.setAttribute("content", "width=device-width height=device-height"); + html.getElementsByTagName("head").item(0).appendChild(element); + element = html.createElement("link"); + element.setAttribute("rel", "stylesheet"); + element.setAttribute("href", "/style.css"); + html.getElementsByTagName("head").item(0).appendChild(element); + r.updateHTMLData(); + } + res.call(r); + }, rej); if(!resp.hasResponse()) { res.call(new Response(resp, "

Error: 404 Not found " + resp.realPath + "

", 404, "Not Found")); } }); } + @GET + @Path("/style.css") + public void style(Request request, Callback res, Callback rej) { + res.call(new Response(request, request.context.file("style.css"), 200, "OK", "text/css")); + } + @GET @Path("/") public void onIndex(Request request, Callback res, Callback rej) { diff --git a/src/de/tudbut/tryumph/server/BrowserContext.java b/src/de/tudbut/tryumph/server/BrowserContext.java index 0894a0b..f7ebbca 100644 --- a/src/de/tudbut/tryumph/server/BrowserContext.java +++ b/src/de/tudbut/tryumph/server/BrowserContext.java @@ -81,7 +81,7 @@ public class BrowserContext { return AsyncJSON.write(data) .compose((resp, res, rej) -> { if(response.isHTML) { - Document document = response.getHtml(); + Document document = response.getHTML(); Element element = document.createElement("script"); Node text = document.createTextNode( "function setCookie(cname, cvalue) {" + @@ -108,8 +108,8 @@ public class BrowserContext { "data.save = function saveData() { setCookie('tryumph.data', encodeURIComponent(JSON.stringify(data))) }" ); element.appendChild(text); - Node body = document.getElementsByTagName("body").item(0); - body.insertBefore(element, body.getFirstChild()); + Node head = document.getElementsByTagName("head").item(0); + head.appendChild(element); response.updateHTMLData(); } if(needsChange) { diff --git a/src/de/tudbut/tryumph/server/Response.java b/src/de/tudbut/tryumph/server/Response.java index 24919d2..3d73b7f 100644 --- a/src/de/tudbut/tryumph/server/Response.java +++ b/src/de/tudbut/tryumph/server/Response.java @@ -71,13 +71,13 @@ public class Response { return html = HTMLParsing.parse(htmlData); } - public String getHtmlData() { + public String getHTMLData() { if(!isHTML) throw new IllegalStateException("Tried to access HTML on a non-HTML response"); return htmlData; } - public Document getHtml() { + public Document getHTML() { if(!isHTML) throw new IllegalStateException("Tried to access HTML on a non-HTML response"); return html; diff --git a/style.css b/style.css new file mode 100644 index 0000000..0e27bf1 --- /dev/null +++ b/style.css @@ -0,0 +1,9 @@ +center { + text-align: center; +} + +.content { + margin: auto; + display: block; + max-width: 700px; +}