|
| 1 | +package example |
| 2 | + |
| 3 | +import scala.scalajs.js |
| 4 | +import scala.scalajs.js.annotation._ |
| 5 | + |
| 6 | +import org.scalajs.dom |
| 7 | +import dom.html |
| 8 | + |
| 9 | +@JSExportTopLevel("ExampleAlert") |
| 10 | +object Alert { |
| 11 | + @JSExport |
| 12 | + def main() = { |
| 13 | + import org.scalajs.dom |
| 14 | + dom.window.alert("Hi from Scala-js-dom") |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +@JSExportTopLevel("ExampleNodeAppendChild") |
| 19 | +object NodeAppendChild { |
| 20 | + @JSExport |
| 21 | + def main(div: html.Div) = { |
| 22 | + val child = dom.document.createElement("div") |
| 23 | + child.textContent = "Hi from Scala-js-dom" |
| 24 | + div.appendChild(child) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +@JSExportTopLevel("ExampleNodeReplaceChildren") |
| 29 | +object NodeReplaceChildren { |
| 30 | + @JSExport |
| 31 | + def main(div: html.Div): Unit = { |
| 32 | + dom.document.replaceChildren() |
| 33 | + dom.document.replaceChildren("") |
| 34 | + dom.document.replaceChildren(div) |
| 35 | + dom.document.replaceChildren("", div) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +@JSExportTopLevel("ExampleElementStyle") |
| 40 | +object ElementStyle { |
| 41 | + @JSExport |
| 42 | + def main(div: html.Div) = { |
| 43 | + val colors = Seq( |
| 44 | + "red", "green", "blue" |
| 45 | + ) |
| 46 | + |
| 47 | + val index = |
| 48 | + util.Random.nextInt(colors.length) |
| 49 | + |
| 50 | + div.style.color = colors(index) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +@JSExportTopLevel("ExampleLocalStorage") |
| 55 | +object LocalStorage { |
| 56 | + @JSExport |
| 57 | + def main(in: html.Input, box: html.Div) = { |
| 58 | + val key = "my-key" |
| 59 | + |
| 60 | + in.value = |
| 61 | + dom.window.localStorage.getItem(key) |
| 62 | + |
| 63 | + in.onkeyup = { (e: dom.Event) => |
| 64 | + dom.window.localStorage.setItem( |
| 65 | + key, in.value |
| 66 | + ) |
| 67 | + box.textContent = |
| 68 | + "Saved! " + in.value |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +@JSExportTopLevel("ExampleCanvas") |
| 74 | +object Canvas { |
| 75 | + @JSExport |
| 76 | + def main(c: html.Canvas) = { |
| 77 | + type Ctx2D = |
| 78 | + dom.CanvasRenderingContext2D |
| 79 | + val ctx = c.getContext("2d") |
| 80 | + .asInstanceOf[Ctx2D] |
| 81 | + val w = 300 |
| 82 | + c.width = w |
| 83 | + c.height = w |
| 84 | + |
| 85 | + ctx.strokeStyle = "red" |
| 86 | + ctx.lineWidth = 3 |
| 87 | + ctx.beginPath() |
| 88 | + ctx.moveTo(w/3, 0) |
| 89 | + ctx.lineTo(w/3, w/3) |
| 90 | + ctx.moveTo(w*2/3, 0) |
| 91 | + ctx.lineTo(w*2/3, w/3) |
| 92 | + ctx.moveTo(w, w/2) |
| 93 | + ctx.arc(w/2, w/2, w/2, 0, 3.14) |
| 94 | + |
| 95 | + ctx.stroke() |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +@JSExportTopLevel("ExampleBase64") |
| 100 | +object Base64 { |
| 101 | + @JSExport |
| 102 | + def main(in: html.Input, |
| 103 | + out: html.Div) = { |
| 104 | + in.onkeyup = { (e: dom.Event) => |
| 105 | + out.textContent = |
| 106 | + dom.window.btoa(in.value) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +@JSExportTopLevel("ExampleEventHandler") |
| 112 | +object EventHandler{ |
| 113 | + @JSExport |
| 114 | + def main(pre: html.Pre) = { |
| 115 | + pre.onmousemove = { |
| 116 | + (e: dom.MouseEvent) => |
| 117 | + pre.textContent = |
| 118 | + s"""e.clientX ${e.clientX} |
| 119 | + |e.clientY ${e.clientY} |
| 120 | + |e.pageX ${e.pageX} |
| 121 | + |e.pageY ${e.pageY} |
| 122 | + |e.screenX ${e.screenX} |
| 123 | + |e.screenY ${e.screenY} |
| 124 | + """.stripMargin |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +@JSExportTopLevel("ExampleFetch") |
| 130 | +object Fetch { |
| 131 | + @JSExport |
| 132 | + def main(pre: html.Pre) = { |
| 133 | + import scala.concurrent |
| 134 | + .ExecutionContext |
| 135 | + .Implicits |
| 136 | + .global |
| 137 | + import js.Thenable.Implicits._ |
| 138 | + val url = |
| 139 | + "https://www.boredapi.com/api/activity" |
| 140 | + val responseText = for { |
| 141 | + response <- dom.fetch(url) |
| 142 | + text <- response.text() |
| 143 | + } yield { |
| 144 | + text |
| 145 | + } |
| 146 | + for (text <- responseText) |
| 147 | + pre.textContent = text |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +@JSExportTopLevel("ExampleWebsocket") |
| 152 | +object Websocket { |
| 153 | + @JSExport |
| 154 | + def main(in: html.Input, |
| 155 | + pre: html.Pre) = { |
| 156 | + val echo = "wss://echo.websocket.org" |
| 157 | + val socket = new dom.WebSocket(echo) |
| 158 | + socket.onmessage = { |
| 159 | + (e: dom.MessageEvent) => |
| 160 | + pre.textContent += |
| 161 | + e.data.toString |
| 162 | + } |
| 163 | + socket.onopen = { (e: dom.Event) => |
| 164 | + in.onkeyup = { (e: dom.Event) => |
| 165 | + socket.send(in.value) |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments