Big refactoring of PARCOURSMOB - Initial commit
This commit is contained in:
184
renderer/renderer.go
Normal file
184
renderer/renderer.go
Normal file
@@ -0,0 +1,184 @@
|
||||
package renderer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/icons"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Renderer struct {
|
||||
TemplatesDir string
|
||||
GlobalConfig *viper.Viper
|
||||
ThemeConfig *viper.Viper
|
||||
}
|
||||
|
||||
func NewRenderer(global *viper.Viper, templates_dir string) *Renderer {
|
||||
theme := viper.New()
|
||||
theme.SetConfigName("config")
|
||||
theme.AddConfigPath(templates_dir)
|
||||
err := theme.ReadInConfig()
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("fatal error config file: %w", err))
|
||||
}
|
||||
return &Renderer{
|
||||
TemplatesDir: templates_dir,
|
||||
GlobalConfig: global,
|
||||
ThemeConfig: theme,
|
||||
}
|
||||
}
|
||||
|
||||
func (renderer *Renderer) Render(name string, w http.ResponseWriter, r *http.Request, files []string, state RenderState) {
|
||||
genericFiles := renderer.ThemeConfig.GetStringSlice("views.generic.files")
|
||||
|
||||
prefixed_files := []string{}
|
||||
for _, f := range genericFiles {
|
||||
prefixed_files = append(prefixed_files, renderer.templateFile(f))
|
||||
}
|
||||
for _, f := range files {
|
||||
prefixed_files = append(prefixed_files, renderer.templateFile(f))
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
t := template.New(name).Funcs(
|
||||
template.FuncMap{
|
||||
"timeFrom": TimeFrom,
|
||||
"genderISO5218": GenderISO5218,
|
||||
"dict": Dict,
|
||||
},
|
||||
)
|
||||
t = template.Must(t.ParseFiles(prefixed_files...))
|
||||
err := t.ExecuteTemplate(w, "main", state)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (renderer *Renderer) RenderNoLayout(name string, w http.ResponseWriter, r *http.Request, files []string, state RenderState) {
|
||||
prefixed_files := []string{}
|
||||
for _, f := range files {
|
||||
prefixed_files = append(prefixed_files, renderer.templateFile(f))
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
t := template.New(name).Funcs(
|
||||
template.FuncMap{
|
||||
"timeFrom": TimeFrom,
|
||||
"genderISO5218": GenderISO5218,
|
||||
"dict": Dict,
|
||||
},
|
||||
)
|
||||
|
||||
t = template.Must(t.ParseFiles(prefixed_files...))
|
||||
err := t.ExecuteTemplate(w, "main", state)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Renderer) templateFile(file string) string {
|
||||
return r.TemplatesDir + file
|
||||
}
|
||||
|
||||
type RenderState struct {
|
||||
icons.IconSet
|
||||
LayoutState
|
||||
Group any
|
||||
Roles any
|
||||
ViewState any // This is a state specific to a given view
|
||||
}
|
||||
|
||||
func NewState(r *http.Request, themeConfig *viper.Viper, menuState string) RenderState {
|
||||
iconset := themeConfig.GetStringMapString("icons.svg")
|
||||
// Get State elements from Request
|
||||
|
||||
g := r.Context().Value(identification.GroupKey)
|
||||
if g == nil {
|
||||
return RenderState{
|
||||
IconSet: icons.NewIconSet(iconset),
|
||||
LayoutState: LayoutState{},
|
||||
}
|
||||
}
|
||||
|
||||
var roles map[string]any
|
||||
|
||||
ro, ok := r.Context().Value(identification.RolesKey).(map[string]any)
|
||||
if ok {
|
||||
roles = ro
|
||||
}
|
||||
|
||||
group := g.(storage.Group)
|
||||
|
||||
modules := group.Data["modules"].(map[string]any)
|
||||
|
||||
ls := LayoutState{
|
||||
AdministrationState: AdministrationState{
|
||||
Display: modules["administration"].(bool),
|
||||
Active: menuState == administrationMenu,
|
||||
},
|
||||
MenuItems: []MenuItem{
|
||||
{
|
||||
Title: "Tableau de bord",
|
||||
Link: "/app/",
|
||||
Active: menuState == "dashboard",
|
||||
Icon: "hero:outline/home",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if modules["beneficiaries"].(bool) {
|
||||
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
||||
Title: "Bénéficiaires",
|
||||
Link: "/app/beneficiaries/",
|
||||
Active: menuState == "beneficiaries",
|
||||
Icon: "hero:outline/user-group",
|
||||
})
|
||||
}
|
||||
|
||||
if modules["journeys"].(bool) {
|
||||
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
||||
Title: "Déplacements",
|
||||
Link: "/app/journeys/",
|
||||
Active: menuState == "journeys",
|
||||
Icon: "hero:outline/user-group",
|
||||
})
|
||||
}
|
||||
|
||||
if modules["vehicles"].(bool) {
|
||||
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
||||
Title: "Véhicules",
|
||||
Link: "/app/vehicles/",
|
||||
Active: menuState == "vehicles",
|
||||
Icon: "hero:outline/user-group",
|
||||
})
|
||||
}
|
||||
|
||||
if modules["vehicles_management"].(bool) {
|
||||
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
||||
Title: "Gestion des véhicules",
|
||||
Link: "/app/vehicles-management/",
|
||||
Active: menuState == "vehicles_management",
|
||||
Icon: "hero:outline/user-group",
|
||||
})
|
||||
}
|
||||
|
||||
if modules["events"].(bool) {
|
||||
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
||||
Title: "Dispositifs",
|
||||
Link: "/app/events/",
|
||||
Active: menuState == "events",
|
||||
Icon: "hero:outline/user-group",
|
||||
})
|
||||
}
|
||||
|
||||
return RenderState{
|
||||
IconSet: icons.NewIconSet(iconset),
|
||||
Group: group,
|
||||
Roles: roles,
|
||||
LayoutState: ls,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user