2015-03-19 05:08:23 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2017-01-23 04:30:49 +00:00
|
|
|
const PluginModule = require('./plugin_module.js').PluginModule;
|
|
|
|
const theme = require('./theme.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const menuUtil = require('./menu_util.js');
|
|
|
|
const Config = require('./config.js').config;
|
|
|
|
const stringFormat = require('../core/string_format.js');
|
|
|
|
const MultiLineEditTextView = require('../core/multi_line_edit_text_view.js').MultiLineEditTextView;
|
2017-01-26 05:18:05 +00:00
|
|
|
const Errors = require('../core/enig_error.js').Errors;
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2016-12-31 21:50:29 +00:00
|
|
|
// deps
|
2017-01-26 05:18:05 +00:00
|
|
|
const async = require('async');
|
|
|
|
const assert = require('assert');
|
|
|
|
const _ = require('lodash');
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.MenuModule = class MenuModule extends PluginModule {
|
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
this.menuName = options.menuName;
|
|
|
|
this.menuConfig = options.menuConfig;
|
|
|
|
this.client = options.client;
|
|
|
|
this.menuConfig.options = options.menuConfig.options || {};
|
|
|
|
this.menuMethods = {}; // methods called from @method's
|
|
|
|
this.menuConfig.config = this.menuConfig.config || {};
|
|
|
|
|
|
|
|
this.cls = _.isBoolean(this.menuConfig.options.cls) ? this.menuConfig.options.cls : Config.menus.cls;
|
|
|
|
|
|
|
|
this.viewControllers = {};
|
|
|
|
}
|
2015-09-08 03:18:09 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
enter() {
|
|
|
|
this.initSequence();
|
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
leave() {
|
|
|
|
this.detachViewControllers();
|
|
|
|
}
|
2015-09-07 06:12:01 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
initSequence() {
|
|
|
|
const self = this;
|
|
|
|
const mciData = {};
|
|
|
|
let pausePosition;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function beforeDisplayArt(callback) {
|
|
|
|
self.beforeArt(callback);
|
|
|
|
},
|
|
|
|
function displayMenuArt(callback) {
|
|
|
|
if(!_.isString(self.menuConfig.art)) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
2015-09-09 04:08:45 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
self.displayAsset(
|
|
|
|
self.menuConfig.art,
|
|
|
|
self.menuConfig.options,
|
|
|
|
(err, artData) => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.trace('Could not display art', { art : self.menuConfig.art, reason : err.message } );
|
|
|
|
} else {
|
|
|
|
mciData.menu = artData.mciMap;
|
|
|
|
}
|
2015-10-10 05:35:40 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
return callback(null); // any errors are non-fatal
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function moveToPromptLocation(callback) {
|
|
|
|
if(self.menuConfig.prompt) {
|
|
|
|
// :TODO: fetch and move cursor to prompt location, if supplied. See notes/etc. on placements
|
|
|
|
}
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function displayPromptArt(callback) {
|
|
|
|
if(!_.isString(self.menuConfig.prompt)) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
2015-07-29 04:31:28 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
if(!_.isObject(self.menuConfig.promptConfig)) {
|
|
|
|
return callback(Errors.MissingConfig('Prompt specified but no "promptConfig" block found'));
|
|
|
|
}
|
2015-09-09 04:31:38 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
self.displayAsset(
|
|
|
|
self.menuConfig.promptConfig.art,
|
|
|
|
self.menuConfig.options,
|
|
|
|
(err, artData) => {
|
|
|
|
if(artData) {
|
|
|
|
mciData.prompt = artData.mciMap;
|
|
|
|
}
|
|
|
|
return callback(err); // pass err here; prompts *must* have art
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function recordCursorPosition(callback) {
|
|
|
|
if(!self.shouldPause()) {
|
|
|
|
return callback(null); // cursor position not needed
|
|
|
|
}
|
2015-09-27 21:35:24 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
self.client.once('cursor position report', pos => {
|
|
|
|
pausePosition = { row : pos[0], col : 1 };
|
|
|
|
self.client.log.trace('After art position recorded', { position : pausePosition } );
|
|
|
|
return callback(null);
|
|
|
|
});
|
2015-09-12 23:17:00 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
self.client.term.rawWrite(ansi.queryPos());
|
|
|
|
},
|
|
|
|
function afterArtDisplayed(callback) {
|
|
|
|
return self.mciReady(mciData, callback);
|
|
|
|
},
|
|
|
|
function displayPauseIfRequested(callback) {
|
|
|
|
if(!self.shouldPause()) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
2015-09-12 23:17:00 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
return self.pausePrompt(pausePosition, callback);
|
|
|
|
},
|
|
|
|
function finishAndNext(callback) {
|
|
|
|
self.finishedLoading();
|
|
|
|
return self.autoNextMenu(callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.warn('Error during init sequence', { error : err.message } );
|
2015-09-12 23:17:00 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
return self.prevMenu( () => { /* dummy */ } );
|
2015-09-10 03:31:04 +00:00
|
|
|
}
|
2015-09-09 04:31:38 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
);
|
|
|
|
}
|
2015-11-14 19:22:21 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
beforeArt(cb) {
|
|
|
|
if(_.isNumber(this.menuConfig.options.baudRate)) {
|
|
|
|
// :TODO: some terminals not supporting cterm style emulated baud rate end up displaying a broken ESC sequence or a single "r" here
|
|
|
|
this.client.term.rawWrite(ansi.setEmulatedBaudRate(this.menuConfig.options.baudRate));
|
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
if(this.cls) {
|
|
|
|
this.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
2015-08-12 23:26:05 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
mciReady(mciData, cb) {
|
|
|
|
// available for sub-classes
|
|
|
|
return cb(null);
|
|
|
|
}
|
2015-09-18 05:11:21 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
finishedLoading() {
|
|
|
|
// nothing in base
|
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
getSaveState() {
|
|
|
|
// nothing in base
|
|
|
|
}
|
2016-08-11 04:48:13 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
restoreSavedState(/*savedState*/) {
|
|
|
|
// nothing in base
|
|
|
|
}
|
2016-08-11 04:48:13 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
getMenuResult() {
|
|
|
|
// nothing in base
|
|
|
|
}
|
2016-08-11 04:48:13 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
nextMenu(cb) {
|
|
|
|
if(!this.haveNext()) {
|
|
|
|
return this.prevMenu(cb); // no next, go to prev
|
2016-08-11 04:48:13 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
|
|
|
|
return this.client.menuStack.next(cb);
|
|
|
|
}
|
2015-09-18 05:11:21 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
prevMenu(cb) {
|
|
|
|
return this.client.menuStack.prev(cb);
|
|
|
|
}
|
2015-09-18 05:11:21 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
gotoMenu(name, options, cb) {
|
|
|
|
return this.client.menuStack.goto(name, options, cb);
|
2015-11-13 23:13:20 +00:00
|
|
|
}
|
2015-11-03 23:42:11 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
addViewController(name, vc) {
|
|
|
|
assert(!this.viewControllers[name], `ViewController by the name of "${name}" already exists!`);
|
2015-11-03 23:42:11 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
this.viewControllers[name] = vc;
|
|
|
|
return vc;
|
|
|
|
}
|
2015-11-03 23:42:11 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
detachViewControllers() {
|
|
|
|
Object.keys(this.viewControllers).forEach( name => {
|
|
|
|
this.viewControllers[name].detachClientEvents();
|
|
|
|
});
|
|
|
|
}
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
shouldPause() {
|
|
|
|
return ('end' === this.menuConfig.options.pause || true === this.menuConfig.options.pause);
|
|
|
|
}
|
2015-03-23 04:52:04 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
hasNextTimeout() {
|
|
|
|
return _.isNumber(this.menuConfig.options.nextTimeout);
|
2015-07-03 05:28:39 +00:00
|
|
|
}
|
2016-06-29 05:06:35 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
haveNext() {
|
|
|
|
return (_.isString(this.menuConfig.next) || _.isArray(this.menuConfig.next));
|
2016-07-03 05:08:54 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
|
|
|
|
autoNextMenu(cb) {
|
|
|
|
const self = this;
|
2016-07-03 05:08:54 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
function gotoNextMenu() {
|
|
|
|
if(self.haveNext()) {
|
|
|
|
return menuUtil.handleNext(self.client, self.menuConfig.next, {}, cb);
|
|
|
|
} else {
|
|
|
|
return self.prevMenu(cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.has(this.menuConfig, 'runtime.autoNext') && true === this.menuConfig.runtime.autoNext) {
|
|
|
|
if(this.hasNextTimeout()) {
|
|
|
|
setTimeout( () => {
|
|
|
|
return gotoNextMenu();
|
|
|
|
}, this.menuConfig.options.nextTimeout);
|
|
|
|
} else {
|
|
|
|
return gotoNextMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 05:12:19 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
standardMCIReadyHandler(mciData, cb) {
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function addViewControllers(callback) {
|
|
|
|
_.forEach(mciData, (mciMap, name) => {
|
|
|
|
assert('menu' === name || 'prompt' === name);
|
|
|
|
self.addViewController(name, new ViewController( { client : self.client } ) );
|
|
|
|
});
|
2015-03-24 05:12:19 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function createMenu(callback) {
|
|
|
|
if(!self.viewControllers.menu) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const menuLoadOpts = {
|
2015-07-30 04:09:51 +00:00
|
|
|
mciMap : mciData.menu,
|
|
|
|
callingMenu : self,
|
|
|
|
withoutForm : _.isObject(mciData.prompt),
|
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
self.viewControllers.menu.loadFromMenuConfig(menuLoadOpts, err => {
|
|
|
|
return callback(err);
|
2015-07-30 04:09:51 +00:00
|
|
|
});
|
2017-01-26 05:18:05 +00:00
|
|
|
},
|
|
|
|
function createPrompt(callback) {
|
|
|
|
if(!self.viewControllers.prompt) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const promptLoadOpts = {
|
2015-07-30 04:09:51 +00:00
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.prompt,
|
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
self.viewControllers.prompt.loadFromPromptConfig(promptLoadOpts, err => {
|
|
|
|
return callback(err);
|
2015-07-30 04:09:51 +00:00
|
|
|
});
|
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
2015-07-30 04:09:51 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
);
|
|
|
|
}
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
displayAsset(name, options, cb) {
|
|
|
|
if(_.isFunction(options)) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
if(options.clearScreen) {
|
|
|
|
this.client.term.rawWrite(ansi.clearScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme.displayThemedAsset(
|
|
|
|
name,
|
|
|
|
this.client,
|
|
|
|
Object.assign( { font : this.menuConfig.config.font }, options ),
|
|
|
|
(err, artData) => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err, artData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2016-12-31 21:50:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
prepViewController(name, formId, artData, cb) {
|
|
|
|
if(_.isUndefined(this.viewControllers[name])) {
|
|
|
|
const vcOpts = {
|
|
|
|
client : this.client,
|
|
|
|
formId : formId,
|
|
|
|
};
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
const vc = this.addViewController(name, new ViewController(vcOpts));
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
const loadOpts = {
|
|
|
|
callingMenu : this,
|
|
|
|
mciMap : artData.mciMap,
|
|
|
|
formId : formId,
|
|
|
|
};
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
return vc.loadFromMenuConfig(loadOpts, cb);
|
|
|
|
}
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
this.viewControllers[name].setFocus(true);
|
|
|
|
return cb(null);
|
2016-12-31 21:50:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
prepViewControllerWithArt(name, formId, options, cb) {
|
|
|
|
this.displayAsset(
|
|
|
|
this.menuConfig.config.art[name],
|
|
|
|
options,
|
|
|
|
(err, artData) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
return this.prepViewController(name, formId, artData, cb);
|
2016-12-31 21:50:29 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
);
|
|
|
|
}
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
pausePrompt(position, cb) {
|
|
|
|
if(!cb && _.isFunction(position)) {
|
|
|
|
cb = position;
|
|
|
|
position = null;
|
2016-12-31 21:50:29 +00:00
|
|
|
}
|
2017-01-22 05:09:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
if(position) {
|
|
|
|
position.x = position.row || position.x || 1;
|
|
|
|
position.y = position.col || position.y || 1;
|
2017-01-23 04:30:49 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
this.client.term.rawWrite(ansi.goto(position.x, position.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme.displayThemedPause( { client : this.client }, cb);
|
2017-01-23 04:30:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
setViewText(formName, mciId, text, appendMultiLine) {
|
|
|
|
const view = this.viewControllers[formName].getView(mciId);
|
|
|
|
if(!view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(appendMultiLine && (view instanceof MultiLineEditTextView)) {
|
|
|
|
view.addText(text);
|
|
|
|
} else {
|
|
|
|
view.setText(text);
|
|
|
|
}
|
2017-01-22 05:09:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
updateCustomViewTextsWithFilter(formName, startId, fmtObj, options) {
|
|
|
|
options = options || {};
|
2017-01-23 04:30:49 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
let textView;
|
|
|
|
let customMciId = startId;
|
|
|
|
const config = this.menuConfig.config;
|
2017-01-22 05:09:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
while( (textView = this.viewControllers[formName].getView(customMciId)) ) {
|
|
|
|
const key = `${formName}InfoFormat${customMciId}`; // e.g. "mainInfoFormat10"
|
|
|
|
const format = config[key];
|
2017-01-22 05:09:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
if(format && (!options.filter || options.filter.find(f => format.indexOf(f) > - 1))) {
|
|
|
|
const text = stringFormat(format, fmtObj);
|
2017-01-23 04:30:49 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
if(options.appendMultiLine && (textView instanceof MultiLineEditTextView)) {
|
|
|
|
textView.addText(text);
|
|
|
|
} else {
|
|
|
|
textView.setText(text);
|
|
|
|
}
|
2017-01-23 04:30:49 +00:00
|
|
|
}
|
2017-01-22 05:09:29 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
++customMciId;
|
|
|
|
}
|
2017-01-22 05:09:29 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
};
|