From ae68bc366b363b97068bbb96ad4e274c80070150 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 28 Nov 2017 22:48:54 +0300 Subject: [PATCH] Add some JS API declarations --- .../java/org/teavm/jso/browser/Navigator.java | 2 +- .../org/teavm/jso/browser/Performance.java | 4 +-- .../java/org/teavm/jso/browser/Window.java | 4 ++- .../org/teavm/jso/dom/events/ErrorEvent.java | 35 ++++++++++++++++++ .../org/teavm/jso/dom/html/HTMLDocument.java | 7 ++++ .../org/teavm/jso/dom/html/HTMLElement.java | 6 ++++ .../java/org/teavm/jso/dom/xml/Document.java | 4 +++ .../java/org/teavm/jso/dom/xml/Element.java | 4 +++ .../org/teavm/jso/workers/AbstractWorker.java | 27 ++++++++++++++ .../org/teavm/jso/workers/MessagePort.java | 36 +++++++++++++++++++ .../org/teavm/jso/workers/SharedWorker.java | 27 ++++++++++++++ .../java/org/teavm/jso/workers/Worker.java | 34 ++++++++++++++++++ 12 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 jso/apis/src/main/java/org/teavm/jso/dom/events/ErrorEvent.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/workers/AbstractWorker.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/workers/MessagePort.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/workers/SharedWorker.java create mode 100644 jso/apis/src/main/java/org/teavm/jso/workers/Worker.java 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 0d5a381f9..854505c82 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 @@ -21,6 +21,6 @@ public final class Navigator { private Navigator() { } - @JSBody(script = "return window.navigator.onLine;") + @JSBody(script = "return (window || self).navigator.onLine;") public static native boolean isOnline(); } diff --git a/jso/apis/src/main/java/org/teavm/jso/browser/Performance.java b/jso/apis/src/main/java/org/teavm/jso/browser/Performance.java index fd98beb09..37c2b7009 100644 --- a/jso/apis/src/main/java/org/teavm/jso/browser/Performance.java +++ b/jso/apis/src/main/java/org/teavm/jso/browser/Performance.java @@ -22,9 +22,9 @@ public final class Performance implements JSObject { private Performance() { } - @JSBody(script = "return window.performance.now();") + @JSBody(script = "return (window || self).performance.now();") public static native double now(); - @JSBody(script = "return typeof(window.performance) !== 'undefined';") + @JSBody(script = "return typeof((window || self).performance) !== 'undefined';") public static native boolean isSupported(); } 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 c46eacee7..a2a370412 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 @@ -149,6 +149,8 @@ public abstract class Window implements JSObject, WindowEventTarget, StorageProv public abstract void stop(); + public abstract void postMessage(JSObject message); + public abstract void postMessage(JSObject message, String targetOrigin); public abstract void postMessage(JSObject message, String targetOrigin, JSArrayReader transfer); @@ -157,7 +159,7 @@ public abstract class Window implements JSObject, WindowEventTarget, StorageProv postMessage(message, targetOrigin, JSArray.of(transfer)); } - @JSBody(script = "return window;") + @JSBody(script = "return window || self;") public static native Window current(); @JSBody(params = "uri", script = "return encodeURI(uri);") diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/events/ErrorEvent.java b/jso/apis/src/main/java/org/teavm/jso/dom/events/ErrorEvent.java new file mode 100644 index 000000000..063a485c6 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/dom/events/ErrorEvent.java @@ -0,0 +1,35 @@ +/* + * Copyright 2017 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.JSProperty; + +public interface ErrorEvent extends Event { + @JSProperty + String getMessage(); + + @JSProperty + String getFileName(); + + @JSProperty("lineno") + int getLineNumber(); + + @JSProperty("colno") + int getColumnNumber(); + + @JSProperty + String getError(); +} 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 df20908d6..3f90b2830 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 @@ -20,6 +20,7 @@ import org.teavm.jso.JSProperty; import org.teavm.jso.browser.Window; import org.teavm.jso.dom.events.EventTarget; import org.teavm.jso.dom.xml.Document; +import org.teavm.jso.dom.xml.NodeList; public interface HTMLDocument extends Document, EventTarget { @JSProperty @@ -53,4 +54,10 @@ public interface HTMLDocument extends Document, EventTarget { static HTMLDocument current() { return Window.current().getDocument(); } + + @Override + HTMLElement querySelector(String selectors); + + @Override + NodeList querySelectorAll(String selectors); } 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 25055f0ee..2ea49fb87 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 @@ -159,4 +159,10 @@ public interface HTMLElement extends Element, ElementCSSInlineStyle, EventTarget clear().appendChild(getOwnerDocument().createTextNode(content)); return this; } + + @Override + HTMLElement querySelector(String selectors); + + @Override + NodeList querySelectorAll(String selectors); } diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/xml/Document.java b/jso/apis/src/main/java/org/teavm/jso/dom/xml/Document.java index dc3ca6c8d..88800f72f 100644 --- a/jso/apis/src/main/java/org/teavm/jso/dom/xml/Document.java +++ b/jso/apis/src/main/java/org/teavm/jso/dom/xml/Document.java @@ -54,4 +54,8 @@ public interface Document extends Node { NodeList getElementsByTagNameNS(String namespaceURI, String localName); Element getElementById(String elementId); + + Element querySelector(String selectors); + + NodeList querySelectorAll(String selectors); } diff --git a/jso/apis/src/main/java/org/teavm/jso/dom/xml/Element.java b/jso/apis/src/main/java/org/teavm/jso/dom/xml/Element.java index f74ac0bef..eab7a3c05 100644 --- a/jso/apis/src/main/java/org/teavm/jso/dom/xml/Element.java +++ b/jso/apis/src/main/java/org/teavm/jso/dom/xml/Element.java @@ -47,4 +47,8 @@ public interface Element extends Node { boolean hasAttribute(String name); boolean hasAttributeNS(String namespaceURI, String localName); + + Element querySelector(String selectors); + + NodeList querySelectorAll(String selectors); } diff --git a/jso/apis/src/main/java/org/teavm/jso/workers/AbstractWorker.java b/jso/apis/src/main/java/org/teavm/jso/workers/AbstractWorker.java new file mode 100644 index 000000000..223ec17da --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/workers/AbstractWorker.java @@ -0,0 +1,27 @@ +/* + * Copyright 2017 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.workers; + +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; +import org.teavm.jso.dom.events.ErrorEvent; +import org.teavm.jso.dom.events.EventListener; +import org.teavm.jso.dom.events.EventTarget; + +public interface AbstractWorker extends JSObject, EventTarget { + @JSProperty("onerror") + void onError(EventListener listener); +} diff --git a/jso/apis/src/main/java/org/teavm/jso/workers/MessagePort.java b/jso/apis/src/main/java/org/teavm/jso/workers/MessagePort.java new file mode 100644 index 000000000..2bad1ed89 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/workers/MessagePort.java @@ -0,0 +1,36 @@ +/* + * Copyright 2017 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.workers; + +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; +import org.teavm.jso.dom.events.EventListener; +import org.teavm.jso.dom.events.EventTarget; +import org.teavm.jso.dom.events.MessageEvent; + +public interface MessagePort extends EventTarget { + void postMessage(JSObject message); + + void start(); + + void close(); + + @JSProperty("onmessage") + void onMessage(EventListener message); + + @JSProperty("onmessageerror") + void onMessageError(EventListener message); +} diff --git a/jso/apis/src/main/java/org/teavm/jso/workers/SharedWorker.java b/jso/apis/src/main/java/org/teavm/jso/workers/SharedWorker.java new file mode 100644 index 000000000..c8165d280 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/workers/SharedWorker.java @@ -0,0 +1,27 @@ +/* + * Copyright 2017 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.workers; + +import org.teavm.jso.JSBody; +import org.teavm.jso.JSProperty; + +public abstract class SharedWorker implements AbstractWorker { + @JSBody(params = "url", script = "return new SharedWorker(url);") + public static native Worker create(String url); + + @JSProperty + public abstract MessagePort getPort(); +} diff --git a/jso/apis/src/main/java/org/teavm/jso/workers/Worker.java b/jso/apis/src/main/java/org/teavm/jso/workers/Worker.java new file mode 100644 index 000000000..ea00af0a5 --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/workers/Worker.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 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.workers; + +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; +import org.teavm.jso.dom.events.EventListener; +import org.teavm.jso.dom.events.MessageEvent; + +public abstract class Worker implements AbstractWorker { + @JSBody(params = "url", script = "return new Worker(url);") + public static native Worker create(String url); + + @JSProperty("onmessage") + public abstract void onMessage(EventListener listener); + + public abstract void postMessage(JSObject message); + + public abstract void terminate(); +}