-
Notifications
You must be signed in to change notification settings - Fork 6
/
banner_editor.go
287 lines (255 loc) · 7.77 KB
/
banner_editor.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package banner_editor
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/url"
"reflect"
"strings"
mobiledetect "github.com/Shaked/gomobiledetect"
"github.com/jinzhu/gorm"
"github.com/qor/admin"
"github.com/qor/assetfs"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/serializable_meta"
)
var (
registeredElements []*Element
registeredExternalStylePaths []string
viewPaths []string
assetFileSystem assetfs.Interface
)
const (
// Laptop default laptop platform name
Laptop = "Laptop"
// Mobile default mobile platform name
Mobile = "Mobile"
)
func init() {
assetFileSystem = assetfs.AssetFS().NameSpace("banner_editor")
}
// BannerEditorConfig configure display elements and setting model
type BannerEditorConfig struct {
MediaLibrary *admin.Resource
Elements []string
SettingResource *admin.Resource
Platforms []Platform
}
// QorBannerEditorSettingInterface interface to support customize setting model
type QorBannerEditorSettingInterface interface {
GetID() uint
serializable_meta.SerializableMetaInterface
}
// QorBannerEditorSetting default setting model
type QorBannerEditorSetting struct {
gorm.Model
serializable_meta.SerializableMeta
}
// Element represent a button/element in banner_editor toolbar
type Element struct {
Icon string
Name string
Label string
Template string
Resource *admin.Resource
Context func(context *admin.Context, setting interface{}) interface{}
}
// Size contains width ad height
type Size struct {
Width int
Height int
}
// Platform used to defined how many platform a banner need to support
type Platform struct {
Name string
SafeArea Size
}
type configurePlatform struct {
Name string
Value string
}
func init() {
admin.RegisterViewPath("github.com/qor/banner_editor/views")
}
// RegisterElement register a element
func RegisterElement(e *Element) {
registeredElements = append(registeredElements, e)
}
// RegisterExternalStylePath register a asset path
func RegisterExternalStylePath(path string) {
registeredExternalStylePaths = append(registeredExternalStylePaths, path)
}
// ConfigureQorMeta configure route and funcmap for banner_editor meta
func (config *BannerEditorConfig) ConfigureQorMeta(metaor resource.Metaor) {
if meta, ok := metaor.(*admin.Meta); ok {
meta.Type = "banner_editor"
Admin := meta.GetBaseResource().(*admin.Resource).GetAdmin()
if config.SettingResource == nil {
config.SettingResource = Admin.NewResource(&QorBannerEditorSetting{})
}
if config.MediaLibrary == nil {
panic("BannerEditor: MediaLibrary can't be blank.")
} else {
if getMediaLibraryResourceURLMethod(config.MediaLibrary.NewStruct()).IsNil() {
panic("BannerEditor: MediaLibrary's struct doesn't have any field implement URL method, please refer media_library.MediaLibrary{}.")
}
config.MediaLibrary.Meta(&admin.Meta{
Name: "BannerEditorUrl",
Type: "hidden",
Valuer: func(v interface{}, c *qor.Context) interface{} {
values := getMediaLibraryResourceURLMethod(v).Call([]reflect.Value{})
if len(values) > 0 {
return values[0]
}
return ""
},
})
config.MediaLibrary.IndexAttrs(config.MediaLibrary.IndexAttrs(), "BannerEditorUrl")
}
if len(config.Platforms) == 0 {
config.Platforms = []Platform{
{Name: Laptop},
{Name: Mobile},
}
}
router := Admin.GetRouter()
res := config.SettingResource
router.Get(fmt.Sprintf("%v/new", res.ToParam()), New, &admin.RouteConfig{Resource: res})
router.Post(fmt.Sprintf("%v", res.ToParam()), Create, &admin.RouteConfig{Resource: res})
router.Put(fmt.Sprintf("%v/%v", res.ToParam(), res.ParamIDName()), Update, &admin.RouteConfig{Resource: res})
Admin.RegisterResourceRouters(res, "read", "update")
Admin.RegisterFuncMap("formatted_banner_edit_value", formattedValue)
Admin.RegisterFuncMap("banner_editor_configure", func(config *BannerEditorConfig) string {
type element struct {
Label string
CreateURL string
Icon string
}
type platform struct {
Name string
Width int
Height int
}
var (
selectedElements = registeredElements
elements = []element{}
newElementURL = router.Prefix + fmt.Sprintf("/%v/new", res.ToParam())
)
if len(config.Elements) != 0 {
selectedElements = []*Element{}
for _, name := range config.Elements {
if e := GetElement(name); e != nil {
selectedElements = append(selectedElements, e)
}
}
}
for _, e := range selectedElements {
element := element{Icon: e.Icon, Label: e.Label, CreateURL: fmt.Sprintf("%v?kind=%v", newElementURL, template.URLQueryEscaper(e.Name))}
if element.Label == "" {
element.Label = e.Name
}
elements = append(elements, element)
}
platforms := []platform{}
for _, p := range config.Platforms {
platforms = append(platforms, platform{Name: p.Name, Width: p.SafeArea.Width, Height: p.SafeArea.Height})
}
results, err := json.Marshal(struct {
Elements []element
ExternalStylePath []string
EditURL string
Platforms []platform
}{
Elements: elements,
ExternalStylePath: registeredExternalStylePaths,
EditURL: fmt.Sprintf("%v/%v/:id/edit", router.Prefix, res.ToParam()),
Platforms: platforms,
})
if err != nil {
return err.Error()
}
return string(results)
})
}
}
// GetElement returnn element struct by name
func GetElement(name string) *Element {
for _, e := range registeredElements {
if e.Name == name {
return e
}
}
return nil
}
// GetID return setting ID
func (setting QorBannerEditorSetting) GetID() uint {
return setting.ID
}
// GetSerializableArgumentResource return setting's resource
func (setting QorBannerEditorSetting) GetSerializableArgumentResource() *admin.Resource {
element := GetElement(setting.Kind)
if element != nil {
return element.Resource
}
return nil
}
// GetContent return HTML string by detector, detector could be a string (Platform, Mobile or other), http request and nil
func GetContent(value string, detector interface{}) string {
if platform, ok := detector.(string); ok {
return getContentByPlatform(value, platform)
} else if req, ok := detector.(*http.Request); ok {
detect := mobiledetect.NewMobileDetect(req, nil)
if detect.IsMobile() && !detect.IsTablet() {
return getContentByPlatform(value, Mobile)
}
return getContentByPlatform(value, Laptop)
}
return getContentByPlatform(value, Laptop)
}
func getContentByPlatform(value string, platform string) string {
configurePlatforms := []configurePlatform{}
if err := json.Unmarshal([]byte(value), &configurePlatforms); err == nil {
if len(configurePlatforms) == 0 {
return ""
}
for _, p := range configurePlatforms {
if p.Name == platform && strings.TrimSpace(p.Value) != "" {
return unescapeValue(p.Value)
}
}
return unescapeValue(configurePlatforms[0].Value)
}
return unescapeValue(value)
}
func unescapeValue(value string) string {
if val, err := url.QueryUnescape(value); err == nil {
return val
}
return value
}
func formattedValue(value string) string {
if value == "" {
return "[]"
}
configurePlatforms := []configurePlatform{}
if err := json.Unmarshal([]byte(value), &configurePlatforms); err == nil {
return value
}
jsonValue, err := json.Marshal(&[]configurePlatform{{Name: Laptop, Value: value}})
if err != nil {
return fmt.Sprintf("BannerEditor: format value to json failure, got %v", err.Error())
}
return string(jsonValue)
}
func getMediaLibraryResourceURLMethod(i interface{}) reflect.Value {
value := reflect.Indirect(reflect.ValueOf(i))
for i := 0; i < value.NumField(); i++ {
field := value.Field(i)
if urlMethod := field.MethodByName("URL"); urlMethod.IsValid() {
return urlMethod
}
}
return reflect.Value{}
}