* New ACS class avail @ client.acs

* Experimental / WIP work on issue #79
This commit is contained in:
Bryan Ashby 2016-07-24 11:48:59 -06:00
parent 8bd61f2d01
commit 08393e0aff

View file

@ -6,11 +6,8 @@ var moduleUtil = require('./module_util.js');
var Log = require('./logger.js').log; var Log = require('./logger.js').log;
var Config = require('./config.js').config; var Config = require('./config.js').config;
var asset = require('./asset.js'); var asset = require('./asset.js');
var getFullConfig = require('./config_util.js').getFullConfig;
var MCIViewFactory = require('./mci_view_factory.js').MCIViewFactory; var MCIViewFactory = require('./mci_view_factory.js').MCIViewFactory;
var acsUtil = require('./acs_util.js');
var fs = require('fs');
var paths = require('path'); var paths = require('path');
var async = require('async'); var async = require('async');
var assert = require('assert'); var assert = require('assert');
@ -61,8 +58,8 @@ function loadMenu(options, cb) {
async.waterfall( async.waterfall(
[ [
function getMenuConfiguration(callback) { function getMenuConfiguration(callback) {
getMenuConfig(options.client, options.name, function menuConfigLoaded(err, menuConfig) { getMenuConfig(options.client, options.name, (err, menuConfig) => {
callback(err, menuConfig); return callback(err, menuConfig);
}); });
}, },
function loadMenuModule(menuConfig, callback) { function loadMenuModule(menuConfig, callback) {
@ -76,14 +73,14 @@ function loadMenu(options, cb) {
category : (!modSupplied || 'systemModule' === modAsset.type) ? null : 'mods', category : (!modSupplied || 'systemModule' === modAsset.type) ? null : 'mods',
}; };
moduleUtil.loadModuleEx(modLoadOpts, function moduleLoaded(err, mod) { moduleUtil.loadModuleEx(modLoadOpts, (err, mod) => {
const modData = { const modData = {
name : modLoadOpts.name, name : modLoadOpts.name,
config : menuConfig, config : menuConfig,
mod : mod, mod : mod,
}; };
callback(err, modData); return callback(err, modData);
}); });
}, },
function createModuleInstance(modData, callback) { function createModuleInstance(modData, callback) {
@ -92,21 +89,20 @@ function loadMenu(options, cb) {
'Creating menu module instance'); 'Creating menu module instance');
try { try {
var moduleInstance = new modData.mod.getModule( const moduleInstance = new modData.mod.getModule({
{ menuName : options.name,
menuName : options.name, menuConfig : modData.config,
menuConfig : modData.config, extraArgs : options.extraArgs,
extraArgs : options.extraArgs, client : options.client,
client : options.client, });
}); return callback(null, moduleInstance);
callback(null, moduleInstance);
} catch(e) { } catch(e) {
callback(e); return callback(e);
} }
} }
], ],
function complete(err, modInst) { (err, modInst) => {
cb(err, modInst); return cb(err, modInst);
} }
); );
} }
@ -153,7 +149,7 @@ function getFormConfigByIDAndMap(menuConfig, formId, mciMap, cb) {
} }
// :TODO: Most of this should be moved elsewhere .... DRY... // :TODO: Most of this should be moved elsewhere .... DRY...
function callModuleMenuMethod(client, asset, path, formData, extraArgs) { function callModuleMenuMethod(client, asset, path, formData, extraArgs, cb) {
if('' === paths.extname(path)) { if('' === paths.extname(path)) {
path += '.js'; path += '.js';
} }
@ -163,86 +159,103 @@ function callModuleMenuMethod(client, asset, path, formData, extraArgs) {
{ path : path, methodName : asset.asset, formData : formData, extraArgs : extraArgs }, { path : path, methodName : asset.asset, formData : formData, extraArgs : extraArgs },
'Calling menu method'); 'Calling menu method');
var methodMod = require(path); const methodMod = require(path);
methodMod[asset.asset](client.currentMenuModule, formData || { }, extraArgs); return methodMod[asset.asset](client.currentMenuModule, formData || { }, extraArgs, cb);
} catch(e) { } catch(e) {
client.log.error( { error : e.toString(), methodName : asset.asset }, 'Failed to execute asset method'); client.log.error( { error : e.toString(), methodName : asset.asset }, 'Failed to execute asset method');
return cb(e);
} }
} }
function handleAction(client, formData, conf) { function handleAction(client, formData, conf, cb) {
assert(_.isObject(conf)); assert(_.isObject(conf));
assert(_.isString(conf.action)); assert(_.isString(conf.action));
cb = function() {
// nothing -- remove me!
};
const actionAsset = asset.parseAsset(conf.action); const actionAsset = asset.parseAsset(conf.action);
assert(_.isObject(actionAsset)); assert(_.isObject(actionAsset));
switch(actionAsset.type) { switch(actionAsset.type) {
case 'method' : case 'method' :
case 'systemMethod' : case 'systemMethod' :
if(_.isString(actionAsset.location)) { if(_.isString(actionAsset.location)) {
callModuleMenuMethod(client, actionAsset, paths.join(Config.paths.mods, actionAsset.location), formData, conf.extraArgs); return callModuleMenuMethod(
} else { client,
if('systemMethod' === actionAsset.type) { actionAsset,
// :TODO: Need to pass optional args here -- conf.extraArgs and args between e.g. () paths.join(Config.paths.mods, actionAsset.location),
// :TODO: Probably better as system_method.js formData,
callModuleMenuMethod(client, actionAsset, paths.join(__dirname, 'system_menu_method.js'), formData, conf.extraArgs); conf.extraArgs,
} else { cb);
// local to current module } else if('systemMethod' === actionAsset.type) {
var currentModule = client.currentMenuModule; // :TODO: Need to pass optional args here -- conf.extraArgs and args between e.g. ()
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) { // :TODO: Probably better as system_method.js
currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs); return callModuleMenuMethod(
} else { client,
client.log.warn( { method : actionAsset.asset }, 'Method does not exist in module'); actionAsset,
} paths.join(__dirname, 'system_menu_method.js'),
} formData,
conf.extraArgs,
cb);
} else {
// local to current module
const currentModule = client.currentMenuModule;
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) {
return currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs, cb);
} }
break;
const err = new Error('Method does not exist');
client.log.warn( { method : actionAsset.asset }, err.message);
return cb(err);
}
case 'menu' : case 'menu' :
client.currentMenuModule.gotoMenu(actionAsset.asset, { formData : formData, extraArgs : conf.extraArgs } ); return client.currentMenuModule.gotoMenu(actionAsset.asset, { formData : formData, extraArgs : conf.extraArgs }, cb );
break;
} }
} }
function handleNext(client, nextSpec, conf) { function handleNext(client, nextSpec, conf, cb) {
assert(_.isString(nextSpec) || _.isArray(nextSpec)); assert(_.isString(nextSpec) || _.isArray(nextSpec));
if(_.isArray(nextSpec)) { if(_.isArray(nextSpec)) {
nextSpec = acsUtil.getConditionalValue(client, nextSpec, 'next'); nextSpec = client.acs.getConditionalValue(nextSpec, 'next');
} }
var nextAsset = asset.getAssetWithShorthand(nextSpec, 'menu'); const nextAsset = asset.getAssetWithShorthand(nextSpec, 'menu');
// :TODO: getAssetWithShorthand() can return undefined - handle it! // :TODO: getAssetWithShorthand() can return undefined - handle it!
conf = conf || {}; conf = conf || {};
var extraArgs = conf.extraArgs || {}; const extraArgs = conf.extraArgs || {};
// :TODO: DRY this with handleAction()
switch(nextAsset.type) { switch(nextAsset.type) {
case 'method' : case 'method' :
case 'systemMethod' : case 'systemMethod' :
if(_.isString(nextAsset.location)) { if(_.isString(nextAsset.location)) {
callModuleMenuMethod(client, nextAsset, paths.join(Config.paths.mods, nextAsset.location), {}, extraArgs); return callModuleMenuMethod(client, nextAsset, paths.join(Config.paths.mods, nextAsset.location), {}, extraArgs, cb);
} else { } else if('systemMethod' === nextAsset.type) {
if('systemMethod' === nextAsset.type) { // :TODO: see other notes about system_menu_method.js here
// :TODO: see other notes about system_menu_method.js here return callModuleMenuMethod(client, nextAsset, paths.join(__dirname, 'system_menu_method.js'), {}, extraArgs, cb);
callModuleMenuMethod(client, nextAsset, paths.join(__dirname, 'system_menu_method.js'), {}, extraArgs); } else {
} else { // local to current module
// local to current module const currentModule = client.currentMenuModule;
var currentModule = client.currentMenuModule; if(_.isFunction(currentModule.menuMethods[nextAsset.asset])) {
if(_.isFunction(currentModule.menuMethods[nextAsset.asset])) { const formData = {}; // we don't have any
currentModule.menuMethods[nextAsset.asset]( { }, extraArgs ); return currentModule.menuMethods[nextAsset.asset]( formData, extraArgs, cb );
}
}
} }
break;
case 'menu' : const err = new Error('Method does not exist');
client.currentMenuModule.gotoMenu(nextAsset.asset, { extraArgs : extraArgs } ); client.log.warn( { method : nextAsset.asset }, err.message);
break; return cb(err);
}
default : case 'menu' :
client.log.error( { nextSpec : nextSpec }, 'Invalid asset type for "next"'); return client.currentMenuModule.gotoMenu(nextAsset.asset, { extraArgs : extraArgs }, cb );
break;
} }
const err = new Error('Invalid asset type for "next"');
client.log.error( { nextSpec : nextSpec }, err.message);
return cb(err);
} }