nippy/js/page_effects.js

100 lines
3.1 KiB
JavaScript
Raw Normal View History

2013-06-01 12:16:45 +00:00
function visibleInParent(element) {
2014-07-06 06:37:03 +00:00
var position = $(element).position().top
return position > -50 && position < ($(element).offsetParent().height() - 50)
2013-06-01 12:16:45 +00:00
}
function hasFragment(link, fragment) {
2014-07-06 06:37:03 +00:00
return $(link).attr("href").indexOf("#" + fragment) != -1
2013-06-01 12:16:45 +00:00
}
function findLinkByFragment(elements, fragment) {
2014-07-06 06:37:03 +00:00
return $(elements).filter(function(i, e) { return hasFragment(e, fragment)}).first()
}
function scrollToCurrentVarLink(elements) {
var elements = $(elements);
var parent = elements.offsetParent();
if (elements.length == 0) return;
var top = elements.first().position().top;
var bottom = elements.last().position().top + elements.last().height();
if (top >= 0 && bottom <= parent.height()) return;
if (top < 0) {
parent.scrollTop(parent.scrollTop() + top);
}
else if (bottom > parent.height()) {
parent.scrollTop(parent.scrollTop() + bottom - parent.height());
}
2013-06-01 12:16:45 +00:00
}
function setCurrentVarLink() {
2014-07-06 06:37:03 +00:00
$('#vars a').parent().removeClass('current')
$('.anchor').
filter(function(index) { return visibleInParent(this) }).
each(function(index, element) {
findLinkByFragment("#vars a", element.id).
parent().
addClass('current')
});
scrollToCurrentVarLink('#vars .current');
2013-06-01 12:16:45 +00:00
}
var hasStorage = (function() { try { return localStorage.getItem } catch(e) {} }())
function scrollPositionId(element) {
2014-07-06 06:37:03 +00:00
var directory = window.location.href.replace(/[^\/]+\.html$/, '')
return 'scroll::' + $(element).attr('id') + '::' + directory
2013-06-01 12:16:45 +00:00
}
function storeScrollPosition(element) {
2014-07-06 06:37:03 +00:00
if (!hasStorage) return;
localStorage.setItem(scrollPositionId(element) + "::x", $(element).scrollLeft())
localStorage.setItem(scrollPositionId(element) + "::y", $(element).scrollTop())
2013-06-01 12:16:45 +00:00
}
function recallScrollPosition(element) {
2014-07-06 06:37:03 +00:00
if (!hasStorage) return;
$(element).scrollLeft(localStorage.getItem(scrollPositionId(element) + "::x"))
$(element).scrollTop(localStorage.getItem(scrollPositionId(element) + "::y"))
2013-06-01 12:16:45 +00:00
}
function persistScrollPosition(element) {
2014-07-06 06:37:03 +00:00
recallScrollPosition(element)
$(element).scroll(function() { storeScrollPosition(element) })
2013-06-01 12:16:45 +00:00
}
function sidebarContentWidth(element) {
2014-07-06 06:37:03 +00:00
var widths = $(element).find('.inner').map(function() { return $(this).innerWidth() })
2013-06-01 12:16:45 +00:00
return Math.max.apply(Math, widths)
}
2014-07-06 06:37:03 +00:00
function resizeSidebars() {
var nsWidth = sidebarContentWidth('#namespaces') + 30
var varWidth = 0
if ($('#vars').length != 0) {
varWidth = sidebarContentWidth('#vars') + 30
}
// snap to grid
var snap = 30;
nsWidth = Math.ceil(nsWidth / snap) * snap;
varWidth = Math.ceil(varWidth / snap) * snap;
$('#namespaces').css('width', nsWidth)
$('#vars').css('width', varWidth)
$('#vars, .namespace-index').css('left', nsWidth + 1)
$('.namespace-docs').css('left', nsWidth + varWidth + 2)
2013-06-01 12:16:45 +00:00
}
2014-07-06 06:37:03 +00:00
$(window).ready(resizeSidebars)
2013-06-01 12:16:45 +00:00
$(window).ready(setCurrentVarLink)
$(window).ready(function() { persistScrollPosition('#namespaces')})
$(window).ready(function() {
$('#content').scroll(setCurrentVarLink)
$(window).resize(setCurrentVarLink)
})