2015-04-09 04:54:13 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var TextView = require('./text_view.js').TextView;
|
|
|
|
var miscUtil = require('./misc_util.js');
|
2015-05-15 23:17:13 +00:00
|
|
|
var strUtil = require('./string_util.js');
|
|
|
|
var ansi = require('./ansi_term.js');
|
2015-04-09 04:54:13 +00:00
|
|
|
|
2015-05-15 23:17:13 +00:00
|
|
|
//var util = require('util');
|
2015-04-09 04:54:13 +00:00
|
|
|
var assert = require('assert');
|
2015-05-15 23:17:13 +00:00
|
|
|
var _ = require('lodash');
|
2015-04-09 04:54:13 +00:00
|
|
|
|
2015-05-15 05:01:00 +00:00
|
|
|
exports.MaskEditTextView = MaskEditTextView;
|
|
|
|
|
|
|
|
// ##/##/#### <--styleSGR2 if fillChar
|
|
|
|
// ^- styleSGR1
|
|
|
|
// buildPattern -> [ RE, RE, '/', RE, RE, '/', RE, RE, RE, RE ]
|
|
|
|
// patternIndex -----^
|
|
|
|
|
2015-05-15 23:17:13 +00:00
|
|
|
// styleSGR1: Literal's (non-focus)
|
|
|
|
// styleSGR2: Literals (focused)
|
|
|
|
// styleSGR3: fillChar
|
|
|
|
|
2015-07-22 05:52:20 +00:00
|
|
|
//
|
|
|
|
// :TODO:
|
|
|
|
// * Hint, e.g. YYYY/MM/DD
|
|
|
|
// * Return values with literals in place
|
|
|
|
//
|
|
|
|
|
2015-05-15 05:01:00 +00:00
|
|
|
function MaskEditTextView(options) {
|
2015-04-09 04:54:13 +00:00
|
|
|
options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true);
|
|
|
|
options.acceptsInput = miscUtil.valueWithDefault(options.acceptsInput, true);
|
2015-05-15 05:01:00 +00:00
|
|
|
options.cursorStyle = miscUtil.valueWithDefault(options.cursorStyle, 'steady block');
|
|
|
|
options.resizable = false;
|
|
|
|
|
|
|
|
TextView.call(this, options);
|
2015-04-09 04:54:13 +00:00
|
|
|
|
2015-05-15 23:17:13 +00:00
|
|
|
this.cursorPos = { x : 0 };
|
|
|
|
this.patternArrayPos = 0;
|
2015-04-09 04:54:13 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2015-05-15 05:01:00 +00:00
|
|
|
this.maskPattern = options.maskPattern || '';
|
|
|
|
|
2015-05-15 23:17:13 +00:00
|
|
|
this.clientBackspace = function() {
|
2015-05-16 05:02:58 +00:00
|
|
|
var fillCharSGR = this.getStyleSGR(3) || this.getSGR();
|
2015-05-15 23:17:13 +00:00
|
|
|
this.client.term.write('\b' + fillCharSGR + this.fillChar + '\b' + this.getFocusSGR());
|
|
|
|
};
|
|
|
|
|
|
|
|
this.drawText = function(s) {
|
|
|
|
var textToDraw = strUtil.stylizeString(s, this.hasFocus ? this.focusTextStyle : this.textStyle);
|
|
|
|
|
|
|
|
assert(textToDraw.length <= self.patternArray.length);
|
|
|
|
|
|
|
|
// draw out the text we have so far
|
|
|
|
var i = 0;
|
|
|
|
var t = 0;
|
|
|
|
while(i < self.patternArray.length) {
|
|
|
|
if(_.isRegExp(self.patternArray[i])) {
|
|
|
|
if(t < textToDraw.length) {
|
|
|
|
self.client.term.write((self.hasFocus ? self.getFocusSGR() : self.getSGR()) + textToDraw[t]);
|
|
|
|
t++;
|
|
|
|
} else {
|
2015-05-16 05:02:58 +00:00
|
|
|
self.client.term.write((self.getStyleSGR(3) || '') + self.fillChar);
|
2015-05-15 23:17:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-05-16 05:02:58 +00:00
|
|
|
var styleSgr = this.hasFocus ? (self.getStyleSGR(2) || '') : (self.getStyleSGR(1) || '');
|
|
|
|
self.client.term.write(styleSgr + self.maskPattern[i]);
|
2015-05-15 23:17:13 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.buildPattern = function() {
|
|
|
|
self.patternArray = [];
|
|
|
|
self.maxLength = 0;
|
|
|
|
|
|
|
|
for(var i = 0; i < self.maskPattern.length; i++) {
|
|
|
|
// :TODO: support escaped characters, e.g. \#. Also allow \\ for a '\' mark!
|
|
|
|
if(self.maskPattern[i] in MaskEditTextView.maskPatternCharacterRegEx) {
|
|
|
|
self.patternArray.push(MaskEditTextView.maskPatternCharacterRegEx[self.maskPattern[i]]);
|
|
|
|
++self.maxLength;
|
2015-05-15 05:01:00 +00:00
|
|
|
} else {
|
2015-05-15 23:17:13 +00:00
|
|
|
self.patternArray.push(self.maskPattern[i]);
|
2015-05-15 05:01:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-18 17:31:35 +00:00
|
|
|
this.getEndOfTextColumn = function() {
|
|
|
|
return this.position.col + this.patternArrayPos;
|
2015-05-16 05:02:58 +00:00
|
|
|
};
|
|
|
|
|
2015-05-15 23:17:13 +00:00
|
|
|
this.buildPattern();
|
2015-04-09 04:54:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-15 23:17:13 +00:00
|
|
|
require('util').inherits(MaskEditTextView, TextView);
|
2015-04-09 04:54:13 +00:00
|
|
|
|
2015-05-15 05:01:00 +00:00
|
|
|
MaskEditTextView.maskPatternCharacterRegEx = {
|
2015-05-15 23:17:13 +00:00
|
|
|
'#' : /[0-9]/, // Numeric
|
|
|
|
'A' : /[a-zA-Z]/, // Alpha
|
|
|
|
'@' : /[0-9a-zA-Z]/, // Alphanumeric
|
|
|
|
'&' : /[\w\d\s]/, // Any "printable" 32-126, 128-255
|
2015-04-09 04:54:13 +00:00
|
|
|
};
|
|
|
|
|
2015-05-15 05:01:00 +00:00
|
|
|
MaskEditTextView.prototype.setMaskPattern = function(pattern) {
|
2015-05-15 23:17:13 +00:00
|
|
|
this.dimens.width = pattern.length;
|
|
|
|
|
|
|
|
this.maskPattern = pattern;
|
|
|
|
this.buildPattern();
|
2015-04-09 04:54:13 +00:00
|
|
|
};
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
MaskEditTextView.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.patternArrayPos--;
|
|
|
|
assert(this.patternArrayPos >= 0);
|
|
|
|
|
|
|
|
if(_.isRegExp(this.patternArray[this.patternArrayPos])) {
|
|
|
|
this.text = this.text.substr(0, this.text.length - 1);
|
|
|
|
this.clientBackspace();
|
|
|
|
} else {
|
|
|
|
while(this.patternArrayPos > 0) {
|
|
|
|
if(_.isRegExp(this.patternArray[this.patternArrayPos])) {
|
|
|
|
this.text = this.text.substr(0, this.text.length - 1);
|
|
|
|
this.client.term.write(ansi.goto(this.position.row, this.getEndOfTextColumn() + 1));
|
|
|
|
this.clientBackspace();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.patternArrayPos--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 05:01:00 +00:00
|
|
|
|
2015-05-15 23:17:13 +00:00
|
|
|
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 = '';
|
|
|
|
this.patternArrayPos = 0;
|
|
|
|
this.setFocus(true); // redraw + adjust cursor
|
2015-05-15 05:01:00 +00:00
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
return;
|
2015-05-15 05:01:00 +00:00
|
|
|
}
|
2015-06-05 22:20:26 +00:00
|
|
|
}
|
2015-05-15 23:17:13 +00:00
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
if(ch && strUtil.isPrintable(ch)) {
|
|
|
|
if(this.text.length < this.maxLength) {
|
|
|
|
ch = strUtil.stylizeString(ch, this.textStyle);
|
2015-04-09 04:54:13 +00:00
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
if(!ch.match(this.patternArray[this.patternArrayPos])) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-09 04:54:13 +00:00
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
this.text += ch;
|
|
|
|
this.patternArrayPos++;
|
2015-05-15 23:17:13 +00:00
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
while(this.patternArrayPos < this.patternArray.length &&
|
|
|
|
!_.isRegExp(this.patternArray[this.patternArrayPos]))
|
|
|
|
{
|
|
|
|
this.patternArrayPos++;
|
2015-05-15 23:17:13 +00:00
|
|
|
}
|
2015-06-05 22:20:26 +00:00
|
|
|
|
|
|
|
this.redraw();
|
|
|
|
this.client.term.write(ansi.goto(this.position.row, this.getEndOfTextColumn()));
|
2015-04-09 04:54:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
MaskEditTextView.super_.prototype.onKeyPress.call(this, ch, key);
|
|
|
|
};
|
2015-06-30 05:14:17 +00:00
|
|
|
|
|
|
|
MaskEditTextView.prototype.setPropertyValue = function(propName, value) {
|
|
|
|
switch(propName) {
|
|
|
|
case 'maskPattern' : this.setMaskPattern(value); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MaskEditTextView.super_.prototype.setPropertyValue.call(this, propName, value);
|
2015-07-22 05:52:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MaskEditTextView.prototype.getData = function() {
|
|
|
|
var rawData = MaskEditTextView.super_.prototype.getData.call(this);
|
2015-12-12 22:52:56 +00:00
|
|
|
|
|
|
|
if(!rawData || 0 === rawData.length) {
|
|
|
|
return rawData;
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:52:20 +00:00
|
|
|
var data = '';
|
|
|
|
|
|
|
|
assert(rawData.length <= this.patternArray.length);
|
|
|
|
|
|
|
|
var p = 0;
|
|
|
|
for(var i = 0; i < this.patternArray.length; ++i) {
|
|
|
|
if(_.isRegExp(this.patternArray[i])) {
|
|
|
|
data += rawData[p++];
|
|
|
|
} else {
|
|
|
|
data += this.patternArray[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|