diff --git a/core/menu_util.js b/core/menu_util.js index d6aa8167..84eb2c62 100644 --- a/core/menu_util.js +++ b/core/menu_util.js @@ -1,44 +1,61 @@ /* jslint node: true */ 'use strict'; +// ENiGMA½ var moduleUtil = require('./module_util.js'); -var theme = require('./theme.js'); -var async = require('async'); var Log = require('./logger.js').log; +var conf = require('./config.js'); -var menuJson = require('../mods/menu.json'); +var fs = require('fs'); +var paths = require('path'); +var async = require('async'); +var stripJsonComments = require('strip-json-comments'); exports.loadMenu = loadMenu; function loadMenu(name, client, cb) { - // want client.loadMenu(...). Replace current "goto module"/etc. with "switchMenu(...)" - // load options/etc -> call menuModule.enter(client, options) - /* - * Ensure JSON section exists + TODO: * check access / ACS * - * ...MenuModule(menuSection) ... .enter(client) */ - if('object' !== typeof menuJson[name] || null === menuJson[name]) { - cb(new Error('No menu by the name of \'' + name + '\'')); - return; - } + async.waterfall( + [ + function loadMenuConfig(callback) { + var configJsonPath = paths.join(conf.config.paths.mods, 'menu.json'); - var menuConfig = menuJson[name]; - Log.debug(menuConfig, 'Menu config'); + fs.readFile(configJsonPath, { encoding : 'utf8' }, function onMenuConfig(err, data) { + try { + var menuJson = JSON.parse(stripJsonComments(data)); - var moduleName = menuConfig.module || 'standard_menu'; + if('object' !== typeof menuJson[name] || null === menuJson[name]) { + callback(new Error('No configuration entry for \'' + name + '\'')); + } else { + callback(err, menuJson[name]); + } + } catch(e) { + callback(e); + } + }); + }, + function menuConfigLoaded(menuConfig, callback) { + Log.debug( { config : menuConfig }, 'Menu configuration loaded'); - moduleUtil.loadModule(moduleName, 'mods', function onModule(err, mod) { - if(err) { - cb(err); - } else { - Log.debug( { moduleName : moduleName, moduleInfo : mod.moduleInfo }, 'Loading menu module'); + var moduleName = menuConfig.module || 'standard_menu'; - var modInst = new mod.getModule(menuConfig); - cb(null, modInst); + moduleUtil.loadModule(moduleName, 'mods', function onModule(err, mod) { + callback(err, mod, menuConfig, moduleName); + }); + }, + ], + function complete(err, mod, menuConfig, moduleName) { + if(err) { + cb(err); + } else { + Log.debug( { moduleName : moduleName, moduleInfo : mod.moduleInfo }, 'Loading menu module instance'); + cb(null, new mod.getModule(menuConfig)); + } } - }); + ); } \ No newline at end of file diff --git a/core/misc_util.js b/core/misc_util.js index 1a19dc5d..ed834ef9 100644 --- a/core/misc_util.js +++ b/core/misc_util.js @@ -1,6 +1,7 @@ -"use strict"; +/* jslint node: true */ +'use strict'; -var paths = require('path'); +var paths = require('path'); exports.isProduction = isProduction; exports.isDevelopment = isDevelopment; @@ -10,19 +11,19 @@ exports.resolvePath = resolvePath; function isProduction() { var env = process.env.NODE_ENV || 'dev'; return 'production' === env; -}; +} function isDevelopment() { return (!(isProduction())); -}; +} function valueWithDefault(val, defVal) { return (typeof val !== 'undefined' ? val : defVal); -}; +} function resolvePath(path) { if(path.substr(0, 2) === '~/') { path = (process.env.HOME || process.env.HOMEPATH || process.env.HOMEDIR || process.cwd()) + path.substr(1); } return paths.resolve(path); -}; \ No newline at end of file +} \ No newline at end of file diff --git a/core/view_controller.js b/core/view_controller.js index d4078bbb..6315c9b3 100644 --- a/core/view_controller.js +++ b/core/view_controller.js @@ -53,10 +53,24 @@ function ViewController(client, formId) { }; this.submitForm = function() { + /* + Generate a form resonse. Example: + + { + id : 0, + submitId : 1, + values : { + "1" : "hurp", + "2" : [ 'a', 'b', ... ], + "3 " 2, + } + + } + */ var formData = { - id : self.formId, - viewId : self.focusedView.id, - values : [], + id : self.formId, + submitId : self.focusedView.id, + values : {}, }; var viewData; @@ -64,10 +78,7 @@ function ViewController(client, formId) { try { viewData = self.views[id].getViewData(); if(typeof viewData !== 'undefined') { - formData.values.push({ - id : id, - data : viewData, - }); + formData.values[id] = viewData; } } catch(e) { console.log(e); diff --git a/mods/goodbye.js b/mods/goodbye.js deleted file mode 100644 index 6774315f..00000000 --- a/mods/goodbye.js +++ /dev/null @@ -1,39 +0,0 @@ -/* jslint node: true */ -'use strict'; - -var MenuModule = require('../core/menu_module.js').MenuModule; -var ansi = require('../core/ansi_term.js'); - -exports.moduleInfo = { - name : 'Goodbye', - desc : 'Log off / Goodbye Module', - author : 'NuSkooler', -}; - -exports.getModule = GoodbyeModule; - -function GoodbyeModule(menuConfig) { - MenuModule.call(this, menuConfig); -} - -require('util').inherits(GoodbyeModule, MenuModule); - -GoodbyeModule.prototype.enter = function(client) { - GoodbyeModule.super_.prototype.enter.call(this, client); -}; - -GoodbyeModule.prototype.beforeArt = function() { - GoodbyeModule.super_.prototype.beforeArt.call(this); - - this.client.term.write(ansi.resetScreen()); -}; - -GoodbyeModule.prototype.mciReady = function(mciMap) { - GoodbyeModule.super_.prototype.mciReady.call(this, mciMap); -}; - -GoodbyeModule.prototype.finishedLoading = function() { - GoodbyeModule.super_.prototype.finishedLoading.call(this); - - this.client.end(); -}; \ No newline at end of file diff --git a/mods/logoff.js b/mods/logoff.js new file mode 100644 index 00000000..b2b4fa7f --- /dev/null +++ b/mods/logoff.js @@ -0,0 +1,39 @@ +/* jslint node: true */ +'use strict'; + +var MenuModule = require('../core/menu_module.js').MenuModule; +var ansi = require('../core/ansi_term.js'); + +exports.moduleInfo = { + name : 'LogOff', + desc : 'Log off / Goodbye Module', + author : 'NuSkooler', +}; + +exports.getModule = LogOffModule; + +function LogOffModule(menuConfig) { + MenuModule.call(this, menuConfig); +} + +require('util').inherits(LogOffModule, MenuModule); + +LogOffModule.prototype.enter = function(client) { + LogOffModule.super_.prototype.enter.call(this, client); +}; + +LogOffModule.prototype.beforeArt = function() { + LogOffModule.super_.prototype.beforeArt.call(this); + + this.client.term.write(ansi.resetScreen()); +}; + +LogOffModule.prototype.mciReady = function(mciMap) { + LogOffModule.super_.prototype.mciReady.call(this, mciMap); +}; + +LogOffModule.prototype.finishedLoading = function() { + LogOffModule.super_.prototype.finishedLoading.call(this); + + this.client.end(); +}; \ No newline at end of file diff --git a/mods/matrix.js b/mods/matrix.js index f335631a..b1af2804 100644 --- a/mods/matrix.js +++ b/mods/matrix.js @@ -71,9 +71,15 @@ MatrixModule.prototype.mciReady = function(mciMap) { vc.on('submit', function onSubmit(form) { console.log(form); - if(0 === form.id && 1 === form.viewId) { - // :TODO: fix me. Need to finalize form data. Current is kinda... meh. - self.client.gotoMenuModule('goodbye'); + var viewModuleMap = { + '0' : 'login', + '1' : 'new', + '2' : 'logoff', + }; + + if(0 === form.id && 1 === form.submitId) { + console.log(viewModuleMap[form.values[1]]); + self.client.gotoMenuModule(viewModuleMap[form.values[1]]); } }); diff --git a/mods/menu.json b/mods/menu.json index c24f4d6f..3ebd2245 100644 --- a/mods/menu.json +++ b/mods/menu.json @@ -1,4 +1,7 @@ { + /* + Menu Configuration + */ "matrix" : { "art" : "matrix", "module" : "matrix" @@ -7,8 +10,8 @@ "art" : "login", "module" : "login" }, - "goodbye" : { + "logoff" : { "art" : "logoff", - "module" : "goodbye" + "module" : "logoff" } } \ No newline at end of file