2015-03-19 05:08:23 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +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;
|
|
|
|
const { getPredefinedMCIValue } = require('../core/predefined_mci.js');
|
|
|
|
|
|
|
|
// deps
|
|
|
|
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 {
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
2018-06-23 03:26:46 +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 || {};
|
2018-08-05 20:06:30 +00:00
|
|
|
this.cls = _.get(this.menuConfig.config, 'cls', Config().menus.cls);
|
2018-06-23 03:26:46 +00:00
|
|
|
this.viewControllers = {};
|
2018-12-01 06:20:44 +00:00
|
|
|
this.interrupt = (_.get(this.menuConfig.config, 'interrupt', MenuModule.InterruptTypes.Queued)).toLowerCase();
|
|
|
|
}
|
2018-11-13 05:03:28 +00:00
|
|
|
|
2018-12-01 06:20:44 +00:00
|
|
|
static get InterruptTypes() {
|
|
|
|
return {
|
|
|
|
Never : 'never',
|
|
|
|
Queued : 'queued',
|
|
|
|
Realtime : 'realtime',
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enter() {
|
|
|
|
this.initSequence();
|
|
|
|
}
|
|
|
|
|
|
|
|
leave() {
|
|
|
|
this.detachViewControllers();
|
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
2018-06-23 03:26:46 +00:00
|
|
|
const self = this;
|
|
|
|
const mciData = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
let pausePosition;
|
|
|
|
|
2018-11-18 01:56:09 +00:00
|
|
|
const hasArt = () => {
|
|
|
|
return _.isString(self.menuConfig.art) ||
|
|
|
|
(Array.isArray(self.menuConfig.art) && _.has(self.menuConfig.art[0], 'acs'));
|
|
|
|
};
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.series(
|
|
|
|
[
|
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) {
|
2018-11-18 01:56:09 +00:00
|
|
|
if(!hasArt()) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.trace('Could not display art', { art : self.menuConfig.art, reason : err.message } );
|
|
|
|
} else {
|
|
|
|
mciData.menu = artData.mciMap;
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback(null); // any errors are non-fatal
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function moveToPromptLocation(callback) {
|
|
|
|
if(self.menuConfig.prompt) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: fetch and move cursor to prompt location, if supplied. See notes/etc. on placements
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function displayPromptArt(callback) {
|
|
|
|
if(!_.isString(self.menuConfig.prompt)) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.isObject(self.menuConfig.promptConfig)) {
|
|
|
|
return callback(Errors.MissingConfig('Prompt specified but no "promptConfig" block found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.displayAsset(
|
|
|
|
self.menuConfig.promptConfig.art,
|
2018-08-05 20:06:30 +00:00
|
|
|
self.menuConfig.config,
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, artData) => {
|
|
|
|
if(artData) {
|
|
|
|
mciData.prompt = artData.mciMap;
|
|
|
|
}
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback(err); // pass err here; prompts *must* have art
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function recordCursorPosition(callback) {
|
|
|
|
if(!self.shouldPause()) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback(null); // cursor position not needed
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.client.once('cursor position report', pos => {
|
|
|
|
pausePosition = { row : pos[0], col : 1 };
|
|
|
|
self.client.log.trace('After art position recorded', pausePosition );
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.client.term.rawWrite(ansi.queryPos());
|
|
|
|
},
|
|
|
|
function afterArtDisplayed(callback) {
|
|
|
|
return self.mciReady(mciData, callback);
|
|
|
|
},
|
|
|
|
function displayPauseIfRequested(callback) {
|
|
|
|
if(!self.shouldPause()) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 } );
|
|
|
|
|
|
|
|
return self.prevMenu( () => { /* dummy */ } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeArt(cb) {
|
2018-08-05 20:06:30 +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
|
2018-08-05 20:06:30 +00:00
|
|
|
this.client.term.rawWrite(ansi.setEmulatedBaudRate(this.menuConfig.config.baudRate));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(this.cls) {
|
|
|
|
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) {
|
2018-12-01 06:20:44 +00:00
|
|
|
if(MenuModule.InterruptTypes.Never === this.interrupt) {
|
2018-11-13 05:03:28 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
2018-12-01 06:20:44 +00:00
|
|
|
let opts = { cls : true }; // clear screen for first message
|
|
|
|
|
2018-11-13 05:03:28 +00:00
|
|
|
async.whilst(
|
|
|
|
() => 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) {
|
2018-12-01 06:20:44 +00:00
|
|
|
if(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
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Default impl: clear screen -> standard display -> reload menu
|
|
|
|
//
|
2018-12-01 06:20:44 +00:00
|
|
|
this.client.interruptQueue.displayWithItem(
|
|
|
|
Object.assign({}, interruptItem, { cls : true }),
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err, false);
|
|
|
|
}
|
|
|
|
this.reload(err => {
|
|
|
|
return cb(err, err ? false : true);
|
|
|
|
});
|
2018-11-15 05:47:20 +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) {
|
|
|
|
if(!this.haveNext()) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return this.prevMenu(cb); // no next, go to prev
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 05:03:28 +00:00
|
|
|
this.displayQueuedInterruptions( () => {
|
|
|
|
return this.client.menuStack.next(cb);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prevMenu(cb) {
|
2018-11-13 05:03:28 +00:00
|
|
|
this.displayQueuedInterruptions( () => {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
setTimeout( () => {
|
|
|
|
return this.prevMenu(cb);
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
addViewController(name, vc) {
|
|
|
|
assert(!this.viewControllers[name], `ViewController by the name of "${name}" already exists!`);
|
|
|
|
|
|
|
|
this.viewControllers[name] = vc;
|
|
|
|
return vc;
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:45:14 +00:00
|
|
|
removeViewController(name) {
|
|
|
|
if(this.viewControllers[name]) {
|
|
|
|
this.viewControllers[name].detachClientEvents();
|
|
|
|
delete this.viewControllers[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
detachViewControllers() {
|
|
|
|
Object.keys(this.viewControllers).forEach( name => {
|
|
|
|
this.viewControllers[name].detachClientEvents();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldPause() {
|
2018-08-05 20:06:30 +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() {
|
|
|
|
return (_.isString(this.menuConfig.next) || _.isArray(this.menuConfig.next));
|
|
|
|
}
|
|
|
|
|
|
|
|
autoNextMenu(cb) {
|
2018-11-13 05:03:28 +00:00
|
|
|
const gotoNextMenu = () => {
|
|
|
|
if(this.haveNext()) {
|
|
|
|
this.displayQueuedInterruptions( () => {
|
|
|
|
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
|
|
|
|
|
|
|
if(_.has(this.menuConfig, 'runtime.autoNext') && true === this.menuConfig.runtime.autoNext) {
|
|
|
|
if(this.hasNextTimeout()) {
|
|
|
|
setTimeout( () => {
|
|
|
|
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.
|
|
|
|
// * Standard/prefdefined 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);
|
|
|
|
self.addViewController(name, new ViewController( { client : self.client } ) );
|
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function createMenu(callback) {
|
|
|
|
if(!self.viewControllers.menu) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const menuLoadOpts = {
|
2018-06-23 03:26:46 +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) {
|
|
|
|
if(!self.viewControllers.prompt) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const promptLoadOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.prompt,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.viewControllers.prompt.loadFromPromptConfig(promptLoadOpts, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayAsset(name, options, cb) {
|
|
|
|
if(_.isFunction(options)) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options.clearScreen) {
|
|
|
|
this.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme.displayThemedAsset(
|
|
|
|
name,
|
|
|
|
this.client,
|
|
|
|
Object.assign( { font : this.menuConfig.config.font }, options ),
|
|
|
|
(err, artData) => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err, artData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepViewController(name, formId, mciMap, cb) {
|
2018-07-10 02:27:09 +00:00
|
|
|
const needsCreated = _.isUndefined(this.viewControllers[name]);
|
|
|
|
if(needsCreated) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const vcOpts = {
|
2018-06-23 03:26:46 +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 = {
|
2018-06-23 03:26:46 +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) {
|
|
|
|
this.displayAsset(
|
|
|
|
this.menuConfig.config.art[name],
|
|
|
|
options,
|
|
|
|
(err, artData) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.prepViewController(name, formId, artData.mciMap, cb);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
optionalMoveToPosition(position) {
|
|
|
|
if(position) {
|
|
|
|
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) {
|
|
|
|
if(!cb && _.isFunction(position)) {
|
|
|
|
cb = position;
|
|
|
|
position = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.optionalMoveToPosition(position);
|
|
|
|
|
|
|
|
return theme.displayThemedPause(this.client, cb);
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:45:14 +00:00
|
|
|
promptForInput( { formName, formId, promptName, prevFormName, position } = {}, options, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
if(!cb && _.isFunction(options)) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2017-02-08 03:20:10 +00:00
|
|
|
|
2018-07-05 00:45:14 +00:00
|
|
|
options.viewController = this.addViewController(
|
|
|
|
formName,
|
|
|
|
new ViewController( { client : this.client, formId } )
|
|
|
|
);
|
|
|
|
|
|
|
|
options.trailingLF = _.get(options, 'trailingLF', false);
|
|
|
|
|
|
|
|
let prevVc;
|
|
|
|
if(prevFormName) {
|
|
|
|
prevVc = this.viewControllers[prevFormName];
|
|
|
|
if(prevVc) {
|
|
|
|
prevVc.setFocus(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//let artHeight;
|
|
|
|
options.submitNotify = () => {
|
|
|
|
if(prevVc) {
|
|
|
|
prevVc.setFocus(true);
|
|
|
|
}
|
|
|
|
this.removeViewController(formName);
|
|
|
|
if(options.clearAtSubmit) {
|
|
|
|
this.optionalMoveToPosition(position);
|
|
|
|
if(options.clearWidth) {
|
|
|
|
this.client.term.rawWrite(`${ansi.reset()}${' '.repeat(options.clearWidth)}`);
|
|
|
|
} 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);
|
|
|
|
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
|
|
|
|
2018-06-22 05:15:04 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCustomViewTextsWithFilter(formName, startId, fmtObj, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
let textView;
|
|
|
|
let customMciId = startId;
|
2018-06-23 03:26:46 +00:00
|
|
|
const config = this.menuConfig.config;
|
|
|
|
const endId = options.endId || 99; // we'll fail to get a view before 99
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
while(customMciId <= endId && (textView = this.viewControllers[formName].getView(customMciId)) ) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const key = `${formName}InfoFormat${customMciId}`; // e.g. "mainInfoFormat10"
|
|
|
|
const format = config[key];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if(format && (!options.filter || options.filter.find(f => format.indexOf(f) > - 1))) {
|
|
|
|
const text = stringFormat(format, fmtObj);
|
|
|
|
|
|
|
|
if(options.appendMultiLine && (textView instanceof MultiLineEditTextView)) {
|
|
|
|
textView.addText(text);
|
|
|
|
} else {
|
|
|
|
textView.setText(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++customMciId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshPredefinedMciViewsByCode(formName, mciCodes) {
|
|
|
|
const form = _.get(this, [ 'viewControllers', formName] );
|
|
|
|
if(form) {
|
|
|
|
form.getViewsByMciCode(mciCodes).forEach(v => {
|
|
|
|
if(!v.setText) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v.setText(getPredefinedMCIValue(this.client, v.mciCode));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 03:51:11 +00:00
|
|
|
|
|
|
|
validateMCIByViewIds(formName, viewIds, cb) {
|
|
|
|
if(!Array.isArray(viewIds)) {
|
|
|
|
viewIds = [ viewIds ];
|
|
|
|
}
|
|
|
|
const form = _.get(this, [ 'viewControllers', formName ] );
|
|
|
|
if(!form) {
|
|
|
|
return cb(Errors.DoesNotExist(`Form does not exist: ${formName}`));
|
|
|
|
}
|
|
|
|
for(let i = 0; i < viewIds.length; ++i) {
|
|
|
|
if(!form.hasView(viewIds[i])) {
|
|
|
|
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'
|
|
|
|
//
|
|
|
|
if(!_.isObject(fields)) {
|
|
|
|
return cb(Errors.Invalid('Invalid validator!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = this.config || this.menuConfig.config;
|
|
|
|
let firstBadKey;
|
|
|
|
let badReason;
|
|
|
|
const good = _.every(fields, (type, key) => {
|
|
|
|
if(_.isFunction(type)) {
|
|
|
|
if(!type(key, config)) {
|
|
|
|
firstBadKey = key;
|
|
|
|
badReason = 'Validate failure';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const c = config[key];
|
|
|
|
let typeOk;
|
|
|
|
if(_.isUndefined(c)) {
|
|
|
|
typeOk = false;
|
|
|
|
badReason = `Missing "${key}", expected ${type}`;
|
|
|
|
} else {
|
|
|
|
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 :
|
|
|
|
typeOk = false;
|
|
|
|
badReason = `Don't know how to validate ${type}`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!typeOk) {
|
|
|
|
firstBadKey = key;
|
|
|
|
if(!badReason) {
|
|
|
|
badReason = `Expected ${type}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return typeOk;
|
|
|
|
});
|
|
|
|
|
|
|
|
return cb(good ? null : Errors.Invalid(`Invalid or missing config option "${firstBadKey}" (${badReason})`));
|
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
};
|