Add JS API wrappers

This commit is contained in:
Alexey Andreev 2015-10-16 15:55:24 +03:00
parent 24952e5b86
commit 53cd861563
20 changed files with 730 additions and 111 deletions

View File

@ -56,6 +56,14 @@ public abstract class XMLHttpRequest implements JSObject {
@JSProperty("onreadystatechange")
public abstract void setOnReadyStateChange(ReadyStateChangeHandler handler);
public final void onComplete(Runnable runnable) {
setOnReadyStateChange(() -> {
if (getReadyState() == DONE) {
runnable.run();
}
});
}
public abstract void overrideMimeType(String mimeType);
@JSProperty

View File

@ -0,0 +1,45 @@
/*
* 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.
*/
package org.teavm.jso.browser;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface History extends JSObject {
@JSProperty
int getLength();
@JSProperty
JSObject getState();
void back();
void forward();
void go(int delta);
void pushState(JSObject data, String title);
void pushState(JSObject data, String title, String url);
void replaceState(JSObject data, String title);
void replaceState(JSObject data, String title, String url);
}

View File

@ -0,0 +1,84 @@
/*
* 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.
*/
package org.teavm.jso.browser;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface Location extends JSObject {
@JSProperty("href")
String getFullURL();
@JSProperty("href")
void setFullURL(String url);
@JSProperty
String getProtocol();
@JSProperty
void setProtocol(String protocol);
@JSProperty
String getHost();
@JSProperty
void setHost(String host);
@JSProperty("hostname")
String getHostName();
@JSProperty("hostname")
void setHostName(String hostName);
@JSProperty
String getPort();
@JSProperty
void setPort(String port);
@JSProperty("pathname")
String getPathName();
@JSProperty("pathname")
void setPathName(String pathName);
@JSProperty
String getSearch();
@JSProperty
void setSearch(String search);
@JSProperty
String getHash();
void setHash(String hash);
void assign(String url);
void reload();
void reload(boolean force);
void replace(String url);
static Location current() {
return Window.current().getLocation();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014 Alexey Andreev.
* 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.
@ -13,14 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.jso.dom.events;
package org.teavm.jso.browser;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSBody;
/**
*
* @author Alexey Andreev
*/
public interface DocumentEvent extends JSObject {
Event createEvent(String eventType);
public final class Navigator {
private Navigator() {
}
@JSBody(params = "", script = "return window.navigator.onLine;")
public static native boolean isOnline();
}

View File

@ -18,14 +18,17 @@ package org.teavm.jso.browser;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.core.JSArray;
import org.teavm.jso.core.JSArrayReader;
import org.teavm.jso.dom.html.HTMLDocument;
import org.teavm.jso.dom.html.HTMLElement;
import org.teavm.jso.dom.html.HTMLIFrameElement;
/**
*
* @author Alexey Andreev
*/
public abstract class Window implements JSObject, EventTarget, StorageProvider {
public abstract class Window implements JSObject, WindowEventTarget, StorageProvider, JSArrayReader<HTMLIFrameElement> {
private Window() {
}
@ -35,12 +38,64 @@ public abstract class Window implements JSObject, EventTarget, StorageProvider {
@JSProperty
public abstract Screen getScreen();
@JSProperty
public abstract int getScreenX();
@JSProperty
public abstract int getScreenY();
@JSProperty
public abstract Location getLocation();
@JSProperty
public abstract HTMLElement getFrameElement();
@JSProperty
public abstract JSArrayReader<HTMLIFrameElement> getFrames();
@JSProperty
public abstract int getInnerWidth();
@JSProperty
public abstract int getInnerHeight();
@JSProperty
public abstract int getOuterWidth();
@JSProperty
public abstract int getOuterHeight();
@JSProperty
public abstract String getName();
@JSProperty
public abstract void setName(String name);
@JSProperty
public abstract Window getParent();
@JSProperty
public abstract Window getTop();
@JSBody(params = "message", script = "alert(message);")
public static native void alert(JSObject message);
@JSBody(params = "message", script = "alert(message);")
public static native void alert(String message);
@JSBody(params = "message", script = "confirm(message);")
public static native boolean confirm(JSObject message);
@JSBody(params = "message", script = "confirm(message);")
public static native boolean confirm(String message);
public static String prompt(String message) {
return prompt(message, "");
}
@JSBody(params = { "message", "default" }, script = "prompt(message, default);")
public static native String prompt(String message, String defaultValue);
@JSBody(params = { "handler", "delay" }, script = "return setTimeout(handler, delay);")
public static native int setTimeout(TimerHandler handler, int delay);
@ -59,6 +114,44 @@ public abstract class Window implements JSObject, EventTarget, StorageProvider {
@JSBody(params = { "timeoutId" }, script = "clearInterval(timeoutId);")
public static native void clearInterval(int timeoutId);
public abstract void blur();
public abstract void focus();
public abstract void close();
public abstract void moveBy(int deltaX, int deltaY);
public abstract void moveTo(int x, int y);
public abstract void resizeBy(int deltaX, int deltaY);
public abstract void resizeTo(int x, int y);
public abstract void scrollBy(int deltaX, int deltaY);
public abstract void scrollTo(int x, int y);
public abstract Window open(String url, String name);
public final Window open(String url, String name, WindowFeatures features) {
return open(url, name, features.sb.toString());
}
public abstract Window open(String url, String name, String features);
public abstract void print();
public abstract void stop();
public abstract void postMessage(JSObject message, String targetOrigin);
public abstract void postMessage(JSObject message, String targetOrigin, JSArrayReader<JSObject> transfer);
public final void postMessage(JSObject message, String targetOrigin, JSObject... transfer) {
postMessage(message, targetOrigin, JSArray.of(transfer));
}
@JSBody(params = {}, script = "return window;")
public static native Window current();
}

View File

@ -0,0 +1,48 @@
/*
* 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.
*/
package org.teavm.jso.browser;
import org.teavm.jso.dom.events.Event;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.FocusEventTarget;
import org.teavm.jso.dom.events.KeyboardEventTarget;
import org.teavm.jso.dom.events.LoadEventTarget;
import org.teavm.jso.dom.events.MessageEvent;
import org.teavm.jso.dom.events.MouseEventTarget;
/**
*
* @author Alexey Andreev
*/
public interface WindowEventTarget extends EventTarget, FocusEventTarget, MouseEventTarget, KeyboardEventTarget,
LoadEventTarget {
default void listenBeforeOnload(EventListener<Event> listener) {
addEventListener("beforeunload", listener);
}
default void neglectBeforeOnload(EventListener<Event> listener) {
removeEventListener("beforeunload", listener);
}
default void listenMessage(EventListener<MessageEvent> listener) {
addEventListener("message", listener);
}
default void neglectMessage(EventListener<MessageEvent> listener) {
removeEventListener("message", listener);
}
}

View File

@ -0,0 +1,75 @@
/*
* 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.
*/
package org.teavm.jso.browser;
/**
*
* @author Alexey Andreev
*/
public class WindowFeatures {
StringBuilder sb = new StringBuilder();
public WindowFeatures() {
}
public WindowFeatures left(int left) {
return add("left=" + left);
}
public WindowFeatures top(int top) {
return add("top=" + top);
}
public WindowFeatures width(int width) {
return add("width=" + width);
}
public WindowFeatures height(int height) {
return add("height=" + height);
}
public WindowFeatures menubar() {
return add("menubar");
}
public WindowFeatures toolbar() {
return add("toolbar");
}
public WindowFeatures location() {
return add("location");
}
public WindowFeatures status() {
return add("status");
}
public WindowFeatures resizable() {
return add("resizable");
}
public WindowFeatures scrollbars() {
return add("resizable");
}
private WindowFeatures add(String feature) {
if (sb.length() > 0) {
sb.append(',');
}
sb.append(feature);
return this;
}
}

View File

@ -88,4 +88,13 @@ public abstract class JSArray<T extends JSObject> implements JSArrayReader<T> {
@JSBody(params = "size", script = "return new Array(size);")
public static native <T extends JSObject> JSArray<T> create(int size);
@SafeVarargs
public static <S extends JSObject> JSArray<S> of(S... items) {
JSArray<S> array = create(items.length);
for (int i = 0; i < items.length; ++i) {
array.set(i, items[i]);
}
return array;
}
}

View File

@ -0,0 +1,38 @@
/*
* 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.
*/
package org.teavm.jso.dom.events;
/**
*
* @author Alexey Andreev
*/
public interface FocusEventTarget extends EventTarget {
default void listenFocus(EventListener<Event> listener) {
addEventListener("focus", listener);
}
default void neglectFocus(EventListener<Event> listener) {
removeEventListener("focus", listener);
}
default void listenBlur(EventListener<Event> listener) {
addEventListener("blur", listener);
}
default void neglectBlur(EventListener<Event> listener) {
removeEventListener("blur", listener);
}
}

View File

@ -0,0 +1,46 @@
/*
* 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.
*/
package org.teavm.jso.dom.events;
/**
*
* @author Alexey Andreev
*/
public interface KeyboardEventTarget extends EventTarget {
default void listenKeyDown(EventListener<KeyboardEvent> listener) {
addEventListener("keydown", listener);
}
default void neglectKeyDown(EventListener<KeyboardEvent> listener) {
removeEventListener("keydown", listener);
}
default void listenKeyUp(EventListener<KeyboardEvent> listener) {
addEventListener("keyup", listener);
}
default void neglectKeyUp(EventListener<KeyboardEvent> listener) {
removeEventListener("keyup", listener);
}
default void listenKeyPress(EventListener<KeyboardEvent> listener) {
addEventListener("keypress", listener);
}
default void neglectKeyPress(EventListener<KeyboardEvent> listener) {
removeEventListener("keypress", listener);
}
}

View File

@ -0,0 +1,30 @@
/*
* 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.
*/
package org.teavm.jso.dom.events;
/**
*
* @author Alexey Andreev
*/
public interface LoadEventTarget extends EventTarget {
default void listenLoad(EventListener<Event> listener) {
addEventListener("load", listener);
}
default void neglectLoad(EventListener<Event> listener) {
addEventListener("load", listener);
}
}

View File

@ -0,0 +1,35 @@
/*
* 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.
*/
package org.teavm.jso.dom.events;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.typedarrays.ArrayBuffer;
/**
*
* @author Alexey Andreev
*/
public interface MessageEvent extends Event {
@JSProperty
JSObject getData();
@JSProperty("data")
String getDataAsString();
@JSProperty("data")
ArrayBuffer getDataAsArray();
}

View File

@ -0,0 +1,86 @@
/*
* 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.
*/
package org.teavm.jso.dom.events;
/**
*
* @author Alexey Andreev
*/
public interface MouseEventTarget extends EventTarget {
default void listenClick(EventListener<MouseEvent> listener) {
addEventListener("click", listener);
}
default void neglectClick(EventListener<MouseEvent> listener) {
removeEventListener("click", listener);
}
default void listenDoubleClick(EventListener<MouseEvent> listener) {
addEventListener("dblclick", listener);
}
default void neglectDoubleClick(EventListener<MouseEvent> listener) {
removeEventListener("dblclick", listener);
}
default void listenMouseDown(EventListener<MouseEvent> listener) {
addEventListener("mousedown", listener);
}
default void neglectMouseDown(EventListener<MouseEvent> listener) {
removeEventListener("mousedown", listener);
}
default void listenMouseUp(EventListener<MouseEvent> listener) {
addEventListener("mouseup", listener);
}
default void neglectMouseUp(EventListener<MouseEvent> listener) {
removeEventListener("mouseup", listener);
}
default void listenMouseOver(EventListener<MouseEvent> listener) {
addEventListener("mouseover", listener);
}
default void neglectMouseOver(EventListener<MouseEvent> listener) {
removeEventListener("mouseover", listener);
}
default void listenMouseEnter(EventListener<MouseEvent> listener) {
addEventListener("mouseenter", listener);
}
default void neglectMouseEnter(EventListener<MouseEvent> listener) {
removeEventListener("mouseenter", listener);
}
default void listenMouseLeaeve(EventListener<MouseEvent> listener) {
addEventListener("mouseleave", listener);
}
default void neglectMouseLeave(EventListener<MouseEvent> listener) {
removeEventListener("mouseleave", listener);
}
default void listenMouseOut(EventListener<MouseEvent> listener) {
addEventListener("mouseout", listener);
}
default void neglectMouseOut(EventListener<MouseEvent> listener) {
removeEventListener("mouseout", listener);
}
}

View File

@ -19,14 +19,20 @@ import java.util.function.Consumer;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.css.ElementCSSInlineStyle;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.FocusEventTarget;
import org.teavm.jso.dom.events.KeyboardEventTarget;
import org.teavm.jso.dom.events.LoadEventTarget;
import org.teavm.jso.dom.events.MouseEventTarget;
import org.teavm.jso.dom.xml.Element;
import org.teavm.jso.dom.xml.Node;
import org.teavm.jso.dom.xml.NodeList;
/**
*
* @author Alexey Andreev
*/
public interface HTMLElement extends Element, ElementCSSInlineStyle, EventTarget {
public interface HTMLElement extends Element, ElementCSSInlineStyle, EventTarget, FocusEventTarget, MouseEventTarget,
KeyboardEventTarget, LoadEventTarget {
@Override
@JSProperty
NodeList<? extends HTMLElement> getElementsByTagName(String name);
@ -120,14 +126,19 @@ public interface HTMLElement extends Element, ElementCSSInlineStyle, EventTarget
default HTMLElement withChild(String tagName) {
HTMLElement result = getOwnerDocument().createElement(tagName);
appendChild(result);
return result;
return this;
}
default HTMLElement withChild(Node node) {
appendChild(node);
return this;
}
default HTMLElement withChild(String tagName, Consumer<HTMLElement> consumer) {
HTMLElement result = getOwnerDocument().createElement(tagName);
appendChild(result);
consumer.accept(result);
return result;
return this;
}
default HTMLElement clear() {

View File

@ -0,0 +1,61 @@
/*
* 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.
*/
package org.teavm.jso.dom.html;
import org.teavm.jso.JSProperty;
import org.teavm.jso.browser.Window;
/**
*
* @author Alexey Andreev
*/
public interface HTMLIFrameElement extends HTMLElement {
@JSProperty
HTMLDocument getContentDocument();
@JSProperty
Window getContentWindow();
@JSProperty
String getWidth();
@JSProperty
void setWidth(String width);
@JSProperty
String getHeight();
@JSProperty
void setHeight(String height);
@JSProperty
String getName();
@JSProperty
void setName(String name);
@JSProperty("src")
String getSourceAddress();
@JSProperty("src")
void setSourceAddress(String src);
@JSProperty("srcdoc")
String getSourceDocument();
@JSProperty("srcdoc")
void setSourceDocument(String srcdoc);
}

View File

@ -83,6 +83,10 @@ public interface HTMLMediaElement extends HTMLElement {
@JSProperty
void setCurrentTime(double currentTime);
default void addCurrentTime(double delta) {
setCurrentTime(getCurrentTime() + delta);
}
@JSProperty
double getDuration();
@ -104,6 +108,10 @@ public interface HTMLMediaElement extends HTMLElement {
@JSProperty
void setPlaybackRate(double playbackRate);
default void addPlaybackRate(double delta) {
setPlaybackRate(getPlaybackRate() + delta);
}
@JSProperty
TimeRanges getPlayed();
@ -149,6 +157,10 @@ public interface HTMLMediaElement extends HTMLElement {
@JSProperty
void setVolume(float volume);
default void addVolume(float delta) {
setVolume(getVolume() + delta);
}
@JSProperty
boolean isMuted();

View File

@ -31,18 +31,14 @@ public final class Client {
}
public static void main(String[] args) {
helloButton.addEventListener("click", evt -> sayHello());
helloButton.listenClick(evt -> sayHello());
}
private static void sayHello() {
helloButton.setDisabled(true);
thinkingPanel.getStyle().setProperty("display", "");
XMLHttpRequest xhr = XMLHttpRequest.create();
xhr.setOnReadyStateChange(() -> {
if (xhr.getReadyState() == XMLHttpRequest.DONE) {
receiveResponse(xhr.getResponseText());
}
});
xhr.onComplete(() -> receiveResponse(xhr.getResponseText()));
xhr.open("GET", "hello");
xhr.send();
}

View File

@ -14,7 +14,7 @@ object Client {
var exprElem = doc.getElementById("expr").asInstanceOf[HTMLInputElement]
var calcElem = doc.getElementById("calculate")
var resultList = doc.getElementById("result-list");
calcElem.addEventListener("click", (e : MouseEvent) => {
calcElem.listenClick((e : MouseEvent) => {
parse(exprElem.getValue().toSeq) match {
case (None, _) => Window.alert("Error parsing expression");
case (Some(x), Nil) => {

View File

@ -39,7 +39,7 @@ public final class Application {
}
HTMLButtonElement saveButton = document.getElementById("save-button").cast();
saveButton.addEventListener("click", e -> {
saveButton.listenClick(e -> {
String key = document.getElementById("key").<HTMLInputElement>cast().getValue();
String value = document.getElementById("value").<HTMLInputElement>cast().getValue();
@ -49,7 +49,7 @@ public final class Application {
}
});
HTMLButtonElement deleteButton = document.getElementById("delete-button").cast();
deleteButton.addEventListener("click", e -> {
deleteButton.listenClick(e -> {
String key = document.getElementById("key").<HTMLInputElement>cast().getValue();
if (key != null && key.length() > 0) {
storage.removeItem(key);
@ -57,7 +57,7 @@ public final class Application {
}
});
HTMLButtonElement deleteAllButton = document.getElementById("delete-all-button").cast();
deleteAllButton.addEventListener("click", e -> {
deleteAllButton.listenClick(e -> {
storage.clear();
draw();
});
@ -75,15 +75,9 @@ public final class Application {
String key = storage.key(i);
String value = storage.getItem(key);
HTMLElement tdKey = document.createElement("td");
tdKey.appendChild(document.createTextNode(key));
HTMLElement tdValue = document.createElement("td");
tdValue.appendChild(document.createTextNode(value));
HTMLElement tr = document.createElement("tr");
tr.appendChild(tdKey);
tr.appendChild(tdValue);
HTMLElement tdKey = document.createElement("td").withText(key);
HTMLElement tdValue = document.createElement("td").withText(value);
HTMLElement tr = document.createElement("tr").withChild(tdKey).withChild(tdValue);
tbody.appendChild(tr);
}

View File

@ -17,7 +17,6 @@ package org.teavm.samples.video;
import org.teavm.jso.browser.Window;
import org.teavm.jso.dom.html.HTMLBodyElement;
import org.teavm.jso.dom.html.HTMLButtonElement;
import org.teavm.jso.dom.html.HTMLDocument;
import org.teavm.jso.dom.html.HTMLElement;
import org.teavm.jso.dom.html.HTMLSourceElement;
@ -31,17 +30,14 @@ public final class Player {
public static void main(String[] args) {
HTMLSourceElement sourceMp4 = document.createElement("source").cast();
sourceMp4.setAttribute("id", "mp4");
sourceMp4.setSrc("http://media.w3.org/2010/05/sintel/trailer.mp4");
sourceMp4.setAttribute("type", "video/mp4");
HTMLSourceElement sourceWebm = document.createElement("source").cast();
sourceWebm.setAttribute("id", "webm");
sourceWebm.setSrc("http://media.w3.org/2010/05/sintel/trailer.webm");
sourceWebm.setAttribute("type", "video/webm");
HTMLSourceElement sourceOgv = document.createElement("source").cast();
sourceOgv.setAttribute("id", "ogv");
sourceOgv.setSrc("http://media.w3.org/2010/05/sintel/trailer.ogv");
sourceOgv.setAttribute("type", "video/ogg");
@ -49,7 +45,6 @@ public final class Player {
p.appendChild(document.createTextNode("Your user agent does not support the HTML5 Video element."));
HTMLVideoElement video = document.createElement("video").cast();
video.setAttribute("id", "video");
video.setControls(true);
video.setPreload("none");
video.setMediaGroup("myVideoGroup");
@ -62,83 +57,32 @@ public final class Player {
HTMLElement divVideo = document.createElement("div");
divVideo.appendChild(video);
HTMLButtonElement loadButton = document.createElement("button").cast();
loadButton.appendChild(document.createTextNode("load()"));
loadButton.addEventListener("click", evt -> video.load());
HTMLButtonElement playButton = document.createElement("button").cast();
playButton.appendChild(document.createTextNode("play()"));
playButton.addEventListener("click", evt -> video.play());
HTMLButtonElement pauseButton = document.createElement("button").cast();
pauseButton.appendChild(document.createTextNode("pause()"));
pauseButton.addEventListener("click", evt -> video.pause());
HTMLButtonElement currentTimePlusButton = document.createElement("button").cast();
currentTimePlusButton.appendChild(document.createTextNode("currentTime+=10"));
currentTimePlusButton.addEventListener("click", evt -> video.setCurrentTime(video.getCurrentTime() + 10));
HTMLButtonElement currentTimeMinusButton = document.createElement("button").cast();
currentTimeMinusButton.appendChild(document.createTextNode("currentTime-=10"));
currentTimeMinusButton.addEventListener("click", evt -> video.setCurrentTime(video.getCurrentTime() - 10));
HTMLButtonElement currentTime50Button = document.createElement("button").cast();
currentTime50Button.appendChild(document.createTextNode("currentTime=50"));
currentTime50Button.addEventListener("click", evt -> video.setCurrentTime(50));
HTMLButtonElement playbackRateIncrementButton = document.createElement("button").cast();
playbackRateIncrementButton.appendChild(document.createTextNode("playbackRate++"));
playbackRateIncrementButton.addEventListener("click", evt -> video.setPlaybackRate(
video.getPlaybackRate() + 1));
HTMLButtonElement playbackRateDecrementButton = document.createElement("button").cast();
playbackRateDecrementButton.appendChild(document.createTextNode("playbackRate--"));
playbackRateDecrementButton.addEventListener("click", evt -> video.setPlaybackRate(
video.getPlaybackRate() - 1));
HTMLButtonElement playbackRatePlusButton = document.createElement("button").cast();
playbackRatePlusButton.appendChild(document.createTextNode("playbackRate+=0.1"));
playbackRatePlusButton.addEventListener("click", evt -> video.setPlaybackRate(video.getPlaybackRate() + 0.1));
HTMLButtonElement playbackRateMinusButton = document.createElement("button").cast();
playbackRateMinusButton.appendChild(document.createTextNode("playbackRate-=0.1"));
playbackRateMinusButton.addEventListener("click", evt -> video.setPlaybackRate(video.getPlaybackRate() - 0.1));
HTMLButtonElement volumePlusButton = document.createElement("button").cast();
volumePlusButton.appendChild(document.createTextNode("volume+=0.1"));
volumePlusButton.addEventListener("click", evt -> video.setVolume(video.getVolume() + 0.1f));
HTMLButtonElement volumeMinusButton = document.createElement("button").cast();
volumeMinusButton.appendChild(document.createTextNode("volume-=0.1"));
volumeMinusButton.addEventListener("click", evt -> video.setVolume(video.getVolume() - 0.1f));
HTMLButtonElement muteButton = document.createElement("button").cast();
muteButton.appendChild(document.createTextNode("muted=true"));
muteButton.addEventListener("click", evt -> video.setMuted(true));
HTMLButtonElement unmuteButton = document.createElement("button").cast();
unmuteButton.appendChild(document.createTextNode("muted=false"));
unmuteButton.addEventListener("click", evt -> video.setMuted(false));
HTMLElement divButtons = document.createElement("div");
divButtons.setAttribute("id", "buttons");
divButtons.appendChild(loadButton);
divButtons.appendChild(playButton);
divButtons.appendChild(pauseButton);
divButtons.appendChild(document.createElement("br"));
divButtons.appendChild(currentTimePlusButton);
divButtons.appendChild(currentTimeMinusButton);
divButtons.appendChild(currentTime50Button);
divButtons.appendChild(document.createElement("br"));
divButtons.appendChild(playbackRateIncrementButton);
divButtons.appendChild(playbackRateDecrementButton);
divButtons.appendChild(playbackRatePlusButton);
divButtons.appendChild(playbackRateMinusButton);
divButtons.appendChild(document.createElement("br"));
divButtons.appendChild(volumePlusButton);
divButtons.appendChild(volumeMinusButton);
divButtons.appendChild(muteButton);
divButtons.appendChild(unmuteButton);
HTMLElement divButtons = document.createElement("div")
.withAttr("id", "button")
.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("pause()").listenClick(evt -> video.pause()))
.withChild("br")
.withChild("button", elem -> elem.withText("currentTime+=10")
.listenClick(evt -> video.addCurrentTime(10)))
.withChild("button", elem -> elem.withText("currentTime-=10")
.listenClick(evt -> video.addCurrentTime(-10)))
.withChild("button", elem -> elem.withText("currentTime-=50")
.listenClick(evt -> video.setCurrentTime(50)))
.withChild("br")
.withChild("button", elem -> elem.withText("playbackRate++")
.listenClick(evt -> video.addPlaybackRate(1)))
.withChild("button", elem -> elem.withText("playbackRate--")
.listenClick(evt -> video.addPlaybackRate(-1)))
.withChild("button", elem -> elem.withText("playbackRate+=0.1")
.listenClick(evt -> video.addPlaybackRate(0.1)))
.withChild("button", elem -> elem.withText("playbackRate-=0.1")
.listenClick(evt -> video.addPlaybackRate(-0.1)))
.withChild("br")
.withChild("button", elem -> elem.withText("volume+=1").listenClick(evt -> video.addVolume(0.1F)))
.withChild("button", elem -> elem.withText("volume-=1").listenClick(evt -> video.addVolume(-0.1F)))
.withChild("button", elem -> elem.withText("mute").listenClick(evt -> video.setMuted(true)))
.withChild("button", elem -> elem.withText("unmute").listenClick(evt -> video.setMuted(false)));
HTMLBodyElement body = document.getBody();
body.appendChild(divVideo);