-
Notifications
You must be signed in to change notification settings - Fork 11
/
func_map.go
56 lines (48 loc) · 1.34 KB
/
func_map.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
package widget
import (
"sort"
"strings"
"github.com/qor/admin"
"github.com/qor/roles"
)
type GroupedWidgets struct {
Group string
Widgets []*Widget
}
var funcMap = map[string]interface{}{
"widget_available_scopes": func() []*Scope {
if len(registeredScopes) > 0 {
return append([]*Scope{{Name: "Default Visitor", Param: "default"}}, registeredScopes...)
}
return []*Scope{}
},
"widget_grouped_widgets": func(context *admin.Context) []*GroupedWidgets {
groupedWidgetsSlice := []*GroupedWidgets{}
OUTER:
for _, w := range registeredWidgets {
var roleNames = []interface{}{}
for _, role := range context.Roles {
roleNames = append(roleNames, role)
}
if w.Permission == nil || w.Permission.HasPermission(roles.Create, roleNames...) {
for _, groupedWidgets := range groupedWidgetsSlice {
if groupedWidgets.Group == w.Group {
groupedWidgets.Widgets = append(groupedWidgets.Widgets, w)
continue OUTER
}
}
groupedWidgetsSlice = append(groupedWidgetsSlice, &GroupedWidgets{
Group: w.Group,
Widgets: []*Widget{w},
})
}
}
sort.SliceStable(groupedWidgetsSlice, func(i, j int) bool {
if groupedWidgetsSlice[i].Group == "" {
return false
}
return strings.Compare(groupedWidgetsSlice[i].Group, groupedWidgetsSlice[j].Group) < 0
})
return groupedWidgetsSlice
},
}