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 conf = require('./config.js'); // :TODO: remove me!
|
|
|
|
var Config = require('./config.js').config;
|
|
|
|
var asset = require('./asset.js');
|
|
|
|
var theme = require('./theme.js');
|
2015-09-10 03:31:04 +00:00
|
|
|
var configCache = require('./config_cache.js');
|
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-10-10 05:35:40 +00:00
|
|
|
exports.applyGeneralThemeCustomization = applyGeneralThemeCustomization;
|
|
|
|
exports.applyMciThemeCustomization = applyMciThemeCustomization;
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
function getMenuConfig(name, cb) {
|
|
|
|
var menuConfig;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function loadMenuJSON(callback) {
|
2015-12-11 20:58:58 +00:00
|
|
|
var menuFilePath = Config.general.menuFile;
|
|
|
|
|
|
|
|
// menuFile is assumed to be in 'mods' if a path is not supplied
|
|
|
|
if('.' === paths.dirname(menuFilePath)) {
|
|
|
|
menuFilePath = paths.join(__dirname, '../mods', menuFilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
configCache.getConfig(menuFilePath, function loaded(err, menuJson) {
|
2015-04-19 08:13:13 +00:00
|
|
|
callback(err, menuJson);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function locateMenuConfig(menuJson, callback) {
|
2015-05-16 05:02:58 +00:00
|
|
|
if(_.has(menuJson, [ 'menus', name ])) {
|
|
|
|
menuConfig = menuJson.menus[name];
|
2015-04-19 08:13:13 +00:00
|
|
|
callback(null);
|
|
|
|
} else {
|
|
|
|
callback(new Error('No menu entry for \'' + name + '\''));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function loadPromptJSON(callback) {
|
|
|
|
if(_.isString(menuConfig.prompt)) {
|
2015-09-10 03:31:04 +00:00
|
|
|
configCache.getModConfig('prompt.hjson', function loaded(err, promptJson, reCached) {
|
2015-04-19 08:13:13 +00:00
|
|
|
callback(err, promptJson);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null, null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function locatePromptConfig(promptJson, callback) {
|
|
|
|
if(promptJson) {
|
2015-05-16 05:02:58 +00:00
|
|
|
if(_.has(promptJson, [ 'prompts', menuConfig.prompt ])) {
|
2015-07-22 05:52:20 +00:00
|
|
|
menuConfig.promptConfig = promptJson.prompts[menuConfig.prompt];
|
2015-04-19 08:13:13 +00:00
|
|
|
} else {
|
|
|
|
callback(new Error('No prompt entry for \'' + menuConfig.prompt + '\''));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
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) {
|
|
|
|
getMenuConfig(options.name, function menuConfigLoaded(err, menuConfig) {
|
|
|
|
callback(err, menuConfig);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function loadMenuModule(menuConfig, callback) {
|
2015-08-08 21:52:47 +00:00
|
|
|
var modAsset = asset.getModuleAsset(menuConfig.module);
|
|
|
|
var modSupplied = null !== modAsset;
|
2015-04-21 04:50:58 +00:00
|
|
|
|
|
|
|
var 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) {
|
2015-04-19 08:13:13 +00:00
|
|
|
var 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,
|
2015-09-18 04:53:19 +00:00
|
|
|
extraArgs : options.extraArgs
|
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));
|
|
|
|
|
|
|
|
var actionAsset = asset.parseAsset(conf.action);
|
|
|
|
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');
|
|
|
|
|
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
|
|
|
}
|
2015-05-14 04:21:55 +00:00
|
|
|
|
2015-10-10 05:35:40 +00:00
|
|
|
|
2015-09-08 04:40:08 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
// :TODO: Seems better in theme.js, but that includes ViewController...which would then include theme.js
|
2015-10-10 05:35:40 +00:00
|
|
|
// ...theme.js only brings in VC to create themed pause prompt. Perhaps that should live elsewhere
|
|
|
|
|
|
|
|
function applyGeneralThemeCustomization(options) {
|
|
|
|
//
|
|
|
|
// options.name
|
|
|
|
// options.client
|
|
|
|
// options.type
|
|
|
|
// options.config
|
|
|
|
//
|
|
|
|
assert(_.isString(options.name));
|
|
|
|
assert(_.isObject(options.client));
|
|
|
|
assert("menus" === options.type || "prompts" === options.type);
|
|
|
|
|
|
|
|
if(_.has(options.client.currentTheme, [ 'customization', options.type, options.name ])) {
|
|
|
|
var themeConfig = options.client.currentTheme.customization[options.type][options.name];
|
|
|
|
|
|
|
|
if(themeConfig.config) {
|
|
|
|
Object.keys(themeConfig.config).forEach(function confEntry(conf) {
|
|
|
|
if(options.config[conf]) {
|
|
|
|
_.defaultsDeep(options.config[conf], themeConfig.config[conf]);
|
|
|
|
} else {
|
|
|
|
options.config[conf] = themeConfig.config[conf];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function applyMciThemeCustomization(options) {
|
2015-05-14 04:21:55 +00:00
|
|
|
//
|
2015-09-08 04:40:08 +00:00
|
|
|
// options.name : menu/prompt name
|
2015-09-28 04:05:40 +00:00
|
|
|
// options.mci : menu/prompt .mci section
|
2015-09-08 04:40:08 +00:00
|
|
|
// options.client : client
|
|
|
|
// options.type : menu|prompt
|
2015-09-10 03:31:04 +00:00
|
|
|
// options.formId : (optional) form ID in cases where multiple forms may exist wanting their own customization
|
|
|
|
//
|
|
|
|
// In the case of formId, the theme must include the ID as well, e.g.:
|
|
|
|
// {
|
|
|
|
// ...
|
|
|
|
// "2" : {
|
|
|
|
// "TL1" : { ... }
|
|
|
|
// }
|
|
|
|
// }
|
2015-05-14 04:21:55 +00:00
|
|
|
//
|
|
|
|
assert(_.isString(options.name));
|
2015-05-14 20:56:02 +00:00
|
|
|
assert("menus" === options.type || "prompts" === options.type);
|
2015-05-14 04:21:55 +00:00
|
|
|
assert(_.isObject(options.client));
|
|
|
|
|
2015-09-28 04:05:40 +00:00
|
|
|
if(_.isUndefined(options.mci)) {
|
|
|
|
options.mci = {};
|
|
|
|
}
|
|
|
|
|
2015-05-14 20:56:02 +00:00
|
|
|
if(_.has(options.client.currentTheme, [ 'customization', options.type, options.name ])) {
|
|
|
|
var themeConfig = options.client.currentTheme.customization[options.type][options.name];
|
2015-09-10 03:31:04 +00:00
|
|
|
|
|
|
|
if(options.formId && _.has(themeConfig, options.formId.toString())) {
|
|
|
|
// form ID found - use exact match
|
|
|
|
themeConfig = themeConfig[options.formId];
|
|
|
|
}
|
|
|
|
|
2015-09-28 04:05:40 +00:00
|
|
|
if(themeConfig.mci) {
|
|
|
|
Object.keys(themeConfig.mci).forEach(function mciEntry(mci) {
|
2015-10-07 03:21:13 +00:00
|
|
|
// :TODO: a better way to do this?
|
|
|
|
if(options.mci[mci]) {
|
|
|
|
_.defaults(options.mci[mci], themeConfig.mci[mci]);
|
|
|
|
} else {
|
|
|
|
options.mci[mci] = themeConfig.mci[mci];
|
|
|
|
}
|
2015-09-28 04:05:40 +00:00
|
|
|
});
|
|
|
|
}
|
2015-05-14 04:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// :TODO: apply generic stuff, e.g. "VM" (vs "VM1")
|
2015-09-10 03:31:04 +00:00
|
|
|
}
|