Samples: merge storage and video into one module

This commit is contained in:
Alexey Andreev 2023-02-10 18:48:01 +01:00
parent bdbce01b9a
commit 670f24a4af
11 changed files with 73 additions and 88 deletions

View File

@ -58,8 +58,7 @@ include("benchmark")
include("pi") include("pi")
include("kotlin") include("kotlin")
include("scala") include("scala")
include("video") include("web-apis")
include("storage")
gradle.allprojects { gradle.allprojects {
apply<WarPlugin>() apply<WarPlugin>()

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2014 Alexey Andreev.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015 Alexey Andreev.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>

View File

@ -26,5 +26,5 @@ dependencies {
teavm.js { teavm.js {
addedToWebApp.set(true) addedToWebApp.set(true)
mainClass.set("org.teavm.samples.video.Player") mainClass.set("org.teavm.samples.webapis.Main")
} }

View File

@ -13,18 +13,20 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.samples.webapis;
plugins { public final class Main {
java private Main() {
war }
id("org.teavm")
}
dependencies { public static void main(String[] args) {
teavm(teavm.libs.jsoApis) switch (args[0]) {
} case "storage":
Storage.run();
teavm.js { break;
addedToWebApp.set(true) case "video":
mainClass.set("org.teavm.samples.storage.Application") Video.run();
break;
}
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2015 Alexey Andreev. * Copyright 2023 Alexey Andreev.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,35 +13,33 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.samples.storage; package org.teavm.samples.webapis;
import org.teavm.jso.browser.Storage;
import org.teavm.jso.browser.Window; import org.teavm.jso.browser.Window;
import org.teavm.jso.dom.html.HTMLButtonElement; import org.teavm.jso.dom.html.HTMLButtonElement;
import org.teavm.jso.dom.html.HTMLDocument; import org.teavm.jso.dom.html.HTMLDocument;
import org.teavm.jso.dom.html.HTMLElement;
import org.teavm.jso.dom.html.HTMLInputElement; import org.teavm.jso.dom.html.HTMLInputElement;
/** /**
* *
* @author Junji Takakura * @author Junji Takakura
*/ */
public final class Application { public final class Storage {
private static HTMLDocument document = Window.current().getDocument(); private static HTMLDocument document = Window.current().getDocument();
private static Storage storage = Window.current().getSessionStorage(); private static org.teavm.jso.browser.Storage storage = Window.current().getSessionStorage();
private Application() { private Storage() {
} }
public static void main(String[] args) { public static void run() {
if (storage == null) { if (storage == null) {
Window.alert("storage is not supported."); Window.alert("storage is not supported.");
} }
HTMLButtonElement saveButton = document.getElementById("save-button").cast(); HTMLButtonElement saveButton = document.getElementById("save-button").cast();
saveButton.listenClick(e -> { saveButton.listenClick(e -> {
String key = document.getElementById("key").<HTMLInputElement>cast().getValue(); var key = document.getElementById("key").<HTMLInputElement>cast().getValue();
String value = document.getElementById("value").<HTMLInputElement>cast().getValue(); var value = document.getElementById("value").<HTMLInputElement>cast().getValue();
if (key != null && key.length() > 0 && value != null && value.length() > 0) { if (key != null && key.length() > 0 && value != null && value.length() > 0) {
storage.setItem(key, value); storage.setItem(key, value);
@ -65,19 +63,19 @@ public final class Application {
} }
private static void draw() { private static void draw() {
HTMLElement tbody = document.getElementById("list"); var tbody = document.getElementById("list");
while (tbody.getFirstChild() != null) { while (tbody.getFirstChild() != null) {
tbody.removeChild(tbody.getFirstChild()); tbody.removeChild(tbody.getFirstChild());
} }
for (int i = 0; i < storage.getLength(); i++) { for (int i = 0; i < storage.getLength(); i++) {
String key = storage.key(i); var key = storage.key(i);
String value = storage.getItem(key); var value = storage.getItem(key);
HTMLElement tdKey = document.createElement("td").withText(key); var tdKey = document.createElement("td").withText(key);
HTMLElement tdValue = document.createElement("td").withText(value); var tdValue = document.createElement("td").withText(value);
HTMLElement tr = document.createElement("tr").withChild(tdKey).withChild(tdValue); var tr = document.createElement("tr").withChild(tdKey).withChild(tdValue);
tbody.appendChild(tr); tbody.appendChild(tr);
} }

View File

@ -13,22 +13,20 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.samples.video; package org.teavm.samples.webapis;
import org.teavm.jso.browser.Window; import org.teavm.jso.browser.Window;
import org.teavm.jso.dom.html.HTMLBodyElement;
import org.teavm.jso.dom.html.HTMLDocument; import org.teavm.jso.dom.html.HTMLDocument;
import org.teavm.jso.dom.html.HTMLElement;
import org.teavm.jso.dom.html.HTMLSourceElement; import org.teavm.jso.dom.html.HTMLSourceElement;
import org.teavm.jso.dom.html.HTMLVideoElement; import org.teavm.jso.dom.html.HTMLVideoElement;
public final class Player { public final class Video {
private static HTMLDocument document = Window.current().getDocument(); private static HTMLDocument document = Window.current().getDocument();
private Player() { private Video() {
} }
public static void main(String[] args) { public static void run() {
HTMLSourceElement sourceMp4 = document.createElement("source").cast(); HTMLSourceElement sourceMp4 = document.createElement("source").cast();
sourceMp4.setSrc("http://media.w3.org/2010/05/sintel/trailer.mp4"); sourceMp4.setSrc("http://media.w3.org/2010/05/sintel/trailer.mp4");
sourceMp4.setAttribute("type", "video/mp4"); sourceMp4.setAttribute("type", "video/mp4");
@ -41,7 +39,7 @@ public final class Player {
sourceOgv.setSrc("http://media.w3.org/2010/05/sintel/trailer.ogv"); sourceOgv.setSrc("http://media.w3.org/2010/05/sintel/trailer.ogv");
sourceOgv.setAttribute("type", "video/ogg"); sourceOgv.setAttribute("type", "video/ogg");
HTMLElement p = document.createElement("p"); var p = document.createElement("p");
p.appendChild(document.createTextNode("Your user agent does not support the HTML5 Video element.")); p.appendChild(document.createTextNode("Your user agent does not support the HTML5 Video element."));
HTMLVideoElement video = document.createElement("video").cast(); HTMLVideoElement video = document.createElement("video").cast();
@ -54,10 +52,10 @@ public final class Player {
video.appendChild(sourceOgv); video.appendChild(sourceOgv);
video.appendChild(p); video.appendChild(p);
HTMLElement divVideo = document.createElement("div"); var divVideo = document.createElement("div");
divVideo.appendChild(video); divVideo.appendChild(video);
HTMLElement divButtons = document.createElement("div") var divButtons = document.createElement("div")
.withAttr("id", "button") .withAttr("id", "button")
.withChild("button", elem -> elem.withText("load()").listenClick(evt -> video.load())) .withChild("button", elem -> elem.withText("load()").listenClick(evt -> video.load()))
.withChild("button", elem -> elem.withText("play()").listenClick(evt -> video.play())) .withChild("button", elem -> elem.withText("play()").listenClick(evt -> video.play()))
@ -84,7 +82,7 @@ public final class Player {
.withChild("button", elem -> elem.withText("mute").listenClick(evt -> video.setMuted(true))) .withChild("button", elem -> elem.withText("mute").listenClick(evt -> video.setMuted(true)))
.withChild("button", elem -> elem.withText("unmute").listenClick(evt -> video.setMuted(false))); .withChild("button", elem -> elem.withText("unmute").listenClick(evt -> video.setMuted(false)));
HTMLBodyElement body = document.getBody(); var body = document.getBody();
body.appendChild(divVideo); body.appendChild(divVideo);
body.appendChild(divButtons); body.appendChild(divButtons);
} }

View File

@ -0,0 +1,30 @@
<!--
~ Copyright 2023 Alexey Andreev.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!DOCTYPE html>
<html>
<head>
<title>Web APIs demo app</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<h1>Please, select a demo</h1>
<ul>
<li><a href="storage.html">Storage</a></li>
<li><a href="video.html">Video</a></li>
</ul>
</body>
</html>

View File

@ -18,9 +18,9 @@
<head> <head>
<title>Web Storage web application</title> <title>Web Storage web application</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" charset="utf-8" src="js/storage.js"></script> <script type="text/javascript" charset="utf-8" src="js/web-apis.js"></script>
</head> </head>
<body onload="main()"> <body onload="main(['storage'])">
<h1>Web Storage web application</h1> <h1>Web Storage web application</h1>
<input type="text" id="key"> <input type="text" id="key">
<input type="text" id="value"> <input type="text" id="value">

View File

@ -18,10 +18,10 @@
<head> <head>
<title>HTML5 Video web application</title> <title>HTML5 Video web application</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" charset="utf-8" src="js/video.js"></script> <script type="text/javascript" charset="utf-8" src="js/web-apis.js"></script>
<link href="style.css" rel="stylesheet" type="text/css"> <link href="video.css" rel="stylesheet" type="text/css">
</head> </head>
<body onload="main()"> <body onload="main(['video'])">
<h1>HTML5 Video web application</h1> <h1>HTML5 Video web application</h1>
</body> </body>
</html> </html>