Skip to content

Commit 07d87ba

Browse files
ruxkorkytta
authored andcommitted
Correctly parse a deeper menu structure
When `MENU_SELECT_PARENTS` is set to `True`, we want to set all elements up to the root to `selected`.
1 parent e89ed4b commit 07d87ba

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

simple_menu/menu.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,20 @@ def process(c, request, name=None):
124124
# determine if we should apply 'selected' to parents when one of their
125125
# children is the 'selected' menu
126126
if getattr(settings, 'MENU_SELECT_PARENTS', False):
127-
def is_child_selected(item):
128-
for child in item.children:
129-
if child.selected or is_child_selected(child):
130-
return True
127+
def get_path_selected(item, parents):
128+
if item.selected:
129+
return parents + [item]
130+
else:
131+
for child in item.children:
132+
path_selected = get_path_selected(child, parents + [item])
133+
if path_selected:
134+
return path_selected
131135

132136
for item in visible:
133-
if is_child_selected(item):
134-
item.selected = True
137+
path_selected = get_path_selected(item, [])
138+
if path_selected:
139+
for item_selected in path_selected:
140+
item_selected.selected = True
135141

136142
return visible
137143

0 commit comments

Comments
 (0)