2014-10-22 05:12:44 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var View = require('./view.js').View;
|
|
|
|
var miscUtil = require('./misc_util.js');
|
|
|
|
var strUtil = require('./string_util.js');
|
|
|
|
var ansi = require('./ansi_term.js');
|
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
exports.TextView = TextView;
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
function TextView(options) {
|
|
|
|
View.call(this, options);
|
2014-10-22 05:12:44 +00:00
|
|
|
|
2015-04-08 05:15:34 +00:00
|
|
|
var self = this;
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
if(options.maxLength) {
|
|
|
|
this.maxLength = options.maxLength;
|
2014-10-22 05:12:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
this.multiLine = options.multiLine || false;
|
|
|
|
this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1);
|
|
|
|
this.justify = options.justify || 'right';
|
2015-04-12 05:48:41 +00:00
|
|
|
this.resizable = miscUtil.valueWithDefault(options.resizable, true);
|
2015-04-09 04:54:13 +00:00
|
|
|
//this.inputType = options.inputType || 'normal';
|
2015-04-08 05:15:34 +00:00
|
|
|
|
|
|
|
this.isPasswordTextStyle = 'P' === this.textStyle || 'password' === this.textStyle;
|
2014-11-02 19:07:17 +00:00
|
|
|
|
2014-10-22 05:12:44 +00:00
|
|
|
assert(!this.multiLine); // :TODO: not yet supported
|
|
|
|
|
|
|
|
if(!this.multiLine) {
|
|
|
|
this.dimens.height = 1;
|
|
|
|
}
|
2015-04-06 06:18:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
this.drawText = function(s) {
|
|
|
|
var ansiColor = this.getANSIColor(this.hasFocus ? this.getFocusColor() : this.getColor());
|
|
|
|
|
|
|
|
if(this.isPasswordTextStyle) {
|
|
|
|
this.client.term.write(strUtil.pad(
|
|
|
|
new Array(s.length + 1).join(this.textMaskChar),
|
|
|
|
this.dimens.width + 1,
|
|
|
|
this.fillChar,
|
|
|
|
this.justify,
|
|
|
|
ansiColor,
|
|
|
|
this.getANSIColor(this.getColor())));
|
|
|
|
} else {
|
|
|
|
var text = strUtil.stylizeString(s, this.hasFocus ? this.focusTextStyle : this.textStyle);
|
|
|
|
this.client.term.write(strUtil.pad(
|
|
|
|
text,
|
|
|
|
this.dimens.width + 1,
|
|
|
|
this.fillChar,
|
|
|
|
this.justify,
|
|
|
|
ansiColor,
|
|
|
|
this.getANSIColor(this.getColor())));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
this.setText(options.text || '');
|
2014-10-22 05:12:44 +00:00
|
|
|
|
|
|
|
if(this.isPasswordTextStyle) {
|
2015-04-16 04:46:45 +00:00
|
|
|
this.textMaskChar = miscUtil.valueWithDefault(options.textMaskChar, '*').substr(0, 1);
|
2014-10-22 05:12:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(TextView, View);
|
|
|
|
|
|
|
|
TextView.prototype.redraw = function() {
|
|
|
|
TextView.super_.prototype.redraw.call(this);
|
|
|
|
|
2015-04-06 06:18:08 +00:00
|
|
|
this.drawText(this.text);
|
2014-10-22 05:12:44 +00:00
|
|
|
};
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
TextView.prototype.setFocus = function(focused) {
|
|
|
|
TextView.super_.prototype.setFocus.call(this, focused);
|
|
|
|
|
|
|
|
this.redraw();
|
|
|
|
this.client.term.write(ansi.goto(this.position.x, this.position.y + this.text.length));
|
2014-11-03 23:49:15 +00:00
|
|
|
this.client.term.write(this.getANSIColor(this.getFocusColor()));
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2015-04-17 04:29:53 +00:00
|
|
|
TextView.prototype.getData = function() {
|
2015-03-31 03:29:06 +00:00
|
|
|
return this.text;
|
|
|
|
};
|
|
|
|
|
2014-10-22 05:12:44 +00:00
|
|
|
TextView.prototype.setText = function(text) {
|
2015-04-12 05:48:41 +00:00
|
|
|
|
|
|
|
var widthDelta = 0;
|
|
|
|
if(this.text && this.text !== text) {
|
|
|
|
widthDelta = Math.abs(this.text.length - text.length);
|
|
|
|
}
|
|
|
|
|
2014-10-22 05:12:44 +00:00
|
|
|
this.text = text;
|
|
|
|
|
|
|
|
if(this.maxLength > 0) {
|
|
|
|
this.text = this.text.substr(0, this.maxLength);
|
|
|
|
}
|
|
|
|
|
2015-04-06 06:18:08 +00:00
|
|
|
this.text = strUtil.stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle);
|
2014-10-22 05:12:44 +00:00
|
|
|
|
2015-04-12 05:48:41 +00:00
|
|
|
/*if(!this.multiLine && !this.dimens.width) {
|
2014-10-22 05:12:44 +00:00
|
|
|
this.dimens.width = this.text.length;
|
2015-04-12 05:48:41 +00:00
|
|
|
}*/
|
|
|
|
|
|
|
|
if(!this.multiLine) {
|
|
|
|
if(this.resizable) {
|
|
|
|
this.dimens.width = this.text.length + widthDelta;
|
|
|
|
}
|
2014-10-22 05:12:44 +00:00
|
|
|
}
|
2014-11-04 05:53:01 +00:00
|
|
|
|
|
|
|
this.redraw();
|
2014-10-22 05:12:44 +00:00
|
|
|
};
|
2015-04-06 06:18:08 +00:00
|
|
|
|
|
|
|
TextView.prototype.clearText = function() {
|
|
|
|
this.setText('');
|
|
|
|
};
|