Documentation
¶
Overview ¶
Package mvc provides a thin model-view-controller layer for building declarative WASM user interfaces using the go-wasmbuild DOM wrappers.
Index ¶
- Constants
- func Counter(label string) string
- func HTML(tagName string, args ...any) dom.Element
- func HasNextPage(state PaginationState) bool
- func HasPreviousPage(state PaginationState) bool
- func New(args ...any) *app
- func NewController(self Controller, fn func(self, child Controller), view ...View) *controller
- func NodeFromAny(child any) dom.Node
- func Page(state PaginationState) uint
- func PageCount(state PaginationState) uint
- func PageEnd(state PaginationState) uint
- func PageStart(state PaginationState) uint
- func Placeholder(args ...any) dom.Element
- func RegisterView(name string, constructor ViewConstructorFunc, eventtypes ...string)
- func RegisteredEvents(name string) []string
- func Router(args ...any) *router
- func Table(args ...any) *table
- func TableHeader(args ...any) *tableHeader
- func TableRow(args ...any) *tableRow
- func Text(text string) dom.Text
- type ActiveGroup
- type ActiveState
- type AddedEvent
- type ChangedEvent
- type Controller
- type DeletedEvent
- type EnabledGroup
- type EnabledState
- type JSONProvider
- type Keyed
- type KeyedModel
- func (m *KeyedModel[K, T]) Append(items ...T)
- func (m *KeyedModel[K, T]) Clear()
- func (m *KeyedModel[K, T]) Get(key K) (T, bool)
- func (m *KeyedModel[K, T]) Items() []T
- func (m *KeyedModel[K, T]) Len() int
- func (m *KeyedModel[K, T]) OnAdded(fn func(AddedEvent[T]))
- func (m *KeyedModel[K, T]) OnChanged(fn func(ChangedEvent[T]))
- func (m *KeyedModel[K, T]) OnDeleted(fn func(DeletedEvent[T]))
- func (m *KeyedModel[K, T]) OnSet(fn func([]T))
- func (m *KeyedModel[K, T]) Remove(key K)
- func (m *KeyedModel[K, T]) Set(items []T)
- func (m *KeyedModel[K, T]) Update(item T)
- type LabelState
- type Model
- type Opt
- type OptSet
- type PaginationState
- type Provider
- type ValueState
- type View
- func NewView(self View, name string, template string, fn func(View, View), args ...any) View
- func NewViewWithElement(self View, element dom.Element, fn func(View, View), opts ...Opt) View
- func ViewFromElement(element dom.Element) (View, error)
- func ViewFromEvent(e dom.Event, views ...string) View
- func ViewFromEventTarget(e dom.Event, views ...string) View
- type ViewConstructorFunc
- type VisibleGroup
- type VisibleState
Constants ¶
const ( ViewRouter = "mvc-router" EventRouterActivate = "mvc-router-activate" EventRouterDeactivate = "mvc-router-deactivate" )
const ( ViewTable = "mvc-table" ViewTableHeader = "mvc-table-header" ViewTableRow = "mvc-table-row" )
const ( // The attribute key which identifies an mvc component DataComponentAttrKey = "data-mvc" // The attribute key which identifies a slot in a component DataSlotAttrKey = "data-slot" // The name of the default slot, when name atribute is missing ContentSlot = "body" )
const ( // ViewApp is the registered component name used for the root application // container within the MVC system. ViewApp = "mvc-app" )
Variables ¶
This section is empty.
Functions ¶
func Counter ¶ added in v0.0.2
Counter returns an attribute value function which generates unique IDs
func HasNextPage ¶ added in v0.0.2
func HasNextPage(state PaginationState) bool
HasNextPage reports whether there is a page after the current one.
func HasPreviousPage ¶ added in v0.0.2
func HasPreviousPage(state PaginationState) bool
HasPreviousPage reports whether there is a page before the current one.
func New ¶
func New(args ...any) *app
New creates an empty application root, attaches it to the document body and returns the view so callers can begin composing content.
func NewController ¶
func NewController(self Controller, fn func(self, child Controller), view ...View) *controller
func NodeFromAny ¶
NodeFromAny returns a Node from a string, Element, Tag or View or returns nil if the type is unsupported
func Page ¶ added in v0.0.2
func Page(state PaginationState) uint
Page returns the current 1-based page number derived from offset and limit. Returns 0 when the page cannot be determined because limit is zero.
func PageCount ¶ added in v0.0.2
func PageCount(state PaginationState) uint
PageCount returns the total number of pages derived from count and limit. Returns 0 when the page count cannot be determined because limit is zero.
func PageEnd ¶ added in v0.0.2
func PageEnd(state PaginationState) uint
PageEnd returns the zero-based exclusive index of the last item on the current page, clamped to the known total count.
func PageStart ¶ added in v0.0.2
func PageStart(state PaginationState) uint
PageStart returns the zero-based inclusive index of the first item on the current page.
func Placeholder ¶
Placeholder returns a placeholder element which is not rendered and has no effect
func RegisterView ¶
func RegisterView(name string, constructor ViewConstructorFunc, eventtypes ...string)
RegisterView registers a view constructor function for a given name, so that the view can be created on-demand, and zero or more event types that a controller which attaches to this view should listen for.
func RegisteredEvents ¶ added in v0.0.2
RegisteredEvents returns the event types registered for the named view. Returns nil if the view is not registered.
func Table ¶ added in v0.0.2
func Table(args ...any) *table
Table creates a basic HTML table with header and body slots.
func TableHeader ¶ added in v0.0.2
func TableHeader(args ...any) *tableHeader
TableHeader creates a table header row.
Types ¶
type ActiveGroup ¶ added in v0.0.2
ActiveGroup is implemented by a container that manages which of its member views are active. Calling SetActive with no arguments deactivates all members.
type ActiveState ¶ added in v0.0.2
ActiveState is implemented by a view that can be marked active or inactive.
type AddedEvent ¶ added in v0.0.2
type AddedEvent[T any] struct { Item T // the item that was added Index int // its position in the list Items []T // full slice after insertion (do not modify) }
AddedEvent is emitted when a new item is inserted into a KeyedModel.
type ChangedEvent ¶ added in v0.0.2
type ChangedEvent[T any] struct { Item T // the updated item Index int // its position in the list Items []T // full slice after update (do not modify) }
ChangedEvent is emitted when an existing item is updated in place.
type Controller ¶
type Controller interface {
// Return all the views
Views() []View
// Attach one or more views to this controller
Attach(...View)
// Detach one or more views from this controller
Detach(...View)
// Fire an action based on an event from a view
EventListener(string, View)
}
A controller reacts to events from one or more views
type DeletedEvent ¶ added in v0.0.2
type DeletedEvent[T any] struct { Item T // the item that was removed Index int // the position it occupied before removal Items []T // full slice after removal (do not modify) }
DeletedEvent is emitted when an item is removed from a KeyedModel.
type EnabledGroup ¶ added in v0.0.2
EnabledGroup is implemented by a container that manages the enabled/disabled state of its member views. The passed views are enabled; all others are disabled. Calling SetEnabled with no arguments disables all members.
type EnabledState ¶ added in v0.0.2
EnabledState is implemented by a view that can be marked enabled or disabled.
type JSONProvider ¶ added in v0.0.2
type JSONProvider[T any] interface { // Get fetches from the given path and decodes the response as T. Get(path string, opts ...js.FetchOption) // GetWithInterval calls Get immediately and then repeatedly at the given // interval. Call Cancel to stop. GetWithInterval(path string, interval time.Duration, opts ...js.FetchOption) // Cancel stops any active interval. Cancel() // Post marshals body as JSON, posts to path, and decodes the response as T. Post(path string, body T, opts ...js.FetchOption) // Put marshals body as JSON, puts to path, and decodes the response as T. Put(path string, body T, opts ...js.FetchOption) // Patch marshals body as JSON, patches path, and decodes the response as T. Patch(path string, body T, opts ...js.FetchOption) // Delete sends a DELETE to path and decodes any response body as T. // If the server returns no body (e.g. 204 No Content), the listener // is called with the zero value of T and a nil error. Delete(path string, opts ...js.FetchOption) // AddEventListener registers a listener called on every completed request. // value is the zero value of T on error; err is nil on success. AddEventListener(fn func(T, error)) }
JSONProvider fetches JSON data from a remote source, encoding and decoding values of type T. Use AddEventListener to receive decoded results.
func NewJSONProvider ¶ added in v0.0.2
func NewJSONProvider[T any](base *url.URL) JSONProvider[T]
NewJSONProvider creates a new JSONProvider with the given base URL. Returns nil if base is nil.
type Keyed ¶ added in v0.0.2
type Keyed[K comparable] interface { PrimaryKey() K }
Keyed is implemented by types that have a unique, comparable primary key. KeyedModel uses this to emit fine-grained "added" and "deleted" events.
type KeyedModel ¶ added in v0.0.2
type KeyedModel[K comparable, T Keyed[K]] struct { // contains filtered or unexported fields }
KeyedModel stores an ordered slice of items of type T (which must implement Keyed[K]).
Set and Clear only fire OnSet listeners — added/deleted/changed are not fired. Append fires OnAdded for each new key, then OnSet. Remove fires OnDeleted, then OnSet. Update fires OnChanged (existing key) or OnAdded (new key), then OnSet.
func (*KeyedModel[K, T]) Append ¶ added in v0.0.2
func (m *KeyedModel[K, T]) Append(items ...T)
Append adds items that do not already exist (by key). For each new item, OnAdded fires before OnSet. Items with an existing key are silently skipped.
func (*KeyedModel[K, T]) Clear ¶ added in v0.0.2
func (m *KeyedModel[K, T]) Clear()
Clear removes all items. Only OnSet listeners fire.
func (*KeyedModel[K, T]) Get ¶ added in v0.0.2
func (m *KeyedModel[K, T]) Get(key K) (T, bool)
Get returns the item with the given key and true, or the zero value and false if no such item exists.
func (*KeyedModel[K, T]) Items ¶ added in v0.0.2
func (m *KeyedModel[K, T]) Items() []T
Items returns a shallow copy of the stored items.
func (*KeyedModel[K, T]) Len ¶ added in v0.0.2
func (m *KeyedModel[K, T]) Len() int
Len returns the number of items in the model.
func (*KeyedModel[K, T]) OnAdded ¶ added in v0.0.2
func (m *KeyedModel[K, T]) OnAdded(fn func(AddedEvent[T]))
OnAdded registers fn to be called when a new item is inserted.
func (*KeyedModel[K, T]) OnChanged ¶ added in v0.0.2
func (m *KeyedModel[K, T]) OnChanged(fn func(ChangedEvent[T]))
OnChanged registers fn to be called when an existing item is updated.
func (*KeyedModel[K, T]) OnDeleted ¶ added in v0.0.2
func (m *KeyedModel[K, T]) OnDeleted(fn func(DeletedEvent[T]))
OnDeleted registers fn to be called when an item is removed.
func (*KeyedModel[K, T]) OnSet ¶ added in v0.0.2
func (m *KeyedModel[K, T]) OnSet(fn func([]T))
OnSet registers fn to be called after any mutation with the full item slice. fn must not modify the slice.
func (*KeyedModel[K, T]) Remove ¶ added in v0.0.2
func (m *KeyedModel[K, T]) Remove(key K)
Remove deletes the item with the given key. OnDeleted fires (with the former index) then OnSet fires. No-op if the key does not exist.
func (*KeyedModel[K, T]) Set ¶ added in v0.0.2
func (m *KeyedModel[K, T]) Set(items []T)
Set replaces all items wholesale. Only OnSet listeners fire; added, deleted, and changed listeners are not called.
func (*KeyedModel[K, T]) Update ¶ added in v0.0.2
func (m *KeyedModel[K, T]) Update(item T)
Update replaces the item that shares the same primary key, firing OnChanged then OnSet. If no item with that key exists yet, it is appended and OnAdded fires instead.
type LabelState ¶ added in v0.0.2
LabelState is implemented by a view that can set a label on a control
type Model ¶ added in v0.0.2
type Model[T any] struct { // contains filtered or unexported fields }
Model stores an ordered slice of items of type T and notifies registered listeners whenever the contents change.
func (*Model[T]) AddEventListener ¶ added in v0.0.2
func (m *Model[T]) AddEventListener(fn func([]T))
AddEventListener registers fn to be called whenever the model changes. fn receives the current item slice (not a copy — do not modify it).
func (*Model[T]) Append ¶ added in v0.0.2
func (m *Model[T]) Append(items ...T)
Append adds items to the end of the slice and notifies all listeners.
func (*Model[T]) Clear ¶ added in v0.0.2
func (m *Model[T]) Clear()
Clear removes all items and notifies all listeners.
func (*Model[T]) Items ¶ added in v0.0.2
func (m *Model[T]) Items() []T
Items returns a shallow copy of the stored items.
type Opt ¶
Opt is a function which can apply options to a view
func WithAriaLabel ¶
WithAriaLabel adds an aria-label attribute to a view
func WithSlotAttr ¶ added in v0.0.2
func WithoutAttr ¶
func WithoutClass ¶
type OptSet ¶
type OptSet interface {
// Return the name of the view
Name() string
// Return the classes of the view
Classes() []string
// Return an attribute value
Attr(key string) string
}
OptSet interface for applying options
type PaginationState ¶ added in v0.0.2
type PaginationState interface {
Offset() uint
SetOffset(uint) View
Limit() uint
SetLimit(uint) View
Count() uint
SetCount(uint) View
}
PaginationState is implemented by a view that tracks paginated collection state in terms of offset, limit, and total count.
Page numbers and navigation state are derived from these values rather than stored independently.
type Provider ¶ added in v0.0.2
type Provider interface {
// Fetch performs a single fetch and calls all listeners with the response or error.
Fetch(path string, opts ...js.FetchOption)
// FetchWithInterval starts periodic fetching at the given interval, calling
// all listeners on each attempt. Fetches immediately, then repeats.
// Call Cancel to stop.
FetchWithInterval(path string, interval time.Duration, opts ...js.FetchOption)
// Cancel stops any active interval fetch.
Cancel()
// AddEventListener registers a listener called on every fetch completion.
// The response is nil on error; err is nil on success.
AddEventListener(fn func(*js.FetchResponse, error))
}
Provider fetches data from a remote source and notifies registered listeners. Use AddEventListener to react to each fetch result (data or error).
func NewProvider ¶ added in v0.0.2
NewProvider creates a new provider with a base URL.
type ValueState ¶ added in v0.0.2
ValueState is implemented by a view that exposes a string value.
type View ¶
type View interface {
// Return the view name
Name() string
// Return the view ID, if set
ID() string
// Return the view's root element
Root() dom.Element
// Return the view's parent view
Parent() View
// Return the view's direct child views from the default content slot
Children() []View
// Return self
Self() View
// Return a slot element by name, or nil if not found
Slot(string) dom.Element
// Replace a named slot with a node amnd apply options to the slot
ReplaceSlot(string, any, ...Opt) View
// Replace the children of a named slot with views, elements or text and apply options to the slot
ReplaceSlotChildren(name string, args ...any) View
// Apply class and attribute options to the root element
Apply(...Opt) View
// Replace the content of the view
Content(...any) View
// Add an event listener to the view's root element
AddEventListener(string, func(dom.Event)) View
// Remove an event listener from the view's root element
RemoveEventListener(string) View
}
View represents a web component in the interface
func NewViewWithElement ¶
Create view from an existing element, applying any options to it
func ViewFromElement ¶ added in v0.0.2
ViewFromElement returns the View registered for the given element, or nil if the element has no data-mvc attribute or the view is not registered.
func ViewFromEvent ¶
ViewFromEvent returns a View from an Event, or nil if the type is unsupported or not found. If one or more view names are provided, only views with those names are returned.
type ViewConstructorFunc ¶
Constructor function for views
type VisibleGroup ¶ added in v0.0.2
VisibleGroup is implemented by a container that manages the visible/hidden state of its member views. The passed views are made visible; all others are hidden. Calling SetVisible with no arguments hides all members.
type VisibleState ¶ added in v0.0.2
VisibleState is implemented by a view that can be shown or hidden.