* Experimental enter/leave events for Views

This commit is contained in:
Bryan Ashby 2015-04-11 23:48:41 -06:00
parent 85a72935fa
commit b0103cb178
5 changed files with 74 additions and 1 deletions

View File

@ -12,6 +12,7 @@ exports.EditTextView = EditTextView;
function EditTextView(options) { function EditTextView(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);
options.resizable = false;
TextView.call(this, options); TextView.call(this, options);

View File

@ -22,6 +22,7 @@ function TextView(options) {
this.multiLine = options.multiLine || false; this.multiLine = options.multiLine || false;
this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1); this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1);
this.justify = options.justify || 'right'; this.justify = options.justify || 'right';
this.resizable = miscUtil.valueWithDefault(options.resizable, true);
//this.inputType = options.inputType || 'normal'; //this.inputType = options.inputType || 'normal';
this.isPasswordTextStyle = 'P' === this.textStyle || 'password' === this.textStyle; this.isPasswordTextStyle = 'P' === this.textStyle || 'password' === this.textStyle;
@ -84,6 +85,12 @@ TextView.prototype.getViewData = function() {
}; };
TextView.prototype.setText = function(text) { TextView.prototype.setText = function(text) {
var widthDelta = 0;
if(this.text && this.text !== text) {
widthDelta = Math.abs(this.text.length - text.length);
}
this.text = text; this.text = text;
if(this.maxLength > 0) { if(this.maxLength > 0) {
@ -92,8 +99,14 @@ TextView.prototype.setText = function(text) {
this.text = strUtil.stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle); this.text = strUtil.stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle);
if(!this.multiLine && !this.dimens.width) { /*if(!this.multiLine && !this.dimens.width) {
this.dimens.width = this.text.length; this.dimens.width = this.text.length;
}*/
if(!this.multiLine) {
if(this.resizable) {
this.dimens.width = this.text.length + widthDelta;
}
} }
this.redraw(); this.redraw();

View File

@ -116,6 +116,20 @@ View.prototype.setPosition = function(pos) {
'Y position ' + this.position.y + ' out of terminal range ' + this.client.term.termWidth); 'Y position ' + this.position.y + ' out of terminal range ' + this.client.term.termWidth);
}; };
View.prototype.setColor = function(fg, bg, flags) {
if(fg) {
this.color.fg = fg;
}
if(bg) {
this.color.bg = bg;
}
if('undefined' !== typeof flags) {
this.color.flags = flags;
}
};
View.prototype.getColor = function() { View.prototype.getColor = function() {
return this.color; return this.color;
}; };
@ -133,6 +147,8 @@ View.prototype.setFocus = function(focused) {
this.hasFocus = focused; this.hasFocus = focused;
this.client.term.write('show' === this.cursor ? ansi.showCursor() : ansi.hideCursor()); this.client.term.write('show' === this.cursor ? ansi.showCursor() : ansi.hideCursor());
this.emit(focused ? 'enter' : 'leave');
}; };
View.prototype.onKeyPress = function(key, isSpecial) { View.prototype.onKeyPress = function(key, isSpecial) {

View File

@ -140,6 +140,10 @@ ViewController.prototype.getView = function(id) {
return this.views[id]; return this.views[id];
}; };
ViewController.prototype.getFocusedView = function() {
return this.focusedView;
};
ViewController.prototype.switchFocus = function(id) { ViewController.prototype.switchFocus = function(id) {
if(this.focusedView && this.focusedView.acceptsFocus) { if(this.focusedView && this.focusedView.acceptsFocus) {
this.focusedView.setFocus(false); this.focusedView.setFocus(false);

View File

@ -51,5 +51,44 @@ ApplyModule.prototype.mciReady = function(mciMap) {
self.viewController = self.addViewController(new ViewController(self.client)); self.viewController = self.addViewController(new ViewController(self.client));
self.viewController.loadFromMCIMapAndConfig( { mciMap : mciMap, menuConfig : self.menuConfig }, function onViewReady(err) { self.viewController.loadFromMCIMapAndConfig( { mciMap : mciMap, menuConfig : self.menuConfig }, function onViewReady(err) {
var usernameView = self.viewController.getView(1);
var userExistsView = self.viewController.getView(10);
usernameView.on('leave', function leave() {
user.getUserIdAndName(usernameView.getViewData(), function userIdAndName(err) {
if(!err) {
userExistsView.setText('That username already exists!');
} else {
userExistsView.setText('');
}
//if(11 !== self.viewController.getFocusedView()) {
self.viewController.switchFocus(2);
//}
});
});
var pwView = self.viewController.getView(8);
var pwConfirmView = self.viewController.getView(9);
var pwSecureView = self.viewController.getView(11);
var pwConfirmNoticeView = self.viewController.getView(12);
// :TODO: show a secure meter here instead
pwView.on('leave', function pwLeave() {
if(pwView.getViewData().length > 3) {
pwSecureView.setColor(32);
pwSecureView.setText('Secure');
} else {
pwSecureView.setColor(31);
pwSecureView.setText('Insecure!');
}
});
pwConfirmView.on('leave', function confirmPwLeave() {
if(pwView.getViewData() !== pwConfirmView.getViewData()) {
pwConfirmNoticeView.setText('Passwords must match!');
} else {
pwConfirmNoticeView.setText('');
}
});
}); });
}; };