component_web_helper.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, width int) 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. newNode.Call("setAttribute", "width", width)
  73. if parent.Call("contains", oldNode).Bool() {
  74. parent.Call(
  75. "replaceChild",
  76. newNode,
  77. oldNode,
  78. )
  79. } else {
  80. parent.Call(
  81. "appendChild",
  82. newNode,
  83. )
  84. }
  85. return newNode
  86. }
  87. func WebBuildFrame(oldNode js.Value, parentComponent Component, view View, path string) js.Value {
  88. parent := parentComponent.(JS).Node()
  89. newNode := WebDocument().Call("createElement", "iframe")
  90. newNode.Call("setAttribute", "src", path)
  91. newNode.Call("setAttribute", "style", ViewToString(view))
  92. if parent.Call("contains", oldNode).Bool() {
  93. parent.Call(
  94. "replaceChild",
  95. newNode,
  96. oldNode,
  97. )
  98. } else {
  99. parent.Call(
  100. "appendChild",
  101. newNode,
  102. )
  103. }
  104. return newNode
  105. }
  106. func WebBuildCode(oldNode js.Value, parentComponent Component, view View, language string, content string) js.Value {
  107. parent := parentComponent.(JS).Node()
  108. codeNode := WebDocument().Call("createElement", "code")
  109. codeNode.Call("setAttribute", "class", "language-"+language)
  110. codeNode.Set("textContent", content)
  111. newNode := WebDocument().Call("createElement", "pre")
  112. newNode.Call("setAttribute", "style", ViewToString(view))
  113. newNode.Call(
  114. "appendChild",
  115. codeNode,
  116. )
  117. if parent.Call("contains", oldNode).Bool() {
  118. parent.Call(
  119. "replaceChild",
  120. newNode,
  121. oldNode,
  122. )
  123. } else {
  124. parent.Call(
  125. "appendChild",
  126. newNode,
  127. )
  128. }
  129. WebWindow().Call("HighlightCode")
  130. return newNode
  131. }
  132. func WebCleanChildren(node js.Value) {
  133. children := node.Get("children")
  134. for i := range children.Length() {
  135. node.Call("removeChild", children.Index(i))
  136. }
  137. }