jogs

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

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

Go to latest
Published: Mar 20, 2017 License: MIT Imports: 7 Imported by: 3

README

jogs

generate editors (web forms) from go structs, specifically meant for developing single page web apps

installation
go get -u github.com/DapperDodo/jogs
sweet spot

jogs may be the right tool for the job if you can say Yes to the following questions:

  • are you using gopherjs to generate the javascript for your single page web app?
  • do you want to make admin facing edit forms for your API, maybe for a CMS?
  • do you already have API clients written in Go?

Yes, Yes, Yes? Sweet!

sweet spot use: main.go
// +build js
package main

import (
	"github.com/DapperDodo/jogs"
	
	// suppose you already have go code containing
	// data type definitions and an api client
	"your/api"
)

func main() {

	// thanks to gopherjs
	// you can simply use your go client to retrieve data from your API
	data := api.Client.Read()

	// thanks to jogs
	// you can automagically generate a web based editor for this data object
	jogs.Root(jogs.DefaultDispatcher, "div-id-editor", data, func(data_out interface{}) {
	
		// thanks to gopherjs
		// you can simply use your go client to save the data after every edit action
		go api.Client.Update(data_out)
	})
}
simple use: main.go
// +build js
package main

import (
	"encoding/json"

	"github.com/gopherjs/jquery"

	"github.com/DapperDodo/jogs"
)

type Data struct {
	Id     int
	GameId string
	Dialog []string
}

func main() {

	data := &Data{
		Id:     1,
		GameId: "444-555-666",
		Dialog: []string{"foo", "bar", "qux"},
	}

	showData(data)

	// start editing
	jogs.Root(jogs.DefaultDispatcher, "panel-content", data, func(data_out interface{}) {
		showData(data)
	})
}

func showData(data interface{}) {
	d, _ := json.MarshalIndent(data, "<br/>", "&nbsp;&nbsp;&nbsp;&nbsp;")
	jquery.NewJQuery("#panel-title").SetHtml(string(d))
}

run this example using gopherjs' built in server:

gopherjs serve

and navigate a browser to:

http://localhost:8080/github.com/DapperDodo/jogs/demo/main/
advanced use: main.go

Jogs can handle complex data structures, for example nesting and slices are fully supported. (TODO: maps, floats and bools)

Also, Jogs sports a powerful plugin structure that makes overriding and extending easy. Custom handlers can be registered, after which struct tags can be used to tell Jogs where to use these custom handlers.

// +build js
package main

import (
	"encoding/json"

	"github.com/gopherjs/jquery"

	"github.com/DapperDodo/jogs"
	"github.com/DapperDodo/jogs/demo/custom_handler"
)

type Data struct {
	Id     int
	GameId string `jogs:"custom"`
	Dialog []string
	Nested Nested
}

type Nested struct {
	Id   int
	Name string
	Nums []int
}

func main() {

	data := &Data{
		Id:     1,
		GameId: "444-555-666",
		Dialog: []string{"foo", "bar", "qux"},
		Nested: Nested{2, "dodo", []int{10, 20}},
	}

	showData(data)

	// register custom handlers
	custom_handler.RegisterAll(jogs.DefaultDispatcher)

	// start editing
	jogs.Root(jogs.DefaultDispatcher, "panel-content", data, func(data_out interface{}) {
		showData(data)
	})

}

func showData(data interface{}) {
	d, _ := json.MarshalIndent(data, "<br/>", "&nbsp;&nbsp;&nbsp;&nbsp;")
	jquery.NewJQuery("#panel-title").SetHtml(string(d))
}

Documentation

Index

Constants

View Source
const (
	OVERRIDE_HANDLER     int = iota // tag field 0 is for custom handlers
	OVERRIDE_LABEL                  // tag field 1 is for overriding the field label (name)
	OVERRIDE_PLACEHOLDER            // tag field 2 is for overriding the placeholder (text to display when the field is empty)
	PARAM_1                         // tag field 3 is for passing a configuration parameter to the handler
	PARAM_2                         // tag field 4 is for passing a configuration parameter to the handler
	PARAM_3                         // tag field 5 is for passing a configuration parameter to the handler
)

Variables

Convenience handle to jQuery

Functions

func Merge

func Merge(skin *template.Template, tpl string, data interface{}) string

func Root

func Root(d *Dispatcher, container string, obj interface{}, cb Callback)

Root is the entry function of jogs. It creates the root node and calls the dispatcher, effectively starting the process of building the editor for our data

TODO: maybe this is superfluous. Could clients create the root node and call the dispatcher themselves?

Types

type Callback

type Callback func(interface{})

Callback propagates changes to the value object

type ConstSelector

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

usage:

type Example struct {
	Fubr  string   `jogs:"CONST,Fuber,select fuber...,FOO BAR QUX FOX"`
}

func (*ConstSelector) Handle

func (h *ConstSelector) Handle(node Node, cb Callback)

type Dispatcher

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

Dispatcher decouples handles from handlers. This makes jogs a very flexible and extensible tool.

var DefaultDispatcher *Dispatcher

func (*Dispatcher) Register

func (d *Dispatcher) Register(handle string, plugin Handler)

type Handler

type Handler interface {
	Handle(node Node, cb Callback)
}

handler is the interface that node editors should implement

type HandlerFunc

type HandlerFunc func(node Node, cb Callback)

HandlerFunc is a wrapper that makes any function with signature func(Node, Callback) implement jogs' handler interface

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(node Node, cb Callback)

type Node

type Node struct {
	Object      interface{} // the actual data object that we want to edit
	ContainerId string      // the id of the dom node in which we want this editor placed, a.k.a. parent
	EditorId    string      // the id of the dom node of this editor
	Handle      string      // a key to the handler that will build this node's editor
	Label       string      // the name of this editor, also the form field name
	Placeholder string      // text to display when the field is empty
	Idx         int         // the index of the node in the struct field list or slice
	Tags        []string    // (optional) arguments for the handler, filled by parsing a struct tag called 'jogs' with comma separated strings (see OVERRIDE_ and PARAM_constants)
}

Node is a structure containing all necessary info for building an editor node.

func (*Node) GetTag

func (n *Node) GetTag(idx int) string

Directories

Path Synopsis
demo
main
+build js
+build js
+build js
+build js

Jump to

Keyboard shortcuts

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