component_web_helper.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //go:build js && wasm
  2. package ultraviolet
  3. import (
  4. "syscall/js"
  5. )
  6. func WebWindow() js.Value {
  7. return js.Global().Get("window")
  8. }
  9. func WebDocument() js.Value {
  10. return js.Global().Get("document")
  11. }
  12. func WebContent() js.Value {
  13. return WebDocument().
  14. Call(
  15. "getElementById",
  16. "content",
  17. )
  18. }
  19. func WebBuildDiv(oldNode js.Value, parent Component, view View, text string, html string, handlers AbstractHandlers) js.Value {
  20. return WebBuildDivInternal(
  21. oldNode,
  22. parent.(JS).Node(),
  23. view,
  24. text,
  25. html,
  26. handlers,
  27. )
  28. }
  29. func WebBuildDivInternal(oldNode js.Value, parent js.Value, view View, text string, html string, handlers AbstractHandlers) js.Value {
  30. newNode := WebDocument().Call("createElement", "div")
  31. newNode.Call("setAttribute", "style", ViewToString(view))
  32. if text != "" {
  33. newNode.Set("innerText", text)
  34. }
  35. if html != "" {
  36. newNode.Set("innerHTML", html)
  37. }
  38. for key, handler := range handlers {
  39. switch key {
  40. case HANDLER_CLICK:
  41. newNode.Call(
  42. "addEventListener",
  43. "click",
  44. js.FuncOf(
  45. func(current js.Value, args []js.Value) interface{} {
  46. handler(nil)
  47. return nil
  48. },
  49. ),
  50. )
  51. }
  52. }
  53. if parent.Call("contains", oldNode).Bool() {
  54. parent.Call(
  55. "replaceChild",
  56. newNode,
  57. oldNode,
  58. )
  59. } else {
  60. parent.Call(
  61. "appendChild",
  62. newNode,
  63. )
  64. }
  65. return newNode
  66. }
  67. func WebBuildImg(oldNode js.Value, parentComponent Component, view View, path string) js.Value {
  68. parent := parentComponent.(JS).Node()
  69. newNode := WebDocument().Call("createElement", "img")
  70. newNode.Call("setAttribute", "src", path)
  71. newNode.Call("setAttribute", "style", ViewToString(view))
  72. if parent.Call("contains", oldNode).Bool() {
  73. parent.Call(
  74. "replaceChild",
  75. newNode,
  76. oldNode,
  77. )
  78. } else {
  79. parent.Call(
  80. "appendChild",
  81. newNode,
  82. )
  83. }
  84. return newNode
  85. }
  86. func WebBuildFrame(oldNode js.Value, parentComponent Component, view View, path string) js.Value {
  87. parent := parentComponent.(JS).Node()
  88. newNode := WebDocument().Call("createElement", "iframe")
  89. newNode.Call("setAttribute", "src", path)
  90. newNode.Call("setAttribute", "style", ViewToString(view))
  91. if parent.Call("contains", oldNode).Bool() {
  92. parent.Call(
  93. "replaceChild",
  94. newNode,
  95. oldNode,
  96. )
  97. } else {
  98. parent.Call(
  99. "appendChild",
  100. newNode,
  101. )
  102. }
  103. return newNode
  104. }
  105. func WebBuildCode(oldNode js.Value, parentComponent Component, view View, language string, content string) js.Value {
  106. parent := parentComponent.(JS).Node()
  107. codeNode := WebDocument().Call("createElement", "code")
  108. codeNode.Call("setAttribute", "class", "language-"+language)
  109. codeNode.Set("textContent", content)
  110. newNode := WebDocument().Call("createElement", "pre")
  111. newNode.Call("setAttribute", "style", ViewToString(view))
  112. newNode.Call(
  113. "appendChild",
  114. codeNode,
  115. )
  116. if parent.Call("contains", oldNode).Bool() {
  117. parent.Call(
  118. "replaceChild",
  119. newNode,
  120. oldNode,
  121. )
  122. } else {
  123. parent.Call(
  124. "appendChild",
  125. newNode,
  126. )
  127. }
  128. WebWindow().Call("HighlightCode")
  129. return newNode
  130. }
  131. func WebCleanChildren(node js.Value) {
  132. children := node.Get("children")
  133. for i := range children.Length() {
  134. node.Call("removeChild", children.Index(i))
  135. }
  136. }