Skip to content
This repository has been archived by the owner on May 14, 2023. It is now read-only.

Commit

Permalink
support multiple level headings
Browse files Browse the repository at this point in the history
  • Loading branch information
ongchi committed Sep 21, 2019
1 parent eb9c6d6 commit dc62df4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
38 changes: 20 additions & 18 deletions bootstrap-toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,37 +107,39 @@

// returns the elements for the top level, and the next below it
getHeadings: function($scope, topLevel) {
var topSelector = "h" + topLevel;

var secondaryLevel = topLevel + 1;
var secondarySelector = "h" + secondaryLevel;
var headings = [];
for (var i = topLevel; i <= 6; i++) {
headings.push("h" + i);
}
var headingList = headings.join()

return this.findOrFilter($scope, topSelector + "," + secondarySelector);
return this.findOrFilter($scope, headingList);
},

getNavLevel: function(el) {
return parseInt(el.tagName.charAt(1), 10);
},

populateNav: function($topContext, topLevel, $headings) {
var $context = $topContext;
var $context = [$topContext];
var $prevNav;

var helpers = this;
$headings.each(function(i, el) {
var navLevel = helpers.getNavLevel(el) - topLevel + 1;
var $newNav = helpers.generateNavItem(el);
var navLevel = helpers.getNavLevel(el);

// determine the proper $context
if (navLevel === topLevel) {
// use top level
$context = $topContext;
} else if ($prevNav && $context === $topContext) {
// create a new level of the tree and switch to it
$context = helpers.createChildNavList($prevNav);
} // else use the current $context

$context.append($newNav);

if (navLevel > $context.length) {
// create next level
$context.push(helpers.createChildNavList($prevNav));
} else if (navLevel < $context.length) {
// goes up
while (navLevel < $context.length) {
$context.pop();
}
}

$context[$context.length - 1].append($newNav);

$prevNav = $newNav;
});
Expand Down
38 changes: 20 additions & 18 deletions dist/bootstrap-toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,37 +107,39 @@

// returns the elements for the top level, and the next below it
getHeadings: function($scope, topLevel) {
var topSelector = "h" + topLevel;

var secondaryLevel = topLevel + 1;
var secondarySelector = "h" + secondaryLevel;
var headings = [];
for (var i = topLevel; i <= 6; i++) {
headings.push("h" + i);
}
var headingList = headings.join()

return this.findOrFilter($scope, topSelector + "," + secondarySelector);
return this.findOrFilter($scope, headingList);
},

getNavLevel: function(el) {
return parseInt(el.tagName.charAt(1), 10);
},

populateNav: function($topContext, topLevel, $headings) {
var $context = $topContext;
var $context = [$topContext];
var $prevNav;

var helpers = this;
$headings.each(function(i, el) {
var navLevel = helpers.getNavLevel(el) - topLevel + 1;
var $newNav = helpers.generateNavItem(el);
var navLevel = helpers.getNavLevel(el);

// determine the proper $context
if (navLevel === topLevel) {
// use top level
$context = $topContext;
} else if ($prevNav && $context === $topContext) {
// create a new level of the tree and switch to it
$context = helpers.createChildNavList($prevNav);
} // else use the current $context

$context.append($newNav);

if (navLevel > $context.length) {
// create next level
$context.push(helpers.createChildNavList($prevNav));
} else if (navLevel < $context.length) {
// goes up
while (navLevel < $context.length) {
$context.pop();
}
}

$context[$context.length - 1].append($newNav);

$prevNav = $newNav;
});
Expand Down
2 changes: 1 addition & 1 deletion dist/bootstrap-toc.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dc62df4

Please sign in to comment.