| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- //go:build js && wasm
- package ultraviolet
- import (
- "syscall/js"
- )
- func WebWindow() js.Value {
- return js.Global().Get("window")
- }
- func WebDocument() js.Value {
- return js.Global().Get("document")
- }
- func WebContent() js.Value {
- return WebDocument().
- Call(
- "getElementById",
- "content",
- )
- }
- func WebBuildDiv(oldNode js.Value, parent Component, view View, text string, html string, handlers AbstractHandlers) js.Value {
- return WebBuildDivInternal(
- oldNode,
- parent.(JS).Node(),
- view,
- text,
- html,
- handlers,
- )
- }
- func WebBuildDivInternal(oldNode js.Value, parent js.Value, view View, text string, html string, handlers AbstractHandlers) js.Value {
- newNode := WebDocument().Call("createElement", "div")
- newNode.Call("setAttribute", "style", ViewToString(view))
- if text != "" {
- newNode.Set("innerText", text)
- }
- if html != "" {
- newNode.Set("innerHTML", html)
- }
- for key, handler := range handlers {
- switch key {
- case HANDLER_CLICK:
- newNode.Call(
- "addEventListener",
- "click",
- js.FuncOf(
- func(current js.Value, args []js.Value) interface{} {
- handler(nil)
- return nil
- },
- ),
- )
- }
- }
- if parent.Call("contains", oldNode).Bool() {
- parent.Call(
- "replaceChild",
- newNode,
- oldNode,
- )
- } else {
- parent.Call(
- "appendChild",
- newNode,
- )
- }
- return newNode
- }
- func WebBuildImg(oldNode js.Value, parentComponent Component, view View, path string) js.Value {
- parent := parentComponent.(JS).Node()
- newNode := WebDocument().Call("createElement", "img")
- newNode.Call("setAttribute", "src", path)
- newNode.Call("setAttribute", "style", ViewToString(view))
- if parent.Call("contains", oldNode).Bool() {
- parent.Call(
- "replaceChild",
- newNode,
- oldNode,
- )
- } else {
- parent.Call(
- "appendChild",
- newNode,
- )
- }
- return newNode
- }
- func WebBuildFrame(oldNode js.Value, parentComponent Component, view View, path string) js.Value {
- parent := parentComponent.(JS).Node()
- newNode := WebDocument().Call("createElement", "iframe")
- newNode.Call("setAttribute", "src", path)
- newNode.Call("setAttribute", "style", ViewToString(view))
- if parent.Call("contains", oldNode).Bool() {
- parent.Call(
- "replaceChild",
- newNode,
- oldNode,
- )
- } else {
- parent.Call(
- "appendChild",
- newNode,
- )
- }
- return newNode
- }
- func WebBuildCode(oldNode js.Value, parentComponent Component, view View, language string, content string) js.Value {
- parent := parentComponent.(JS).Node()
- codeNode := WebDocument().Call("createElement", "code")
- codeNode.Call("setAttribute", "class", "language-"+language)
- codeNode.Set("textContent", content)
- newNode := WebDocument().Call("createElement", "pre")
- newNode.Call("setAttribute", "style", ViewToString(view))
- newNode.Call(
- "appendChild",
- codeNode,
- )
- if parent.Call("contains", oldNode).Bool() {
- parent.Call(
- "replaceChild",
- newNode,
- oldNode,
- )
- } else {
- parent.Call(
- "appendChild",
- newNode,
- )
- }
- WebWindow().Call("HighlightCode")
- return newNode
- }
- func WebCleanChildren(node js.Value) {
- children := node.Get("children")
- for i := range children.Length() {
- node.Call("removeChild", children.Index(i))
- }
- }
|