cleanup unnecesary code after terminal element addition.

This commit is contained in:
Christopher Jeffrey 2015-01-24 21:33:30 -08:00
parent 879a072353
commit 5bdfc18daf
2 changed files with 80 additions and 198 deletions

View File

@ -170,6 +170,10 @@ function reset() {
ball.angle = 'down'; ball.angle = 'down';
ball.rtop = 1; ball.rtop = 1;
ball.rleft = 1; ball.rleft = 1;
if ((score.lwins + score.rwins) % 2 !== 0) {
ball.direction = 'left';
ball.rleft = table.width - 1;
}
lpaddle.rtop = 0; lpaddle.rtop = 0;
rpaddle.rtop = 0; rpaddle.rtop = 0;
@ -207,7 +211,7 @@ function startGame() {
if (lpaddle.rbottom > 0) lpaddle.rtop += lpaddle.speed; if (lpaddle.rbottom > 0) lpaddle.rtop += lpaddle.speed;
if (!socket) if (rpaddle.rbottom > 0) rpaddle.rtop += rpaddle.speed; if (!socket) if (rpaddle.rbottom > 0) rpaddle.rtop += rpaddle.speed;
if (lpaddle.rbottom < 0) lpaddle.rtop = table.height - lpaddle.height - 1; if (lpaddle.rbottom < 0) lpaddle.rtop = table.height - lpaddle.height - 1;
if (rpaddle.rbottom < 0) rpaddle.rbottom = table.height - rpaddle.height - 1; if (rpaddle.rbottom < 0) rpaddle.rtop = table.height - rpaddle.height - 1;
screen.render(); screen.render();
} }
}); });

View File

@ -1497,7 +1497,7 @@ Screen.prototype.spawn = function(file, args, options) {
options.stdio = 'inherit'; options.stdio = 'inherit';
program.lsaveCursor('spawn'); program.lsaveCursor('spawn');
//program.csr(0, program.rows - 1); // program.csr(0, program.rows - 1);
program.normalBuffer(); program.normalBuffer();
program.showCursor(); program.showCursor();
if (mouse) program.disableMouse(); if (mouse) program.disableMouse();
@ -1516,7 +1516,7 @@ Screen.prototype.spawn = function(file, args, options) {
program.output.write = write; program.output.write = write;
program.alternateBuffer(); program.alternateBuffer();
//program.csr(0, program.rows - 1); // program.csr(0, program.rows - 1);
if (mouse) program.enableMouse(); if (mouse) program.enableMouse();
screen.alloc(); screen.alloc();
@ -1702,13 +1702,6 @@ function Element(options) {
height: options.height height: options.height
}; };
// if (options.position.left == null && options.position.right == null) {
// options.position.left = 0;
// }
// if (options.position.top == null && options.position.bottom == null) {
// options.position.top = 0;
// }
if (options.position.width === 'shrink' if (options.position.width === 'shrink'
|| options.position.height === 'shrink') { || options.position.height === 'shrink') {
if (options.position.width === 'shrink') { if (options.position.width === 'shrink') {
@ -1800,7 +1793,7 @@ function Element(options) {
// TODO: Possibly move this to Node for screen.on('mouse', ...). // TODO: Possibly move this to Node for screen.on('mouse', ...).
this.on('newListener', function fn(type) { this.on('newListener', function fn(type) {
//type = type.split(' ').slice(1).join(' '); // type = type.split(' ').slice(1).join(' ');
if (type === 'mouse' if (type === 'mouse'
|| type === 'click' || type === 'click'
|| type === 'mouseover' || type === 'mouseover'
@ -1836,18 +1829,16 @@ function Element(options) {
if (this.style.hover) { if (this.style.hover) {
options.hoverEffects = this.style.hover; options.hoverEffects = this.style.hover;
//delete this.style.hover;
} }
if (this.style.focus) { if (this.style.focus) {
options.focusEffects = this.style.focus; options.focusEffects = this.style.focus;
//delete this.style.focus;
} }
// if (options.effects) { if (options.effects) {
// if (options.effects.hover) options.hoverEffects = options.effects.hover; if (options.effects.hover) options.hoverEffects = options.effects.hover;
// if (options.effects.focus) options.focusEffects = options.effects.focus; if (options.effects.focus) options.focusEffects = options.effects.focus;
// } }
[['hoverEffects', 'mouseover', 'mouseout', '_htemp'], [['hoverEffects', 'mouseover', 'mouseout', '_htemp'],
['focusEffects', 'focus', 'blur', '_ftemp']].forEach(function(props) { ['focusEffects', 'focus', 'blur', '_ftemp']].forEach(function(props) {
@ -2556,9 +2547,9 @@ Element.prototype._getWidth = function(get) {
&& this.position.left !== 'center') { && this.position.left !== 'center') {
width -= this.parent.ileft; width -= this.parent.ileft;
} }
//if (this.position.right != null) { // if (this.position.right != null) {
width -= this.parent.iright; width -= this.parent.iright;
//} // }
} }
} }
@ -2600,9 +2591,9 @@ Element.prototype._getHeight = function(get) {
&& this.position.top !== 'center') { && this.position.top !== 'center') {
height -= this.parent.itop; height -= this.parent.itop;
} }
//if (this.position.bottom != null) { // if (this.position.bottom != null) {
height -= this.parent.ibottom; height -= this.parent.ibottom;
//} // }
} }
} }
@ -2652,9 +2643,9 @@ Element.prototype._getRight = function(get) {
if (this.position.right == null && this.position.left != null) { if (this.position.right == null && this.position.left != null) {
right = this.screen.cols - (this._getLeft(get) + this._getWidth(get)); right = this.screen.cols - (this._getLeft(get) + this._getWidth(get));
if (this.screen.autoPadding) { if (this.screen.autoPadding) {
//if (this.position.right != null) { // if (this.position.right != null) {
right += this.parent.iright; right += this.parent.iright;
//} // }
} }
return right; return right;
} }
@ -2662,9 +2653,9 @@ Element.prototype._getRight = function(get) {
right = (parent.right || 0) + (this.position.right || 0); right = (parent.right || 0) + (this.position.right || 0);
if (this.screen.autoPadding) { if (this.screen.autoPadding) {
//if (this.position.right != null) { // if (this.position.right != null) {
right += this.parent.iright; right += this.parent.iright;
//} // }
} }
return right; return right;
@ -2713,9 +2704,9 @@ Element.prototype._getBottom = function(get) {
if (this.position.bottom == null && this.position.top != null) { if (this.position.bottom == null && this.position.top != null) {
bottom = this.screen.rows - (this._getTop(get) + this._getHeight(get)); bottom = this.screen.rows - (this._getTop(get) + this._getHeight(get));
if (this.screen.autoPadding) { if (this.screen.autoPadding) {
//if (this.position.bottom != null) { // if (this.position.bottom != null) {
bottom += this.parent.ibottom; bottom += this.parent.ibottom;
//} // }
} }
return bottom; return bottom;
} }
@ -2723,9 +2714,9 @@ Element.prototype._getBottom = function(get) {
bottom = (parent.bottom || 0) + (this.position.bottom || 0); bottom = (parent.bottom || 0) + (this.position.bottom || 0);
if (this.screen.autoPadding) { if (this.screen.autoPadding) {
//if (this.position.bottom != null) { // if (this.position.bottom != null) {
bottom += this.parent.ibottom; bottom += this.parent.ibottom;
//} // }
} }
return bottom; return bottom;
@ -3068,7 +3059,9 @@ Element.prototype._getShrink = function(xi, xl, yi, yl, get) {
Element.prototype._getCoords = function(get, noscroll) { Element.prototype._getCoords = function(get, noscroll) {
if (this.hidden) return; if (this.hidden) return;
// if (this.parent._rendering) get = true; // if (this.parent._rendering) {
// get = true;
// }
var xi = this._getLeft(get) var xi = this._getLeft(get)
, xl = xi + this._getWidth(get) , xl = xi + this._getWidth(get)
@ -3209,10 +3202,10 @@ Element.prototype._getCoords = function(get, noscroll) {
} }
} }
//if (this.parent.lpos) { // if (this.parent.lpos) {
// this.parent.lpos._scrollBottom = Math.max( // this.parent.lpos._scrollBottom = Math.max(
// this.parent.lpos._scrollBottom, yl); // this.parent.lpos._scrollBottom, yl);
//} // }
return { return {
xi: xi, xi: xi,
@ -3493,9 +3486,13 @@ Element.prototype.render = function() {
if (el.screen._ci !== -1) { if (el.screen._ci !== -1) {
el.index = el.screen._ci++; el.index = el.screen._ci++;
} }
//if (el.screen._rendering) el._rendering = true; // if (el.screen._rendering) {
// el._rendering = true;
// }
el.render(); el.render();
//if (el.screen._rendering) el._rendering = false; // if (el.screen._rendering) {
// el._rendering = false;
// }
}); });
this._emit('render', [coords]); this._emit('render', [coords]);
@ -3753,22 +3750,9 @@ function Line(options) {
this.border = { this.border = {
type: 'bg', type: 'bg',
__proto__: this __proto__: this
// get ch() { return self.ch; },
// set ch(c) { return self.ch = c; }
}; };
this.style.border = this.style; this.style.border = this.style;
// Maybe instead of the above:
// this.on('prerender', function() {
// self._style = self.style;
// self.border = { type: 'bg', ch: self.ch };
// self.style = { border: self.style };
// });
//
// this.on('render', function(coords) {
// self.style = self._style;
// });
} }
Line.prototype.__proto__ = Box.prototype; Line.prototype.__proto__ = Box.prototype;
@ -3876,12 +3860,12 @@ function ScrollableBox(options) {
return; return;
} }
if (options.vi && key.name === 'g' && !key.shift) { if (options.vi && key.name === 'g' && !key.shift) {
self.scroll(-self._clines.length); self.scrollTo(0);
self.screen.render(); self.screen.render();
return; return;
} }
if (options.vi && key.name === 'g' && key.shift) { if (options.vi && key.name === 'g' && key.shift) {
self.scroll(self._clines.length); self.scrollTo(self._scrollBottom());
self.screen.render(); self.screen.render();
return; return;
} }
@ -4187,15 +4171,11 @@ function List(options) {
if (options.mouse) { if (options.mouse) {
this.screen._listenMouse(this); this.screen._listenMouse(this);
//this.screen.on('element wheeldown', function(el, data) {
this.on('element wheeldown', function(el, data) { this.on('element wheeldown', function(el, data) {
// if (el !== self && !el.hasAncestor(self)) return;
self.select(self.selected + 2); self.select(self.selected + 2);
self.screen.render(); self.screen.render();
}); });
//this.screen.on('element wheelup', function(el, data) {
this.on('element wheelup', function(el, data) { this.on('element wheelup', function(el, data) {
// if (el !== self && !el.hasAncestor(self)) return;
self.select(self.selected - 2); self.select(self.selected - 2);
self.screen.render(); self.screen.render();
}); });
@ -4291,8 +4271,8 @@ function List(options) {
this.on('resize', function() { this.on('resize', function() {
var visible = self.height - self.iheight; var visible = self.height - self.iheight;
// if (self.selected < visible - 1) {
if (visible >= self.selected + 1) { if (visible >= self.selected + 1) {
//if (self.selected < visible - 1) {
self.childBase = 0; self.childBase = 0;
self.childOffset = self.selected; self.childOffset = self.selected;
} else { } else {
@ -4468,8 +4448,11 @@ List.prototype.select = function(index) {
index = this.items.indexOf(index); index = this.items.indexOf(index);
} }
if (index < 0) index = 0; if (index < 0) {
else if (index >= this.items.length) index = this.items.length - 1; index = 0;
} else if (index >= this.items.length) {
index = this.items.length - 1;
}
if (this.selected === index && this._listInitialized) return; if (this.selected === index && this._listInitialized) return;
this._listInitialized = true; this._listInitialized = true;
@ -4502,9 +4485,10 @@ List.prototype.pick = function(label, callback) {
if (focused && focused._done) focused._done('stop'); if (focused && focused._done) focused._done('stop');
this.screen.saveFocus(); this.screen.saveFocus();
//var parent = this.parent; // XXX Keep above:
//this.detach(); // var parent = this.parent;
//parent.append(this); // this.detach();
// parent.append(this);
this.focus(); this.focus();
this.show(); this.show();
@ -4529,7 +4513,6 @@ List.prototype.enterSelected = function(i) {
List.prototype.cancelSelected = function(i) { List.prototype.cancelSelected = function(i) {
if (i != null) this.select(i); if (i != null) this.select(i);
//this.emit('action', this.items[this.selected], this.selected);
this.emit('action'); this.emit('action');
this.emit('cancel'); this.emit('cancel');
}; };
@ -4550,25 +4533,9 @@ function Form(options) {
options.ignoreKeys = true; options.ignoreKeys = true;
Box.call(this, options); Box.call(this, options);
//this.on('element focus', function(el, ch, key) {
// self._refresh();
// if (~self._children.indexOf(el)) {
// self._selected = el;
// }
//});
if (options.keys) { if (options.keys) {
this.screen._listenKeys(this); this.screen._listenKeys(this);
//this.screen.on('element keypress', function(el, ch, key) {
this.on('element keypress', function(el, ch, key) { this.on('element keypress', function(el, ch, key) {
// Make sure we're not entering input into a textbox.
// if (self.screen.grabKeys || self.screen.lockKeys) {
// return;
// }
// Make sure we're a form or input element.
// if (el !== self && !el.hasAncestor(self)) return;
if ((key.name === 'tab' && !key.shift) if ((key.name === 'tab' && !key.shift)
|| (el.type === 'textbox' && options.autoNext && key.name === 'enter') || (el.type === 'textbox' && options.autoNext && key.name === 'enter')
|| key.name === 'down' || key.name === 'down'
@ -4639,7 +4606,6 @@ Form.prototype.next = function() {
if (!this._selected) { if (!this._selected) {
this._selected = this._children[0]; this._selected = this._children[0];
if (!this._selected.visible) return this.next(); if (!this._selected.visible) return this.next();
// return this._selected;
if (this.screen.focused !== this._selected) return this._selected; if (this.screen.focused !== this._selected) return this._selected;
} }
@ -4663,7 +4629,6 @@ Form.prototype.previous = function() {
if (!this._selected) { if (!this._selected) {
this._selected = this._children[this._children.length - 1]; this._selected = this._children[this._children.length - 1];
if (!this._selected.visible) return this.previous(); if (!this._selected.visible) return this.previous();
// return this._selected;
if (this.screen.focused !== this._selected) return this._selected; if (this.screen.focused !== this._selected) return this._selected;
} }
@ -4836,7 +4801,6 @@ function Textarea(options) {
this.value = options.value || ''; this.value = options.value || '';
//this.on('prerender', this._render.bind(this));
this.__updateCursor = this._updateCursor.bind(this); this.__updateCursor = this._updateCursor.bind(this);
this.on('resize', this.__updateCursor); this.on('resize', this.__updateCursor);
this.on('move', this.__updateCursor); this.on('move', this.__updateCursor);
@ -5037,7 +5001,6 @@ Textarea.prototype._listener = function(ch, key) {
} }
if (this.value !== value) { if (this.value !== value) {
//this.setValue();
this.screen.render(); this.screen.render();
} }
}; };
@ -5046,7 +5009,6 @@ Textarea.prototype._typeScroll = function() {
// XXX Workaround // XXX Workaround
var height = this.height - this.iheight; var height = this.height - this.iheight;
if (this._clines.length - this.childBase > height) { if (this._clines.length - this.childBase > height) {
//this.setContent(this.value + '\n');
this.scroll(this._clines.length); this.scroll(this._clines.length);
} }
}; };
@ -5299,8 +5261,6 @@ function ProgressBar(options) {
self.setProgress(p); self.setProgress(p);
}); });
} }
//this.on('render', this._render.bind(this));
} }
ProgressBar.prototype.__proto__ = Input.prototype; ProgressBar.prototype.__proto__ = Input.prototype;
@ -5583,15 +5543,12 @@ function Checkbox(options) {
this.on('focus', function(old) { this.on('focus', function(old) {
var lpos = self.lpos; var lpos = self.lpos;
if (!lpos) return; if (!lpos) return;
//self.screen.program.saveCursor();
self.screen.program.lsaveCursor('checkbox'); self.screen.program.lsaveCursor('checkbox');
self.screen.program.cup(lpos.yi, lpos.xi + 1); self.screen.program.cup(lpos.yi, lpos.xi + 1);
self.screen.program.showCursor(); self.screen.program.showCursor();
}); });
this.on('blur', function() { this.on('blur', function() {
//self.screen.program.hideCursor();
//self.screen.program.restoreCursor();
self.screen.program.lrestoreCursor('checkbox', true); self.screen.program.lrestoreCursor('checkbox', true);
}); });
} }
@ -5756,9 +5713,10 @@ Prompt.prototype.readInput = function(text, value, callback) {
value = ''; value = '';
} }
//var parent = this.parent; // Keep above:
//this.detach(); // var parent = this.parent;
//parent.append(this); // this.detach();
// parent.append(this);
this.show(); this.show();
this.setContent(' ' + text); this.setContent(' ' + text);
@ -5842,9 +5800,10 @@ Question.prototype.ask = function(text, callback) {
var self = this; var self = this;
var press, okay, cancel; var press, okay, cancel;
//var parent = this.parent; // Keep above:
//this.detach(); // var parent = this.parent;
//parent.append(this); // this.detach();
// parent.append(this);
this.show(); this.show();
this.setContent(' ' + text); this.setContent(' ' + text);
@ -5908,16 +5867,18 @@ Message.prototype.type = 'message';
Message.prototype.log = Message.prototype.log =
Message.prototype.display = function(text, time, callback) { Message.prototype.display = function(text, time, callback) {
var self = this; var self = this;
if (typeof time === 'function') { if (typeof time === 'function') {
callback = time; callback = time;
time = null; time = null;
} }
if (time == null) time = 3;
//time = time || 3;
//var parent = this.parent; if (time == null) time = 3;
//this.detach();
//parent.append(this); // Keep above:
// var parent = this.parent;
// this.detach();
// parent.append(this);
if (this.scrollable) { if (this.scrollable) {
this.screen.saveFocus(); this.screen.saveFocus();
@ -6021,9 +5982,10 @@ Loading.prototype.type = 'loading';
Loading.prototype.load = function(text) { Loading.prototype.load = function(text) {
var self = this; var self = this;
//var parent = this.parent; // XXX Keep above:
//this.detach(); // var parent = this.parent;
//parent.append(this); // this.detach();
// parent.append(this);
this.show(); this.show();
this.setContent(text); this.setContent(text);
@ -6071,10 +6033,6 @@ function Listbar(options) {
options = options || {}; options = options || {};
// XXX Workaround to make sure buttons don't
// overlap border on the right.
// options.scrollable = true;
this.items = []; this.items = [];
this.ritems = []; this.ritems = [];
this.commands = []; this.commands = [];
@ -6086,15 +6044,6 @@ function Listbar(options) {
Box.call(this, options); Box.call(this, options);
//this._.debug = new Box({
// parent: this.screen,
// top: 0,
// left: 0,
// height: 'shrink',
// width: 'shrink',
// content: '...'
//});
if (options.commands || options.items) { if (options.commands || options.items) {
this.setItems(options.commands || options.items); this.setItems(options.commands || options.items);
} }
@ -6290,17 +6239,10 @@ Listbar.prototype.appendItem = function(item, callback) {
this.append(el); this.append(el);
if (cmd.callback) { if (cmd.callback) {
//el.on('press', cmd.callback);
//this.on('select', function(el) {
// if (el._.cmd.callback) {
// el._.cmd.callback();
// }
//});
if (cmd.keys) { if (cmd.keys) {
this.screen.key(cmd.keys, function(ch, key) { this.screen.key(cmd.keys, function(ch, key) {
self.emit('action', el, self.selected); self.emit('action', el, self.selected);
self.emit('select', el, self.selected); self.emit('select', el, self.selected);
//el.press();
if (el._.cmd.callback) { if (el._.cmd.callback) {
el._.cmd.callback(); el._.cmd.callback();
} }
@ -6318,7 +6260,6 @@ Listbar.prototype.appendItem = function(item, callback) {
el.on('click', function(data) { el.on('click', function(data) {
self.emit('action', el, self.selected); self.emit('action', el, self.selected);
self.emit('select', el, self.selected); self.emit('select', el, self.selected);
//el.press();
if (el._.cmd.callback) { if (el._.cmd.callback) {
el._.cmd.callback(); el._.cmd.callback();
} }
@ -6363,33 +6304,31 @@ Listbar.prototype.select = function(offset) {
, visible = 0 , visible = 0
, el; , el;
if (offset < 0) offset = 0; if (offset < 0) {
else if (offset >= this.items.length) offset = this.items.length - 1; offset = 0;
} else if (offset >= this.items.length) {
offset = this.items.length - 1;
}
el = this.items[offset]; el = this.items[offset];
if (!el) return; if (!el) return;
this.items.forEach(function(el, i) { this.items.forEach(function(el, i) {
if (i < self.leftBase) return; if (i < self.leftBase) return;
var lpos = el._getCoords(); var lpos = el._getCoords();
if (!lpos) return; if (!lpos) return;
// XXX Need this because overflowed elements will still return lpos.
if (lpos.xl - lpos.xi <= 0) return; if (lpos.xl - lpos.xi <= 0) return;
drawn += (lpos.xl - lpos.xi) + 2; drawn += (lpos.xl - lpos.xi) + 2;
// XXX Need this because overflowed elements will still return lpos.
//drawn += el.getText().length + 2 + 2;
if (drawn <= width) visible++; if (drawn <= width) visible++;
}); });
var diff = offset - (this.leftBase + this.leftOffset); var diff = offset - (this.leftBase + this.leftOffset);
if (offset > this.leftBase + this.leftOffset) { if (offset > this.leftBase + this.leftOffset) {
if (offset > this.leftBase + visible - 1) { if (offset > this.leftBase + visible - 1) {
//this.leftOffset = visible;
//this.leftBase = offset - visible;
this.leftOffset = 0; this.leftOffset = 0;
this.leftBase = offset; this.leftBase = offset;
} else { } else {
@ -6404,18 +6343,6 @@ Listbar.prototype.select = function(offset) {
this.leftOffset -= diff; this.leftOffset -= diff;
} }
} }
//this.leftOffset = Math.max(0, this.leftOffset);
//this.leftBase = Math.max(0, this.leftBase);
//this._.debug.setContent(JSON.stringify({
// leftOffset: this.leftOffset,
// leftBase: this.leftBase,
// drawn: drawn,
// visible: visible,
// width: width,
// diff: diff
//}, null, 2));
}; };
Listbar.prototype.removeItem = function(child) { Listbar.prototype.removeItem = function(child) {
@ -6449,7 +6376,6 @@ Listbar.prototype.moveRight = function(offset) {
Listbar.prototype.selectTab = function(index) { Listbar.prototype.selectTab = function(index) {
var item = this.items[index]; var item = this.items[index];
if (item) { if (item) {
//item.press();
if (item._.cmd.callback) { if (item._.cmd.callback) {
item._.cmd.callback(); item._.cmd.callback();
} }
@ -6458,52 +6384,6 @@ Listbar.prototype.selectTab = function(index) {
} }
}; };
/**
* DirManager - Merge into FileManager?
*/
function DirManager(options) {
var self = this;
if (!(this instanceof Node)) {
return new DirManager(options);
}
options = options || {};
FileManager.call(this, options);
this.on('cd', function(dir) {
if (dir === self.cwd) return;
self.emit('file', dir);
});
}
DirManager.prototype.__proto__ = FileManager.prototype;
DirManager.prototype.type = 'dir-manager';
/**
* Passbox - Useless
*/
function Passbox(options) {
var self = this;
if (!(this instanceof Node)) {
return new Passbox(options);
}
options = options || {};
options.censor = true;
Textbox.call(this, options);
}
Passbox.prototype.__proto__ = Textbox.prototype;
Passbox.prototype.type = 'passbox';
/** /**
* Terminal * Terminal
*/ */
@ -6748,8 +6628,6 @@ exports.Question = exports.question = Question;
exports.Message = exports.message = Message; exports.Message = exports.message = Message;
exports.Loading = exports.loading = Loading; exports.Loading = exports.loading = Loading;
exports.Listbar = exports.listbar = Listbar; exports.Listbar = exports.listbar = Listbar;
exports.DirManager = exports.dirmanager = DirManager;
exports.Passbox = exports.passbox = Passbox;
exports.Terminal = exports.terminal = Terminal; exports.Terminal = exports.terminal = Terminal;