😎 Give your xaringan slides some style
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

160 lines
4.7KB

  1. /*!
  2. * Bootstrap Table of Contents v0.4.1 (http://afeld.github.io/bootstrap-toc/)
  3. * Copyright 2015 Aidan Feldman
  4. * Licensed under MIT (https://github.com/afeld/bootstrap-toc/blob/gh-pages/LICENSE.md) */
  5. (function() {
  6. 'use strict';
  7. window.Toc = {
  8. helpers: {
  9. // return all matching elements in the set, or their descendants
  10. findOrFilter: function($el, selector) {
  11. // http://danielnouri.org/notes/2011/03/14/a-jquery-find-that-also-finds-the-root-element/
  12. // http://stackoverflow.com/a/12731439/358804
  13. var $descendants = $el.find(selector);
  14. return $el.filter(selector).add($descendants).filter(':not([data-toc-skip])');
  15. },
  16. generateUniqueIdBase: function(el) {
  17. var text = $(el).text();
  18. var anchor = text.trim().toLowerCase().replace(/[^A-Za-z0-9]+/g, '-');
  19. return anchor || el.tagName.toLowerCase();
  20. },
  21. generateUniqueId: function(el) {
  22. var anchorBase = this.generateUniqueIdBase(el);
  23. for (var i = 0; ; i++) {
  24. var anchor = anchorBase;
  25. if (i > 0) {
  26. // add suffix
  27. anchor += '-' + i;
  28. }
  29. // check if ID already exists
  30. if (!document.getElementById(anchor)) {
  31. return anchor;
  32. }
  33. }
  34. },
  35. generateAnchor: function(el) {
  36. if (el.id) {
  37. return el.id;
  38. } else {
  39. var anchor = this.generateUniqueId(el);
  40. el.id = anchor;
  41. return anchor;
  42. }
  43. },
  44. createNavList: function() {
  45. return $('<ul class="nav"></ul>');
  46. },
  47. createChildNavList: function($parent) {
  48. var $childList = this.createNavList();
  49. $parent.append($childList);
  50. return $childList;
  51. },
  52. generateNavEl: function(anchor, text) {
  53. var $a = $('<a></a>');
  54. $a.attr('href', '#' + anchor);
  55. $a.text(text);
  56. var $li = $('<li></li>');
  57. $li.append($a);
  58. return $li;
  59. },
  60. generateNavItem: function(headingEl) {
  61. var anchor = this.generateAnchor(headingEl);
  62. var $heading = $(headingEl);
  63. var text = $heading.data('toc-text') || $heading.text();
  64. return this.generateNavEl(anchor, text);
  65. },
  66. // Find the first heading level (`<h1>`, then `<h2>`, etc.) that has more than one element. Defaults to 1 (for `<h1>`).
  67. getTopLevel: function($scope) {
  68. for (var i = 1; i <= 6; i++) {
  69. var $headings = this.findOrFilter($scope, 'h' + i);
  70. if ($headings.length > 1) {
  71. return i;
  72. }
  73. }
  74. return 1;
  75. },
  76. // returns the elements for the top level, and the next below it
  77. getHeadings: function($scope, topLevel) {
  78. var topSelector = 'h' + topLevel;
  79. var secondaryLevel = topLevel + 1;
  80. var secondarySelector = 'h' + secondaryLevel;
  81. return this.findOrFilter($scope, topSelector + ',' + secondarySelector);
  82. },
  83. getNavLevel: function(el) {
  84. return parseInt(el.tagName.charAt(1), 10);
  85. },
  86. populateNav: function($topContext, topLevel, $headings) {
  87. var $context = $topContext;
  88. var $prevNav;
  89. var helpers = this;
  90. $headings.each(function(i, el) {
  91. var $newNav = helpers.generateNavItem(el);
  92. var navLevel = helpers.getNavLevel(el);
  93. // determine the proper $context
  94. if (navLevel === topLevel) {
  95. // use top level
  96. $context = $topContext;
  97. } else if ($prevNav && $context === $topContext) {
  98. // create a new level of the tree and switch to it
  99. $context = helpers.createChildNavList($prevNav);
  100. } // else use the current $context
  101. $context.append($newNav);
  102. $prevNav = $newNav;
  103. });
  104. },
  105. parseOps: function(arg) {
  106. var opts;
  107. if (arg.jquery) {
  108. opts = {
  109. $nav: arg
  110. };
  111. } else {
  112. opts = arg;
  113. }
  114. opts.$scope = opts.$scope || $(document.body);
  115. return opts;
  116. }
  117. },
  118. // accepts a jQuery object, or an options object
  119. init: function(opts) {
  120. opts = this.helpers.parseOps(opts);
  121. // ensure that the data attribute is in place for styling
  122. opts.$nav.attr('data-toggle', 'toc');
  123. var $topContext = this.helpers.createChildNavList(opts.$nav);
  124. var topLevel = this.helpers.getTopLevel(opts.$scope);
  125. var $headings = this.helpers.getHeadings(opts.$scope, topLevel);
  126. this.helpers.populateNav($topContext, topLevel, $headings);
  127. }
  128. };
  129. $(function() {
  130. $('nav[data-toggle="toc"]').each(function(i, el) {
  131. var $nav = $(el);
  132. Toc.init($nav);
  133. });
  134. });
  135. })();