jawstree

package
v0.116.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: MIT Imports: 14 Imported by: 0

README

jawstree

Provides a statically served and embedded version of Quercus.js, a lightweight and customizable JavaScript treeview library with no dependencies.

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/staticserve"
	"github.com/linkdata/jaws/templatereloader"
)

// 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("/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 it's 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("/", jw.Handler("index.html", jaws.Bind(&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)
	}
}

The example expects an assets directory in the source tree:

assets
├── static
│   └── images
│       └── favicon.png
└── ui
    ├── somepage.html
    ├── otherpage.html
    └── index.html

Documentation

Overview

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/staticserve"
	"github.com/linkdata/jaws/templatereloader"
)

// 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("/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 it's 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("/", jw.Handler("index.html", jaws.Bind(&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)
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Setup

func Setup(jw *jaws.Jaws, handleFn jaws.HandleFunc, prefix string) (urls []*url.URL, err error)

Types

type Node

type Node struct {
	Tree     *Tree   `json:"-"`
	Parent   *Node   `json:"-"`
	ID       string  `json:"id,omitzero"`
	Name     string  `json:"name"`
	Selected bool    `json:"selected,omitzero"`
	Children []*Node `json:"children,omitzero"`
}

func Root

func Root(r *os.Root) (rootnode *Node, err error)

func (*Node) GetNames

func (n *Node) GetNames() (names []string)

func (*Node) GetSelected

func (n *Node) GetSelected() (nameslist [][]string)

func (*Node) HasNames

func (n *Node) HasNames(names []string) (yes bool)

func (*Node) JawsPathSet

func (n *Node) JawsPathSet(elem *jaws.Element, jspath string, value any)

func (*Node) SetSelected

func (n *Node) SetSelected(nameslist [][]string) (changed []*Node)

func (*Node) Walk

func (n *Node) Walk(jspath string, fn func(jspath string, node *Node))

type Tree

type Tree struct {
	*jaws.JsVar[Node]
	// contains filtered or unexported fields
}

func New

func New(id string, jsvar *jaws.JsVar[Node]) (t *Tree)

func (*Tree) JawsRender

func (t *Tree) JawsRender(e *jaws.Element, w io.Writer, params []any) (err error)

Jump to

Keyboard shortcuts

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