-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcontroller.go
111 lines (94 loc) · 3.16 KB
/
controller.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
package sorting
import (
"fmt"
"html/template"
"net/http"
"path"
"strconv"
"github.com/qor/admin"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/roles"
)
func updatePosition(context *admin.Context) {
if result, err := context.FindOne(); err == nil {
if position, ok := result.(sortingInterface); ok {
if pos, err := strconv.Atoi(context.Request.Form.Get("to")); err == nil {
var count int
if _, ok := result.(sortingDescInterface); ok {
var result = context.Resource.NewStruct()
context.GetDB().Set("l10n:mode", "locale").Order("position DESC", true).First(result)
count = result.(sortingInterface).GetPosition()
pos = count - pos + 1
}
if MoveTo(context.GetDB(), position, pos) == nil {
var pos = position.GetPosition()
if _, ok := result.(sortingDescInterface); ok {
pos = count - pos + 1
}
context.Writer.Write([]byte(fmt.Sprintf("%d", pos)))
return
}
}
}
}
context.Writer.WriteHeader(admin.HTTPUnprocessableEntity)
context.Writer.Write([]byte("Error"))
}
// ConfigureQorResource configure sorting for qor admin
func (s *Sorting) ConfigureQorResourceBeforeInitialize(res resource.Resourcer) {
if res, ok := res.(*admin.Resource); ok {
res.UseTheme("sorting")
if res.Permission == nil {
res.Permission = roles.NewPermission()
}
role := res.Permission.Role
if _, ok := role.Get("sorting_mode"); !ok {
role.Register("sorting_mode", func(req *http.Request, currentUser interface{}) bool {
return req.URL.Query().Get("sorting") != ""
})
}
res.Meta(&admin.Meta{
Name: "Position",
Valuer: func(value interface{}, ctx *qor.Context) interface{} {
db := ctx.GetDB()
var count int
var pos = value.(sortingInterface).GetPosition()
if _, ok := modelValue(value).(sortingDescInterface); ok {
if total, ok := db.Get("sorting_total_count"); ok {
count = total.(int)
} else {
var result = res.NewStruct()
db.New().Order("position DESC", true).First(result)
count = result.(sortingInterface).GetPosition()
db.InstantSet("sorting_total_count", count)
}
pos = count - pos + 1
}
primaryKey := ctx.GetDB().NewScope(value).PrimaryKeyValue()
url := path.Join(ctx.Request.URL.Path, fmt.Sprintf("%v", primaryKey), "sorting/update_position")
return template.HTML(fmt.Sprintf("<input type=\"number\" disabled class=\"qor-sorting__position\" value=\"%v\" data-sorting-url=\"%v\" data-position=\"%v\">", pos, url, pos))
},
Permission: roles.Allow(roles.Read, "sorting_mode"),
})
}
}
func (s *Sorting) ConfigureQorResource(res resource.Resourcer) {
if res, ok := res.(*admin.Resource); ok {
Admin := res.GetAdmin()
res.OverrideIndexAttrs(func() {
res.IndexAttrs(res.IndexAttrs(), "Position")
})
res.OverrideNewAttrs(func() {
res.NewAttrs(res.NewAttrs(), "-Position")
})
res.OverrideEditAttrs(func() {
res.EditAttrs(res.EditAttrs(), "-Position")
})
res.OverrideShowAttrs(func() {
res.ShowAttrs(res.ShowAttrs(), "-Position")
})
router := Admin.GetRouter()
router.Post(fmt.Sprintf("/%v/%v/sorting/update_position", res.ToParam(), res.ParamIDName()), updatePosition)
}
}