|
| 1 | +/** |
| 2 | + * @author: Kaushik Gopal |
| 3 | + * |
| 4 | + * A jQuery function that displays the footnotes |
| 5 | + * on the side (sidenotes) for easier reading. |
| 6 | + * |
| 7 | + * This is as recommended by Edward Tufte's style sidenotes: |
| 8 | + * https://edwardtufte.github.io/tufte-css/#sidenotes |
| 9 | + * |
| 10 | + **/ |
| 11 | +(function () { |
| 12 | + |
| 13 | + $(window).on("load", function() { |
| 14 | + const $footnotes = $(".footnotes"), |
| 15 | + $postContent = $(".post-content"); |
| 16 | + |
| 17 | + loadSideNotesFromFootnotes($postContent, $footnotes); |
| 18 | + |
| 19 | + $(window).resize(function() { |
| 20 | + // console.log(" XXX -- RESIZE -- XXX "); |
| 21 | + |
| 22 | + // TODO: optimization if window width doesn't change that much |
| 23 | + // const new_ww = $(".wrapper").outerWidth(); |
| 24 | + // if (new_ww === windowWidth) return; |
| 25 | + // windowWidth = new_ww; |
| 26 | + |
| 27 | + loadSideNotesFromFootnotes($postContent, $footnotes); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + function loadSideNotesFromFootnotes($postContent, $footnotes) { |
| 32 | + |
| 33 | + // remove any existing side notes to begin |
| 34 | + $(".sidenote").remove(); |
| 35 | + |
| 36 | + //region Should we even show sidenotes? |
| 37 | + |
| 38 | + // has post content |
| 39 | + if ($postContent.length < 1) { |
| 40 | + $footnotes.show(); // previous resize could have hidden footnotes |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + const startPosition = $postContent.position().left |
| 45 | + + $postContent.outerWidth() |
| 46 | + + 60; // some padding |
| 47 | + |
| 48 | + // console.log(" ---> postWidth " + $(".post").outerWidth()); |
| 49 | + // console.log(" ---> snStart " + startPosition + startPosition/3); |
| 50 | + |
| 51 | + // has room to show side content |
| 52 | + if(startPosition + startPosition / 3 > $(".post").outerWidth()) { |
| 53 | + $footnotes.show(); // previous resize could have hidden footnotes |
| 54 | + return; |
| 55 | + } |
| 56 | + //endregion |
| 57 | + |
| 58 | + const $fnItems = $footnotes.find("ol li"); |
| 59 | + |
| 60 | + $("sup").each(function(index) { |
| 61 | + const $footnoteText = $fnItems.eq(index).text().trim(); |
| 62 | + createSideNote($(this), $footnoteText, startPosition); |
| 63 | + }); |
| 64 | + |
| 65 | + $footnotes.hide(); |
| 66 | + } |
| 67 | + |
| 68 | + function createSideNote(superscript, footnoteText, startPosition) { |
| 69 | + |
| 70 | + // console.log(" ---> " + superscript.text() + " : " + footnoteText); |
| 71 | + |
| 72 | + // construct side note <div> |
| 73 | + let div = $(document.createElement('div')) |
| 74 | + .text(footnoteText) |
| 75 | + .addClass("sidenote"); |
| 76 | + |
| 77 | + const topPosition = superscript.offset(); |
| 78 | + |
| 79 | + div.css({ |
| 80 | + position: "absolute", |
| 81 | + left: startPosition, |
| 82 | + top: topPosition["top"], |
| 83 | + width: startPosition/3, |
| 84 | + }); |
| 85 | + |
| 86 | + if (startPosition > 420) { |
| 87 | + superscript.hover(function() { |
| 88 | + div.addClass("sidenote-hover"); |
| 89 | + }, function () { |
| 90 | + div.removeClass("sidenote-hover"); |
| 91 | + }); |
| 92 | + } else { |
| 93 | + div.addClass("sidenote-hover"); |
| 94 | + } |
| 95 | + |
| 96 | + // attach side note <div> |
| 97 | + $(document.body).append(div); |
| 98 | + } |
| 99 | + |
| 100 | +})(); |
0 commit comments