possibly a better fix for form focusing. see #38.

This commit is contained in:
Christopher Jeffrey 2014-03-30 03:55:56 -05:00
parent d624fe0c45
commit 7965ee581c

View File

@ -4382,36 +4382,23 @@ 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 = [];
var out = [];
this.children.forEach(function fn(el) {
if (el.keyable) out.push(el);
el.children.forEach(fn);
});
this.children.forEach(function fn(el) {
if (el.keyable && el.visible) out.push(el);
el.children.forEach(fn);
});
this._children = out;
}
};
Form.prototype._visible = function() {
return !!this._children.filter(function(el) {
return el.visible;
}).length;
this._children = out;
};
Form.prototype.next = function() {
this._refresh();
if (!this._visible()) return;
if (!this._children.length) return;
if (!this._selected) {
this._selected = this._children[0];
if (!this._selected.visible) return this.next();
// return this._selected;
if (this.screen.focused !== this._selected) return this._selected;
}
@ -4419,23 +4406,20 @@ Form.prototype.next = function() {
var i = this._children.indexOf(this._selected);
if (!~i || !this._children[i + 1]) {
this._selected = this._children[0];
if (!this._selected.visible) return this.next();
return this._selected;
}
this._selected = this._children[i + 1];
if (!this._selected.visible) return this.next();
return this._selected;
};
Form.prototype.previous = function() {
this._refresh();
if (!this._visible()) return;
if (!this._children.length) return;
if (!this._selected) {
this._selected = this._children[this._children.length - 1];
if (!this._selected.visible) return this.previous();
// return this._selected;
if (this.screen.focused !== this._selected) return this._selected;
}
@ -4443,12 +4427,10 @@ Form.prototype.previous = function() {
var i = this._children.indexOf(this._selected);
if (!~i || !this._children[i - 1]) {
this._selected = this._children[this._children.length - 1];
if (!this._selected.visible) return this.previous();
return this._selected;
}
this._selected = this._children[i - 1];
if (!this._selected.visible) return this.previous();
return this._selected;
};