dom

package module
v0.0.0-...-b8f35d6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2021 License: Apache-2.0 Imports: 8 Imported by: 8

README

dom Go Reference

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.

It is important to note, that there is a little custom lifecycle semantic fo some listeners, to make releasing less leaky and easier to use.

Roadmap

It is planned to replace this handwritten layer with an automatically generated version of the HTML living standard which is based on a bikeshedding format with embedded webidl descriptions, see also https://github.com/whatwg/dom/blob/master/review-drafts/2020-06.bs. In contrast to already available generated bindings for Go and other languages like Rust, the human readable documentation should be in the mix.

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

View Source
const EventRelease = "forms-release"

Variables

View Source
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 Post

func Post(f func())

func SetTimeout

func SetTimeout(d time.Duration, f func())

Types

type Document

type Document struct {
	// contains filtered or unexported fields
}

func GetDocument

func GetDocument() Document

func (Document) Body

func (n Document) Body() Element

func (Document) CreateElement

func (n Document) CreateElement(name string) Element

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

func (n Document) CreateElementNS(ns string, name string) Element

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 (n Document) DocumentElement() Element

func (Document) GetElementById

func (n Document) GetElementById(id string) Element

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) AddClass

func (n Element) AddClass(v string) Element

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 (n Element) AddReleaseListener(f func()) Element

func (Element) AppendElement

func (n Element) AppendElement(aChild Element) Element

func (Element) AppendTextNode

func (n Element) AppendTextNode(t string) Element

func (Element) Call

func (n Element) Call(name string, args ...interface{}) Element

Call is an unclean abstraction and can invoke attached javascript methods. Args which are elements, are internally unwrapped.

func (Element) Children

func (n Element) Children() []Element

func (Element) Clear

func (n Element) Clear() Element

func (Element) Equal

func (n Element) Equal(o Element) bool

Equal has the Javascript == semantic on the Element (equal reference)

func (Element) Get

func (n Element) Get(p string) interface{}

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

func (n Element) GetAttribute(p string) interface{}

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) GetID

func (n Element) GetID() string

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 (n Element) GetTagName() string

func (Element) HasClass

func (n Element) HasClass(v string) bool

func (Element) IsNull

func (n Element) IsNull() bool

IsNull returns true, if the represented Element is actually a null value.

func (Element) LastChild

func (n Element) LastChild() Element

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

func (n Element) Remove() Element

Removes this Element from its parent and Release it. See also https://developer.mozilla.org/de/docs/Web/API/ChildNode/remove.

func (Element) RemoveAttribute

func (n Element) RemoveAttribute(a string) Element

RemoveAttribute deletes the html attribute.

func (Element) RemoveClass

func (n Element) RemoveClass(v string) Element

func (Element) ReplaceWith

func (n Element) ReplaceWith(o Element) Element

func (Element) Set

func (n Element) Set(p string, x interface{}) Element

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

func (n Element) SetAttribute(a string, x interface{}) Element

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 (n Element) SetClassName(str string) Element

func (Element) SetID

func (n Element) SetID(id string)

SetID update the according unique element id. See also https://developer.mozilla.org/en-US/docs/Web/API/Element/id.

func (Element) SetInnerHTML

func (n Element) SetInnerHTML(v string) Element

func (Element) SetTextContent

func (n Element) SetTextContent(v string) Element

func (Element) String

func (n Element) String() string

func (Element) Style

func (n Element) Style() Style

type Global

type Global struct {
	// contains filtered or unexported fields
}

func GetGlobal

func GetGlobal() Global

func (Global) Get

func (n Global) Get(name string) Element

type Location

type Location struct {
	// contains filtered or unexported fields
}

func (Location) Href

func (n Location) Href() string

func (Location) Reload

func (n Location) Reload(force bool) Location

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.

func (Rect) GetBottom

func (n Rect) GetBottom() int

func (Rect) GetHeight

func (n Rect) GetHeight() int

func (Rect) GetLeft

func (n Rect) GetLeft() int

func (Rect) GetRight

func (n Rect) GetRight() int

func (Rect) GetTop

func (n Rect) GetTop() int

func (Rect) GetWidth

func (n Rect) GetWidth() int

func (Rect) GetX

func (n Rect) GetX() int

func (Rect) GetY

func (n Rect) GetY() int

func (Rect) SetBottom

func (n Rect) SetBottom(b int) Rect

func (Rect) SetHeight

func (n Rect) SetHeight(height int) Rect

func (Rect) SetLeft

func (n Rect) SetLeft(l int) Rect

func (Rect) SetRight

func (n Rect) SetRight(r int) Rect

func (Rect) SetTop

func (n Rect) SetTop(t int) Rect

func (Rect) SetWidth

func (n Rect) SetWidth(width int) Rect

func (Rect) SetX

func (n Rect) SetX(x int) Rect

func (Rect) SetY

func (n Rect) SetY(y int) Rect

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

func (s Style) SetProperty(k, v string)

type Window

type Window struct {
	// contains filtered or unexported fields
}

func GetWindow

func GetWindow() Window

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) Document

func (n Window) Document() Document

Document returns a reference to the document contained in the window.

func (Window) HashChange

func (n Window) HashChange(f func()) Releasable

func (Window) InnerHeight

func (n Window) InnerHeight() int

func (Window) InnerWidth

func (n Window) InnerWidth() int

func (Window) Location

func (n Window) Location() Location

func (Window) MatchesMedia

func (n Window) MatchesMedia(criteria string) bool

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

func (Window) SetLocation

func (n Window) SetLocation(url string) Window

Directories

Path Synopsis
Package router provides a simple browser based router, which just works on the anchor element.
Package router provides a simple browser based router, which just works on the anchor element.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL