-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ajax.go
126 lines (112 loc) · 3.32 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
package action
import (
"encoding/json"
"github.com/GoAdminGroup/go-admin/context"
"github.com/GoAdminGroup/go-admin/modules/constant"
"github.com/GoAdminGroup/go-admin/modules/language"
"github.com/GoAdminGroup/go-admin/template/types"
"html/template"
)
type AjaxAction struct {
BaseAction
Url string
Method string
Data AjaxData
Alert bool
AlertData AlertData
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(),
Handlers: context.Handlers{handler.Wrap()},
}
}
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) SetData(data map[string]interface{}) *AjaxAction {
ajax.Data = ajax.Data.Add(data)
return ajax
}
func (ajax *AjaxAction) SetUrl(url string) *AjaxAction {
ajax.Url = url
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) {
if (data.code === 0) {
swal(data.msg, '', 'success');
} else {
swal(data.msg, '', 'error');
}
},
error: function (data) {
if (data.responseText !== "") {
swal(data.responseJSON.msg, '', 'error');
} else {
swal('error', '', 'error');
}
},
});`
if ajax.Alert {
b, _ := json.Marshal(ajax.AlertData)
ajaxStatement = "swal(" + string(b) + `,
function () {
` + ajaxStatement + `
});`
}
return template.JS(`$('.` + ajax.BtnId + `').on('click', function (event) {
let data = ` + ajax.Data.JSON() + `;
let id = $(this).attr("data-id");
if (id && id !== "") {
data["id"] = id;
}
` + ajaxStatement + `
});`)
}
func (ajax *AjaxAction) BtnAttribute() template.HTML { return template.HTML(`href="javascript:;"`) }