2015-03-23 04:52:04 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-03-26 05:23:14 +00:00
|
|
|
// ENiGMA½
|
2015-03-23 04:52:04 +00:00
|
|
|
var moduleUtil = require('./module_util.js');
|
2015-03-23 05:16:49 +00:00
|
|
|
var Log = require('./logger.js').log;
|
2015-04-19 08:13:13 +00:00
|
|
|
var Config = require('./config.js').config;
|
|
|
|
var asset = require('./asset.js');
|
2016-01-15 05:44:33 +00:00
|
|
|
var getFullConfig = require('./config_util.js').getFullConfig;
|
2015-08-20 00:50:40 +00:00
|
|
|
var MCIViewFactory = require('./mci_view_factory.js').MCIViewFactory;
|
2015-11-14 19:22:21 +00:00
|
|
|
var acsUtil = require('./acs_util.js');
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2015-03-26 05:23:14 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var paths = require('path');
|
|
|
|
var async = require('async');
|
2015-03-27 04:58:22 +00:00
|
|
|
var assert = require('assert');
|
2015-04-02 04:13:29 +00:00
|
|
|
var _ = require('lodash');
|
2015-03-27 04:58:22 +00:00
|
|
|
|
2015-05-14 04:21:55 +00:00
|
|
|
exports.loadMenu = loadMenu;
|
|
|
|
exports.getFormConfigByIDAndMap = getFormConfigByIDAndMap;
|
|
|
|
exports.handleAction = handleAction;
|
2015-09-09 04:08:45 +00:00
|
|
|
exports.handleNext = handleNext;
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2016-01-15 05:44:33 +00:00
|
|
|
function getMenuConfig(client, name, cb) {
|
2015-04-19 08:13:13 +00:00
|
|
|
var menuConfig;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
2016-01-15 05:44:33 +00:00
|
|
|
function locateMenuConfig(callback) {
|
|
|
|
if(_.has(client.currentTheme, [ 'menus', name ])) {
|
|
|
|
menuConfig = client.currentTheme.menus[name];
|
2015-04-19 08:13:13 +00:00
|
|
|
callback(null);
|
|
|
|
} else {
|
|
|
|
callback(new Error('No menu entry for \'' + name + '\''));
|
|
|
|
}
|
|
|
|
},
|
2016-01-15 05:44:33 +00:00
|
|
|
function locatePromptConfig(callback) {
|
2015-04-19 08:13:13 +00:00
|
|
|
if(_.isString(menuConfig.prompt)) {
|
2016-01-15 05:44:33 +00:00
|
|
|
if(_.has(client.currentTheme, [ 'prompts', menuConfig.prompt ])) {
|
|
|
|
menuConfig.promptConfig = client.currentTheme.prompts[menuConfig.prompt];
|
2016-07-10 02:08:08 +00:00
|
|
|
callback(null);
|
2015-04-19 08:13:13 +00:00
|
|
|
} else {
|
|
|
|
callback(new Error('No prompt entry for \'' + menuConfig.prompt + '\''));
|
2016-01-15 05:44:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback(null);
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
cb(err, menuConfig);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-04 20:41:04 +00:00
|
|
|
function loadMenu(options, cb) {
|
2015-04-19 08:13:13 +00:00
|
|
|
assert(_.isObject(options));
|
|
|
|
assert(_.isString(options.name));
|
|
|
|
assert(_.isObject(options.client));
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getMenuConfiguration(callback) {
|
2016-01-15 05:44:33 +00:00
|
|
|
getMenuConfig(options.client, options.name, function menuConfigLoaded(err, menuConfig) {
|
2015-04-19 08:13:13 +00:00
|
|
|
callback(err, menuConfig);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function loadMenuModule(menuConfig, callback) {
|
2015-04-21 04:50:58 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
const modAsset = asset.getModuleAsset(menuConfig.module);
|
|
|
|
const modSupplied = null !== modAsset;
|
|
|
|
|
|
|
|
const modLoadOpts = {
|
2015-08-08 21:52:47 +00:00
|
|
|
name : modSupplied ? modAsset.asset : 'standard_menu',
|
|
|
|
path : (!modSupplied || 'systemModule' === modAsset.type) ? __dirname : Config.paths.mods,
|
|
|
|
category : (!modSupplied || 'systemModule' === modAsset.type) ? null : 'mods',
|
2015-04-21 04:50:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
moduleUtil.loadModuleEx(modLoadOpts, function moduleLoaded(err, mod) {
|
2016-02-03 04:35:59 +00:00
|
|
|
const modData = {
|
2015-04-21 04:50:58 +00:00
|
|
|
name : modLoadOpts.name,
|
2015-04-19 08:13:13 +00:00
|
|
|
config : menuConfig,
|
|
|
|
mod : mod,
|
|
|
|
};
|
|
|
|
|
|
|
|
callback(err, modData);
|
|
|
|
});
|
2015-08-08 21:52:47 +00:00
|
|
|
},
|
2015-04-19 08:13:13 +00:00
|
|
|
function createModuleInstance(modData, callback) {
|
|
|
|
Log.debug(
|
2015-08-16 19:35:34 +00:00
|
|
|
{ moduleName : modData.name, extraArgs : options.extraArgs, config : modData.config, info : modData.mod.modInfo },
|
2015-04-19 08:13:13 +00:00
|
|
|
'Creating menu module instance');
|
|
|
|
|
|
|
|
try {
|
2015-05-14 04:21:55 +00:00
|
|
|
var moduleInstance = new modData.mod.getModule(
|
|
|
|
{
|
|
|
|
menuName : options.name,
|
|
|
|
menuConfig : modData.config,
|
2016-02-03 04:35:59 +00:00
|
|
|
extraArgs : options.extraArgs,
|
2016-04-13 04:38:32 +00:00
|
|
|
client : options.client,
|
2015-05-14 04:21:55 +00:00
|
|
|
});
|
2015-04-19 08:13:13 +00:00
|
|
|
callback(null, moduleInstance);
|
|
|
|
} catch(e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err, modInst) {
|
|
|
|
cb(err, modInst);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
function getFormConfigByIDAndMap(menuConfig, formId, mciMap, cb) {
|
2015-04-02 04:13:29 +00:00
|
|
|
assert(_.isObject(menuConfig));
|
2015-03-27 04:58:22 +00:00
|
|
|
|
2015-04-02 04:13:29 +00:00
|
|
|
if(!_.isObject(menuConfig.form)) {
|
|
|
|
cb(new Error('Invalid or missing \'form\' member for menu'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.isObject(menuConfig.form[formId])) {
|
|
|
|
cb(new Error('No form found for formId ' + formId));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var formForId = menuConfig.form[formId];
|
2015-08-20 00:50:40 +00:00
|
|
|
var mciReqKey = _.filter(_.pluck(_.sortBy(mciMap, 'code'), 'code'), function(mci) {
|
|
|
|
return MCIViewFactory.UserViewCodes.indexOf(mci) > -1;
|
|
|
|
}).join('');
|
2015-04-02 04:13:29 +00:00
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
Log.trace( { mciKey : mciReqKey }, 'Looking for MCI configuration key');
|
2015-07-22 05:52:20 +00:00
|
|
|
|
2015-09-03 05:11:17 +00:00
|
|
|
//
|
|
|
|
// Exact, explicit match?
|
|
|
|
//
|
2015-04-02 04:13:29 +00:00
|
|
|
if(_.isObject(formForId[mciReqKey])) {
|
2015-09-03 05:11:17 +00:00
|
|
|
Log.trace( { mciKey : mciReqKey }, 'Using exact configuration key match');
|
2015-04-02 04:13:29 +00:00
|
|
|
cb(null, formForId[mciReqKey]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-03 05:11:17 +00:00
|
|
|
//
|
|
|
|
// Generic match
|
|
|
|
//
|
2015-04-02 04:13:29 +00:00
|
|
|
if(_.has(formForId, 'mci') || _.has(formForId, 'submit')) {
|
2015-09-03 05:11:17 +00:00
|
|
|
Log.trace('Using generic configuration');
|
2015-04-02 04:13:29 +00:00
|
|
|
cb(null, formForId);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-27 04:58:22 +00:00
|
|
|
|
2015-07-22 05:52:20 +00:00
|
|
|
cb(new Error('No matching form configuration found for key \'' + mciReqKey + '\''));
|
2015-04-02 04:13:29 +00:00
|
|
|
}
|
2015-04-21 04:50:58 +00:00
|
|
|
|
2015-09-09 04:08:45 +00:00
|
|
|
// :TODO: Most of this should be moved elsewhere .... DRY...
|
2015-09-10 03:31:04 +00:00
|
|
|
function callModuleMenuMethod(client, asset, path, formData, extraArgs) {
|
2015-09-09 04:08:45 +00:00
|
|
|
if('' === paths.extname(path)) {
|
|
|
|
path += '.js';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-09-24 05:41:06 +00:00
|
|
|
client.log.trace(
|
|
|
|
{ path : path, methodName : asset.asset, formData : formData, extraArgs : extraArgs },
|
|
|
|
'Calling menu method');
|
2015-09-20 07:29:07 +00:00
|
|
|
|
2015-09-09 04:08:45 +00:00
|
|
|
var methodMod = require(path);
|
2015-09-10 03:31:04 +00:00
|
|
|
methodMod[asset.asset](client.currentMenuModule, formData || { }, extraArgs);
|
2015-09-09 04:08:45 +00:00
|
|
|
} catch(e) {
|
|
|
|
client.log.error( { error : e.toString(), methodName : asset.asset }, 'Failed to execute asset method');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
function handleAction(client, formData, conf) {
|
|
|
|
assert(_.isObject(conf));
|
|
|
|
assert(_.isString(conf.action));
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
const actionAsset = asset.parseAsset(conf.action);
|
2015-04-21 04:50:58 +00:00
|
|
|
assert(_.isObject(actionAsset));
|
2015-04-21 05:24:15 +00:00
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
switch(actionAsset.type) {
|
|
|
|
case 'method' :
|
2015-04-21 05:24:15 +00:00
|
|
|
case 'systemMethod' :
|
2015-04-21 04:50:58 +00:00
|
|
|
if(_.isString(actionAsset.location)) {
|
2015-09-20 07:29:07 +00:00
|
|
|
callModuleMenuMethod(client, actionAsset, paths.join(Config.paths.mods, actionAsset.location), formData, conf.extraArgs);
|
2015-04-21 04:50:58 +00:00
|
|
|
} else {
|
2015-04-21 05:24:15 +00:00
|
|
|
if('systemMethod' === actionAsset.type) {
|
2015-07-20 23:13:14 +00:00
|
|
|
// :TODO: Need to pass optional args here -- conf.extraArgs and args between e.g. ()
|
|
|
|
// :TODO: Probably better as system_method.js
|
2015-09-10 03:31:04 +00:00
|
|
|
callModuleMenuMethod(client, actionAsset, paths.join(__dirname, 'system_menu_method.js'), formData, conf.extraArgs);
|
2015-04-21 05:24:15 +00:00
|
|
|
} else {
|
|
|
|
// local to current module
|
|
|
|
var currentModule = client.currentMenuModule;
|
|
|
|
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) {
|
|
|
|
currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs);
|
2015-09-20 04:55:09 +00:00
|
|
|
} else {
|
|
|
|
client.log.warn( { method : actionAsset.asset }, 'Method does not exist in module');
|
2015-04-21 05:24:15 +00:00
|
|
|
}
|
2015-04-21 04:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'menu' :
|
2015-11-04 06:15:49 +00:00
|
|
|
client.currentMenuModule.gotoMenu(actionAsset.asset, { formData : formData, extraArgs : conf.extraArgs } );
|
2015-04-21 04:50:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-09 04:08:45 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
function handleNext(client, nextSpec, conf) {
|
2015-11-14 19:22:21 +00:00
|
|
|
assert(_.isString(nextSpec) || _.isArray(nextSpec));
|
|
|
|
|
|
|
|
if(_.isArray(nextSpec)) {
|
|
|
|
nextSpec = acsUtil.getConditionalValue(client, nextSpec, 'next');
|
|
|
|
}
|
|
|
|
|
2015-09-09 04:08:45 +00:00
|
|
|
var nextAsset = asset.getAssetWithShorthand(nextSpec, 'menu');
|
2016-07-10 02:08:08 +00:00
|
|
|
// :TODO: getAssetWithShorthand() can return undefined - handle it!
|
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
conf = conf || {};
|
|
|
|
var extraArgs = conf.extraArgs || {};
|
|
|
|
|
2015-09-09 04:08:45 +00:00
|
|
|
switch(nextAsset.type) {
|
|
|
|
case 'method' :
|
|
|
|
case 'systemMethod' :
|
|
|
|
if(_.isString(nextAsset.location)) {
|
2015-11-04 06:15:49 +00:00
|
|
|
callModuleMenuMethod(client, nextAsset, paths.join(Config.paths.mods, nextAsset.location), {}, extraArgs);
|
2015-09-09 04:08:45 +00:00
|
|
|
} else {
|
|
|
|
if('systemMethod' === nextAsset.type) {
|
|
|
|
// :TODO: see other notes about system_menu_method.js here
|
2015-09-10 03:31:04 +00:00
|
|
|
callModuleMenuMethod(client, nextAsset, paths.join(__dirname, 'system_menu_method.js'), {}, extraArgs);
|
2015-09-09 04:08:45 +00:00
|
|
|
} else {
|
|
|
|
// local to current module
|
|
|
|
var currentModule = client.currentMenuModule;
|
2015-11-04 06:15:49 +00:00
|
|
|
if(_.isFunction(currentModule.menuMethods[nextAsset.asset])) {
|
|
|
|
currentModule.menuMethods[nextAsset.asset]( { }, extraArgs );
|
2015-09-09 04:08:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'menu' :
|
2015-11-04 06:15:49 +00:00
|
|
|
client.currentMenuModule.gotoMenu(nextAsset.asset, { extraArgs : extraArgs } );
|
2015-09-09 04:08:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
client.log.error( { nextSpec : nextSpec }, 'Invalid asset type for "next"');
|
|
|
|
break;
|
|
|
|
}
|
2015-04-21 04:50:58 +00:00
|
|
|
}
|