From 2e4af24c99ae468f1bd9e6e5c8751dc53bfc2741 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 3 Apr 2015 18:51:03 +0400 Subject: [PATCH] Creating IndexedDB API wrappers --- .../org/teavm/dom/events/EventListener.java | 4 +- .../org/teavm/dom/events/EventTarget.java | 8 +- .../org/teavm/dom/html/HTMLBodyElement.java | 15 ++-- .../org/teavm/dom/indexeddb/EventHandler.java | 28 +++++++ .../teavm/dom/indexeddb/IDBCountRequest.java | 27 +++++++ .../org/teavm/dom/indexeddb/IDBCursor.java | 26 +++++++ .../teavm/dom/indexeddb/IDBCursorRequest.java | 27 +++++++ .../org/teavm/dom/indexeddb/IDBDatabase.java | 65 +++++++++++++++++ .../org/teavm/dom/indexeddb/IDBError.java | 28 +++++++ .../org/teavm/dom/indexeddb/IDBFactory.java | 48 ++++++++++++ .../teavm/dom/indexeddb/IDBGetRequest.java | 28 +++++++ .../org/teavm/dom/indexeddb/IDBIndex.java | 26 +++++++ .../teavm/dom/indexeddb/IDBObjectStore.java | 73 +++++++++++++++++++ .../indexeddb/IDBObjectStoreParameters.java | 45 ++++++++++++ .../teavm/dom/indexeddb/IDBOpenDBRequest.java | 31 ++++++++ .../org/teavm/dom/indexeddb/IDBRequest.java | 46 ++++++++++++ .../teavm/dom/indexeddb/IDBRequestSource.java | 25 +++++++ .../teavm/dom/indexeddb/IDBTransaction.java | 26 +++++++ .../dom/indexeddb/IDBVersionChangeEvent.java | 31 ++++++++ teavm-jso/src/main/java/org/teavm/jso/JS.java | 53 ++++++++++++++ 20 files changed, 647 insertions(+), 13 deletions(-) create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/EventHandler.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCountRequest.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursor.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursorRequest.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBDatabase.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBError.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBFactory.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBGetRequest.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBIndex.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStore.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStoreParameters.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBOpenDBRequest.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBRequest.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBRequestSource.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBTransaction.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBVersionChangeEvent.java diff --git a/teavm-dom/src/main/java/org/teavm/dom/events/EventListener.java b/teavm-dom/src/main/java/org/teavm/dom/events/EventListener.java index cc0b6c491..6dc21b923 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/events/EventListener.java +++ b/teavm-dom/src/main/java/org/teavm/dom/events/EventListener.java @@ -23,6 +23,6 @@ import org.teavm.jso.JSObject; * @author Alexey Andreev */ @JSFunctor -public interface EventListener extends JSObject { - void handleEvent(Event evt); +public interface EventListener extends JSObject { + void handleEvent(E evt); } diff --git a/teavm-dom/src/main/java/org/teavm/dom/events/EventTarget.java b/teavm-dom/src/main/java/org/teavm/dom/events/EventTarget.java index 0a6e229cf..e30a7cdad 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/events/EventTarget.java +++ b/teavm-dom/src/main/java/org/teavm/dom/events/EventTarget.java @@ -22,13 +22,13 @@ import org.teavm.jso.JSObject; * @author Alexey Andreev */ public interface EventTarget extends JSObject { - void addEventListener(String type, EventListener listener, boolean useCapture); + void addEventListener(String type, EventListener listener, boolean useCapture); - void addEventListener(String type, EventListener listener); + void addEventListener(String type, EventListener listener); - void removeEventListener(String type, EventListener listener, boolean useCapture); + void removeEventListener(String type, EventListener listener, boolean useCapture); - void removeEventListener(String type, EventListener listener); + void removeEventListener(String type, EventListener listener); boolean dispatchEvent(Event evt); } diff --git a/teavm-dom/src/main/java/org/teavm/dom/html/HTMLBodyElement.java b/teavm-dom/src/main/java/org/teavm/dom/html/HTMLBodyElement.java index bcd99ebff..2bc0a4b45 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/html/HTMLBodyElement.java +++ b/teavm-dom/src/main/java/org/teavm/dom/html/HTMLBodyElement.java @@ -15,6 +15,7 @@ */ package org.teavm.dom.html; +import org.teavm.dom.events.Event; import org.teavm.dom.events.EventListener; import org.teavm.jso.JSProperty; @@ -24,24 +25,24 @@ import org.teavm.jso.JSProperty; */ public interface HTMLBodyElement extends HTMLElement { @JSProperty("onbeforeunload") - void setOnBeforeUnload(EventListener listener); + void setOnBeforeUnload(EventListener listener); @JSProperty("onerror") - void setOnError(EventListener listener); + void setOnError(EventListener listener); @JSProperty("onload") - void setOnLoad(EventListener listener); + void setOnLoad(EventListener listener); @JSProperty("onmessage") - void setOnMessage(EventListener listener); + void setOnMessage(EventListener listener); @JSProperty("onoffline") - void setOnOffline(EventListener listener); + void setOnOffline(EventListener listener); @JSProperty("ononline") - void setOnOnline(EventListener listener); + void setOnOnline(EventListener listener); @JSProperty("ononunload") - void setOnUnload(EventListener listener); + void setOnUnload(EventListener listener); } diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/EventHandler.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/EventHandler.java new file mode 100644 index 000000000..19500ce4f --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/EventHandler.java @@ -0,0 +1,28 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.jso.JSFunctor; +import org.teavm.jso.JSObject; + +/** + * + * @author Alexey Andreev + */ +@JSFunctor +public interface EventHandler extends JSObject { + void handleEvent(); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCountRequest.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCountRequest.java new file mode 100644 index 000000000..c11a24bdb --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCountRequest.java @@ -0,0 +1,27 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public interface IDBCountRequest extends IDBRequest { + @JSProperty + int getResult(); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursor.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursor.java new file mode 100644 index 000000000..6eb09dfac --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursor.java @@ -0,0 +1,26 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.jso.JSObject; + +/** + * + * @author Alexey Andreev + */ +public interface IDBCursor extends JSObject { + +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursorRequest.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursorRequest.java new file mode 100644 index 000000000..4066b60e5 --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursorRequest.java @@ -0,0 +1,27 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public interface IDBCursorRequest extends IDBRequest { + @JSProperty + IDBCursor getResult(); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBDatabase.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBDatabase.java new file mode 100644 index 000000000..c9fbda5d7 --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBDatabase.java @@ -0,0 +1,65 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.dom.events.EventTarget; +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public interface IDBDatabase extends EventTarget { + String TRANSACTION_READONLY = "readonly"; + + String TRANSACTION_READWRITE = "readwrite"; + + String TRANSACTION_VERSIONCHANGE = "versionchange"; + + @JSProperty + String getName(); + + @JSProperty + int getVersion(); + + @JSProperty + String[] getObjectStoreNames(); + + IDBObjectStore createObjectStore(String name, IDBObjectStoreParameters optionalParameters); + + IDBObjectStore createObjectStore(String name); + + void deleteObjectStore(String name); + + IDBTransaction transaction(String storeName, String transactionMode); + + IDBTransaction transaction(String storeName); + + IDBTransaction transaction(String[] storeNames, String transactionMode); + + IDBTransaction transaction(String[] storeNames); + + void close(); + + @JSProperty("onabort") + void setOnAbort(EventHandler handler); + + @JSProperty("onerror") + void setOnError(EventHandler handler); + + @JSProperty("onversionchange") + void setOnVersionChange(EventHandler handler); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBError.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBError.java new file mode 100644 index 000000000..16fd19f2f --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBError.java @@ -0,0 +1,28 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public abstract class IDBError implements JSObject { + @JSProperty + public abstract String getName(); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBFactory.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBFactory.java new file mode 100644 index 000000000..b8e5199d4 --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBFactory.java @@ -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.dom.indexeddb; + +import org.teavm.jso.JS; +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; + +/** + * + * @author Alexey Andreev + */ +public abstract class IDBFactory implements JSObject { + public static boolean isSupported() { + return !JS.isUndefined(getInstanceImpl()); + } + + public static IDBFactory getInstance() { + IDBFactory factory = getInstanceImpl(); + if (JS.isUndefined(factory)) { + throw new IllegalStateException("IndexedDB is not supported in this browser"); + } + return factory; + } + + @JSBody(params = {}, script = "return window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || " + + "window.msIndexedDB;") + static native IDBFactory getInstanceImpl(); + + public abstract IDBOpenDBRequest open(String name, int version); + + public abstract IDBOpenDBRequest deleteDatabase(String name); + + public abstract int cmp(JSObject a, JSObject b); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBGetRequest.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBGetRequest.java new file mode 100644 index 000000000..60ef72e62 --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBGetRequest.java @@ -0,0 +1,28 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public interface IDBGetRequest extends IDBRequest { + @JSProperty + JSObject getResult(); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBIndex.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBIndex.java new file mode 100644 index 000000000..c2f459efe --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBIndex.java @@ -0,0 +1,26 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.jso.JSObject; + +/** + * + * @author Alexey Andreev + */ +public interface IDBIndex extends JSObject { + +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStore.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStore.java new file mode 100644 index 000000000..4f5ae28be --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStore.java @@ -0,0 +1,73 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.jso.*; + +/** + * + * @author Alexey Andreev + */ +public abstract class IDBObjectStore implements JSObject { + @JSProperty + public abstract String getName(); + + @JSProperty("keyPath") + abstract JSObject getKeyPathImpl(); + + public final String[] getKeyPath() { + JSObject result = getKeyPathImpl(); + if (JS.getType(result) == JSType.STRING) { + return new String[] { JS.unwrapString(result) }; + } else { + return JS.unwrapArray((JSStringArray)result); + } + } + + @JSProperty + public abstract String[] getIndexNames(); + + @JSProperty + public abstract boolean isAutoIncrement(); + + public abstract IDBRequest put(JSObject value, JSObject key); + + public abstract IDBRequest put(JSObject value); + + public abstract IDBRequest add(JSObject value, JSObject key); + + public abstract IDBRequest add(JSObject value); + + public abstract IDBRequest delete(JSObject key); + + public abstract IDBGetRequest get(JSObject key); + + public abstract IDBRequest clear(); + + public abstract IDBCursorRequest openCursor(); + + public abstract IDBIndex createIndex(String name, String key); + + public abstract IDBIndex createIndex(String name, String[] keys); + + public abstract IDBIndex index(String name); + + public abstract void deleteIndex(String name); + + public abstract IDBCountRequest count(); + + public abstract IDBCountRequest count(JSObject key); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStoreParameters.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStoreParameters.java new file mode 100644 index 000000000..a25ea5141 --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStoreParameters.java @@ -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.dom.indexeddb; + +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public abstract class IDBObjectStoreParameters implements JSObject { + @JSBody(params = "", script = "return {};") + public static native IDBObjectStoreParameters create(); + + public final IDBObjectStoreParameters keyPath(String... keys) { + setKeyPath(keys); + return this; + } + + public final IDBObjectStoreParameters autoIncrement(boolean autoIncrement) { + setAutoIncrement(autoIncrement); + return this; + } + + @JSProperty + abstract void setKeyPath(String[] keys); + + @JSProperty + abstract void setAutoIncrement(boolean autoIncrement); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBOpenDBRequest.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBOpenDBRequest.java new file mode 100644 index 000000000..0e737d210 --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBOpenDBRequest.java @@ -0,0 +1,31 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.dom.events.EventListener; +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public interface IDBOpenDBRequest extends IDBRequest { + @JSProperty + void setOnBlocked(EventHandler handler); + + @JSProperty("onupgradeneeded") + void setOnUpgradeNeeded(EventListener listener); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBRequest.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBRequest.java new file mode 100644 index 000000000..af3cd608f --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBRequest.java @@ -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.dom.indexeddb; + +import org.teavm.dom.events.EventTarget; +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public interface IDBRequest extends EventTarget { + String STATE_PENDING = "pending"; + String STATE_DONE = "done"; + + @JSProperty + IDBError getError(); + + @JSProperty + IDBRequestSource getSource(); + + @JSProperty + IDBTransaction getTransaction(); + + @JSProperty + String getReadyState(); + + @JSProperty("onerror") + void setOnError(EventHandler handler); + + @JSProperty("onsuccess") + void setOnSuccess(EventHandler handler); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBRequestSource.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBRequestSource.java new file mode 100644 index 000000000..47d9d13ba --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBRequestSource.java @@ -0,0 +1,25 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.jso.JSObject; + +/** + * + * @author Alexey Andreev + */ +public interface IDBRequestSource extends JSObject { +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBTransaction.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBTransaction.java new file mode 100644 index 000000000..8f196f221 --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBTransaction.java @@ -0,0 +1,26 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.jso.JSObject; + +/** + * + * @author Alexey Andreev + */ +public interface IDBTransaction extends JSObject { + +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBVersionChangeEvent.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBVersionChangeEvent.java new file mode 100644 index 000000000..7ebfe048e --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBVersionChangeEvent.java @@ -0,0 +1,31 @@ +/* + * 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.dom.indexeddb; + +import org.teavm.dom.events.Event; +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public interface IDBVersionChangeEvent extends Event { + @JSProperty + int getOldVersion(); + + @JSProperty + int getNewVersion(); +} diff --git a/teavm-jso/src/main/java/org/teavm/jso/JS.java b/teavm-jso/src/main/java/org/teavm/jso/JS.java index a0dc42cde..4811fe1fa 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/JS.java +++ b/teavm-jso/src/main/java/org/teavm/jso/JS.java @@ -15,6 +15,7 @@ */ package org.teavm.jso; +import java.lang.reflect.Array; import java.util.Iterator; import org.teavm.dependency.PluggableDependency; import org.teavm.javascript.spi.InjectedBy; @@ -347,6 +348,58 @@ public final class JS { @InjectedBy(JSNativeGenerator.class) public static native char unwrapCharacter(JSObject obj); + public static T[] unwrapArray(Class type, JSArray array) { + @SuppressWarnings("unchecked") + T[] result = (T[])Array.newInstance(type, array.getLength()); + for (int i = 0; i < result.length; ++i) { + result[i] = array.get(i); + } + return result; + } + + public static T[][] unwrapArray2(Class type, JSArray> array) { + @SuppressWarnings("unchecked") + T[][] result = (T[][])Array.newInstance(Array.newInstance(type, 0).getClass(), array.getLength()); + for (int i = 0; i < result.length; ++i) { + result[i] = unwrapArray(type, array.get(i)); + } + return result; + } + + public static T[][][] unwrapArray3(Class type, JSArray>> array) { + Class baseType = Array.newInstance(type, 0).getClass(); + @SuppressWarnings("unchecked") + T[][][] result = (T[][][])Array.newInstance(Array.newInstance(baseType, 0).getClass(), array.getLength()); + for (int i = 0; i < result.length; ++i) { + result[i] = unwrapArray2(type, array.get(i)); + } + return result; + } + + public static String[] unwrapArray(JSStringArray array) { + String[] result = new String[array.getLength()]; + for (int i = 0; i < result.length; ++i) { + result[i] = array.get(i); + } + return result; + } + + public static String[][] unwrapArray2(JSArray array) { + String[][] result = new String[array.getLength()][]; + for (int i = 0; i < result.length; ++i) { + result[i] = unwrapArray(array.get(i)); + } + return result; + } + + public static String[][][] unwrapArray3(JSArray> array) { + String[][][] result = new String[array.getLength()][][]; + for (int i = 0; i < result.length; ++i) { + result[i] = unwrapArray2(array.get(i)); + } + return result; + } + @InjectedBy(JSNativeGenerator.class) public static native boolean isUndefined(JSObject obj);