* A bit of work on VerticalMenuView
* Inheritance updates for Views * Fixes and better handling of MCI factory
This commit is contained in:
parent
c3aa4c44e2
commit
edc8266310
|
@ -163,8 +163,6 @@ function ANSIEscapeParser(options) {
|
|||
|
||||
|
||||
self.emit('mci', mciCode, id, args);
|
||||
console.log(self.row + ', ' + self.column);
|
||||
console.log(match[0]);
|
||||
|
||||
if(self.mciReplaceChar.length > 0) {
|
||||
escape('m', [self.lastFlags, self.lastFgColor, self.lastBgColor]);
|
||||
|
|
|
@ -426,6 +426,7 @@ function display(art, options, cb) {
|
|||
parser.on('mci', function onMCI(mciCode, id, args) {
|
||||
id = id || generatedId++;
|
||||
var mapItem = mciCode + id;
|
||||
// :TODO: Avoid mutiple [] lookups here
|
||||
if(mci[mapItem]) {
|
||||
mci[mapItem].focusColor = {
|
||||
fg : parser.fgColor,
|
||||
|
|
|
@ -6,9 +6,6 @@ var paths = require('path');
|
|||
var miscUtil = require('./misc_util.js');
|
||||
|
||||
module.exports = {
|
||||
// :TODO: remove this ... anti-pattern!
|
||||
//config : undefined,
|
||||
|
||||
defaultPath : function() {
|
||||
var base = miscUtil.resolvePath('~/');
|
||||
if(base) {
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
/* jslint node: true */
|
||||
'use strict';
|
||||
|
||||
var TextView = require('./text_view.js').TextView;
|
||||
var EditTextView = require('./edit_text_view.js').EditTextView;
|
||||
var ButtonView = require('./button_view.js').ButtonView;
|
||||
var Config = require('./config.js').config;
|
||||
var packageJson = require('../package.json');
|
||||
var assert = require('assert');
|
||||
var TextView = require('./text_view.js').TextView;
|
||||
var EditTextView = require('./edit_text_view.js').EditTextView;
|
||||
var ButtonView = require('./button_view.js').ButtonView;
|
||||
var VerticalMenuView = require('./vertical_menu_view.js').VerticalMenuView;
|
||||
var Config = require('./config.js').config;
|
||||
var packageJson = require('../package.json');
|
||||
var assert = require('assert');
|
||||
|
||||
exports.MCIViewFactory = MCIViewFactory;
|
||||
|
||||
|
@ -19,11 +20,19 @@ MCIViewFactory.prototype.getPredefinedViewLabel = function(name) {
|
|||
switch(name) {
|
||||
case 'BN' : label = Config.bbsName; break;
|
||||
case 'VL' : label = 'ENiGMA½ v' + packageJson.version; break;
|
||||
case 'VN' : label = packageJson.version; break;
|
||||
}
|
||||
|
||||
return label;
|
||||
};
|
||||
|
||||
// :TODO: probably do something like this and generalize all of this:
|
||||
/*
|
||||
var MCI_ARG_MAP = {
|
||||
'ET' : { 0 : 'maxLength', 1 : 'textStyle' }
|
||||
};
|
||||
*/
|
||||
|
||||
MCIViewFactory.prototype.createFromMCI = function(mci) {
|
||||
assert(mci.code);
|
||||
assert(mci.id > 0);
|
||||
|
@ -37,8 +46,18 @@ MCIViewFactory.prototype.createFromMCI = function(mci) {
|
|||
position : { x : mci.position[0], y : mci.position[1] },
|
||||
};
|
||||
|
||||
// :TODO: move this stuff out of the switch to their own methods/objects
|
||||
function setOption(pos, name) {
|
||||
if(mci.args.length > pos && mci.args[pos].length > 0) {
|
||||
options[name] = mci.args[pos];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
switch(mci.code) {
|
||||
case 'TL' :
|
||||
// :TODO: convert to setOption()
|
||||
if(mci.args.length > 0) {
|
||||
options.textStyle = mci.args[0];
|
||||
}
|
||||
|
@ -56,19 +75,17 @@ MCIViewFactory.prototype.createFromMCI = function(mci) {
|
|||
break;
|
||||
|
||||
case 'ET' :
|
||||
if(mci.args.length > 0) {
|
||||
options.maxLength = mci.args[0];
|
||||
options.dimens = { width : options.maxLength };
|
||||
if(setOption(0, 'maxLength')) {
|
||||
options.dimens = { width : options.maxLength };
|
||||
}
|
||||
|
||||
if(mci.args.length > 1) {
|
||||
options.textStyle = mci.args[1];
|
||||
}
|
||||
setOption(1, 'textStyle');
|
||||
|
||||
view = new EditTextView(this.client, options);
|
||||
break;
|
||||
|
||||
case 'PL' :
|
||||
// :TODO: convert to setOption()
|
||||
if(mci.args.length > 0) {
|
||||
options.text = this.getPredefinedViewLabel(mci.args[0]);
|
||||
if(options.text) {
|
||||
|
@ -91,6 +108,7 @@ MCIViewFactory.prototype.createFromMCI = function(mci) {
|
|||
break;
|
||||
|
||||
case 'BV' :
|
||||
// :TODO: convert to setOption()
|
||||
if(mci.args.length > 0) {
|
||||
options.text = mci.args[0];
|
||||
options.dimens = { width : options.text.length };
|
||||
|
@ -98,6 +116,12 @@ MCIViewFactory.prototype.createFromMCI = function(mci) {
|
|||
|
||||
view = new ButtonView(this.client, options);
|
||||
break;
|
||||
|
||||
case 'VM' :
|
||||
setOption(0, 'itemSpacing');
|
||||
|
||||
view = new VerticalMenuView(this.client, options);
|
||||
break;
|
||||
}
|
||||
|
||||
return view;
|
||||
|
|
|
@ -3,26 +3,39 @@
|
|||
|
||||
var View = require('./view.js').View;
|
||||
var ansi = require('./ansi_term.js');
|
||||
var miscUtil = require('./misc_util.js');
|
||||
var util = require('util');
|
||||
var assert = require('assert');
|
||||
|
||||
exports.MenuView = MenuView;
|
||||
|
||||
function MenuView(client, options) {
|
||||
options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true);
|
||||
var self = this;
|
||||
|
||||
View.call(this, client, options);
|
||||
|
||||
this.focusedItemIndex = 0;
|
||||
|
||||
|
||||
//// --- TESTING
|
||||
options.items = [ 'Login', 'Apply', 'Logout' ];
|
||||
//options.itemSpacing = 2;
|
||||
//// --- TESTING
|
||||
|
||||
this.items = [];
|
||||
if(this.options.items) {
|
||||
this.options.items.forEach(function onItem(itemText) {
|
||||
this.items.push({
|
||||
self.items.push({
|
||||
text : itemText,
|
||||
focused : false,
|
||||
selected : false,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
this.itemSpacing = this.options.itemSpacing || 1;
|
||||
this.itemSpacing = parseInt(this.itemSpacing, 10);
|
||||
|
||||
this.focusPrefix = this.options.focusPrefix || '';
|
||||
this.focusSuffix = this.options.focusSuffix || '';
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ function TextView(client, options) {
|
|||
this.maxLength = this.options.maxLength;
|
||||
}
|
||||
|
||||
this.textStyle = this.options.textStyle || 'normal';
|
||||
this.multiLine = this.options.multiLine || false;
|
||||
this.fillChar = miscUtil.valueWithDefault(this.options.fillChar, ' ').substr(0, 1);
|
||||
|
||||
|
@ -29,8 +28,6 @@ function TextView(client, options) {
|
|||
|
||||
this.setText(this.options.text || '');
|
||||
|
||||
this.isPasswordTextStyle = 'P' === this.textStyle || 'password' === this.textStyle;
|
||||
|
||||
if(this.isPasswordTextStyle) {
|
||||
this.textMaskChar = miscUtil.valueWithDefault(this.options.textMaskChar, '*').substr(0, 1);
|
||||
}
|
||||
|
@ -45,7 +42,7 @@ TextView.prototype.redraw = function() {
|
|||
this.client.term.write(this.getANSIColor(color));
|
||||
|
||||
// :TODO: If using fillChar, don't just pad. switch to non-focus color & fill with |fillChar|
|
||||
|
||||
// :TODO: Apply justification
|
||||
if(this.isPasswordTextStyle) {
|
||||
this.client.term.write(strUtil.pad(new Array(this.text.length + 1).join(this.textMaskChar), this.dimens.width));
|
||||
} else {
|
||||
|
|
|
@ -3,15 +3,70 @@
|
|||
|
||||
var MenuView = require('./menu_view.js').MenuView;
|
||||
var ansi = require('./ansi_term.js');
|
||||
var strUtil = require('./string_util.js');
|
||||
var util = require('util');
|
||||
var assert = require('assert');
|
||||
|
||||
exports.VerticalMenuView = VerticalMenuView;
|
||||
|
||||
function VerticalMenuView(client, options) {
|
||||
MenuView.call(this, client, options);
|
||||
|
||||
var self = this;
|
||||
|
||||
this.cacheXPositions = function() {
|
||||
if(self.xPositionCacheExpired) {
|
||||
var count = this.items.length;
|
||||
var x = self.position.x;
|
||||
for(var i = 0; i < count; ++i) {
|
||||
if(i > 0) {
|
||||
x += self.itemSpacing;
|
||||
}
|
||||
|
||||
self.items[i].xPosition = x;
|
||||
}
|
||||
self.xPositionCacheExpired = false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
util.inherits(VerticalMenuView, MenuView);
|
||||
|
||||
VerticalMenuView.prototype.setPosition = function(pos) {
|
||||
VerticalMenuView.super_.prototype.setPosition.call(this, pos);
|
||||
|
||||
this.xPositionCacheExpired = true;
|
||||
};
|
||||
|
||||
VerticalMenuView.prototype.redraw = function() {
|
||||
VerticalMenuView.super_.prototype.redraw.call(this);
|
||||
|
||||
var color = this.getColor();
|
||||
var focusColor = this.getFocusColor();
|
||||
console.log(focusColor);
|
||||
var x = this.position.x;
|
||||
var y = this.position.y;
|
||||
|
||||
var count = this.items.length;
|
||||
var item;
|
||||
var text;
|
||||
|
||||
this.cacheXPositions();
|
||||
|
||||
for(var i = 0; i < count; ++i) {
|
||||
item = this.items[i];
|
||||
|
||||
this.client.term.write(ansi.goto(item.xPosition, y));
|
||||
|
||||
this.client.term.write(this.getANSIColor(i === this.focusedItemIndex || item.selected ? focusColor : color));
|
||||
|
||||
text = strUtil.stylizeString(item.text, item.hasFocus ? this.focusTextStyle : this.textStyle);
|
||||
this.client.term.write(text); // :TODO: apply justify, and style
|
||||
}
|
||||
};
|
||||
|
||||
VerticalMenuView.prototype.setFocus = function(focused) {
|
||||
VerticalMenuView.super_.prototype.setFocus.call(this, focused);
|
||||
|
||||
this.redraw();
|
||||
};
|
|
@ -33,6 +33,9 @@ function View(client, options) {
|
|||
this.position = { x : 0, y : 0 };
|
||||
this.dimens = { height : 1, width : 0 };
|
||||
|
||||
this.textStyle = this.options.textStyle || 'normal';
|
||||
this.focusTextStyle = this.options.focusTextStyle || this.textStyle;
|
||||
|
||||
if(this.options.id) {
|
||||
this.setId(this.options.id);
|
||||
}
|
||||
|
@ -41,6 +44,8 @@ function View(client, options) {
|
|||
this.setPosition(this.options.position);
|
||||
}
|
||||
|
||||
this.isPasswordTextStyle = 'P' === this.textStyle || 'password' === this.textStyle;
|
||||
|
||||
// :TODO: Don't allow width/height > client.term
|
||||
if(this.options.dimens && this.options.dimens.height) {
|
||||
this.dimens.height = this.options.dimens.height;
|
||||
|
|
|
@ -126,15 +126,17 @@ ViewController.prototype.setViewOrder = function(order) {
|
|||
});
|
||||
}
|
||||
|
||||
var view;
|
||||
var count = viewIdOrder.length - 1;
|
||||
for(var i = 0; i < count; ++i) {
|
||||
this.views[viewIdOrder[i]].nextId = viewIdOrder[i + 1];
|
||||
}
|
||||
if(viewIdOrder.length > 0) {
|
||||
var view;
|
||||
var count = viewIdOrder.length - 1;
|
||||
for(var i = 0; i < count; ++i) {
|
||||
this.views[viewIdOrder[i]].nextId = viewIdOrder[i + 1];
|
||||
}
|
||||
|
||||
this.firstId = viewIdOrder[0];
|
||||
var lastId = viewIdOrder[viewIdOrder.length - 1];
|
||||
this.views[lastId].nextId = this.firstId;
|
||||
this.firstId = viewIdOrder[0];
|
||||
var lastId = viewIdOrder.length > 1 ? viewIdOrder[viewIdOrder.length - 1] : this.firstId;
|
||||
this.views[lastId].nextId = this.firstId;
|
||||
}
|
||||
};
|
||||
|
||||
ViewController.prototype.loadFromMCIMap = function(mciMap) {
|
||||
|
@ -148,7 +150,7 @@ ViewController.prototype.loadFromMCIMap = function(mciMap) {
|
|||
if(view) {
|
||||
view.on('action', self.onViewAction);
|
||||
self.addView(view);
|
||||
view.redraw();
|
||||
view.redraw(); // :TODO: This can result in double redraw() if we set focus on this item after
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
@ -30,7 +30,7 @@ function entryPoint(client) {
|
|||
|
||||
//art.getArt('SO-CC1.ANS'/* 'MATRIX'*/, { types: ['.ans'], random: true}, function onArt(err, theArt) {
|
||||
//client.user.properties.art_theme_id = '';
|
||||
theme.getThemeArt('MATRIX_1', client.user.properties.art_theme_id, function onArt(err, theArt) {
|
||||
theme.getThemeArt('MCI_VM1.ANS', client.user.properties.art_theme_id, function onArt(err, theArt) {
|
||||
|
||||
//art.getArt('MATRIX_1.ANS', {}, function onArt(err, theArt) {
|
||||
if(!err) {
|
||||
|
@ -65,46 +65,6 @@ function entryPoint(client) {
|
|||
//vc.getView(4).setText('Login');
|
||||
vc.setViewOrder();
|
||||
vc.switchFocus(1);
|
||||
//vc.addView(etv);
|
||||
//vc.switchFocus(2);
|
||||
|
||||
/*
|
||||
|
||||
client.on('key press', function onKp(key, isSpecial) {
|
||||
key = 'string' === typeof key ? key : key.toString();
|
||||
etv.onKeyPress(key, isSpecial);
|
||||
});
|
||||
|
||||
client.on('special key', function onSK(keyName) {
|
||||
etv.onSpecialKeyPress(keyName);
|
||||
});
|
||||
*/
|
||||
|
||||
/*
|
||||
var vc = new view.ViewsController(client);
|
||||
vc.loadFromMCIMap(mci);
|
||||
vc.setViewOrder();
|
||||
vc.switchFocus(1);
|
||||
vc.setSubmitView(2);
|
||||
|
||||
vc.on('action', function onAction(act) {
|
||||
if('submit' === act.action) {
|
||||
var un = vc.getView(1).value;
|
||||
var pw = vc.getView(2).value;
|
||||
console.log('userName: ' + un);
|
||||
console.log('password: ' + pw);
|
||||
|
||||
user.User.loadWithCredentials(un, pw, function onUser(err, user) {
|
||||
if(err) {
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(user.id);
|
||||
});
|
||||
}
|
||||
});
|
||||
*/
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue