/
location.go
105 lines (87 loc) · 2.91 KB
/
location.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
package location
import (
"fmt"
"strings"
"github.com/qor/admin"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/qor/utils"
)
func init() {
admin.RegisterViewPath("github.com/qor/location/views")
}
var (
_ admin.MetaConfigInterface = &LocationConfig{}
// GoogleAPIKey Key of Google Map API
GoogleAPIKey string
// BaiduAPIKey Key of Baidu Map API
BaiduAPIKey string
)
// LocationConfig Location Meta's Config
type LocationConfig struct {
Backend string
BaiduAPIKey string
GoogleAPIKey string
}
// ConfigureQorMeta configure Qor Meta to implement MetaConfig interface
func (locationConfig *LocationConfig) ConfigureQorMeta(meta resource.Metaor) {
if GoogleAPIKey != "" && locationConfig.GoogleAPIKey == "" {
locationConfig.GoogleAPIKey = GoogleAPIKey
} else if BaiduAPIKey != "" && locationConfig.BaiduAPIKey == "" {
locationConfig.BaiduAPIKey = BaiduAPIKey
}
if locationConfig.Backend == "" {
if locationConfig.GoogleAPIKey != "" {
locationConfig.Backend = "google"
} else if locationConfig.BaiduAPIKey != "" {
locationConfig.Backend = "baidu"
}
}
}
// Location is a struct, you could embedded it into your model to get the Location feature for your model
type Location struct {
Address string
City string
Region string
Country string
Zip string
Latitude float64
Longitude float64
}
type locationInterface interface {
GetLocation() *Location
}
// GetLocation get location from your model
func (location *Location) GetLocation() *Location {
return location
}
// ConfigureQorResource configure qor locale for Qor Admin
func (*Location) ConfigureQorResource(res resource.Resourcer) {
if res, ok := res.(*admin.Resource); ok {
Admin := res.GetAdmin()
res.UseTheme("location")
Admin.RegisterFuncMap("replace_suffix", func(str, suffix, newSuffix string) string {
return fmt.Sprint(strings.TrimSuffix(str, suffix), newSuffix)
})
scope := Admin.DB.NewScope(res.Value)
if field, ok := scope.GetModelStruct().ModelType.FieldByName("Location"); ok {
labelName := field.Name
if customName, ok := utils.ParseTagOption(field.Tag.Get("location"))["NAME"]; ok {
labelName = customName
}
res.Meta(&admin.Meta{Name: field.Name, Label: labelName, Type: "location", Config: &LocationConfig{GoogleAPIKey: GoogleAPIKey, BaiduAPIKey: BaiduAPIKey}, Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
return resource.(locationInterface).GetLocation()
}})
res.IndexAttrs(res.IndexAttrs(), "-"+field.Name, "-Latitude", "-Longitude")
res.OverrideNewAttrs(func() {
res.NewAttrs(res.NewAttrs(), "-Address", "-City", "-Region", "-Country", "-Zip", "-Latitude", "-Longitude")
})
res.OverrideEditAttrs(func() {
res.EditAttrs(res.EditAttrs(), "-Address", "-City", "-Region", "-Country", "-Zip", "-Latitude", "-Longitude")
})
res.OverrideShowAttrs(func() {
res.ShowAttrs(res.ShowAttrs(), "-"+field.Name)
})
}
}
}