add browser-example-with-library maven project (kotlin->javascript)

This commit is contained in:
Michael Nedzelsky
2014-11-08 21:53:02 +03:00
parent 6c55e12588
commit 85a16c3c51
7 changed files with 9178 additions and 0 deletions
@@ -0,0 +1,15 @@
## Sample Application
This (really simple ;) application shows how to use Kotlin and the maven plugin to generate JavaScript and invoke it from inside a HTML web page.
The source [Hello.kt](https://github.com/JetBrains/kotlin/blob/master/libraries/examples/browser-example-with-library/src/main/kotlin/sample/Hello.kt) uses the [kotlin.browser](http://jetbrains.github.com/kotlin/versions/snapshot/apidocs/kotlin/browser/package-summary.html) API to access the *document* property to modify the HTML.
### Running the sample in a web browser
To run the example try:
cd libraries/examples/browser-example-with-library
mvn install
open sample.html
This should open a browser which then shows some simple HTML which then includes some dynamically generated content.
@@ -0,0 +1,118 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>browser-example-with-library</artifactId>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-js-library</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-js-library-example</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${project.version}</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>${selenium.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>js</id>
<goals>
<goal>js</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-js-library</artifactId>
<version>${project.version}</version>
<outputDirectory>${project.build.directory}/js</outputDirectory>
<includes>*.js</includes>
</artifactItem>
<artifactItem>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-js-library-example</artifactId>
<version>${project.version}</version>
<outputDirectory>${project.build.directory}/js</outputDirectory>
<includes>*.js</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,25 @@
<html>
<head>
</head>
<body>
<h1>Kotlin Sample</h1>
<div id="foo">
</div>
<script type="text/javascript" src="src/js/jquery.js"></script>
<script src="target/js/kotlin.js"></script>
<script src="target/js/kotlin-js-library-example.js"></script>
<script type="text/javascript" src="target/js/browser-example-with-library.js"></script>
<!--
TODO ideally we should be able to code generate this entry point if we can discover
any annotated methods with documentReady
-->
<script type="text/javascript">
$(document).ready(Kotlin.modules['browser-example-with-library'].sample.myApp)
</script>
</body>
</html>
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,16 @@
package sample
import kotlin.Pair
import kotlin.browser.document
import library.sample.*
fun myApp() {
val element = document.getElementById("foo")
if (element != null) {
val p = Pair(10, 20)
val x = pairAdd(p)
val y = pairMul(p)
val z = IntHolder(100).value
element.appendChild(document.createTextNode("x=$x y=$y z=$z")!!)
}
}
@@ -0,0 +1,22 @@
package test.sample
import org.junit.Test as test
import org.openqa.selenium.WebDriver
import org.openqa.selenium.htmlunit.HtmlUnitDriver
import org.openqa.selenium.By
import java.io.File
import kotlin.test.*
open class SampleTest {
open val driver: WebDriver = HtmlUnitDriver(true)
test fun homePage(): Unit {
driver.get("file://" + File("sample.html").getCanonicalPath())
Thread.sleep(1000)
val foo = driver.findElement(By.id("foo"))!!
val text = foo.getText() ?: ""
println("Found $foo with text '$text'")
assertEquals("x=30 y=200 z=100", text.trim())
}
}
+1
View File
@@ -103,6 +103,7 @@
<module>examples/js-example</module>
<module>examples/kotlin-js-library-example</module>
<module>examples/browser-example</module>
<module>examples/browser-example-with-library</module>
</modules>
<dependencies>