-
Notifications
You must be signed in to change notification settings - Fork 36
/
render_ionic.go
127 lines (108 loc) · 3.64 KB
/
render_ionic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package flow
import (
"context"
"fmt"
"github.com/beevik/etree"
)
const ionicURL = "http://119.27.161.206:8080/ionic.js"
// NewIonicRenderer 渲染到ionic
func NewIonicRenderer() Renderer {
return &ionicRenderer{}
}
type ionicRenderer struct {
}
func (p *ionicRenderer) Render(ctx context.Context, form *NodeFormResult) ([]byte, error) {
// content := page.CreateElement("ion-content")
doc := etree.NewDocument()
doc.WriteSettings.CanonicalText = true
// doc.WriteSettings.
list := p.createFormTemplate(&doc.Element)
for _, field := range form.Fields {
createFunc, _ := p.getCreateFunctionByFieldType(field)
if createFunc != nil {
createFunc(list, field)
fmt.Println("domfffffffffffffff", list)
}
}
// var buf = make([]byte, 0)
doc.Indent(2)
buf, err := doc.WriteToBytes()
return buf, err
}
func (p *ionicRenderer) createFormTemplate(doc *etree.Element) *etree.Element {
body := doc.CreateElement("body")
viewport := body.CreateElement("meta")
viewport.CreateAttr("name", "viewport")
viewport.CreateAttr("content", "width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no")
ionicScript := body.CreateElement("script")
ionicScript.SetText("")
ionicScript.CreateAttr("src", ionicURL)
app := body.CreateElement("ion-app")
page := app.CreateElement("ion-page")
content := page.CreateElement("ion-content")
// form := content.CreateElement("form")
// form.CreateAttr("action", "/api/form")
// form.CreateAttr("method", "post")
list := content.CreateElement("ion-list")
return list
}
func (p *ionicRenderer) getCreateFunctionByFieldType(field *FormFieldResult) (func(*etree.Element, *FormFieldResult), error) {
switch field.Type {
case "string":
return p.createStringInput, nil
case "long":
return p.createLongInput, nil
case "date":
return p.createDateInput, nil
case "enum":
return p.createSelectInput, nil
case "boolean":
return p.createRatioInput, nil
}
return nil, nil
}
func (p *ionicRenderer) createStringInput(form *etree.Element, field *FormFieldResult) {
item := form.CreateElement("ion-item")
label := item.CreateElement("ion-label")
label.SetText(field.Label)
input := item.CreateElement("ion-input")
input.CreateAttr("id", field.ID)
// input.SetText(field.DefaultValue)
}
func (p *ionicRenderer) createLongInput(form *etree.Element, field *FormFieldResult) {
item := form.CreateElement("ion-item")
label := item.CreateElement("ion-label")
label.SetText(field.Label)
input := item.CreateElement("ion-input")
input.CreateAttr("id", field.ID)
// input.SetText(field.DefaultValue)
}
func (p *ionicRenderer) createDateInput(form *etree.Element, field *FormFieldResult) {
item := form.CreateElement("ion-item")
label := item.CreateElement("ion-label")
label.SetText(field.Label)
input := item.CreateElement("ion-datetime")
input.CreateAttr("id", field.ID)
// input.SetText(field.DefaultValue)
}
func (p *ionicRenderer) createSelectInput(form *etree.Element, field *FormFieldResult) {
item := form.CreateElement("ion-item")
label := item.CreateElement("ion-label")
label.SetText(field.Label)
input := item.CreateElement("ion-select")
input.CreateAttr("id", field.ID)
for _, option := range field.Values {
optionElement := input.CreateElement("ion-select-option")
optionElement.SetText(option.Name)
// optionElement.CreateAttr("id", option)
optionElement.CreateAttr("id", option.ID)
}
}
func (p *ionicRenderer) createRatioInput(form *etree.Element, field *FormFieldResult) {
item := form.CreateElement("ion-item")
label := item.CreateElement("ion-label")
label.SetText(field.Label)
input := item.CreateElement("ion-radio")
input.CreateAttr("id", field.ID)
// input.SetText(field.DefaultValue)
}