From d624fe0c4516b681ec308586abd85fd32ae5b682 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 30 Mar 2014 03:51:54 -0500 Subject: [PATCH] fix call stack overflow on form focusing. fixes #38. --- lib/widget.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/widget.js b/lib/widget.js index 5d99e45..5b178aa 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -4382,6 +4382,10 @@ Form.prototype.__proto__ = Box.prototype; Form.prototype.type = 'form'; Form.prototype._refresh = function() { + // XXX Possibly remove this if statement and refresh on every focus. + // Also potentially only include *visible* focusable elements. + // This would remove the need to check for _selected.visible in previous() + // and next(). if (!this._children) { var out = []; @@ -4394,9 +4398,17 @@ Form.prototype._refresh = function() { } }; +Form.prototype._visible = function() { + return !!this._children.filter(function(el) { + return el.visible; + }).length; +}; + Form.prototype.next = function() { this._refresh(); + if (!this._visible()) return; + if (!this._selected) { this._selected = this._children[0]; if (!this._selected.visible) return this.next(); @@ -4419,6 +4431,8 @@ Form.prototype.next = function() { Form.prototype.previous = function() { this._refresh(); + if (!this._visible()) return; + if (!this._selected) { this._selected = this._children[this._children.length - 1]; if (!this._selected.visible) return this.previous(); @@ -4439,11 +4453,13 @@ Form.prototype.previous = function() { }; Form.prototype.focusNext = function() { - this.next().focus(); + var next = this.next(); + if (next) next.focus(); }; Form.prototype.focusPrevious = function() { - this.previous().focus(); + var previous = this.previous(); + if (previous) previous.focus(); }; Form.prototype.resetSelected = function() { @@ -4452,12 +4468,12 @@ Form.prototype.resetSelected = function() { Form.prototype.focusFirst = function() { this.resetSelected(); - this.next().focus(); + this.focusNext(); }; Form.prototype.focusLast = function() { this.resetSelected(); - this.previous().focus(); + this.focusPrevious(); }; Form.prototype.submit = function() {