enigma-bbs/core/text_view.js

168 lines
4.3 KiB
JavaScript
Raw Normal View History

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');
2014-10-22 05:12:44 +00:00
var util = require('util');
var _ = require('lodash');
2014-10-22 05:12:44 +00:00
exports.TextView = TextView;
function TextView(options) {
if(options.dimens) {
options.dimens.height = 1; // force height of 1 for TextView's & sub classes
}
View.call(this, options);
2014-10-22 05:12:44 +00:00
2015-04-08 05:15:34 +00:00
var self = this;
if(options.maxLength) {
this.maxLength = options.maxLength;
} else {
this.maxLength = this.client.term.termWidth - this.position.col;
2014-10-22 05:12:44 +00:00
}
this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1);
this.justify = options.justify || 'right';
this.resizable = miscUtil.valueWithDefault(options.resizable, true);
this.horizScroll = miscUtil.valueWithDefault(options.horizScroll, true);
if(_.isString(options.textOverflow)) {
this.textOverflow = options.textOverflow;
}
if(_.isString(options.textMaskChar) && 1 === options.textMaskChar.length) {
this.textMaskChar = options.textMaskChar;
2014-10-22 05:12:44 +00:00
}
this.drawText = function(s) {
//
// |<- this.maxLength
// ABCDEFGHIJK
// |ABCDEFG| ^_ this.text.length
// ^-- this.dimens.width
//
var textToDraw = _.isString(this.textMaskChar) ?
new Array(s.length + 1).join(this.textMaskChar) :
strUtil.stylizeString(s, this.hasFocus ? this.focusTextStyle : this.textStyle);
2016-07-16 19:06:03 +00:00
// :TODO: Add pipe code support
if(textToDraw.length > this.dimens.width) {
// XXXXXXXXXXXXXXXXX
// this is the text but too long
// text but too long
if(this.hasFocus) {
if(this.horizScroll) {
textToDraw = textToDraw.substr(textToDraw.length - this.dimens.width, textToDraw.length);
}
} else {
if(textToDraw.length > this.dimens.width) {
if(this.textOverflow &&
this.dimens.width > this.textOverflow.length &&
textToDraw.length - this.textOverflow.length >= this.textOverflow.length)
{
textToDraw = textToDraw.substr(0, this.dimens.width - this.textOverflow.length) + this.textOverflow;
} else {
textToDraw = textToDraw.substr(0, this.dimens.width);
}
}
}
}
this.client.term.write(strUtil.pad(
textToDraw,
this.dimens.width + 1,
this.fillChar,
this.justify,
this.hasFocus ? this.getFocusSGR() : this.getSGR(),
this.getStyleSGR(1) || this.getSGR()
), false);
};
this.getEndOfTextColumn = function() {
var offset = Math.min(this.text.length, this.dimens.width);
return this.position.col + offset;
};
2015-12-24 18:54:03 +00:00
// :TODO: Whatever needs init here should be done separately from setText() since it redraws/etc.
this.setText(options.text || '');
2014-10-22 05:12:44 +00:00
}
util.inherits(TextView, View);
TextView.prototype.redraw = function() {
TextView.super_.prototype.redraw.call(this);
2015-12-24 18:54:03 +00:00
if(_.isString(this.text)) {
this.drawText(this.text);
}
2014-10-22 05:12:44 +00:00
};
TextView.prototype.setFocus = function(focused) {
TextView.super_.prototype.setFocus.call(this, focused);
this.redraw();
this.client.term.write(ansi.goto(this.position.row, this.getEndOfTextColumn()));
this.client.term.write(this.getFocusSGR());
};
TextView.prototype.getData = function() {
return this.text;
};
2014-10-22 05:12:44 +00:00
TextView.prototype.setText = function(text) {
2016-07-16 19:06:03 +00:00
if(!_.isString(text)) {
text = text.toString();
}
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);
}
this.text = strUtil.stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle);
2014-10-22 05:12:44 +00:00
/*
if(this.resizable) {
this.dimens.width = this.text.length + widthDelta;
2014-10-22 05:12:44 +00:00
}
*/
if(this.autoScale.width) {
this.dimens.width = this.text.length + widthDelta;
}
this.redraw();
2014-10-22 05:12:44 +00:00
};
TextView.prototype.clearText = function() {
this.setText('');
};
TextView.prototype.setPropertyValue = function(propName, value) {
switch(propName) {
2016-07-16 19:06:03 +00:00
case 'textMaskChar' : this.textMaskChar = value.substr(0, 1); break;
case 'textOverflow' : this.textOverflow = value; break;
case 'maxLength' : this.maxLength = parseInt(value, 10); break;
case 'password' :
if(true === value) {
this.textMaskChar = this.client.currentTheme.helpers.getPasswordChar();
}
break;
}
TextView.super_.prototype.setPropertyValue.call(this, propName, value);
};