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-05-15 23:17:13 +00:00
|
|
|
//var ansi = require('./ansi_term.js');
|
2015-04-28 02:19:17 +00:00
|
|
|
|
2015-05-15 23:17:13 +00:00
|
|
|
//var util = require('util');
|
2014-10-23 05:41:00 +00:00
|
|
|
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-05-28 04:53:21 +00:00
|
|
|
this.cursorPos = { row : 0, col : 0 };
|
2015-04-27 02:46:16 +00:00
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
this.clientBackspace = function() {
|
2015-05-15 05:01:00 +00:00
|
|
|
var fillCharSGR = this.getStyleSGR(1) || this.getSGR();
|
|
|
|
this.client.term.write('\b' + fillCharSGR + this.fillChar + '\b' + this.getFocusSGR());
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-15 23:17:13 +00:00
|
|
|
require('util').inherits(EditTextView, TextView);
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
EditTextView.prototype.onKeyPress = function(ch, key) {
|
|
|
|
if(key) {
|
2015-07-02 02:18:34 +00:00
|
|
|
if(this.isKeyMapped('backspace', key.name)) {
|
2015-06-05 22:20:26 +00:00
|
|
|
if(this.text.length > 0) {
|
|
|
|
this.text = this.text.substr(0, this.text.length - 1);
|
|
|
|
|
|
|
|
if(this.text.length >= this.dimens.width) {
|
|
|
|
this.redraw();
|
|
|
|
} else {
|
2015-06-29 04:31:12 +00:00
|
|
|
this.cursorPos.col -= 1;
|
|
|
|
if(this.cursorPos.col >= 0) {
|
2015-06-05 22:20:26 +00:00
|
|
|
this.clientBackspace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2015-07-02 02:18:34 +00:00
|
|
|
} else if(this.isKeyMapped('clearLine', key.name)) {
|
2015-06-05 22:20:26 +00:00
|
|
|
this.text = '';
|
2015-06-29 04:31:12 +00:00
|
|
|
this.cursorPos.col = 0;
|
2015-06-05 22:20:26 +00:00
|
|
|
this.setFocus(true); // resetting focus will redraw & adjust cursor
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ch && strUtil.isPrintable(ch)) {
|
|
|
|
if(this.text.length < this.maxLength) {
|
|
|
|
ch = strUtil.stylizeString(ch, this.textStyle);
|
|
|
|
|
|
|
|
this.text += ch;
|
|
|
|
|
|
|
|
if(this.text.length > this.dimens.width) {
|
|
|
|
// no shortcuts - redraw the view
|
|
|
|
this.redraw();
|
|
|
|
} else {
|
2015-06-29 04:31:12 +00:00
|
|
|
this.cursorPos.col += 1;
|
2015-06-05 22:20:26 +00:00
|
|
|
|
|
|
|
if(this.textMaskChar) {
|
|
|
|
this.client.term.write(this.textMaskChar);
|
|
|
|
} else {
|
|
|
|
this.client.term.write(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EditTextView.super_.prototype.onKeyPress.call(this, ch, key);
|
|
|
|
};
|
|
|
|
|
2015-06-29 04:31:12 +00:00
|
|
|
EditTextView.prototype.setText = function(text) {
|
|
|
|
// draw & set |text|
|
|
|
|
EditTextView.super_.prototype.setText.call(this, text);
|
2015-04-27 02:46:16 +00:00
|
|
|
|
2015-06-29 04:31:12 +00:00
|
|
|
// adjust local cursor tracking
|
|
|
|
this.cursorPos = { row : 0, col : text.length };
|
2015-06-05 22:20:26 +00:00
|
|
|
};
|