Documentation
¶
Overview ¶
Package jawstree provides a JaWS widget and embedded assets for the Quercus.js treeview library.
Example ¶
package main
import (
"embed"
"log/slog"
"net/http"
"sync"
"github.com/linkdata/jaws"
"github.com/linkdata/jaws/jawsboot"
"github.com/linkdata/jaws/jawstree"
"github.com/linkdata/jaws/lib/bind"
"github.com/linkdata/jaws/lib/templatereloader"
"github.com/linkdata/jaws/lib/ui"
"github.com/linkdata/staticserve"
)
// This example assumes an 'assets' directory:
//
//. assets/
//. static/
//. images/
//. favicon.png
//. ui/
//. index.html
//go:embed assets
var assetsFS embed.FS
func setupJaws(jw *jaws.Jaws, mux *http.ServeMux) (err error) {
mux.Handle("GET /jaws/", jw) // Ensure the JaWS routes are handled
var tmpl jaws.TemplateLookuper
if tmpl, err = templatereloader.New(assetsFS, "assets/ui/*.html", ""); err == nil {
jw.AddTemplateLookuper(tmpl)
// Initialize jawsboot; we will serve the JavaScript and CSS from /static/*.[js|css].
// All files under assets/static will be available under /static. Any favicon loaded
// this way will have its URL available using jaws.FaviconURL().
if err = jw.Setup(mux.Handle, "/static",
jawsboot.Setup,
jawstree.Setup,
staticserve.MustNewFS(assetsFS, "assets/static", "images/favicon.png"),
); err == nil {
// Add a route to our index template with a bound variable accessible as '.Dot' in the template
var mu sync.Mutex
var f float64
mux.Handle("GET /", ui.Handler(jw, "index.html", bind.New(&mu, &f)))
}
}
return
}
func main() {
jw, err := jaws.New()
if err == nil {
jw.Logger = slog.Default()
if err = setupJaws(jw, http.DefaultServeMux); err == nil {
// start the JaWS processing loop and the HTTP server
go jw.Serve()
slog.Error(http.ListenAndServe("localhost:8080", nil).Error())
}
}
if err != nil {
panic(err)
}
}
Output:
Index ¶
- func Setup(jw *jaws.Jaws, handleFn jaws.HandleFunc, prefix string) (urls []*url.URL, err error)
- type Node
- func (node *Node) GetNames() (names []string)
- func (node *Node) GetSelected() (nameLists [][]string)
- func (node *Node) HasNames(names []string) (yes bool)
- func (node *Node) JawsPathSet(elem *jaws.Element, jsPath string, value any)
- func (node *Node) MarshalJSON() (b []byte, err error)
- func (node *Node) SetSelected(nameLists [][]string) (changed []*Node)
- func (node *Node) Walk(jsPath string, fn func(jsPath string, node *Node))
- type Option
- type Tree
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Setup ¶
Setup registers embedded jawstree static assets under prefix.
It is intended to be passed to jaws.Jaws.Setup. Returned URLs should be included in the page head through jaws.Jaws.GenerateHeadHTML.
Types ¶
type Node ¶
type Node struct {
Tree *Tree `json:"-"` // owning tree, set by New
Parent *Node `json:"-"` // parent node, nil for root
Name string `json:"name"` // display name
ID string `json:"id,omitzero"` // JSON path ID, set by New
Selected bool `json:"selected,omitzero"` // selected state
Disabled bool `json:"disabled,omitzero"` // false when selectable
Children []*Node `json:"children,omitzero"` // child nodes
}
Node is one tree node rendered by Tree.
func Root ¶
func Root(r *os.Root, filterFn func(dirpath string, ent fs.DirEntry) (include bool)) (rootnode *Node, err error)
Root builds a root node from an os.Root. If filterFn is not nil, it must return true for a directory entry to be included in the tree.
func (*Node) GetSelected ¶
GetSelected returns the paths of all selected nodes.
func (*Node) JawsPathSet ¶
JawsPathSet mirrors browser-side selected-state changes back into the tree.
func (*Node) MarshalJSON ¶ added in v0.300.0
MarshalJSON writes the Quercus.js JSON shape for n.
func (*Node) SetSelected ¶
SetSelected applies selected paths and returns nodes that changed.
type Option ¶ added in v0.300.0
type Option int
Option configures a Tree.
const ( // SearchEnabled enables tree search controls. SearchEnabled Option = (1 << iota) // InitiallyExpanded renders nodes expanded initially. InitiallyExpanded // MultiSelectEnabled allows multiple selected nodes. MultiSelectEnabled // ShowSelectAllButton shows a select-all control. ShowSelectAllButton // ShowInvertSelectionButton shows an invert-selection control. ShowInvertSelectionButton // ShowExpandCollapseAllButtons shows expand/collapse-all controls. ShowExpandCollapseAllButtons // NodeSelectionDisabled disables node selection. NodeSelectionDisabled // CascadeSelectChildren cascades selection to child nodes. CascadeSelectChildren // CheckboxSelectionEnabled renders checkbox selection controls. CheckboxSelectionEnabled )
type Tree ¶
Tree renders and updates a Quercus.js tree bound to a ui.JsVar.
func New ¶
New returns a tree widget with id, jsvar and options.
New initializes node IDs and tree back-pointers in jsvar.Ptr.
func (*Tree) JawsRender ¶
JawsRender renders the hidden root data element and tree initialization script.
func (*Tree) JawsUpdate ¶ added in v0.300.0
JawsUpdate sends the latest tree JSON to the browser.