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-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-04-20 04:58:18 +00:00
|
|
|
exports.loadMenu = loadMenu;
|
|
|
|
exports.getFormConfigByIDAndMap = getFormConfigByIDAndMap;
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
|
|
|
|
function loadModJSON(fileName, cb) {
|
|
|
|
// :TODO: really need to cache menu.json and prompt.json only reloading if they change - see chokidar & gaze npms
|
|
|
|
var filePath = paths.join(Config.paths.mods, fileName);
|
|
|
|
|
|
|
|
fs.readFile(filePath, { encoding : 'utf8' }, function jsonData(err, data) {
|
|
|
|
try {
|
|
|
|
var json = JSON.parse(stripJsonComments(data));
|
|
|
|
cb(null, json);
|
|
|
|
} catch(e) {
|
|
|
|
cb(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMenuConfig(name, cb) {
|
|
|
|
var menuConfig;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function loadMenuJSON(callback) {
|
|
|
|
loadModJSON('menu.json', function loaded(err, menuJson) {
|
|
|
|
callback(err, menuJson);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function locateMenuConfig(menuJson, callback) {
|
|
|
|
if(_.isObject(menuJson[name])) {
|
|
|
|
menuConfig = menuJson[name];
|
|
|
|
callback(null);
|
|
|
|
} else {
|
|
|
|
callback(new Error('No menu entry for \'' + name + '\''));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function loadPromptJSON(callback) {
|
|
|
|
if(_.isString(menuConfig.prompt)) {
|
|
|
|
loadModJSON('prompt.json', function loaded(err, promptJson) {
|
|
|
|
callback(err, promptJson);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null, null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function locatePromptConfig(promptJson, callback) {
|
|
|
|
if(promptJson) {
|
|
|
|
if(_.isObject(promptJson[menuConfig.prompt])) {
|
|
|
|
menuConfig.promptConfig = promptJson[menuConfig.prompt];
|
|
|
|
} 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) {
|
|
|
|
var moduleName = menuConfig.module || 'standard_menu';
|
|
|
|
|
|
|
|
moduleUtil.loadModule(moduleName, 'mods', function moduleLoaded(err, mod) {
|
|
|
|
var modData = {
|
|
|
|
name : moduleName,
|
|
|
|
config : menuConfig,
|
|
|
|
mod : mod,
|
|
|
|
};
|
|
|
|
|
|
|
|
callback(err, modData);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function createModuleInstance(modData, callback) {
|
|
|
|
Log.debug(
|
|
|
|
{ moduleName : modData.name, args : options.args, config : modData.config, info : modData.mod.modInfo },
|
|
|
|
'Creating menu module instance');
|
|
|
|
|
|
|
|
try {
|
|
|
|
var moduleInstance = new modData.mod.getModule( { menuConfig : modData.config, args : options.args } );
|
|
|
|
callback(null, moduleInstance);
|
|
|
|
} catch(e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err, modInst) {
|
|
|
|
cb(err, modInst);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadMenu2(options, cb) {
|
2015-04-04 20:41:04 +00:00
|
|
|
|
|
|
|
assert(options);
|
|
|
|
assert(options.name);
|
|
|
|
assert(options.client);
|
|
|
|
|
|
|
|
var name = options.name;
|
|
|
|
var client = options.client;
|
2015-03-23 04:52:04 +00:00
|
|
|
/*
|
2015-03-26 05:23:14 +00:00
|
|
|
TODO:
|
2015-03-23 04:52:04 +00:00
|
|
|
* check access / ACS
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-03-26 05:23:14 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
2015-04-19 08:13:13 +00:00
|
|
|
// :TODO: Need a good way to cache this information & only (re)load if modified
|
2015-03-26 05:23:14 +00:00
|
|
|
function loadMenuConfig(callback) {
|
|
|
|
var configJsonPath = paths.join(conf.config.paths.mods, 'menu.json');
|
|
|
|
|
|
|
|
fs.readFile(configJsonPath, { encoding : 'utf8' }, function onMenuConfig(err, data) {
|
|
|
|
try {
|
|
|
|
var menuJson = JSON.parse(stripJsonComments(data));
|
|
|
|
|
2015-04-02 04:13:29 +00:00
|
|
|
if(!_.isObject(menuJson[name])) {
|
2015-03-26 05:23:14 +00:00
|
|
|
callback(new Error('No configuration entry for \'' + name + '\''));
|
|
|
|
} else {
|
|
|
|
callback(err, menuJson[name]);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function menuConfigLoaded(menuConfig, callback) {
|
|
|
|
var moduleName = menuConfig.module || 'standard_menu';
|
|
|
|
|
|
|
|
moduleUtil.loadModule(moduleName, 'mods', function onModule(err, mod) {
|
|
|
|
callback(err, mod, menuConfig, moduleName);
|
|
|
|
});
|
2015-04-04 20:41:04 +00:00
|
|
|
}
|
2015-03-26 05:23:14 +00:00
|
|
|
],
|
|
|
|
function complete(err, mod, menuConfig, moduleName) {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2015-04-04 20:41:04 +00:00
|
|
|
Log.debug(
|
|
|
|
{ moduleName : moduleName, args : options.args, config : menuConfig, info : mod.moduleInfo },
|
|
|
|
'Creating menu module instance');
|
|
|
|
|
|
|
|
// :TODO: throw from MenuModule() - catch here
|
|
|
|
cb(null, new mod.getModule({ menuConfig : menuConfig, args : options.args } ));
|
2015-03-26 05:23:14 +00:00
|
|
|
}
|
2015-03-23 04:52:04 +00:00
|
|
|
}
|
2015-03-26 05:23:14 +00:00
|
|
|
);
|
2015-03-27 04:58:22 +00:00
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
var mciReqKey = _.sortBy(Object.keys(mciMap), String).join('');
|
|
|
|
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-04-02 04:13:29 +00:00
|
|
|
cb(new Error('No matching form configuration found'));
|
|
|
|
}
|