From e8c8975f13d867ec4b4dfe0c09e64385a091eb99 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Wed, 23 Dec 2015 19:08:24 -0700 Subject: [PATCH] Allow hotkeys to submit --- core/horizontal_menu_view.js | 8 -------- core/menu_view.js | 28 ++++++++++++++++++++++++++++ core/toggle_menu_view.js | 14 ++++++-------- mods/prompt.hjson | 1 + 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/core/horizontal_menu_view.js b/core/horizontal_menu_view.js index d908ea6e..87c194ee 100644 --- a/core/horizontal_menu_view.js +++ b/core/horizontal_menu_view.js @@ -140,14 +140,6 @@ HorizontalMenuView.prototype.onKeyPress = function(ch, key) { } } - if(ch && this.hotKeys) { - var keyIndex = this.hotKeys[this.caseInsensitiveHotKeys ? ch.toLowerCase() : ch]; - if(_.isNumber(keyIndex)) { - this.focusedItemIndex = keyIndex; - this.redraw(); - } - } - HorizontalMenuView.super_.prototype.onKeyPress.call(this, ch, key); }; diff --git a/core/menu_view.js b/core/menu_view.js index e9fa468c..4ad4c977 100644 --- a/core/menu_view.js +++ b/core/menu_view.js @@ -44,6 +44,16 @@ function MenuView(options) { this.hasFocusItems = function() { return !_.isUndefined(self.focusItems); }; + + this.getHotKeyItemIndex = function(ch) { + if(ch && self.hotKeys) { + var keyIndex = self.hotKeys[self.caseInsensitiveHotKeys ? ch.toLowerCase() : ch]; + if(_.isNumber(keyIndex)) { + return keyIndex; + } + } + return -1; + }; } util.inherits(MenuView, View); @@ -72,6 +82,23 @@ MenuView.prototype.focusPrevious = function() { this.emit('index update', this.focusedItemIndex); }; +MenuView.prototype.setFocusItemIndex = function(index) { + this.focusedItemIndex = index; +}; + +MenuView.prototype.onKeyPress = function(ch, key) { + var itemIndex = this.getHotKeyItemIndex(ch); + if(itemIndex >= 0) { + this.setFocusItemIndex(itemIndex); + + if(true === this.hotKeySubmit) { + this.emit('action', 'accept'); + } + } + + MenuView.super_.prototype.onKeyPress.call(this, ch, key); +}; + MenuView.prototype.setFocusItems = function(items) { var self = this; @@ -97,6 +124,7 @@ MenuView.prototype.setPropertyValue = function(propName, value) { case 'items' : this.setItems(value); break; case 'focusItems' : this.setFocusItems(value); break; case 'hotKeys' : this.setHotKeys(value); break; + case 'hotKeySubmit' : this.hotKeySubmit = value; break; } MenuView.super_.prototype.setPropertyValue.call(this, propName, value); diff --git a/core/toggle_menu_view.js b/core/toggle_menu_view.js index f9ad4f82..a740782c 100644 --- a/core/toggle_menu_view.js +++ b/core/toggle_menu_view.js @@ -62,6 +62,12 @@ ToggleMenuView.prototype.redraw = function() { } }; +ToggleMenuView.prototype.setFocusItemIndex = function(index) { + ToggleMenuView.super_.prototype.setFocusItemIndex.call(this, index); // sets this.focusedItemIndex + + this.updateSelection(); +}; + ToggleMenuView.prototype.setFocus = function(focused) { ToggleMenuView.super_.prototype.setFocus.call(this, focused); @@ -93,14 +99,6 @@ ToggleMenuView.prototype.onKeyPress = function(ch, key) { } } - if(ch && this.hotKeys) { - var keyIndex = this.hotKeys[this.caseInsensitiveHotKeys ? ch.toLowerCase() : ch]; - if(_.isNumber(keyIndex)) { - this.focusedItemIndex = keyIndex; - this.updateSelection(); - } - } - ToggleMenuView.super_.prototype.onKeyPress.call(this, ch, key); }; diff --git a/mods/prompt.hjson b/mods/prompt.hjson index 8e0b3ebc..b1168bc4 100644 --- a/mods/prompt.hjson +++ b/mods/prompt.hjson @@ -41,6 +41,7 @@ items: [ "yes", "no" ] focus: true hotKeys: { Y: 0, N: 1 } + hotKeySubmit: true } } }