timbre/js/page_effects.js

113 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

2013-06-01 19:42:03 +07:00
function visibleInParent(element) {
2014-09-02 17:58:21 +07:00
var position = $(element).position().top
return position > -50 && position < ($(element).offsetParent().height() - 50)
2013-06-01 19:42:03 +07:00
}
function hasFragment(link, fragment) {
2014-09-02 17:58:21 +07:00
return $(link).attr("href").indexOf("#" + fragment) != -1
2013-06-01 19:42:03 +07:00
}
function findLinkByFragment(elements, fragment) {
2014-09-02 17:58:21 +07: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 19:42:03 +07:00
}
function setCurrentVarLink() {
2016-01-14 12:26:16 +07:00
$('.secondary a').parent().removeClass('current')
2014-09-02 17:58:21 +07:00
$('.anchor').
filter(function(index) { return visibleInParent(this) }).
each(function(index, element) {
2016-01-14 12:26:16 +07:00
findLinkByFragment(".secondary a", element.id).
2014-09-02 17:58:21 +07:00
parent().
addClass('current')
});
2016-01-14 12:26:16 +07:00
scrollToCurrentVarLink('.secondary .current');
2013-06-01 19:42:03 +07:00
}
var hasStorage = (function() { try { return localStorage.getItem } catch(e) {} }())
function scrollPositionId(element) {
2014-09-02 17:58:21 +07:00
var directory = window.location.href.replace(/[^\/]+\.html$/, '')
return 'scroll::' + $(element).attr('id') + '::' + directory
2013-06-01 19:42:03 +07:00
}
function storeScrollPosition(element) {
2014-09-02 17:58:21 +07:00
if (!hasStorage) return;
localStorage.setItem(scrollPositionId(element) + "::x", $(element).scrollLeft())
localStorage.setItem(scrollPositionId(element) + "::y", $(element).scrollTop())
2013-06-01 19:42:03 +07:00
}
function recallScrollPosition(element) {
2014-09-02 17:58:21 +07:00
if (!hasStorage) return;
$(element).scrollLeft(localStorage.getItem(scrollPositionId(element) + "::x"))
$(element).scrollTop(localStorage.getItem(scrollPositionId(element) + "::y"))
2013-06-01 19:42:03 +07:00
}
function persistScrollPosition(element) {
2014-09-02 17:58:21 +07:00
recallScrollPosition(element)
$(element).scroll(function() { storeScrollPosition(element) })
2013-06-01 19:42:03 +07:00
}
function sidebarContentWidth(element) {
2014-09-02 17:58:21 +07:00
var widths = $(element).find('.inner').map(function() { return $(this).innerWidth() })
2013-06-01 19:42:03 +07:00
return Math.max.apply(Math, widths)
}
2016-02-22 14:51:33 +07:00
function calculateSize(width, snap, margin, minimum) {
if (width == 0) {
return 0
}
else {
return Math.max(minimum, (Math.ceil(width / snap) * snap) + (margin * 2))
}
}
2014-09-02 17:58:21 +07:00
function resizeSidebars() {
2016-02-22 14:51:33 +07:00
var primaryWidth = sidebarContentWidth('.primary')
2016-01-14 12:26:16 +07:00
var secondaryWidth = 0
2014-09-02 17:58:21 +07:00
2016-01-14 12:26:16 +07:00
if ($('.secondary').length != 0) {
2016-02-22 14:51:33 +07:00
secondaryWidth = sidebarContentWidth('.secondary')
2014-09-02 17:58:21 +07:00
}
// snap to grid
2016-02-22 14:51:33 +07:00
primaryWidth = calculateSize(primaryWidth, 32, 13, 160)
secondaryWidth = calculateSize(secondaryWidth, 32, 13, 160)
2016-01-14 12:26:16 +07:00
$('.primary').css('width', primaryWidth)
$('.secondary').css('width', secondaryWidth).css('left', primaryWidth + 1)
if (secondaryWidth > 0) {
$('#content').css('left', primaryWidth + secondaryWidth + 2)
}
else {
$('#content').css('left', primaryWidth + 1)
}
2013-06-01 19:42:03 +07:00
}
2014-09-02 17:58:21 +07:00
$(window).ready(resizeSidebars)
2013-06-01 19:42:03 +07:00
$(window).ready(setCurrentVarLink)
2016-01-14 12:26:16 +07:00
$(window).ready(function() { persistScrollPosition('.primary')})
2013-06-01 19:42:03 +07:00
$(window).ready(function() {
$('#content').scroll(setCurrentVarLink)
$(window).resize(setCurrentVarLink)
})