enigma-bbs/core/key_entry_view.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-07-21 04:11:57 +00:00
/* jslint node: true */
'use strict';
const View = require('./view.js').View;
const valueWithDefault = require('./misc_util.js').valueWithDefault;
const isPrintable = require('./string_util.js').isPrintable;
const stylizeString = require('./string_util.js').stylizeString;
const _ = require('lodash');
module.exports = class KeyEntryView extends View {
constructor(options) {
options.acceptsFocus = valueWithDefault(options.acceptsFocus, true);
options.acceptsInput = valueWithDefault(options.acceptsInput, true);
super(options);
this.eatTabKey = options.eatTabKey || true;
this.caseInsensitive = options.caseInsensitive || true;
// :TODO: allow (by default) only supplied keys[] to even draw
}
onKeyPress(ch, key) {
if(ch && isPrintable(ch)) {
this.redraw(); // sets position
this.client.term.write(stylizeString(ch, this.textStyle));
}
2016-07-21 04:21:46 +00:00
if(ch && this.caseInsensitive) {
2016-07-21 04:11:57 +00:00
ch = ch.toUpperCase();
}
this.keyEntered = ch || key.name;
if(key && 'tab' === key.name && !this.eatTabKey) {
return this.emit('action', 'next', key);
}
this.emit('action', 'accept');
// NOTE: we don't call super here. KeyEntryView is a special snowflake.
}
setPropertyValue(propName, propValue) {
switch(propName) {
case 'eatTabKey' :
if(_.isBoolean(propValue)) {
this.eatTabKey = propValue;
}
break;
case 'caseInsensitive' :
if(_.isBoolean(propValue)) {
this.caseInsensitive = propValue;
}
break;
}
super.setPropertyValue(propName, propValue);
}
getData() { return this.keyEntered; }
};