From fc9a53bdd3e7cd720441e8401f08ab4ffe4c0229 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 27 Jan 2019 23:46:09 +0300 Subject: [PATCH] JS: add more API declarations --- .../org/teavm/jso/ajax/XMLHttpRequest.java | 5 ++ .../java/org/teavm/jso/browser/Navigator.java | 6 ++ .../java/org/teavm/jso/browser/Window.java | 7 +- .../java/org/teavm/jso/core/JSMapLike.java | 27 +++++++ .../main/java/org/teavm/jso/core/JSON.java | 30 +++++++ .../java/org/teavm/jso/core/JSObjects.java | 42 ++++++++++ .../org/teavm/jso/dom/events/MouseEvent.java | 6 ++ .../teavm/jso/dom/html/HTMLAnchorElement.java | 6 ++ .../org/teavm/jso/dom/html/HTMLDocument.java | 15 ++++ .../org/teavm/jso/dom/html/HTMLElement.java | 4 + .../org/teavm/jso/websocket/CloseEvent.java | 30 +++++++ .../org/teavm/jso/websocket/WebSocket.java | 78 +++++++++++++++++++ 12 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 jso/apis/src/main/java/org/teavm/jso/core/JSMapLike.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/core/JSON.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/core/JSObjects.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/websocket/CloseEvent.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/websocket/WebSocket.java diff --git a/jso/apis/src/main/java/org/teavm/jso/ajax/XMLHttpRequest.java b/jso/apis/src/main/java/org/teavm/jso/ajax/XMLHttpRequest.java index 4368bfd63..3edc20e97 100644 --- a/jso/apis/src/main/java/org/teavm/jso/ajax/XMLHttpRequest.java +++ b/jso/apis/src/main/java/org/teavm/jso/ajax/XMLHttpRequest.java @@ -90,4 +90,9 @@ public abstract class XMLHttpRequest implements JSObject { @JSBody(script = "return new XMLHttpRequest();") public static native XMLHttpRequest create(); + + public abstract void abort(); + + @JSProperty + public abstract String getResponseURL(); } diff --git a/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java b/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java index 206d67f1e..901585adb 100644 --- a/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java +++ b/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java @@ -30,4 +30,10 @@ public final class Navigator { @JSBody(script = "return (\"geolocation\" in navigator);") public static native boolean isGeolocationAvailable(); + + @JSBody(script = "return navigator.language;") + public static native String getLanguage(); + + @JSBody(script = "return navigator.languages;") + public static native String[] getLanguages(); } diff --git a/jso/apis/src/main/java/org/teavm/jso/browser/Window.java b/jso/apis/src/main/java/org/teavm/jso/browser/Window.java index fb87f1e71..ff0fd36b9 100644 --- a/jso/apis/src/main/java/org/teavm/jso/browser/Window.java +++ b/jso/apis/src/main/java/org/teavm/jso/browser/Window.java @@ -41,7 +41,6 @@ public abstract class Window implements JSObject, WindowEventTarget, StorageProv @JSProperty public abstract int getScreenY(); - @JSProperty public abstract Location getLocation(); @@ -184,4 +183,10 @@ public abstract class Window implements JSObject, WindowEventTarget, StorageProv @JSProperty public abstract double getDevicePixelRatio(); + + @JSBody(params = "s", script = "return window.atob(s);") + public static native String atob(String s); + + @JSBody(params = "s", script = "return window.btoa(s);") + public static native String btoa(String s); } diff --git a/jso/apis/src/main/java/org/teavm/jso/core/JSMapLike.java b/jso/apis/src/main/java/org/teavm/jso/core/JSMapLike.java new file mode 100644 index 000000000..7fb0808d7 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/core/JSMapLike.java @@ -0,0 +1,27 @@ +/* + * Copyright 2019 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.core; + +import org.teavm.jso.JSIndexer; +import org.teavm.jso.JSObject; + +public interface JSMapLike extends JSObject { + @JSIndexer + T get(String key); + + @JSIndexer + void set(String key, T object); +} diff --git a/jso/apis/src/main/java/org/teavm/jso/core/JSON.java b/jso/apis/src/main/java/org/teavm/jso/core/JSON.java new file mode 100644 index 000000000..7bcebaecc --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/core/JSON.java @@ -0,0 +1,30 @@ +/* + * Copyright 2019 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.core; + +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; + +public final class JSON { + private JSON() { + } + + @JSBody(params = "s", script = "return JSON.parse(s);") + public static native T parse(String s); + + @JSBody(params = "o", script = "JSON.stringify(o);") + public static native String stringify(JSObject o); +} diff --git a/jso/apis/src/main/java/org/teavm/jso/core/JSObjects.java b/jso/apis/src/main/java/org/teavm/jso/core/JSObjects.java new file mode 100644 index 000000000..1cd8ff844 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/core/JSObjects.java @@ -0,0 +1,42 @@ +/* + * Copyright 2019 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.core; + +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; + +public final class JSObjects { + private JSObjects() { + } + + @JSBody(params = "object", script = "return Object.getOwnPropertyNames(object);") + public static native String[] getOwnPropertyNames(JSObject object); + + @JSBody(script = "return Object.create(null);") + public static native T create(); + + @JSBody(params = "object", script = "return typeof object === 'undefined';") + public static native boolean isUndefined(JSObject object); + + @JSBody(params = "object", script = "return typeof object;") + public static native String typeOf(JSObject object); + + @JSBody(params = "object", script = "return object.toString();") + public static native String toString(JSObject object); + + @JSBody(params = { "object", "name" }, script = "return name in object;") + public static native boolean hasProperty(JSObject object, String name); +} diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/events/MouseEvent.java b/jso/apis/src/main/java/org/teavm/jso/dom/events/MouseEvent.java index 3ee30f405..76187622c 100644 --- a/jso/apis/src/main/java/org/teavm/jso/dom/events/MouseEvent.java +++ b/jso/apis/src/main/java/org/teavm/jso/dom/events/MouseEvent.java @@ -59,6 +59,12 @@ public interface MouseEvent extends Event { @JSProperty EventTarget getRelatedTarget(); + @JSProperty + double getMovementX(); + + @JSProperty + double getMovementY(); + void initMouseEvent(String type, boolean canBubble, boolean cancelable, JSObject view, int detail, int screenX, int screenY, int clientX, int clientY, boolean ctrlKey, boolean altKey, boolean shiftKey, boolean metaKey, short button, EventTarget relatedTarget); diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLAnchorElement.java b/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLAnchorElement.java index cd7452888..20ddd6660 100644 --- a/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLAnchorElement.java +++ b/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLAnchorElement.java @@ -63,4 +63,10 @@ public interface HTMLAnchorElement extends HTMLElement { @JSProperty void setText(String value); + + @JSProperty + String getDownload(); + + @JSProperty + void setDownload(String download); } diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLDocument.java b/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLDocument.java index 43cbe816c..a08a850f3 100644 --- a/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLDocument.java +++ b/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLDocument.java @@ -73,4 +73,19 @@ public interface HTMLDocument extends Document, EventTarget { void execCommand(String commandName, boolean showDefaultUI, String valueArgument); void execCommand(String commandName); + + @JSProperty + String getCookie(); + + @JSProperty + void setCookie(String cookie); + + @JSProperty + String getTitle(); + + @JSProperty + void setTitle(String title); + + @JSProperty + HTMLElement getPointerLockElement(); } diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLElement.java b/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLElement.java index f68b58bdd..4669dc90a 100644 --- a/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLElement.java +++ b/jso/apis/src/main/java/org/teavm/jso/dom/html/HTMLElement.java @@ -186,4 +186,8 @@ public interface HTMLElement extends Element, ElementCSSInlineStyle, EventTarget @Override NodeList querySelectorAll(String selectors); + + void requestPointerLock(); + + void exitPointerLock(); } diff --git a/jso/apis/src/main/java/org/teavm/jso/websocket/CloseEvent.java b/jso/apis/src/main/java/org/teavm/jso/websocket/CloseEvent.java new file mode 100644 index 000000000..207743a76 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/websocket/CloseEvent.java @@ -0,0 +1,30 @@ +/* + * Copyright 2019 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.websocket; + +import org.teavm.jso.JSProperty; +import org.teavm.jso.dom.events.Event; + +public interface CloseEvent extends Event { + @JSProperty + int getCode(); + + @JSProperty + String getReason(); + + @JSProperty("wasClean") + boolean wasClean(); +} diff --git a/jso/apis/src/main/java/org/teavm/jso/websocket/WebSocket.java b/jso/apis/src/main/java/org/teavm/jso/websocket/WebSocket.java new file mode 100644 index 000000000..cbf906366 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/websocket/WebSocket.java @@ -0,0 +1,78 @@ +/* + * Copyright 2019 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.websocket; + +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; +import org.teavm.jso.dom.events.Event; +import org.teavm.jso.dom.events.EventListener; +import org.teavm.jso.dom.events.MessageEvent; + +public abstract class WebSocket implements JSObject { + @JSProperty("onclose") + public abstract void onClose(EventListener eventListener); + + @JSProperty("onerror") + public abstract void onError(EventListener eventListener); + + @JSProperty("onmessage") + public abstract void onMessage(EventListener eventListener); + + @JSProperty("onopen") + public abstract void onOpen(EventListener eventListener); + + @JSBody(params = "url", script = "return new WebSocket(url);") + public static native WebSocket create(String url); + + @JSBody(params = { "url", "protocols" }, script = "return new WebSocket(url, protocols);") + public static native WebSocket create(String url, String protocols); + + @JSBody(params = { "url", "protocols" }, script = "return new WebSocket(url, protocols);") + public static native WebSocket create(String url, String[] protocols); + + public abstract void close(); + + public abstract void close(int code); + + public abstract void close(int code, String reason); + + public abstract void send(String data); + + @JSProperty + public abstract String getBinaryType(); + + @JSProperty + public abstract void setBinaryType(String binaryType); + + @JSProperty + public abstract int getBufferedAmount(); + + @JSProperty + public abstract String getExtensions(); + + @JSProperty + public abstract String getProtocol(); + + @JSProperty + public abstract int getReadyState(); + + @JSProperty + public abstract String getUrl(); + + @JSBody(script = "return typeof WebSocket !== 'undefined';") + protected static native boolean isSupported(); +}