|
| 1 | +function visibleInParent(element) { |
| 2 | + var position = $(element).position().top |
| 3 | + return position > -50 && position < ($(element).offsetParent().height() - 50) |
| 4 | +} |
| 5 | + |
| 6 | +function hasFragment(link, fragment) { |
| 7 | + return $(link).attr("href").indexOf("#" + fragment) != -1 |
| 8 | +} |
| 9 | + |
| 10 | +function findLinkByFragment(elements, fragment) { |
| 11 | + return $(elements).filter(function(i, e) { return hasFragment(e, fragment)}).first() |
| 12 | +} |
| 13 | + |
| 14 | +function scrollToCurrentVarLink(elements) { |
| 15 | + var elements = $(elements); |
| 16 | + var parent = elements.offsetParent(); |
| 17 | + |
| 18 | + if (elements.length == 0) return; |
| 19 | + |
| 20 | + var top = elements.first().position().top; |
| 21 | + var bottom = elements.last().position().top + elements.last().height(); |
| 22 | + |
| 23 | + if (top >= 0 && bottom <= parent.height()) return; |
| 24 | + |
| 25 | + if (top < 0) { |
| 26 | + parent.scrollTop(parent.scrollTop() + top); |
| 27 | + } |
| 28 | + else if (bottom > parent.height()) { |
| 29 | + parent.scrollTop(parent.scrollTop() + bottom - parent.height()); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function setCurrentVarLink() { |
| 34 | + $('.secondary a').parent().removeClass('current') |
| 35 | + $('.anchor'). |
| 36 | + filter(function(index) { return visibleInParent(this) }). |
| 37 | + each(function(index, element) { |
| 38 | + findLinkByFragment(".secondary a", element.id). |
| 39 | + parent(). |
| 40 | + addClass('current') |
| 41 | + }); |
| 42 | + scrollToCurrentVarLink('.secondary .current'); |
| 43 | +} |
| 44 | + |
| 45 | +var hasStorage = (function() { try { return localStorage.getItem } catch(e) {} }()) |
| 46 | + |
| 47 | +function scrollPositionId(element) { |
| 48 | + var directory = window.location.href.replace(/[^\/]+\.html$/, '') |
| 49 | + return 'scroll::' + $(element).attr('id') + '::' + directory |
| 50 | +} |
| 51 | + |
| 52 | +function storeScrollPosition(element) { |
| 53 | + if (!hasStorage) return; |
| 54 | + localStorage.setItem(scrollPositionId(element) + "::x", $(element).scrollLeft()) |
| 55 | + localStorage.setItem(scrollPositionId(element) + "::y", $(element).scrollTop()) |
| 56 | +} |
| 57 | + |
| 58 | +function recallScrollPosition(element) { |
| 59 | + if (!hasStorage) return; |
| 60 | + $(element).scrollLeft(localStorage.getItem(scrollPositionId(element) + "::x")) |
| 61 | + $(element).scrollTop(localStorage.getItem(scrollPositionId(element) + "::y")) |
| 62 | +} |
| 63 | + |
| 64 | +function persistScrollPosition(element) { |
| 65 | + recallScrollPosition(element) |
| 66 | + $(element).scroll(function() { storeScrollPosition(element) }) |
| 67 | +} |
| 68 | + |
| 69 | +function sidebarContentWidth(element) { |
| 70 | + var widths = $(element).find('.inner').map(function() { return $(this).innerWidth() }) |
| 71 | + return Math.max.apply(Math, widths) |
| 72 | +} |
| 73 | + |
| 74 | +function calculateSize(width, snap, margin, minimum) { |
| 75 | + if (width == 0) { |
| 76 | + return 0 |
| 77 | + } |
| 78 | + else { |
| 79 | + return Math.max(minimum, (Math.ceil(width / snap) * snap) + (margin * 2)) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function resizeSidebars() { |
| 84 | + var primaryWidth = sidebarContentWidth('.primary') |
| 85 | + var secondaryWidth = 0 |
| 86 | + |
| 87 | + if ($('.secondary').length != 0) { |
| 88 | + secondaryWidth = sidebarContentWidth('.secondary') |
| 89 | + } |
| 90 | + |
| 91 | + // snap to grid |
| 92 | + primaryWidth = calculateSize(primaryWidth, 32, 13, 160) |
| 93 | + secondaryWidth = calculateSize(secondaryWidth, 32, 13, 160) |
| 94 | + |
| 95 | + $('.primary').css('width', primaryWidth) |
| 96 | + $('.secondary').css('width', secondaryWidth).css('left', primaryWidth + 1) |
| 97 | + |
| 98 | + if (secondaryWidth > 0) { |
| 99 | + $('#content').css('left', primaryWidth + secondaryWidth + 2) |
| 100 | + } |
| 101 | + else { |
| 102 | + $('#content').css('left', primaryWidth + 1) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +$(window).ready(resizeSidebars) |
| 107 | +$(window).ready(setCurrentVarLink) |
| 108 | +$(window).ready(function() { persistScrollPosition('.primary')}) |
| 109 | +$(window).ready(function() { |
| 110 | + $('#content').scroll(setCurrentVarLink) |
| 111 | + $(window).resize(setCurrentVarLink) |
| 112 | +}) |
0 commit comments