From 37226d4b667e1c33d71960288152790c55159027 Mon Sep 17 00:00:00 2001 From: Gautham Warrier Date: Thu, 20 Nov 2014 14:25:11 +0530 Subject: [PATCH] Optimize Code Block View toggling. Also auto-hide Code section for pages without code --- js/custom.js | 77 +++++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/js/custom.js b/js/custom.js index 23578e2..4eff71f 100644 --- a/js/custom.js +++ b/js/custom.js @@ -5,15 +5,11 @@ $(function () { $(this).next().slideToggle(); }); - // Bootstrap Table Class $('table').addClass('table'); - - // Responsive menu spinner $('#menu-spinner-button').click(function () { $('#sub-nav-collapse').slideToggle(); }); - // Catch browser resize $(window).resize(function () { // Remove transition inline style on large screens if ($(window).width() >= 768) @@ -26,40 +22,47 @@ var t = $('#github-ribbon'); var a = $('article'); if (t[0] && a[0] && a[0].scrollHeight > $('.right-column').height()) t[0].style.right = '16px'; -//Toggle Code Block Visibility -function toggleCodeBlocks() { - var t = localStorage.getItem("toggleCodeStats") - t = (t + 1) % 3; - localStorage.setItem("toggleCodeStats", t); - var a = $('.content-page article'); - var c = a.children().filter('pre'); - var d = $('.right-column'); - if (d.hasClass('float-view')) { - d.removeClass('float-view'); - $('#toggleCodeBlockBtn')[0].innerHTML = "Hide Code Blocks"; - } else { - if (c.hasClass('hidden')) { - d.addClass('float-view'); - c.removeClass('hidden'); - $('#toggleCodeBlockBtn')[0].innerHTML = "Show Code Blocks Inline"; - } else { - c.addClass('hidden'); - $('#toggleCodeBlockBtn')[0].innerHTML = "Show Code Blocks"; - } +function setCodeBlockStyle(x) { + switch (x) { + default: + case 0: + toggleCodeBlockBtn.innerHTML = "Show Code Blocks Inline"; + codeBlockView.addClass('float-view'); + codeBlocks.removeClass('hidden'); + break; + case 1: + toggleCodeBlockBtn.innerHTML = "Hide Code Blocks"; + codeBlockView.removeClass('float-view'); + codeBlocks.removeClass('hidden'); + break; + case 2: + toggleCodeBlockBtn.innerHTML = "Show Code Blocks"; + codeBlockView.removeClass('float-view'); + codeBlocks.addClass('hidden'); + break; } } -if (localStorage.getItem("toggleCodeStats") >= 0) { - var t = localStorage.getItem("toggleCodeStats"); - if (t == 1) { - toggleCodeBlocks(); - localStorage.setItem("toggleCodeStats", 1); - } - if (t == 2) { - toggleCodeBlocks(); - toggleCodeBlocks(); - localStorage.setItem("toggleCodeStats", 2); - } -} else { - localStorage.setItem("toggleCodeStats", 0); +function toggleCodeBlocks() { + codeBlockState = (codeBlockState + 1) % 3; + localStorage.setItem("codeBlockState", codeBlockState); + setCodeBlockStyle(codeBlockState); } + +//Initialize CodeBlock Visibility Settings +$(function () { + toggleCodeBlockBtn = $('#toggleCodeBlockBtn')[0]; + codeBlockView = $('.right-column'); + codeBlocks = $('.content-page article > pre'); + codeBlockState = localStorage.getItem("codeBlockState"); + if (!codeBlockState) { + codeBlockState = 0; + localStorage.setItem("codeBlockState", codeBlockState); + } else codeBlockState = parseInt(codeBlockState); + if (!codeBlockView.size()) return; + if (!codeBlocks.size()) { + codeBlockState = 2; + toggleCodeBlockBtn.classList.add('hidden'); + } + setCodeBlockStyle(codeBlockState); +}); \ No newline at end of file