Skip to content

Commit c1c3fcb

Browse files
schettino72Li Haoyi
authored and
Li Haoyi
committed
Migrated readme to use Scalatex, added a lot more examples. Use readme/run to build the readme, and then push the contents out readme/target/output/ to the gh-pages branch
1 parent ea2ad4b commit c1c3fcb

File tree

8 files changed

+415
-89
lines changed

8 files changed

+415
-89
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: scala
22
script:
33
- sbt ++$TRAVIS_SCALA_VERSION package
4+
- sbt ++2.11.4 readme/run
45
scala:
56
- 2.10.4
67
- 2.11.2

LICENSE

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Documentation marked "MDN" is thanks to Mozilla Contributors
2+
at https://developer.mozilla.org/en-US/docs/Web/API and available
3+
under the Creative Commons Attribution-ShareAlike v2.5 or later.
4+
http://creativecommons.org/licenses/by-sa/2.5/
5+
6+
This should not affect you, as a user of the library, as long as you don't
7+
modify these MDN materials (e.g. you leave them as is, or replace them
8+
wholesale). In particular, packaging this library with MDN materials unchanged
9+
is fine for any purpose (including commercial) according to the
10+
CC-Attribute-ShareAlike License.
11+
12+
Everything else (e.g. the code) is under the MIT License
13+
14+
The MIT License (MIT)
15+
16+
Copyright (c) 2013 Li Haoyi
17+
18+
Permission is hereby granted, free of charge, to any person obtaining a copy
19+
of this software and associated documentation files (the "Software"), to deal
20+
in the Software without restriction, including without limitation the rights
21+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22+
copies of the Software, and to permit persons to whom the Software is
23+
furnished to do so, subject to the following conditions:
24+
25+
The above copyright notice and this permission notice shall be included in
26+
all copies or substantial portions of the Software.
27+
28+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34+
THE SOFTWARE.

README.md

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1 @@
1-
scala-js-dom
2-
============
3-
4-
Scala-js-dom provides a nice statically typed interface to the DOM such that it can be called from Scala code without resorting to `js.Dynamic`. All javascript globals functions, singletons and classes are members of the `org.scalajs.dom` package, e.g.
5-
6-
```scala
7-
import org.scalajs.dom
8-
dom.alert("HAI")
9-
```
10-
11-
Will cause a javascript alert box saying `HAI` to appear. Other javascript classes and objects can be similarly accessed e.g. `new dom.XMLHttpRequest()` to perform a new Ajax request, `dom.document` to access the global `document` object, or `dom.setInterval(() => ???, 1000)` to schedule a recurring task every second. The names of these functions/singletons/classes match their Javascript equivalents exactly, so if you want to know how to do something (e.g. "how do I open a websocket connection") the syntax is the same as if you were using raw Javascript.
12-
13-
The goal of this project is to provide a thin-but-idiomatic-scala interface to modern browser APIs. In particular:
14-
15-
- Deprecated properties/methods/types will not be present.
16-
- IE-only, Chrome-only, FF-only, and in general browser-specific attributes will not be present.
17-
- The name of a Scala type should map directly to the name of the corresponding Javascript type.
18-
- Any type which is a Javascript type (e.g. you can `instanceof` in javascript) should be a Scala `class`; any other interface which isn't a Javascript type should be a `trait`.
19-
- Read-only members should be `def`, and not-directly-instantiable classes should have `private` constructors.
20-
21-
Extensions
22-
----------
23-
24-
Apart from `Color`, Scala-js-dom contains some useful helpers and implicit classes in `org.scalajs.dom.extensions` that serve no purpose other than to make your use of the DOM more pleasant.
25-
Examples include the `Ajax.get` and `Ajax.post` methods which let you avoid messing with `dom.XMLHttpRequest` directly, or `KeyCodes` which provides a nice list of the keycodes that result from pressing various keys on the keyboard.
26-
27-
Usage
28-
-----
29-
30-
Add the following to your sbt build definition:
31-
32-
```scala
33-
libraryDependencies += "org.scala-lang.modules.scalajs" %%% "scalajs-dom" % "0.6"
34-
```
35-
36-
then enjoy the types available in `org.scalajs.dom`.
37-
38-
scalajs-dom 0.6 is built and published for Scala.js 0.5.0 and following in
39-
the 0.5.x series, with both Scala 2.10 and 2.11.
40-
41-
See also [roll](https://github.com/lihaoyi/roll) ([live demo](http://lihaoyi.github.io/roll/)) and [scala-js-games](https://github.com/lihaoyi/scala-js-games) for an example of its use. [Scala-js-fiddle](http://www.scala-js-fiddle.com/) also contains a pile of [fun examples](http://www.scala-js-fiddle.com/gist/9405209/Oscilloscope.scala) that demonstrate its usage. Pull requests/forks are welcome!
42-
43-
Contributing
44-
------------
45-
46-
Scala-js-dom is a work in progress. The current code base is a hodgepodge of auto-generated/scraped/hand-tweaked code, and is full of rough edges. If you see something that you think can be improved, feel free to send a pull request. These could include:
47-
48-
- Improved doc-comments; who doesn't love better docs?
49-
- Missing methods/properties/classes; send the PR adding it in including it together with a link to an authoritative source (e.g. MDN) and it should get merged.
50-
- Additional extensions (in `org.scalajs.dom.extensions`). These currently represent an arbitrary collection of helpers that have been needed so far. If there's some implicit that you find you need and you think other people will to, send a pull request and we can talk about it.
51-
52-
License
53-
-------
54-
55-
Documentation marked "MDN" is thanks to Mozilla Contributors
56-
at https://developer.mozilla.org/en-US/docs/Web/API and available
57-
under the Creative Commons Attribution-ShareAlike v2.5 or later.
58-
http://creativecommons.org/licenses/by-sa/2.5/
59-
60-
This should not affect you, as a user of the library, as long as you don't
61-
modify these MDN materials (e.g. you leave them as is, or replace them
62-
wholesale). In particular, packaging this library with MDN materials unchanged
63-
is fine for any purpose (including commercial) according to the
64-
CC-Attribute-ShareAlike License.
65-
66-
Everything else (e.g. the code) is under the MIT License
67-
68-
The MIT License (MIT)
69-
70-
Copyright (c) 2013 Li Haoyi
71-
72-
Permission is hereby granted, free of charge, to any person obtaining a copy
73-
of this software and associated documentation files (the "Software"), to deal
74-
in the Software without restriction, including without limitation the rights
75-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
76-
copies of the Software, and to permit persons to whom the Software is
77-
furnished to do so, subject to the following conditions:
78-
79-
The above copyright notice and this permission notice shall be included in
80-
all copies or substantial portions of the Software.
81-
82-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
83-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
84-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
85-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
86-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
87-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
88-
THE SOFTWARE.
1+
[Documentation](http://scala-js.github.io/scala-js-dom)

build.sbt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,16 @@ pomExtra := (
5555
)
5656

5757
pomIncludeRepository := { _ => false }
58+
59+
lazy val readme = project
60+
.settings(scalatex.SbtPlugin.projectSettings:_*)
61+
.settings(
62+
scalaVersion := "2.11.4",
63+
libraryDependencies += "com.lihaoyi" %% "scalatex-site" % "0.1.0",
64+
(resources in Compile) += (fullOptJS in (example, Compile)).value.data
65+
)
66+
67+
lazy val example = project
68+
.dependsOn(root)
69+
.enablePlugins(ScalaJSPlugin)
70+
.settings(scalaVersion := "2.11.2")
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package example
2+
3+
import scala.scalajs.js.JSApp
4+
5+
import org.scalajs.dom
6+
7+
import scala.scalajs.js.annotation.JSExport
8+
9+
@JSExport
10+
object Alert {
11+
@JSExport
12+
def main() = {
13+
import org.scalajs.dom
14+
dom.alert("Hi from Scala-js-dom")
15+
}
16+
}
17+
18+
@JSExport
19+
object NodeAppendChild {
20+
@JSExport
21+
def main(div: dom.HTMLDivElement) = {
22+
val child = dom.document
23+
.createElement("div")
24+
25+
child.textContent =
26+
"Hi from Scala-js-dom"
27+
28+
div.appendChild(child)
29+
}
30+
}
31+
32+
@JSExport
33+
object ElementStyle {
34+
@JSExport
35+
def main(div: dom.HTMLDivElement) = {
36+
val colors = Seq(
37+
"red", "green", "blue"
38+
)
39+
40+
val index =
41+
util.Random.nextInt(colors.length)
42+
43+
div.style.color = colors(index)
44+
}
45+
}
46+
47+
@JSExport
48+
object LocalStorage {
49+
@JSExport
50+
def main(in: dom.HTMLInputElement,
51+
box: dom.HTMLDivElement) = {
52+
val key = "my-key"
53+
54+
in.value =
55+
dom.localStorage.getItem(key)
56+
57+
in.onkeyup = (e: dom.Event) => {
58+
dom.localStorage.setItem(
59+
key, in.value
60+
)
61+
box.textContent =
62+
"Saved! " + in.value
63+
}
64+
}
65+
}
66+
67+
@JSExport
68+
object Canvas {
69+
@JSExport
70+
def main(c: dom.HTMLCanvasElement) = {
71+
type Ctx2D =
72+
dom.CanvasRenderingContext2D
73+
val ctx = c.getContext("2d")
74+
.asInstanceOf[Ctx2D]
75+
val w = 300
76+
c.width = w
77+
c.height = w
78+
79+
ctx.strokeStyle = "red"
80+
ctx.lineWidth = 3
81+
ctx.beginPath()
82+
ctx.moveTo(w/3, 0)
83+
ctx.lineTo(w/3, w/3)
84+
ctx.moveTo(w*2/3, 0)
85+
ctx.lineTo(w*2/3, w/3)
86+
ctx.moveTo(w, w/2)
87+
ctx.arc(w/2, w/2, w/2, 0, 3.14)
88+
89+
ctx.stroke()
90+
}
91+
}
92+
93+
@JSExport
94+
object Base64 {
95+
@JSExport
96+
def main(in: dom.HTMLInputElement,
97+
out: dom.HTMLDivElement) = {
98+
in.onkeyup = { (e: dom.Event) =>
99+
out.textContent =
100+
dom.btoa(in.value)
101+
}
102+
}
103+
}
104+
105+
@JSExport
106+
object EventHandler{
107+
@JSExport
108+
def main(pre: dom.HTMLPreElement) = {
109+
pre.onmousemove = {
110+
(e: dom.MouseEvent) =>
111+
pre.textContent =
112+
s"""e.clientX ${e.clientX}
113+
|e.clientY ${e.clientY}
114+
|e.pageX ${e.pageX}
115+
|e.pageY ${e.pageY}
116+
|e.screenX ${e.screenX}
117+
|e.screenY ${e.screenY}
118+
""".stripMargin
119+
}
120+
}
121+
}
122+
123+
@JSExport
124+
object XMLHttpRequest{
125+
@JSExport
126+
def main(pre: dom.HTMLPreElement) = {
127+
val xhr = new dom.XMLHttpRequest()
128+
xhr.open("GET",
129+
"http://api.openweathermap.org/" +
130+
"data/2.5/weather?q=Singapore"
131+
)
132+
xhr.onload = (e: dom.Event) => {
133+
if (xhr.status == 200) {
134+
pre.textContent =
135+
xhr.responseText
136+
}
137+
}
138+
xhr.send()
139+
}
140+
}
141+
142+
@JSExport
143+
object Websocket {
144+
@JSExport
145+
def main(in: dom.HTMLInputElement,
146+
pre: dom.HTMLPreElement) = {
147+
val echo = "ws://echo.websocket.org"
148+
val socket = new dom.WebSocket(echo)
149+
socket.onmessage = {
150+
(e: dom.MessageEvent) =>
151+
pre.textContent +=
152+
e.data.toString
153+
}
154+
socket.onopen = (e: dom.Event) => {
155+
in.onkeyup = (e: dom.Event) => {
156+
socket.send(in.value)
157+
}
158+
}
159+
}
160+
}
161+
162+
@JSExport
163+
object AjaxExtension {
164+
@JSExport
165+
def main(pre: dom.HTMLPreElement) = {
166+
import dom.extensions.Ajax
167+
import scalajs.concurrent
168+
.JSExecutionContext
169+
.Implicits
170+
.runNow
171+
val url =
172+
"http://api.openweathermap.org/" +
173+
"data/2.5/weather?q=Singapore"
174+
Ajax.get(url).onSuccess{ case xhr =>
175+
pre.textContent = xhr.responseText
176+
}
177+
}
178+
}

project/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ resolvers += Resolver.url("scala-js-releases",
44

55
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.0-M1")
66

7-
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
7+
addSbtPlugin("com.lihaoyi" % "scalatex-sbt-plugin" % "0.1.0")
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package readme
2+
3+
import java.nio.file.{Paths, Files}
4+
5+
import scalatags.Text.all._
6+
7+
object Main{
8+
def main(args: Array[String]): Unit = {
9+
new scalatex.site.Site{
10+
def content = Map("index.html" -> Index())
11+
override def autoResources = super.autoResources + "example-opt.js"
12+
override def generateHtml(outputRoot: String) = {
13+
for((path, frag) <- content){
14+
val txt = html(
15+
head(headFrags),
16+
bodyFrag(frag)
17+
).render
18+
Files.write(
19+
Paths.get(outputRoot + path),
20+
("<!DOCTYPE html>" + txt).getBytes
21+
)
22+
}
23+
24+
}
25+
}.renderTo("readme/target/output/")
26+
}
27+
}
28+
object sect extends scalatex.site.Section()
29+
object hl extends scalatex.site.Highlighter{
30+
def scala(s: String) = this.highlight(s, "scala")
31+
def suffixMappings = Map(
32+
"scala" -> "scala"
33+
)
34+
override def pathMappings = Seq(
35+
"" -> "https://github.com/scala-js/scala-js-dom/tree/master"
36+
)
37+
}

0 commit comments

Comments
 (0)