generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusecase.go
116 lines (91 loc) · 2.99 KB
/
usecase.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
package jsonform
import (
"fmt"
"html/template"
"io"
)
// Form describes form parameters.
type Form struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
SchemaName string `json:"schemaName,omitempty"`
ValueURL string `json:"valueUrl,omitempty"`
SubmitURL string `json:"submitUrl,omitempty"`
SubmitMethod string `json:"submitMethod,omitempty"`
SuccessStatus int `json:"successStatus,omitempty"`
// OnSuccess is a javascript callback that receives XMLHttpRequest value in case of successful response.
OnSuccess template.JS `json:"-"`
// OnFail is a javascript callback that receives XMLHttpRequest value in case of a failure response.
OnFail template.JS `json:"-"`
// OnError is a javascript callback that receives string HTML value in case of an error while processing the form.
OnError template.JS `json:"-"`
// OnBeforeSubmit is a javascript callback that receives form data after Submit button is clicked and before request is sent.
OnBeforeSubmit template.JS `json:"-"`
// OnRequestFinished is a javascript callback that receives XMLHttpRequest after request is finished.
OnRequestFinished template.JS `json:"-"`
Schema *FormSchema `json:"schema,omitempty"`
Value interface{} `json:"value,omitempty"`
// SubmitText is an optional description of submit button.
SubmitText string `json:"-"`
}
// Page allows page customizations.
type Page struct {
// AppendHTMLHead is injected into the <head> of an HTML document.
AppendHTMLHead template.HTML
// PrependHTML is added before the forms.
PrependHTML template.HTML
// AppendHTML is added after the forms.
AppendHTML template.HTML
// Title is set to HTML document title.
Title string
}
var formTemplate = loadTemplate("form_tmpl.html")
// Render renders forms as web page.
func (r *Repository) Render(w io.Writer, p Page, forms ...Form) error {
type pageData struct {
Page
Params []Form
BaseURL string
}
d := pageData{
Page: p,
BaseURL: r.baseURL,
}
for _, form := range forms {
if d.Title == "" {
d.Title = form.Title
}
if form.Schema == nil && form.Value != nil {
s, err := r.formSchema(form.Value)
if err != nil {
return err
}
form.Schema = &FormSchema{}
*form.Schema = *s
submit := FormItem{FormType: "submit", FormTitle: "Submit"}
if form.OnBeforeSubmit == "" && form.OnRequestFinished == "" {
form.OnBeforeSubmit = "startSpinner"
form.OnRequestFinished = "stopSpinner"
}
if form.SubmitText != "" {
submit.FormTitle = form.SubmitText
}
form.Schema.Form = append(form.Schema.Form, submit)
}
d.Params = append(d.Params, form)
}
return formTemplate.Execute(w, d)
}
func (r *Repository) formSchema(value interface{}) (*FormSchema, error) {
formSchema := r.Schema(value)
if formSchema == nil {
if r.Strict {
return nil, fmt.Errorf("missing form schema for %T", value)
}
if err := r.Add(value); err != nil {
return nil, err
}
formSchema = r.Schema(value)
}
return formSchema, nil
}