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-07-20 03:49:48 +00:00
|
|
|
var jsonCache = require('./json_cache.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-03-26 05:23:14 +00:00
|
|
|
var stripJsonComments = require('strip-json-comments');
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2015-05-14 04:21:55 +00:00
|
|
|
exports.loadMenu = loadMenu;
|
|
|
|
exports.getFormConfigByIDAndMap = getFormConfigByIDAndMap;
|
|
|
|
exports.handleAction = handleAction;
|
|
|
|
exports.applyThemeCustomization = applyThemeCustomization;
|
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-07-20 03:49:48 +00:00
|
|
|
jsonCache.getJSON('menu.json', 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-07-24 04:23:44 +00:00
|
|
|
jsonCache.getJSON('prompt.json', 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(
|
|
|
|
{ moduleName : modData.name, args : options.args, config : modData.config, info : modData.mod.modInfo },
|
|
|
|
'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,
|
|
|
|
args : options.args,
|
|
|
|
});
|
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-07-22 05:52:20 +00:00
|
|
|
var mciReqKey = _.pluck(_.sortBy(mciMap, 'code'), 'code').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-04-02 04:13:29 +00:00
|
|
|
if(_.isObject(formForId[mciReqKey])) {
|
|
|
|
cb(null, formForId[mciReqKey]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.has(formForId, 'mci') || _.has(formForId, 'submit')) {
|
|
|
|
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
|
|
|
|
|
|
|
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-07-27 04:51:06 +00:00
|
|
|
// :TODO: Most of this should be moved elsewhere .... DRY...
|
2015-04-21 05:24:15 +00:00
|
|
|
function callModuleMenuMethod(path) {
|
|
|
|
if('' === paths.extname(path)) {
|
|
|
|
path += '.js';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var methodMod = require(path);
|
|
|
|
methodMod[actionAsset.asset](client.currentMenuModule, formData, conf.extraArgs);
|
|
|
|
} catch(e) {
|
|
|
|
Log.error( { error : e.toString(), methodName : actionAsset.asset }, 'Failed to execute asset method');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-21 05:24:15 +00:00
|
|
|
callModuleMenuMethod(paths.join(Config.paths.mods, actionAsset.location));
|
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-04-21 05:24:15 +00:00
|
|
|
callModuleMenuMethod(paths.join(__dirname, 'system_menu_method.js'));
|
|
|
|
} else {
|
|
|
|
// local to current module
|
|
|
|
var currentModule = client.currentMenuModule;
|
|
|
|
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) {
|
|
|
|
currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs);
|
|
|
|
}
|
2015-04-21 04:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'menu' :
|
|
|
|
client.gotoMenuModule( { name : actionAsset.asset, formData : formData, extraArgs : conf.extraArgs } );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 04:21:55 +00:00
|
|
|
|
|
|
|
function applyThemeCustomization(options) {
|
|
|
|
//
|
|
|
|
// options.name : menu/prompt name
|
|
|
|
// options.configMci : menu or prompt config (menu.json / prompt.json) specific mci section
|
|
|
|
// options.client : client
|
2015-05-14 20:56:02 +00:00
|
|
|
// options.type : menu|prompt
|
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));
|
|
|
|
|
|
|
|
if(_.isUndefined(options.configMci)) {
|
|
|
|
options.configMci = {};
|
|
|
|
}
|
|
|
|
|
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-05-14 04:21:55 +00:00
|
|
|
Object.keys(themeConfig).forEach(function mciEntry(mci) {
|
|
|
|
_.defaults(options.configMci[mci], themeConfig[mci]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// :TODO: apply generic stuff, e.g. "VM" (vs "VM1")
|
|
|
|
}
|