* WIP on MaskEditTextView
This commit is contained in:
parent
a96af34a20
commit
9ac2e9af6e
|
@ -4,9 +4,9 @@
|
||||||
var TextView = require('./text_view.js').TextView;
|
var TextView = require('./text_view.js').TextView;
|
||||||
var miscUtil = require('./misc_util.js');
|
var miscUtil = require('./misc_util.js');
|
||||||
var strUtil = require('./string_util.js');
|
var strUtil = require('./string_util.js');
|
||||||
var ansi = require('./ansi_term.js');
|
//var ansi = require('./ansi_term.js');
|
||||||
|
|
||||||
var util = require('util');
|
//var util = require('util');
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ function EditTextView(options) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
util.inherits(EditTextView, TextView);
|
require('util').inherits(EditTextView, TextView);
|
||||||
|
|
||||||
EditTextView.prototype.onKeyPress = function(key, isSpecial) {
|
EditTextView.prototype.onKeyPress = function(key, isSpecial) {
|
||||||
if(isSpecial) {
|
if(isSpecial) {
|
||||||
|
|
|
@ -3,9 +3,12 @@
|
||||||
|
|
||||||
var TextView = require('./text_view.js').TextView;
|
var TextView = require('./text_view.js').TextView;
|
||||||
var miscUtil = require('./misc_util.js');
|
var miscUtil = require('./misc_util.js');
|
||||||
|
var strUtil = require('./string_util.js');
|
||||||
|
var ansi = require('./ansi_term.js');
|
||||||
|
|
||||||
var util = require('util');
|
//var util = require('util');
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
var _ = require('lodash');
|
||||||
|
|
||||||
exports.MaskEditTextView = MaskEditTextView;
|
exports.MaskEditTextView = MaskEditTextView;
|
||||||
|
|
||||||
|
@ -14,6 +17,10 @@ exports.MaskEditTextView = MaskEditTextView;
|
||||||
// buildPattern -> [ RE, RE, '/', RE, RE, '/', RE, RE, RE, RE ]
|
// buildPattern -> [ RE, RE, '/', RE, RE, '/', RE, RE, RE, RE ]
|
||||||
// patternIndex -----^
|
// patternIndex -----^
|
||||||
|
|
||||||
|
// styleSGR1: Literal's (non-focus)
|
||||||
|
// styleSGR2: Literals (focused)
|
||||||
|
// styleSGR3: fillChar
|
||||||
|
|
||||||
function MaskEditTextView(options) {
|
function MaskEditTextView(options) {
|
||||||
options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true);
|
options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true);
|
||||||
options.acceptsInput = miscUtil.valueWithDefault(options.acceptsInput, true);
|
options.acceptsInput = miscUtil.valueWithDefault(options.acceptsInput, true);
|
||||||
|
@ -23,38 +30,73 @@ function MaskEditTextView(options) {
|
||||||
TextView.call(this, options);
|
TextView.call(this, options);
|
||||||
|
|
||||||
this.cursorPos = { x : 0 };
|
this.cursorPos = { x : 0 };
|
||||||
|
this.patternArrayPos = 0;
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.maskPattern = options.maskPattern || '';
|
this.maskPattern = options.maskPattern || '';
|
||||||
|
|
||||||
this.buildPattern = function(pattern) {
|
this.clientBackspace = function() {
|
||||||
this.patternArray = [];
|
var fillCharSGR = this.getStyleSGR(2) || this.getSGR();
|
||||||
for(var i = 0; i < pattern.length; i++) {
|
this.client.term.write('\b' + fillCharSGR + this.fillChar + '\b' + this.getFocusSGR());
|
||||||
if(pattern[i] in MaskEditTextView.maskPatternCharacterRegEx) {
|
|
||||||
this.patternArray.push(MaskEditTextView.maskPatternCharacterRegEx[pattern[i]]);
|
|
||||||
} else {
|
|
||||||
this.patternArray.push(pattern[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log(this.patternArray)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.buildPattern(this.maskPattern);
|
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 {
|
||||||
|
self.client.term.write((self.getStyleSGR(2) || '') + self.fillChar);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.client.term.write((self.getStyleSGR(1) || '') + self.maskPattern[i]);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
} else {
|
||||||
|
self.patternArray.push(self.maskPattern[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.buildPattern();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
util.inherits(MaskEditTextView, TextView);
|
require('util').inherits(MaskEditTextView, TextView);
|
||||||
|
|
||||||
MaskEditTextView.maskPatternCharacterRegEx = {
|
MaskEditTextView.maskPatternCharacterRegEx = {
|
||||||
'#' : /[0-9]/,
|
'#' : /[0-9]/, // Numeric
|
||||||
'?' : /[a-zA-Z]/,
|
'A' : /[a-zA-Z]/, // Alpha
|
||||||
'&' : /[\w\d\s]/, // 32-126, 128-255
|
'@' : /[0-9a-zA-Z]/, // Alphanumeric
|
||||||
'A' : /[0-9a-zA-Z]/,
|
'&' : /[\w\d\s]/, // Any "printable" 32-126, 128-255
|
||||||
};
|
};
|
||||||
|
|
||||||
MaskEditTextView.prototype.setMaskPattern = function(pattern) {
|
MaskEditTextView.prototype.setMaskPattern = function(pattern) {
|
||||||
this.buildPattern(pattern);
|
this.dimens.width = pattern.length;
|
||||||
|
|
||||||
|
this.maskPattern = pattern;
|
||||||
|
this.buildPattern();
|
||||||
};
|
};
|
||||||
|
|
||||||
MaskEditTextView.prototype.onKeyPress = function(key, isSpecial) {
|
MaskEditTextView.prototype.onKeyPress = function(key, isSpecial) {
|
||||||
|
@ -67,36 +109,59 @@ MaskEditTextView.prototype.onKeyPress = function(key, isSpecial) {
|
||||||
if(this.text.length < this.maxLength) {
|
if(this.text.length < this.maxLength) {
|
||||||
key = strUtil.stylizeString(key, this.textStyle);
|
key = strUtil.stylizeString(key, this.textStyle);
|
||||||
|
|
||||||
/*this.text += key;
|
if(!key.match(this.patternArray[this.patternArrayPos])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.text += key;
|
||||||
|
this.patternArrayPos++;
|
||||||
|
|
||||||
|
while(this.patternArrayPos < this.patternArray.length &&
|
||||||
|
!_.isRegExp(this.patternArray[this.patternArrayPos]))
|
||||||
|
{
|
||||||
|
this.patternArrayPos++;
|
||||||
|
}
|
||||||
|
|
||||||
if(this.text.length > this.dimens.width) {
|
|
||||||
// no shortcuts - redraw the view
|
|
||||||
this.redraw();
|
this.redraw();
|
||||||
} else {
|
|
||||||
this.cursorPos.x += 1;
|
|
||||||
|
|
||||||
if(this.maskPatternChar) {
|
|
||||||
this.client.term.write(this.maskPatternChar);
|
|
||||||
} else {
|
|
||||||
this.client.term.write(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MaskEditTextView.super_.prototype.onKeyPress(this, key, isSpecial);
|
MaskEditTextView.super_.prototype.onKeyPress.call(this, key, isSpecial);
|
||||||
};
|
};
|
||||||
|
|
||||||
MaskEditTextView.prototype.onSpecialKeyPress = function(keyName) {
|
MaskEditTextView.prototype.onSpecialKeyPress = function(keyName) {
|
||||||
|
|
||||||
if(this.isSpecialKeyMapped('backspace', keyName)) {
|
if(this.isSpecialKeyMapped('backspace', keyName)) {
|
||||||
/*
|
|
||||||
if(this.text.length > 0) {
|
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.text = this.text.substr(0, this.text.length - 1);
|
||||||
this.clientBackspace();
|
this.clientBackspace();
|
||||||
|
} else {
|
||||||
|
var offset = -1;
|
||||||
|
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.x, this.position.y + (this.text.length - offset)));
|
||||||
|
// :TODO: use better ANSI Position code to just go back
|
||||||
|
// this.client.term.write(ansi.goto(this.position.x, yPosition));
|
||||||
|
|
||||||
|
this.clientBackspace();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
*/
|
console.log('skip past ' + this.patternArray[this.patternArrayPos])
|
||||||
|
//this.client.term.write(ansi.back() + ansi.back());
|
||||||
|
this.patternArrayPos--;
|
||||||
|
offset++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if(this.isSpecialKeyMapped('clearLine', keyName)) {
|
||||||
|
this.text = '';
|
||||||
|
this.patternArrayPos = 0;
|
||||||
|
this.setFocus(true); // redraw + adjust cursor
|
||||||
}
|
}
|
||||||
|
|
||||||
MaskEditTextView.super_.prototype.onSpecialKeyPress.call(this, keyName);
|
MaskEditTextView.super_.prototype.onSpecialKeyPress.call(this, keyName);
|
||||||
|
|
Binary file not shown.
|
@ -334,7 +334,8 @@
|
||||||
"BT5ME1ME2" : {
|
"BT5ME1ME2" : {
|
||||||
"mci" : {
|
"mci" : {
|
||||||
"ME1" : {
|
"ME1" : {
|
||||||
"maskPattern" : "##/##/##"
|
"maskPattern" : "##/##/##",
|
||||||
|
"styleSGR1" : "|00|30|01"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue