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-03-26 05:23:14 +00:00
|
|
|
var conf = require('./config.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
|
|
|
|
|
|
|
exports.loadMenu = loadMenu;
|
2015-03-27 04:58:22 +00:00
|
|
|
exports.getFormConfig = getFormConfig;
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2015-04-04 20:41:04 +00:00
|
|
|
function loadMenu(options, cb) {
|
|
|
|
|
|
|
|
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(
|
|
|
|
[
|
|
|
|
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-02 04:13:29 +00:00
|
|
|
function getFormConfig(menuConfig, formId, mciMap, cb) {
|
|
|
|
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'));
|
|
|
|
}
|