/
funcmapmaker.go
109 lines (89 loc) · 3.1 KB
/
funcmapmaker.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
package funcmapmaker
import (
"html/template"
"net/http"
"github.com/dfang/qor-demo/app/admin"
"github.com/dfang/qor-demo/config"
"github.com/dfang/qor-demo/config/i18n"
"github.com/dfang/qor-demo/models/products"
"github.com/dfang/qor-demo/models/seo"
"github.com/dfang/qor-demo/models/users"
"github.com/dfang/qor-demo/utils"
"github.com/qor/action_bar"
"github.com/qor/i18n/inline_edit"
"github.com/qor/qor"
"github.com/qor/render"
"github.com/qor/session"
"github.com/qor/session/manager"
"github.com/qor/widget"
)
// GetEditMode get edit mode
func GetEditMode(w http.ResponseWriter, req *http.Request) bool {
return admin.ActionBar.EditMode(w, req)
}
// AddFuncMapMaker add FuncMapMaker to view
func AddFuncMapMaker(view *render.Render) *render.Render {
oldFuncMapMaker := view.FuncMapMaker
view.FuncMapMaker = func(render *render.Render, req *http.Request, w http.ResponseWriter) template.FuncMap {
funcMap := template.FuncMap{}
if oldFuncMapMaker != nil {
funcMap = oldFuncMapMaker(render, req, w)
}
// Add `t` method
for key, fc := range inline_edit.FuncMap(i18n.I18n, utils.GetCurrentLocale(req), GetEditMode(w, req)) {
funcMap[key] = fc
}
for key, value := range admin.ActionBar.FuncMap(w, req) {
funcMap[key] = value
}
widgetContext := admin.Widgets.NewContext(&widget.Context{
DB: utils.GetDB(req),
Options: map[string]interface{}{"Request": req},
InlineEdit: GetEditMode(w, req),
})
for key, fc := range widgetContext.FuncMap() {
funcMap[key] = fc
}
funcMap["raw"] = func(str string) template.HTML {
return template.HTML(utils.HTMLSanitizer.Sanitize(str))
}
funcMap["flashes"] = func() []session.Message {
return manager.SessionManager.Flashes(w, req)
}
// Add `action_bar` method
funcMap["render_action_bar"] = func() template.HTML {
return admin.ActionBar.Actions(action_bar.Action{Name: "Edit SEO", Link: seo.SEOCollection.SEOSettingURL("/help")}).Render(w, req)
}
funcMap["render_seo_tag"] = func() template.HTML {
return seo.SEOCollection.Render(&qor.Context{DB: utils.GetDB(req)}, "Default Page")
}
funcMap["get_categories"] = func() (categories []products.Category) {
utils.GetDB(req).Find(&categories)
return
}
funcMap["current_locale"] = func() string {
return utils.GetCurrentLocale(req)
}
funcMap["current_user"] = func() *users.User {
return utils.GetCurrentUser(req)
}
funcMap["related_products"] = func(cv products.ColorVariation) []products.Product {
var products []products.Product
utils.GetDB(req).Preload("ColorVariations").Limit(4).Find(&products, "id <> ?", cv.ProductID)
return products
}
funcMap["other_also_bought"] = func(cv products.ColorVariation) []products.Product {
var products []products.Product
utils.GetDB(req).Preload("ColorVariations").Order("id ASC").Limit(8).Find(&products, "id <> ?", cv.ProductID)
return products
}
funcMap["amazon_payment_gateway"] = func() interface{} {
return config.Config.AmazonPay
}
funcMap["format_price"] = func(price interface{}) string {
return utils.FormatPrice(price)
}
return funcMap
}
return view
}