Allow hotkeys to submit

This commit is contained in:
Bryan Ashby 2015-12-23 19:08:24 -07:00
parent 30a69b2779
commit e8c8975f13
4 changed files with 35 additions and 16 deletions

View File

@ -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); HorizontalMenuView.super_.prototype.onKeyPress.call(this, ch, key);
}; };

View File

@ -44,6 +44,16 @@ function MenuView(options) {
this.hasFocusItems = function() { this.hasFocusItems = function() {
return !_.isUndefined(self.focusItems); 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); util.inherits(MenuView, View);
@ -72,6 +82,23 @@ MenuView.prototype.focusPrevious = function() {
this.emit('index update', this.focusedItemIndex); 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) { MenuView.prototype.setFocusItems = function(items) {
var self = this; var self = this;
@ -97,6 +124,7 @@ MenuView.prototype.setPropertyValue = function(propName, value) {
case 'items' : this.setItems(value); break; case 'items' : this.setItems(value); break;
case 'focusItems' : this.setFocusItems(value); break; case 'focusItems' : this.setFocusItems(value); break;
case 'hotKeys' : this.setHotKeys(value); break; case 'hotKeys' : this.setHotKeys(value); break;
case 'hotKeySubmit' : this.hotKeySubmit = value; break;
} }
MenuView.super_.prototype.setPropertyValue.call(this, propName, value); MenuView.super_.prototype.setPropertyValue.call(this, propName, value);

View File

@ -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.prototype.setFocus = function(focused) {
ToggleMenuView.super_.prototype.setFocus.call(this, 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); ToggleMenuView.super_.prototype.onKeyPress.call(this, ch, key);
}; };

View File

@ -41,6 +41,7 @@
items: [ "yes", "no" ] items: [ "yes", "no" ]
focus: true focus: true
hotKeys: { Y: 0, N: 1 } hotKeys: { Y: 0, N: 1 }
hotKeySubmit: true
} }
} }
} }