2022-08-11 15:26:55 +00:00
|
|
|
package renderer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/utils/icons"
|
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
2022-10-17 03:02:19 +00:00
|
|
|
"git.coopgo.io/coopgo-platform/emailing"
|
2022-08-11 15:26:55 +00:00
|
|
|
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Renderer struct {
|
|
|
|
TemplatesDir string
|
|
|
|
GlobalConfig *viper.Viper
|
|
|
|
ThemeConfig *viper.Viper
|
2022-10-17 03:02:19 +00:00
|
|
|
Mailer *emailing.Mailer
|
2022-08-11 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-08-12 12:53:54 +00:00
|
|
|
"json": JSON,
|
2022-10-17 03:02:19 +00:00
|
|
|
"walkingLength": WalkingLength,
|
|
|
|
"divideFloat64": Divide[float64],
|
|
|
|
"divideInt": Divide[int],
|
2022-08-11 15:26:55 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
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,
|
2022-08-12 12:53:54 +00:00
|
|
|
"json": JSON,
|
2022-10-17 03:02:19 +00:00
|
|
|
"divideFloat64": Divide[float64],
|
|
|
|
"divideInt": Divide[int],
|
2022-08-11 15:26:55 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2022-10-17 03:02:19 +00:00
|
|
|
Display: modules["administration"] != nil && modules["administration"].(bool),
|
2022-08-11 15:26:55 +00:00
|
|
|
Active: menuState == administrationMenu,
|
|
|
|
},
|
2022-09-05 05:25:05 +00:00
|
|
|
|
|
|
|
//TODO from configuration for icons at least
|
2022-08-11 15:26:55 +00:00
|
|
|
MenuItems: []MenuItem{
|
|
|
|
{
|
|
|
|
Title: "Tableau de bord",
|
|
|
|
Link: "/app/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == dashboardMenu,
|
2022-08-11 15:26:55 +00:00
|
|
|
Icon: "hero:outline/home",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["beneficiaries"] != nil && modules["beneficiaries"].(bool) {
|
2022-08-11 15:26:55 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Bénéficiaires",
|
|
|
|
Link: "/app/beneficiaries/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == beneficiariesMenu,
|
2022-08-11 15:26:55 +00:00
|
|
|
Icon: "hero:outline/user-group",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["journeys"] != nil && modules["journeys"].(bool) {
|
2022-08-11 15:26:55 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Déplacements",
|
|
|
|
Link: "/app/journeys/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == journeysMenu,
|
2022-08-12 12:53:54 +00:00
|
|
|
Icon: "hero:outline/map",
|
2022-08-11 15:26:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["vehicles"] != nil && modules["vehicles"].(bool) {
|
2022-08-11 15:26:55 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
2022-08-12 12:53:54 +00:00
|
|
|
Title: "Véhicules partagés",
|
2022-08-11 15:26:55 +00:00
|
|
|
Link: "/app/vehicles/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == vehiclesMenu,
|
2022-09-05 05:25:05 +00:00
|
|
|
Icon: "tabler-icons:car",
|
2022-08-11 15:26:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["vehicles_management"] != nil && modules["vehicles_management"].(bool) {
|
2022-08-11 15:26:55 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Gestion des véhicules",
|
|
|
|
Link: "/app/vehicles-management/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == vehiclesmanagementMenu,
|
2022-08-12 12:53:54 +00:00
|
|
|
Icon: "hero:outline/briefcase",
|
2022-08-11 15:26:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["agenda"] != nil && modules["agenda"].(bool) {
|
2022-08-11 15:26:55 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
2022-09-05 05:25:05 +00:00
|
|
|
Title: "Agenda dispositifs",
|
|
|
|
Link: "/app/agenda/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == agendaMenu,
|
2022-08-12 12:53:54 +00:00
|
|
|
Icon: "hero:outline/calendar",
|
2022-08-11 15:26:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["directory"] != nil && modules["directory"].(bool) {
|
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Répertoire solutions",
|
|
|
|
Link: "/app/directory/",
|
|
|
|
Active: menuState == directoryMenu,
|
|
|
|
Icon: "hero:outline/document-text",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:26:55 +00:00
|
|
|
return RenderState{
|
|
|
|
IconSet: icons.NewIconSet(iconset),
|
|
|
|
Group: group,
|
|
|
|
Roles: roles,
|
|
|
|
LayoutState: ls,
|
|
|
|
}
|
|
|
|
}
|