Debian Codegraph @

inst/rmd/h/tocify/jquery.tocify.js

raw symbols SCIP
1 /* jquery Tocify - v1.9.1 - 2013-10-22
2 * http://www.gregfranko.com/jquery.tocify.js/
3 * Copyright (c) 2013 Greg Franko; Licensed MIT */
4
5 // Immediately-Invoked Function Expression (IIFE) [Ben Alman Blog Post](http://benalman.com/news/2010/11/immediately-invoked-function-expression/) that calls another IIFE that contains all of the plugin logic. I used this pattern so that anyone viewing this code would not have to scroll to the bottom of the page to view the local parameters that were passed to the main IIFE.
6 (function(tocify) {
7
8 // ECMAScript 5 Strict Mode: [John Resig Blog Post](http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/)
9 "use strict";
10
11 // Calls the second IIFE and locally passes in the global jQuery, window, and document objects
12 tocify(window.jQuery, window, document);
13
14 }
15
16 // Locally passes in `jQuery`, the `window` object, the `document` object, and an `undefined` variable. The `jQuery`, `window` and `document` objects are passed in locally, to improve performance, since javascript first searches for a variable match within the local variables set before searching the global variables set. All of the global variables are also passed in locally to be minifier friendly. `undefined` can be passed in locally, because it is not a reserved word in JavaScript.
17 (function($, window, document, undefined) {
18
19 // ECMAScript 5 Strict Mode: [John Resig Blog Post](http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/)
20 "use strict";
21
22 var tocClassName = "tocify",
23 tocClass = "." + tocClassName,
24 tocFocusClassName = "tocify-focus",
25 tocHoverClassName = "tocify-hover",
26 hideTocClassName = "tocify-hide",
27 hideTocClass = "." + hideTocClassName,
28 headerClassName = "tocify-header",
29 headerClass = "." + headerClassName,
30 subheaderClassName = "tocify-subheader",
31 subheaderClass = "." + subheaderClassName,
32 itemClassName = "tocify-item",
33 itemClass = "." + itemClassName,
34 extendPageClassName = "tocify-extend-page",
35 extendPageClass = "." + extendPageClassName;
36
37 // Calling the jQueryUI Widget Factory Method
38 $.widget("toc.tocify", {
39
40 //Plugin version
41 version: "1.9.1",
42
43 // These options will be used as defaults
44 options: {
45
46 // **context**: Accepts String: Any jQuery selector
47 // The container element that holds all of the elements used to generate the table of contents
48 context: "body",
49
50 // **ignoreSelector**: Accepts String: Any jQuery selector
51 // A selector to any element that would be matched by selectors that you wish to be ignored
52 ignoreSelector: null,
53
54 // **selectors**: Accepts an Array of Strings: Any jQuery selectors
55 // The element's used to generate the table of contents. The order is very important since it will determine the table of content's nesting structure
56 selectors: "h1, h2, h3",
57
58 // **showAndHide**: Accepts a boolean: true or false
59 // Used to determine if elements should be shown and hidden
60 showAndHide: true,
61
62 // **showEffect**: Accepts String: "none", "fadeIn", "show", or "slideDown"
63 // Used to display any of the table of contents nested items
64 showEffect: "slideDown",
65
66 // **showEffectSpeed**: Accepts Number (milliseconds) or String: "slow", "medium", or "fast"
67 // The time duration of the show animation
68 showEffectSpeed: "medium",
69
70 // **hideEffect**: Accepts String: "none", "fadeOut", "hide", or "slideUp"
71 // Used to hide any of the table of contents nested items
72 hideEffect: "slideUp",
73
74 // **hideEffectSpeed**: Accepts Number (milliseconds) or String: "slow", "medium", or "fast"
75 // The time duration of the hide animation
76 hideEffectSpeed: "medium",
77
78 // **smoothScroll**: Accepts a boolean: true or false
79 // Determines if a jQuery animation should be used to scroll to specific table of contents items on the page
80 smoothScroll: true,
81
82 // **smoothScrollSpeed**: Accepts Number (milliseconds) or String: "slow", "medium", or "fast"
83 // The time duration of the smoothScroll animation
84 smoothScrollSpeed: "medium",
85
86 // **scrollTo**: Accepts Number (pixels)
87 // The amount of space between the top of page and the selected table of contents item after the page has been scrolled
88 scrollTo: 0,
89
90 // **showAndHideOnScroll**: Accepts a boolean: true or false
91 // Determines if table of contents nested items should be shown and hidden while scrolling
92 showAndHideOnScroll: true,
93
94 // **highlightOnScroll**: Accepts a boolean: true or false
95 // Determines if table of contents nested items should be highlighted (set to a different color) while scrolling
96 highlightOnScroll: true,
97
98 // **highlightOffset**: Accepts a number
99 // The offset distance in pixels to trigger the next active table of contents item
100 highlightOffset: 40,
101
102 // **theme**: Accepts a string: "bootstrap", "jqueryui", or "none"
103 // Determines if Twitter Bootstrap, jQueryUI, or Tocify classes should be added to the table of contents
104 theme: "bootstrap",
105
106 // **extendPage**: Accepts a boolean: true or false
107 // If a user scrolls to the bottom of the page and the page is not tall enough to scroll to the last table of contents item, then the page height is increased
108 extendPage: true,
109
110 // **extendPageOffset**: Accepts a number: pixels
111 // How close to the bottom of the page a user must scroll before the page is extended
112 extendPageOffset: 100,
113
114 // **history**: Accepts a boolean: true or false
115 // Adds a hash to the page url to maintain history
116 history: true,
117
118 // **scrollHistory**: Accepts a boolean: true or false
119 // Adds a hash to the page url, to maintain history, when scrolling to a TOC item
120 scrollHistory: false,
121
122 // **hashGenerator**: How the hash value (the anchor segment of the URL, following the
123 // # character) will be generated.
124 //
125 // "compact" (default) - #CompressesEverythingTogether
126 // "pretty" - #looks-like-a-nice-url-and-is-easily-readable
127 // function(text, element){} - Your own hash generation function that accepts the text as an
128 // argument, and returns the hash value.
129 hashGenerator: "compact",
130
131 // **highlightDefault**: Accepts a boolean: true or false
132 // Set's the first TOC item as active if no other TOC item is active.
133 highlightDefault: true
134
135 },
136
137 // _Create
138 // -------
139 // Constructs the plugin. Only called once.
140 _create: function() {
141
142 var self = this;
143
144 self.extendPageScroll = true;
145
146 // Internal array that keeps track of all TOC items (Helps to recognize if there are duplicate TOC item strings)
147 self.items = [];
148
149 // Generates the HTML for the dynamic table of contents
150 self._generateToc();
151
152 // Adds CSS classes to the newly generated table of contents HTML
153 self._addCSSClasses();
154
155 self.webkit = (function() {
156
157 for (var prop in window) {
158
159 if (prop) {
160
161 if (prop.toLowerCase().indexOf("webkit") !== -1) {
162
163 return true;
164
165 }
166
167 }
168
169 }
170
171 return false;
172
173 }());
174
175 // Adds jQuery event handlers to the newly generated table of contents
176 self._setEventHandlers();
177
178 // Binding to the Window load event to make sure the correct scrollTop is calculated
179 $(window).on("load", function() {
180
181 // Sets the active TOC item
182 self._setActiveElement(true);
183
184 // Once all animations on the page are complete, this callback function will be called
185 $("html, body").promise().done(function() {
186
187 setTimeout(function() {
188
189 self.extendPageScroll = false;
190
191 }, 0);
192
193 });
194
195 });
196
197 },
198
199 // _generateToc
200 // ------------
201 // Generates the HTML for the dynamic table of contents
202 _generateToc: function() {
203
204 // _Local variables_
205
206 // Stores the plugin context in the self variable
207 var self = this,
208
209 // All of the HTML tags found within the context provided (i.e. body) that match the top level jQuery selector above
210 firstElem,
211
212 // Instantiated variable that will store the top level newly created unordered list DOM element
213 ul,
214 ignoreSelector = self.options.ignoreSelector;
215
216
217 // Determine the element to start the toc with
218 // get all the top level selectors
219 firstElem = [];
220 var selectors = this.options.selectors.replace(/ /g, "").split(",");
221 // find the first set that have at least one non-ignored element
222 for(var i = 0; i < selectors.length; i++) {
223 var foundSelectors = $(this.options.context).find(selectors[i]);
224 for (var s = 0; s < foundSelectors.length; s++) {
225 if (!$(foundSelectors[s]).is(ignoreSelector)) {
226 firstElem = foundSelectors;
227 break;
228 }
229 }
230 if (firstElem.length> 0)
231 break;
232 }
233
234 if (!firstElem.length) {
235
236 self.element.addClass(hideTocClassName);
237
238 return;
239
240 }
241
242 self.element.addClass(tocClassName);
243
244 // Loops through each top level selector
245 firstElem.each(function(index) {
246
247 //If the element matches the ignoreSelector then we skip it
248 if ($(this).is(ignoreSelector)) {
249 return;
250 }
251
252 // Creates an unordered list HTML element and adds a dynamic ID and standard class name
253 ul = $("<ul/>", {
254 "id": headerClassName + index,
255 "class": headerClassName
256 }).
257
258 // Appends a top level list item HTML element to the previously created HTML header
259 append(self._nestElements($(this), index));
260
261 // Add the created unordered list element to the HTML element calling the plugin
262 self.element.append(ul);
263
264 // Finds all of the HTML tags between the header and subheader elements
265 $(this).nextUntil(this.nodeName.toLowerCase()).each(function() {
266
267 // If there are no nested subheader elemements
268 if ($(this).find(self.options.selectors).length === 0) {
269
270 // Loops through all of the subheader elements
271 $(this).filter(self.options.selectors).each(function() {
272
273 //If the element matches the ignoreSelector then we skip it
274 if ($(this).is(ignoreSelector)) {
275 return;
276 }
277
278 self._appendSubheaders.call(this, self, ul);
279
280 });
281
282 }
283
284 // If there are nested subheader elements
285 else {
286
287 // Loops through all of the subheader elements
288 $(this).find(self.options.selectors).each(function() {
289
290 //If the element matches the ignoreSelector then we skip it
291 if ($(this).is(ignoreSelector)) {
292 return;
293 }
294
295 self._appendSubheaders.call(this, self, ul);
296
297 });
298
299 }
300
301 });
302
303 });
304
305 },
306
307 _setActiveElement: function(pageload) {
308
309 var self = this,
310
311 hash = window.location.hash.substring(1),
312
313 elem = self.element.find('li[data-unique="' + hash + '"]');
314
315 if (hash.length) {
316
317 // Removes highlighting from all of the list item's
318 self.element.find("." + self.focusClass).removeClass(self.focusClass);
319
320 // Highlights the current list item that was clicked
321 elem.addClass(self.focusClass);
322
323 // Triggers the click event on the currently focused TOC item
324 elem.click();
325
326 } else {
327
328 // Removes highlighting from all of the list item's
329 self.element.find("." + self.focusClass).removeClass(self.focusClass);
330
331 if (!hash.length && pageload && self.options.highlightDefault) {
332
333 // Highlights the first TOC item if no other items are highlighted
334 self.element.find(itemClass).first().addClass(self.focusClass);
335
336 }
337
338 }
339
340 return self;
341
342 },
343
344 // _nestElements
345 // -------------
346 // Helps create the table of contents list by appending nested list items
347 _nestElements: function(self, index) {
348
349 var arr, item, hashValue;
350
351 arr = $.grep(this.items, function(item) {
352
353 return item === self.text();
354
355 });
356
357 // If there is already a duplicate TOC item
358 if (arr.length) {
359
360 // Adds the current TOC item text and index (for slight randomization) to the internal array
361 this.items.push(self.text() + index);
362
363 }
364
365 // If there not a duplicate TOC item
366 else {
367
368 // Adds the current TOC item text to the internal array
369 this.items.push(self.text());
370
371 }
372
373 hashValue = this._generateHashValue(arr, self, index);
374
375 // Appends a list item HTML element to the last unordered list HTML element found within the HTML element calling the plugin
376 item = $("<li/>", {
377
378 // Sets a common class name to the list item
379 "class": itemClassName,
380
381 "data-unique": hashValue
382
383 });
384
385 if (this.options.theme !== "bootstrap3") {
386
387 item.append($("<a/>", {
388
389 "html": self.html()
390
391 }));
392
393 } else {
394
395 item.html(self.html());
396
397 }
398
399 // Adds an HTML anchor tag before the currently traversed HTML element
400 self.before($("<div/>", {
401
402 // Sets a name attribute on the anchor tag to the text of the currently traversed HTML element (also making sure that all whitespace is replaced with an underscore)
403 "name": hashValue,
404
405 "data-unique": hashValue
406
407 }));
408
409 return item;
410
411 },
412
413 // _generateHashValue
414 // ------------------
415 // Generates the hash value that will be used to refer to each item.
416 _generateHashValue: function(arr, self, index) {
417
418 var hashValue = "",
419 hashGeneratorOption = this.options.hashGenerator;
420
421 if (hashGeneratorOption === "pretty") {
422
423 // prettify the text
424 hashValue = self.text().toLowerCase().replace(/\s/g, "-");
425
426 // fix double hyphens
427 while (hashValue.indexOf("--") > -1) {
428 hashValue = hashValue.replace(/--/g, "-");
429 }
430
431 // fix colon-space instances
432 while (hashValue.indexOf(":-") > -1) {
433 hashValue = hashValue.replace(/:-/g, "-");
434 }
435
436 } else if (typeof hashGeneratorOption === "function") {
437
438 // call the function
439 hashValue = hashGeneratorOption(self.text(), self);
440
441 } else {
442
443 // compact - the default
444 hashValue = self.text().replace(/\s/g, "");
445
446 }
447
448 // add the index if we need to
449 if (arr.length) {
450 hashValue += "" + index;
451 }
452
453 // return the value
454 return hashValue;
455
456 },
457
458 // _appendElements
459 // ---------------
460 // Helps create the table of contents list by appending subheader elements
461
462 _appendSubheaders: function(self, ul) {
463
464 // The current element index
465 var index = $(this).index(self.options.selectors),
466
467 // Finds the previous header DOM element
468 previousHeader = $(self.options.selectors).eq(index - 1),
469
470 currentTagName = +$(this).prop("tagName").charAt(1),
471
472 previousTagName = +previousHeader.prop("tagName").charAt(1),
473
474 lastSubheader;
475
476 // If the current header DOM element is smaller than the previous header DOM element or the first subheader
477 if (currentTagName < previousTagName) {
478
479 // Selects the last unordered list HTML found within the HTML element calling the plugin
480 self.element.find(subheaderClass + "[data-tag=" + currentTagName + "]").last().append(self._nestElements($(this), index));
481
482 }
483
484 // If the current header DOM element is the same type of header(eg. h4) as the previous header DOM element
485 else if (currentTagName === previousTagName) {
486
487 ul.find(itemClass).last().after(self._nestElements($(this), index));
488
489 } else {
490
491 // Selects the last unordered list HTML found within the HTML element calling the plugin
492 ul.find(itemClass).last().
493
494 // Appends an unorderedList HTML element to the dynamic `unorderedList` variable and sets a common class name
495 after($("<ul/>", {
496
497 "class": subheaderClassName,
498
499 "data-tag": currentTagName
500
501 })).next(subheaderClass).
502
503 // Appends a list item HTML element to the last unordered list HTML element found within the HTML element calling the plugin
504 append(self._nestElements($(this), index));
505 }
506
507 },
508
509 // _setEventHandlers
510 // ----------------
511 // Adds jQuery event handlers to the newly generated table of contents
512 _setEventHandlers: function() {
513
514 // _Local variables_
515
516 // Stores the plugin context in the self variable
517 var self = this,
518
519 // Instantiates a new variable that will be used to hold a specific element's context
520 $self,
521
522 // Instantiates a new variable that will be used to determine the smoothScroll animation time duration
523 duration;
524
525 // Event delegation that looks for any clicks on list item elements inside of the HTML element calling the plugin
526 this.element.on("click.tocify", "li", function(event) {
527
528 if (self.options.history) {
529
530 window.location.hash = $(this).attr("data-unique");
531
532 }
533
534 // Removes highlighting from all of the list item's
535 self.element.find("." + self.focusClass).removeClass(self.focusClass);
536
537 // Highlights the current list item that was clicked
538 $(this).addClass(self.focusClass);
539
540 // If the showAndHide option is true
541 if (self.options.showAndHide) {
542
543 var elem = $('li[data-unique="' + $(this).attr("data-unique") + '"]');
544
545 self._triggerShow(elem);
546
547 }
548
549 self._scrollTo($(this));
550
551 });
552
553 // Mouseenter and Mouseleave event handlers for the list item's within the HTML element calling the plugin
554 this.element.find("li").on({
555
556 // Mouseenter event handler
557 "mouseenter.tocify": function() {
558
559 // Adds a hover CSS class to the current list item
560 $(this).addClass(self.hoverClass);
561
562 // Makes sure the cursor is set to the pointer icon
563 $(this).css("cursor", "pointer");
564
565 },
566
567 // Mouseleave event handler
568 "mouseleave.tocify": function() {
569
570 if (self.options.theme !== "bootstrap") {
571
572 // Removes the hover CSS class from the current list item
573 $(this).removeClass(self.hoverClass);
574
575 }
576
577 }
578 });
579
580 // only attach handler if needed (expensive in IE)
581 if (self.options.extendPage || self.options.highlightOnScroll || self.options.scrollHistory || self.options.showAndHideOnScroll) {
582 // Window scroll event handler
583 $(window).on("scroll.tocify", function() {
584
585 // Once all animations on the page are complete, this callback function will be called
586 $("html, body").promise().done(function() {
587
588 // Local variables
589
590 // Stores how far the user has scrolled
591 var winScrollTop = $(window).scrollTop(),
592
593 // Stores the height of the window
594 winHeight = $(window).height(),
595
596 // Stores the height of the document
597 docHeight = $(document).height(),
598
599 scrollHeight = $("body")[0].scrollHeight,
600
601 // Instantiates a variable that will be used to hold a selected HTML element
602 elem,
603
604 lastElem,
605
606 lastElemOffset,
607
608 currentElem;
609
610 if (self.options.extendPage) {
611
612 // If the user has scrolled to the bottom of the page and the last toc item is not focused
613 if ((self.webkit && winScrollTop >= scrollHeight - winHeight - self.options.extendPageOffset) || (!self.webkit && winHeight + winScrollTop > docHeight - self.options.extendPageOffset)) {
614
615 if (!$(extendPageClass).length) {
616
617 lastElem = $('div[data-unique="' + $(itemClass).last().attr("data-unique") + '"]');
618
619 if (!lastElem.length) return;
620
621 // Gets the top offset of the page header that is linked to the last toc item
622 lastElemOffset = lastElem.offset().top;
623
624 // Appends a div to the bottom of the page and sets the height to the difference of the window scrollTop and the last element's position top offset
625 $(self.options.context).append($("<div/>", {
626
627 "class": extendPageClassName,
628
629 "height": Math.abs(lastElemOffset - winScrollTop) + "px",
630
631 "data-unique": extendPageClassName
632
633 }));
634
635 if (self.extendPageScroll) {
636
637 currentElem = self.element.find('li.' + self.focusClass);
638
639 self._scrollTo($('div[data-unique="' + currentElem.attr("data-unique") + '"]'));
640
641 }
642
643 }
644
645 }
646
647 }
648
649 // The zero timeout ensures the following code is run after the scroll events
650 setTimeout(function() {
651
652 // _Local variables_
653
654 // Stores the distance to the closest anchor
655 var closestAnchorDistance = null,
656
657 // Stores the index of the closest anchor
658 closestAnchorIdx = null,
659
660 // Keeps a reference to all anchors
661 anchors = $(self.options.context).find("div[data-unique]"),
662
663 anchorText;
664
665 // Determines the index of the closest anchor
666 anchors.each(function(idx) {
667 var distance = Math.abs(($(this).next().length ? $(this).next() : $(this)).offset().top - winScrollTop - self.options.highlightOffset);
668 if (closestAnchorDistance == null || distance < closestAnchorDistance) {
669 closestAnchorDistance = distance;
670 closestAnchorIdx = idx;
671 } else {
672 return false;
673 }
674 });
675
676 anchorText = $(anchors[closestAnchorIdx]).attr("data-unique");
677
678 // Stores the list item HTML element that corresponds to the currently traversed anchor tag
679 elem = $('li[data-unique="' + anchorText + '"]');
680
681 // If the `highlightOnScroll` option is true and a next element is found
682 if (self.options.highlightOnScroll && elem.length) {
683
684 // Removes highlighting from all of the list item's
685 self.element.find("." + self.focusClass).removeClass(self.focusClass);
686
687 // Highlights the corresponding list item
688 elem.addClass(self.focusClass);
689
690 }
691
692 if (self.options.scrollHistory) {
693
694 if (window.location.hash !== "#" + anchorText) {
695
696 window.location.replace("#" + anchorText);
697
698 }
699 }
700
701 // If the `showAndHideOnScroll` option is true
702 if (self.options.showAndHideOnScroll && self.options.showAndHide) {
703
704 self._triggerShow(elem, true);
705
706 }
707
708 }, 0);
709
710 });
711
712 });
713 }
714
715 },
716
717 // Show
718 // ----
719 // Opens the current sub-header
720 show: function(elem, scroll) {
721
722 // Stores the plugin context in the `self` variable
723 var self = this,
724 element = elem;
725
726 // If the sub-header is not already visible
727 if (!elem.is(":visible")) {
728
729 // If the current element does not have any nested subheaders, is not a header, and its parent is not visible
730 if (!elem.find(subheaderClass).length && !elem.parent().is(headerClass) && !elem.parent().is(":visible")) {
731
732 // Sets the current element to all of the subheaders within the current header
733 elem = elem.parents(subheaderClass).add(elem);
734
735 }
736
737 // If the current element does not have any nested subheaders and is not a header
738 else if (!elem.children(subheaderClass).length && !elem.parent().is(headerClass)) {
739
740 // Sets the current element to the closest subheader
741 elem = elem.closest(subheaderClass);
742
743 }
744
745 //Determines what jQuery effect to use
746 switch (self.options.showEffect) {
747
748 //Uses `no effect`
749 case "none":
750
751 elem.show();
752
753 break;
754
755 //Uses the jQuery `show` special effect
756 case "show":
757
758 elem.show(self.options.showEffectSpeed);
759
760 break;
761
762 //Uses the jQuery `slideDown` special effect
763 case "slideDown":
764
765 elem.slideDown(self.options.showEffectSpeed);
766
767 break;
768
769 //Uses the jQuery `fadeIn` special effect
770 case "fadeIn":
771
772 elem.fadeIn(self.options.showEffectSpeed);
773
774 break;
775
776 //If none of the above options were passed, then a `jQueryUI show effect` is expected
777 default:
778
779 elem.show();
780
781 break;
782
783 }
784
785 }
786
787 // If the current subheader parent element is a header
788 if (elem.parent().is(headerClass)) {
789
790 // Hides all non-active sub-headers
791 self.hide($(subheaderClass).not(elem));
792
793 }
794
795 // If the current subheader parent element is not a header
796 else {
797
798 // Hides all non-active sub-headers
799 self.hide($(subheaderClass).not(elem.closest(headerClass).find(subheaderClass).not(elem.siblings())));
800
801 }
802
803 // Maintains chainablity
804 return self;
805
806 },
807
808 // Hide
809 // ----
810 // Closes the current sub-header
811 hide: function(elem) {
812
813 // Stores the plugin context in the `self` variable
814 var self = this;
815
816 //Determines what jQuery effect to use
817 switch (self.options.hideEffect) {
818
819 // Uses `no effect`
820 case "none":
821
822 elem.hide();
823
824 break;
825
826 // Uses the jQuery `hide` special effect
827 case "hide":
828
829 elem.hide(self.options.hideEffectSpeed);
830
831 break;
832
833 // Uses the jQuery `slideUp` special effect
834 case "slideUp":
835
836 elem.slideUp(self.options.hideEffectSpeed);
837
838 break;
839
840 // Uses the jQuery `fadeOut` special effect
841 case "fadeOut":
842
843 elem.fadeOut(self.options.hideEffectSpeed);
844
845 break;
846
847 // If none of the above options were passed, then a `jqueryUI hide effect` is expected
848 default:
849
850 elem.hide();
851
852 break;
853
854 }
855
856 // Maintains chainablity
857 return self;
858 },
859
860 // _triggerShow
861 // ------------
862 // Determines what elements get shown on scroll and click
863 _triggerShow: function(elem, scroll) {
864
865 var self = this;
866
867 // If the current element's parent is a header element or the next element is a nested subheader element
868 if (elem.parent().is(headerClass) || elem.next().is(subheaderClass)) {
869
870 // Shows the next sub-header element
871 self.show(elem.next(subheaderClass), scroll);
872
873 }
874
875 // If the current element's parent is a subheader element
876 else if (elem.parent().is(subheaderClass)) {
877
878 // Shows the parent sub-header element
879 self.show(elem.parent(), scroll);
880
881 }
882
883 // Maintains chainability
884 return self;
885
886 },
887
888 // _addCSSClasses
889 // --------------
890 // Adds CSS classes to the newly generated table of contents HTML
891 _addCSSClasses: function() {
892
893 // If the user wants a jqueryUI theme
894 if (this.options.theme === "jqueryui") {
895
896 this.focusClass = "ui-state-default";
897
898 this.hoverClass = "ui-state-hover";
899
900 //Adds the default styling to the dropdown list
901 this.element.addClass("ui-widget").find(".toc-title").addClass("ui-widget-header").end().find("li").addClass("ui-widget-content");
902
903 }
904
905 // If the user wants a twitterBootstrap theme
906 else if (this.options.theme === "bootstrap") {
907
908 this.element.find(headerClass + "," + subheaderClass).addClass("nav nav-list");
909
910 this.focusClass = "active";
911
912 }
913
914 // If the user wants a twitterBootstrap theme
915 else if (this.options.theme === "bootstrap3") {
916
917 this.element.find(headerClass + "," + subheaderClass).addClass("list-group");
918
919 this.element.find(itemClass).addClass("list-group-item");
920
921 this.focusClass = "active";
922
923 }
924
925 // If a user does not want a prebuilt theme
926 else {
927
928 // Adds more neutral classes (instead of jqueryui)
929
930 this.focusClass = tocFocusClassName;
931
932 this.hoverClass = tocHoverClassName;
933
934 }
935
936 //Maintains chainability
937 return this;
938
939 },
940
941 // setOption
942 // ---------
943 // Sets a single Tocify option after the plugin is invoked
944 setOption: function() {
945
946 // Calls the jQueryUI Widget Factory setOption method
947 $.Widget.prototype._setOption.apply(this, arguments);
948
949 },
950
951 // setOptions
952 // ----------
953 // Sets a single or multiple Tocify options after the plugin is invoked
954 setOptions: function() {
955
956 // Calls the jQueryUI Widget Factory setOptions method
957 $.Widget.prototype._setOptions.apply(this, arguments);
958
959 },
960
961 // _scrollTo
962 // ---------
963 // Scrolls to a specific element
964 _scrollTo: function(elem) {
965
966 var self = this,
967 duration = self.options.smoothScroll || 0,
968 scrollTo = self.options.scrollTo,
969 currentDiv = $('div[data-unique="' + elem.attr("data-unique") + '"]');
970
971 if (!currentDiv.length) {
972
973 return self;
974
975 }
976
977 // Once all animations on the page are complete, this callback function will be called
978 $("html, body").promise().done(function() {
979
980 // Animates the html and body element scrolltops
981 $("html, body").animate({
982
983 // Sets the jQuery `scrollTop` to the top offset of the HTML div tag that matches the current list item's `data-unique` tag
984 "scrollTop": currentDiv.offset().top - ($.isFunction(scrollTo) ? scrollTo.call() : scrollTo) + "px"
985
986 }, {
987
988 // Sets the smoothScroll animation time duration to the smoothScrollSpeed option
989 "duration": duration
990
991 });
992
993 });
994
995 // Maintains chainability
996 return self;
997
998 }
999
1000 });
1001
1002 })); //end of plugin