Skip to content

Commit 8f787aa

Browse files
add methods for retrieving nesting depth and generating a left padding CSS value to Group struct
1 parent 3104c6b commit 8f787aa

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

models/group/group.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,35 @@ func (g *Group) ShortName(length int) string {
174174
return util.EllipsisDisplayString(g.Name, length)
175175
}
176176

177+
// Depth retrieves the depth/nesting level of this group
178+
func (g *Group) Depth(ctx context.Context) (d int) {
179+
err := g.LoadParentGroup(ctx)
180+
if err != nil {
181+
return 0
182+
}
183+
pg := g.ParentGroup
184+
for {
185+
if pg == nil {
186+
break
187+
}
188+
if pg.ParentGroup == nil {
189+
err = pg.LoadParentGroup(ctx)
190+
if err != nil {
191+
return 0
192+
}
193+
}
194+
d++
195+
pg = pg.ParentGroup
196+
}
197+
return
198+
}
199+
200+
// DisplayLeftMargin generates a value for the left margin
201+
// displayed on the frontend beside this group
202+
func (g *Group) DisplayLeftMargin(ctx context.Context) string {
203+
return fmt.Sprintf("%drem", g.Depth(ctx)+1)
204+
}
205+
177206
func GetGroupByID(ctx context.Context, id int64) (*Group, error) {
178207
group := new(Group)
179208

0 commit comments

Comments
 (0)