misc fixes for radiobutton, textarea, textbox, checkbox.

This commit is contained in:
Christopher Jeffrey 2013-07-24 17:16:55 -05:00
parent 1c9d25780a
commit 6fde3e3a38
2 changed files with 40 additions and 38 deletions

View File

@ -154,7 +154,7 @@ Node.prototype.detach = function() {
Node.prototype.forDescendants = function(iter, s) {
if (s) iter(this);
el.children.forEach(function emit(el) {
this.children.forEach(function emit(el) {
iter(el);
el.children.forEach(emit);
});
@ -4075,7 +4075,15 @@ Textbox.prototype.setInput = function(callback) {
this.screen.program.showCursor();
this.screen.program.sgr('normal');
this._callback = function(err, value) {
this._callback = function fn(err, value) {
if (fn.done) return;
fn.done = true;
self.removeListener('keypress', self.__listener);
self.removeListener('blur', self._callback);
delete self.__listener;
delete self._callback;
self.screen.program.hideCursor();
self.screen.grabKeys = false;
@ -4101,6 +4109,7 @@ Textbox.prototype.setInput = function(callback) {
this.__listener = this._listener.bind(this);
this.on('keypress', this.__listener);
this.on('blur', this._callback);
};
Textbox.prototype._listener = function(ch, key) {
@ -4109,9 +4118,6 @@ Textbox.prototype._listener = function(ch, key) {
, width = this.width - this.iwidth;
if (key.name === 'escape' || key.name === 'enter') {
delete this._callback;
this.removeListener('keypress', this.__listener);
delete this.__listener;
callback(null, key.name === 'enter' ? value : null);
} else if (key.name === 'backspace') {
if (this.value.length) {
@ -4121,25 +4127,16 @@ Textbox.prototype._listener = function(ch, key) {
this.screen.program.cub();
}
}
} else {
if (ch) {
// Tabs only work with textareas.
if (ch === '\t') ch = ' ';
this.value += ch;
if (this.secret) return;
if (this.value.length < width) {
this.screen.program.cuf();
}
} else if (ch) {
// Tabs only work with textareas.
if (ch === '\t') ch = ' ';
this.value += ch;
if (this.secret) return;
if (this.value.length < width) {
this.screen.program.cuf();
}
}
// Maybe just use this instead of render hook:
// Problem - user can't set .value willy nilly.
// if (this.value !== value) {
// var i = -(this.width - this.iwidth - 1);
// this.setContent(this.value.slice(i));
// }
this.screen.render();
};
@ -4279,7 +4276,15 @@ Textarea.prototype.setInput = function(callback) {
this.screen.program.showCursor();
this.screen.program.sgr('normal');
this._callback = function(err, value) {
this._callback = function fn(err, value) {
if (fn.done) return;
fn.done = true;
self.removeListener('keypress', self.__listener);
self.removeListener('blur', self._callback);
delete self.__listener;
delete self._callback;
self.screen.program.hideCursor();
self.screen.grabKeys = false;
@ -4305,6 +4310,7 @@ Textarea.prototype.setInput = function(callback) {
this.__listener = this._listener.bind(this);
this.on('keypress', this.__listener);
this.on('blur', this._callback);
};
Textarea.prototype._listener = function(ch, key) {
@ -4322,18 +4328,13 @@ Textarea.prototype._listener = function(ch, key) {
}
if (key.name === 'escape') {
delete this._callback;
this.removeListener('keypress', this.__listener);
delete this.__listener;
callback(null, key.name === 'enter' ? value : null);
} else if (key.name === 'backspace') {
if (this.value.length) {
this.value = this.value.slice(0, -1);
}
} else {
if (ch) {
this.value += ch;
}
} else if (ch) {
this.value += ch;
}
if (this.value !== value) {
@ -4748,7 +4749,7 @@ function Checkbox(options) {
if (options.mouse) {
this.on('click', function() {
self.check();
self.toggle();
self.screen.render();
});
}
@ -4836,14 +4837,14 @@ function RadioButton(options) {
this.on('check', function() {
var el = self;
while (el = el.parent) {
if (self.parent.type === 'radio-set'
|| self.parent.type === 'form') {
break;
}
if (el.type === 'radio-set'
|| el.type === 'form') break;
}
if (!el) el = self.parent;
el.children.forEach(function(el) {
if (el === self) return;
el = el || self.parent;
el.forDescendants(function(el) {
if (el.type !== 'radio-button' || el === self) {
return;
}
el.uncheck();
});
});

View File

@ -191,7 +191,8 @@ var output = blessed.scrollabletext({
left: 0,
top: 20,
height: 5,
width: '100%',
left: 0,
right: 0,
bg: 'red',
content: 'foobar'
});