-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigation.js
96 lines (73 loc) · 2.77 KB
/
navigation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function bindNavigationKeys() {
$(document).bind('keypress', 'j', handleNavForward);
$(document).bind('keypress', 'k', handleNavBackward);
$(document).bind('keypress', 'n', handleNavNextPage);
}
function handleNavForward(e) {
console.log("nav ffwd");
var target = getFirstNonVisibleItemAfterAnyVisible();
var itemList = store.getItemList();
var firstItem = itemList.first();
var firstItemOffset = firstItem.offset();
var currentOffset = $(window).scrollTop();
if (firstItem.length > 0 && currentOffset < firstItemOffset.top)
target = firstItem;
console.log("offsets", currentOffset, firstItemOffset);
if (target.length === 0) {
console.log("nav ffw target links", target);
target = store.getMarkReadLinks();
}
$(window).scrollTo(target, 300);
//do stuff with "key" here...
}
function handleNavBackward(e) {
console.log("nav bkwd");
var viewportHeight = $(window).height();
var target = getLastNonVisibleItemBeforeAnyVisible();
$(window).scrollTo(target, 0);
$(window).scrollTo("-=" + viewportHeight + "px", 300);
}
function handleNavNextPage() {
var nextPageLink = store.getNextPageLink();
console.log("nextPageLink", nextPageLink);
var href = nextPageLink.attr("href");
nextPageLink.trigger("click");
if (href != null && !href.blank() && !href.startsWith("#"))
location.href = href;
}
function getFirstNonVisibleItemAfterAnyVisible() {
var itemList = store.getItemList();
debugDir("itemList", itemList);
var foundOneVisible = false;
var returnItem = $([]);
_.some(itemList, function(thisItem) {
var thisItemInViewPort = isElementInViewport(thisItem);
console.log(thisItemInViewPort, thisItem)
if (thisItemInViewPort && !foundOneVisible)
foundOneVisible = true;
if (!thisItemInViewPort && foundOneVisible) {
returnItem = $(thisItem);
debugDir("firstNonVisibleItemAfterAnyVisible", returnItem);
return true;
}
return false;
});
return returnItem;
}
function getLastNonVisibleItemBeforeAnyVisible() {
var itemList = _.reverse(store.getItemList());
var foundOneVisible = false;
var returnItem = $([]);
_.some(itemList, function(thisItem) {
var thisItemInViewPort = isElementInViewport(thisItem);
if (thisItemInViewPort && !foundOneVisible)
foundOneVisible = true;
if (!thisItemInViewPort && foundOneVisible) {
returnItem = $(thisItem);
debugDir("lastNonVisibleItemBeforeAnyVisible", returnItem);
return true;
}
return false;
});
return returnItem;
}