refactor: data structs for template + method
enclosing rooms.Room into roomTableData, with methods to be used in tempalte to get other template related derived data structures
This commit is contained in:
parent
8b23103e8e
commit
e4c56506be
|
@ -72,10 +72,7 @@ func streamingRoomStates(
|
|||
templFile := "templates/room.gohtml"
|
||||
tableTemplates := "templates/tableTemplates.gohtml"
|
||||
tmpl := template.Must(
|
||||
template.New("").Funcs(template.FuncMap{
|
||||
"bricksForPerson": bricksForPerson,
|
||||
"personsFromRoom": personsFromRoom,
|
||||
}).ParseFS(templateFs, tableTemplates, templFile))
|
||||
template.New("").ParseFS(templateFs, tableTemplates, templFile))
|
||||
|
||||
roomStream := roomsM.Subscribe(r.Context(), roomName)
|
||||
for room := range roomStream {
|
||||
|
@ -84,7 +81,11 @@ func streamingRoomStates(
|
|||
|
||||
var buffer bytes.Buffer
|
||||
|
||||
err := tmpl.ExecuteTemplate(&buffer, "simpleRoomShow", room)
|
||||
roomTemplateData := roomTableData{
|
||||
Room: &room,
|
||||
currentPerson: session.PersonId,
|
||||
}
|
||||
err := tmpl.ExecuteTemplate(&buffer, "simpleRoomShow", &roomTemplateData)
|
||||
if err != nil {
|
||||
log.Printf("/rooms/subscribe/%s got error on template %s", roomName, err)
|
||||
}
|
||||
|
@ -197,10 +198,7 @@ func roomPageRoute(
|
|||
baseFile := "templates/base.gohtml"
|
||||
tableTemplates := "templates/tableTemplates.gohtml"
|
||||
tmpl := template.Must(
|
||||
template.New("").Funcs(template.FuncMap{
|
||||
"bricksForPerson": bricksForPerson,
|
||||
"personsFromRoom": personsFromRoom,
|
||||
}).ParseFS(templateFs, tableTemplates, templFile, baseFile))
|
||||
template.New("").ParseFS(templateFs, tableTemplates, templFile, baseFile))
|
||||
|
||||
type GestureData struct {
|
||||
Name string
|
||||
|
@ -215,10 +213,13 @@ func roomPageRoute(
|
|||
}
|
||||
|
||||
contentData := struct {
|
||||
Room rooms.Room
|
||||
Room *roomTableData
|
||||
Gestures []GestureData
|
||||
}{
|
||||
Room: room,
|
||||
Room: &roomTableData{
|
||||
Room: &room,
|
||||
currentPerson: session.PersonId,
|
||||
},
|
||||
Gestures: gesturesData,
|
||||
}
|
||||
data := pageData{
|
||||
|
|
|
@ -33,32 +33,29 @@ var brickColors = map[rooms.HandGesture]template.CSS{
|
|||
rooms.ChangeTopic: "--change-topic-color",
|
||||
}
|
||||
|
||||
// data to be passed to "roomPeople" template
|
||||
type roomTableData struct {
|
||||
Persons []personData
|
||||
Total int
|
||||
Tangens float64
|
||||
*rooms.Room
|
||||
currentPerson rooms.PersonId
|
||||
}
|
||||
|
||||
func personsFromRoom(room rooms.Room) roomTableData {
|
||||
// TODO start from the 'logged in person'
|
||||
total := len(room.Paricipants)
|
||||
persons := make([]personData, 0, total)
|
||||
tangens := math.Tan(math.Pi / float64(total)) // Math.tan(Math.PI/m);
|
||||
// tangens = math.Round(tangens*100) / 100
|
||||
func (room *roomTableData)Persons() []personData {
|
||||
persons := make([]personData, 0, room.Total())
|
||||
for i, pId := range room.Paricipants {
|
||||
personData := personDataFromRoom(room, pId)
|
||||
personData.Index = i
|
||||
// personData.Tan = math.Round(tangens*100)/100
|
||||
persons = append(persons, personData)
|
||||
}
|
||||
return roomTableData{
|
||||
Persons: persons,
|
||||
Total: total,
|
||||
Tangens: tangens,
|
||||
}
|
||||
return persons
|
||||
}
|
||||
func (r *roomTableData)Total() int {
|
||||
return len(r.Paricipants)
|
||||
}
|
||||
func (r *roomTableData)Tangens() float64 {
|
||||
return math.Tan(math.Pi / float64(r.Total())) // Math.tan(Math.PI/m);
|
||||
}
|
||||
|
||||
func personDataFromRoom(room rooms.Room, pId rooms.PersonId) personData {
|
||||
func personDataFromRoom(room *roomTableData, pId rooms.PersonId) personData {
|
||||
person, exists := room.AllKnownPeople[pId]
|
||||
if !exists || !slices.Contains(room.Paricipants, pId) {
|
||||
return personData{}
|
||||
|
@ -85,7 +82,7 @@ func personDataFromRoom(room rooms.Room, pId rooms.PersonId) personData {
|
|||
}
|
||||
}
|
||||
|
||||
func bricksForPerson(pData personData) []brickState {
|
||||
func (pData personData)BricksForPerson() []brickState {
|
||||
var result = make([]brickState, 5)
|
||||
for gesture := rooms.ChangeTopic; gesture <= rooms.Meta; gesture++ {
|
||||
// for index := rooms.Meta; index >= rooms.ChangeTopic; index-- {
|
||||
|
@ -168,7 +165,7 @@ func roomTemplatesPreview(
|
|||
Bricks []SingleBrickData
|
||||
ABrick brickState
|
||||
TestPerson personData
|
||||
ARoom rooms.Room
|
||||
ARoom *roomTableData
|
||||
}{
|
||||
DefaultColor: "--expand-color",
|
||||
ABrick: brickState{
|
||||
|
@ -198,7 +195,10 @@ func roomTemplatesPreview(
|
|||
},
|
||||
},
|
||||
TestPerson: testPersonData,
|
||||
ARoom: aRoom,
|
||||
ARoom: &roomTableData{
|
||||
Room: &aRoom,
|
||||
currentPerson: aRoom.Paricipants[0],
|
||||
},
|
||||
}
|
||||
|
||||
pageData := pageData{
|
||||
|
@ -207,10 +207,7 @@ func roomTemplatesPreview(
|
|||
}
|
||||
|
||||
baseFile := "templates/base.gohtml"
|
||||
tmpl := template.Must(template.New("").Funcs(template.FuncMap{
|
||||
"bricksForPerson": bricksForPerson,
|
||||
"personsFromRoom": personsFromRoom,
|
||||
}).ParseFS(templateFs, baseFile,
|
||||
tmpl := template.Must(template.New("").ParseFS(templateFs, baseFile,
|
||||
"templates/tableTemplates.gohtml",
|
||||
"templates/tableTemplatesPreview.gohtml",
|
||||
))
|
||||
|
|
|
@ -65,13 +65,14 @@
|
|||
{{end}}
|
||||
|
||||
<h2>Now for a person</h2>
|
||||
<p>expected to be called with personData</p>
|
||||
{{ define "personBlocks" }}
|
||||
|
||||
<div class="person-bricks "
|
||||
id="person-{{.Index}}"
|
||||
style="--i: {{ .Index }}"
|
||||
>
|
||||
{{ range (bricksForPerson .) }}
|
||||
{{ range .BricksForPerson }}
|
||||
{{ template "brick" . }}
|
||||
{{ end }}
|
||||
<p>{{ .Name }}</p>
|
||||
|
@ -81,8 +82,8 @@
|
|||
|
||||
|
||||
<h2>And now i'll want to get all persons for a room</h2>
|
||||
<p>expected be called with room *roomTableData</p>
|
||||
{{ define "roomPeople" }}
|
||||
{{ with (personsFromRoom .)}}
|
||||
<div
|
||||
class="circle-container relative"
|
||||
style="--tan: {{ .Tangens }}; --m: {{ .Total }};"
|
||||
|
@ -91,5 +92,4 @@
|
|||
{{ template "personBlocks" . }}
|
||||
{{ end }}
|
||||
</div>
|
||||
{{ end }}
|
||||
{{end}}
|
||||
|
|
Loading…
Reference in New Issue