Documentation ¶
Overview ¶
Package wasm defines several utilities to facilitate the writing of small WASM widgets in GoNB (or elsewhere).
It's based on `gowebapi`, but extends it in ways to make it ergonomic.
The variable `IsWasm` can be used to check in runtime if a program was compiled for wasm -- in case this is needed. This is the only symbol exported by this package for non-wasm builds.
It's built on top of the library `github.com/gowebapi/webapi`, a wrapper to convert most web APIs directly to Go methods -- as opposed to using the "syscall/js" indirect calling methods.
**Warning**: this is still experimental, and the API may change. Hopefully someone will improve `gowebapi` or create something new better.
Index ¶
- Constants
- Variables
- func Alert(msg string)
- func Alertf(msg string, args ...any)
- func Append(parent, child NodeCompatible)
- func AppendHTML(parent Element, html string)
- func AsButton(e EventTargetCompatible) *html.HTMLButtonElement
- func AsCanvas(e EventTargetCompatible) *canvas.HTMLCanvasElement
- func AsHTML(e EventTargetCompatible) *html.HTMLElement
- func AsImage(e EventTargetCompatible) *html.HTMLImageElement
- func AsInput(e EventTargetCompatible) *html.HTMLInputElement
- func AsKeyboardEvent(e EventCompatible) *htmlevent.KeyboardEvent
- func AsMouseEvent(e EventCompatible) *htmlevent.MouseEvent
- func AsNode(e NodeCompatible) *dom.Node
- func AsOption(e EventTargetCompatible) *html.HTMLOptionElement
- func AsScript(e EventTargetCompatible) *html.HTMLScriptElement
- func AsSelect(e EventTargetCompatible) *html.HTMLSelectElement
- func AsSpan(e EventTargetCompatible) *html.HTMLSpanElement
- func AsTR(e EventTargetCompatible) *html.HTMLTableRowElement
- func AsTable(e EventTargetCompatible) *html.HTMLTableElement
- func ById(id string) *html.HTMLElement
- func Disable(e HtmlElementCompatible)
- func DiscardEvent(e *domcore.Event)
- func Enable(e HtmlElementCompatible)
- func IsNil(e Compatible) bool
- func NewElem(tag string, attributes ...string) *dom.Element
- func On(element EventTargetCompatible, eventType string, ...)
- func OnWith(element EventTargetCompatible, eventType string, ...)
- func TRAddValue(tr *html.HTMLTableRowElement, value interface{}) *html.HTMLTableColElement
- func WaitForever()
- type Audio
- type Compatible
- type Element
- type EventCompatible
- type EventOptions
- type EventTargetCompatible
- type HtmlElementCompatible
- type KeyValue
- type KeyboardEventCompatible
- type MouseEventCompatible
- type NodeCompatible
Constants ¶
const ( KeyCodeNone = uint(0) KeyCodeLeft = 37 KeyCodeUp = 38 KeyCodeRight = 39 KeyCodeDown = 40 )
Key codes that can be used when comparing with a KeyboardEventCompatible.KeyCode()
Variables ¶
var ( // Doc is a shortcut to the webpage's DOM document. Doc *webapi.Document // Win is a shortcut to the webpage's DOM window. Win *webapi.Window )
var IsWasm = false
IsWasm is true in WASM builds, and false otherwise.
Functions ¶
func Append ¶
func Append(parent, child NodeCompatible)
Append will append the child to the parent node.
func AppendHTML ¶
AppendHTML inserts html at the end of the current element. It's a shortcut to InsertAdjacentHTML("beforeend", html).
func AsButton ¶
func AsButton(e EventTargetCompatible) *html.HTMLButtonElement
AsButton casts some type of element as an HTMLButtonElement.
func AsCanvas ¶
func AsCanvas(e EventTargetCompatible) *canvas.HTMLCanvasElement
AsCanvas casts some type of element as an HTMLCanvasElement.
func AsHTML ¶
func AsHTML(e EventTargetCompatible) *html.HTMLElement
AsHTML casts some type of element as an HTMLElement.
func AsImage ¶
func AsImage(e EventTargetCompatible) *html.HTMLImageElement
AsImage casts some type of element as an HTMLImageElement.
func AsInput ¶
func AsInput(e EventTargetCompatible) *html.HTMLInputElement
AsInput casts some type of element as an HTMLInputElement.
func AsKeyboardEvent ¶
func AsKeyboardEvent(e EventCompatible) *htmlevent.KeyboardEvent
AsKeyboardEvent casts some type of element as an HTMLKeyboardEventElement.
func AsMouseEvent ¶
func AsMouseEvent(e EventCompatible) *htmlevent.MouseEvent
AsMouseEvent casts some type of element as an HTMLMouseEventElement.
func AsNode ¶
func AsNode(e NodeCompatible) *dom.Node
AsNode converts any pointer to a struct that extends dom.Node, back to a *dom.Node.
func AsOption ¶
func AsOption(e EventTargetCompatible) *html.HTMLOptionElement
AsOption casts some type of element as an HTMLOptionElement.
func AsScript ¶
func AsScript(e EventTargetCompatible) *html.HTMLScriptElement
AsScript casts some type of element as an HTMLScriptElement.
func AsSelect ¶
func AsSelect(e EventTargetCompatible) *html.HTMLSelectElement
AsSelect casts some type of element as an HTMLSelectElement.
func AsSpan ¶
func AsSpan(e EventTargetCompatible) *html.HTMLSpanElement
AsSpan casts some type of element as an HTMLSpanElement.
func AsTR ¶
func AsTR(e EventTargetCompatible) *html.HTMLTableRowElement
AsTR casts some type of element as an HTMLTableRowElement.
func AsTable ¶
func AsTable(e EventTargetCompatible) *html.HTMLTableElement
AsTable casts some type of element as an HTMLTableElement.
func ById ¶
func ById(id string) *html.HTMLElement
ById returns the HTML element with the given Id, or nil if didn't find it.
func DiscardEvent ¶
DiscardEvent is an event handler that stops propagation and the default browser handling. Can be used as a parameter to the `On` function.
func NewElem ¶
NewElem creates a new DOM element with the tag / attributes given. Attributes can be given without a value (e.g.: `readonly`) or as a key/value pair (`width=10`), split by the first "=".
func On ¶
func On(element EventTargetCompatible, eventType string, callback func(ev EventCompatible))
On adds a callback to the element when event type happens, using `element.AddEventListener`. All event options default to false. See OnWith() to set options.
func OnWith ¶
func OnWith(element EventTargetCompatible, eventType string, callback func(ev EventCompatible), options EventOptions)
OnWith adds a callback to the element when eventType happens, using `element.AddEventListener`. There is no easy way to remove the event listener from Go :(
Event types: "change", "load", "click", "mouseup", etc.
func TRAddValue ¶
func TRAddValue(tr *html.HTMLTableRowElement, value interface{}) *html.HTMLTableColElement
TRAddValue adds a cell to a row (`tr`) in a table.
func WaitForever ¶
func WaitForever()
WaitForever waits on a channel forever, preventing the Go WASM program to ever exit. This is needed on a program that will be listening to user interaction. Use at the end of the program.
Types ¶
type Audio ¶
type Audio struct {
// contains filtered or unexported fields
}
Audio is a simple wrapper over JS value that should represent a "<audio ...>" node.
func AsAudio ¶
func AsAudio(e *html.HTMLElement) *Audio
AsAudio casts some type of element as an audio.AudioNode.
type Compatible ¶
Compatible is an interface implemented by most of webapi types.
type Element ¶
type Element interface { NamespaceURI() *string Prefix() *string LocalName() string TagName() string Id() string SetId(value string) ClassName() string SetClassName(value string) ClassList() *domcore.DOMTokenList Slot() string SetSlot(value string) Attributes() *dom.NamedNodeMap ShadowRoot() *dom.ShadowRoot InnerHTML() string SetInnerHTML(value string) OuterHTML() string SetOuterHTML(value string) ScrollTop() float64 SetScrollTop(value float64) ScrollLeft() float64 SetScrollLeft(value float64) ScrollWidth() int ScrollHeight() int ClientTop() int ClientLeft() int ClientWidth() int ClientHeight() int OnFullscreenChange() domcore.EventHandlerFunc OnFullscreenError() domcore.EventHandlerFunc Children() *dom.HTMLCollection FirstElementChild() *dom.Element LastElementChild() *dom.Element ChildElementCount() uint PreviousElementSibling() *dom.Element NextElementSibling() *dom.Element AssignedSlot() js.Value Role() *string SetRole(value *string) AriaActiveDescendant() *string SetAriaActiveDescendant(value *string) AriaAtomic() *string SetAriaAtomic(value *string) AriaAutoComplete() *string SetAriaAutoComplete(value *string) AriaBusy() *string SetAriaBusy(value *string) AriaChecked() *string SetAriaChecked(value *string) AriaColCount() *string SetAriaColCount(value *string) AriaColIndex() *string SetAriaColIndex(value *string) AriaColSpan() *string SetAriaColSpan(value *string) AriaControls() *string SetAriaControls(value *string) AriaCurrent() *string SetAriaCurrent(value *string) AriaDescribedBy() *string SetAriaDescribedBy(value *string) AriaDetails() *string SetAriaDetails(value *string) AriaDisabled() *string SetAriaDisabled(value *string) AriaErrorMessage() *string SetAriaErrorMessage(value *string) AriaExpanded() *string SetAriaExpanded(value *string) AriaFlowTo() *string SetAriaFlowTo(value *string) AriaHasPopup() *string SetAriaHasPopup(value *string) AriaHidden() *string SetAriaHidden(value *string) AriaInvalid() *string SetAriaInvalid(value *string) AriaKeyShortcuts() *string SetAriaKeyShortcuts(value *string) AriaLabel() *string SetAriaLabel(value *string) AriaLabelledBy() *string SetAriaLabelledBy(value *string) AriaLevel() *string SetAriaLevel(value *string) AriaLive() *string SetAriaLive(value *string) AriaModal() *string SetAriaModal(value *string) AriaMultiLine() *string SetAriaMultiLine(value *string) AriaMultiSelectable() *string SetAriaMultiSelectable(value *string) AriaOrientation() *string SetAriaOrientation(value *string) AriaOwns() *string SetAriaOwns(value *string) AriaPlaceholder() *string SetAriaPlaceholder(value *string) AriaPosInSet() *string SetAriaPosInSet(value *string) AriaPressed() *string SetAriaPressed(value *string) AriaReadOnly() *string SetAriaReadOnly(value *string) AriaRelevant() *string SetAriaRelevant(value *string) AriaRequired() *string SetAriaRequired(value *string) AriaRoleDescription() *string SetAriaRoleDescription(value *string) AriaRowCount() *string SetAriaRowCount(value *string) AriaRowIndex() *string SetAriaRowIndex(value *string) AriaRowSpan() *string SetAriaRowSpan(value *string) AriaSelected() *string SetAriaSelected(value *string) AriaSetSize() *string SetAriaSetSize(value *string) AriaSort() *string SetAriaSort(value *string) AriaValueMax() *string SetAriaValueMax(value *string) AriaValueMin() *string SetAriaValueMin(value *string) AriaValueNow() *string SetAriaValueNow(value *string) AriaValueText() *string SetAriaValueText(value *string) AddEventFullscreenChange(listener func(event *domcore.Event, currentTarget *dom.Element)) js.Func SetOnFullscreenChange(listener func(event *domcore.Event, currentTarget *dom.Element)) js.Func AddEventFullscreenError(listener func(event *domcore.Event, currentTarget *dom.Element)) js.Func SetOnFullscreenError(listener func(event *domcore.Event, currentTarget *dom.Element)) js.Func HasAttributes() (_result bool) GetAttributeNames() (_result []string) GetAttribute(qualifiedName string) (_result *string) GetAttributeNS(namespace *string, localName string) (_result *string) SetAttribute(qualifiedName string, value string) SetAttributeNS(namespace *string, qualifiedName string, value string) RemoveAttribute(qualifiedName string) RemoveAttributeNS(namespace *string, localName string) ToggleAttribute(qualifiedName string, force *bool) (_result bool) HasAttribute(qualifiedName string) (_result bool) HasAttributeNS(namespace *string, localName string) (_result bool) GetAttributeNode(qualifiedName string) (_result *dom.Attr) GetAttributeNodeNS(namespace *string, localName string) (_result *dom.Attr) SetAttributeNode(attr *dom.Attr) (_result *dom.Attr) SetAttributeNodeNS(attr *dom.Attr) (_result *dom.Attr) RemoveAttributeNode(attr *dom.Attr) (_result *dom.Attr) AttachShadow(init *dom.ShadowRootInit) (_result *dom.ShadowRoot) Closest(selectors string) (_result *dom.Element) Matches(selectors string) (_result bool) WebkitMatchesSelector(selectors string) (_result bool) GetElementsByTagName(qualifiedName string) (_result *dom.HTMLCollection) GetElementsByTagNameNS(namespace *string, localName string) (_result *dom.HTMLCollection) GetElementsByClassName(classNames string) (_result *dom.HTMLCollection) InsertAdjacentElement(where string, element *dom.Element) (_result *dom.Element) InsertAdjacentText(where string, data string) InsertAdjacentHTML(position string, text string) GetFragmentInformation(filter dom.FragmentFilter) (_result *dom.PromiseDeadFragmentInformation) ComputedStyleMap() (_result *typedom.StylePropertyMapReadOnly) GetClientRects() (_result *geometry.DOMRectList) GetBoundingClientRect() (_result *geometry.DOMRect) ScrollIntoView(arg *dom.Union) Scroll(options *view.ScrollToOptions) Scroll2(x float64, y float64) ScrollTo(options *view.ScrollToOptions) ScrollTo2(x float64, y float64) ScrollBy(options *view.ScrollToOptions) ScrollBy2(x float64, y float64) RequestFullscreen(options *dom.FullscreenOptions) (_result *javascript.PromiseVoid) SetPointerCapture(pointerId int) ReleasePointerCapture(pointerId int) HasPointerCapture(pointerId int) (_result bool) RequestPointerLock() GetBoxQuads(options *view.BoxQuadOptions) (_result []*geometry.DOMQuad) ConvertQuadFromNode(quad *geometry.DOMQuadInit, from *dom.Union, options *view.ConvertCoordinateOptions) (_result *geometry.DOMQuad) ConvertRectFromNode(rect *geometry.DOMRectReadOnly, from *dom.Union, options *view.ConvertCoordinateOptions) (_result *geometry.DOMQuad) ConvertPointFromNode(point *geometry.DOMPointInit, from *dom.Union, options *view.ConvertCoordinateOptions) (_result *geometry.DOMPoint) Prepend(nodes ...*dom.Union) Append(nodes ...*dom.Union) QuerySelector(selectors string) (_result *dom.Element) QuerySelectorAll(selectors string) (_result *dom.NodeList) Before(nodes ...*dom.Union) After(nodes ...*dom.Union) ReplaceWith(nodes ...*dom.Union) Remove() Animate(keyframes *javascript.Object, options *dom.Union) (_result *webani.Animation) GetAnimations() (_result []*webani.Animation) }
Element is the interface implemented by webapi.Element structure.
type EventCompatible ¶
type EventCompatible interface { Compatible Bubbles() bool PreventDefault() StopPropagation() StopImmediatePropagation() }
type EventOptions ¶
type EventOptions struct {
Capture, Once, Passive bool
}
EventOptions are the options passed to AddEventListener used in the `OnWith` function.
type EventTargetCompatible ¶
type EventTargetCompatible interface { Compatible AddEventListener(_type string, callback *domcore.EventListenerValue, options *domcore.Union) }
EventTargetCompatible is the interface that all Element/Node types of the DOM implement.
type HtmlElementCompatible ¶
type HtmlElementCompatible interface { Element Style() *cssom.CSSStyleDeclaration }
HtmlElementCompatible is anything that behaves like a dom.HtmlElement.
type KeyValue ¶
type KeyValue struct {
Key, Value string
}
KeyValue is a simple structure used for different things. Key and Value are both string.
type KeyboardEventCompatible ¶
type MouseEventCompatible ¶
type NodeCompatible ¶
type NodeCompatible interface { EventTargetCompatible AppendChild(node *dom.Node) (_result *dom.Node) ChildNodes() *dom.NodeList RemoveChild(child *dom.Node) (_result *dom.Node) }
NodeCompatible is an interface implemented by several of the webapi types that support having sub-nodes.