BUGFIX: j/k navigation was scrolling stuff in insane ways on first press

This commit is contained in:
Sam 2014-05-29 15:58:05 +10:00
parent bb887964b1
commit 2d9db9ebc9
1 changed files with 21 additions and 1 deletions

View File

@ -133,11 +133,31 @@ Discourse.KeyboardShortcuts = Ember.Object.createWithMixins({
// loop is not allowed
if (direction === -1 && index === 0) { return; }
// if nothing is selected go to the first post on screen
if ($selected.length === 0) {
var scrollTop = $('body').scrollTop();
index = 0;
$articles.each(function(){
var top = $(this).position().top;
if(top > scrollTop) {
return false;
}
index += 1;
});
if(index >= $articles.length){
index = $articles.length - 1;
}
}
var $article = $articles.eq(index + direction);
if ($article.size() > 0) {
$articles.removeClass('selected');
$article.addClass('selected');
Em.run.next(function(){
$article.addClass('selected');
});
var rgx = new RegExp("post-cloak-(\\d+)").exec($article.parent()[0].id);
if (rgx === null || typeof rgx[1] === 'undefined') {