Merge branch 'implements-web-storage' of

https://github.com/jtakakura/teavm into jtakakura-implements-web-storage

Conflicts:
	teavm-dom/src/main/java/org/teavm/dom/browser/Window.java
This commit is contained in:
Alexey Andreev 2015-01-26 11:09:31 +04:00
commit a659447fbb
9 changed files with 388 additions and 2 deletions

View File

@ -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();
}

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.dom.browser;
import org.teavm.jso.JSProperty;
/**
*
* @author Junji Takakura
*/
public interface StorageProvider {
@JSProperty
Storage getSessionStorage();
@JSProperty
Storage getLocalStorage();
}

View File

@ -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();

View File

@ -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();
}

View File

@ -32,5 +32,6 @@
<modules>
<module>teavm-samples-hello</module>
<module>teavm-samples-benchmark</module>
<module>teavm-samples-storage</module>
</modules>
</project>

View File

@ -0,0 +1,110 @@
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.teavm</groupId>
<artifactId>teavm-samples</artifactId>
<version>0.3.0-SNAPSHOT</version>
</parent>
<artifactId>teavm-samples-storage</artifactId>
<packaging>war</packaging>
<name>TeaVM Web Storage web application</name>
<description>A sample application that demonstrate how to use Web Storage API.</description>
<dependencies>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-classlib</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-jso</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-dom</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/generated/js</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>web-client</id>
<phase>prepare-package</phase>
<goals>
<goal>build-javascript</goal>
</goals>
<configuration>
<targetDirectory>${project.build.directory}/generated/js/teavm</targetDirectory>
<mainClass>org.teavm.samples.storage.Application</mainClass>
<runtime>SEPARATE</runtime>
<minifying>false</minifying>
<debugInformationGenerated>true</debugInformationGenerated>
<sourceMapsGenerated>true</sourceMapsGenerated>
<sourceFilesCopied>true</sourceFilesCopied>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>../../checkstyle.xml</configLocation>
<propertyExpansion>config_loc=${basedir}/../..</propertyExpansion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>

View File

@ -0,0 +1,39 @@
<!--
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.
-->
<!DOCTYPE html>
<html>
<head>
<title>Web Storage web application</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" charset="utf-8" src="teavm/runtime.js"></script>
<script type="text/javascript" charset="utf-8" src="teavm/classes.js"></script>
</head>
<body onload="main()">
<h1>Web Storage web application</h1>
<input type="text" id="key">
<input type="text" id="value">
<button id="save-button">Save</button>
<button id="delete-button">Delete</button>
<button id="delete-all-button">Delete All</button>
<table border="1">
<tr>
<th>Key</th><th>Value</th>
</tr>
<tbody id="list">
</tbody>
</table>
</body>
</html>