Skip to content

Commit

Permalink
pushState should also track the highlighted item.
Browse files Browse the repository at this point in the history
  • Loading branch information
eheikes committed Aug 16, 2015
1 parent 8be2818 commit 4b5538f
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions toread.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ module.directive('list', function($location, $window, $timeout, toreadService) {
scope.limit = getParamOrDefault('limit', 20, true);
scope.tagFilter = getParamOrDefault('tag', null, true);
scope.q = getParamOrDefault('q', ''); // search query
scope.highlighted = getParamOrDefault('highlighted', null);

var random = function(lo, hi) {
return Math.floor(Math.random() * (hi - lo + 1)) + lo;
};

var pushState = function() {
var params = {
highlighted: scope.highlighted,
limit: scope.limit,
offset: scope.offset,
q: scope.q,
Expand All @@ -91,18 +93,26 @@ module.directive('list', function($location, $window, $timeout, toreadService) {
$location.search(params);
};

// Highlight the nth item in the list (starting at 0)
// and scroll the screen to its position.
var highlightOffset = function(n) {
scope.entries[n].highlighted = true;
$timeout(function() {
$('html,body').animate({
scrollTop: $('ul.entries li').eq(n).offset().top
}, 500);
});
};

scope.chooseRandom = function() {
var chosenIndex = random(0, scope.info.total - 1);
var remainder = chosenIndex % scope.limit;

scope.offset = parseInt(chosenIndex / scope.limit, 10) * scope.limit;
scope.showList().then(function() {
scope.entries[remainder].highlighted = true;
$timeout(function() {
$('html,body').animate({
scrollTop: $('ul.entries li').eq(remainder).offset().top
}, 500);
});
scope.highlighted = remainder;
pushState();
highlightOffset(remainder);
});
};

Expand Down Expand Up @@ -153,7 +163,11 @@ module.directive('list', function($location, $window, $timeout, toreadService) {

scope.$on('refreshList', scope.showList);

scope.showList();
scope.showList().then(function() {
if (scope.highlighted) {
highlightOffset(scope.highlighted);
}
});
}
}
});
Expand Down

0 comments on commit 4b5538f

Please sign in to comment.