forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.go
189 lines (168 loc) · 4.85 KB
/
ajax.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package action
import (
"encoding/json"
"html/template"
"strings"
"github.com/romutchio/go-admin/context"
"github.com/romutchio/go-admin/modules/constant"
"github.com/romutchio/go-admin/modules/language"
"github.com/romutchio/go-admin/template/types"
)
type AjaxAction struct {
BaseAction
Url string
Method string
Data AjaxData
Alert bool
AlertData AlertData
SuccessJS template.JS
ErrorJS template.JS
ParameterJS template.JS
Event Event
Handlers []context.Handler
}
type AlertData struct {
Title string `json:"title"`
Type string `json:"type"`
ShowCancelButton bool `json:"showCancelButton"`
ConfirmButtonColor string `json:"confirmButtonColor"`
ConfirmButtonText string `json:"confirmButtonText"`
CloseOnConfirm bool `json:"closeOnConfirm"`
CancelButtonText string `json:"cancelButtonText"`
}
func Ajax(id string, handler types.Handler) *AjaxAction {
if id == "" {
panic("wrong ajax action parameter, empty id")
}
return &AjaxAction{
Url: URL(id),
Method: "post",
Data: NewAjaxData(),
SuccessJS: `if (data.code === 0) {
swal(data.msg, '', 'success');
} else {
swal(data.msg, '', 'error');
}`,
ErrorJS: `if (data.responseText !== "") {
swal(data.responseJSON.msg, '', 'error');
} else {
swal('error', '', 'error');
}`,
Handlers: context.Handlers{handler.Wrap()},
Event: EventClick,
}
}
func (ajax *AjaxAction) WithAlert(data ...AlertData) *AjaxAction {
ajax.Alert = true
if len(data) > 0 {
ajax.AlertData = data[0]
} else {
ajax.AlertData = AlertData{
Title: "",
Type: "warning",
ShowCancelButton: true,
ConfirmButtonColor: "#DD6B55",
ConfirmButtonText: language.Get("yes"),
CloseOnConfirm: false,
CancelButtonText: language.Get("cancel"),
}
}
return ajax
}
func (ajax *AjaxAction) AddData(data map[string]interface{}) *AjaxAction {
ajax.Data = ajax.Data.Add(data)
return ajax
}
func (ajax *AjaxAction) SetData(data map[string]interface{}) *AjaxAction {
ajax.Data = data
return ajax
}
func (ajax *AjaxAction) SetUrl(url string) *AjaxAction {
ajax.Url = url
return ajax
}
func (ajax *AjaxAction) SetSuccessJS(successJS template.JS) *AjaxAction {
ajax.SuccessJS = successJS
return ajax
}
func (ajax *AjaxAction) ChangeHTMLWhenSuccess(identify string, text ...string) *AjaxAction {
data := template.JS("data.data")
if len(text) > 0 {
if len(text[0]) > 5 && text[0][:5] == "data." {
data = template.JS(text[0])
} else {
data = `"` + template.JS(text[0]) + `"`
}
}
selector := template.JS(identify)
if !strings.Contains(identify, "$") {
selector = `$("` + template.JS(identify) + `")`
}
ajax.SuccessJS = `
if (data.code === 0) {
if (` + selector + `.is("input")) {
` + selector + `.val(` + data + `);
} else if (` + selector + `.is("select")) {
` + selector + `.val(` + data + `);
} else {
` + selector + `.html(` + data + `);
}
} else {
swal(data.msg, '', 'error');
}`
return ajax
}
func (ajax *AjaxAction) SetEvent(event Event) *AjaxAction {
ajax.Event = event
return ajax
}
func (ajax *AjaxAction) SetErrorJS(errorJS template.JS) *AjaxAction {
ajax.ErrorJS = errorJS
return ajax
}
func (ajax *AjaxAction) SetParameterJS(parameterJS template.JS) *AjaxAction {
ajax.ParameterJS += parameterJS
return ajax
}
func (ajax *AjaxAction) SetMethod(method string) *AjaxAction {
ajax.Method = method
return ajax
}
func (ajax *AjaxAction) GetCallbacks() context.Node {
return context.Node{
Path: ajax.Url,
Method: ajax.Method,
Handlers: ajax.Handlers,
Value: map[string]interface{}{constant.ContextNodeNeedAuth: 1},
}
}
func (ajax *AjaxAction) Js() template.JS {
ajaxStatement := `$.ajax({
method: '` + ajax.Method + `',
url: "` + ajax.Url + `",
data: data,
success: function (data) {
` + string(ajax.SuccessJS) + `
},
error: function (data) {
` + string(ajax.ErrorJS) + `
},
});`
if ajax.Alert {
b, _ := json.Marshal(ajax.AlertData)
ajaxStatement = "swal(" + string(b) + `,
function () {
` + ajaxStatement + `
});`
}
return template.JS(`$('`+ajax.BtnId+`').on('`+string(ajax.Event)+`', function (event) {
let data = `+ajax.Data.JSON()+`;
`) + ajax.ParameterJS + template.JS(`
let id = $(this).attr("data-id");
if (id && id !== "") {
data["id"] = id;
}
`+ajaxStatement+`
});`)
}
func (ajax *AjaxAction) BtnAttribute() template.HTML { return template.HTML(`href="javascript:;"`) }