From 973e10fb8b20e5e6c37ee1123e6cbc241d3e3a17 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sat, 9 Jun 2018 22:45:01 -0600 Subject: [PATCH] HOME/END key support in lists --- core/menu_view.js | 20 ++++++++++++++++---- core/vertical_menu_view.js | 17 +++++++++++++++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/core/menu_view.js b/core/menu_view.js index dc2f5c81..598e04cd 100644 --- a/core/menu_view.js +++ b/core/menu_view.js @@ -60,6 +60,10 @@ function MenuView(options) { } return -1; }; + + this.emitIndexUpdate = function() { + self.emit('index update', self.focusedItemIndex); + } } util.inherits(MenuView, View); @@ -174,19 +178,27 @@ MenuView.prototype.getItem = function(index) { }; MenuView.prototype.focusNext = function() { - this.emit('index update', this.focusedItemIndex); + this.emitIndexUpdate(); }; MenuView.prototype.focusPrevious = function() { - this.emit('index update', this.focusedItemIndex); + this.emitIndexUpdate(); }; MenuView.prototype.focusNextPageItem = function() { - this.emit('index update', this.focusedItemIndex); + this.emitIndexUpdate(); }; MenuView.prototype.focusPreviousPageItem = function() { - this.emit('index update', this.focusedItemIndex); + this.emitIndexUpdate(); +}; + +MenuView.prototype.focusFirst = function() { + this.emitIndexUpdate(); +}; + +MenuView.prototype.focusLast = function() { + this.emitIndexUpdate(); }; MenuView.prototype.setFocusItemIndex = function(index) { diff --git a/core/vertical_menu_view.js b/core/vertical_menu_view.js index 57570e16..fe5c1203 100644 --- a/core/vertical_menu_view.js +++ b/core/vertical_menu_view.js @@ -165,7 +165,6 @@ VerticalMenuView.prototype.setFocusItemIndex = function(index) { }; VerticalMenuView.prototype.onKeyPress = function(ch, key) { - if(key) { if(this.isKeyMapped('up', key.name)) { this.focusPrevious(); @@ -173,8 +172,12 @@ VerticalMenuView.prototype.onKeyPress = function(ch, key) { this.focusNext(); } else if(this.isKeyMapped('page up', key.name)) { this.focusPreviousPageItem(); - } else if( this.isKeyMapped('page down', key.name)) { + } else if(this.isKeyMapped('page down', key.name)) { this.focusNextPageItem(); + } else if(this.isKeyMapped('home', key.name)) { + this.focusFirst(); + } else if(this.isKeyMapped('end', key.name)) { + this.focusLast(); } } @@ -308,6 +311,16 @@ VerticalMenuView.prototype.focusNextPageItem = function() { return VerticalMenuView.super_.prototype.focusNextPageItem.call(this); }; +VerticalMenuView.prototype.focusFirst = function() { + this.setFocusItemIndex(0); + return VerticalMenuView.super_.prototype.focusFirst.call(this); +}; + +VerticalMenuView.prototype.focusLast = function() { + this.setFocusItemIndex(this.items.length - 1); + return VerticalMenuView.super_.prototype.focusLast.call(this); +}; + VerticalMenuView.prototype.setFocusItems = function(items) { VerticalMenuView.super_.prototype.setFocusItems.call(this, items);