2014-10-23 05:41:00 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
var MCIViewFactory = require('./mci_view_factory.js').MCIViewFactory;
|
|
|
|
var menuUtil = require('./menu_util.js');
|
|
|
|
var asset = require('./asset.js');
|
|
|
|
var ansi = require('./ansi_term.js');
|
2015-03-28 00:02:00 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
var events = require('events');
|
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
|
|
|
var async = require('async');
|
|
|
|
var _ = require('lodash');
|
|
|
|
var paths = require('path');
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.ViewController = ViewController;
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
var MCI_REGEXP = /([A-Z]{2})([0-9]{1,2})/;
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2015-04-14 06:19:14 +00:00
|
|
|
function ViewController(options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(_.isObject(options));
|
|
|
|
assert(_.isObject(options.client));
|
|
|
|
|
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
var self = this;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client = options.client;
|
|
|
|
this.views = {}; // map of ID -> view
|
|
|
|
this.formId = options.formId || 0;
|
|
|
|
this.mciViewFactory = new MCIViewFactory(this.client); // :TODO: can this not be a singleton?
|
|
|
|
this.noInput = _.isBoolean(options.noInput) ? options.noInput : false;
|
|
|
|
this.actionKeyMap = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Small wrapper/proxy around handleAction() to ensure we do not allow
|
|
|
|
// input/additional actions queued while performing an action
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
this.handleActionWrapper = function (formData, actionBlock, cb) {
|
|
|
|
if (self.waitActionCompletion) {
|
|
|
|
if (cb) {
|
2018-07-05 00:46:40 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
2018-06-23 03:26:46 +00:00
|
|
|
return; // ignore until this is finished!
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.log.trace({ actionBlock }, 'Action match');
|
2019-06-08 17:57:36 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
self.waitActionCompletion = true;
|
2022-06-05 20:04:25 +00:00
|
|
|
menuUtil.handleAction(self.client, formData, actionBlock, err => {
|
|
|
|
if (err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: What can we really do here?
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('ALREADYTHERE' === err.reasonCode) {
|
|
|
|
self.client.log.trace(err.reason);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.log.warn({ err: err }, 'Error during handleAction()');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.waitActionCompletion = false;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-07-05 00:46:40 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.clientKeyPressHandler = function (ch, key) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Process key presses treating form submit mapped keys special.
|
|
|
|
// Everything else is forwarded on to the focused View, if any.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
var actionForKey = key ? self.actionKeyMap[key.name] : self.actionKeyMap[ch];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (actionForKey) {
|
|
|
|
if (_.isNumber(actionForKey.viewId)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Key works on behalf of a view -- switch focus & submit
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
self.switchFocus(actionForKey.viewId);
|
|
|
|
self.submitForm(key);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (_.isString(actionForKey.action)) {
|
|
|
|
const formData = self.getFocusedView() ? self.getFormData() : {};
|
2018-06-22 05:15:04 +00:00
|
|
|
self.handleActionWrapper(
|
2022-06-05 20:04:25 +00:00
|
|
|
Object.assign({ ch: ch, key: key }, formData), // formData + key info
|
|
|
|
actionForKey
|
|
|
|
); // actionBlock
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.focusedView && self.focusedView.acceptsInput) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.focusedView.onKeyPress(ch, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.viewActionListener = function (action, key) {
|
|
|
|
switch (action) {
|
|
|
|
case 'next':
|
|
|
|
self.emit('action', { view: this, action: action, key: key });
|
2018-06-22 05:15:04 +00:00
|
|
|
self.nextFocus();
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'accept':
|
|
|
|
if (self.focusedView && self.focusedView.submit) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: need to do validation here!!!
|
2018-06-22 05:15:04 +00:00
|
|
|
var focusedView = self.focusedView;
|
2022-06-05 20:04:25 +00:00
|
|
|
self.validateView(
|
|
|
|
focusedView,
|
|
|
|
function validated(err, newFocusedViewId) {
|
|
|
|
if (err) {
|
|
|
|
var newFocusedView =
|
|
|
|
self.getView(newFocusedViewId) || focusedView;
|
|
|
|
self.setViewFocusWithEvents(newFocusedView, true);
|
|
|
|
} else {
|
|
|
|
self.submitForm(key);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
//self.submitForm(key);
|
|
|
|
} else {
|
|
|
|
self.nextFocus();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.submitForm = function (key) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.emit('submit', this.getFormData(key));
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getLogFriendlyFormData = function (formData) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var safeFormData = _.cloneDeep(formData);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (safeFormData.value.password) {
|
2018-06-22 05:15:04 +00:00
|
|
|
safeFormData.value.password = '*****';
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (safeFormData.value.passwordConfirm) {
|
2018-06-22 05:15:04 +00:00
|
|
|
safeFormData.value.passwordConfirm = '*****';
|
|
|
|
}
|
|
|
|
return safeFormData;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.switchFocusEvent = function (event, view) {
|
|
|
|
if (self.emitSwitchFocus) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitSwitchFocus = true;
|
|
|
|
self.emit(event, view);
|
|
|
|
self.emitSwitchFocus = false;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.createViewsFromMCI = function (mciMap, cb) {
|
2022-08-16 04:23:14 +00:00
|
|
|
const views = [];
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each(
|
|
|
|
Object.keys(mciMap),
|
|
|
|
(name, nextItem) => {
|
|
|
|
const mci = mciMap[name];
|
|
|
|
const view = self.mciViewFactory.createFromMCI(mci);
|
|
|
|
|
|
|
|
if (view) {
|
|
|
|
if (false === self.noInput) {
|
|
|
|
view.on('action', self.viewActionListener);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-08-16 04:23:14 +00:00
|
|
|
views.push(view);
|
2022-06-05 20:04:25 +00:00
|
|
|
self.addView(view);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return nextItem(null);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
self.setViewOrder();
|
2022-08-16 04:23:14 +00:00
|
|
|
return cb(err, views);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: move this elsewhere
|
2022-06-05 20:04:25 +00:00
|
|
|
this.setViewPropertiesFromMCIConf = function (view, conf) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var propAsset;
|
|
|
|
var propValue;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
for (var propName in conf) {
|
2018-06-22 05:15:04 +00:00
|
|
|
propAsset = asset.getViewPropertyAsset(conf[propName]);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (propAsset) {
|
|
|
|
switch (propAsset.type) {
|
|
|
|
case 'config':
|
2018-06-22 05:15:04 +00:00
|
|
|
propValue = asset.resolveConfigAsset(conf[propName]);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'sysStat':
|
2018-06-22 05:15:04 +00:00
|
|
|
propValue = asset.resolveSystemStatAsset(conf[propName]);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// :TODO: handle @art (e.g. text : @art ...)
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'method':
|
|
|
|
case 'systemMethod':
|
|
|
|
if ('validate' === propName) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: handle propAsset.location for @method script specification
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('systemMethod' === propAsset.type) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: implementation validation @systemMethod handling!
|
2022-06-05 20:04:25 +00:00
|
|
|
var methodModule = require(paths.join(
|
|
|
|
__dirname,
|
|
|
|
'system_view_validate.js'
|
|
|
|
));
|
|
|
|
if (_.isFunction(methodModule[propAsset.asset])) {
|
2018-06-22 05:15:04 +00:00
|
|
|
propValue = methodModule[propAsset.asset];
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
_.isFunction(
|
|
|
|
self.client.currentMenuModule.menuMethods[
|
|
|
|
propAsset.asset
|
|
|
|
]
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
propValue =
|
|
|
|
self.client.currentMenuModule.menuMethods[
|
|
|
|
propAsset.asset
|
|
|
|
];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(propAsset.location)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
// :TODO: clean this code up!
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('systemMethod' === propAsset.type) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO:
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// local to current module
|
2018-06-22 05:15:04 +00:00
|
|
|
var currentModule = self.client.currentMenuModule;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
_.isFunction(
|
|
|
|
currentModule.menuMethods[propAsset.asset]
|
|
|
|
)
|
|
|
|
) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Fix formData & extraArgs... this all needs general processing
|
2022-06-05 20:04:25 +00:00
|
|
|
propValue = currentModule.menuMethods[
|
|
|
|
propAsset.asset
|
|
|
|
]({}, {}); //formData, conf.extraArgs);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
2019-11-09 02:20:14 +00:00
|
|
|
propValue = conf[propName];
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
propValue = conf[propName];
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isUndefined(propValue)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
view.setPropertyValue(propName, propValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
this.applyViewConfig = function (config, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
let highestId = 1;
|
|
|
|
let submitId;
|
|
|
|
let initialFocusId = 1;
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
async.each(
|
|
|
|
Object.keys(config.mci || {}),
|
|
|
|
function entry(mci, nextItem) {
|
|
|
|
const mciMatch = mci.match(MCI_REGEXP); // :TODO: How to handle auto-generated IDs????
|
|
|
|
if (null === mciMatch) {
|
|
|
|
self.client.log.warn({ mci: mci }, 'Unable to parse MCI code');
|
|
|
|
return;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
const viewId = parseInt(mciMatch[2]);
|
|
|
|
assert(!isNaN(viewId), 'Cannot parse view ID: ' + mciMatch[2]); // shouldn't be possible with RegExp used
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
if (viewId > highestId) {
|
|
|
|
highestId = viewId;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
const view = self.getView(viewId);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
if (!view) {
|
|
|
|
return nextItem(null);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
const mciConf = config.mci[mci];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
self.setViewPropertiesFromMCIConf(view, mciConf);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
if (mciConf.focus) {
|
|
|
|
initialFocusId = viewId;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
if (true === view.submit) {
|
|
|
|
submitId = viewId;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
nextItem(null);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
// default to highest ID if no 'submit' entry present
|
|
|
|
if (!submitId) {
|
|
|
|
const highestIdView = self.getView(highestId);
|
|
|
|
if (highestIdView) {
|
|
|
|
highestIdView.submit = true;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-12 20:11:36 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
return cb(err, { initialFocusId: initialFocusId });
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// method for comparing submitted form data to configuration entries
|
2022-06-05 20:04:25 +00:00
|
|
|
this.actionBlockValueComparator = function (formValue, actionValue) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// For a match to occur, one of the following must be true:
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// * actionValue is a Object:
|
|
|
|
// a) All key/values must exactly match
|
|
|
|
// b) value is null; The key (view ID or "argName") must be present
|
|
|
|
// in formValue. This is a wildcard/any match.
|
|
|
|
// * actionValue is a Number: This represents a view ID that
|
|
|
|
// must be present in formValue.
|
|
|
|
// * actionValue is a string: This represents a view with
|
|
|
|
// "argName" set that must be present in formValue.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isUndefined(actionValue)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isNumber(actionValue) || _.isString(actionValue)) {
|
|
|
|
if (_.isUndefined(formValue[actionValue])) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
:TODO: support:
|
|
|
|
value: {
|
|
|
|
someArgName: [ "key1", "key2", ... ],
|
|
|
|
someOtherArg: [ "key1, ... ]
|
|
|
|
}
|
|
|
|
*/
|
2018-06-22 05:15:04 +00:00
|
|
|
var actionValueKeys = Object.keys(actionValue);
|
2022-06-05 20:04:25 +00:00
|
|
|
for (var i = 0; i < actionValueKeys.length; ++i) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var viewId = actionValueKeys[i];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.has(formValue, viewId)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
null !== actionValue[viewId] &&
|
|
|
|
actionValue[viewId] !== formValue[viewId]
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!options.detached) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.attachClientEvents();
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.setViewFocusWithEvents = function (view, focused) {
|
|
|
|
if (!view || !view.acceptsFocus) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (focused) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.switchFocusEvent('return', view);
|
|
|
|
self.focusedView = view;
|
|
|
|
} else {
|
|
|
|
self.switchFocusEvent('leave', view);
|
|
|
|
}
|
|
|
|
|
|
|
|
view.setFocus(focused);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.validateView = function (view, cb) {
|
|
|
|
if (view && _.isFunction(view.validate)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
view.validate(view.getData(), function validateResult(err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
var viewValidationListener =
|
|
|
|
self.client.currentMenuModule.menuMethods.viewValidationListener;
|
|
|
|
if (_.isFunction(viewValidationListener)) {
|
|
|
|
if (err) {
|
|
|
|
err.view = view; // pass along the view that failed
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
viewValidationListener(
|
|
|
|
err,
|
|
|
|
function validationComplete(newViewFocusId) {
|
|
|
|
cb(err, newViewFocusId);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
};
|
2014-10-23 05:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(ViewController, events.EventEmitter);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.attachClientEvents = function () {
|
|
|
|
if (this.attached) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
var self = this;
|
2015-07-12 02:12:07 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.on('key press', this.clientKeyPressHandler);
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
Object.keys(this.views).forEach(function vid(i) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// remove, then add to ensure we only have one listener
|
2018-06-22 05:15:04 +00:00
|
|
|
self.views[i].removeListener('action', self.viewActionListener);
|
|
|
|
self.views[i].on('action', self.viewActionListener);
|
|
|
|
});
|
2015-07-12 02:12:07 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.attached = true;
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.detachClientEvents = function () {
|
|
|
|
if (!this.attached) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.removeListener('key press', this.clientKeyPressHandler);
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
for (var id in this.views) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.views[id].removeAllListeners();
|
|
|
|
}
|
2014-11-04 07:34:54 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.attached = false;
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.viewExists = function (id) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return id in this.views;
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.addView = function (view) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(!this.viewExists(view.id), 'View with ID ' + view.id + ' already exists');
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.views[view.id] = view;
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.getView = function (id) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.views[id];
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.hasView = function (id) {
|
2018-11-15 03:51:11 +00:00
|
|
|
return this.getView(id) ? true : false;
|
2018-12-09 09:35:34 +00:00
|
|
|
};
|
2018-11-15 03:51:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.getViewsByMciCode = function (mciCode) {
|
|
|
|
if (!Array.isArray(mciCode)) {
|
|
|
|
mciCode = [mciCode];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const views = [];
|
|
|
|
_.each(this.views, v => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (mciCode.includes(v.mciCode)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
views.push(v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return views;
|
2018-02-01 05:38:02 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.getFocusedView = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.focusedView;
|
2015-04-12 05:48:41 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.setFocus = function (focused) {
|
|
|
|
if (focused) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.attachClientEvents();
|
|
|
|
} else {
|
|
|
|
this.detachClientEvents();
|
|
|
|
}
|
2015-09-20 04:55:09 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.setViewFocusWithEvents(this.focusedView, focused);
|
2015-07-04 18:02:37 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.resetInitialFocus = function () {
|
|
|
|
if (this.formInitialFocusId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.switchFocus(this.formInitialFocusId);
|
|
|
|
}
|
2017-02-02 02:42:27 +00:00
|
|
|
};
|
2016-10-25 03:49:45 +00:00
|
|
|
|
2022-08-16 04:23:14 +00:00
|
|
|
ViewController.prototype.applyViewOffsets = function (
|
|
|
|
views,
|
|
|
|
offsetCol,
|
|
|
|
offsetRow,
|
|
|
|
force = false
|
|
|
|
) {
|
|
|
|
if (!Array.isArray(views)) {
|
|
|
|
views = [views];
|
|
|
|
}
|
|
|
|
|
|
|
|
views.forEach(view => {
|
|
|
|
if (force || !view.offsetsApplied) {
|
|
|
|
view.offsetsApplied = true;
|
|
|
|
view.setPosition({
|
|
|
|
col: view.position.col + offsetCol,
|
|
|
|
row: view.position.row + offsetRow,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.switchFocus = function (id) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Perform focus switching validation now
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
var self = this;
|
2018-06-23 03:26:46 +00:00
|
|
|
var focusedView = self.focusedView;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.validateView(focusedView, function validated(err, newFocusedViewId) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var newFocusedView = self.getView(newFocusedViewId) || focusedView;
|
|
|
|
self.setViewFocusWithEvents(newFocusedView, true);
|
|
|
|
} else {
|
|
|
|
self.attachClientEvents();
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// remove from old
|
2018-06-22 05:15:04 +00:00
|
|
|
self.setViewFocusWithEvents(focusedView, false);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// set to new
|
2018-06-22 05:15:04 +00:00
|
|
|
self.setViewFocusWithEvents(self.getView(id), true);
|
|
|
|
}
|
|
|
|
});
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.nextFocus = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
let nextFocusView = this.focusedView ? this.focusedView : this.views[this.firstId];
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// find the next view that accepts focus
|
2022-06-05 20:04:25 +00:00
|
|
|
while (nextFocusView && nextFocusView.nextId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
nextFocusView = this.getView(nextFocusView.nextId);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!nextFocusView || nextFocusView.acceptsFocus) {
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (nextFocusView && this.focusedView !== nextFocusView) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.switchFocus(nextFocusView.id);
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.setViewOrder = function (order) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var viewIdOrder = order || [];
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === viewIdOrder.length) {
|
|
|
|
for (var id in this.views) {
|
|
|
|
if (this.views[id].acceptsFocus) {
|
2018-06-22 05:15:04 +00:00
|
|
|
viewIdOrder.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
viewIdOrder.sort(function intSort(a, b) {
|
|
|
|
return a - b;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (viewIdOrder.length > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var count = viewIdOrder.length - 1;
|
2022-06-05 20:04:25 +00:00
|
|
|
for (var i = 0; i < count; ++i) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.views[viewIdOrder[i]].nextId = viewIdOrder[i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.firstId = viewIdOrder[0];
|
2022-06-05 20:04:25 +00:00
|
|
|
var lastId =
|
|
|
|
viewIdOrder.length > 1 ? viewIdOrder[viewIdOrder.length - 1] : this.firstId;
|
2018-06-22 05:15:04 +00:00
|
|
|
this.views[lastId].nextId = this.firstId;
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.redrawAll = function (initialFocusId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.term.rawWrite(ansi.hideCursor());
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
for (var id in this.views) {
|
|
|
|
if (initialFocusId === id) {
|
|
|
|
continue; // will draw @ focus
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
this.views[id].redraw();
|
|
|
|
}
|
2015-06-26 04:34:33 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.term.rawWrite(ansi.showCursor());
|
2015-06-26 04:34:33 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.loadFromPromptConfig = function (options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(_.isObject(options));
|
|
|
|
assert(_.isObject(options.mciMap));
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
var self = this;
|
|
|
|
var promptConfig = _.isObject(options.config)
|
|
|
|
? options.config
|
|
|
|
: self.client.currentMenuModule.menuConfig.promptConfig;
|
|
|
|
var initialFocusId = 1; // default to first
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function createViewsFromMCI(callback) {
|
|
|
|
self.createViewsFromMCI(options.mciMap, function viewsCreated(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function applyViewConfiguration(callback) {
|
2022-08-13 01:50:41 +00:00
|
|
|
if (promptConfig && _.isObject(promptConfig.mci)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.applyViewConfig(promptConfig, function configApplied(err, info) {
|
|
|
|
initialFocusId = info.initialFocusId;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function prepareFormSubmission(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (false === self.noInput) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.on('submit', function promptSubmit(formData) {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.log.trace({ formData }, 'Prompt submit');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-07-05 00:46:40 +00:00
|
|
|
const doSubmitNotify = () => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.submitNotify) {
|
2018-07-05 00:46:40 +00:00
|
|
|
options.submitNotify();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleIt = (fd, conf) => {
|
|
|
|
self.handleActionWrapper(fd, conf, () => {
|
|
|
|
doSubmitNotify();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(self.client.currentMenuModule.menuConfig.action)) {
|
2018-07-05 00:46:40 +00:00
|
|
|
handleIt(formData, self.client.currentMenuModule.menuConfig);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
//
|
2018-07-05 00:46:40 +00:00
|
|
|
// Menus that reference prompts can have a special "submit" block without the
|
2018-06-23 03:26:46 +00:00
|
|
|
// hassle of by-form-id configurations, etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// "submit" : [
|
|
|
|
// { ... }
|
|
|
|
// ]
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-07-05 00:46:40 +00:00
|
|
|
const menuConfig = self.client.currentMenuModule.menuConfig;
|
|
|
|
let submitConf;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Array.isArray(menuConfig.submit)) {
|
|
|
|
// standalone prompts)) {
|
2018-07-05 00:46:40 +00:00
|
|
|
submitConf = menuConfig.submit;
|
|
|
|
} else {
|
|
|
|
// look for embedded prompt configurations - using their own form ID within the menu
|
|
|
|
submitConf =
|
2022-06-05 20:04:25 +00:00
|
|
|
_.get(menuConfig, [
|
|
|
|
'form',
|
|
|
|
formData.id,
|
|
|
|
'submit',
|
|
|
|
formData.submitId,
|
|
|
|
]) ||
|
|
|
|
_.get(menuConfig, [
|
|
|
|
'form',
|
|
|
|
formData.id,
|
|
|
|
'submit',
|
|
|
|
'*',
|
|
|
|
]);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!Array.isArray(submitConf)) {
|
2018-07-05 00:46:40 +00:00
|
|
|
doSubmitNotify();
|
2022-06-05 20:04:25 +00:00
|
|
|
return self.client.log.debug(
|
|
|
|
'No configuration to handle submit'
|
|
|
|
);
|
2018-07-05 00:46:40 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-07-05 00:46:40 +00:00
|
|
|
// locate any matching action block
|
2022-06-05 20:04:25 +00:00
|
|
|
const actionBlock = submitConf.find(actionBlock =>
|
|
|
|
_.isEqualWith(
|
|
|
|
formData.value,
|
|
|
|
actionBlock.value,
|
|
|
|
self.actionBlockValueComparator
|
|
|
|
)
|
|
|
|
);
|
|
|
|
if (actionBlock) {
|
2018-07-05 00:46:40 +00:00
|
|
|
handleIt(formData, actionBlock);
|
|
|
|
} else {
|
|
|
|
doSubmitNotify();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function loadActionKeys(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isObject(promptConfig) || !_.isArray(promptConfig.actionKeys)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
promptConfig.actionKeys.forEach(ak => {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// * 'keys' must be present and be an array of key names
|
|
|
|
// * If 'viewId' is present, key(s) will focus & submit on behalf
|
|
|
|
// of the specified view.
|
|
|
|
// * If 'action' is present, that action will be procesed when
|
|
|
|
// triggered by key(s)
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Ultimately, create a map of key -> { action block }
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isArray(ak.keys)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ak.keys.forEach(kn => {
|
|
|
|
self.actionKeyMap[kn] = ak;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function drawAllViews(callback) {
|
|
|
|
self.redrawAll(initialFocusId);
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function setInitialViewFocus(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (initialFocusId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.switchFocus(initialFocusId);
|
|
|
|
}
|
|
|
|
callback(null);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
2015-04-19 08:13:13 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.loadFromMenuConfig = function (options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(_.isObject(options));
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isObject(options.mciMap)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb(new Error('Missing option: mciMap'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
var self = this;
|
|
|
|
var formIdKey = options.formId ? options.formId.toString() : '0';
|
|
|
|
this.formInitialFocusId = 1; // default to first
|
2018-06-22 05:15:04 +00:00
|
|
|
var formConfig;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: honor options.withoutForm
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function findMatchingFormConfig(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
menuUtil.getFormConfigByIDAndMap(
|
|
|
|
self.client.currentMenuModule.menuConfig,
|
|
|
|
formIdKey,
|
|
|
|
options.mciMap,
|
|
|
|
function matchingConfig(err, fc) {
|
|
|
|
formConfig = fc;
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
// non-fatal
|
|
|
|
self.client.log.trace(
|
|
|
|
{
|
|
|
|
reason: err.message,
|
|
|
|
mci: Object.keys(options.mciMap),
|
|
|
|
formId: formIdKey,
|
|
|
|
},
|
|
|
|
'Unable to find matching form configuration'
|
|
|
|
);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function createViews(callback) {
|
2022-08-16 04:23:14 +00:00
|
|
|
self.createViewsFromMCI(options.mciMap, (err, views) => {
|
|
|
|
if (!err && _.isObject(options.viewOffsets)) {
|
|
|
|
self.applyViewOffsets(
|
|
|
|
views,
|
|
|
|
options.viewOffsets.col,
|
|
|
|
options.viewOffsets.row
|
|
|
|
);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
function applyThemeCustomization(callback) {
|
|
|
|
formConfig = formConfig || {};
|
|
|
|
formConfig.mci = formConfig.mci || {};
|
|
|
|
//self.client.currentMenuModule.menuConfig.config = self.client.currentMenuModule.menuConfig.config || {};
|
|
|
|
|
|
|
|
//console.log('menu config.....');
|
|
|
|
//console.log(self.client.currentMenuModule.menuConfig)
|
|
|
|
|
|
|
|
menuUtil.applyMciThemeCustomization({
|
|
|
|
name : self.client.currentMenuModule.menuName,
|
|
|
|
type : 'menus',
|
|
|
|
client : self.client,
|
|
|
|
mci : formConfig.mci,
|
|
|
|
//config : self.client.currentMenuModule.menuConfig.config,
|
|
|
|
formId : formIdKey,
|
|
|
|
});
|
|
|
|
|
|
|
|
//console.log('after theme...')
|
|
|
|
//console.log(self.client.currentMenuModule.menuConfig.config)
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
2016-01-15 05:58:56 +00:00
|
|
|
*/
|
2018-06-22 05:15:04 +00:00
|
|
|
function applyViewConfiguration(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isObject(formConfig)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.applyViewConfig(formConfig, function configApplied(err, info) {
|
|
|
|
self.formInitialFocusId = info.initialFocusId;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function prepareFormSubmission(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isObject(formConfig) || !_.isObject(formConfig.submit)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
callback(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.on('submit', function formSubmit(formData) {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.log.trace({ formData }, 'Form submit');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Locate configuration for this form ID
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-07-05 00:46:40 +00:00
|
|
|
const confForFormId =
|
2022-06-05 20:04:25 +00:00
|
|
|
_.get(formConfig, ['submit', formData.submitId]) ||
|
|
|
|
_.get(formConfig, ['submit', '*']);
|
|
|
|
|
|
|
|
if (!Array.isArray(confForFormId)) {
|
|
|
|
return self.client.log.debug(
|
|
|
|
{ formId: formData.submitId },
|
|
|
|
'No configuration for form ID'
|
|
|
|
);
|
2018-07-05 00:46:40 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-07-05 00:46:40 +00:00
|
|
|
// locate a matching action block, if any
|
2022-06-05 20:04:25 +00:00
|
|
|
const actionBlock = confForFormId.find(actionBlock =>
|
|
|
|
_.isEqualWith(
|
|
|
|
formData.value,
|
|
|
|
actionBlock.value,
|
|
|
|
self.actionBlockValueComparator
|
|
|
|
)
|
|
|
|
);
|
|
|
|
if (actionBlock) {
|
2018-07-05 00:46:40 +00:00
|
|
|
self.handleActionWrapper(formData, actionBlock);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function loadActionKeys(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isObject(formConfig) || !_.isArray(formConfig.actionKeys)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
callback(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
formConfig.actionKeys.forEach(function akEntry(ak) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// * 'keys' must be present and be an array of key names
|
|
|
|
// * If 'viewId' is present, key(s) will focus & submit on behalf
|
|
|
|
// of the specified view.
|
|
|
|
// * If 'action' is present, that action will be procesed when
|
|
|
|
// triggered by key(s)
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Ultimately, create a map of key -> { action block }
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isArray(ak.keys)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ak.keys.forEach(function actionKeyName(kn) {
|
|
|
|
self.actionKeyMap[kn] = ak;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function drawAllViews(callback) {
|
|
|
|
self.redrawAll(self.formInitialFocusId);
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function setInitialViewFocus(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.formInitialFocusId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.switchFocus(self.formInitialFocusId);
|
|
|
|
}
|
|
|
|
callback(null);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
function complete(err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isFunction(cb)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-04-19 08:13:13 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.formatMCIString = function (format) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var self = this;
|
|
|
|
var view;
|
2015-04-04 20:41:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return format.replace(/{(\d+)}/g, function replacer(match, number) {
|
|
|
|
view = self.getView(number);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!view) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return match;
|
|
|
|
}
|
2015-04-04 20:41:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return view.getData();
|
|
|
|
});
|
2015-04-04 20:41:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ViewController.prototype.getFormData = function (key) {
|
2018-06-22 05:15:04 +00:00
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Example form data:
|
|
|
|
{
|
|
|
|
id : 0,
|
|
|
|
submitId : 1,
|
|
|
|
value : {
|
|
|
|
"1" : "hurp",
|
|
|
|
"2" : [ 'a', 'b', ... ],
|
|
|
|
"3" 2,
|
|
|
|
"pants" : "no way"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
2018-06-22 05:15:04 +00:00
|
|
|
const formData = {
|
2022-06-05 20:04:25 +00:00
|
|
|
id: this.formId,
|
|
|
|
submitId: this.focusedView.id,
|
|
|
|
value: {},
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (key) {
|
2018-06-22 05:15:04 +00:00
|
|
|
formData.key = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
let viewData;
|
|
|
|
_.each(this.views, view => {
|
|
|
|
try {
|
2018-06-23 03:26:46 +00:00
|
|
|
// don't fill forms with static, non user-editable data data
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!view.acceptsInput) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-08 17:57:36 +00:00
|
|
|
// some form values may be omitted from submission all together
|
2022-06-05 20:04:25 +00:00
|
|
|
if (view.omitFromSubmission) {
|
2019-06-08 17:57:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
viewData = view.getData();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isUndefined(viewData)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
formData.value[view.submitArgName ? view.submitArgName : view.id] = viewData;
|
|
|
|
} catch (e) {
|
|
|
|
this.client.log.error(
|
|
|
|
{ error: e.message },
|
|
|
|
'Exception caught gathering form data'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return formData;
|
2016-10-25 03:49:45 +00:00
|
|
|
};
|