Documentation
¶
Overview ¶
Package dom provides a stable firewall towards the "syscall/js" API. The wasm platform does not fulfill the Go 1 stability guarantee and may change and break (as already happened) with any release.
The package provides a more type safe abstraction layer on top of the js API which more or less directly represents the DOM API.
Index ¶
- Constants
- Variables
- func GenerateID() string
- func Post(f func())
- func SetTimeout(d time.Duration, f func())
- type Document
- type Element
- func (n Element) AddClass(v string) Element
- func (n Element) AddEventListener(typ string, once bool, listener func()) Releasable
- func (n Element) AddKeyListener(typ string, f func(keyCode int)) Releasable
- func (n Element) AddReleaseListener(f func()) Element
- func (n Element) AppendElement(aChild Element) Element
- func (n Element) AppendTextNode(t string) Element
- func (n Element) Call(name string, args ...interface{}) Element
- func (n Element) Children() []Element
- func (n Element) Clear() Element
- func (n Element) Equal(o Element) bool
- func (n Element) Get(p string) interface{}
- func (n Element) GetAttribute(p string) interface{}
- func (n Element) GetBoundingClientRect() Rect
- func (n Element) GetID() string
- func (n Element) GetTagName() string
- func (n Element) HasClass(v string) bool
- func (n Element) IsNull() bool
- func (n Element) LastChild() Element
- func (n Element) Release()
- func (n Element) Remove() Element
- func (n Element) RemoveAttribute(a string) Element
- func (n Element) RemoveClass(v string) Element
- func (n Element) ReplaceWith(o Element) Element
- func (n Element) Set(p string, x interface{}) Element
- func (n Element) SetAttribute(a string, x interface{}) Element
- func (n Element) SetClassName(str string) Element
- func (n Element) SetID(id string)
- func (n Element) SetInnerHTML(v string) Element
- func (n Element) SetTextContent(v string) Element
- func (n Element) String() string
- func (n Element) Style() Style
- type Global
- type Location
- type Rect
- func (n Rect) GetBottom() int
- func (n Rect) GetHeight() int
- func (n Rect) GetLeft() int
- func (n Rect) GetRight() int
- func (n Rect) GetTop() int
- func (n Rect) GetWidth() int
- func (n Rect) GetX() int
- func (n Rect) GetY() int
- func (n Rect) SetBottom(b int) Rect
- func (n Rect) SetHeight(height int) Rect
- func (n Rect) SetLeft(l int) Rect
- func (n Rect) SetRight(r int) Rect
- func (n Rect) SetTop(t int) Rect
- func (n Rect) SetWidth(width int) Rect
- func (n Rect) SetX(x int) Rect
- func (n Rect) SetY(y int) Rect
- type Releasable
- type ReleaseFunc
- type Style
- type Window
- func (n Window) AddEventListener(typ string, listener func()) Releasable
- func (n Window) Document() Document
- func (n Window) HashChange(f func()) Releasable
- func (n Window) InnerHeight() int
- func (n Window) InnerWidth() int
- func (n Window) Location() Location
- func (n Window) MatchesMedia(criteria string) bool
- func (n Window) OnHashChange(f func()) Releasable
- func (n Window) OnPopState(f func()) Releasable
- func (n Window) SetLocation(url string) Window
Constants ¶
const EventRelease = "forms-release"
Variables ¶
var GlobalPanicHandler = func() { r := recover() if r == nil { return } msg := fmt.Sprint(r) log.Println(msg, string(debug.Stack())) body := GetWindow().Document().Body() body.Clear() body.SetClassName("bg-gray-300") body.AppendElement(formatPanic(msg)) }
The GlobalPanicHandler should be called with a defer in every method or callback which will likely cause a panic. A non-recovered panic will cause the wasm-Code to just exit silently, which is in practice not very helpful. You probably want to do some event logging or just show a support screen. Without that, the user may try to continue interacting with an already dead application, which must be avoided in all cases, to ensure usability.
The default implementation will try to recover a panic and replaces the body content with a readable stack trace. The formatting uses some tailwind css classes.
Functions ¶
func GenerateID ¶
func GenerateID() string
GenerateID returns the next unique identifier for dom elements.
func SetTimeout ¶
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
func GetDocument ¶
func GetDocument() Document
func (Document) CreateElement ¶
In an HTML document, the document.createElement() method creates the HTML element specified by tagName, or an HTMLUnknownElement if tagName isn't recognized.
func (Document) CreateElementNS ¶
In an HTML document, the document.createElement() method creates the HTML element specified by tagName, or an HTMLUnknownElement if tagName isn't recognized.
func (Document) DocumentElement ¶
func (Document) GetElementById ¶
GeElementById follows https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById. If not found, returns an Element whose Element.IsNull method will return true.
type Element ¶
type Element struct {
// contains filtered or unexported fields
}
Element is always used in a value context, to avoid additional GC pressure. Therefore our state-handling relies on Javascript messaging (especially the release cycles).
func (Element) AddEventListener ¶
func (n Element) AddEventListener(typ string, once bool, listener func()) Releasable
AddEventListener is internally very complex, because it keeps a global callback reference to connect the wasm and the javascript context. The wasm side must keep a global un-collectable function and the javascript side does the same. This makes event handling currently very expensive. Always ensure that you call Release on this Element to free all resources.
func (Element) AddKeyListener ¶
func (n Element) AddKeyListener(typ string, f func(keyCode int)) Releasable
The keydown event is fired when a key is pressed. See also https://developer.mozilla.org/en-US/docs/Web/API/Document/keydown_event
func (Element) AddReleaseListener ¶
func (Element) AppendElement ¶
func (Element) AppendTextNode ¶
func (Element) Call ¶
Call is an unclean abstraction and can invoke attached javascript methods. Args which are elements, are internally unwrapped.
func (Element) Get ¶
Get returns unwrapped primitives like string, bool or float but for others its return value is undefined and may change at any time.
func (Element) GetAttribute ¶
GetAttribute returns unwrapped primitives like string, bool or float but for others its return value is undefined and may change at any time.
func (Element) GetBoundingClientRect ¶
See https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect.
func (Element) GetID ¶
GetID returns the empty string or the according unique element id. See also https://developer.mozilla.org/en-US/docs/Web/API/Element/id.
func (Element) GetTagName ¶
func (Element) LastChild ¶
LastChild returns last child node. See also https://developer.mozilla.org/de/docs/Web/API/Node/lastChild.
func (Element) Release ¶
func (n Element) Release()
Release is part of our custom lifecycle. We need a manual destructor, because we have currently two running contexts: our wasm program and the browsers javascript interpreter. This is important to remove callbacks and other attached resources, which would otherwise never be freed, due to global or cyclic references. All contained children element, will also receive a Release call.
func (Element) Remove ¶
Removes this Element from its parent and Release it. See also https://developer.mozilla.org/de/docs/Web/API/ChildNode/remove.
func (Element) RemoveAttribute ¶
RemoveAttribute deletes the html attribute.
func (Element) RemoveClass ¶
func (Element) ReplaceWith ¶
func (Element) Set ¶
Set sets the javascript property. Most standard attributes have an according property. See also https://javascript.info/dom-attributes-and-properties#html-attributes.
func (Element) SetAttribute ¶
SetAttribute sets the html attribute. There are attributes, which have no corresponding javascript property. See https://javascript.info/dom-attributes-and-properties#html-attributes.
func (Element) SetClassName ¶
func (Element) SetID ¶
SetID update the according unique element id. See also https://developer.mozilla.org/en-US/docs/Web/API/Element/id.
func (Element) SetInnerHTML ¶
func (Element) SetTextContent ¶
type Rect ¶
type Rect struct {
// contains filtered or unexported fields
}
A Rect represents a DOMRect see also https://developer.mozilla.org/en-US/docs/Web/API/DOMRect. It seems undefined, what nature the coordinates are.
type Releasable ¶
type Releasable interface {
Release()
}
A Releasable cleans up references and the resource must not be used afterwards anymore.
type ReleaseFunc ¶
type ReleaseFunc func()
func (ReleaseFunc) Release ¶
func (f ReleaseFunc) Release()
type Style ¶
type Style struct {
// contains filtered or unexported fields
}
func (Style) SetProperty ¶
type Window ¶
type Window struct {
// contains filtered or unexported fields
}
func (Window) AddEventListener ¶
func (n Window) AddEventListener(typ string, listener func()) Releasable
AddEventListener registers a new listener. Note that there is no automatic release management as for Element.
func (Window) HashChange ¶
func (n Window) HashChange(f func()) Releasable
func (Window) InnerHeight ¶
func (Window) InnerWidth ¶
func (Window) MatchesMedia ¶
MatchesMedia is the javascript equivalent to css media queries. criteria is for example
- (min-width:800px)
- (min-width:800px) or (orientation: landscape)
- (max-width: 800px)
func (Window) OnHashChange ¶
func (n Window) OnHashChange(f func()) Releasable
func (Window) OnPopState ¶
func (n Window) OnPopState(f func()) Releasable