add wrapContent to Element prototype.

This commit is contained in:
Christopher Jeffrey 2013-07-18 02:37:39 -05:00
parent 42351724d3
commit 3c42a91fc5

View File

@ -1595,10 +1595,9 @@ Element.prototype.parseContent = function() {
// this._oclines = content.split('\n');
// Could move these 2 lines back to setContent (?)
content = content.replace(/\x1b(?!\[[\d;]*m)/g, '');
content = this._parseTags(content || '');
content = this._parseTags(content);
content = content.replace(/\t/g, ' ');
this._clines = wrapContent(content, width,
this.parseTags, this.align, this.type === 'textarea');
this._clines = this._wrapContent(content, width);
this._clines.width = width;
this._clines.content = this.content;
this._pcontent = this._clines.join('\n');
@ -1628,8 +1627,7 @@ Element.prototype._parseTags = function(text) {
});
};
function sp(line, width, align) {
Element.prototype._align = function(line, width, align) {
if (!align) return line;
var len = line.replace(/\x1b\[[\d;]*m/g, '').length
@ -1647,33 +1645,25 @@ function sp(line, width, align) {
}
return line;
}
};
Element.prototype._wrapContent = function(content, width) {
var el = this
, tags = this.parseTags
, state = this.align
, margin = 0;
// 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 |
// A possibly related bug:
// For some reason (see jitsu-ui):
// {red-fg}my-app2{/red-fg} gets wrapped to:
// {red-fg}my-app\n2{/red-fg} when my-app2
// does not. Since escape codes are not printable
// characters, this means wrapContent is doing
// something wrong and determining length including
// at least 1 char from the escape code.
function wrapContent(content, width, tags, state, margin) {
var lines = content.split('\n')
, out = [];
if (!content) {
out.push(content || '');
out.push(content);
return out;
}
// Useful for textareas.
if (margin && width > 1) width--;
if (el.scrollbar) margin++;
if (el.type === 'textarea') margin++;
if (width > margin) width -= margin;
lines.forEach(function(line) {
var align = state
@ -1739,10 +1729,10 @@ function wrapContent(content, width, tags, state, margin) {
if (esc) {
part = part.slice(0, -esc[0].length);
line = line.substring(i - 1 - esc[0].length);
out.push(sp(part, width, align));
out.push(el._align(part, width, align));
} else {
line = line.substring(i - 1);
out.push(sp(part, width, align));
out.push(el._align(part, width, align));
}
// Make sure we didn't wrap the line to the very end, otherwise
@ -1756,13 +1746,11 @@ function wrapContent(content, width, tags, state, margin) {
return;
}
out.push(sp(line, width, align));
out.push(el._align(line, width, align));
});
return out;
}
};
Element.prototype.__defineGetter__('visible', function() {
var el = this;