Browse Source

Keep working on prototype

master
Brett Langdon 7 years ago
parent
commit
1fb3e02dbc
No known key found for this signature in database GPG Key ID: B664881177781B04
22 changed files with 414 additions and 174 deletions
  1. +33
    -18
      document.go
  2. +29
    -10
      element.go
  3. +7
    -0
      elementiface.go
  4. +35
    -0
      event.go
  5. +8
    -0
      eventiface.go
  6. +1
    -1
      eventtargetiface.go
  7. +5
    -23
      generate/document.json
  8. +3
    -12
      generate/element.json
  9. +40
    -0
      generate/elementiface.json
  10. +6
    -0
      generate/event.json
  11. +37
    -0
      generate/eventiface.json
  12. +1
    -1
      generate/eventtargetiface.json
  13. +46
    -37
      generate/main.go
  14. +3
    -55
      generate/node.json
  15. +67
    -0
      generate/nodeiface.json
  16. +3
    -0
      generate/window.json
  17. +0
    -14
      helpers.go
  18. +4
    -1
      init.go
  19. +56
    -2
      node.go
  20. +6
    -0
      nodeiface.go
  21. +14
    -0
      value.go
  22. +10
    -0
      window.go

+ 33
- 18
document.go View File

@ -7,32 +7,21 @@ type document struct {
js.Value
}
func (d *document) JSValue() js.Value { return d.Value }
func (d *document) GetBody() *Element {
val := d.Get("body")
return &Element{Value: val}
}
func (d *document) CreateElement(tagName string) *Element {
val := d.Call("createElement", ToValue(tagName))
val := d.Call("createElement", ToJSValue(tagName))
return &Element{Value: val}
}
func (d *document) GetElementById(id string) *Element {
val := d.Call("getElementById", ToValue(id))
val := d.Call("getElementById", ToJSValue(id))
return &Element{Value: val}
}
func (d *document) QuerySelector(selector string) *Element {
val := d.Call("querySelector", ToValue(selector))
return &Element{Value: val}
}
func (d *document) QuerySelectorAll(selector string) []*Element {
val := d.Call("querySelectorAll", ToValue(selector))
elms := make([]*Element, 0)
for i := 0; i < val.Length(); i += 1 {
elms = append(elms, &Element{Value: val.Index(i)})
}
return elms
}
func (d *document) GetElementsByName(name string) []*Element {
val := d.Call("getElementsByName", ToValue(name))
val := d.Call("getElementsByName", ToJSValue(name))
elms := make([]*Element, 0)
for i := 0; i < val.Length(); i += 1 {
elms = append(elms, &Element{Value: val.Index(i)})
@ -40,13 +29,13 @@ func (d *document) GetElementsByName(name string) []*Element {
return elms
}
func (d *document) Write(markup string) {
d.Call("write", ToValue(markup))
d.Call("write", ToJSValue(markup))
}
func (d *document) AddEventListener(t string, listener js.Callback) {
d.Call("addEventListener", ToValue(t), ToValue(listener))
d.Call("addEventListener", ToJSValue(t), ToJSValue(listener))
}
func (d *document) AppendChild(aChild *Element) *Element {
val := d.Call("appendChild", ToValue(aChild))
val := d.Call("appendChild", ToJSValue(aChild))
return &Element{Value: val}
}
func (d *document) GetBaseURI() string {
@ -92,3 +81,29 @@ func (d *document) GetTextContent() string {
func (d *document) SetTextContent(v string) {
d.Set("textContent", v)
}
func (d *document) QuerySelector(selector string) *Element {
val := d.Call("querySelector", ToJSValue(selector))
return &Element{Value: val}
}
func (d *document) QuerySelectorAll(selector string) []*Element {
val := d.Call("querySelectorAll", ToJSValue(selector))
elms := make([]*Element, 0)
for i := 0; i < val.Length(); i += 1 {
elms = append(elms, &Element{Value: val.Index(i)})
}
return elms
}
func (d *document) GetClassName() string {
val := d.Get("className")
return val.String()
}
func (d *document) SetClassName(v string) {
d.Set("className", v)
}
func (d *document) GetId() string {
val := d.Get("id")
return val.String()
}
func (d *document) SetId(v string) {
d.Set("id", v)
}

+ 29
- 10
element.go View File

@ -7,19 +7,12 @@ type Element struct {
js.Value
}
func (e *Element) GetClassName() string {
val := e.Get("className")
return val.String()
}
func (e *Element) GetId() string {
val := e.Get("id")
return val.String()
}
func (e *Element) JSValue() js.Value { return e.Value }
func (e *Element) AddEventListener(t string, listener js.Callback) {
e.Call("addEventListener", ToValue(t), ToValue(listener))
e.Call("addEventListener", ToJSValue(t), ToJSValue(listener))
}
func (e *Element) AppendChild(aChild *Element) *Element {
val := e.Call("appendChild", ToValue(aChild))
val := e.Call("appendChild", ToJSValue(aChild))
return &Element{Value: val}
}
func (e *Element) GetBaseURI() string {
@ -65,3 +58,29 @@ func (e *Element) GetTextContent() string {
func (e *Element) SetTextContent(v string) {
e.Set("textContent", v)
}
func (e *Element) QuerySelector(selector string) *Element {
val := e.Call("querySelector", ToJSValue(selector))
return &Element{Value: val}
}
func (e *Element) QuerySelectorAll(selector string) []*Element {
val := e.Call("querySelectorAll", ToJSValue(selector))
elms := make([]*Element, 0)
for i := 0; i < val.Length(); i += 1 {
elms = append(elms, &Element{Value: val.Index(i)})
}
return elms
}
func (e *Element) GetClassName() string {
val := e.Get("className")
return val.String()
}
func (e *Element) SetClassName(v string) {
e.Set("className", v)
}
func (e *Element) GetId() string {
val := e.Get("id")
return val.String()
}
func (e *Element) SetId(v string) {
e.Set("id", v)
}

+ 7
- 0
elementiface.go View File

@ -0,0 +1,7 @@
// DO NOT EDIT - generated file
package dom
type ElementIFace interface {
QuerySelector(selector string) *Element
QuerySelectorAll(selector string) []*Element
}

+ 35
- 0
event.go View File

@ -0,0 +1,35 @@
// DO NOT EDIT - generated file
package dom
import "syscall/js"
type Event struct {
js.Value
}
func (e *Event) JSValue() js.Value { return e.Value }
func (e *Event) PreventDefault() {
e.Call("preventDefault")
}
func (e *Event) StopPropagation() {
e.Call("stopPropagation")
}
func (e *Event) StopImmediatePropagation() {
e.Call("stopImmediatePropagation")
}
func (e *Event) GetCurrentTarget() js.Value {
val := e.Get("currentTarget")
return val
}
func (e *Event) GetTarget() js.Value {
val := e.Get("target")
return val
}
func (e *Event) GetType() string {
val := e.Get("type")
return val.String()
}
func (e *Event) GetSrcElement() js.Value {
val := e.Get("srcElement")
return val
}

+ 8
- 0
eventiface.go View File

@ -0,0 +1,8 @@
// DO NOT EDIT - generated file
package dom
type EventIFace interface {
PreventDefault()
StopPropagation()
StopImmediatePropagation()
}

eventtarget.go → eventtargetiface.go View File


+ 5
- 23
generate/document.json View File

@ -1,13 +1,15 @@
{
"Type": "document",
"Implements": [
"EventTarget",
"Node"
"EventTargetIFace",
"NodeIFace",
"ElementIFace"
],
"Properties": [
{
"Name": "body",
"Type": "*Element"
"Type": "*Element",
"ReadOnly": true
}
],
"Functions": [
@ -31,26 +33,6 @@
],
"ReturnType": "*Element"
},
{
"Name": "querySelector",
"Arguments": [
{
"Name": "selector",
"Type": "string"
}
],
"ReturnType": "*Element"
},
{
"Name": "querySelectorAll",
"Arguments": [
{
"Name": "selector",
"Type": "string"
}
],
"ReturnType": "[]*Element"
},
{
"Name": "getElementsByName",
"Arguments": [


+ 3
- 12
generate/element.json View File

@ -1,17 +1,8 @@
{
"Type": "Element",
"Implements": [
"EventTarget",
"Node"
],
"Properties": [
{
"Name": "className",
"Type": "string"
},
{
"Name": "id",
"Type": "string"
}
"EventTargetIFace",
"NodeIFace",
"ElementIFace"
]
}

+ 40
- 0
generate/elementiface.json View File

@ -0,0 +1,40 @@
{
"Type": "ElementIFace",
"Interface": true,
"Implements": [
"EventTarget",
"Node"
],
"Properties": [
{
"Name": "className",
"Type": "string"
},
{
"Name": "id",
"Type": "string"
}
],
"Functions": [
{
"Name": "querySelector",
"Arguments": [
{
"Name": "selector",
"Type": "string"
}
],
"ReturnType": "*Element"
},
{
"Name": "querySelectorAll",
"Arguments": [
{
"Name": "selector",
"Type": "string"
}
],
"ReturnType": "[]*Element"
}
]
}

+ 6
- 0
generate/event.json View File

@ -0,0 +1,6 @@
{
"Type": "Event",
"Implements": [
"EventIFace"
]
}

+ 37
- 0
generate/eventiface.json View File

@ -0,0 +1,37 @@
{
"Type": "EventIFace",
"Interface": true,
"Properties": [
{
"Name": "currentTarget",
"Type": "js.Value",
"ReadOnly": true
},
{
"Name": "target",
"Type": "js.Value",
"ReadOnly": true
},
{
"Name": "type",
"Type": "string",
"ReadOnly": true
},
{
"Name": "srcElement",
"Type": "js.Value",
"ReadOnly": true
}
],
"Functions": [
{
"Name": "preventDefault"
},
{
"Name": "stopPropagation"
},
{
"Name": "stopImmediatePropagation"
}
]
}

generate/eventtarget.json → generate/eventtargetiface.json View File


+ 46
- 37
generate/main.go View File

@ -13,9 +13,9 @@ import (
)
type TypeProperty struct {
Name string
Type string
Setter bool
Name string
Type string
ReadOnly bool
}
func (p TypeProperty) GetterName() string {
@ -68,6 +68,31 @@ func (t TypeStructure) writeArguments(out *bytes.Buffer, args []FunctionArgument
}
}
func (t TypeStructure) writeReturnValue(out *bytes.Buffer, rt string) error {
switch rt {
case "string":
out.WriteString("return val.String()\r\n")
case "js.Value":
out.WriteString("return val")
case "NodeIFace":
out.WriteString("return &Node{ Value: val }\r\n")
case "*Element":
out.WriteString("return &Element{ Value: val }\r\n")
case "[]*Element":
out.WriteString("elms := make([]*Element, 0)\r\n")
out.WriteString("for i := 0; i < val.Length(); i += 1 {\r\n")
out.WriteString("\telms = append(elms, &Element{Value: val.Index(i)})\r\n")
out.WriteString("}\r\n")
out.WriteString("return elms\r\n")
case "":
// skip
default:
return fmt.Errorf("Unknown function return type %s", rt)
}
return nil
}
func (t TypeStructure) writeFunctions(out *bytes.Buffer, funcs []TypeFunction) error {
for _, f := range funcs {
out.WriteString(fmt.Sprintf("func (%s *%s) %s(", t.ShortName(), t.Type, f.GoName()))
@ -85,26 +110,12 @@ func (t TypeStructure) writeFunctions(out *bytes.Buffer, funcs []TypeFunction) e
out.WriteString(fmt.Sprintf("%s.Call(\"%s\"", t.ShortName(), f.Name))
for _, arg := range f.Arguments {
out.WriteString(",")
out.WriteString(fmt.Sprintf("ToValue(%s)", arg.Name))
out.WriteString(fmt.Sprintf("ToJSValue(%s)", arg.Name))
}
out.WriteString(")\r\n")
switch f.ReturnType {
case "string":
out.WriteString("return val.String()\r\n")
case "*Element":
out.WriteString("return &Element{ Value: val }\r\n")
case "[]*Element":
out.WriteString("elms := make([]*Element, 0)\r\n")
out.WriteString("for i := 0; i < val.Length(); i += 1 {\r\n")
out.WriteString("\telms = append(elms, &Element{Value: val.Index(i)})\r\n")
out.WriteString("}\r\n")
out.WriteString("return elms\r\n")
case "":
// skip
default:
return fmt.Errorf("Unknown function return type %s", f.ReturnType)
}
// Return value
t.writeReturnValue(out, f.ReturnType)
out.WriteString("}\r\n")
}
@ -114,26 +125,18 @@ func (t TypeStructure) writeFunctions(out *bytes.Buffer, funcs []TypeFunction) e
func (t TypeStructure) writeProperties(out *bytes.Buffer, props []TypeProperty) error {
for _, p := range props {
out.WriteString(fmt.Sprintf("func (%s *%s) %s() %s {\r\n", t.ShortName(), t.Type, p.GetterName(), p.Type))
out.WriteString(fmt.Sprintf("\tval := %s.Get(\"%s\")\r\n", t.ShortName(), p.Name))
switch p.Type {
case "string":
out.WriteString("return val.String()\r\n")
case "*Element":
out.WriteString("return &Element{ Value: val }\r\n")
case "[]*Element":
out.WriteString("elms := make([]*Element, 0)\r\n")
out.WriteString("for i := 0; i < val.Length(); i += 1 {\r\n")
out.WriteString("\telms = append(elms, &Element{Value: val.Index(i)})\r\n")
out.WriteString("}\r\n")
out.WriteString("return elms\r\n")
case "":
// skip
default:
return fmt.Errorf("Unknown property return type %s", p.Type)
if p.Type == "" {
return fmt.Errorf("Property %q must have a type defined", p.Name)
out.WriteString("\tval := ")
}
out.WriteString(fmt.Sprintf("val := %s.Get(\"%s\")\r\n", t.ShortName(), p.Name))
// Return value
t.writeReturnValue(out, p.Type)
out.WriteString(fmt.Sprintf("}\r\n"))
if p.Setter {
if !p.ReadOnly {
out.WriteString(fmt.Sprintf("func (%s *%s) %s(v %s){\r\n", t.ShortName(), t.Type, p.SetterName(), p.Type))
out.WriteString(fmt.Sprintf("\t%s.Set(\"%s\", v)\r\n", t.ShortName(), p.Name))
out.WriteString("}\r\n")
@ -165,6 +168,12 @@ func (t TypeStructure) generateStruct(out *bytes.Buffer, types map[string]TypeSt
out.WriteString("}\r\n")
// Convert to type
out.WriteString(fmt.Sprintf("func JSTo%s (v js.Value) *%s { return &%s{ Value: v }}", t.Type, t.Type, t.Type))
// JSValue
out.WriteString(fmt.Sprintf("func (%s *%s) JSValue() js.Value { return %s.Value }\r\n", t.ShortName(), t.Type, t.ShortName()))
// Properties
err = t.writeProperties(out, t.Properties)
if err != nil {


+ 3
- 55
generate/node.json View File

@ -1,59 +1,7 @@
{
"Type": "Node",
"Interface": true,
"Properties": [
{
"Name": "baseURI",
"Type": "string"
},
{
"Name": "firstChild",
"Type": "*Element"
},
{
"Name": "lastChild",
"Type": "*Element"
},
{
"Name": "nextSibling",
"Type": "*Element"
},
{
"Name": "previousSibling",
"Type": "*Element"
},
{
"Name": "parentElement",
"Type": "*Element"
},
{
"Name": "rootElement",
"Type": "*Element"
},
{
"Name": "prefix",
"Type": "string"
},
{
"Name": "nodeName",
"Type": "string"
},
{
"Name": "textContent",
"Type": "string",
"Setter": true
}
],
"Functions": [
{
"Name": "appendChild",
"Arguments": [
{
"Name": "aChild",
"Type": "*Element"
}
],
"ReturnType": "*Element"
}
"Implements": [
"EventTargetIFace",
"NodeIFace"
]
}

+ 67
- 0
generate/nodeiface.json View File

@ -0,0 +1,67 @@
{
"Type": "NodeIFace",
"Interface": true,
"Properties": [
{
"Name": "baseURI",
"Type": "string",
"ReadOnly": true
},
{
"Name": "firstChild",
"Type": "*Element",
"ReadOnly": true
},
{
"Name": "lastChild",
"Type": "*Element",
"ReadOnly": true
},
{
"Name": "nextSibling",
"Type": "*Element",
"ReadOnly": true
},
{
"Name": "previousSibling",
"Type": "*Element",
"ReadOnly": true
},
{
"Name": "parentElement",
"Type": "*Element",
"ReadOnly": true
},
{
"Name": "rootElement",
"Type": "*Element",
"ReadOnly": true
},
{
"Name": "prefix",
"Type": "string",
"ReadOnly": true
},
{
"Name": "nodeName",
"Type": "string",
"ReadOnly": true
},
{
"Name": "textContent",
"Type": "string"
}
],
"Functions": [
{
"Name": "appendChild",
"Arguments": [
{
"Name": "aChild",
"Type": "*Element"
}
],
"ReturnType": "*Element"
}
]
}

+ 3
- 0
generate/window.json View File

@ -0,0 +1,3 @@
{
"Type": "window"
}

+ 0
- 14
helpers.go View File

@ -1,14 +0,0 @@
package dom
import "syscall/js"
func ToValue(v interface{}) js.Value {
switch t := v.(type) {
case *Element:
return t.Value
case *document:
return t.Value
default:
return js.ValueOf(v)
}
}

+ 4
- 1
init.go View File

@ -5,8 +5,11 @@ import "syscall/js"
var (
Document *document
Window *window
)
func init() {
Document = &document{Value: js.Global().Get("document")}
g := js.Global()
Document = &document{Value: g.Get("document")}
Window = &window{Value: g.Get("window")}
}

+ 56
- 2
node.go View File

@ -1,6 +1,60 @@
// DO NOT EDIT - generated file
package dom
type Node interface {
AppendChild(aChild *Element) *Element
import "syscall/js"
type Node struct {
js.Value
}
func (n *Node) JSValue() js.Value { return n.Value }
func (n *Node) AddEventListener(t string, listener js.Callback) {
n.Call("addEventListener", ToJSValue(t), ToJSValue(listener))
}
func (n *Node) AppendChild(aChild *Element) *Element {
val := n.Call("appendChild", ToJSValue(aChild))
return &Element{Value: val}
}
func (n *Node) GetBaseURI() string {
val := n.Get("baseURI")
return val.String()
}
func (n *Node) GetFirstChild() *Element {
val := n.Get("firstChild")
return &Element{Value: val}
}
func (n *Node) GetLastChild() *Element {
val := n.Get("lastChild")
return &Element{Value: val}
}
func (n *Node) GetNextSibling() *Element {
val := n.Get("nextSibling")
return &Element{Value: val}
}
func (n *Node) GetPreviousSibling() *Element {
val := n.Get("previousSibling")
return &Element{Value: val}
}
func (n *Node) GetParentElement() *Element {
val := n.Get("parentElement")
return &Element{Value: val}
}
func (n *Node) GetRootElement() *Element {
val := n.Get("rootElement")
return &Element{Value: val}
}
func (n *Node) GetPrefix() string {
val := n.Get("prefix")
return val.String()
}
func (n *Node) GetNodeName() string {
val := n.Get("nodeName")
return val.String()
}
func (n *Node) GetTextContent() string {
val := n.Get("textContent")
return val.String()
}
func (n *Node) SetTextContent(v string) {
n.Set("textContent", v)
}

+ 6
- 0
nodeiface.go View File

@ -0,0 +1,6 @@
// DO NOT EDIT - generated file
package dom
type NodeIFace interface {
AppendChild(aChild *Element) *Element
}

+ 14
- 0
value.go View File

@ -0,0 +1,14 @@
package dom
import "syscall/js"
type JSValue interface {
JSValue() js.Value
}
func ToJSValue(x interface{}) js.Value {
if v, ok := x.(JSValue); ok {
return v.JSValue()
}
return js.ValueOf(x)
}

+ 10
- 0
window.go View File

@ -0,0 +1,10 @@
// DO NOT EDIT - generated file
package dom
import "syscall/js"
type window struct {
js.Value
}
func (w *window) JSValue() js.Value { return w.Value }

Loading…
Cancel
Save