2015-03-19 05:08:23 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-03-23 04:52:04 +00:00
|
|
|
var PluginModule = require('./plugin_module.js').PluginModule;
|
|
|
|
var theme = require('./theme.js');
|
2015-04-19 08:13:13 +00:00
|
|
|
var art = require('./art.js');
|
2015-04-04 20:41:04 +00:00
|
|
|
var Log = require('./logger.js').log;
|
2015-04-10 04:49:56 +00:00
|
|
|
var ansi = require('./ansi_term.js');
|
2015-04-19 08:13:13 +00:00
|
|
|
var asset = require('./asset.js');
|
|
|
|
var ViewController = require('./view_controller.js').ViewController;
|
2015-04-21 04:50:58 +00:00
|
|
|
var menuUtil = require('./menu_util.js');
|
2015-03-23 04:52:04 +00:00
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
var assert = require('assert');
|
2015-04-19 08:13:13 +00:00
|
|
|
var _ = require('lodash');
|
2015-03-19 05:08:23 +00:00
|
|
|
|
|
|
|
exports.MenuModule = MenuModule;
|
|
|
|
|
2015-04-04 20:41:04 +00:00
|
|
|
function MenuModule(options) {
|
|
|
|
PluginModule.call(this, options);
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2015-03-23 04:52:04 +00:00
|
|
|
var self = this;
|
2015-04-04 20:41:04 +00:00
|
|
|
this.menuConfig = options.menuConfig;
|
2015-04-10 04:49:56 +00:00
|
|
|
this.menuConfig.options = options.menuConfig.options || {};
|
2015-04-19 08:13:13 +00:00
|
|
|
this.menuMethods = {}; // methods called from @method's
|
|
|
|
this.viewControllers = {}; // name->vc
|
|
|
|
|
|
|
|
this.displayArtAsset = function(assetSpec, cb) {
|
|
|
|
var artAsset = asset.getArtAsset(assetSpec);
|
|
|
|
|
|
|
|
if(!artAsset) {
|
|
|
|
cb(new Error('Asset not found: ' + assetSpec));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var dispOptions = {
|
|
|
|
name : artAsset.asset,
|
|
|
|
client : self.client,
|
|
|
|
font : self.menuConfig.font,
|
|
|
|
};
|
|
|
|
|
|
|
|
switch(artAsset.type) {
|
|
|
|
case 'art' :
|
|
|
|
theme.displayThemeArt(dispOptions, function displayed(err, mciMap) {
|
|
|
|
cb(err, mciMap);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'method' :
|
|
|
|
// :TODO: fetch and render via method/generator
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
cb(new Error('Unsupported art asset type'));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2015-03-24 05:12:19 +00:00
|
|
|
this.initSequence = function() {
|
2015-04-19 08:13:13 +00:00
|
|
|
var mciData = { };
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function beforeDisplayArt(callback) {
|
|
|
|
self.beforeArt();
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function displayMenuArt(callback) {
|
|
|
|
if(_.isString(self.menuConfig.art)) {
|
|
|
|
self.displayArtAsset(self.menuConfig.art, function displayed(err, mciMap) {
|
|
|
|
mciData.menu = mciMap;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function moveToPromptLocation(callback) {
|
|
|
|
if(self.menuConfig.prompt) {
|
|
|
|
// :TODO: fetch and move cursor to prompt location, if supplied. See notes/etc. on placements
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function displayPromptArt(callback) {
|
|
|
|
if(_.isString(self.menuConfig.prompt)) {
|
|
|
|
// If a prompt is specified, we need the configuration
|
|
|
|
if(!_.isObject(self.menuConfig.promptConfig)) {
|
|
|
|
callback(new Error('Prompt specified but configuraiton not found!'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prompts *must* have art. If it's missing it's an error
|
2015-04-21 04:50:58 +00:00
|
|
|
// :TODO: allow inline prompts in the future, e.g. @inline:memberName -> { "memberName" : { "text" : "stuff", ... } }
|
2015-04-19 08:13:13 +00:00
|
|
|
var promptConfig = self.menuConfig.promptConfig;
|
|
|
|
self.displayArtAsset(promptConfig.art, function displayed(err, mciMap) {
|
|
|
|
mciData.prompt = mciMap;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function afterArtDisplayed(callback) {
|
|
|
|
self.mciReady(mciData);
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
if(err) {
|
|
|
|
// :TODO: what to do exactly?????
|
|
|
|
}
|
|
|
|
|
|
|
|
self.finishedLoading();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2015-03-19 05:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(MenuModule, PluginModule);
|
|
|
|
|
|
|
|
MenuModule.prototype.enter = function(client) {
|
2015-03-23 04:52:04 +00:00
|
|
|
this.client = client;
|
2015-04-19 08:13:13 +00:00
|
|
|
assert(_.isObject(client));
|
2015-03-24 05:12:19 +00:00
|
|
|
|
|
|
|
this.initSequence();
|
2015-03-19 05:08:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MenuModule.prototype.leave = function() {
|
2015-04-19 08:13:13 +00:00
|
|
|
var self = this;
|
|
|
|
Object.keys(this.viewControllers).forEach(function entry(name) {
|
|
|
|
self.viewControllers[name].detachClientEvents();
|
|
|
|
});
|
2015-03-19 05:08:23 +00:00
|
|
|
};
|
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
MenuModule.prototype.addViewController = function(name, vc) {
|
|
|
|
assert(!this.viewControllers[name]);
|
|
|
|
this.viewControllers[name] = vc;
|
2015-03-23 04:52:04 +00:00
|
|
|
return vc;
|
|
|
|
};
|
|
|
|
|
2015-03-24 05:12:19 +00:00
|
|
|
MenuModule.prototype.beforeArt = function() {
|
2015-04-21 04:50:58 +00:00
|
|
|
if(this.menuConfig.options.cls) {
|
2015-04-10 04:49:56 +00:00
|
|
|
this.client.term.write(ansi.resetScreen());
|
|
|
|
}
|
2015-03-24 05:12:19 +00:00
|
|
|
};
|
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
MenuModule.prototype.mciReady = function(mciData) {
|
2015-03-24 05:12:19 +00:00
|
|
|
};
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
MenuModule.prototype.standardMCIReadyHandler = function(mciData) {
|
|
|
|
//
|
|
|
|
// A quick rundown:
|
|
|
|
// * We may have mciData.menu, mciData.prompt, or both.
|
|
|
|
// * Prompt form is favored over menu form if both are present.
|
|
|
|
// * Standard/prefdefined MCI entries must load both (e.g. %BN is expected to resolve)
|
|
|
|
//
|
|
|
|
var self = this;
|
|
|
|
self.viewControllers = {};
|
|
|
|
|
|
|
|
//var vcOpts = { client : self.client };
|
|
|
|
|
|
|
|
_.forEach(mciData, function entry(mciMap, name) {
|
|
|
|
assert('menu' === name || 'prompt' === name);
|
|
|
|
self.viewControllers[name] = new ViewController( { client : self.client } );
|
|
|
|
});
|
|
|
|
|
|
|
|
var viewsReady = function(err) {
|
|
|
|
// :TODO: what should really happen here?
|
|
|
|
if(err) {
|
|
|
|
Log.warn(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(self.viewControllers.menu) {
|
|
|
|
var menuLoadOpts = {
|
|
|
|
mciMap : mciData.menu,
|
|
|
|
callingMenu : self,
|
|
|
|
withoutForm : _.isObject(mciData.prompt),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.viewControllers.menu.loadFromMenuConfig(menuLoadOpts, viewsReady);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(self.viewControllers.prompt) {
|
|
|
|
var promptLoadOpts = {
|
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.prompt,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.viewControllers.prompt.loadFromPromptConfig(promptLoadOpts, viewsReady);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-24 05:12:19 +00:00
|
|
|
MenuModule.prototype.finishedLoading = function() {
|
2015-04-19 08:13:13 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if(_.isNumber(this.menuConfig.options.nextTimeout) &&
|
|
|
|
_.isString(this.menuConfig.next))
|
|
|
|
{
|
|
|
|
setTimeout(function nextTimeout() {
|
|
|
|
self.client.gotoMenuModule( { name : self.menuConfig.next } );
|
|
|
|
}, this.menuConfig.options.nextTimeout);
|
2015-04-21 04:50:58 +00:00
|
|
|
} else {
|
|
|
|
if(!_.isObject(self.menuConfig.form) && !_.isString(self.menuConfig.prompt) &&
|
|
|
|
_.isString(self.menuConfig.action))
|
|
|
|
{
|
|
|
|
menuUtil.handleAction(self.client, null, self.menuConfig);
|
|
|
|
}
|
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
};
|