From 1aca2e1069b7bf72e4d062f1a5584e4b8b5390bd Mon Sep 17 00:00:00 2001 From: Sasa Nikolic Date: Thu, 17 Jan 2019 19:24:45 +0100 Subject: [PATCH 1/5] Rerolled MathieuDuponchelle's changes for the depth setting --- dist/bootstrap-toc.js | 68 ++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/dist/bootstrap-toc.js b/dist/bootstrap-toc.js index 2ce99ad..c092424 100644 --- a/dist/bootstrap-toc.js +++ b/dist/bootstrap-toc.js @@ -86,6 +86,11 @@ return $li; }, + generateEmptyNavEl: function() { + var $li = $('
  • '); + return $li; + }, + generateNavItem: function(headingEl) { var anchor = this.generateAnchor(headingEl); var $heading = $(headingEl); @@ -97,7 +102,7 @@ getTopLevel: function($scope) { for (var i = 1; i <= 6; i++) { var $headings = this.findOrFilter($scope, "h" + i); - if ($headings.length > 1) { + if ($headings.length > 0) { return i; } } @@ -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; }); }, @@ -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; } }, @@ -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); } }; From a5815fad1faec06a889c72222d55a3b2a750c7d7 Mon Sep 17 00:00:00 2001 From: Sasa Nikolic Date: Thu, 17 Jan 2019 19:29:00 +0100 Subject: [PATCH 2/5] Reverted back the change for headings.length --- dist/bootstrap-toc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/bootstrap-toc.js b/dist/bootstrap-toc.js index c092424..7d4d2bf 100644 --- a/dist/bootstrap-toc.js +++ b/dist/bootstrap-toc.js @@ -102,7 +102,7 @@ getTopLevel: function($scope) { for (var i = 1; i <= 6; i++) { var $headings = this.findOrFilter($scope, "h" + i); - if ($headings.length > 0) { + if ($headings.length > 1) { return i; } } From 5150901773f38a1f347e0105e3ac1536fc893c63 Mon Sep 17 00:00:00 2001 From: Sasa Nikolic Date: Thu, 17 Jan 2019 19:46:42 +0100 Subject: [PATCH 3/5] Minified the js --- dist/bootstrap-toc.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/bootstrap-toc.min.js b/dist/bootstrap-toc.min.js index c628326..58ce94f 100644 --- a/dist/bootstrap-toc.min.js +++ b/dist/bootstrap-toc.min.js @@ -2,4 +2,4 @@ * Bootstrap Table of Contents v1.0.1 (http://afeld.github.io/bootstrap-toc/) * Copyright 2015 Aidan Feldman * Licensed under MIT (https://github.com/afeld/bootstrap-toc/blob/gh-pages/LICENSE.md) */ -!function(a){"use strict";window.Toc={helpers:{findOrFilter:function(e,t){var n=e.find(t);return e.filter(t).add(n).filter(":not([data-toc-skip])")},generateUniqueIdBase:function(e){return a(e).text().trim().replace(/\'/gi,"").replace(/[& +$,:;=?@"#{}|^~[`%!'<>\]\.\/\(\)\*\\\n\t\b\v]/g,"-").replace(/-{2,}/g,"-").substring(0,64).replace(/^-+|-+$/gm,"").toLowerCase()||e.tagName.toLowerCase()},generateUniqueId:function(e){for(var t=this.generateUniqueIdBase(e),n=0;;n++){var r=t;if(0')},createChildNavList:function(e){var t=this.createNavList();return e.append(t),t},generateNavEl:function(e,t){var n=a('');n.attr("href","#"+e),n.text(t);var r=a("
  • ");return r.append(n),r},generateNavItem:function(e){var t=this.generateAnchor(e),n=a(e),r=n.data("toc-text")||n.text();return this.generateNavEl(t,r)},getTopLevel:function(e){for(var t=1;t<=6;t++){if(1\]\.\/\(\)\*\\\n\t\b\v]/g,"-").replace(/-{2,}/g,"-").substring(0,64).replace(/^-+|-+$/gm,"").toLowerCase()||t.tagName.toLowerCase()},generateUniqueId:function(e){for(var t=this.generateUniqueIdBase(e),a=0;;a++){var n=t;if(a>0&&(n+="-"+a),!document.getElementById(n))return n}},generateAnchor:function(e){if(e.id)return e.id;var t=this.generateUniqueId(e);return e.id=t,t},createNavList:function(){return e('')},createChildNavList:function(e){var t=this.createNavList();return e.append(t),t},generateNavEl:function(t,a){var n=e('');n.attr("href","#"+t),n.text(a);var r=e("
  • ");return r.append(n),r},generateEmptyNavEl:function(){return e("
  • ")},generateNavItem:function(t){var a=this.generateAnchor(t),n=e(t),r=n.data("toc-text")||n.text();return this.generateNavEl(a,r)},getTopLevel:function(e){for(var t=1;t<=6;t++){if(this.findOrFilter(e,"h"+t).length>1)return t}return 1},getHeadings:function(e,t,a){for(var n="",r=a;r Date: Mon, 18 Mar 2019 19:30:45 +0100 Subject: [PATCH 4/5] Updated the options section in the readme --- index.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.md b/index.md index a2531c5..e8789f5 100644 --- a/index.md +++ b/index.md @@ -87,7 +87,9 @@ 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 | The max depth of the navigation. Defaults to 2. | {: .table } ## Customization From 964361cfa6faa296f0aa7018468220e118e88398 Mon Sep 17 00:00:00 2001 From: Sasa Nikolic Date: Mon, 18 Mar 2019 19:37:08 +0100 Subject: [PATCH 5/5] Fixed the table display --- index.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.md b/index.md index e8789f5..ec49b85 100644 --- a/index.md +++ b/index.md @@ -87,9 +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)`. - -| `depth` | number | The max depth of the navigation. Defaults to 2. | +| `$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