diff --git a/teavm-dom/src/main/java/org/teavm/dom/browser/Storage.java b/teavm-dom/src/main/java/org/teavm/dom/browser/Storage.java new file mode 100644 index 000000000..375129116 --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/browser/Storage.java @@ -0,0 +1,39 @@ +/* + * 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.browser; + +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; + +/** + * + * @author Junji Takakura + */ +public interface Storage extends JSObject { + + @JSProperty + int getLength(); + + String key(int index); + + String getItem(String key); + + void setItem(String key, String value); + + void removeItem(String key); + + void clear(); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/browser/StorageProvider.java b/teavm-dom/src/main/java/org/teavm/dom/browser/StorageProvider.java new file mode 100644 index 000000000..cedc9c3a6 --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/browser/StorageProvider.java @@ -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.dom.browser; + +import org.teavm.jso.JSProperty; + +/** + * + * @author Junji Takakura + */ +public interface StorageProvider { + @JSProperty + Storage getSessionStorage(); + + @JSProperty + Storage getLocalStorage(); +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/browser/Window.java b/teavm-dom/src/main/java/org/teavm/dom/browser/Window.java index 0e0243eb1..de54a754d 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/browser/Window.java +++ b/teavm-dom/src/main/java/org/teavm/dom/browser/Window.java @@ -16,7 +16,6 @@ package org.teavm.dom.browser; import org.teavm.dom.ajax.XMLHttpRequest; -import org.teavm.dom.events.EventTarget; import org.teavm.dom.html.HTMLDocument; import org.teavm.dom.json.JSON; import org.teavm.dom.typedarrays.*; @@ -29,7 +28,7 @@ import org.teavm.jso.JSProperty; * * @author Alexey Andreev */ -public interface Window extends JSGlobal, EventTarget { +public interface Window extends JSGlobal, StorageProvider { @JSProperty HTMLDocument getDocument(); diff --git a/teavm-dom/src/main/java/org/teavm/dom/events/StorageEvent.java b/teavm-dom/src/main/java/org/teavm/dom/events/StorageEvent.java new file mode 100644 index 000000000..707761864 --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/events/StorageEvent.java @@ -0,0 +1,41 @@ +/* + * 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. + */ +package org.teavm.dom.events; + +import org.teavm.dom.browser.Storage; +import org.teavm.jso.JSProperty; + +/** + * + * @author Junji Takakura + */ +public interface StorageEvent extends Event { + + @JSProperty + String getKey(); + + @JSProperty + String getOldValue(); + + @JSProperty + String getNewValue(); + + @JSProperty + String getUrl(); + + @JSProperty + Storage getStorageArea(); +} diff --git a/teavm-samples/pom.xml b/teavm-samples/pom.xml index 1b9dc40c4..6795fb884 100644 --- a/teavm-samples/pom.xml +++ b/teavm-samples/pom.xml @@ -32,5 +32,6 @@ teavm-samples-hello teavm-samples-benchmark + teavm-samples-storage \ No newline at end of file diff --git a/teavm-samples/teavm-samples-storage/pom.xml b/teavm-samples/teavm-samples-storage/pom.xml new file mode 100644 index 000000000..edfa3d4e1 --- /dev/null +++ b/teavm-samples/teavm-samples-storage/pom.xml @@ -0,0 +1,110 @@ + + + 4.0.0 + + + org.teavm + teavm-samples + 0.3.0-SNAPSHOT + + teavm-samples-storage + + war + + TeaVM Web Storage web application + A sample application that demonstrate how to use Web Storage API. + + + + org.teavm + teavm-classlib + ${project.version} + + + org.teavm + teavm-jso + ${project.version} + + + org.teavm + teavm-dom + ${project.version} + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + + + + maven-war-plugin + 2.4 + + + + ${project.build.directory}/generated/js + + + + + + org.teavm + teavm-maven-plugin + ${project.version} + + + web-client + prepare-package + + build-javascript + + + ${project.build.directory}/generated/js/teavm + org.teavm.samples.storage.Application + SEPARATE + false + true + true + true + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + ../../checkstyle.xml + config_loc=${basedir}/../.. + + + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + \ No newline at end of file diff --git a/teavm-samples/teavm-samples-storage/src/main/java/org/teavm/samples/storage/Application.java b/teavm-samples/teavm-samples-storage/src/main/java/org/teavm/samples/storage/Application.java new file mode 100644 index 000000000..b14cfc3a5 --- /dev/null +++ b/teavm-samples/teavm-samples-storage/src/main/java/org/teavm/samples/storage/Application.java @@ -0,0 +1,106 @@ +/* + * 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.samples.storage; + +import org.teavm.dom.browser.Storage; +import org.teavm.dom.browser.Window; +import org.teavm.dom.events.Event; +import org.teavm.dom.events.EventListener; +import org.teavm.dom.html.HTMLButtonElement; +import org.teavm.dom.html.HTMLDocument; +import org.teavm.dom.html.HTMLElement; +import org.teavm.dom.html.HTMLInputElement; +import org.teavm.jso.JS; + +/** +* +* @author Junji Takakura +*/ +public final class Application { + + private static Window window = (Window)JS.getGlobal(); + private static HTMLDocument document = window.getDocument(); + private static Storage storage = window.getSessionStorage(); + + private Application() { + } + + public static void main(String[] args) { + if (storage == null) { + window.alert("storage is not supported."); + } + + HTMLButtonElement saveButton = (HTMLButtonElement)document.getElementById("save-button"); + saveButton.addEventListener("click", new EventListener() { + @Override + public void handleEvent(Event evt) { + String key = ((HTMLInputElement)document.getElementById("key")).getValue(); + String value = ((HTMLInputElement)document.getElementById("value")).getValue(); + + if (key != null && key.length() > 0 && value != null && value.length() > 0) { + storage.setItem(key, value); + draw(); + } + } + }); + HTMLButtonElement deleteButton = (HTMLButtonElement)document.getElementById("delete-button"); + deleteButton.addEventListener("click", new EventListener() { + @Override + public void handleEvent(Event evt) { + String key = ((HTMLInputElement)document.getElementById("key")).getValue(); + + if (key != null && key.length() > 0) { + storage.removeItem(key); + draw(); + } + } + }); + HTMLButtonElement deleteAllButton = (HTMLButtonElement)document.getElementById("delete-all-button"); + deleteAllButton.addEventListener("click", new EventListener() { + @Override + public void handleEvent(Event evt) { + storage.clear(); + draw(); + } + }); + draw(); + } + + private static void draw() { + HTMLElement tbody = document.getElementById("list"); + + while (tbody.getFirstChild() != null) { + tbody.removeChild(tbody.getFirstChild()); + } + + for (int i = 0; i < storage.getLength(); i++) { + 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); + + tbody.appendChild(tr); + } + } +} diff --git a/teavm-samples/teavm-samples-storage/src/main/webapp/WEB-INF/web.xml b/teavm-samples/teavm-samples-storage/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 000000000..bfc410b12 --- /dev/null +++ b/teavm-samples/teavm-samples-storage/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,21 @@ + + + + \ No newline at end of file diff --git a/teavm-samples/teavm-samples-storage/src/main/webapp/index.html b/teavm-samples/teavm-samples-storage/src/main/webapp/index.html new file mode 100644 index 000000000..a961da445 --- /dev/null +++ b/teavm-samples/teavm-samples-storage/src/main/webapp/index.html @@ -0,0 +1,39 @@ + + + + + Web Storage web application + + + + + +

Web Storage web application

+ + + + + + + + + + + +
KeyValue
+ + \ No newline at end of file