From a4226f14137f7c319352059be164087a224d7ebe Mon Sep 17 00:00:00 2001 From: curben Date: Thu, 4 Oct 2018 12:21:36 +0930 Subject: [PATCH] Combine sri-fallback and typing Save one HTTP request --- .../typing/layout/_partial/after-footer.ejs | 3 +- themes/typing/source/js/main.js | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 themes/typing/source/js/main.js diff --git a/themes/typing/layout/_partial/after-footer.ejs b/themes/typing/layout/_partial/after-footer.ejs index d14ec2c..1502f88 100644 --- a/themes/typing/layout/_partial/after-footer.ejs +++ b/themes/typing/layout/_partial/after-footer.ejs @@ -1,4 +1,4 @@ -<%- js('js/sri-fallback.min') %> +<%- js('js/main') %> @@ -17,4 +17,3 @@ <% } %> -<%- js('js/typing') %> diff --git a/themes/typing/source/js/main.js b/themes/typing/source/js/main.js new file mode 100644 index 0000000..8dbe2c0 --- /dev/null +++ b/themes/typing/source/js/main.js @@ -0,0 +1,81 @@ +/* This is concat of sri-fallback.min.js and typing.js +* Any changes to typing.js needs to be reflected here (just concat again) +*/ +!function(){var a="x-sri-fallback";function o(e){var r=e.getAttribute&&e.getAttribute(a),i=(e.tagName||"").toLowerCase(),n="script"===i&&e.src?"src":"link"===i&&"stylesheet"===e.rel&&e.href?"href":null;r&&n&&(e.onerror=function(){var t=document.createElement(i);t.crossOrigin=e.crossOrigin,t.integrity=e.integrity,t[n]=r,"link"===i&&(t.rel=e.rel),t.onerror=function(){console.log("SRI fallback "+r+" also failed to match integrity string "+t.integrity+".")},document.head.appendChild(t)})}new MutationObserver(function(t){for(var e=0;e' + alt + '') + } + + $(this).wrap('') + }) + + $(this).find('.fancybox').each(function () { + $(this).attr('rel', 'article' + i) + }) + }) + + if ($.fancybox) { + $('.fancybox').fancybox() + } + + + //Add "Copy" button to code snippet + var code = document.getElementsByClassName('code'); + + for (var i = 0; i < code.length; i++) { + var button = document.createElement('button'); + button.className = 'copy-button'; + button.textContent = 'Copy'; + + code[i].appendChild(button); + } + + $(document).ready(function() { + + // Add copy to clipboard button for code snippet + var copyCode = new ClipboardJS('.copy-button', { + target: function(trigger) { + return trigger.previousElementSibling; + } + }); + + copyCode.on('success', function(event) { + event.clearSelection(); + event.trigger.textContent = 'Copied'; + window.setTimeout(function() { + event.trigger.textContent = 'Copy'; + }, 2000); + }); + + copyCode.on('error', function(event) { + event.trigger.textContent = 'Press "Ctrl + C" to copy'; + window.setTimeout(function() { + event.trigger.textContent = 'Copy'; + }, 2000); + }); + + // Navigation menu + $('#menu').click(function (event) { + var nav = $('#main-nav'); + nav.toggle('fast'); + }); + + // Show navigation button for smaller screen + $(window).resize(function () { + var viewportWidth = $(window).width(); + if (viewportWidth > 468) { + $('#main-nav').show('fast'); + } else { + $('#main-nav').hide('fast'); + } + }); + }); +})(jQuery)