|
| 1 | +/* |
| 2 | + * Table of Contents jQuery Plugin - jquery.toc |
| 3 | + * |
| 4 | + * Copyright 2013-2016 Nikhil Dabas |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 7 | + * in compliance with the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 12 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 13 | + * or implied. See the License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +(function ($) { |
| 18 | + "use strict"; |
| 19 | + |
| 20 | + // Builds a list with the table of contents in the current selector. |
| 21 | + // options: |
| 22 | + // content: where to look for headings |
| 23 | + // headings: string with a comma-separated list of selectors to be used as headings, ordered |
| 24 | + // by their relative hierarchy level |
| 25 | + var toc = function (options) { |
| 26 | + return this.each(function () { |
| 27 | + var root = $(this), |
| 28 | + data = root.data(), |
| 29 | + thisOptions, |
| 30 | + stack = [root], // The upside-down stack keeps track of list elements |
| 31 | + listTag = this.tagName, |
| 32 | + currentLevel = 0, |
| 33 | + headingSelectors; |
| 34 | + |
| 35 | + // Defaults: plugin parameters override data attributes, which override our defaults |
| 36 | + thisOptions = $.extend( |
| 37 | + {content: "body", headings: "h1,h2,h3"}, |
| 38 | + {content: data.toc || undefined, headings: data.tocHeadings || undefined}, |
| 39 | + options |
| 40 | + ); |
| 41 | + headingSelectors = thisOptions.headings.split(","); |
| 42 | + |
| 43 | + // Set up some automatic IDs if we do not already have them |
| 44 | + $(thisOptions.content).find(thisOptions.headings).attr("id", function (index, attr) { |
| 45 | + // In HTML5, the id attribute must be at least one character long and must not |
| 46 | + // contain any space characters. |
| 47 | + // |
| 48 | + // We just use the HTML5 spec now because all browsers work fine with it. |
| 49 | + // https://mathiasbynens.be/notes/html5-id-class |
| 50 | + var generateUniqueId = function (text) { |
| 51 | + // Generate a valid ID. Spaces are replaced with underscores. We also check if |
| 52 | + // the ID already exists in the document. If so, we append "_1", "_2", etc. |
| 53 | + // until we find an unused ID. |
| 54 | + |
| 55 | + if (text.length === 0) { |
| 56 | + text = "?"; |
| 57 | + } |
| 58 | + |
| 59 | + var baseId = text.replace(/\s+/g, "_"), suffix = "", count = 1; |
| 60 | + |
| 61 | + while (document.getElementById(baseId + suffix) !== null) { |
| 62 | + suffix = "_" + count++; |
| 63 | + } |
| 64 | + |
| 65 | + return baseId + suffix; |
| 66 | + }; |
| 67 | + |
| 68 | + return attr || generateUniqueId($(this).text()); |
| 69 | + }).each(function () { |
| 70 | + // What level is the current heading? |
| 71 | + var elem = $(this), level = $.map(headingSelectors, function (selector, index) { |
| 72 | + return elem.is(selector) ? index : undefined; |
| 73 | + })[0]; |
| 74 | + |
| 75 | + if (level > currentLevel) { |
| 76 | + // If the heading is at a deeper level than where we are, start a new nested |
| 77 | + // list, but only if we already have some list items in the parent. If we do |
| 78 | + // not, that means that we're skipping levels, so we can just add new list items |
| 79 | + // at the current level. |
| 80 | + // In the upside-down stack, unshift = push, and stack[0] = the top. |
| 81 | + var parentItem = stack[0].children("li:last")[0]; |
| 82 | + if (parentItem) { |
| 83 | + stack.unshift($("<" + listTag + "/>").appendTo(parentItem)); |
| 84 | + } |
| 85 | + } else { |
| 86 | + // Truncate the stack to the current level by chopping off the 'top' of the |
| 87 | + // stack. We also need to preserve at least one element in the stack - that is |
| 88 | + // the containing element. |
| 89 | + stack.splice(0, Math.min(currentLevel - level, Math.max(stack.length - 1, 0))); |
| 90 | + } |
| 91 | + |
| 92 | + // Add the list item |
| 93 | + $("<li/>").appendTo(stack[0]).append( |
| 94 | + $("<a/>").text(elem.text()).attr("href", "#" + elem.attr("id")) |
| 95 | + ); |
| 96 | + |
| 97 | + currentLevel = level; |
| 98 | + }); |
| 99 | + }); |
| 100 | + }, old = $.fn.toc; |
| 101 | + |
| 102 | + $.fn.toc = toc; |
| 103 | + |
| 104 | + $.fn.toc.noConflict = function () { |
| 105 | + $.fn.toc = old; |
| 106 | + return this; |
| 107 | + }; |
| 108 | + |
| 109 | + // Data API |
| 110 | + $(function () { |
| 111 | + toc.call($("[data-toc]")); |
| 112 | + }); |
| 113 | +}(window.jQuery)); |
0 commit comments