major cleanup. remove old code.

This commit is contained in:
Christopher Jeffrey 2013-06-20 07:18:04 -05:00
parent f431e98c03
commit 4f0f9e53d7
2 changed files with 69 additions and 167 deletions

View File

@ -146,6 +146,8 @@ The base node which everything inherits from.
- **append(node)** - append a node to this node's children. - **append(node)** - append a node to this node's children.
- **remove(node)** - remove child node from node. - **remove(node)** - remove child node from node.
- **detach()** - remove node from its parent. - **detach()** - remove node from its parent.
- **emitDescendants(type, args..., [iterator])** - emit event for element, and
recursively emit same event for all descendants.
#### Screen (from Node) #### Screen (from Node)

View File

@ -68,6 +68,10 @@ Node.prototype.prepend = function(element) {
el.emit('attach'); el.emit('attach');
if (el.children) el.children.forEach(emit); if (el.children) el.children.forEach(emit);
})(element); })(element);
//element.emitDescendants('attach', function(el) {
// el._detached = false;
//});
}; };
Node.prototype.append = function(element) { Node.prototype.append = function(element) {
@ -92,6 +96,10 @@ Node.prototype.append = function(element) {
el.emit('attach'); el.emit('attach');
if (el.children) el.children.forEach(emit); if (el.children) el.children.forEach(emit);
})(element); })(element);
//element.emitDescendants('attach', function(el) {
// el._detached = false;
//});
}; };
Node.prototype.remove = function(element) { Node.prototype.remove = function(element) {
@ -121,12 +129,33 @@ Node.prototype.remove = function(element) {
el.emit('detach'); el.emit('detach');
if (el.children) el.children.forEach(emit); if (el.children) el.children.forEach(emit);
})(element); })(element);
//element.emitDescendants('detach', function(el) {
// el._detached = true;
//});
}; };
Node.prototype.detach = function(element) { Node.prototype.detach = function(element) {
this.parent.remove(element); this.parent.remove(element);
}; };
Node.prototype.emitDescendants = function() {
var args = Array.prototype.slice(arguments)
, iter;
if (typeof args[args.length-1] === 'function') {
iter = args.pop();
}
(function emit(el) {
if (iter) iter(el);
el.emit.apply(el, args);
if (el.children) {
el.children.forEach(emit);
}
})(this);
};
/** /**
* Screen * Screen
*/ */
@ -735,7 +764,9 @@ function Element(options) {
this.parseTags = options.parseTags || options.tags; this.parseTags = options.parseTags || options.tags;
// TODO: Maybe simply set _pcontent with _parseTags result. // TODO: Maybe simply set _pcontent with _parseTags result.
this.content = this._parseTags(options.content || ''); //this.content = this._parseTags(options.content || '');
this.setContent(options.content || '');
if (options.label) { if (options.label) {
this.append(new Box({ this.append(new Box({
@ -777,15 +808,7 @@ function Element(options) {
self.parseContent(); self.parseContent();
}); });
this.parseContent(); //this.parseContent();
//if (this.parent) {
// this.parseContent();
//} else {
// this.once('reparent', function() {
// self.parseContent();
// });
//}
} }
Element.prototype.__proto__ = Node.prototype; Element.prototype.__proto__ = Node.prototype;
@ -808,17 +831,19 @@ Element.prototype.emit = function(type) {
*/ */
Element.prototype.parseContent = function() { Element.prototype.parseContent = function() {
if (!this.content) return; if (this.detached) return false;
if (this.detached) return;
var w = this.width - (this.border ? 2 : 0); var w = this.width - (this.border ? 2 : 0);
if (this._clines == null if (this._clines == null
|| this._clines.width !== w || this._clines.width !== w
|| this._clines.content !== this.content) { || this._clines.content !== this.content) {
this._clines = wrapContent(this.content, w, this.align); this._clines = wrapContent(this.content, w, this.align);
this._clines.width = w;
this._clines.content = this.content;
this._pcontent = this._clines.join('\n'); this._pcontent = this._clines.join('\n');
this.emit('parsed content');
return true;
} }
return false;
}; };
Element.prototype.hide = function() { Element.prototype.hide = function() {
@ -858,14 +883,14 @@ Element.prototype.focus = function() {
this.screen.emit('element focus', old, this); this.screen.emit('element focus', old, this);
}; };
Element.prototype.setContent = function(content) { Element.prototype.setContent = function(content, noClear) {
//var ret = this.render(true); //var ret = this.render(true);
var ret = this._lastPos; var ret = this._lastPos;
// TODO: Maybe simply set _pcontent with _parseTags result. // TODO: Maybe simply set _pcontent with _parseTags result.
// text = text.replace(/\x1b(?!\[[\d;]*m)/g, ''); // text = text.replace(/\x1b(?!\[[\d;]*m)/g, '');
this.content = this._parseTags(content || ''); this.content = this._parseTags(content || '');
this.parseContent(); this.parseContent();
if (ret) { if (ret && !noClear) {
//if (ret && !this.hidden) { //if (ret && !this.hidden) {
this.screen.clearRegion(ret.xi, ret.xl, ret.yi, ret.yl); this.screen.clearRegion(ret.xi, ret.xl, ret.yi, ret.yl);
} }
@ -1213,23 +1238,12 @@ Box.prototype.__proto__ = Element.prototype;
// TODO: Optimize. Move elsewhere. // TODO: Optimize. Move elsewhere.
Box.prototype._getShrinkSize = function(content) { Box.prototype._getShrinkSize = function(content) {
if (this._clines) {
return { return {
height: this._clines.length, height: this._clines.length,
width: this._clines.reduce(function(current, line) { width: this._clines.reduce(function(current, line) {
line = line.replace(/\x1b\[[\d;]*m/g, ''); line = line.replace(/\x1b\[[\d;]*m/g, '');
return line.length > current return line.length > current
? line.length //+ (lines.length > 1 ? 1 : 0) // for newlines ? line.length
: current;
}, 0)
};
}
var lines = content.replace(/\x1b\[[\d;]*m/g, '').split('\n');
return {
height: lines.length,
width: lines.reduce(function(current, line) {
return line.length > current
? line.length //+ (lines.length > 1 ? 1 : 0) // for newlines
: current; : current;
}, 0) }, 0)
}; };
@ -1242,6 +1256,8 @@ Box.prototype.render = function(stop) {
// NOTE: Maybe move this `hidden` check down below `stop` check and return `ret`. // NOTE: Maybe move this `hidden` check down below `stop` check and return `ret`.
if (this.hidden) return; if (this.hidden) return;
this.parseContent();
var lines = this.screen.lines var lines = this.screen.lines
, xi_ = this.left , xi_ = this.left
, xi , xi
@ -1282,86 +1298,6 @@ Box.prototype.render = function(stop) {
} }
} }
// NOTE: Could simply change some offsets around and move this below the
// shrink block. Could also stop passing a string to _getShrinkSize.
/*
if (this.align === 'center' || this.align === 'right') {
content = '{' + this.align + '}' + content + '{/' + this.align + '}';
} else
*/
/*
if (this.align === 'center' || this.align === 'right') {
var ncontent = content.replace(/\x1b\[[\d;]*m/g, '')
, width = this.width - (this.border ? 2 : 0) - this.padding * 2;
if (ncontent.length < width) {
var s = width - ncontent.length;
if (this.align === 'center') {
s = Array(((s / 2 | 0) - (this.border ? 1 : 0)) + 1).join(' ');
content = s + content + (this.shrink ? s : '');
//s = (s / 2 | 0) - (this.border ? 1 : 0);
//ci -= s;
//xl += s * 2;
} else {
s -= this.left; // this shouldn't be necessary
s = Array((s - (this.border ? 2 : 0)) + 1).join(' ');
content = s + content;
//s = s - (this.border ? 2 : 0);
//ci -= s;
//xl += s;
}
}
}
*/
//if (!this._pcontent && this.content) {
// this._clines = wrapContent(this.content, this.width - (this.border ? 2 : 0));
// this._pcontent = this._clines.join('\n');
// content = this._pcontent;
// //content = wrapContent(this.content, this.width - (this.border ? 2 : 0)).join('\n');
//}
/*
if (this.tags || this.align) {
var self = this;
content = content.replace(/(^|\n){(center|right)}([^\0]+?){\/\2}(?=\n|$)/g, function(_, start, align, text) {
var lines = text.trim().split('\n')
, line
, i = 0
, width = self.width - (self.border ? 2 : 0) - self.padding * 2
, out = ''
, len
, s;
for (; i < lines.length; i++) {
line = lines[i];
len = line.replace(/\x1b\[[\d;]*m/g, '').length;
s = width - len;
if (s < 0) {
out += '\n' + line;
continue;
}
if (align === 'center') {
s = Array(((s / 2 | 0) - (self.border ? 1 : 0)) + 1).join(' ');
out += '\n' + s + line + (self.shrink ? s : '');
} else {
s -= self.left; // this shouldn't be necessary
s = Array((s - (self.border ? 2 : 0)) + 1).join(' ');
out += '\n' + s + line;
}
}
if (out[0] === '\n' && !start) {
out = out.substring(1);
}
return out;
});
}
*/
// TODO: Check for 'center', recalculate yi, and xi. Better yet, simply // TODO: Check for 'center', recalculate yi, and xi. Better yet, simply
// move this check into this.left/width/etc. // move this check into this.left/width/etc.
if (this.shrink) { if (this.shrink) {
@ -1375,16 +1311,14 @@ Box.prototype.render = function(stop) {
xl = xi_ + w + (this.border ? 2 : 0) + this.padding; xl = xi_ + w + (this.border ? 2 : 0) + this.padding;
//xl++; // make it one cell wider for newlines //xl++; // make it one cell wider for newlines
} }
if (this.childBase == null) {
if (this.options.top == null && this.options.bottom != null) { if (this.options.top == null && this.options.bottom != null) {
yi_ = yl - h - (this.border ? 2 : 0) - this.padding; yi_ = yl - h - (this.border ? 2 : 0) - this.padding;
} else { } else {
yl = yi_ + h + (this.border ? 2 : 0) + this.padding; yl = yi_ + h + (this.border ? 2 : 0) + this.padding;
} }
} }
}
//if (this.options.vshrink && this._clines) {
// yl = yi_ + this._clines.length + (this.border ? 2 : 0) + this.padding;
//}
var ret = this._lastPos = { var ret = this._lastPos = {
xi: xi_, xi: xi_,
@ -1936,29 +1870,9 @@ function ScrollableText(options) {
}); });
} }
/* this.on('parsed content', function() {
this.screen.on('resize', function() {
self._recalculateIndex(); self._recalculateIndex();
}); });
this.on('resize', function() {
self._recalculateIndex();
});
//this._clines = [];
//this._pcontent = '';
this.contentIndex = 0;
// Not technically necessary, but can't hurt.
this._recalculateIndex();
if (!this.parent) {
// Not technically necessary, but can't hurt.
this.once('reparent', function() {
self._recalculateIndex();
});
}
*/
} }
ScrollableText.prototype.__proto__ = ScrollableBox.prototype; ScrollableText.prototype.__proto__ = ScrollableBox.prototype;
@ -1980,13 +1894,7 @@ ScrollableText.prototype.scroll = function(offset) {
// feeds. This allows us to take preformatted text output from other programs // feeds. This allows us to take preformatted text output from other programs
// and put it in a scrollable text box. // and put it in a scrollable text box.
if (this.content != null) { if (this.content != null) {
this._parseContent(); this.parseContent();
//w = this.width - (this.border ? 2 : 0);
//if (this._clines == null || this._clines.width !== w) {
// this._clines = wrapContent(this.content, w);
// this._clines.content = this.content;
// this._pcontent = this._clines.join('\n');
//}
max = this._clines.length - 1 - (this.height - (this.border ? 2 : 0)); max = this._clines.length - 1 - (this.height - (this.border ? 2 : 0));
if (max < 0) max = 0; if (max < 0) max = 0;
@ -2006,17 +1914,8 @@ ScrollableText.prototype.scroll = function(offset) {
return ret; return ret;
}; };
ScrollableText.prototype._setContent = ScrollableText.prototype.setContent;
ScrollableText.prototype.setContent = function(content) {
var ret = this._setContent(content);
//this._recalculateIndex();
return ret;
};
ScrollableText.prototype._recalculateIndex = function() { ScrollableText.prototype._recalculateIndex = function() {
if (this.detached) return; if (this.detached) return;
//this._clines = wrapContent(this.content, this.width - (this.border ? 2 : 0));
//this._pcontent = this._clines.join('\n');
var max = this._clines.length - 1 - (this.height - (this.border ? 2 : 0)); var max = this._clines.length - 1 - (this.height - (this.border ? 2 : 0));
if (max < 0) max = 0; if (max < 0) max = 0;
@ -2031,14 +1930,6 @@ ScrollableText.prototype._recalculateIndex = function() {
this.contentIndex = t; this.contentIndex = t;
}; };
ScrollableText.prototype._parseContent = ScrollableText.prototype.parseContent;
ScrollableText.prototype.parseContent = function(content) {
var ret = this._parseContent();
this._recalculateIndex();
return ret;
};
/** /**
* Input * Input
*/ */
@ -2139,10 +2030,11 @@ Textbox.prototype._listener = function(ch, key) {
Textbox.prototype._render = Input.prototype.render; Textbox.prototype._render = Input.prototype.render;
Textbox.prototype.render = function(stop) { Textbox.prototype.render = function(stop) {
var content = this.content; var content = this.content;
// NOTE: This workaround will never work with _pcontent. //this.content = this.content.slice(-(this.width - (this.border ? 2 : 0) - 1));
this.content = this.content.slice(-(this.width - (this.border ? 2 : 0) - 1)); this.setContent(content.slice(-(this.width - (this.border ? 2 : 0) - 1)), true);
var ret = this._render(stop); var ret = this._render(stop);
this.content = content; //this.content = content;
this.setContent(content, true);
if (stop) return ret; if (stop) return ret;
return ret; return ret;
}; };
@ -2451,6 +2343,13 @@ function wrapContent(content, width, state) {
var lines = content.split('\n') var lines = content.split('\n')
, out = []; , out = [];
if (!content) {
out.width = width;
out.content = content || '';
out.push(content || '');
return out;
}
lines.forEach(function(line) { lines.forEach(function(line) {
var align = state var align = state
, cap; , cap;
@ -2517,6 +2416,7 @@ function wrapContent(content, width, state) {
}); });
out.width = width; out.width = width;
out.content = content;
return out; return out;
} }