constructor and options handling.

This commit is contained in:
Christopher Jeffrey 2013-06-14 15:58:54 -05:00
parent 8cf00fe1b7
commit 5247b41d8b
2 changed files with 60 additions and 6 deletions

View File

@ -619,7 +619,7 @@ Program.prototype._bindResponse = function(s) {
}; };
Program.prototype.receive = function(text, callback) { Program.prototype.receive = function(text, callback) {
var listeners = this.listeners('keypress') var listeners = (this._events && this._events['keypress']) || []
, bak = listeners.slice() , bak = listeners.slice()
, self = this; , self = this;
@ -648,7 +648,7 @@ Program.prototype.receive = function(text, callback) {
}; };
Program.prototype.receive_ = function(text, callback) { Program.prototype.receive_ = function(text, callback) {
var listeners = this.listeners('keypress') var listeners = (this._events && this._events['keypress']) || []
, bak = listeners.slice() , bak = listeners.slice()
, self = this; , self = this;
@ -701,7 +701,7 @@ Program.prototype.rsetx = function(x) {
if (!x) return; if (!x) return;
return x > 0 return x > 0
? this.forward(x) ? this.forward(x)
: this.back(x); : this.back(-x);
}; };
Program.prototype.rsety = function(y) { Program.prototype.rsety = function(y) {
@ -709,12 +709,12 @@ Program.prototype.rsety = function(y) {
if (!y) return; if (!y) return;
return y > 0 return y > 0
? this.up(y) ? this.up(y)
: this.down(y); : this.down(-y);
}; };
Program.prototype.rmove = function(x, y) { Program.prototype.rmove = function(x, y) {
this.rsetX(x); this.rsetx(x);
this.rsetY(y); this.rsety(y);
}; };
Program.prototype.simpleInsert = function(ch, i, attr) { Program.prototype.simpleInsert = function(ch, i, attr) {

View File

@ -15,6 +15,10 @@ var EventEmitter = require('events').EventEmitter;
*/ */
function Node(options) { function Node(options) {
if (!(this instanceof Node)) {
return new Node(options);
}
EventEmitter.call(this); EventEmitter.call(this);
this.options = options || {}; this.options = options || {};
@ -90,6 +94,19 @@ Node.prototype.detach = function(element) {
function Screen(options) { function Screen(options) {
var self = this; var self = this;
if (!(this instanceof Screen)) {
return new Screen(options);
}
if (options && options.rsety && options.listen) {
options = { program: options };
}
options = options || {};
options.program = options.program
|| require('./program').global
|| new (require('./program'))(options);
if (!Screen.global) { if (!Screen.global) {
Screen.global = this; Screen.global = this;
} }
@ -547,6 +564,10 @@ Screen.prototype.fillRegion = function(attr, ch, xi, xl, yi, yl) {
function Element(options) { function Element(options) {
var self = this; var self = this;
if (!(this instanceof Element)) {
return new Element(options);
}
Node.call(this, options); Node.call(this, options);
this.position = { this.position = {
@ -997,6 +1018,9 @@ Element.prototype.__defineSetter__('rbottom', function(val) {
*/ */
function Box(options) { function Box(options) {
if (!(this instanceof Box)) {
return new Box(options);
}
Element.call(this, options); Element.call(this, options);
} }
@ -1240,6 +1264,10 @@ Text.prototype.__proto__ = Box.prototype;
*/ */
function Line(options) { function Line(options) {
if (!(this instanceof Line)) {
return new Line(options);
}
var orientation = options.orientation || 'vertical'; var orientation = options.orientation || 'vertical';
delete options.orientation; delete options.orientation;
@ -1272,6 +1300,9 @@ Line.prototype.__proto__ = Box.prototype;
*/ */
function ScrollableBox(options) { function ScrollableBox(options) {
if (!(this instanceof ScrollableBox)) {
return new ScrollableBox(options);
}
Box.call(this, options); Box.call(this, options);
this.scrollable = true; this.scrollable = true;
this.childOffset = 0; this.childOffset = 0;
@ -1313,6 +1344,10 @@ ScrollableBox.prototype.scroll = function(offset) {
function List(options) { function List(options) {
var self = this; var self = this;
if (!(this instanceof List)) {
return new List(options);
}
ScrollableBox.call(this, options); ScrollableBox.call(this, options);
this.items = []; this.items = [];
@ -1457,6 +1492,10 @@ List.prototype.down = function(offset) {
function ScrollableText(options) { function ScrollableText(options) {
var self = this; var self = this;
if (!(this instanceof ScrollableText)) {
return new ScrollableText(options);
}
options.alwaysScroll = true; options.alwaysScroll = true;
ScrollableBox.call(this, options); ScrollableBox.call(this, options);
@ -1560,6 +1599,9 @@ ScrollableText.prototype._recalculateIndex = function() {
*/ */
function Input(options) { function Input(options) {
if (!(this instanceof Input)) {
return new Input(options);
}
Box.call(this, options); Box.call(this, options);
} }
@ -1570,6 +1612,9 @@ Input.prototype.__proto__ = Box.prototype;
*/ */
function Textbox(options) { function Textbox(options) {
if (!(this instanceof Textbox)) {
return new Textbox(options);
}
Input.call(this, options); Input.call(this, options);
this.screen._listenKeys(this); this.screen._listenKeys(this);
} }
@ -1669,6 +1714,9 @@ Textbox.prototype.setEditor = function(callback) {
*/ */
function Textarea(options) { function Textarea(options) {
if (!(this instanceof Textarea)) {
return new Textarea(options);
}
Input.call(this, options); Input.call(this, options);
} }
@ -1679,6 +1727,9 @@ Textarea.prototype.__proto__ = Input.prototype;
*/ */
function Button(options) { function Button(options) {
if (!(this instanceof Button)) {
return new Button(options);
}
Input.call(this, options); Input.call(this, options);
} }
@ -1689,6 +1740,9 @@ Button.prototype.__proto__ = Input.prototype;
*/ */
function ProgressBar(options) { function ProgressBar(options) {
if (!(this instanceof ProgressBar)) {
return new ProgressBar(options);
}
Input.call(this, options); Input.call(this, options);
this.filled = options.filled || 0; this.filled = options.filled || 0;
if (typeof this.filled === 'string') { if (typeof this.filled === 'string') {