more textbox/textarea refactoring.

This commit is contained in:
Christopher Jeffrey 2013-07-24 18:20:30 -05:00
parent c1f17e866c
commit 633b76cec5
1 changed files with 22 additions and 51 deletions

View File

@ -4049,6 +4049,7 @@ function Textbox(options) {
this.on('resize', this.__updateCursor); this.on('resize', this.__updateCursor);
this.on('move', this.__updateCursor); this.on('move', this.__updateCursor);
//this.on('prerender', this._render.bind(this)); //this.on('prerender', this._render.bind(this));
//this.on('focus', self.readInput.bind(this));
} }
Textbox.prototype.__proto__ = Input.prototype; Textbox.prototype.__proto__ = Input.prototype;
@ -4061,6 +4062,14 @@ Textbox.prototype.setInput = function(callback) {
var self = this var self = this
, focused = this.screen.focused === this; , focused = this.screen.focused === this;
if (this._callback) return;
// We could just return here, but:
// If we enable focus-read, this is necessary for
// legacy programs which manually bind to focus.
//if (this._callback) return this._callbacks.push(callback);
//this._callbacks = callback ? [callback] : [];
if (!focused) { if (!focused) {
this.screen.saveFocus(); this.screen.saveFocus();
this.focus(); this.focus();
@ -4097,6 +4106,16 @@ Textbox.prototype.setInput = function(callback) {
} }
self.emit('action', value); self.emit('action', value);
// We could just execute the callback here, but:
// If we enable focus-read, this is necessary for
// legacy programs which manually bind to focus.
//self._callbacks.forEach(function(callback) {
// return err
// ? callback(err)
// : callback(null, value);
//});
//delete self._callbacks;
if (!callback) return; if (!callback) return;
return err return err
@ -4205,6 +4224,8 @@ function Textarea(options) {
this.__updateCursor = this.updateCursor.bind(this); this.__updateCursor = this.updateCursor.bind(this);
this.on('resize', this.__updateCursor); this.on('resize', this.__updateCursor);
this.on('move', this.__updateCursor); this.on('move', this.__updateCursor);
//this.on('prerender', this._render.bind(this));
//this.on('focus', self.readInput.bind(this));
} }
Textarea.prototype.__proto__ = ScrollableText.prototype; Textarea.prototype.__proto__ = ScrollableText.prototype;
@ -4264,57 +4285,7 @@ Textarea.prototype.updateCursor = function() {
Textarea.prototype.input = Textarea.prototype.input =
Textarea.prototype.readInput = Textarea.prototype.readInput =
Textarea.prototype.setInput = function(callback) { Textarea.prototype.setInput = Textbox.prototype.setInput;
var self = this
, focused = this.screen.focused === this;
if (!focused) {
this.screen.saveFocus();
this.focus();
}
this.screen.grabKeys = true;
this.updateCursor();
this.screen.program.showCursor();
this.screen.program.sgr('normal');
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;
if (!focused) {
self.screen.restoreFocus();
}
if (err) {
self.emit('error', err);
} else if (value != null) {
self.emit('submit', value);
} else {
self.emit('cancel', value);
}
self.emit('action', value);
if (!callback) return;
return err
? callback(err)
: callback(null, value);
};
this.__listener = this._listener.bind(this);
this.on('keypress', this.__listener);
this.on('blur', this._callback);
};
Textarea.prototype._listener = function(ch, key) { Textarea.prototype._listener = function(ch, key) {
var callback = this._callback var callback = this._callback