From 633b76cec5edb9a397a26e127023b059ca713dc7 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 24 Jul 2013 18:20:30 -0500 Subject: [PATCH] more textbox/textarea refactoring. --- lib/widget.js | 73 ++++++++++++++++----------------------------------- 1 file changed, 22 insertions(+), 51 deletions(-) diff --git a/lib/widget.js b/lib/widget.js index 8a47ad1..c4c1cac 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -4049,6 +4049,7 @@ function Textbox(options) { this.on('resize', this.__updateCursor); this.on('move', this.__updateCursor); //this.on('prerender', this._render.bind(this)); + //this.on('focus', self.readInput.bind(this)); } Textbox.prototype.__proto__ = Input.prototype; @@ -4061,6 +4062,14 @@ Textbox.prototype.setInput = function(callback) { var self = 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) { this.screen.saveFocus(); this.focus(); @@ -4097,6 +4106,16 @@ Textbox.prototype.setInput = function(callback) { } 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; return err @@ -4205,6 +4224,8 @@ function Textarea(options) { this.__updateCursor = this.updateCursor.bind(this); this.on('resize', 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; @@ -4264,57 +4285,7 @@ Textarea.prototype.updateCursor = function() { Textarea.prototype.input = Textarea.prototype.readInput = -Textarea.prototype.setInput = function(callback) { - 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.setInput = Textbox.prototype.setInput; Textarea.prototype._listener = function(ch, key) { var callback = this._callback