gowd
packagemoduleThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
README¶
gowd
Build cross platform GUI apps with GO and HTML/JS/CSS (powered bynwjs)
How to use this library:
- Download and installnwjs
- Install this library
go get github.com/dtylman/gowd
- Clone this repo.
- Place
package.json
,index.html
,main.go
andmain.js
fromtemplate in a new folder. go build
- Edit
main.js
and setgoBinary
to your your executable name:var goBinary = "./template"; //or template.exe
- Run
nw .
, the hello world template should appear:
Usage
Simplest "hello world":
import "github.com/dtylman/gowd"func main() {body, err := gowd.ParseElement("<h1>Hello world</h1>", nil)if err != nil {panic(err)}gowd.Run(body)}
Adding a button:
import ("github.com/dtylman/gowd")func main() {body, err := gowd.ParseElement("<h1>Hello world</h1>", nil)if err != nil {panic(err)}p := body.AddElement(gowd.NewElement("p"))btn := p.AddElement(gowd.NewElement("button"))btn.SetText("Click me")btn.OnEvent(gowd.OnClick, btnClicked)gowd.Run(body)}func btnClicked(sender *gowd.Element, event *gowd.EventElement) {sender.SetText("Clicked!")}
Creating and binding from HTML:
import ("github.com/dtylman/gowd""fmt")func main() {body, err := gowd.ParseElement("<h1>Hello world</h1>", nil)if err != nil {panic(err)}p := body.AddElement(gowd.NewElement("p"))em := gowd.NewElementMap()p.AddHtml(`<select id="select1"><option value="" disabled="disabled" selected="selected">Please select a name</option><option value="1">One</option><option value="2">Two</option></select>`, em)em["select1"].OnEvent(gowd.OnChange, btnClicked)em["select1"].Object = bodygowd.Run(body)}func btnClicked(sender *gowd.Element, event *gowd.EventElement) {body := sender.Object.(*gowd.Element)body.AddElement(gowd.NewStyledText(fmt.Sprintf("Selected %s", event.GetValue()), gowd.BoldText))body.AddElement(gowd.NewElement("br"))}
Using bootstrap:
'gowd' supports creating bootstrap elements using thebootstrap package.
First, add bootsrap css and js to yourindex.html
file:
<script type="text/javascript" src="js/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/> <script type="text/javascript" src="js/bootstrap.min.js"></script>
Then you can create bootsrap items:
import ("github.com/dtylman/gowd""github.com/dtylman/gowd/bootstrap""time""fmt")var body *gowd.Elementfunc main() {//creates a new bootstrap fluid containerbody = bootstrap.NewContainer(false)// add some elements using the object modeldiv := bootstrap.NewElement("div", "well")row := bootstrap.NewRow(bootstrap.NewColumn(bootstrap.ColumnLarge, 6, div))body.AddElement(row)// add some other elements from HTMLdiv.AddHTML(`<div class="dropdown"><button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example<span class="caret"></span></button><ul class="dropdown-menu" id="dropdown-menu"><li><a href="#">HTML</a></li><li><a href="#">CSS</a></li><li><a href="#">JavaScript</a></li></ul></div>`, nil)// add a button to show a progress barbtn := bootstrap.NewButton(bootstrap.ButtonPrimary, "Start")btn.OnEvent(gowd.OnClick, btnClicked)row.AddElement(bootstrap.NewColumn(bootstrap.ColumnLarge, 4, bootstrap.NewElement("div", "well", btn)))//start the ui loopgowd.Run(body)}// happens when the 'start' button is clickedfunc btnClicked(sender *gowd.Element, event *gowd.EventElement) {// adds a text and progress bar to the body text := body.AddElement(gowd.NewStyledText("Working...", gowd.BoldText))progressBar := bootstrap.NewProgressBar()body.AddElement(progressBar.Element)// makes the body stop responding to user eventsbody.Disable()// clean up - remove the added elementsdefer func() {body.RemoveElement(text)body.RemoveElement(progressBar.Element)body.Enable()}()// render the progress barfor i := 0; i <= 123; i++ {progressBar.SetValue(i, 123)text.SetText(fmt.Sprintf("Working %v", i))time.Sleep(time.Millisecond * 20)// this will cause the body to be refreshedbody.Render()}}
This will yield the following app:
More a more advanced usage, see theTodo sample
Documentation¶
Index¶
- Constants
- Variables
- func Alert(text string)
- func ExecJS(js string)
- func ExecJSNow(js string)
- func Run(body *Element) error
- type Element
- func NewElement(tag string) *Element
- func NewElementFromNode(node *html.Node, em ElementsMap) *Element
- func NewStyledText(text string, style string) *Element
- func NewText(text string) *Element
- func ParseElement(innerHTML string, em ElementsMap) (*Element, error)
- func ParseElementFromFile(fileName string, em ElementsMap) (*Element, error)
- func ParseElements(r io.Reader, em ElementsMap) ([]*Element, error)
- func (e *Element) AddElement(elem *Element) *Element
- func (e *Element) AddHTML(innerHTML string, em ElementsMap) ([]*Element, error)
- func (e *Element) AutoFocus()
- func (e *Element) Disable()
- func (e *Element) Enable()
- func (e *Element) Find(id string) *Element
- func (e *Element) GetAttribute(key string) (string, bool)
- func (e *Element) GetID() string
- func (e *Element) GetValue() string
- func (e *Element) Hide()
- func (e *Element) OnEvent(event string, handler EventHandler)
- func (e *Element) OnKeyPressEvent(event string, keyCode int, handler EventHandler)
- func (e *Element) ProcessEvent(event *Event)
- func (e *Element) RemoveAttribute(key string)
- func (e *Element) RemoveElement(elem *Element)
- func (e *Element) RemoveElements()
- func (e *Element) Render() error
- func (e *Element) SetAttribute(key, val string)
- func (e *Element) SetAttributes(event *EventElement)
- func (e *Element) SetClass(class string)
- func (e *Element) SetElement(elem *Element) *Element
- func (e *Element) SetID(id string)
- func (e *Element) SetText(text string)
- func (e *Element) SetValue(val string)
- func (e *Element) Show()
- func (e *Element) UnsetClass(class string)
- type ElementsMap
- type Event
- type EventElement
- type EventHandler
Constants¶
const (//OnClick onclick eventOnClick = "onclick"//OnChange onchange eventOnChange = "onchange"//OnKeyPress onkeypress eventOnKeyPress = "onkeypress")
const (//BoldText <b>BoldText = "b"//StrongText <strong>StrongText = "strong"//ItalicText <i>ItalicText = "i"//EmphasizedText <em>EmphasizedText = "em"//MarkedText <mark>MarkedText = "mark"//SmallText <small>SmallText = "small"//DeletedText <del>DeletedText = "del"//InsertedText <ins>InsertedText = "ins"//SubscriptText <sub>SubscriptText = "sub"//SuperscriptText <sup>SuperscriptText = "sup"//TitleText <title>TitleText = "title"//Paragraph <p>Paragraph = "p"//Heading1 <h1>Heading1 = "h1"//Heading2 <h2>Heading2 = "h2"//Heading3 <h3>Heading3 = "h3"//Heading4 <h4>Heading4 = "h4"//Heading5 <h5>Heading5 = "h5"//Heading6 <h6>Heading6 = "h6")
Variables¶
Functions¶
Types¶
typeElement¶
type Element struct {//Parent the parent elementParent *Element//Kids child elementsKids []*Element//Attributes element attributes...Attributes []html.Attribute//Object arbitrary user object that can be associated with element.Object interface{}//Hidden if true the element will not be renderedHiddenbool// contains filtered or unexported fields}
Element represents a DOM element and its state.
funcNewElementFromNode¶
func NewElementFromNode(node *html.Node, emElementsMap) *Element
NewElementFromNode creates an element from existing node
funcNewStyledText¶
NewStyledText creates new text element using a specific style
funcParseElement¶
func ParseElement(innerHTMLstring, emElementsMap) (*Element,error)
ParseElement parses an html with only one root tag, returns the root element.
funcParseElementFromFile¶
func ParseElementFromFile(fileNamestring, emElementsMap) (*Element,error)
ParseElementFromFile like ParseElement, but reads input from a file
funcParseElements¶
func ParseElements(rio.Reader, emElementsMap) ([]*Element,error)
ParseElements parse an html fragment and return a list of elements
func (*Element)AddElement¶
AddElement adds a child element
func (*Element)AddHTML¶
func (e *Element) AddHTML(innerHTMLstring, emElementsMap) ([]*Element,error)
AddHTML parses the provided element and adds it to the current element. Returns a list of root elements from `html`.If em is not nil, for each HTML tag that has the `id` attribute set the corresponding element will be stored in thegiven ElementMap.
func (*Element)AutoFocus¶
func (e *Element) AutoFocus()
AutoFocus adds the auto-focus atribute to the element
func (*Element)GetAttribute¶
GetAttribute returns value for attribute
func (*Element)OnEvent¶
func (e *Element) OnEvent(eventstring, handlerEventHandler)
OnEvent register an DOM element event.
func (*Element)OnKeyPressEvent¶
func (e *Element) OnKeyPressEvent(eventstring, keyCodeint, handlerEventHandler)
OnKeyPressEvent register handler as an OnKeyPressed event.
func (*Element)ProcessEvent¶
ProcessEvent fires the event provided
func (*Element)RemoveAttribute¶
RemoveAttribute removes the provided attribute by name
func (*Element)RemoveElement¶
RemoveElement remove a specific kid
func (*Element)SetAttribute¶
SetAttribute adds or set the attribute
func (*Element)SetAttributes¶
func (e *Element) SetAttributes(event *EventElement)
SetAttributes sets attributes from an event element.
func (*Element)SetElement¶
SetElement removes all child elemens and adds elem as first child
func (*Element)UnsetClass¶
UnsetClass removes the given class name from the class attribute
typeElementsMap¶
ElementsMap maps Elements by their `id` attributes
typeEvent¶
type Event struct {Namestring `json:"name"`SenderEventElement `json:"sender"`Inputs []EventElement `json:"inputs"`}
Event represents a DOM event
typeEventElement¶
EventElement represents the DOM element sending an event
func (*EventElement)GetValue¶
func (e *EventElement) GetValue()string
GetValue gets the value of the event sender.
typeEventHandler¶
type EventHandler func(sender *Element, event *EventElement)
EventHandler handler for DOM event.