2014-10-23 05:41:00 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var TextView = require('./text_view.js').TextView;
|
|
|
|
var miscUtil = require('./misc_util.js');
|
|
|
|
var strUtil = require('./string_util.js');
|
2015-04-28 02:19:17 +00:00
|
|
|
var ansi = require('./ansi_term.js');
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
2015-04-19 08:13:13 +00:00
|
|
|
var _ = require('lodash');
|
2014-10-23 05:41:00 +00:00
|
|
|
|
|
|
|
exports.EditTextView = EditTextView;
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
function EditTextView(options) {
|
2015-04-08 05:15:34 +00:00
|
|
|
options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true);
|
|
|
|
options.acceptsInput = miscUtil.valueWithDefault(options.acceptsInput, true);
|
2015-04-28 02:19:17 +00:00
|
|
|
options.cursorStyle = miscUtil.valueWithDefault(options.cursorStyle, 'steady block');
|
2015-04-12 05:48:41 +00:00
|
|
|
options.resizable = false;
|
2015-04-27 02:46:16 +00:00
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
TextView.call(this, options);
|
2015-04-08 05:15:34 +00:00
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
this.cursorPos = { x : 0 };
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
this.clientBackspace = function() {
|
2014-11-03 23:49:15 +00:00
|
|
|
this.client.term.write(
|
|
|
|
'\b' + this.getANSIColor(this.getColor()) + this.fillChar + '\b' + this.getANSIColor(this.getFocusColor()));
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(EditTextView, TextView);
|
|
|
|
|
2014-10-24 04:18:38 +00:00
|
|
|
EditTextView.prototype.onKeyPress = function(key, isSpecial) {
|
2014-10-23 05:41:00 +00:00
|
|
|
if(isSpecial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(1 === key.length);
|
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
if(this.text.length < this.maxLength) {
|
2014-10-23 05:41:00 +00:00
|
|
|
key = strUtil.stylizeString(key, this.textStyle);
|
|
|
|
|
|
|
|
this.text += key;
|
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
if(this.text.length > this.dimens.width) {
|
|
|
|
// no shortcuts - redraw the view
|
|
|
|
this.redraw();
|
2014-10-23 05:41:00 +00:00
|
|
|
} else {
|
2015-04-27 02:46:16 +00:00
|
|
|
this.cursorPos.x += 1;
|
|
|
|
|
|
|
|
if(this.textMaskChar) {
|
|
|
|
this.client.term.write(this.textMaskChar);
|
|
|
|
} else {
|
|
|
|
this.client.term.write(key);
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-24 04:18:38 +00:00
|
|
|
|
|
|
|
EditTextView.super_.prototype.onKeyPress.call(this, key, isSpecial);
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EditTextView.prototype.onSpecialKeyPress = function(keyName) {
|
|
|
|
if(this.isSpecialKeyMapped('backspace', keyName)) {
|
|
|
|
if(this.text.length > 0) {
|
|
|
|
this.text = this.text.substr(0, this.text.length - 1);
|
2015-04-27 02:46:16 +00:00
|
|
|
|
|
|
|
if(this.text.length >= this.dimens.width) {
|
|
|
|
this.redraw();
|
|
|
|
} else {
|
|
|
|
this.cursorPos.x -= 1;
|
|
|
|
if(this.cursorPos.x >= 0) {
|
|
|
|
this.clientBackspace();
|
|
|
|
}
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
}
|
2015-04-27 22:04:41 +00:00
|
|
|
} else if(this.isSpecialKeyMapped('clearLine', keyName)) {
|
|
|
|
this.text = '';
|
|
|
|
this.cursorPos.x = 0;
|
|
|
|
this.setFocus(true); // resetting focus will redraw & adjust cursor
|
2014-10-23 05:41:00 +00:00
|
|
|
}
|
2014-10-24 04:18:38 +00:00
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
|
2014-10-24 04:18:38 +00:00
|
|
|
EditTextView.super_.prototype.onSpecialKeyPress.call(this, keyName);
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|