enigma-bbs/core/view.js

97 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-10-17 04:03:32 +00:00
/* jslint node: true */
'use strict';
2014-10-22 05:12:44 +00:00
var events = require('events');
2014-10-17 04:03:32 +00:00
var util = require('util');
2014-10-20 03:06:39 +00:00
var assert = require('assert');
2014-10-22 05:12:44 +00:00
var ansi = require('./ansi_term.js');
2014-10-17 04:03:32 +00:00
exports.View = View;
2014-10-20 03:06:39 +00:00
2014-10-22 05:12:44 +00:00
var VIEW_SPECIAL_KEY_MAP_DEFAULT = {
2014-10-20 03:06:39 +00:00
enter : [ 'enter' ],
exit : [ 'esc' ],
2014-10-22 05:12:44 +00:00
backspace : [ 'backspace' ],
del : [ 'del' ],
2014-10-20 03:06:39 +00:00
next : [ 'tab' ],
};
2014-10-22 05:12:44 +00:00
function View(client, options) {
events.EventEmitter.call(this);
2014-10-20 03:06:39 +00:00
2014-10-22 05:12:44 +00:00
assert(client);
2014-10-20 03:06:39 +00:00
2014-10-22 05:12:44 +00:00
var self = this;
2014-10-20 03:06:39 +00:00
2014-10-22 05:12:44 +00:00
this.client = client;
this.options = options || {};
2014-10-20 05:30:44 +00:00
2014-10-22 05:12:44 +00:00
this.acceptsFocus = false;
this.acceptsInput = false;
2014-10-20 05:30:44 +00:00
2014-10-22 05:12:44 +00:00
this.position = { x : 0, y : 0 };
this.dimens = { height : 1, width : 0 };
2014-10-20 03:06:39 +00:00
2014-10-22 05:12:44 +00:00
if(this.options.position) {
this.setPosition(this.options.position);
2014-10-20 03:06:39 +00:00
}
2014-10-22 05:12:44 +00:00
if(this.options.dimens && this.options.dimens.height) {
this.dimens.height = this.options.dimens.height;
2014-10-20 03:06:39 +00:00
}
2014-10-22 05:12:44 +00:00
if(this.options.dimens && this.options.dimens.width) {
this.dimens.width = this.options.dimens.width;
2014-10-20 03:06:39 +00:00
}
2014-10-22 05:12:44 +00:00
this.color = this.options.color || { flags : 0, fg : 7, bg : 0 };
this.focusColor = this.options.focusColor || this.color;
2014-10-20 03:06:39 +00:00
2014-10-22 05:12:44 +00:00
if(this.acceptsInput) {
this.specialKeyMap = this.options.specialKeyMap || VIEW_SPECIAL_KEY_MAP_DEFAULT;
2014-10-20 03:06:39 +00:00
}
}
2014-10-22 05:12:44 +00:00
util.inherits(View, events.EventEmitter);
2014-10-20 05:30:44 +00:00
2014-10-22 05:12:44 +00:00
View.prototype.setPosition = function(pos) {
//
// We allow [x, y], { x : x, y : y }, or (x, y)
//
if(util.isArray(pos)) {
this.position.x = pos[0];
this.position.y = pos[1];
} else if(pos.x && pos.y) {
this.position.x = pos.x;
this.position.y = pos.y;
} else if(2 === arguments.length) {
var x = parseInt(arguments[0], 10);
var y = parseInt(arguments[1], 10);
if(!isNaN(x) && !isNaN(y)) {
this.position.x = x;
this.position.y = y;
2014-10-20 03:06:39 +00:00
}
}
2014-10-22 05:12:44 +00:00
assert(this.position.x > 0 && this.position.x < this.client.term.termHeight);
assert(this.position.y > 0 && this.position.y < this.client.term.termWidth);
2014-10-20 03:06:39 +00:00
};
2014-10-22 05:12:44 +00:00
View.prototype.getColor = function() {
return this.color;
2014-10-20 05:30:44 +00:00
};
2014-10-22 05:12:44 +00:00
View.prototype.getFocusColor = function() {
return this.focusColor;
2014-10-20 03:06:39 +00:00
};
2014-10-22 05:12:44 +00:00
View.prototype.redraw = function() {
this.client.term.write(ansi.goto(this.position.x, this.position.y));
2014-10-20 03:06:39 +00:00
};
2014-10-22 05:12:44 +00:00
View.prototype.setFocus = function(focused) {
assert(this.acceptsFocus, 'View does not accept focus');
2014-10-20 03:06:39 +00:00
2014-10-22 05:12:44 +00:00
this.hasFocus = focused;
2014-10-20 03:06:39 +00:00
};