2015-03-19 05:08:23 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +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').get;
|
|
|
|
const stringFormat = require('../core/string_format.js');
|
|
|
|
const MultiLineEditTextView =
|
|
|
|
require('../core/multi_line_edit_text_view.js').MultiLineEditTextView;
|
|
|
|
const Errors = require('../core/enig_error.js').Errors;
|
2018-06-23 03:26:46 +00:00
|
|
|
const { getPredefinedMCIValue } = require('../core/predefined_mci.js');
|
2022-06-02 17:12:23 +00:00
|
|
|
const EnigAssert = require('./enigma_assert');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
|
|
|
const assert = require('assert');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const iconvDecode = require('iconv-lite').decode;
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.MenuModule = class MenuModule extends PluginModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.menuName = options.menuName;
|
|
|
|
this.menuConfig = options.menuConfig;
|
|
|
|
this.client = options.client;
|
|
|
|
this.menuMethods = {}; // methods called from @method's
|
|
|
|
this.menuConfig.config = this.menuConfig.config || {};
|
|
|
|
this.cls = _.get(this.menuConfig.config, 'cls', Config().menus.cls);
|
|
|
|
this.viewControllers = {};
|
|
|
|
this.interrupt = _.get(
|
|
|
|
this.menuConfig.config,
|
|
|
|
'interrupt',
|
|
|
|
MenuModule.InterruptTypes.Queued
|
|
|
|
).toLowerCase();
|
|
|
|
|
|
|
|
if (MenuModule.InterruptTypes.Realtime === this.interrupt) {
|
|
|
|
this.realTimeInterrupt = 'blocked';
|
2019-01-04 05:03:00 +00:00
|
|
|
}
|
2018-12-01 06:20:44 +00:00
|
|
|
}
|
2018-11-13 05:03:28 +00:00
|
|
|
|
2018-12-01 06:20:44 +00:00
|
|
|
static get InterruptTypes() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
Never: 'never',
|
|
|
|
Queued: 'queued',
|
|
|
|
Realtime: 'realtime',
|
2018-12-01 06:20:44 +00:00
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enter() {
|
|
|
|
this.initSequence();
|
|
|
|
}
|
|
|
|
|
|
|
|
leave() {
|
|
|
|
this.detachViewControllers();
|
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
2022-06-05 20:04:25 +00:00
|
|
|
const self = this;
|
|
|
|
const mciData = {};
|
|
|
|
let pausePosition = { row: 0, column: 0 };
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-11-18 01:56:09 +00:00
|
|
|
const hasArt = () => {
|
2022-06-05 20:04:25 +00:00
|
|
|
return (
|
|
|
|
_.isString(self.menuConfig.art) ||
|
|
|
|
(Array.isArray(self.menuConfig.art) &&
|
|
|
|
_.has(self.menuConfig.art[0], 'acs'))
|
|
|
|
);
|
2018-11-18 01:56:09 +00:00
|
|
|
};
|
|
|
|
|
2022-03-26 01:10:33 +00:00
|
|
|
async.waterfall(
|
2018-06-22 05:15:04 +00:00
|
|
|
[
|
2018-11-13 05:03:28 +00:00
|
|
|
function beforeArtInterrupt(callback) {
|
2018-12-01 06:20:44 +00:00
|
|
|
return self.displayQueuedInterruptions(callback);
|
2018-11-13 05:03:28 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
function beforeDisplayArt(callback) {
|
2018-11-13 05:03:28 +00:00
|
|
|
return self.beforeArt(callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function displayMenuArt(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!hasArt()) {
|
2022-03-26 01:10:33 +00:00
|
|
|
return callback(null, null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.displayAsset(
|
|
|
|
self.menuConfig.art,
|
2018-08-05 20:06:30 +00:00
|
|
|
self.menuConfig.config,
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, artData) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.client.log.trace('Could not display art', {
|
|
|
|
art: self.menuConfig.art,
|
|
|
|
reason: err.message,
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
mciData.menu = artData.mciMap;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (artData) {
|
2022-03-26 01:10:33 +00:00
|
|
|
pausePosition.row = artData.height + 1;
|
|
|
|
}
|
2022-03-25 01:01:07 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(null, artData); // any errors are non-fatal
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2022-03-26 01:10:33 +00:00
|
|
|
function displayPromptArt(artData, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isString(self.menuConfig.prompt)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isObject(self.menuConfig.promptConfig)) {
|
|
|
|
return callback(
|
|
|
|
Errors.MissingConfig(
|
|
|
|
'Prompt specified but no "promptConfig" block found'
|
|
|
|
)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 03:20:55 +00:00
|
|
|
const options = Object.assign({}, self.menuConfig.config);
|
2022-03-26 01:10:33 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isNumber(artData?.height)) {
|
2022-03-26 01:10:33 +00:00
|
|
|
options.startRow = artData.height + 1;
|
|
|
|
}
|
2022-03-25 01:01:07 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
self.displayAsset(
|
|
|
|
self.menuConfig.promptConfig.art,
|
2022-03-26 01:10:33 +00:00
|
|
|
options,
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, artData) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (artData) {
|
2018-06-22 05:15:04 +00:00
|
|
|
mciData.prompt = artData.mciMap;
|
2022-03-26 01:10:33 +00:00
|
|
|
pausePosition.row = artData.height + 1;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-03-26 01:10:33 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(err); // pass err here; prompts *must* have art
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function afterArtDisplayed(callback) {
|
|
|
|
return self.mciReady(mciData, callback);
|
|
|
|
},
|
|
|
|
function displayPauseIfRequested(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!self.shouldPause()) {
|
2022-03-28 19:18:00 +00:00
|
|
|
return callback(null, null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
self.client.term.termHeight > 0 &&
|
|
|
|
pausePosition.row > self.client.termHeight
|
|
|
|
) {
|
2022-03-26 00:27:01 +00:00
|
|
|
// If this scrolled, the prompt will go to the bottom of the screen
|
|
|
|
pausePosition.row = self.client.termHeight;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self.pausePrompt(pausePosition, callback);
|
|
|
|
},
|
2022-03-26 01:10:33 +00:00
|
|
|
function finishAndNext(artInfo, callback) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.finishedLoading();
|
2019-01-04 05:03:00 +00:00
|
|
|
self.realTimeInterrupt = 'allowed';
|
2018-06-22 05:15:04 +00:00
|
|
|
return self.autoNextMenu(callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.client.log.warn('Error during init sequence', {
|
|
|
|
error: err.message,
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return self.prevMenu(() => {
|
|
|
|
/* dummy */
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeArt(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isNumber(this.menuConfig.config.baudRate)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: some terminals not supporting cterm style emulated baud rate end up displaying a broken ESC sequence or a single "r" here
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client.term.rawWrite(
|
|
|
|
ansi.setEmulatedBaudRate(this.menuConfig.config.baudRate)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.cls) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
mciReady(mciData, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// available for sub-classes
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
finishedLoading() {
|
2018-06-23 03:26:46 +00:00
|
|
|
// nothing in base
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 05:03:28 +00:00
|
|
|
displayQueuedInterruptions(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (MenuModule.InterruptTypes.Never === this.interrupt) {
|
2018-11-13 05:03:28 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
let opts = { cls: true }; // clear screen for first message
|
2018-12-01 06:20:44 +00:00
|
|
|
|
2018-11-13 05:03:28 +00:00
|
|
|
async.whilst(
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => callback(null, this.client.interruptQueue.hasItems()),
|
2018-12-01 06:20:44 +00:00
|
|
|
next => {
|
|
|
|
this.client.interruptQueue.displayNext(opts, err => {
|
|
|
|
opts = {};
|
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
},
|
2018-11-13 05:03:28 +00:00
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-12-01 06:20:44 +00:00
|
|
|
);
|
2018-11-13 05:03:28 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 05:04:29 +00:00
|
|
|
attemptInterruptNow(interruptItem, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
this.realTimeInterrupt !== 'allowed' ||
|
|
|
|
MenuModule.InterruptTypes.Realtime !== this.interrupt
|
|
|
|
) {
|
2018-11-15 05:47:20 +00:00
|
|
|
return cb(null, false); // don't eat up the item; queue for later
|
|
|
|
}
|
|
|
|
|
2019-01-23 03:19:05 +00:00
|
|
|
this.realTimeInterrupt = 'blocked';
|
|
|
|
|
2018-11-15 05:47:20 +00:00
|
|
|
//
|
|
|
|
// Default impl: clear screen -> standard display -> reload menu
|
|
|
|
//
|
2019-01-23 03:19:05 +00:00
|
|
|
const done = (err, removeFromQueue) => {
|
|
|
|
this.realTimeInterrupt = 'allowed';
|
|
|
|
return cb(err, removeFromQueue);
|
|
|
|
};
|
|
|
|
|
2018-12-01 06:20:44 +00:00
|
|
|
this.client.interruptQueue.displayWithItem(
|
2022-06-05 20:04:25 +00:00
|
|
|
Object.assign({}, interruptItem, { cls: true }),
|
2018-12-01 06:20:44 +00:00
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2019-01-23 03:19:05 +00:00
|
|
|
return done(err, false);
|
2018-12-01 06:20:44 +00:00
|
|
|
}
|
|
|
|
this.reload(err => {
|
2019-01-23 03:19:05 +00:00
|
|
|
return done(err, err ? false : true);
|
2018-12-01 06:20:44 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
}
|
|
|
|
);
|
2018-11-15 05:04:29 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
getSaveState() {
|
2018-06-23 03:26:46 +00:00
|
|
|
// nothing in base
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
restoreSavedState(/*savedState*/) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// nothing in base
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getMenuResult() {
|
2018-06-23 03:26:46 +00:00
|
|
|
// default to the formData that was provided @ a submit, if any
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.submitFormData;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextMenu(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.haveNext()) {
|
|
|
|
return this.prevMenu(cb); // no next, go to prev
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.displayQueuedInterruptions(() => {
|
2018-11-13 05:03:28 +00:00
|
|
|
return this.client.menuStack.next(cb);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prevMenu(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.displayQueuedInterruptions(() => {
|
2018-11-13 05:03:28 +00:00
|
|
|
return this.client.menuStack.prev(cb);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gotoMenu(name, options, cb) {
|
|
|
|
return this.client.menuStack.goto(name, options, cb);
|
|
|
|
}
|
|
|
|
|
2019-09-17 04:05:03 +00:00
|
|
|
gotoMenuOrPrev(name, options, cb) {
|
|
|
|
this.client.menuStack.goto(name, options, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!err) {
|
|
|
|
if (cb) {
|
2019-09-17 04:05:03 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.prevMenu(cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
gotoMenuOrShowMessage(name, message, options, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!cb && _.isFunction(options)) {
|
2019-09-17 04:05:03 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || { clearScreen: true };
|
|
|
|
|
|
|
|
this.gotoMenu(name, options, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
if (options.clearScreen) {
|
2019-09-17 04:05:03 +00:00
|
|
|
this.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.client.term.write(`${message}\n`);
|
2022-06-05 20:04:25 +00:00
|
|
|
return this.pausePrompt(() => {
|
2019-09-17 04:05:03 +00:00
|
|
|
return this.prevMenu(cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2019-09-17 04:05:03 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-18 02:00:47 +00:00
|
|
|
reload(cb) {
|
|
|
|
const prevMenu = this.client.menuStack.pop();
|
|
|
|
prevMenu.instance.leave();
|
|
|
|
return this.client.menuStack.goto(prevMenu.name, cb);
|
|
|
|
}
|
|
|
|
|
2018-07-22 21:57:51 +00:00
|
|
|
prevMenuOnTimeout(timeout, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
setTimeout(() => {
|
2018-07-22 21:57:51 +00:00
|
|
|
return this.prevMenu(cb);
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
addViewController(name, vc) {
|
2022-06-05 20:04:25 +00:00
|
|
|
assert(
|
|
|
|
!this.viewControllers[name],
|
|
|
|
`ViewController by the name of "${name}" already exists!`
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
this.viewControllers[name] = vc;
|
|
|
|
return vc;
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:45:14 +00:00
|
|
|
removeViewController(name) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.viewControllers[name]) {
|
2018-07-05 00:45:14 +00:00
|
|
|
this.viewControllers[name].detachClientEvents();
|
|
|
|
delete this.viewControllers[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
detachViewControllers() {
|
2022-06-05 20:04:25 +00:00
|
|
|
Object.keys(this.viewControllers).forEach(name => {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.viewControllers[name].detachClientEvents();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldPause() {
|
2022-06-05 20:04:25 +00:00
|
|
|
return (
|
|
|
|
'end' === this.menuConfig.config.pause ||
|
|
|
|
true === this.menuConfig.config.pause
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hasNextTimeout() {
|
2018-08-05 20:06:30 +00:00
|
|
|
return _.isNumber(this.menuConfig.config.nextTimeout);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
haveNext() {
|
2022-06-05 20:04:25 +00:00
|
|
|
return _.isString(this.menuConfig.next) || _.isArray(this.menuConfig.next);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
autoNextMenu(cb) {
|
2018-11-13 05:03:28 +00:00
|
|
|
const gotoNextMenu = () => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.haveNext()) {
|
|
|
|
this.displayQueuedInterruptions(() => {
|
2018-11-13 05:03:28 +00:00
|
|
|
return menuUtil.handleNext(this.client, this.menuConfig.next, {}, cb);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-11-13 05:03:28 +00:00
|
|
|
return this.prevMenu(cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-12-01 06:20:44 +00:00
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
_.has(this.menuConfig, 'runtime.autoNext') &&
|
|
|
|
true === this.menuConfig.runtime.autoNext
|
|
|
|
) {
|
|
|
|
if (this.hasNextTimeout()) {
|
|
|
|
setTimeout(() => {
|
2018-06-22 05:15:04 +00:00
|
|
|
return gotoNextMenu();
|
2018-08-05 20:06:30 +00:00
|
|
|
}, this.menuConfig.config.nextTimeout);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
return gotoNextMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
standardMCIReadyHandler(mciData, cb) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// A quick rundown:
|
|
|
|
// * We may have mciData.menu, mciData.prompt, or both.
|
|
|
|
// * Prompt form is favored over menu form if both are present.
|
2019-01-23 04:54:12 +00:00
|
|
|
// * Standard/predefined MCI entries must load both (e.g. %BN is expected to resolve)
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function addViewControllers(callback) {
|
|
|
|
_.forEach(mciData, (mciMap, name) => {
|
|
|
|
assert('menu' === name || 'prompt' === name);
|
2022-06-05 20:04:25 +00:00
|
|
|
self.addViewController(
|
|
|
|
name,
|
|
|
|
new ViewController({ client: self.client })
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function createMenu(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!self.viewControllers.menu) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const menuLoadOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
mciMap: mciData.menu,
|
|
|
|
callingMenu: self,
|
|
|
|
withoutForm: _.isObject(mciData.prompt),
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.viewControllers.menu.loadFromMenuConfig(menuLoadOpts, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function createPrompt(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!self.viewControllers.prompt) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const promptLoadOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
callingMenu: self,
|
|
|
|
mciMap: mciData.prompt,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.viewControllers.prompt.loadFromPromptConfig(
|
|
|
|
promptLoadOpts,
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-25 04:27:50 +00:00
|
|
|
displayAsset(nameOrData, options, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isFunction(options)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.clearScreen) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
options = Object.assign(
|
|
|
|
{ client: this.client, font: this.menuConfig.config.font },
|
|
|
|
options
|
|
|
|
);
|
2019-05-25 04:27:50 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Buffer.isBuffer(nameOrData)) {
|
2019-05-25 04:27:50 +00:00
|
|
|
const data = iconvDecode(nameOrData, options.encoding || 'cp437');
|
2022-06-05 20:04:25 +00:00
|
|
|
return theme.displayPreparedArt(options, { data }, (err, artData) => {
|
|
|
|
if (cb) {
|
|
|
|
return cb(err, artData);
|
2019-05-25 04:27:50 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
});
|
2019-05-25 04:27:50 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return theme.displayThemedAsset(
|
2019-05-25 04:27:50 +00:00
|
|
|
nameOrData,
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client,
|
2019-05-25 04:27:50 +00:00
|
|
|
options,
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, artData) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err, artData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepViewController(name, formId, mciMap, cb) {
|
2018-07-10 02:27:09 +00:00
|
|
|
const needsCreated = _.isUndefined(this.viewControllers[name]);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (needsCreated) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const vcOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
client: this.client,
|
|
|
|
formId: formId,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const vc = this.addViewController(name, new ViewController(vcOpts));
|
|
|
|
|
|
|
|
const loadOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
callingMenu: this,
|
|
|
|
mciMap: mciMap,
|
|
|
|
formId: formId,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return vc.loadFromMenuConfig(loadOpts, err => {
|
2018-07-10 02:27:09 +00:00
|
|
|
return cb(err, vc, true);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.viewControllers[name].setFocus(true);
|
|
|
|
|
2018-07-10 02:27:09 +00:00
|
|
|
return cb(null, this.viewControllers[name], false);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prepViewControllerWithArt(name, formId, options, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.displayAsset(this.menuConfig.config.art[name], options, (err, artData) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
|
|
|
|
return this.prepViewController(name, formId, artData.mciMap, cb);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
optionalMoveToPosition(position) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (position) {
|
2018-06-22 05:15:04 +00:00
|
|
|
position.x = position.row || position.x || 1;
|
|
|
|
position.y = position.col || position.y || 1;
|
|
|
|
|
|
|
|
this.client.term.rawWrite(ansi.goto(position.x, position.y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pausePrompt(position, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!cb && _.isFunction(position)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb = position;
|
|
|
|
position = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.optionalMoveToPosition(position);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return theme.displayThemedPause(this.client, { position }, cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
promptForInput(
|
|
|
|
{ formName, formId, promptName, prevFormName, position } = {},
|
|
|
|
options,
|
|
|
|
cb
|
|
|
|
) {
|
|
|
|
if (!cb && _.isFunction(options)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2017-02-08 03:20:10 +00:00
|
|
|
|
2018-07-05 00:45:14 +00:00
|
|
|
options.viewController = this.addViewController(
|
|
|
|
formName,
|
2022-06-05 20:04:25 +00:00
|
|
|
new ViewController({ client: this.client, formId })
|
2018-07-05 00:45:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
options.trailingLF = _.get(options, 'trailingLF', false);
|
|
|
|
|
|
|
|
let prevVc;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (prevFormName) {
|
2018-07-05 00:45:14 +00:00
|
|
|
prevVc = this.viewControllers[prevFormName];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (prevVc) {
|
2018-07-05 00:45:14 +00:00
|
|
|
prevVc.setFocus(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 03:53:11 +00:00
|
|
|
const originalSubmitNotify = options.submitNotify;
|
|
|
|
|
2018-07-05 00:45:14 +00:00
|
|
|
options.submitNotify = () => {
|
2022-06-14 03:53:11 +00:00
|
|
|
if (_.isFunction(originalSubmitNotify)) {
|
|
|
|
originalSubmitNotify();
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (prevVc) {
|
2018-07-05 00:45:14 +00:00
|
|
|
prevVc.setFocus(true);
|
|
|
|
}
|
|
|
|
this.removeViewController(formName);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.clearAtSubmit) {
|
2018-07-05 00:45:14 +00:00
|
|
|
this.optionalMoveToPosition(position);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.clearWidth) {
|
|
|
|
this.client.term.rawWrite(
|
|
|
|
`${ansi.reset()}${' '.repeat(options.clearWidth)}`
|
|
|
|
);
|
2018-07-05 00:45:14 +00:00
|
|
|
} else {
|
|
|
|
// :TODO: handle multi-rows via artHeight
|
|
|
|
this.client.term.rawWrite(ansi.eraseLine());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-02-08 03:20:10 +00:00
|
|
|
|
2018-07-05 00:45:14 +00:00
|
|
|
options.viewController.setFocus(true);
|
2017-02-08 03:20:10 +00:00
|
|
|
|
2018-07-05 00:45:14 +00:00
|
|
|
this.optionalMoveToPosition(position);
|
2022-06-14 03:53:11 +00:00
|
|
|
if (!options.position) {
|
|
|
|
options.position = position;
|
|
|
|
}
|
2018-07-05 00:45:14 +00:00
|
|
|
theme.displayThemedPrompt(promptName, this.client, options, (err, artInfo) => {
|
|
|
|
/*
|
|
|
|
if(artInfo) {
|
|
|
|
artHeight = artInfo.height;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return cb(err, artInfo);
|
|
|
|
});
|
2018-06-23 03:26:46 +00:00
|
|
|
}
|
2017-01-23 04:30:49 +00:00
|
|
|
|
2022-06-02 17:12:23 +00:00
|
|
|
displayArtAndPrepViewController(name, formId, options, cb) {
|
|
|
|
const config = this.menuConfig.config;
|
|
|
|
EnigAssert(_.isObject(config));
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
2022-06-12 20:12:03 +00:00
|
|
|
callback => {
|
|
|
|
if (options.clearScreen) {
|
2022-06-02 17:12:23 +00:00
|
|
|
this.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
theme.displayThemedAsset(
|
|
|
|
config.art[name],
|
|
|
|
this.client,
|
2022-06-12 20:12:03 +00:00
|
|
|
{ font: this.menuConfig.font, trailingLF: false },
|
2022-06-02 17:12:23 +00:00
|
|
|
(err, artData) => {
|
|
|
|
return callback(err, artData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(artData, callback) => {
|
2022-06-12 20:12:03 +00:00
|
|
|
if (_.isUndefined(this.viewControllers[name])) {
|
2022-06-02 17:12:23 +00:00
|
|
|
const vcOpts = {
|
2022-06-12 20:12:03 +00:00
|
|
|
client: this.client,
|
|
|
|
formId: formId,
|
2022-06-02 17:12:23 +00:00
|
|
|
};
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
if (!_.isUndefined(options.noInput)) {
|
2022-06-02 17:12:23 +00:00
|
|
|
vcOpts.noInput = options.noInput;
|
|
|
|
}
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
const vc = this.addViewController(
|
|
|
|
name,
|
|
|
|
new ViewController(vcOpts)
|
|
|
|
);
|
2022-06-02 17:12:23 +00:00
|
|
|
|
|
|
|
if (_.isFunction(options.artDataPrep)) {
|
|
|
|
try {
|
|
|
|
options.artDataPrep(name, artData, vc);
|
2022-06-12 20:12:03 +00:00
|
|
|
} catch (e) {
|
2022-06-02 17:12:23 +00:00
|
|
|
return callback(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadOpts = {
|
2022-06-12 20:12:03 +00:00
|
|
|
callingMenu: this,
|
|
|
|
mciMap: artData.mciMap,
|
|
|
|
formId: formId,
|
2022-08-16 04:23:14 +00:00
|
|
|
viewOffsets: options.viewOffsets,
|
2022-06-02 17:12:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.viewControllers[name].setFocus(true);
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
setViewText(formName, mciId, text, appendMultiLine) {
|
2019-05-25 04:27:50 +00:00
|
|
|
const view = this.getView(formName, mciId);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!view) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (appendMultiLine && view instanceof MultiLineEditTextView) {
|
2022-07-18 05:02:08 +00:00
|
|
|
view.setAnsi(text);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
view.setText(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 04:27:50 +00:00
|
|
|
getView(formName, id) {
|
|
|
|
const form = this.viewControllers[formName];
|
|
|
|
return form && form.getView(id);
|
|
|
|
}
|
|
|
|
|
2022-08-16 04:23:14 +00:00
|
|
|
getCustomViewsWithFilter(formName, startId, options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2022-08-16 04:23:14 +00:00
|
|
|
const views = [];
|
|
|
|
|
|
|
|
let view;
|
2018-06-22 05:15:04 +00:00
|
|
|
let customMciId = startId;
|
2022-06-05 20:04:25 +00:00
|
|
|
const config = this.menuConfig.config;
|
|
|
|
const endId = options.endId || 99; // we'll fail to get a view before 99
|
|
|
|
|
|
|
|
while (
|
|
|
|
customMciId <= endId &&
|
2022-08-16 04:23:14 +00:00
|
|
|
(view = this.viewControllers[formName].getView(customMciId))
|
2022-06-05 20:04:25 +00:00
|
|
|
) {
|
|
|
|
const key = `${formName}InfoFormat${customMciId}`; // e.g. "mainInfoFormat10"
|
|
|
|
const format = config[key];
|
|
|
|
|
|
|
|
if (
|
|
|
|
format &&
|
|
|
|
(!options.filter || options.filter.find(f => format.indexOf(f) > -1))
|
|
|
|
) {
|
2022-08-16 04:23:14 +00:00
|
|
|
view.key = key; // cache
|
|
|
|
views.push(view);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++customMciId;
|
|
|
|
}
|
2022-08-16 04:23:14 +00:00
|
|
|
|
|
|
|
return views;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCustomViewTextsWithFilter(formName, startId, fmtObj, options) {
|
|
|
|
options = options || {};
|
|
|
|
const views = this.getCustomViewsWithFilter(formName, startId, options);
|
|
|
|
const config = this.menuConfig.config;
|
|
|
|
|
|
|
|
views.forEach(view => {
|
|
|
|
const format = config[view.key];
|
|
|
|
const text = stringFormat(format, fmtObj);
|
|
|
|
|
|
|
|
if (options.appendMultiLine && view instanceof MultiLineEditTextView) {
|
|
|
|
view.addText(text);
|
|
|
|
} else {
|
|
|
|
if (view.getData() != text) {
|
|
|
|
view.setText(text);
|
|
|
|
} else {
|
|
|
|
view.redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
refreshPredefinedMciViewsByCode(formName, mciCodes) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const form = _.get(this, ['viewControllers', formName]);
|
|
|
|
if (form) {
|
2018-06-22 05:15:04 +00:00
|
|
|
form.getViewsByMciCode(mciCodes).forEach(v => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!v.setText) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v.setText(getPredefinedMCIValue(this.client, v.mciCode));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 03:51:11 +00:00
|
|
|
|
|
|
|
validateMCIByViewIds(formName, viewIds, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!Array.isArray(viewIds)) {
|
|
|
|
viewIds = [viewIds];
|
2018-11-15 03:51:11 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
const form = _.get(this, ['viewControllers', formName]);
|
|
|
|
if (!form) {
|
2018-11-15 03:51:11 +00:00
|
|
|
return cb(Errors.DoesNotExist(`Form does not exist: ${formName}`));
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
for (let i = 0; i < viewIds.length; ++i) {
|
|
|
|
if (!form.hasView(viewIds[i])) {
|
2018-11-15 03:51:11 +00:00
|
|
|
return cb(Errors.MissingMci(`Missing MCI ${viewIds[i]}`));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cb(null);
|
|
|
|
}
|
2018-12-03 02:30:50 +00:00
|
|
|
|
|
|
|
validateConfigFields(fields, cb) {
|
|
|
|
//
|
|
|
|
// fields is expected to be { key : type || validator(key, config) }
|
|
|
|
// where |type| is 'string', 'array', object', 'number'
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isObject(fields)) {
|
2018-12-03 02:30:50 +00:00
|
|
|
return cb(Errors.Invalid('Invalid validator!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = this.config || this.menuConfig.config;
|
|
|
|
let firstBadKey;
|
|
|
|
let badReason;
|
|
|
|
const good = _.every(fields, (type, key) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isFunction(type)) {
|
|
|
|
if (!type(key, config)) {
|
2018-12-03 02:30:50 +00:00
|
|
|
firstBadKey = key;
|
2022-06-05 20:04:25 +00:00
|
|
|
badReason = 'Validate failure';
|
2018-12-03 02:30:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const c = config[key];
|
|
|
|
let typeOk;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isUndefined(c)) {
|
2018-12-03 02:30:50 +00:00
|
|
|
typeOk = false;
|
|
|
|
badReason = `Missing "${key}", expected ${type}`;
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'string':
|
|
|
|
typeOk = _.isString(c);
|
|
|
|
break;
|
|
|
|
case 'object':
|
|
|
|
typeOk = _.isObject(c);
|
|
|
|
break;
|
|
|
|
case 'array':
|
|
|
|
typeOk = Array.isArray(c);
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
typeOk = !isNaN(parseInt(c));
|
|
|
|
break;
|
|
|
|
default:
|
2018-12-03 02:30:50 +00:00
|
|
|
typeOk = false;
|
|
|
|
badReason = `Don't know how to validate ${type}`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!typeOk) {
|
2018-12-03 02:30:50 +00:00
|
|
|
firstBadKey = key;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!badReason) {
|
2018-12-03 02:30:50 +00:00
|
|
|
badReason = `Expected ${type}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return typeOk;
|
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(
|
|
|
|
good
|
|
|
|
? null
|
|
|
|
: Errors.Invalid(
|
|
|
|
`Invalid or missing config option "${firstBadKey}" (${badReason})`
|
|
|
|
)
|
|
|
|
);
|
2018-12-03 02:30:50 +00:00
|
|
|
}
|
2020-11-10 04:32:34 +00:00
|
|
|
|
|
|
|
// Various common helpers
|
2022-06-12 20:12:03 +00:00
|
|
|
getDateFormat(defaultStyle = 'short') {
|
|
|
|
return (
|
|
|
|
this.config.dateFormat ||
|
|
|
|
this.client.currentTheme.helpers.getDateFormat(defaultStyle)
|
|
|
|
);
|
2020-11-10 04:32:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
getTimeFormat(defaultStyle = 'short') {
|
|
|
|
return (
|
|
|
|
this.config.timeFormat ||
|
|
|
|
this.client.currentTheme.helpers.getTimeFormat(defaultStyle)
|
|
|
|
);
|
2020-11-10 04:32:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
getDateTimeFormat(defaultStyle = 'short') {
|
|
|
|
return (
|
|
|
|
this.config.dateTimeFormat ||
|
|
|
|
this.client.currentTheme.helpers.getDateTimeFormat(defaultStyle)
|
|
|
|
);
|
2020-11-10 04:32:34 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
};
|