* clearScreen -> cls for nostalgia

* module cleanup: some simple modules moved to general_menu_method.js @methods
* More work on menu configuration & options
- Removed formatting of args for now. Too tied to MCI, not really needed with argName stuff
This commit is contained in:
Bryan Ashby 2015-04-20 22:50:58 -06:00
parent 8db72430d3
commit ec5f1836c5
16 changed files with 350 additions and 1233 deletions

View file

@ -101,7 +101,7 @@ var SGRValues = {
yellowBG : 43,
blueBG : 44,
cyanBG : 47,
cyanBG : 46,
whiteBG : 47,
};

View file

@ -1,6 +1,8 @@
/* jslint node: true */
'use strict';
var Config = require('./config.js').config;
var _ = require('lodash');
var assert = require('assert');
@ -49,4 +51,4 @@ function getArtAsset(art, cb) {
asset : art,
};
}
}
}

View file

@ -3,7 +3,6 @@
// ENiGMA½
var conf = require('./config.js');
var moduleUtil = require('./module_util.js');
var logger = require('./logger.js');
var miscUtil = require('./misc_util.js');
var database = require('./database.js');
@ -75,55 +74,6 @@ function bbsMain() {
);
}
/*
function bbsMain() {
var mainArgs = parseArgs();
var configPathSupplied = false;
var configPath = conf.defaultPath();
if(mainArgs.indexOf('--help') > 0) {
// :TODO: display help
} else {
var argCount = mainArgs.length;
for(var i = 0; i < argCount; ++i) {
var arg = mainArgs[i];
if('--config' == arg) {
configPathSupplied = true;
configPath = mainArgs[i + 1];
}
}
}
try {
conf.initFromFile(configPath);
} catch(e) {
//
// If the user supplied a config and we can't read, parse, whatever
// then output a error and bail.
//
if(configPathSupplied) {
if(e.code === 'ENOENT') {
console.error('Configuration file does not exist: ' + configPath);
}
return;
}
console.log('No configuration file found, creating defaults.');
conf.createDefault();
}
initialize(function onInit(err) {
if(err) {
console.error('Error initializing: ' + util.inspect(err));
return;
}
startListening();
});
}
*/
function parseArgs() {
var args = [];
process.argv.slice(2).forEach(function(val, index, array) {
@ -179,6 +129,8 @@ function startListening() {
return [];
}
var moduleUtil = require('./module_util.js'); // late load so we get Config
moduleUtil.loadModulesForCategory('servers', function onServerModule(err, module) {
if(err) {
logger.log.info(err);
@ -261,12 +213,12 @@ function prepareClient(client, cb) {
[
function getRandTheme(callback) {
theme.getRandomTheme(function randTheme(err, themeId) {
client.user.properties.art_theme_id = themeId || '';
client.user.properties.theme_id = themeId || '';
callback(null);
});
},
function setCurrentThemeInfo(callback) {
theme.getThemeInfo(client.user.properties.art_theme_id, function themeInfo(err, info) {
theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) {
client.currentThemeInfo = info;
callback(null);
});
@ -277,7 +229,7 @@ function prepareClient(client, cb) {
}
);
} else {
client.user.properties.art_theme_id = conf.config.preLoginTheme;
client.user.properties.theme_id = conf.config.preLoginTheme;
cb(null);
}
}

View file

@ -8,6 +8,7 @@ var Log = require('./logger.js').log;
var ansi = require('./ansi_term.js');
var asset = require('./asset.js');
var ViewController = require('./view_controller.js').ViewController;
var menuUtil = require('./menu_util.js');
var async = require('async');
var assert = require('assert');
@ -90,7 +91,7 @@ function MenuModule(options) {
}
// Prompts *must* have art. If it's missing it's an error
// :TODO: allow inline prompts in the future, e.g. @inline:memberName -> "memberName" : { ... }
// :TODO: allow inline prompts in the future, e.g. @inline:memberName -> { "memberName" : { "text" : "stuff", ... } }
var promptConfig = self.menuConfig.promptConfig;
self.displayArtAsset(promptConfig.art, function displayed(err, mciMap) {
mciData.prompt = mciMap;
@ -114,92 +115,6 @@ function MenuModule(options) {
}
);
};
/*
this.initSequence2 = function() {
async.waterfall(
[
function beforeDisplayArt(callback) {
self.beforeArt();
callback(null);
},
function displayArtAsset(callback) {
var artAsset = asset.getArtAsset(self.menuConfig.art);
if(!artAsset) {
// no art to display
callback(null, null);
return;
}
var dispOptions = {
name : artAsset.asset,
client : self.client,
font : self.menuConfig.font,
};
if('art' === artAsset.type) {
theme.displayThemeArt(dispOptions, function displayedArt(err, mciMap) {
callback(null, mciMap);
});
} else if('method' === artAsset.type) {
// :TODO: support fetching the asset (e.g. rendering into buffer) -> display it.
}
},
function afterArtDisplayed(mciMap, callback) {
if(mciMap) {
self.mciReady(mciMap);
}
callback(null);
},
function loadPrompt(callback) {
if(!_.isString(self.menuConfig.prompt)) {
// no prompt supplied
callback(null, null);
return;
}
var loadPromptOpts = {
name : self.menuConfig.prompt,
client : self.client,
};
promptUtil.loadPrompt(loadPromptOpts, function promptLoaded(err, prompt) {
callback(err, prompt);
});
},
function displayPrompt(prompt, callback) {
if(!prompt) {
callback(null);
return;
}
// :TODO: go to proper prompt location before displaying
var dispOptions = {
art : prompt.artInfo.data,
sauce : prompt.artInfo.sauce,
client : self.client,
font : prompt.config.font,
};
art.display(dispOptions, function displayed(err, mciMap) {
});
}
],
function complete(err) {
if(err) {
// :TODO: Log me!!! ... and what else?
console.log(err);
}
self.finishedLoading();
}
);
};
*/
}
require('util').inherits(MenuModule, PluginModule);
@ -225,7 +140,7 @@ MenuModule.prototype.addViewController = function(name, vc) {
};
MenuModule.prototype.beforeArt = function() {
if(this.menuConfig.options.clearScreen) {
if(this.menuConfig.options.cls) {
this.client.term.write(ansi.resetScreen());
}
};
@ -233,6 +148,50 @@ MenuModule.prototype.beforeArt = function() {
MenuModule.prototype.mciReady = function(mciData) {
};
MenuModule.prototype.standardMCIReadyHandler = function(mciData) {
//
// A quick rundown:
// * We may have mciData.menu, mciData.prompt, or both.
// * Prompt form is favored over menu form if both are present.
// * Standard/prefdefined MCI entries must load both (e.g. %BN is expected to resolve)
//
var self = this;
self.viewControllers = {};
//var vcOpts = { client : self.client };
_.forEach(mciData, function entry(mciMap, name) {
assert('menu' === name || 'prompt' === name);
self.viewControllers[name] = new ViewController( { client : self.client } );
});
var viewsReady = function(err) {
// :TODO: what should really happen here?
if(err) {
Log.warn(err);
}
};
if(self.viewControllers.menu) {
var menuLoadOpts = {
mciMap : mciData.menu,
callingMenu : self,
withoutForm : _.isObject(mciData.prompt),
};
self.viewControllers.menu.loadFromMenuConfig(menuLoadOpts, viewsReady);
}
if(self.viewControllers.prompt) {
var promptLoadOpts = {
callingMenu : self,
mciMap : mciData.prompt,
};
self.viewControllers.prompt.loadFromPromptConfig(promptLoadOpts, viewsReady);
}
};
MenuModule.prototype.finishedLoading = function() {
var self = this;
@ -243,5 +202,12 @@ MenuModule.prototype.finishedLoading = function() {
setTimeout(function nextTimeout() {
self.client.gotoMenuModule( { name : self.menuConfig.next } );
}, this.menuConfig.options.nextTimeout);
} else {
if(!_.isObject(self.menuConfig.form) && !_.isString(self.menuConfig.prompt) &&
_.isString(self.menuConfig.action))
{
menuUtil.handleAction(self.client, null, self.menuConfig);
}
}
};

View file

@ -19,6 +19,7 @@ var stripJsonComments = require('strip-json-comments');
exports.loadMenu = loadMenu;
exports.getFormConfigByIDAndMap = getFormConfigByIDAndMap;
exports.handleAction = handleAction;
function loadModJSON(fileName, cb) {
@ -93,11 +94,18 @@ function loadMenu(options, cb) {
});
},
function loadMenuModule(menuConfig, callback) {
var moduleName = menuConfig.module || 'standard_menu';
moduleUtil.loadModule(moduleName, 'mods', function moduleLoaded(err, mod) {
var modSupplied = _.isString(menuConfig.module);
var modLoadOpts = {
name : modSupplied ? menuConfig.module : 'standard_menu',
path : modSupplied ? Config.paths.mods : __dirname,
category : modSupplied ? 'mods' : null,
};
moduleUtil.loadModuleEx(modLoadOpts, function moduleLoaded(err, mod) {
var modData = {
name : moduleName,
name : modLoadOpts.name,
config : menuConfig,
mod : mod,
};
@ -124,63 +132,6 @@ function loadMenu(options, cb) {
);
}
function loadMenu2(options, cb) {
assert(options);
assert(options.name);
assert(options.client);
var name = options.name;
var client = options.client;
/*
TODO:
* check access / ACS
*
*/
async.waterfall(
[
// :TODO: Need a good way to cache this information & only (re)load if modified
function loadMenuConfig(callback) {
var configJsonPath = paths.join(conf.config.paths.mods, 'menu.json');
fs.readFile(configJsonPath, { encoding : 'utf8' }, function onMenuConfig(err, data) {
try {
var menuJson = JSON.parse(stripJsonComments(data));
if(!_.isObject(menuJson[name])) {
callback(new Error('No configuration entry for \'' + name + '\''));
} else {
callback(err, menuJson[name]);
}
} catch(e) {
callback(e);
}
});
},
function menuConfigLoaded(menuConfig, callback) {
var moduleName = menuConfig.module || 'standard_menu';
moduleUtil.loadModule(moduleName, 'mods', function onModule(err, mod) {
callback(err, mod, menuConfig, moduleName);
});
}
],
function complete(err, mod, menuConfig, moduleName) {
if(err) {
cb(err);
} else {
Log.debug(
{ moduleName : moduleName, args : options.args, config : menuConfig, info : mod.moduleInfo },
'Creating menu module instance');
// :TODO: throw from MenuModule() - catch here
cb(null, new mod.getModule({ menuConfig : menuConfig, args : options.args } ));
}
}
);
}
function getFormConfigByIDAndMap(menuConfig, formId, mciMap, cb) {
assert(_.isObject(menuConfig));
@ -209,3 +160,42 @@ function getFormConfigByIDAndMap(menuConfig, formId, mciMap, cb) {
cb(new Error('No matching form configuration found'));
}
function handleAction(client, formData, conf) {
assert(_.isObject(conf));
assert(_.isString(conf.action));
var actionAsset = asset.parseAsset(conf.action);
assert(_.isObject(actionAsset));
switch(actionAsset.type) {
case 'method' :
if(_.isString(actionAsset.location)) {
try {
// allow ".js" omission
if(''=== paths.extname(actionAsset.location)) {
actionAsset.location += '.js';
}
var methodMod = require(paths.join(Config.paths.mods, actionAsset.location));
if(_.isFunction(methodMod[actionAsset.asset])) {
methodMod[actionAsset.asset](client.currentMenuModule, formData, conf.extraArgs);
}
} catch(e) {
Log.error( { error : e, methodName : actionAsset.asset }, 'Failed to execute asset method');
}
} else {
// local to current module
var currentModule = client.currentMenuModule;
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) {
currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs);
}
}
break;
case 'menu' :
client.gotoMenuModule( { name : actionAsset.asset, formData : formData, extraArgs : conf.extraArgs } );
break;
}
}

View file

@ -1,51 +1,46 @@
/* jslint node: true */
'use strict';
var Config = require('./config.js').config;
var miscUtil = require('./misc_util.js');
var fs = require('fs');
var paths = require('path');
var conf = require('./config.js');
var miscUtil = require('./misc_util.js');
var _ = require('lodash');
var assert = require('assert');
// exports
exports.loadModuleEx = loadModuleEx;
exports.loadModule = loadModule;
exports.loadModulesForCategory = loadModulesForCategory;
function loadModule(name, category, cb) {
var config = conf.config;
var path = config.paths[category];
function loadModuleEx(options, cb) {
assert(_.isObject(options));
assert(_.isString(options.name));
assert(_.isString(options.path));
if(!path) {
cb(new Error('not sure where to look for "' + name + '" of category "' + category + '"'));
return;
}
var modConfig = _.isObject(Config[options.category]) ? Config[options.category][options.name] : null;
// update conf to point at this module's section, if any
config = config[category] ? config[category][name] : null;
if(config && false === config.enabled) {
cb(new Error('module "' + name + '" is disabled'));
if(_.isObject(modConfig) && false === modConfig.enabled) {
cb(new Error('Module "' + options.name + '" is disabled'));
return;
}
try {
var mod = require(paths.join(path, name + '.js'));
var mod = require(paths.join(options.path, options.name + '.js'));
if(!mod.moduleInfo) {
cb(new Error('module is missing \'moduleInfo\' section'));
if(!_.isObject(mod.moduleInfo)) {
cb(new Error('Module is missing "moduleInfo" section'));
return;
}
if(!_.isFunction(mod.getModule)) {
cb(new Error('Invalid or missing missing \'getModule\' method'));
cb(new Error('Invalid or missing "getModule" method for module!'));
return;
}
// :TODO: what was the point of this? Remove it
mod.runtime = {
config : config
};
// Safe configuration, if any, for convience to the module
mod.runtime = { config : modConfig };
cb(null, mod);
} catch(e) {
@ -53,8 +48,21 @@ function loadModule(name, category, cb) {
}
}
function loadModule(name, category, cb) {
var path = Config.paths[category];
if(!_.isString(path)) {
cb(new Error('Not sure where to look for "' + name + '" of category "' + category + '"'));
return;
}
loadModuleEx( { name : name, path : path, category : category }, function loaded(err, mod) {
cb(err, mod);
});
}
function loadModulesForCategory(category, cb) {
var path = conf.config.paths[category];
var path = Config.paths[category];
fs.readdir(path, function onFiles(err, files) {
if(err) {

34
core/standard_menu.js Normal file
View file

@ -0,0 +1,34 @@
/* jslint node: true */
'use strict';
var MenuModule = require('./menu_module.js').MenuModule;
exports.getModule = StandardMenuModule;
exports.moduleInfo = {
name : 'Standard Menu Module',
desc : 'A Menu Module capable of handing standard configurations',
author : 'NuSkooler',
};
function StandardMenuModule(menuConfig) {
MenuModule.call(this, menuConfig);
}
require('util').inherits(StandardMenuModule, MenuModule);
StandardMenuModule.prototype.enter = function(client) {
StandardMenuModule.super_.prototype.enter.call(this, client);
};
StandardMenuModule.prototype.beforeArt = function() {
StandardMenuModule.super_.prototype.beforeArt.call(this);
};
StandardMenuModule.prototype.mciReady = function(mciData) {
StandardMenuModule.super_.prototype.mciReady.call(this, mciData);
// we do this so other modules can be both customized and still perform standard tasks
StandardMenuModule.super_.prototype.standardMCIReadyHandler.call(this, mciData);
};

View file

@ -138,7 +138,7 @@ function displayThemeArt(options, cb) {
assert(_.isObject(options.client));
assert(_.isString(options.name));
getThemeArt(options.name, options.client.user.properties.art_theme_id, function themeArt(err, artInfo) {
getThemeArt(options.name, options.client.user.properties.theme_id, function themeArt(err, artInfo) {
if(err) {
cb(err);
} else {

View file

@ -183,7 +183,7 @@ View.prototype.onSpecialKeyPress = function(keyName) {
if(this.isSpecialKeyMapped('accept', keyName)) {
this.emit('action', 'accept');
} else if(this.isSpecialKeyMapped('next', keyName)) {
console.log('next')
this.emit('action', 'next');
}
};

View file

@ -1,432 +0,0 @@
/* jslint node: true */
'use strict';
var util = require('util');
var ansi = require('./ansi_term.js');
var miscUtil = require('./misc_util.js');
var strUtil = require('./string_util.js');
var assert = require('assert');
var events = require('events');
var logger = require('./logger.js');
exports.View = View;
exports.LabelView = LabelView;
exports.TextEditView = TextEditView;
exports.ViewsController = ViewsController;
function View(client, options) {
events.EventEmitter.call(this);
var self = this;
this.client = client;
this.options = options;
this.acceptsFocus = false;
this.acceptsKeys = false;
}
util.inherits(View, events.EventEmitter);
View.prototype.place = function(pos) {
//
// We allow [x, y], { x : x, y : y }, or (x, y)
//
if(util.isArray(pos)) {
this.x = pos[0];
this.y = pos[1];
} else if(pos.x && pos.y) {
this.x = pos.x;
this.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.x = x;
this.y = y;
}
}
assert(this.x > 0 && this.x < this.client.term.termHeight);
assert(this.y > 0 && this.y < this.client.term.termWidth);
this.client.term.write(ansi.goto(this.x, this.y));
};
View.prototype.setFocus = function(focused) {
assert(this.x);
assert(this.y);
};
View.prototype.getColor = function() {
return this.options.color;
};
View.prototype.getFocusColor = function() {
return this.options.focusColor || this.getColor();
};
function LabelView(client, text, options) {
View.call(this, client);
var self = this;
if(options) {
if(options.maxWidth) {
text = text.substr(0, options.maxWidth);
}
text = strUtil.stylizeString(text, options.style);
}
this.value = text;
this.height = 1;
this.width = this.value.length;
}
util.inherits(LabelView, View);
LabelView.prototype.place = function(pos) {
LabelView.super_.prototype.place.apply(this, arguments);
this.client.term.write(this.value);
};
///////////////////////////////////////////////////////////////////////////////
var INTERACTIVE_VIEW_DEFAULT_SPECIAL_KEYSET = {
enter : [ 'enter' ],
exit : [ 'esc' ],
backspace : [ 'backspace', 'del' ],
next : [ 'tab' ],
};
function InteractiveView(client, options) {
View.call(this, client);
this.acceptsFocus = true;
this.acceptsKeys = true;
if(options) {
this.options = options;
} else {
this.options = {
};
}
this.options.specialKeySet = miscUtil.valueWithDefault(
options.specialKeySet, INTERACTIVE_VIEW_DEFAULT_SPECIAL_KEYSET
);
this.isSpecialKeyFor = function(checkFor, specialKey) {
return this.options.specialKeySet[checkFor].indexOf(specialKey) > -1;
};
this.backspace = function() {
this.client.term.write('\b \b');
};
}
util.inherits(InteractiveView, View);
InteractiveView.prototype.setFocus = function(focused) {
InteractiveView.super_.prototype.setFocus.call(this, focused);
this.hasFocus = focused;
};
InteractiveView.prototype.setNextView = function(id) {
this.nextId = id;
};
var TEXT_EDIT_INPUT_TYPES = [
'normal', 'N',
'password', 'P',
'upper', 'U',
'lower', 'l',
];
function TextEditView(client, options) {
InteractiveView.call(this, client, options);
if(!options) {
this.options.multiLine = false;
}
this.options.inputType = miscUtil.valueWithDefault(this.options.inputType, 'normal');
assert(TEXT_EDIT_INPUT_TYPES.indexOf(this.options.inputType) > -1);
if('password' === this.options.inputType || 'P' === this.options.inputType) {
this.options.inputMaskChar = miscUtil.valueWithDefault(this.options.inputMaskChar, '*').substr(0,1);
}
this.value = miscUtil.valueWithDefault(options.defaultValue, '');
// :TODO: hilight, text, etc., should come from options or default for theme if not provided
// focus=fg + bg
// standard=fg +bg
}
util.inherits(TextEditView, InteractiveView);
TextEditView.prototype.place = function(pos) {
TextEditView.super_.prototype.place.apply(this, arguments);
if(!this.options.maxWidth) {
this.options.maxWidth = this.client.term.termWidth - this.x;
}
this.width = this.options.maxWidth;
};
TextEditView.prototype.setFocus = function(focused) {
TextEditView.super_.prototype.setFocus.call(this, focused);
this.client.term.write(ansi.goto(this.x, this.y));
this.redraw();
this.client.term.write(ansi.goto(this.x, this.y + this.value.length));
};
TextEditView.prototype.redraw = function() {
var color = this.hasFocus ? this.getFocusColor() : this.getColor();
this.client.term.write(ansi.sgr(color.flags, color.fg, color.bg));
this.client.term.write(strUtil.pad(this.value, this.width));
};
TextEditView.prototype.onKeyPressed = function(k, isSpecial) {
assert(this.hasFocus);
if(isSpecial) {
return; // handled via onSpecialKeyPressed()
}
if(this.value.length < this.options.maxWidth) {
k = strUtil.stylizeString(k, this.options.inputType);
this.value += k;
if('P' === this.options.inputType.charAt(0).toUpperCase()) {
this.client.term.write(this.options.inputMaskChar);
} else {
this.client.term.write(k);
}
}
};
TextEditView.prototype.onSpecialKeyPressed = function(keyName) {
assert(this.hasFocus);
console.log(keyName);
if(this.isSpecialKeyFor('backspace', keyName)) {
if(this.value.length > 0) {
this.value = this.value.substr(0, this.value.length - 1);
this.backspace();
}
} else if(this.isSpecialKeyFor('enter', keyName)) {
if(this.options.multiLine) {
} else {
this.emit('action', 'accepted');
}
} else if(this.isSpecialKeyFor('next', keyName)) {
this.emit('action', 'next');
}
};
function MenuView(options) {
}
function VerticalMenuView(options) {
}
///////////////////////////////////////////////////////
// :TODO: Move to view_controller.js
function ViewsController(client) {
events.EventEmitter.call(this);
var self = this;
this.views = {};
this.client = client;
client.on('key press', function onKeyPress(k, isSpecial) {
if(self.focusedView && self.focusedView.acceptsKeys) {
self.focusedView.onKeyPressed(k, isSpecial);
}
});
client.on('special key', function onSpecialKey(keyName) {
if(self.focusedView && self.focusedView.acceptsKeys) {
self.focusedView.onSpecialKeyPressed(keyName);
}
});
this.onViewAction = function(action) {
console.log(action + ' @ ' + this.id);
if('next' === action) {
self.emit('action', { view : this, action : action });
self.nextFocus();
} else if('accepted' === action) {
if(self.submitViewId == this.id) {
self.emit('action', { view : this, action : 'submit' });
} else {
self.nextFocus();
}
}
/*
if(self.submitViewId == this.id) {
self.emit('action', { view : this, action : 'submit' });
} else {
self.emit('action', { view : this, action : action });
if('accepted' === action || 'next' === action) {
self.nextFocus();
}
}*/
};
}
util.inherits(ViewsController, events.EventEmitter);
ViewsController.prototype.addView = function(viewInfo) {
viewInfo.view.id = viewInfo.id;
this.views[viewInfo.id] = {
view : viewInfo.view,
pos : viewInfo.pos
};
viewInfo.view.place(viewInfo.pos);
};
ViewsController.prototype.viewExists = function(id) {
return id in this.views;
};
ViewsController.prototype.getView = function(id) {
return this.views[id].view;
};
ViewsController.prototype.switchFocus = function(id) {
var view = this.getView(id);
if(!view) {
logger.log.warn('Invalid view', { id : id });
return false;
}
if(!view.acceptsFocus) {
logger.log.warn('View does not accept focus', { id : id });
return false;
}
this.focusedView = view;
view.setFocus(true);
};
ViewsController.prototype.nextFocus = function() {
var nextId = this.focusedView.nextId;
this.focusedView.setFocus(false);
if(nextId > 0) {
this.switchFocus(nextId);
} else {
this.switchFocus(this.firstId);
}
};
ViewsController.prototype.setSubmitView = function(id) {
this.submitViewId = id;
};
ViewsController.prototype.loadFromMCIMap = function(mciMap) {
var factory = new MCIViewFactory(this.client);
var view;
var mci;
for(var entry in mciMap) {
mci = mciMap[entry];
view = factory.createFromMCI(mci);
if(view) {
this.addView({
id : mci.id,
view : view,
pos : mci.position
});
view.on('action', this.onViewAction);
}
}
};
ViewsController.prototype.setViewOrder = function(order) {
var idOrder = [];
if(order) {
// :TODO:
} else {
for(var id in this.views) {
idOrder.push(id);
}
// :TODO: simply sort
console.log(idOrder);
this.firstId = idOrder[0];
}
var view;
for(var i = 0; i < idOrder.length - 1; ++i) {
view = this.getView(idOrder[i]);
if(view) {
view.setNextView(idOrder[i + 1]);
}
}
};
///////////////////////////////////////////////////
function MCIViewFactory(client, mci) {
this.client = client;
}
MCIViewFactory.prototype.createFromMCI = function(mci) {
assert(mci.code);
assert(mci.id > 0);
var view;
var options = {};
switch(mci.code) {
case 'EV' :
if(mci.args.length > 0) {
options.maxWidth = mci.args[0];
}
if(mci.args.length > 1) {
options.inputType = mci.args[1];
}
options.color = mci.color;
options.focusColor = mci.focusColor;
view = new TextEditView(this.client, options);
break;
}
return view;
};

View file

@ -22,6 +22,7 @@ var MCI_REGEXP = /([A-Z]{2})([0-9]{1,2})/;
function ViewController(options) {
assert(_.isObject(options));
assert(_.isObject(options.client));
events.EventEmitter.call(this);
@ -32,7 +33,7 @@ function ViewController(options) {
this.formId = options.formId || 0;
this.mciViewFactory = new MCIViewFactory(this.client);
this.onClientKeyPress = function(key, isSpecial) {
this.clientKeyPressHandler = function(key, isSpecial) {
if(isSpecial) {
return;
}
@ -43,7 +44,10 @@ function ViewController(options) {
}
};
this.onClientSpecialKeyPress = function(keyName) {
this.clientSpecialKeyHandler = function(keyName) {
// :TODO: Handle special key mappings from config, e.g. 'esc'
if(self.focusedView && self.focusedView.acceptsInput) {
self.focusedView.onSpecialKeyPress(keyName);
}
@ -127,42 +131,6 @@ function ViewController(options) {
self.emitSwitchFocus = false;
};
this.handleSubmitAction = function(callingMenu, formData, conf) {
assert(_.isObject(conf));
assert(_.isString(conf.action));
var actionAsset = asset.parseAsset(conf.action);
assert(_.isObject(actionAsset));
var extraArgs;
if(conf.extraArgs) {
extraArgs = self.formatMenuArgs(conf.extraArgs);
}
switch(actionAsset.type) {
case 'method' :
if(_.isString(actionAsset.location)) {
// :TODO: allow omition of '.js'
var methodMod = require(paths.join(Config.paths.mods, actionAsset.location));
if(_.isFunction(methodMod[actionAsset.asset])) {
methodMod[actionAsset.asset](callingMenu, formData, extraArgs);
}
} else {
// local to current module
var currentModule = self.client.currentMenuModule;
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) {
currentModule.menuMethods[actionAsset.asset](formData, extraArgs);
}
}
break;
case 'menu' :
// :TODO: update everythign to handle this format
self.client.gotoMenuModule( { name : actionAsset.asset, submitData : formData, extraArgs : extraArgs } );
break;
}
};
this.createViewsFromMCI = function(mciMap, cb) {
async.each(Object.keys(mciMap), function entry(name, nextItem) {
var mci = mciMap[name];
@ -247,8 +215,8 @@ ViewController.prototype.attachClientEvents = function() {
return;
}
this.client.on('key press', this.onClientKeyPress);
this.client.on('special key', this.onClientSpecialKeyPress);
this.client.on('key press', this.clientKeyPressHandler);
this.client.on('special key', this.clientSpecialKeyHandler);
this.attached = true;
};
@ -258,8 +226,8 @@ ViewController.prototype.detachClientEvents = function() {
return;
}
this.client.removeListener('key press', this.onClientKeyPress);
this.client.removeListener('special key', this.onClientSpecialKeyPress);
this.client.removeListener('key press', this.clientKeyPressHandler);
this.client.removeListener('special key', this.clientSpecialKeyHandler);
for(var id in this.views) {
this.views[id].removeAllListeners();
@ -356,13 +324,10 @@ ViewController.prototype.loadFromMCIMap = function(mciMap) {
ViewController.prototype.loadFromPromptConfig = function(options, cb) {
assert(_.isObject(options));
assert(_.isObject(options.callingMenu));
assert(_.isObject(options.callingMenu.menuConfig));
assert(_.isObject(options.callingMenu.menuConfig.promptConfig));
assert(_.isObject(options.mciMap));
var promptConfig = options.callingMenu.menuConfig.promptConfig;
var self = this;
var promptConfig = self.client.currentMenuModule.menuConfig.promptConfig;
var initialFocusId = 1; // default to first
async.waterfall(
@ -383,7 +348,7 @@ ViewController.prototype.loadFromPromptConfig = function(options, cb) {
self.on('submit', function promptSubmit(formData) {
Log.trace( { formData : self.getLogFriendlyFormData(formData) }, 'Prompt submit');
self.handleSubmitAction(options.callingMenu, formData, options.callingMenu.menuConfig);
menuUtil.handleAction(self.client, formData, self.client.currentMenuModule.menuConfig);
});
callback(null);
@ -403,8 +368,6 @@ ViewController.prototype.loadFromPromptConfig = function(options, cb) {
ViewController.prototype.loadFromMenuConfig = function(options, cb) {
assert(_.isObject(options));
assert(_.isObject(options.callingMenu));
assert(_.isObject(options.callingMenu.menuConfig));
assert(_.isObject(options.mciMap));
var self = this;
@ -417,33 +380,40 @@ ViewController.prototype.loadFromMenuConfig = function(options, cb) {
// method for comparing submitted form data to configuration entries
var actionBlockValueComparator = function(formValue, actionValue) {
//
// Any key(s) in actionValue must:
// 1) Be present in formValue
// 2) Either:
// a) Be set to null (wildcard/any)
// b) Have matching value(s)
// For a match to occur, one of the following must be true:
//
var keys = Object.keys(actionValue);
for(var i = 0; i < keys.length; ++i) {
var name = keys[i];
// submit data contains config key?
if(!_.has(formValue, name)) {
return false; // not present in what was submitted
}
if(null !== actionValue[name] && actionValue[name] !== formValue[name]) {
// * actionValue is a Object:
// a) All key/values must exactly match
// b) value is null; The key (view ID) must be present
// in formValue. This is a wildcard/any match.
// * actionValue is a Number: This represents a view ID that
// must be present in formValue.
//
if(_.isNumber(actionValue)) {
if(_.isUndefined(formValue[actionValue])) {
return false;
}
} else {
var actionValueKeys = Object.keys(actionValue);
for(var i = 0; i < actionValueKeys.length; ++i) {
var viewId = actionValueKeys[i];
if(!_.has(formValue, viewId)) {
return false;
}
if(null !== actionValue[viewId] && actionValue[viewId] !== formValue[viewId]) {
return false;
}
}
}
return true;
};
async.waterfall(
[
function findMatchingFormConfig(callback) {
menuUtil.getFormConfigByIDAndMap(options.callingMenu.menuConfig, formIdKey, options.mciMap, function matchingConfig(err, fc) {
menuUtil.getFormConfigByIDAndMap(self.client.currentMenuModule.menuConfig, formIdKey, options.mciMap, function matchingConfig(err, fc) {
formConfig = fc;
if(err) {
@ -501,7 +471,7 @@ ViewController.prototype.loadFromMenuConfig = function(options, cb) {
var actionBlock = confForFormId[c];
if(_.isEqual(formData.value, actionBlock.value, actionBlockValueComparator)) {
self.handleSubmitAction(options.callingMenu, formData, actionBlock);
menuUtil.handleAction(self.client, formData, actionBlock);
break; // there an only be one...
}
}
@ -524,176 +494,6 @@ ViewController.prototype.loadFromMenuConfig = function(options, cb) {
);
};
ViewController.prototype.loadFromMCIMapAndConfig = function(options, cb) {
assert(options.mciMap);
var factory = new MCIViewFactory(this.client);
var self = this;
var formIdKey = options.formId ? options.formId.toString() : '0';
var initialFocusId;
var formConfig;
var mciRegEx = /([A-Z]{2})([0-9]{1,2})/;
// :TODO: remove all the passing of fromConfig - use local
// :TODO: break all of this up ... a lot
async.waterfall(
[
function getFormConfig(callback) {
menuUtil.getFormConfigByIDAndMap(options.menuConfig, formIdKey, options.mciMap, function onFormConfig(err, fc) {
formConfig = fc;
if(err) {
// :TODO: fix logging of err here:
Log.trace(
{ err : err.toString(), mci : Object.keys(options.mciMap), formIdKey : formIdKey } ,
'Unable to load menu configuration');
}
callback(null);
});
},
function createViews(callback) {
self.createViewsFromMCI(options.mciMap, function viewsCreated(err) {
callback(err);
});
},
function applyFormConfig(callback) {
if(formConfig) {
async.each(Object.keys(formConfig.mci), function onMciConf(mci, eachCb) {
var mciMatch = mci.match(mciRegEx); // :TODO: what about auto-generated IDs? Do they simply not apply to menu configs?
var viewId = parseInt(mciMatch[2]);
var view = self.getView(viewId);
var mciConf = formConfig.mci[mci];
// :TODO: Break all of this up ... and/or better way of doing it
if(mciConf.items) {
view.setItems(mciConf.items);
}
if(mciConf.submit) {
view.submit = true; // :TODO: should really be actual value
}
if(mciConf.text) {
view.setText(mciConf.text);
}
if(mciConf.focus) {
initialFocusId = viewId;
}
eachCb(null);
},
function eachMciConfComplete(err) {
callback(err);
});
} else {
callback(null);
}
},
function mapMenuSubmit(callback) {
if(formConfig) {
//
// If we have a 'submit' section, create a submit handler
// and map the various entries to menus/etc.
//
if(_.isObject(formConfig.submit)) {
// :TODO: If this model is kept, formData does not need to include actual data, just form ID & submitID
// we can get the rest here via each view in form -> getData()
self.on('submit', function onSubmit(formData) {
Log.debug( { formData : formData }, 'Submit form');
var confForFormId;
if(_.isObject(formConfig.submit[formData.submitId])) {
confForFormId = formConfig.submit[formData.submitId];
} else if(_.isObject(formConfig.submit['*'])) {
confForFormId = formConfig.submit['*'];
} else {
// no configuration for this submitId
return;
}
var formValueCompare = function(formDataValue, formConfigValue) {
//
// Any key(s) in formConfigValue must:
// 1) be present in formDataValue
// 2) must either:
// a) be set to null (wildcard/any)
// b) have matching values
//
var formConfigValueKeys = Object.keys(formConfigValue);
for(var k = 0; k < formConfigValueKeys.length; ++k) {
var memberKey = formConfigValueKeys[k];
// submit data contains config key?
if(!_.has(formDataValue, memberKey)) {
return false; // not present in what was submitted
}
if(null !== formConfigValue[memberKey] && formConfigValue[memberKey] !== formDataValue[memberKey]) {
return false;
}
}
return true;
};
var conf;
for(var c = 0; c < confForFormId.length; ++c) {
conf = confForFormId[c];
if(_.isEqual(formData.value, conf.value, formValueCompare)) {
if(!conf.action) {
continue;
}
var formattedArgs;
if(conf.args) {
formattedArgs = self.formatMenuArgs(conf.args);
}
var actionAsset = asset.parseAsset(conf.action);
assert(_.isObject(actionAsset));
if('method' === actionAsset.type) {
if(actionAsset.location) {
// :TODO: call with (client, args, ...) at least.
} else {
// local to current module
var currentMod = self.client.currentMenuModule;
if(currentMod.menuMethods[actionAsset.asset]) {
currentMod.menuMethods[actionAsset.asset](formattedArgs);
}
}
} else if('menu' === actionAsset.type) {
self.client.gotoMenuModule( { name : actionAsset.asset, args : formattedArgs } );
}
}
}
});
}
}
callback(null);
},
function setInitialFocus(callback) {
if(initialFocusId) {
self.switchFocus(initialFocusId);
}
callback(null);
}
],
function complete(err) {
if(cb) {
cb(err);
}
}
);
};
ViewController.prototype.formatMCIString = function(format) {
var self = this;
var view;
@ -709,6 +509,7 @@ ViewController.prototype.formatMCIString = function(format) {
});
};
/*
ViewController.prototype.formatMenuArgs = function(args) {
var self = this;
@ -718,4 +519,5 @@ ViewController.prototype.formatMenuArgs = function(args) {
}
return value;
});
};
};
*/