clearPos. fix textarea.

This commit is contained in:
Christopher Jeffrey 2013-07-04 01:50:50 -05:00
parent 06400d089f
commit e7e8216611
2 changed files with 64 additions and 102 deletions

View File

@ -446,6 +446,7 @@ A box which allows text input.
keyboard. takes a callback which receives the final value.
- **readEditor(callback)** - open text editor in `$EDITOR`, read the output from
the resulting file. takes a callback which receives the final value.
- **clearInput** - clear input.
#### Textarea (from Input/ScrollableText)
@ -471,6 +472,7 @@ A box which allows multiline text input.
keyboard. takes a callback which receives the final value.
- **readEditor(callback)** - open text editor in `$EDITOR`, read the output from
the resulting file. takes a callback which receives the final value.
- **clearInput** - clear input.
#### Button (from Input)

View File

@ -142,14 +142,12 @@ Node.prototype.remove = function(element) {
// el._detached = true;
//});
//var ret = this._lastPos;
//if (ret) this.screen.clearRegion(ret.xi, ret.xl, ret.yi, ret.yl);
// this.clearPos();
};
Node.prototype.detach = function() {
if (this.parent) this.parent.remove(this);
//var ret = this._lastPos;
//if (ret) this.screen.clearRegion(ret.xi, ret.xl, ret.yi, ret.yl);
// this.clearPos();
};
Node.prototype.emitDescendants = function() {
@ -1023,29 +1021,6 @@ function Element(options) {
self.parseContent();
});
// if (this.options.hoverBg != null) {
// var hoverBg = convert(this.options.hoverBg);
//
// this.on('mouseover', function() {
// // XXX Possibly a better alternative for the below workaround.
// self._bg = self.bg;
// //if (self._bg == null) self._bg = self.bg;
// self.bg = hoverBg;
// self.screen.render();
// });
//
// this.on('mouseout', function() {
// // XXX Workaround
// // if (self.parent.type === 'list'
// // && self === self.parent.items[self.parent.selected]
// // && self.bg === self.parent.selectedBg) {
// // return;
// // }
// if (self._bg != null) self.bg = self._bg;
// self.screen.render();
// });
// }
if (this.options.hoverBg != null) {
this.options.hoverEffects = this.options.hoverEffects || {};
this.options.hoverEffects.bg = this.options.hoverBg;
@ -1121,11 +1096,7 @@ Element.prototype.onScreenEvent = function(type, listener) {
Element.prototype.hide = function() {
if (this.hidden) return;
this.hidden = true;
//var ret = this.render(true);
var ret = this._lastPos;
if (ret) {
this.screen.clearRegion(ret.xi, ret.xl, ret.yi, ret.yl);
}
this.clearPos();
this.emit('hide');
//if (this.screen.focused === this) {
// this.screen.focusPop();
@ -1156,12 +1127,9 @@ Element.prototype.focus = function() {
};
Element.prototype.setContent = function(content, noClear) {
var ret = this._lastPos;
this.content = content || '';
this.parseContent();
if (ret && !noClear) {
this.screen.clearRegion(ret.xi, ret.xl, ret.yi, ret.yl);
}
if (!noClear) this.clearPos();
};
Element.prototype.parseContent = function() {
@ -1222,6 +1190,20 @@ Element.prototype.__defineGetter__('detached', function() {
return false;
});
Element.prototype.key = function(key, listener) {
return this.on('key ' + key, listener);
};
Element.prototype.clearPos = function() {
if (this._lastPos && !this._lastPos.cleared) {
// optimize by making sure we only clear once.
this._lastPos.cleared = true;
this.screen.clearRegion(
this._lastPos.xi, this._lastPos.xl,
this._lastPos.yi, this._lastPos.yl);
}
};
/**
* Positioning
*/
@ -1392,9 +1374,7 @@ Element.prototype.__defineSetter__('left', function(val) {
val -= this.parent.left;
if (this.position.left === val) return;
this.emit('move');
this.screen.clearRegion(
this.left, this.left + this.width,
this.top, this.top + this.height);
this.clearPos();
return this.options.left = this.position.left = val;
});
@ -1407,9 +1387,7 @@ Element.prototype.__defineSetter__('right', function(val) {
val -= this.parent.right;
if (this.position.right === val) return;
this.emit('move');
this.screen.clearRegion(
this.left, this.left + this.width,
this.top, this.top + this.height);
this.clearPos();
//if (this.options.right == null) {
// return this.options.left = this.position.left = this.screen.width - 1 - val;
//}
@ -1425,9 +1403,7 @@ Element.prototype.__defineSetter__('top', function(val) {
val -= this.parent.top;
if (this.position.top === val) return;
this.emit('move');
this.screen.clearRegion(
this.left, this.left + this.width,
this.top, this.top + this.height);
this.clearPos();
return this.options.top = this.position.top = val;
});
@ -1440,9 +1416,7 @@ Element.prototype.__defineSetter__('bottom', function(val) {
val -= this.parent.bottom;
if (this.position.bottom === val) return;
this.emit('move');
this.screen.clearRegion(
this.left, this.left + this.width,
this.top, this.top + this.height);
this.clearPos();
//if (this.options.bottom == null) {
// return this.options.top = this.position.top = this.screen.height - 1 - val;
//}
@ -1452,36 +1426,28 @@ Element.prototype.__defineSetter__('bottom', function(val) {
Element.prototype.__defineSetter__('width', function(val) {
if (this.position.width === val) return;
this.emit('resize');
this.screen.clearRegion(
this.left, this.left + this.width,
this.top, this.top + this.height);
this.clearPos();
return this.options.width = this.position.width = val;
});
Element.prototype.__defineSetter__('height', function(val) {
if (this.position.height === val) return;
this.emit('resize');
this.screen.clearRegion(
this.left, this.left + this.width,
this.top, this.top + this.height);
this.clearPos();
return this.options.height = this.position.height = val;
});
Element.prototype.__defineSetter__('rleft', function(val) {
if (this.position.left === val) return;
this.emit('move');
this.screen.clearRegion(
this.left, this.left + this.width,
this.top, this.top + this.height);
this.clearPos();
return this.options.left = this.position.left = val;
});
Element.prototype.__defineSetter__('rright', function(val) {
if (this.position.right === val) return;
this.emit('move');
this.screen.clearRegion(
this.left, this.left + this.width,
this.top, this.top + this.height);
this.clearPos();
//if (this.options.right == null) {
// return this.options.left = this.position.left = this.parent.width - 1 - val;
//}
@ -1491,33 +1457,20 @@ Element.prototype.__defineSetter__('rright', function(val) {
Element.prototype.__defineSetter__('rtop', function(val) {
if (this.position.top === val) return;
this.emit('move');
// if (this._lastPos) {
// this.screen.clearRegion(
// this._lastPos.xi, this._lastPos.xl,
// this._lastPos.yi, this._lastPos.yl);
// }
this.screen.clearRegion(
this.left, this.left + this.width,
this.top, this.top + this.height);
this.clearPos();
return this.options.top = this.position.top = val;
});
Element.prototype.__defineSetter__('rbottom', function(val) {
if (this.position.bottom === val) return;
this.emit('move');
this.screen.clearRegion(
this.left, this.left + this.width,
this.top, this.top + this.height);
this.clearPos();
//if (this.options.bottom == null) {
// return this.options.top = this.position.top = this.parent.height - 1 - val;
//}
return this.options.bottom = this.position.bottom = val;
});
Element.prototype.key = function(key, listener) {
return this.on('key ' + key, listener);
};
/**
* Box
*/
@ -1734,6 +1687,7 @@ outer:
}
continue;
}
// if (ch < ' ') ch = ' ';
if (attr !== cell[0] || ch !== cell[1]) {
lines[yi][xi][0] = attr;
@ -2526,6 +2480,11 @@ Textbox.prototype._listener = function(ch, key) {
this.screen.render();
};
Textbox.prototype.clearInput = function() {
this.value = '';
this.setContent('');
};
Textbox.prototype.submit = function() {
return this._listener(null, { name: 'enter' });
};
@ -2602,17 +2561,24 @@ Textarea.prototype.type = 'textarea';
Textarea.prototype.updateCursor = function() {
if (this.screen.focused !== this) return;
/*
var clen = this._clines.length;
var last = this._clines[this._clines.length-1];
if (last.length === this.width - (this.border ? 2 : 0)) {
//if (!(this._clines.length - this.childBase >= this.height - (this.border ? 2 : 0))) {
last = '';
clen++;
}
*/
var _clines = this.value.split('\n');
var clen = _clines.length;
var last = _clines[_clines.length-1];
var line = Math.min(
clen - 1 - this.childBase,
this.height - (this.border ? 2 : 0));
this.height - (this.border ? 2 : 0) - 1);
this.screen.program.cup(
this.top + 1 + (this.border ? 1 : 0) + line,
@ -2665,7 +2631,6 @@ Textarea.prototype._listener = function(ch, key) {
;
}
// if (key.name === 'escape' || key.name === 'enter') {
if (key.name === 'escape') {
delete this._callback;
this.removeListener('keypress', this.__listener);
@ -2674,46 +2639,35 @@ Textarea.prototype._listener = function(ch, key) {
} else if (key.name === 'backspace') {
if (this.value.length) {
this.value = this.value.slice(0, -1);
/*
var last = this._clines[this._clines.length-1];
if (last.length === this.width - (this.border ? 2 : 0)) {
last = '';
}
if (last.length === 0) {
this.screen.program.cuu();
this.screen.program.cuf(this.width - (this.border ? 2 : 0));
} else {
this.screen.program.cub();
}
*/
}
} else {
if (ch) {
this.value += ch;
/*
var last = this._clines[this._clines.length-1];
if (last.length === this.width - (this.border ? 2 : 0)) {
last = '';
}
if (last.length < this.width - (this.border ? 2 : 0) - 1) {
this.screen.program.cuf();
} else {
this.screen.program.cud();
this.screen.program.cub(this.width - (this.border ? 2 : 0));
}
*/
}
}
if (this.value !== value) {
this.setContent(this.value);
this._typeScroll();
this.updateCursor();
this.screen.render();
}
};
Textarea.prototype._typeScroll = function() {
// XXX Workaround
if (this._clines.length - this.childBase > this.height - (this.border ? 2 : 0)) {
this.setContent(this.value + '\n');
this.scroll(this._clines.length);
}
};
Textarea.prototype.clearInput = function() {
this.value = '';
this.setContent('');
};
Textarea.prototype.submit = function() {
// return this._listener(null, { name: 'enter' });
return this._listener('\x1b', { name: 'escape' });
};
@ -2732,6 +2686,7 @@ Textarea.prototype.setEditor = function(callback) {
if (err) return callback(err);
self.value = value;
self.setContent(self.value);
self._typeScroll();
self.screen.render();
return self.setInput(callback);
});
@ -2972,6 +2927,11 @@ function sp(line, width, align) {
}
// TODO: Add text padding.
// TODO: Fix a bug where, in a box with a width of 3, `jjj` is:
// |jjj|
// But `jjjj` is:
// |jj |
// |jj |
function wrapContent(content, width, tags, state) {
var lines = content.split('\n')
, out = [];