From e277287b2e588631ff6c6d70b7a73cdeacd53d31 Mon Sep 17 00:00:00 2001 From: lidlanca Date: Sat, 13 Sep 2014 23:59:20 -0400 Subject: [PATCH] Fix: rapid/continuous keyboard Next event under Topics List view may lose selected item on "load more" Using Next keyboard shortcut (J) rapidly while reaching last item in the topic list and causing a "load more". The handler might get into a state where it can not detect any selected item due to the delay from Em.run.next and will result in position reset. +added boundary check of last item on next, early in the handler to avoid unnecessary computations https://meta.discourse.org/t/keyboard-shortcut-next-previous-continues-key-down-reset-to-first-item-in-list-on-load-more/20042 --- .../javascripts/discourse/lib/keyboard_shortcuts.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/keyboard_shortcuts.js b/app/assets/javascripts/discourse/lib/keyboard_shortcuts.js index f6e76f8c5..af3a8f099 100644 --- a/app/assets/javascripts/discourse/lib/keyboard_shortcuts.js +++ b/app/assets/javascripts/discourse/lib/keyboard_shortcuts.js @@ -202,8 +202,11 @@ Discourse.KeyboardShortcuts = Ember.Object.createWithMixins({ var $selected = $articles.filter('.selected'), index = $articles.index($selected); - // loop is not allowed - if (direction === -1 && index === 0) { return; } + if($selected.length !== 0){ //boundries check + // loop is not allowed + if (direction === -1 && index === 0) { return; } + if (direction === 1 && index === ($articles.size()-1) ) { return;} + } // if nothing is selected go to the first post on screen if ($selected.length === 0) { @@ -229,10 +232,8 @@ Discourse.KeyboardShortcuts = Ember.Object.createWithMixins({ if ($article.size() > 0) { $articles.removeClass('selected'); - Em.run.next(function(){ - $article.addClass('selected'); - }); - + $article.addClass('selected'); + var rgx = new RegExp("post-cloak-(\\d+)").exec($article.parent()[0].id); if (rgx === null || typeof rgx[1] === 'undefined') { this._scrollList($article, direction);