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

View File

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