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;
|
|
|
|
|
|
|
|
function TextView(client, options) {
|
|
|
|
View.call(this, client, options);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if(this.options.maxLength) {
|
|
|
|
this.maxLength = this.options.maxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.textStyle = this.options.textStyle || 'normal';
|
|
|
|
this.multiLine = this.options.multiLine || false;
|
|
|
|
|
|
|
|
assert(!this.multiLine); // :TODO: not yet supported
|
|
|
|
|
|
|
|
if(!this.multiLine) {
|
|
|
|
this.dimens.height = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setText(this.options.text || '');
|
|
|
|
|
|
|
|
this.isPasswordTextStyle = 'P' === self.textStyle || 'password' === self.textStyle;
|
|
|
|
|
|
|
|
if(this.isPasswordTextStyle) {
|
2014-10-23 05:41:00 +00:00
|
|
|
this.textMaskChar = miscUtil.valueWithDefault(this.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);
|
|
|
|
|
|
|
|
var color = this.hasFocus ? this.getFocusColor() : this.getColor();
|
|
|
|
|
2014-10-27 04:06:41 +00:00
|
|
|
//this.client.term.write(ansi.sgr(color.flags, color.fg, color.bg));
|
|
|
|
this.client.term.write(this.getANSIColor(color));
|
2014-10-22 05:12:44 +00:00
|
|
|
|
|
|
|
if(this.isPasswordTextStyle) {
|
2014-10-27 04:06:41 +00:00
|
|
|
this.client.term.write(strUtil.pad(new Array(this.text.length + 1).join(this.textMaskChar), this.dimens.width));
|
2014-10-22 05:12:44 +00:00
|
|
|
} else {
|
2014-10-27 04:06:41 +00:00
|
|
|
this.client.term.write(strUtil.pad(this.text, this.dimens.width));
|
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.client.term.write(ansi.goto(this.position.x, this.position.y));
|
|
|
|
this.redraw();
|
|
|
|
this.client.term.write(ansi.goto(this.position.x, this.position.y + this.text.length));
|
|
|
|
};
|
|
|
|
|
2014-10-22 05:12:44 +00:00
|
|
|
TextView.prototype.setText = function(text) {
|
|
|
|
this.text = text;
|
|
|
|
|
|
|
|
if(this.maxLength > 0) {
|
|
|
|
this.text = this.text.substr(0, this.maxLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.text = strUtil.stylizeString(this.text, this.textStyle);
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
if(!this.multiLine && !this.dimens.width) {
|
2014-10-22 05:12:44 +00:00
|
|
|
this.dimens.width = this.text.length;
|
|
|
|
}
|
|
|
|
};
|