* Lots of WIP around menu.json handlinge/etc.

This commit is contained in:
Bryan Ashby 2015-03-29 21:47:48 -06:00
parent 8c9b0e729f
commit d6ffa2b26e
6 changed files with 162 additions and 42 deletions

View File

@ -66,6 +66,13 @@ function loadMenu(name, client, cb) {
function getFormConfig(menuConfig, mciMap, cb) { function getFormConfig(menuConfig, mciMap, cb) {
assert(menuConfig);
if(!menuConfig.form) {
cb(new Error('No form section specified for menu'));
return;
}
async.filter( async.filter(
menuConfig.form, menuConfig.form,
function check(form, callback) { function check(form, callback) {
@ -75,12 +82,14 @@ function getFormConfig(menuConfig, mciMap, cb) {
} }
var count = form.mciReq.length; var count = form.mciReq.length;
if(Object.keys(mciMap).length === count) {
for(var i = 0; i < count; ++i) { for(var i = 0; i < count; ++i) {
if(!mciMap[form.mciReq[i]]) { if(!mciMap[form.mciReq[i]]) {
callback(false); callback(false);
} }
} }
callback(true); callback(true);
}
}, },
function filtered(form) { function filtered(form) {
if(form.length > 0) { if(form.length > 0) {

View File

@ -1,13 +1,16 @@
/* jslint node: true */ /* jslint node: true */
'use strict'; 'use strict';
// ENiGMA½
var MCIViewFactory = require('./mci_view_factory.js').MCIViewFactory;
var menuUtil = require('./menu_util.js');
var Log = require('./logger.js').log;
var events = require('events'); var events = require('events');
var util = require('util'); var util = require('util');
var assert = require('assert'); var assert = require('assert');
var MCIViewFactory = require('./mci_view_factory.js').MCIViewFactory;
var menuUtil = require('./menu_util.js');
var async = require('async'); var async = require('async');
var ld = require('lodash');
exports.ViewController = ViewController; exports.ViewController = ViewController;
@ -62,7 +65,7 @@ function ViewController(client, formId) {
{ {
id : 0, id : 0,
submitId : 1, submitId : 1,
values : { value : {
"1" : "hurp", "1" : "hurp",
"2" : [ 'a', 'b', ... ], "2" : [ 'a', 'b', ... ],
"3 " 2, "3 " 2,
@ -73,7 +76,7 @@ function ViewController(client, formId) {
var formData = { var formData = {
id : self.formId, id : self.formId,
submitId : self.focusedView.id, submitId : self.focusedView.id,
values : {}, value : {},
}; };
var viewData; var viewData;
@ -81,10 +84,10 @@ function ViewController(client, formId) {
try { try {
viewData = self.views[id].getViewData(); viewData = self.views[id].getViewData();
if(typeof viewData !== 'undefined') { if(typeof viewData !== 'undefined') {
formData.values[id] = viewData; formData.value[id] = viewData;
} }
} catch(e) { } catch(e) {
console.log(e); Log.error(e); // :TODO: Log better ;)
} }
} }
@ -203,23 +206,26 @@ ViewController.prototype.loadFromMCIMap = function(mciMap) {
}); });
}; };
ViewController.prototype.loadFromMCIMapAndConfig = function(mciMap, menuConfig, cb) { ViewController.prototype.loadFromMCIMapAndConfig = function(options, cb) {
assert(options.mciMap);
var factory = new MCIViewFactory(this.client); var factory = new MCIViewFactory(this.client);
var self = this; var self = this;
async.waterfall( async.waterfall(
[ [
function getFormConfig(callback) { function getFormConfig(callback) {
menuUtil.getFormConfig(menuConfig, mciMap, function onFormConfig(err, formConfig) { menuUtil.getFormConfig(options.menuConfig, options.mciMap, function onFormConfig(err, formConfig) {
if(err) { if(err) {
// :TODO: Log about missing form config -- this is not fatal, however Log.warn(err, 'Unable to load menu configuration');
} }
callback(null, formConfig); callback(null, formConfig);
}); });
}, },
function createViewsFromMCIMap(formConfig, callback) { function createViewsFromMCIMap(formConfig, callback) {
async.each(Object.keys(mciMap), function onMciEntry(name, eachCb) { async.each(Object.keys(options.mciMap), function onMciEntry(name, eachCb) {
var mci = mciMap[name]; var mci = options.mciMap[name];
var view = factory.createFromMCI(mci); var view = factory.createFromMCI(mci);
if(view) { if(view) {
@ -236,6 +242,7 @@ ViewController.prototype.loadFromMCIMapAndConfig = function(mciMap, menuConfig,
}); });
}, },
function applyFormConfig(formConfig, callback) { function applyFormConfig(formConfig, callback) {
if(formConfig) {
async.each(Object.keys(formConfig.mci), function onMciConf(mci, eachCb) { async.each(Object.keys(formConfig.mci), function onMciConf(mci, eachCb) {
var viewId = parseInt(mci[2]); // :TODO: what about auto-generated ID's? Do they simply not apply to menu configs? var viewId = parseInt(mci[2]); // :TODO: what about auto-generated ID's? Do they simply not apply to menu configs?
var mciConf = formConfig.mci[mci]; var mciConf = formConfig.mci[mci];
@ -253,11 +260,41 @@ ViewController.prototype.loadFromMCIMapAndConfig = function(mciMap, menuConfig,
self.switchFocus(viewId); self.switchFocus(viewId);
} }
eachCb(null); eachCb(null);
}, },
function eachMciConfComplete(err) { function eachMciConfComplete(err) {
callback(err); callback(err, formConfig);
}); });
} else {
callback(null);
}
},
function mapMenuSubmit(formConfig, callback) {
if(formConfig) {
//
// If we have a 'submit' section, create a submit handler
// and map the various entries to menus/etc.
//
if(formConfig.submit && formConfig.submit.length > 0) {
self.on('submit', function onSubmit(formData) {
Log.debug( { formData : formData }, 'Submit form');
for(var c = 0; c < formConfig.submit.length; ++c) {
console.log(formConfig.submit[c]);
if(ld.isEqual(formData.value, formConfig.submit[c].value)) {
self.client.gotoMenuModule(formConfig.submit[c].menu);
break;
}
// :TODO: Match various wildcards, etc.
}
});
}
} else {
callback(null);
}
} }
], ],
function complete(err) { function complete(err) {

BIN
mods/art/LOGIN1.ANS Normal file

Binary file not shown.

48
mods/login.js Normal file
View File

@ -0,0 +1,48 @@
/* jslint node: true */
'use strict';
var ansi = require('../core/ansi_term.js');
var art = require('../core/art.js');
var user = require('../core/user.js');
var theme = require('../core/theme.js');
var MenuModule = require('../core/menu_module.js').MenuModule;
var ViewController = require('../core/view_controller.js').ViewController;
//var async = require('async');
// :TODO: clean up requires
exports.moduleInfo = {
name : 'Login',
desc : 'Login Module',
author : 'NuSkooler',
};
exports.getModule = LoginModule;
function LoginModule(menuConfig) {
MenuModule.call(this, menuConfig);
}
require('util').inherits(LoginModule, MenuModule);
LoginModule.prototype.enter = function(client) {
LoginModule.super_.prototype.enter.call(this, client);
};
LoginModule.prototype.beforeArt = function() {
LoginModule.super_.prototype.beforeArt.call(this);
this.client.term.write(ansi.resetScreen());
};
LoginModule.prototype.mciReady = function(mciMap) {
LoginModule.super_.prototype.mciReady.call(this, mciMap);
var self = this;
var vc = self.addViewController(new ViewController(self.client));
vc.loadFromMCIMapAndConfig( { mciMap : mciMap, menuConfig : self.menuConfig }, function onViewReady(err) {
});
};

View File

@ -4,6 +4,7 @@
*/ */
"matrix" : { "matrix" : {
"art" : "matrix", "art" : "matrix",
// :TODO: Not currently supporting more than form 0. Should probably do that!
"form" : [ "form" : [
{ {
"mciReq" : [ "VM1" ], "mciReq" : [ "VM1" ],
@ -11,12 +12,35 @@
"VM1" : { "VM1" : {
"submit" : true, "submit" : true,
"focus" : true, "focus" : true,
// :TODO: need a good way to localize these ... Standard Orig->Lookup seems good.
"items" : [ "Login", "Apply", "Log Off" ] "items" : [ "Login", "Apply", "Log Off" ]
} }
} },
/*
Some Examples:
VerticalMenu: { id: 0, submitId: 1, value: { '1': 1 } }
Another concept:
"menu" : "@helper.js/logoff" -> calls helper.js::logoff(...)
*/
"submit" : [
{
"value" : { "1" : 0 },
"menu" : "login"
},
{
"value" : { "1" : 1 },
"menu" : "apply"
},
{
"value" : { "1" : 2 },
"menu" : "logoff"
}
]
} }
] ]
//"module" : "matrix"
}, },
"login" : { "login" : {
"art" : "login", "art" : "login",

View File

@ -37,11 +37,13 @@ StandardMenuModule.prototype.mciReady = function(mciMap) {
var self = this; var self = this;
var vc = self.addViewController(new ViewController(self.client)); var vc = self.addViewController(new ViewController(self.client));
vc.loadFromMCIMapAndConfig(mciMap, self.menuConfig, function onViewReady(err) { vc.loadFromMCIMapAndConfig( { mciMap : mciMap, menuConfig : self.menuConfig }, function onViewReady(err) {
if(err) { if(err) {
console.log(err); console.log(err);
} else { } else {
// vc.switchFocus(1); /* vc.on('submit', function onFormSubmit(formData) {
console.log(formData);
});*/
} }
}); });
@ -117,8 +119,8 @@ StandardMenuModule.prototype.mciReady = function(mciMap) {
}; };
if(0 === form.id && 1 === form.submitId) { if(0 === form.id && 1 === form.submitId) {
console.log(viewModuleMap[form.values[1]]); console.log(viewModuleMap[form.value[1]]);
self.client.gotoMenuModule(viewModuleMap[form.values[1]]); self.client.gotoMenuModule(viewModuleMap[form.value[1]]);
} }
}); });