enigma-bbs/core/view.js

269 lines
6.6 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');
var colorCodes = require('./color_codes.js');
var _ = require('lodash');
2014-10-17 04:03:32 +00:00
exports.View = View;
exports.VIEW_SPECIAL_KEY_MAP_DEFAULT = VIEW_SPECIAL_KEY_MAP_DEFAULT;
2014-10-20 03:06:39 +00:00
2014-10-22 05:12:44 +00:00
var VIEW_SPECIAL_KEY_MAP_DEFAULT = {
accept : [ 'return' ],
2014-10-20 03:06:39 +00:00
exit : [ 'esc' ],
backspace : [ 'backspace', 'del' ],
2014-10-22 05:12:44 +00:00
del : [ 'del' ],
2014-10-20 03:06:39 +00:00
next : [ 'tab' ],
2014-11-01 15:50:11 +00:00
up : [ 'up arrow' ],
down : [ 'down arrow' ],
end : [ 'end' ],
home : [ 'home' ],
left : [ 'left arrow' ],
right : [ 'right arrow' ],
clearLine : [ 'ctrl + y' ],
2014-10-20 03:06:39 +00:00
};
function View(options) {
2014-10-22 05:12:44 +00:00
events.EventEmitter.call(this);
2014-10-20 03:06:39 +00:00
assert(_.isObject(options));
assert(_.isObject(options.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
this.client = options.client;
2014-10-20 05:30:44 +00:00
this.cursor = options.cursor || 'show';
this.cursorStyle = options.cursorStyle || 'default';
this.acceptsFocus = options.acceptsFocus || false;
this.acceptsInput = options.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
this.textStyle = options.textStyle || 'normal';
this.focusTextStyle = options.focusTextStyle || this.textStyle;
2015-04-08 05:15:34 +00:00
if(options.id) {
this.setId(options.id);
}
if(options.position) {
this.setPosition(options.position);
2014-10-20 03:06:39 +00:00
}
if(_.isObject(options.autoScale)) {
this.autoScale = options.autoScale;
} else {
this.autoScale = { height : true, width : true };
}
if(options.dimens) {
this.setDimension(options.dimens);
this.autoScale = { height : false, width : false };
} else {
this.dimens = {
width : options.width || 0,
height : 0
};
2014-10-20 03:06:39 +00:00
}
// :TODO: Just use styleSGRx for these, e.g. styleSGR0, styleSGR1 = norm/focus
this.ansiSGR = options.ansiSGR || ansi.getSGRFromGraphicRendition( { fg : 39, bg : 49 }, true);
this.ansiFocusSGR = options.ansiFocusSGR || this.ansiSGR;
2014-10-20 03:06:39 +00:00
2015-04-30 22:41:43 +00:00
this.styleSGR1 = options.styleSGR1 || this.ansiSGR;
this.styleSGR2 = options.styleSGR2 || this.ansiFocusSGR;
2014-10-22 05:12:44 +00:00
if(this.acceptsInput) {
this.specialKeyMap = options.specialKeyMap || VIEW_SPECIAL_KEY_MAP_DEFAULT;
2014-10-20 03:06:39 +00:00
}
this.isKeyMapped = function(keySet, keyName) {
return _.has(this.specialKeyMap, keySet) && this.specialKeyMap[keySet].indexOf(keyName) > -1;
};
this.getANSIColor = function(color) {
var sgr = [ color.flags, color.fg ];
if(color.bg !== color.flags) {
sgr.push(color.bg);
}
return ansi.sgr(sgr);
};
this.hideCusor = function() {
self.client.term.rawWrite(ansi.hideCursor());
};
this.restoreCursor = function() {
//this.client.term.write(ansi.setCursorStyle(this.cursorStyle));
this.client.term.rawWrite('show' === this.cursor ? ansi.showCursor() : ansi.hideCursor());
};
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
View.prototype.setId = function(id) {
this.id = id;
};
2014-10-20 05:30:44 +00:00
View.prototype.getId = function() {
return this.id;
};
2014-10-22 05:12:44 +00:00
View.prototype.setPosition = function(pos) {
//
// Allow the following forms: [row, col], { row : r, col : c }, or (row, col)
2014-10-22 05:12:44 +00:00
//
if(util.isArray(pos)) {
this.position.row = pos[0];
this.position.col = pos[1];
} else if(pos.row && pos.col) {
this.position.row = pos.row;
this.position.col = pos.col;
2014-10-22 05:12:44 +00:00
} else if(2 === arguments.length) {
this.position.row = parseInt(arguments[0], 10);
this.position.col = parseInt(arguments[1], 10);
2014-10-20 03:06:39 +00:00
}
assert(!(isNaN(this.position.row)));
assert(!(isNaN(this.position.col)));
2014-10-20 03:06:39 +00:00
assert(
this.position.row > 0 && this.position.row <= this.client.term.termHeight,
'X position ' + this.position.row + ' out of terminal range ' + this.client.term.termHeight);
assert(
this.position.col > 0 && this.position.col <= this.client.term.termWidth,
'Y position ' + this.position.col + ' out of terminal range ' + this.client.term.termWidth);
2014-10-20 03:06:39 +00:00
};
View.prototype.setDimension = function(dimens) {
assert(_.isObject(dimens) && _.isNumber(dimens.height) && _.isNumber(dimens.width));
this.dimens = dimens;
this.autoScale = { height : false, width : false };
};
View.prototype.setHeight = function(height) {
height = parseInt(height, 10);
assert(_.isNumber(height));
// :TODO: assert height is within this.client.term.termHeight
this.dimens.height = height;
this.autoScale.height = false;
};
View.prototype.setWidth = function(width) {
width = parseInt(width);
assert(_.isNumber(width));
// :TODO: assert width is appropriate for this.client.term.termWidth
this.dimens.width = width;
this.autoScale.width = false;
};
View.prototype.getSGR = function() {
return this.ansiSGR;
};
View.prototype.getStyleSGR = function(n) {
assert(_.isNumber(n));
return this['styleSGR' + n];
};
View.prototype.getFocusSGR = function() {
return this.ansiFocusSGR;
};
2014-10-20 03:06:39 +00:00
View.prototype.setPropertyValue = function(propName, value) {
switch(propName) {
case 'height' : this.setHeight(value); break;
case 'width' : this.setWidth(value); break;
case 'focus' : this.setFocus(value); break;
case 'text' :
if('setText' in this) {
this.setText(value);
}
break;
case 'textStyle' : this.textStyle = value; break;
case 'focusTextStyle' : this.focusTextStyle = value; break;
case 'justify' : this.justify = value; break;
case 'fillChar' :
if('fillChar' in this) {
if(_.isNumber(value)) {
this.fillChar = String.fromCharCode(value);
} else if(_.isString(value)) {
this.fillChar = value.substr(0, 1);
}
}
break;
case 'submit' :
if(_.isBoolean(value)) {
this.submit = value;
}/* else {
this.submit = _.isArray(value) && value.length > 0;
}
*/
break;
case 'resizable' :
if(_.isBoolean(value)) {
this.resizable = value;
}
break;
case 'argName' : this.submitArgName = value; break;
}
if(/styleSGR[0-9]{1,2}/.test(propName)) {
if(_.isObject(value)) {
this[propName] = ansi.getSGRFromGraphicRendition(value, true);
} else if(_.isString(value)) {
this[propName] = colorCodes.pipeToAnsi(value);
}
}
};
2014-10-22 05:12:44 +00:00
View.prototype.redraw = function() {
this.client.term.write(ansi.goto(this.position.row, this.position.col));
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;
this.restoreCursor();
};
View.prototype.onKeyPress = function(ch, key) {
assert(this.hasFocus, 'View does not have focus');
assert(this.acceptsInput, 'View does not accept input');
if(key) {
assert(this.specialKeyMap, 'No special key map defined');
if(this.isKeyMapped('accept', key.name)) {
this.emit('action', 'accept', key);
} else if(this.isKeyMapped('next', key.name)) {
this.emit('action', 'next', key);
}
}
if(ch) {
assert(1 === ch.length);
}
};
View.prototype.getData = function() {
2014-10-20 03:06:39 +00:00
};