+ ToggleMenuView

* Start of hotkeys for menus
* General menu cleanup & rework
This commit is contained in:
Bryan Ashby 2015-04-27 16:04:41 -06:00
parent 10d8812300
commit 028c5be418
10 changed files with 172 additions and 9 deletions

View File

@ -17,12 +17,18 @@ exports.Client = Client;
//var ANSI_CONTROL_REGEX = /(?:(?:\u001b\[)|\u009b)(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\u001b[A-M]/g; //var ANSI_CONTROL_REGEX = /(?:(?:\u001b\[)|\u009b)(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\u001b[A-M]/g;
// :TODO: Move all of the key stuff to it's own module // :TODO: Move all of the key stuff to it's own module
//
// Resources & Standards:
// * http://www.ansi-bbs.org/ansi-bbs-core-server.html
//
var ANSI_KEY_NAME_MAP = { var ANSI_KEY_NAME_MAP = {
0x08 : 'backspace', 0x08 : 'backspace', // BS
0x09 : 'tab', 0x09 : 'tab', //
0x7f : 'del', 0x7f : 'del',
0x1b : 'esc', 0x1b : 'esc',
0x0d : 'enter', 0x0d : 'enter',
0x19 : 'end of medium', // EM / CTRL-Y
}; };
var ANSI_KEY_CSI_NAME_MAP = { var ANSI_KEY_CSI_NAME_MAP = {

View File

@ -34,7 +34,6 @@ EditTextView.prototype.onKeyPress = function(key, isSpecial) {
assert(1 === key.length); assert(1 === key.length);
// :TODO: how to handle justify left/center?
if(this.text.length < this.maxLength) { if(this.text.length < this.maxLength) {
key = strUtil.stylizeString(key, this.textStyle); key = strUtil.stylizeString(key, this.textStyle);
@ -58,8 +57,6 @@ EditTextView.prototype.onKeyPress = function(key, isSpecial) {
}; };
EditTextView.prototype.onSpecialKeyPress = function(keyName) { EditTextView.prototype.onSpecialKeyPress = function(keyName) {
// :TODO: handle 'enter' & others for multiLine
if(this.isSpecialKeyMapped('backspace', keyName)) { if(this.isSpecialKeyMapped('backspace', keyName)) {
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);
@ -73,6 +70,10 @@ EditTextView.prototype.onSpecialKeyPress = function(keyName) {
} }
} }
} }
} else if(this.isSpecialKeyMapped('clearLine', keyName)) {
this.text = '';
this.cursorPos.x = 0;
this.setFocus(true); // resetting focus will redraw & adjust cursor
} }

View File

@ -6,6 +6,7 @@ var EditTextView = require('./edit_text_view.js').EditTextView;
var ButtonView = require('./button_view.js').ButtonView; var ButtonView = require('./button_view.js').ButtonView;
var VerticalMenuView = require('./vertical_menu_view.js').VerticalMenuView; var VerticalMenuView = require('./vertical_menu_view.js').VerticalMenuView;
var SpinnerMenuView = require('./spinner_menu_view.js').SpinnerMenuView; var SpinnerMenuView = require('./spinner_menu_view.js').SpinnerMenuView;
var ToggleMenuView = require('./toggle_menu_view.js').ToggleMenuView;
var Config = require('./config.js').config; var Config = require('./config.js').config;
var packageJson = require('../package.json'); var packageJson = require('../package.json');
@ -177,6 +178,14 @@ MCIViewFactory.prototype.createFromMCI = function(mci) {
view = new SpinnerMenuView(options); view = new SpinnerMenuView(options);
break; break;
case 'TM' :
setOption(0, 'textStyle');
setFocusOption(0, 'focusTextStyle')
view = new ToggleMenuView(options);
break;
default : default :
options.text = this.getPredefinedViewLabel(mci.code); options.text = this.getPredefinedViewLabel(mci.code);
if(options.text) { if(options.text) {

View File

@ -4,8 +4,10 @@
var View = require('./view.js').View; var View = require('./view.js').View;
var ansi = require('./ansi_term.js'); var ansi = require('./ansi_term.js');
var miscUtil = require('./misc_util.js'); var miscUtil = require('./misc_util.js');
var util = require('util'); var util = require('util');
var assert = require('assert'); var assert = require('assert');
var _ = require('lodash');
exports.MenuView = MenuView; exports.MenuView = MenuView;
@ -23,6 +25,8 @@ function MenuView(options) {
this.items = []; this.items = [];
} }
this.setHotKeys(options.hotkeys);
this.focusedItemIndex = options.focusedItemIndex || 0; this.focusedItemIndex = options.focusedItemIndex || 0;
this.focusedItemIndex = this.items.length >= this.focusedItemIndex ? this.focusedItemIndex : 0; this.focusedItemIndex = this.items.length >= this.focusedItemIndex ? this.focusedItemIndex : 0;
@ -35,6 +39,7 @@ function MenuView(options) {
this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1); this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1);
this.justify = options.justify || 'none'; this.justify = options.justify || 'none';
/*
this.moveSelection = function(fromIndex, toIndex) { this.moveSelection = function(fromIndex, toIndex) {
assert(!self.positionCacheExpired); assert(!self.positionCacheExpired);
assert(fromIndex >= 0 && fromIndex <= self.items.length); assert(fromIndex >= 0 && fromIndex <= self.items.length);
@ -47,6 +52,7 @@ function MenuView(options) {
self.focusedItemIndex = toIndex; self.focusedItemIndex = toIndex;
self.drawItem(toIndex); self.drawItem(toIndex);
}; };
*/
/* /*
this.cachePositions = function() { this.cachePositions = function() {
@ -97,3 +103,9 @@ MenuView.prototype.setItems = function(items) {
} }
}; };
MenuView.prototype.setHotKeys = function(hotkeys) {
if(_.isObject(hotkeys)) {
this.hotkeys = hotkeys;
}
}

109
core/toggle_menu_view.js Normal file
View File

@ -0,0 +1,109 @@
/* jslint node: true */
'use strict';
var MenuView = require('./menu_view.js').MenuView;
var ansi = require('./ansi_term.js');
var strUtil = require('./string_util.js');
var util = require('util');
var assert = require('assert');
var _ = require('lodash');
exports.ToggleMenuView = ToggleMenuView;
function ToggleMenuView (options) {
options.cursor = options.cursor || 'hide';
MenuView.call(this, options);
var self = this;
this.cachePositions = function() {
self.positionCacheExpired = false;
};
this.updateSelection = function() {
assert(!self.positionCacheExpired);
assert(this.focusedItemIndex >= 0 && this.focusedItemIndex <= self.items.length);
self.redraw();
};
}
util.inherits(ToggleMenuView, MenuView);
ToggleMenuView.prototype.redraw = function() {
ToggleMenuView.super_.prototype.redraw.call(this);
this.cachePositions();
this.client.term.write(this.getANSIColor(this.hasFocus ? this.getFocusColor() : this.getColor()));
assert(this.items.length === 2);
for(var i = 0; i < 2; i++) {
var item = this.items[i];
var text = strUtil.stylizeString(item.text, i === this.focusedItemIndex ? this.focusTextStyle : this.textStyle);
if(1 === i) {
this.client.term.write(this.getANSIColor(this.getColor()) + ' / '); // :TODO: We need a color for this!!!
}
this.client.term.write(this.getANSIColor(i === this.focusedItemIndex ? this.getFocusColor() : this.getColor()));
this.client.term.write(text);
}
}
ToggleMenuView.prototype.setFocus = function(focused) {
ToggleMenuView.super_.prototype.setFocus.call(this, focused);
this.redraw();
};
ToggleMenuView.prototype.onKeyPress = function(key, isSpecial) {
if(isSpecial || !this.hotkeys) {
return;
}
assert(1 === key.length);
var keyIndex = this.hotkeys[key];
if(!_.isUndefined(keyIndex)) {
this.focusedItemIndex = keyIndex;
this.updateSelection();
}
ToggleMenuView.super_.prototype.onKeyPress.call(this, key, isSpecial);
}
ToggleMenuView.prototype.onSpecialKeyPress = function(keyName) {
if(this.isSpecialKeyMapped('right', keyName) || this.isSpecialKeyMapped('down', keyName)) {
if(this.items.length - 1 === this.focusedItemIndex) {
this.focusedItemIndex = 0;
} else {
this.focusedItemIndex++;
}
} else if(this.isSpecialKeyMapped('left', keyName) || this.isSpecialKeyMapped('up', keyName)) {
if(0 === this.focusedItemIndex) {
this.focusedItemIndex = this.items.length - 1;
} else {
this.focusedItemIndex--;
}
}
this.updateSelection();
ToggleMenuView.super_.prototype.onSpecialKeyPress.call(this, keyName);
}
ToggleMenuView.prototype.getData = function() {
return this.focusedItemIndex;
};
ToggleMenuView.prototype.setItems = function(items) {
ToggleMenuView.super_.prototype.setItems.call(this, items);
this.items = this.items.splice(0, 2); // switch/toggle only works with two elements
this.dimens.width = this.items.join(' / ').length; // :TODO: allow configurable seperator
}

View File

@ -16,7 +16,7 @@ function VerticalMenuView(options) {
var self = this; var self = this;
this.itemSpacing = 3; this.itemSpacing = 3; // :TODO: bring from options/configurable
this.calculateDimens = function() { this.calculateDimens = function() {
if(!self.dimens || !self.dimens.width) { if(!self.dimens || !self.dimens.width) {
@ -54,6 +54,19 @@ function VerticalMenuView(options) {
} }
}; };
this.changeSelection = function(fromIndex, toIndex) {
assert(!self.positionCacheExpired);
assert(fromIndex >= 0 && fromIndex <= self.items.length);
assert(toIndex >= 0 && toIndex <= self.items.length);
self.items[fromIndex].focused = false;
self.drawItem(fromIndex);
self.items[toIndex].focused = true;
self.focusedItemIndex = toIndex;
self.drawItem(toIndex);
};
this.drawItem = function(index) { this.drawItem = function(index) {
assert(!this.positionCacheExpired); assert(!this.positionCacheExpired);
@ -91,6 +104,7 @@ VerticalMenuView.prototype.setFocus = function(focused) {
this.redraw(); this.redraw();
}; };
VerticalMenuView.prototype.onSpecialKeyPress = function(keyName) { VerticalMenuView.prototype.onSpecialKeyPress = function(keyName) {
var prevFocusedItemIndex = this.focusedItemIndex; var prevFocusedItemIndex = this.focusedItemIndex;
@ -110,7 +124,7 @@ VerticalMenuView.prototype.onSpecialKeyPress = function(keyName) {
} }
if(prevFocusedItemIndex !== this.focusedItemIndex) { if(prevFocusedItemIndex !== this.focusedItemIndex) {
this.moveSelection(prevFocusedItemIndex, this.focusedItemIndex); this.changeSelection(prevFocusedItemIndex, this.focusedItemIndex);
} }
VerticalMenuView.super_.prototype.onSpecialKeyPress.call(this, keyName); VerticalMenuView.super_.prototype.onSpecialKeyPress.call(this, keyName);

View File

@ -18,6 +18,9 @@ var VIEW_SPECIAL_KEY_MAP_DEFAULT = {
next : [ 'tab' ], next : [ 'tab' ],
up : [ 'up arrow' ], up : [ 'up arrow' ],
down : [ 'down arrow' ], down : [ 'down arrow' ],
left : [ 'left arrow' ],
right : [ 'right arrow' ],
clearLine : [ 'end of medium' ],
}; };
function View(options) { function View(options) {

View File

@ -203,6 +203,11 @@ function ViewController(options) {
view.textMaskChar = self.client.currentThemeInfo.getPasswordChar(); view.textMaskChar = self.client.currentThemeInfo.getPasswordChar();
} }
value = getViewProp('hotkeys');
if(_.isObject(value)) {
view.setHotKeys(value);
}
value = getViewProp('submit'); value = getViewProp('submit');
if(_.isBoolean(value)) { if(_.isBoolean(value)) {

Binary file not shown.

View File

@ -204,18 +204,22 @@
"options" : { "cls" : true }, "options" : { "cls" : true },
"form" : { "form" : {
"0" : { "0" : {
"ET1ET2ET3ET5SM4" : { "ET1ET2ET3ET5SM4TM6" : {
"mci" : { "mci" : {
"ET1" : { "maxLength" : 1 }, "ET1" : { "maxLength" : 1 },
"ET2" : { "maxLength" : 1 }, "ET2" : { "maxLength" : 1 },
"ET3" : { "maxLength" : 1 }, "ET3" : { "maxLength" : 1 },
"SM4" : { "SM4" : {
"items" : [ "YES", "NO", "Maybe So" ] "items" : [ "One", "Two", "Three", "Four" ]
}, },
"ET5" : { "ET5" : {
"password" : true, "password" : true,
"submit" : [ "esc" ], "submit" : [ "esc" ],
"fillChar" : "#" "fillChar" : "#"
},
"TM6" : {
"items" : [ "Yes", "No" ],
"hotkeys" : { "y" : 0, "n" : 1 }
} }
} }
} }