* isSpecialKeyMapped() renamed to isKeyMapped()

* Functional HorizontalMenuView
* Minor updates
This commit is contained in:
Bryan Ashby 2015-07-01 20:18:34 -06:00
parent a1b87c7a60
commit c1469a1f9c
11 changed files with 81 additions and 23 deletions

View File

@ -32,7 +32,7 @@ require('util').inherits(EditTextView, TextView);
EditTextView.prototype.onKeyPress = function(ch, key) { EditTextView.prototype.onKeyPress = function(ch, key) {
if(key) { if(key) {
if(this.isSpecialKeyMapped('backspace', key.name)) { if(this.isKeyMapped('backspace', key.name)) {
if(this.text.length > 0) { if(this.text.length > 0) {
this.text = this.text.substr(0, this.text.length - 1); this.text = this.text.substr(0, this.text.length - 1);
@ -47,7 +47,7 @@ EditTextView.prototype.onKeyPress = function(ch, key) {
} }
return; return;
} else if(this.isSpecialKeyMapped('clearLine', key.name)) { } else if(this.isKeyMapped('clearLine', key.name)) {
this.text = ''; this.text = '';
this.cursorPos.col = 0; this.cursorPos.col = 0;
this.setFocus(true); // resetting focus will redraw & adjust cursor this.setFocus(true); // resetting focus will redraw & adjust cursor

View File

@ -10,6 +10,8 @@ var _ = require('lodash');
exports.HorizontalMenuView = HorizontalMenuView; exports.HorizontalMenuView = HorizontalMenuView;
// :TODO: Update this to allow scrolling if number of items cannot fit in width (similar to VerticalMenuView)
function HorizontalMenuView(options) { function HorizontalMenuView(options) {
options.cursor = options.cursor || 'hide'; options.cursor = options.cursor || 'hide';
@ -107,4 +109,46 @@ HorizontalMenuView.prototype.setItems = function(items) {
HorizontalMenuView.super_.prototype.setItems.call(this, items); HorizontalMenuView.super_.prototype.setItems.call(this, items);
this.positionCacheExpired = true; this.positionCacheExpired = true;
};
HorizontalMenuView.prototype.onKeyPress = function(ch, key) {
if(key) {
var prevFocusedItemIndex = this.focusedItemIndex;
if(this.isKeyMapped('left', key.name)) {
if(0 === this.focusedItemIndex) {
this.focusedItemIndex = this.items.length - 1;
} else {
this.focusedItemIndex--;
}
} else if(this.isKeyMapped('right', key.name)) {
if(this.items.length - 1 === this.focusedItemIndex) {
this.focusedItemIndex = 0;
} else {
this.focusedItemIndex++;
}
}
if(prevFocusedItemIndex !== this.focusedItemIndex) {
// :TODO: Optimize this in cases where we only need to redraw two items. Always the case now, somtimes
// if this is changed to allow scrolling
this.redraw();
return;
}
}
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.prototype.getData = function() {
return this.focusedItemIndex;
}; };

View File

@ -106,7 +106,7 @@ MaskEditTextView.prototype.setMaskPattern = function(pattern) {
MaskEditTextView.prototype.onKeyPress = function(ch, key) { MaskEditTextView.prototype.onKeyPress = function(ch, key) {
if(key) { if(key) {
if(this.isSpecialKeyMapped('backspace', key.name)) { if(this.isKeyMapped('backspace', key.name)) {
if(this.text.length > 0) { if(this.text.length > 0) {
this.patternArrayPos--; this.patternArrayPos--;
assert(this.patternArrayPos >= 0); assert(this.patternArrayPos >= 0);
@ -128,7 +128,7 @@ MaskEditTextView.prototype.onKeyPress = function(ch, key) {
} }
return; return;
} else if(this.isSpecialKeyMapped('clearLine', key.name)) { } else if(this.isKeyMapped('clearLine', key.name)) {
this.text = ''; this.text = '';
this.patternArrayPos = 0; this.patternArrayPos = 0;
this.setFocus(true); // redraw + adjust cursor this.setFocus(true); // redraw + adjust cursor

View File

@ -499,13 +499,13 @@ MultiLineEditTextView.prototype.onKeyPress = function(key, isSpecial) {
}; };
MultiLineEditTextView.prototype.onSpecialKeyPress = function(keyName) { MultiLineEditTextView.prototype.onSpecialKeyPress = function(keyName) {
if(this.isSpecialKeyMapped('up', keyName)) { if(this.isKeyMapped('up', keyName)) {
this.cursorUp(); this.cursorUp();
} else if(this.isSpecialKeyMapped('down', keyName)) { } else if(this.isKeyMapped('down', keyName)) {
this.cursorDown(); this.cursorDown();
} else if(this.isSpecialKeyMapped('left', keyName)) { } else if(this.isKeyMapped('left', keyName)) {
this.cursorLeft(); this.cursorLeft();
} else if(this.isSpecialKeyMapped('right', keyName)) { } else if(this.isKeyMapped('right', keyName)) {
this.cursorRight(); this.cursorRight();
} }

View File

@ -1036,7 +1036,7 @@ MultiLineEditTextView2.prototype.onKeyPress = function(ch, key) {
if(key) { if(key) {
HANDLED_SPECIAL_KEYS.forEach(function aKey(specialKey) { HANDLED_SPECIAL_KEYS.forEach(function aKey(specialKey) {
if(self.isSpecialKeyMapped(specialKey, key.name)) { if(self.isKeyMapped(specialKey, key.name)) {
self[_.camelCase('keyPress ' + specialKey)](); self[_.camelCase('keyPress ' + specialKey)]();
handled = true; handled = true;
} }

View File

@ -26,7 +26,7 @@ function SpinnerMenuView(options) {
*/ */
this.updateSelection = function() { this.updateSelection = function() {
assert(!self.positionCacheExpired); //assert(!self.positionCacheExpired);
assert(this.focusedItemIndex >= 0 && this.focusedItemIndex <= self.items.length); assert(this.focusedItemIndex >= 0 && this.focusedItemIndex <= self.items.length);
@ -66,7 +66,7 @@ SpinnerMenuView.prototype.setFocus = function(focused) {
SpinnerMenuView.prototype.onKeyPress = function(ch, key) { SpinnerMenuView.prototype.onKeyPress = function(ch, key) {
if(key) { if(key) {
if(this.isSpecialKeyMapped('up', key.name)) { if(this.isKeyMapped('up', key.name)) {
if(0 === this.focusedItemIndex) { if(0 === this.focusedItemIndex) {
this.focusedItemIndex = this.items.length - 1; this.focusedItemIndex = this.items.length - 1;
} else { } else {
@ -75,7 +75,7 @@ SpinnerMenuView.prototype.onKeyPress = function(ch, key) {
this.updateSelection(); this.updateSelection();
return; return;
} else if(this.isSpecialKeyMapped('down', key.name)) { } else if(this.isKeyMapped('down', key.name)) {
if(this.items.length - 1 === this.focusedItemIndex) { if(this.items.length - 1 === this.focusedItemIndex) {
this.focusedItemIndex = 0; this.focusedItemIndex = 0;
} else { } else {

View File

@ -25,7 +25,7 @@ function ToggleMenuView (options) {
*/ */
this.updateSelection = function() { this.updateSelection = function() {
assert(!self.positionCacheExpired); //assert(!self.positionCacheExpired);
assert(this.focusedItemIndex >= 0 && this.focusedItemIndex <= self.items.length); assert(this.focusedItemIndex >= 0 && this.focusedItemIndex <= self.items.length);
self.redraw(); self.redraw();
@ -71,14 +71,14 @@ ToggleMenuView.prototype.setFocus = function(focused) {
ToggleMenuView.prototype.onKeyPress = function(ch, key) { ToggleMenuView.prototype.onKeyPress = function(ch, key) {
if(key) { if(key) {
var needsUpdate; var needsUpdate;
if(this.isSpecialKeyMapped('right', key.name) || this.isSpecialKeyMapped('down', key.name)) { if(this.isKeyMapped('right', key.name) || this.isKeyMapped('down', key.name)) {
if(this.items.length - 1 === this.focusedItemIndex) { if(this.items.length - 1 === this.focusedItemIndex) {
this.focusedItemIndex = 0; this.focusedItemIndex = 0;
} else { } else {
this.focusedItemIndex++; this.focusedItemIndex++;
} }
needsUpdate = true; needsUpdate = true;
} else if(this.isSpecialKeyMapped('left', key.name) || this.isSpecialKeyMapped('up', key.name)) { } else if(this.isKeyMapped('left', key.name) || this.isKeyMapped('up', key.name)) {
if(0 === this.focusedItemIndex) { if(0 === this.focusedItemIndex) {
this.focusedItemIndex = this.items.length - 1; this.focusedItemIndex = this.items.length - 1;
} else { } else {
@ -95,7 +95,7 @@ ToggleMenuView.prototype.onKeyPress = function(ch, key) {
if(ch && this.hotKeys) { if(ch && this.hotKeys) {
var keyIndex = this.hotKeys[this.caseInsensitiveHotKeys ? ch.toLowerCase() : ch]; var keyIndex = this.hotKeys[this.caseInsensitiveHotKeys ? ch.toLowerCase() : ch];
if(!_.isUndefined(keyIndex)) { if(_.isNumber(keyIndex)) {
this.focusedItemIndex = keyIndex; this.focusedItemIndex = keyIndex;
this.updateSelection(); this.updateSelection();
} }

View File

@ -109,7 +109,7 @@ VerticalMenuView.prototype.onKeyPress = function(ch, key) {
if(key) { if(key) {
var prevFocusedItemIndex = this.focusedItemIndex; var prevFocusedItemIndex = this.focusedItemIndex;
if(this.isSpecialKeyMapped('up', key.name)) { if(this.isKeyMapped('up', key.name)) {
if(0 === this.focusedItemIndex) { if(0 === this.focusedItemIndex) {
this.focusedItemIndex = this.items.length - 1; this.focusedItemIndex = this.items.length - 1;
@ -125,7 +125,7 @@ VerticalMenuView.prototype.onKeyPress = function(ch, key) {
this.viewWindow.bottom--; this.viewWindow.bottom--;
} }
} }
} else if(this.isSpecialKeyMapped('down', key.name)) { } else if(this.isKeyMapped('down', key.name)) {
if(this.items.length - 1 === this.focusedItemIndex) { if(this.items.length - 1 === this.focusedItemIndex) {
this.focusedItemIndex = 0; this.focusedItemIndex = 0;
@ -144,6 +144,7 @@ VerticalMenuView.prototype.onKeyPress = function(ch, key) {
} }
if(prevFocusedItemIndex !== this.focusedItemIndex) { if(prevFocusedItemIndex !== this.focusedItemIndex) {
// :TODO: Optimize this for cases where no scrolling occured & only two items need updated
this.redraw(); this.redraw();
} }
} }

View File

@ -80,7 +80,7 @@ function View(options) {
this.specialKeyMap = options.specialKeyMap || VIEW_SPECIAL_KEY_MAP_DEFAULT; this.specialKeyMap = options.specialKeyMap || VIEW_SPECIAL_KEY_MAP_DEFAULT;
} }
this.isSpecialKeyMapped = function(keySet, keyName) { this.isKeyMapped = function(keySet, keyName) {
return _.has(this.specialKeyMap, keySet) && this.specialKeyMap[keySet].indexOf(keyName) > -1; return _.has(this.specialKeyMap, keySet) && this.specialKeyMap[keySet].indexOf(keyName) > -1;
}; };
@ -241,9 +241,9 @@ View.prototype.onKeyPress = function(ch, key) {
if(key) { if(key) {
assert(this.specialKeyMap, 'No special key map defined'); assert(this.specialKeyMap, 'No special key map defined');
if(this.isSpecialKeyMapped('accept', key.name)) { if(this.isKeyMapped('accept', key.name)) {
this.emit('action', 'accept'); this.emit('action', 'accept');
} else if(this.isSpecialKeyMapped('next', key.name)) { } else if(this.isKeyMapped('next', key.name)) {
this.emit('action', 'next'); this.emit('action', 'next');
} }
} }

View File

@ -406,15 +406,28 @@
"options" : { "cls" : true }, "options" : { "cls" : true },
"form" : { "form" : {
"0" : { "0" : {
"BT5HM1" : { "BT5HM1HM2" : {
"mci" : { "mci" : {
"HM1" : { "HM1" : {
"items" : [ "One", "Two", "Three" ] "items" : [ "One", "Two", "Three" ],
"hotKeys" : { "1" : 0, "2" : 1, "3" : 2 }
},
"HM2" : {
"items" : [ "Uno", "Dos", "Tres" ],
"hotKeys" : { "U" : 0, "D" : 1, "T" : 2 }
}, },
"BT5" : { "BT5" : {
"text" : "< Back", "text" : "< Back",
"submit" : [ "escape" ] "submit" : [ "escape" ]
} }
},
"submit" : {
"*" : [
{
"value" : 5,
"action" : "@menu:demoMain"
}
]
} }
} }
} }