Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depth setting #52

Open
wants to merge 5 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions dist/bootstrap-toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
return $li;
},

generateEmptyNavEl: function() {
var $li = $('<li></li>');
return $li;
},

generateNavItem: function(headingEl) {
var anchor = this.generateAnchor(headingEl);
var $heading = $(headingEl);
Expand All @@ -105,41 +110,55 @@
return 1;
},

// 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;
// Returns the headings by depth defined in the settings.
getHeadings: function($scope, depth, topLevel) {
var selector = '';
for (var i = topLevel; i < topLevel + depth; i++) {
selector += 'h' + i;
if (i < topLevel + depth - 1)
selector += ',';
}

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

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

populateNav: function($topContext, topLevel, $headings) {
var $context = $topContext;
var $prevNav;
populateNav: function($topContext, depth, topLevel, $headings) {
var $contexts = new Array(depth);
var helpers = this;

$contexts[0] = $topContext;
$topContext.lastNav = null;

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

// 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
for (j = relLevel + 1; j < $contexts.length; j++) {
$contexts[j] = null;
}

$context.append($newNav);
if (!$contexts[relLevel]) {
for (j = 0; j < relLevel; j++) {
if (!$contexts[j + 1]) {
if (!$contexts[j].lastNav) {
var $emptyNav = helpers.generateEmptyNavEl();
$contexts[j].append($emptyNav);
$contexts[j].lastNav = $emptyNav;
}
$contexts[j + 1] = helpers.createChildNavList($contexts[j].lastNav);
$contexts[j + 1].lastNav = null;
}
}
}

$prevNav = $newNav;
$contexts[relLevel].append($newNav);
$contexts[relLevel].lastNav = $newNav;
});
},

Expand All @@ -153,6 +172,7 @@
opts = arg;
}
opts.$scope = opts.$scope || $(document.body);
opts.depth = opts.depth || opts.$nav.attr('data-toc-depth') || 2;
return opts;
}
},
Expand All @@ -166,8 +186,8 @@

var $topContext = this.helpers.createChildNavList(opts.$nav);
var topLevel = this.helpers.getTopLevel(opts.$scope);
var $headings = this.helpers.getHeadings(opts.$scope, topLevel);
this.helpers.populateNav($topContext, topLevel, $headings);
var $headings = this.helpers.getHeadings(opts.$scope, opts.depth, topLevel);
this.helpers.populateNav($topContext, opts.depth, topLevel, $headings);
}
};

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.

3 changes: 2 additions & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ All options are optional, unless otherwise indicated.
| option | type | notes |
| -------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `$nav` | jQuery Object | (required) The element that the navigation will be created in. |
| `$scope` | jQuery Object | The element where the search for headings will be limited to, or the list of headings that will be used in the navigation. Defaults to `$(document.body)`. |
| `$scope` | jQuery Object | The element where the search for headings will be limited to, or the list of headings that will be used in the navigation. Defaults to `$(document.body)`. |
| `depth` | number (int) | The max depth of the navigation. Defaults to 2. |
{: .table }

## Customization
Expand Down