TestPlaceholder function

Parameters:

  • t *testing.T
Show/Hide Function Body
{}

On function

On attaches a handler function for the given event to target.

Optional opts are forwarded to addEventListener as-is.

It returns a function that removes the listener and releases resources.

Parameters:

  • event string
  • target js.Value
  • handler func(js.Value)
  • opts ...any

Returns:

  • func()
Show/Hide Function Body
{
	fn := js.FuncOf(func(this js.Value, args []js.Value) any {
		if len(args) > 0 {
			handler(args[0])
		} else {
			handler(js.Null())
		}
		return nil
	})
	callArgs := []any{event, fn}
	if len(opts) > 0 {
		callArgs = append(callArgs, opts...)
	}
	target.Call("addEventListener", callArgs...)
	return func() {
		target.Call("removeEventListener", event, fn)
		fn.Release()
	}
}

OnClick function

OnClick attaches a click handler to target.

Parameters:

  • target js.Value
  • handler func(js.Value)

Returns:

  • func()
Show/Hide Function Body
{
	return On("click", target, handler)
}

OnScroll function

OnScroll attaches a scroll handler to target.

Parameters:

  • target js.Value
  • handler func(js.Value)

Returns:

  • func()
Show/Hide Function Body
{
	return On("scroll", target, handler)
}

OnInput function

OnInput attaches an input handler to target.

Parameters:

  • target js.Value
  • handler func(js.Value)

Returns:

  • func()
Show/Hide Function Body
{
	return On("input", target, handler)
}

OnTimeUpdate function

OnTimeUpdate attaches a timeupdate handler to target.

Parameters:

  • target js.Value
  • handler func(js.Value)

Returns:

  • func()
Show/Hide Function Body
{
	return On("timeupdate", target, handler)
}

OnKeyDown function

OnKeyDown attaches a keydown handler to the window object.

Parameters:

  • handler func(js.Value)

Returns:

  • func()
Show/Hide Function Body
{
	return On("keydown", js.Window(), handler)
}

OnKeyUp function

OnKeyUp attaches a keyup handler to the window object.

Parameters:

  • handler func(js.Value)

Returns:

  • func()
Show/Hide Function Body
{
	return On("keyup", js.Window(), handler)
}

Listen function

Listen attaches an event listener to target and returns a channel

that receives the first argument of the event callback.

Parameters:

  • event string
  • target js.Value

Returns:

  • <-chan js.Value
Show/Hide Function Body
{
	ch := make(chan js.Value)
	fn := js.FuncOf(func(this js.Value, args []js.Value) any {
		if len(args) > 0 {
			ch <- args[0]
		} else {
			ch <- js.Null()
		}
		return nil
	})
	target.Call("addEventListener", event, fn)
	return ch
}

ObserveMutations function

ObserveMutations observes DOM mutations on the first node matching sel.

It returns a channel receiving MutationRecord objects and a stop function

that disconnects the observer and releases resources.

Parameters:

  • sel string

Returns:

  • <-chan js.Value
  • func()
Show/Hide Function Body
{
	ch := make(chan js.Value)
	node := js.Document().Call("querySelector", sel)
	fn := js.FuncOf(func(this js.Value, args []js.Value) any {
		mutations := args[0]
		for i := 0; i < mutations.Length(); i++ {
			m := mutations.Index(i)
			t := m.Get("target")
			if t.Truthy() && t.Get("closest").Type() != js.TypeUndefined {
				if t.Call("closest", "[data-rfw-ignore]").Truthy() {
					continue
				}
			}
			ch <- m
		}
		return nil
	})
	observer := js.MutationObserver().New(fn)
	opts := js.NewDict()
	opts.Set("childList", true)
	opts.Set("subtree", true)
	observer.Call("observe", node, opts.Value)
	stop := func() {
		observer.Call("disconnect")
		fn.Release()
	}
	return ch, stop
}

ObserveIntersections function

ObserveIntersections observes intersections for elements matching sel.

opts is passed directly to the IntersectionObserver constructor.

It returns a channel receiving IntersectionObserverEntry objects and a

stop function to disconnect the observer and release resources.

Parameters:

  • sel string
  • opts js.Value

Returns:

  • <-chan js.Value
  • func()
Show/Hide Function Body
{
	ch := make(chan js.Value)
	fn := js.FuncOf(func(this js.Value, args []js.Value) any {
		entries := args[0]
		for i := 0; i < entries.Length(); i++ {
			ch <- entries.Index(i)
		}
		return nil
	})
	observer := js.IntersectionObserver().New(fn, opts)
	nodes := js.Document().Call("querySelectorAll", sel)
	for i := 0; i < nodes.Length(); i++ {
		observer.Call("observe", nodes.Index(i))
	}
	stop := func() {
		observer.Call("disconnect")
		fn.Release()
	}
	return ch, stop
}

testing import

Import example:

import "testing"

github.com/rfwlab/rfw/v1/js import

Import example:

import "github.com/rfwlab/rfw/v1/js"

Imported as:

js