diff options
| author | Yehonal <yehonal.azeroth@gmail.com> | 2018-12-23 11:23:16 +0100 |
|---|---|---|
| committer | Yehonal <yehonal.azeroth@gmail.com> | 2018-12-23 15:19:25 +0100 |
| commit | 5052e7090d9d4440886dd262aff5491bc5107d87 (patch) | |
| tree | 484e8b52489be3a36bbf3e44e068f8cfd16e1e3d | |
| parent | f73bd860bb566be9684876c68f6c55fd1940fddf (diff) | |
| download | wiki-5052e7090d9d4440886dd262aff5491bc5107d87.tar.gz wiki-5052e7090d9d4440886dd262aff5491bc5107d87.tar.bz2 wiki-5052e7090d9d4440886dd262aff5491bc5107d87.zip | |
non blocking functions
| -rw-r--r-- | assets/js/red-links.js | 54 | ||||
| -rw-r--r-- | assets/js/toc.js | 193 |
2 files changed, 131 insertions, 116 deletions
diff --git a/assets/js/red-links.js b/assets/js/red-links.js index de3bb25..065d3a9 100644 --- a/assets/js/red-links.js +++ b/assets/js/red-links.js @@ -1,36 +1,38 @@ - (function ($) { // // RED LINK FEATURE (Hacky) // TODO: filter external links $.fn.redLinks = function () { - $('a').each(function () { - // avoid red link for external urls - if (this.hostname != window.location.hostname) - return; + setTimeout( + function () { + $('a').each(function () { + // avoid red link for external urls + if (this.hostname != window.location.hostname) + return; - var ext = this.href.split('.').pop().split(/\#|\?/)[0]; + var ext = this.href.split('.').pop().split(/\#|\?/)[0]; - // [Performance tip] pessimistic condition based on the fact that - // markdown files are automatically converted in html - // if they are part of the wiki (the real check is right below) - if (ext.toLowerCase() == "md" || ext.toLowerCase() == "markdown") - $(this).css('color', 'red'); + // [Performance tip] pessimistic condition based on the fact that + // markdown files are automatically converted in html + // if they are part of the wiki (the real check is right below) + if (ext.toLowerCase() == "md" || ext.toLowerCase() == "markdown") + $(this).css('color', 'red'); - var that = this; - $.ajax({ - type: 'HEAD', - url: this.href, - success: function () { - $(that).css('color', ''); - }, - error: function (xhr, ajaxOptions, thrownError) { - if (xhr.status == 404) { - $(that).css('color', 'red'); - } - } - }); - }); + var that = this; + $.ajax({ + type: 'HEAD', + url: this.href, + success: function () { + $(that).css('color', ''); + }, + error: function (xhr, ajaxOptions, thrownError) { + if (xhr.status == 404) { + $(that).css('color', 'red'); + } + } + }); + }); + }, 0); }; -})(jQuery); +})(jQuery);
\ No newline at end of file diff --git a/assets/js/toc.js b/assets/js/toc.js index d8fcf33..ac46c78 100644 --- a/assets/js/toc.js +++ b/assets/js/toc.js @@ -1,106 +1,119 @@ - // https://github.com/ghiculescu/jekyll-table-of-contents (function ($) { $.fn.toc = function (options) { - var defaults = { - noBackToTopLinks: false, - title: '<i>Jump to...</i>', - minimumHeaders: 3, - headers: 'h1, h2, h3, h4, h5, h6', - listType: 'ol', // values: [ol|ul] - showEffect: 'show', // values: [show|slideDown|fadeIn|none] - showSpeed: 'slow', // set to 0 to deactivate effect - classes: { - list: '', - item: '' - } - }, - settings = $.extend(defaults, options); - - function fixedEncodeURIComponent(str) { - return encodeURIComponent(str).replace(/[!'()*]/g, function (c) { - return '%' + c.charCodeAt(0).toString(16); - }); - } - - function createLink(header) { - var innerText = (header.textContent === undefined) ? header.innerText : header.textContent; - return "<a href='#" + fixedEncodeURIComponent(header.id) + "'>" + innerText + "</a>"; - } + setTimeout(function () { + var defaults = { + noBackToTopLinks: false, + title: '<i>Jump to...</i>', + minimumHeaders: 3, + headers: 'h1, h2, h3, h4, h5, h6', + listType: 'ol', // values: [ol|ul] + showEffect: 'show', // values: [show|slideDown|fadeIn|none] + showSpeed: 'slow', // set to 0 to deactivate effect + classes: { + list: '', + item: '' + } + }, + settings = $.extend(defaults, options); - var headers = $(settings.headers).filter(function () { - // get all headers with an ID - var previousSiblingName = $(this).prev().attr("name"); - if (!this.id && previousSiblingName) { - this.id = $(this).attr("id", previousSiblingName.replace(/\./g, "-")); + function fixedEncodeURIComponent(str) { + return encodeURIComponent(str).replace(/[!'()*]/g, function (c) { + return '%' + c.charCodeAt(0).toString(16); + }); } - // Yehonal - if (!this.id) { - this.id = $(this).text().replace(/\W/g, '_'); + function createLink(header) { + var innerText = (header.textContent === undefined) ? header.innerText : header.textContent; + return "<a href='#" + fixedEncodeURIComponent(header.id) + "'>" + innerText + "</a>"; } - return this.id; - }), output = $(this); - if (!headers.length || headers.length < settings.minimumHeaders || !output.length) { - $(this).hide(); - return; - } - - if (0 === settings.showSpeed) { - settings.showEffect = 'none'; - } + var headers = $(settings.headers).filter(function () { + // get all headers with an ID + var previousSiblingName = $(this).prev().attr("name"); + if (!this.id && previousSiblingName) { + this.id = $(this).attr("id", previousSiblingName.replace(/\./g, "-")); + } - var render = { - show: function () { output.hide().html(html).show(settings.showSpeed); }, - slideDown: function () { output.hide().html(html).slideDown(settings.showSpeed); }, - fadeIn: function () { output.hide().html(html).fadeIn(settings.showSpeed); }, - none: function () { output.html(html); } - }; + // Yehonal + if (!this.id) { + this.id = $(this).text().replace(/\W/g, '_'); + } - var get_level = function (ele) { return parseInt(ele.nodeName.replace("H", ""), 10); }; - var highest_level = headers.map(function (_, ele) { return get_level(ele); }).get().sort()[0]; - var return_to_top = '<i class="icon-arrow-up back-to-top"> </i>'; + return this.id; + }), + output = $(this); + if (!headers.length || headers.length < settings.minimumHeaders || !output.length) { + $(this).hide(); + return; + } - var level = get_level(headers[0]), - this_level, - html = settings.title + " <" + settings.listType + " class=\"" + settings.classes.list + "\">"; - headers.on('click', function () { - if (!settings.noBackToTopLinks) { - window.location.hash = this.id; + if (0 === settings.showSpeed) { + settings.showEffect = 'none'; } - }) - .addClass('clickable-header') - .each(function (_, header) { - this_level = get_level(header); - if (!settings.noBackToTopLinks && this_level === highest_level) { - $(header).addClass('top-level-header').after(return_to_top); + + var render = { + show: function () { + output.hide().html(html).show(settings.showSpeed); + }, + slideDown: function () { + output.hide().html(html).slideDown(settings.showSpeed); + }, + fadeIn: function () { + output.hide().html(html).fadeIn(settings.showSpeed); + }, + none: function () { + output.html(html); } - if (this_level === level) // same level as before; same indenting - html += "<li class=\"" + settings.classes.item + "\">" + createLink(header); - else if (this_level <= level) { // higher level than before; end parent ol - for (var i = this_level; i < level; i++) { - html += "</li></" + settings.listType + ">" + }; + + var get_level = function (ele) { + return parseInt(ele.nodeName.replace("H", ""), 10); + }; + var highest_level = headers.map(function (_, ele) { + return get_level(ele); + }).get().sort()[0]; + var return_to_top = '<i class="icon-arrow-up back-to-top"> </i>'; + + var level = get_level(headers[0]), + this_level, + html = settings.title + " <" + settings.listType + " class=\"" + settings.classes.list + "\">"; + headers.on('click', function () { + if (!settings.noBackToTopLinks) { + window.location.hash = this.id; } - html += "<li class=\"" + settings.classes.item + "\">" + createLink(header); - } - else if (this_level > level) { // lower level than before; expand the previous to contain a ol - for (i = this_level; i > level; i--) { - html += "<" + settings.listType + " class=\"" + settings.classes.list + "\">" + - "<li class=\"" + settings.classes.item + "\">" + }) + .addClass('clickable-header') + .each(function (_, header) { + this_level = get_level(header); + if (!settings.noBackToTopLinks && this_level === highest_level) { + $(header).addClass('top-level-header').after(return_to_top); } - html += createLink(header); - } - level = this_level; // update for the next one - }); - html += "</" + settings.listType + ">"; - if (!settings.noBackToTopLinks) { - $(document).on('click', '.back-to-top', function () { - $(window).scrollTop(0); - window.location.hash = ''; - }); - } + if (this_level === level) // same level as before; same indenting + html += "<li class=\"" + settings.classes.item + "\">" + createLink(header); + else if (this_level <= level) { // higher level than before; end parent ol + for (var i = this_level; i < level; i++) { + html += "</li></" + settings.listType + ">" + } + html += "<li class=\"" + settings.classes.item + "\">" + createLink(header); + } else if (this_level > level) { // lower level than before; expand the previous to contain a ol + for (i = this_level; i > level; i--) { + html += "<" + settings.listType + " class=\"" + settings.classes.list + "\">" + + "<li class=\"" + settings.classes.item + "\">" + } + html += createLink(header); + } + level = this_level; // update for the next one + }); + html += "</" + settings.listType + ">"; + if (!settings.noBackToTopLinks) { + $(document).on('click', '.back-to-top', function () { + $(window).scrollTop(0); + window.location.hash = ''; + }); + } - render[settings.showEffect](); - }; -})(jQuery); + render[settings.showEffect](); + }, 0); + } +})(jQuery);
\ No newline at end of file |
