Skip to content

Commit cac07f4

Browse files
Scott Bloch-Wehba-Seawarddorbin
Scott Bloch-Wehba-Seaward
authored andcommitted
feat(docs): highlight current page nav root when collapsed (spinnaker#480)
When collapsing a section of nav that contains a link to the current page the highlight indicating the current page is hidden. This PR introduces a highlight to collapsed nav entries to indicate current location in the hierarchy.
1 parent ea02102 commit cac07f4

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

_includes/nav_script.html

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
<script>
22
$(document).ready(function() {
3+
var ACTIVE_CLASS = 'active';
4+
var ACTIVE_SELECTOR = '.' + ACTIVE_CLASS;
5+
var OPEN_CLASS = 'open';
6+
37
$('.nav__items > li, li.collapsed').each(function(i, li) {
48
var $li = $(li);
59
var $ul = $li.find('> ul');
6-
$li.addClass('collapsible');
7-
$li.find('> span').on('click', function(e) {
8-
$li.toggleClass('open');
9-
$ul.toggle({ duration: 100 });
10+
var hasActiveChild = $li.find(ACTIVE_SELECTOR).length !== 0;
11+
setInitialSubnavState($li, $ul, hasActiveChild);
12+
$li.find('> span').on('click', function() {
13+
toggleSubNav($li, $ul, hasActiveChild);
1014
});
11-
if ($li.hasClass('collapsed')) {
12-
if ($li.find('.active').length === 0) {
13-
$ul.hide();
15+
});
16+
17+
function setInitialSubnavState($parentLi, $subnavUl, hasActiveChild) {
18+
$parentLi.addClass('collapsible');
19+
if ($parentLi.hasClass('collapsed')) {
20+
if (hasActiveChild) {
21+
$parentLi.addClass(OPEN_CLASS);
1422
} else {
15-
$li.addClass('open');
23+
$subnavUl.hide();
1624
}
25+
} else {
26+
$parentLi.addClass(OPEN_CLASS);
1727
}
18-
});
28+
}
29+
30+
function toggleSubNav($parentLi, $subnavUl, hasActiveChild) {
31+
if (hasActiveChild) {
32+
if (!$parentLi.hasClass(OPEN_CLASS)) {
33+
$parentLi.removeClass(ACTIVE_CLASS);
34+
} else {
35+
$parentLi.addClass(ACTIVE_CLASS);
36+
}
37+
}
38+
$parentLi.toggleClass(OPEN_CLASS);
39+
$subnavUl.toggle({ duration: 100 });
40+
}
1941
});
2042
</script>

0 commit comments

Comments
 (0)