Skip to content

Commit f56a75b

Browse files
committed
calculate menu active status from server
1 parent 31173b6 commit f56a75b

File tree

3 files changed

+24
-55
lines changed

3 files changed

+24
-55
lines changed

adminlteui/core.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,14 @@ def make(self, request, models=None, deep=1, deep_limit=0):
5555
menu_item['target_blank'] = self.target_blank
5656
menu_item['menu_type'] = self.menu_type or 'group'
5757

58+
if menu_item['menu_type'] != 'group':
59+
if menu_item['url'] == request.path:
60+
menu_item['active'] = True
61+
5862
if self.child:
5963
if deep_limit == 0 or deep <= deep_limit:
6064
child_list = []
65+
has_child_active = False
6166
for child in self.child:
6267
deep += 1
6368
child_menu = child.make(request, models, deep, deep_limit)
@@ -66,8 +71,13 @@ def make(self, request, models=None, deep=1, deep_limit=0):
6671
if child_menu.get('menu_type', 'group') == 'group':
6772
if len(child_menu.get('child')) == 0:
6873
continue
74+
if child_menu.get('active') is True:
75+
has_child_active = True
76+
6977
child_list.append(child_menu)
7078
menu_item['child'] = child_list
79+
if has_child_active is True:
80+
menu_item['active'] = True
7181
else:
7282
return None
7383
return menu_item

adminlteui/templates/admin/base.html

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -441,56 +441,6 @@ <h3 class="control-sidebar-heading">Chat Settings</h3>
441441
{% if adminlte.demo %}
442442
<script src={% static "admin/dist/js/demo.js" %}></script>
443443
{% endif %}
444-
<script>
445-
// use js control active status of siderbar-menu
446-
$(function() {
447-
var menuActions = document.querySelectorAll('.sidebar-menu a')
448-
var pathname = document.querySelector('#adminIndex').pathname
449-
for (var i in menuActions) {
450-
try {
451-
if (window.location.pathname === pathname) {
452-
menuActions[i].parentNode.setAttribute('class', 'active')
453-
break
454-
}
455-
456-
if (window.location.href.indexOf(menuActions[i].href) !== -1) {
457-
if (menuActions[i].href===window.location.origin + pathname) {
458-
continue
459-
}
460-
console.log('menu matched.', window.location.href, menuActions[i].href);
461-
menuActions[i].parentNode.setAttribute('class', 'active');
462-
463-
let parent = menuActions[i].parentNode;
464-
while (true) {
465-
if (parent.parentNode.getAttribute('class') !== 'sidebar-menu tree') {
466-
if (parent.parentNode.getAttribute('class').indexOf('treeview-menu') !== -1) {
467-
parent.parentNode.setAttribute('class', 'treeview-menu menu-open');
468-
parent.parentNode.parentNode.setAttribute('class', 'treeview active');
469-
}
470-
parent = parent.parentNode;
471-
} else {
472-
break
473-
}
474-
}
475-
break
476-
}
477-
} catch (e) {
478-
console.log(window.location.href, menuActions[i].href, e)
479-
}
480-
}
481-
482-
var topMenuActions = document.querySelectorAll('.top-menu a')
483-
for (var j in topMenuActions) {
484-
if (window.location.href.indexOf(topMenuActions[j].href) !== -1) {
485-
topMenuActions[j].parentNode.setAttribute('class', 'active');
486-
if (topMenuActions[j].parentNode.parentNode.getAttribute('class') === 'dropdown-menu') {
487-
topMenuActions[j].parentNode.parentNode.parentNode.setAttribute('class', 'dropdown active');
488-
}
489-
console.log('top menu matched.', window.location.href, topMenuActions[j].href);
490-
}
491-
}
492-
})
493-
</script>
494444

495445
<script>
496446
// sidebar push menu control

adminlteui/templatetags/adminlte_menu.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,29 @@ def render_main_menu(menu):
3030
for menu_item in menu:
3131
child = menu_item.get('child', [])
3232
if child:
33+
if menu_item.get('active') is True:
34+
treeview_class = 'treeview active menu-open'
35+
treeview_menu_class = 'treeview-menu menu-open'
36+
else:
37+
treeview_class = 'treeview'
38+
treeview_menu_class = 'treeview-menu'
3339
menu_item_html = f'''
34-
<li class="treeview">
40+
<li class="{treeview_class}">
3541
<a href="javascript:void(0)">
3642
<i class="fa {menu_item.get('icon')}"></i>
3743
<span style="overflow: hidden; display: inline-block; vertical-align:top;">{menu_item.get('name')}</span>
3844
<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>
3945
</a>
40-
<ul class="treeview-menu">
46+
<ul class="{treeview_menu_class}">
4147
{render_main_menu(child)}
4248
</ul>
4349
</li>
4450
'''
4551
else:
4652
target_blank = '' if menu_item.get('target_blank') is False else 'target="_blank"'
53+
flag = 'active' if menu_item.get('active') is True else ''
4754
menu_item_html = f'''
48-
<li><a {target_blank} href="{menu_item.get('url')}"><i class="fa {menu_item.get('icon')}"></i><span> {menu_item.get('name')}</span></a></li>
55+
<li class="{flag}"><a {target_blank} href="{menu_item.get('url')}"><i class="fa {menu_item.get('icon')}"></i><span> {menu_item.get('name')}</span></a></li>
4956
'''
5057
html += menu_item_html
5158
return html
@@ -64,8 +71,9 @@ def render_top_menu(menu):
6471
for menu_item in menu:
6572
child = menu_item.get('child', [])
6673
if child:
74+
flag = 'active' if menu_item.get('active') is True else ''
6775
menu_item_html = f'''
68-
<li class="dropdown">
76+
<li class="dropdown {flag}">
6977
<a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown">
7078
{menu_item.get('name')} <span class="caret"></span>
7179
</a>
@@ -76,8 +84,9 @@ def render_top_menu(menu):
7684
'''
7785
else:
7886
target_blank = '' if menu_item.get('target_blank') is False else 'target="_blank"'
87+
flag = 'active' if menu_item.get('active') is True else ''
7988
menu_item_html = f'''
80-
<li><a {target_blank} href="{menu_item.get('url')}"> {menu_item.get('name')}</a></li>
89+
<li class="{flag}"><a {target_blank} href="{menu_item.get('url')}"> {menu_item.get('name')}</a></li>
8190
'''
8291
html += menu_item_html
8392
return html

0 commit comments

Comments
 (0)