Skip to content

Instantly share code, notes, and snippets.

@fabianneve
Created June 13, 2018 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabianneve/413b64e721beb292420003defd519939 to your computer and use it in GitHub Desktop.
Save fabianneve/413b64e721beb292420003defd519939 to your computer and use it in GitHub Desktop.
"use strict";
(function($){
$.fn.tocMenu = function(options)
{
// Helpder Methodes
String.prototype.repeat = function( num )
{
return new Array( num + 1 ).join( this );
};
// Default settings
var defaults = {
orderedlist : true,
customStyle : "no-bullets",
attachTo : ".ewiki-slink",
prepend : true,
headlineText : "Table of Content",
maxLevel : 6
};
// check if settings exist
var settings = $.extend( {}, defaults, options );
var tocElements = "";
this.filter(
function(){
var listTag = "ol";
if($("#MSOLayout_InDesignMode") !== null && $("#MSOLayout_InDesignMode").val()){
return;
}
if(!settings.orderedlist){
listTag = "ul";
}
// Search for filter
var headers = $(":header", $(this));
// Just in case no headers have been found
if(headers.length === 0){
return;
}
// define previous tag name used for loop and indention
var prevTagName = null;
// check if header should be set
if(settings.headlineText !== null){
tocElements += "<b>"+settings.headlineText+"</b>";
}
// initalize element of toc
if(settings.customStyle !== null){
tocElements += "<"+listTag+" class='tocMain "+settings.customStyle+"'>";
} else {
tocElements += "<"+listTag+" class='tocMain'>";
}
// Defiend level of indention to create perfect unorder list
var lvlCounter = 0;
// Loop through headline
for(var i = 0; i < headers.length; i++){
var nameLink = "<a name='chapter"+i+"' />";
var tmpHeader = $(headers[i]).prepend(nameLink);
if(prevTagName !== null && tmpHeader.prop('tagName') < prevTagName){
tocElements += ("</"+listTag+">").repeat(lvlCounter);
lvlCounter -= 1;
} else if(prevTagName !== null && tmpHeader.prop('tagName') > prevTagName){
tocElements += "<"+listTag+">";
lvlCounter += 1;
}
tocElements += "<li><a href='#chapter"+i+"'>"+tmpHeader.text()+"</a></li>\n";
prevTagName = tmpHeader.prop('tagName');
}
}
);
if(settings.prepend){
$(settings.attachTo).prepend(tocElements);
} else {
$(settings.attachTo).append(tocElements);
}
};
})(jQuery);
var wrap = $("#s4-workspace");
wrap.on("scroll", function(e) {
if (this.scrollTop > 130 ) {
wrap.addClass("scroll");
} else {
wrap.removeClass("scroll");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment