2014-10-23 05:41:00 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.ViewController = ViewController;
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2018-06-23 03:26:46 +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);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
var self = this;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +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;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
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
|
|
|
//
|
|
|
|
this.handleActionWrapper = function(formData, actionBlock) {
|
|
|
|
if(self.waitActionCompletion) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return; // ignore until this is finished!
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.waitActionCompletion = true;
|
|
|
|
menuUtil.handleAction(self.client, formData, actionBlock, (err) => {
|
|
|
|
if(err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: What can we really do here?
|
2018-06-22 05:15:04 +00:00
|
|
|
if('ALREADYTHERE' === err.reasonCode) {
|
|
|
|
self.client.log.trace( err.reason );
|
|
|
|
} else {
|
|
|
|
self.client.log.warn( { err : err }, 'Error during handleAction()');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.waitActionCompletion = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.clientKeyPressHandler = function(ch, key) {
|
|
|
|
//
|
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];
|
|
|
|
if(actionForKey) {
|
|
|
|
if(_.isNumber(actionForKey.viewId)) {
|
|
|
|
//
|
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);
|
|
|
|
} else if(_.isString(actionForKey.action)) {
|
|
|
|
const formData = self.getFocusedView() ? self.getFormData() : { };
|
|
|
|
self.handleActionWrapper(
|
2018-06-23 03:26:46 +00:00
|
|
|
Object.assign( { ch : ch, key : key }, formData ), // formData + key info
|
|
|
|
actionForKey); // actionBlock
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(self.focusedView && self.focusedView.acceptsInput) {
|
|
|
|
self.focusedView.onKeyPress(ch, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.viewActionListener = function(action, key) {
|
|
|
|
switch(action) {
|
|
|
|
case 'next' :
|
|
|
|
self.emit('action', { view : this, action : action, key : key });
|
|
|
|
self.nextFocus();
|
|
|
|
break;
|
|
|
|
|
|
|
|
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;
|
|
|
|
self.validateView(focusedView, function validated(err, newFocusedViewId) {
|
|
|
|
if(err) {
|
|
|
|
var newFocusedView = self.getView(newFocusedViewId) || focusedView;
|
|
|
|
self.setViewFocusWithEvents(newFocusedView, true);
|
|
|
|
} else {
|
|
|
|
self.submitForm(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//self.submitForm(key);
|
|
|
|
} else {
|
|
|
|
self.nextFocus();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.submitForm = function(key) {
|
|
|
|
self.emit('submit', this.getFormData(key));
|
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: replace this in favor of overriding toJSON() for various things such that logging will *never* output them
|
2018-06-22 05:15:04 +00:00
|
|
|
this.getLogFriendlyFormData = function(formData) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: these fields should be part of menu.json sensitiveMembers[]
|
2018-06-22 05:15:04 +00:00
|
|
|
var safeFormData = _.cloneDeep(formData);
|
|
|
|
if(safeFormData.value.password) {
|
|
|
|
safeFormData.value.password = '*****';
|
|
|
|
}
|
|
|
|
if(safeFormData.value.passwordConfirm) {
|
|
|
|
safeFormData.value.passwordConfirm = '*****';
|
|
|
|
}
|
|
|
|
return safeFormData;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.switchFocusEvent = function(event, view) {
|
|
|
|
if(self.emitSwitchFocus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitSwitchFocus = true;
|
|
|
|
self.emit(event, view);
|
|
|
|
self.emitSwitchFocus = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.createViewsFromMCI = function(mciMap, cb) {
|
|
|
|
async.each(Object.keys(mciMap), (name, nextItem) => {
|
2018-06-23 03:26:46 +00:00
|
|
|
const mci = mciMap[name];
|
|
|
|
const view = self.mciViewFactory.createFromMCI(mci);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if(view) {
|
|
|
|
if(false === self.noInput) {
|
|
|
|
view.on('action', self.viewActionListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.addView(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextItem(null);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
self.setViewOrder();
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: move this elsewhere
|
2018-06-22 05:15:04 +00:00
|
|
|
this.setViewPropertiesFromMCIConf = function(view, conf) {
|
|
|
|
|
|
|
|
var propAsset;
|
|
|
|
var propValue;
|
|
|
|
|
|
|
|
for(var propName in conf) {
|
|
|
|
propAsset = asset.getViewPropertyAsset(conf[propName]);
|
|
|
|
if(propAsset) {
|
|
|
|
switch(propAsset.type) {
|
|
|
|
case 'config' :
|
|
|
|
propValue = asset.resolveConfigAsset(conf[propName]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'sysStat' :
|
|
|
|
propValue = asset.resolveSystemStatAsset(conf[propName]);
|
|
|
|
break;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: handle @art (e.g. text : @art ...)
|
2018-06-22 05:15:04 +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
|
2018-06-22 05:15:04 +00:00
|
|
|
if('systemMethod' === propAsset.type) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: implementation validation @systemMethod handling!
|
2018-06-22 05:15:04 +00:00
|
|
|
var methodModule = require(paths.join(__dirname, 'system_view_validate.js'));
|
|
|
|
if(_.isFunction(methodModule[propAsset.asset])) {
|
|
|
|
propValue = methodModule[propAsset.asset];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(_.isFunction(self.client.currentMenuModule.menuMethods[propAsset.asset])) {
|
|
|
|
propValue = self.client.currentMenuModule.menuMethods[propAsset.asset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(_.isString(propAsset.location)) {
|
|
|
|
// :TODO: clean this code up!
|
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
if(_.isFunction(currentModule.menuMethods[propAsset.asset])) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Fix formData & extraArgs... this all needs general processing
|
2018-06-22 05:15:04 +00:00
|
|
|
propValue = currentModule.menuMethods[propAsset.asset]({}, {});//formData, conf.extraArgs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
propValue = propValue = conf[propName];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
propValue = conf[propName];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.isUndefined(propValue)) {
|
|
|
|
view.setPropertyValue(propName, propValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.applyViewConfig = function(config, cb) {
|
|
|
|
let highestId = 1;
|
|
|
|
let submitId;
|
|
|
|
let initialFocusId = 1;
|
|
|
|
|
|
|
|
async.each(Object.keys(config.mci || {}), function entry(mci, nextItem) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const mciMatch = mci.match(MCI_REGEXP); // :TODO: How to handle auto-generated IDs????
|
2018-06-22 05:15:04 +00:00
|
|
|
if(null === mciMatch) {
|
|
|
|
self.client.log.warn( { mci : mci }, 'Unable to parse MCI code');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const viewId = parseInt(mciMatch[2]);
|
2018-06-23 03:26:46 +00:00
|
|
|
assert(!isNaN(viewId), 'Cannot parse view ID: ' + mciMatch[2]); // shouldn't be possible with RegExp used
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if(viewId > highestId) {
|
|
|
|
highestId = viewId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const view = self.getView(viewId);
|
|
|
|
|
|
|
|
if(!view) {
|
|
|
|
self.client.log.warn( { viewId : viewId }, 'Cannot find view');
|
|
|
|
nextItem(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mciConf = config.mci[mci];
|
|
|
|
|
|
|
|
self.setViewPropertiesFromMCIConf(view, mciConf);
|
|
|
|
|
|
|
|
if(mciConf.focus) {
|
|
|
|
initialFocusId = viewId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(true === view.submit) {
|
|
|
|
submitId = viewId;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextItem(null);
|
|
|
|
},
|
|
|
|
err => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// default to highest ID if no 'submit' entry present
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!submitId) {
|
|
|
|
var highestIdView = self.getView(highestId);
|
|
|
|
if(highestIdView) {
|
|
|
|
highestIdView.submit = true;
|
|
|
|
} else {
|
|
|
|
self.client.log.warn( { highestId : highestId }, 'View does not exist');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(err, { initialFocusId : initialFocusId } );
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// method for comparing submitted form data to configuration entries
|
2018-06-22 05:15:04 +00:00
|
|
|
this.actionBlockValueComparator = function(formValue, actionValue) {
|
|
|
|
//
|
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
|
|
|
//
|
|
|
|
if(_.isUndefined(actionValue)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.isNumber(actionValue) || _.isString(actionValue)) {
|
|
|
|
if(_.isUndefined(formValue[actionValue])) {
|
|
|
|
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);
|
|
|
|
for(var i = 0; i < actionValueKeys.length; ++i) {
|
|
|
|
var viewId = actionValueKeys[i];
|
|
|
|
if(!_.has(formValue, viewId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(null !== actionValue[viewId] && actionValue[viewId] !== formValue[viewId]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.client.log.trace(
|
|
|
|
{
|
2018-06-23 03:26:46 +00:00
|
|
|
formValue : formValue,
|
|
|
|
actionValue : actionValue
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
'Action match'
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!options.detached) {
|
|
|
|
this.attachClientEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setViewFocusWithEvents = function(view, focused) {
|
|
|
|
if(!view || !view.acceptsFocus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(focused) {
|
|
|
|
self.switchFocusEvent('return', view);
|
|
|
|
self.focusedView = view;
|
|
|
|
} else {
|
|
|
|
self.switchFocusEvent('leave', view);
|
|
|
|
}
|
|
|
|
|
|
|
|
view.setFocus(focused);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.validateView = function(view, cb) {
|
|
|
|
if(view && _.isFunction(view.validate)) {
|
|
|
|
view.validate(view.getData(), function validateResult(err) {
|
|
|
|
var viewValidationListener = self.client.currentMenuModule.menuMethods.viewValidationListener;
|
|
|
|
if(_.isFunction(viewValidationListener)) {
|
|
|
|
if(err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
err.view = view; // pass along the view that failed
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
viewValidationListener(err, function validationComplete(newViewFocusId) {
|
|
|
|
cb(err, newViewFocusId);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
};
|
2014-10-23 05:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(ViewController, events.EventEmitter);
|
|
|
|
|
|
|
|
ViewController.prototype.attachClientEvents = function() {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(this.attached) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
ViewController.prototype.detachClientEvents = function() {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!this.attached) {
|
|
|
|
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
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
for(var id in this.views) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-02-01 05:38:02 +00:00
|
|
|
ViewController.prototype.getViewsByMciCode = function(mciCode) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!Array.isArray(mciCode)) {
|
|
|
|
mciCode = [ mciCode ];
|
|
|
|
}
|
|
|
|
|
|
|
|
const views = [];
|
|
|
|
_.each(this.views, v => {
|
|
|
|
if(mciCode.includes(v.mciCode)) {
|
|
|
|
views.push(v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return views;
|
2018-02-01 05:38:02 +00:00
|
|
|
};
|
|
|
|
|
2015-04-12 05:48:41 +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
|
|
|
};
|
|
|
|
|
2015-07-09 04:34:40 +00:00
|
|
|
ViewController.prototype.setFocus = function(focused) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(focused) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-10-25 03:49:45 +00:00
|
|
|
ViewController.prototype.resetInitialFocus = function() {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(this.formInitialFocusId) {
|
|
|
|
return this.switchFocus(this.formInitialFocusId);
|
|
|
|
}
|
2017-02-02 02:42:27 +00:00
|
|
|
};
|
2016-10-25 03:49:45 +00:00
|
|
|
|
2014-10-23 05:41:00 +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
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
var self = this;
|
|
|
|
var focusedView = self.focusedView;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.validateView(focusedView, function validated(err, newFocusedViewId) {
|
|
|
|
if(err) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-12-10 07:04:38 +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
|
2018-06-22 05:15:04 +00:00
|
|
|
while(nextFocusView && nextFocusView.nextId) {
|
|
|
|
nextFocusView = this.getView(nextFocusView.nextId);
|
|
|
|
if(!nextFocusView || nextFocusView.acceptsFocus) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nextFocusView && this.focusedView !== nextFocusView) {
|
|
|
|
this.switchFocus(nextFocusView.id);
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ViewController.prototype.setViewOrder = function(order) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var viewIdOrder = order || [];
|
|
|
|
|
|
|
|
if(0 === viewIdOrder.length) {
|
|
|
|
for(var id in this.views) {
|
|
|
|
if(this.views[id].acceptsFocus) {
|
|
|
|
viewIdOrder.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
viewIdOrder.sort(function intSort(a, b) {
|
|
|
|
return a - b;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if(viewIdOrder.length > 0) {
|
|
|
|
var count = viewIdOrder.length - 1;
|
|
|
|
for(var i = 0; i < count; ++i) {
|
|
|
|
this.views[viewIdOrder[i]].nextId = viewIdOrder[i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.firstId = viewIdOrder[0];
|
|
|
|
var lastId = viewIdOrder.length > 1 ? viewIdOrder[viewIdOrder.length - 1] : this.firstId;
|
|
|
|
this.views[lastId].nextId = this.firstId;
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2015-06-26 04:34:33 +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
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
for(var id in this.views) {
|
|
|
|
if(initialFocusId === id) {
|
2018-06-23 03:26:46 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
ViewController.prototype.loadFromPromptConfig = function(options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(_.isObject(options));
|
|
|
|
assert(_.isObject(options.mciMap));
|
|
|
|
|
2018-06-23 03:26:46 +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) {
|
|
|
|
if(_.isObject(promptConfig.mci)) {
|
|
|
|
self.applyViewConfig(promptConfig, function configApplied(err, info) {
|
|
|
|
initialFocusId = info.initialFocusId;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function prepareFormSubmission(callback) {
|
|
|
|
if(false === self.noInput) {
|
|
|
|
|
|
|
|
self.on('submit', function promptSubmit(formData) {
|
|
|
|
self.client.log.trace( { formData : self.getLogFriendlyFormData(formData) }, 'Prompt submit');
|
|
|
|
|
|
|
|
if(_.isString(self.client.currentMenuModule.menuConfig.action)) {
|
|
|
|
self.handleActionWrapper(formData, self.client.currentMenuModule.menuConfig);
|
|
|
|
} else {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Menus that reference prompts can have a sepcial "submit" block without the
|
|
|
|
// 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
|
|
|
//
|
|
|
|
var menuSubmit = self.client.currentMenuModule.menuConfig.submit;
|
|
|
|
if(!_.isArray(menuSubmit)) {
|
|
|
|
self.client.log.debug('No configuration to handle submit');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Locate matching action block
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: this is basically the same as for menus -- DRY it up!
|
2018-06-22 05:15:04 +00:00
|
|
|
for(var c = 0; c < menuSubmit.length; ++c) {
|
|
|
|
var actionBlock = menuSubmit[c];
|
|
|
|
|
|
|
|
if(_.isEqualWith(formData.value, actionBlock.value, self.actionBlockValueComparator)) {
|
|
|
|
self.handleActionWrapper(formData, actionBlock);
|
2018-06-23 03:26:46 +00:00
|
|
|
break; // there an only be one...
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function loadActionKeys(callback) {
|
|
|
|
if(!_.isObject(promptConfig) || !_.isArray(promptConfig.actionKeys)) {
|
|
|
|
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
|
|
|
//
|
|
|
|
if(!_.isArray(ak.keys)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ak.keys.forEach(kn => {
|
|
|
|
self.actionKeyMap[kn] = ak;
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function drawAllViews(callback) {
|
|
|
|
self.redrawAll(initialFocusId);
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function setInitialViewFocus(callback) {
|
|
|
|
if(initialFocusId) {
|
|
|
|
self.switchFocus(initialFocusId);
|
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
2015-04-19 08:13:13 +00:00
|
|
|
};
|
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
ViewController.prototype.loadFromMenuConfig = function(options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(_.isObject(options));
|
|
|
|
|
|
|
|
if(!_.isObject(options.mciMap)) {
|
|
|
|
cb(new Error('Missing option: mciMap'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +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) {
|
|
|
|
menuUtil.getFormConfigByIDAndMap(self.client.currentMenuModule.menuConfig, formIdKey, options.mciMap, function matchingConfig(err, fc) {
|
|
|
|
formConfig = fc;
|
|
|
|
|
|
|
|
if(err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// non-fatal
|
2018-06-22 05:15:04 +00:00
|
|
|
self.client.log.trace(
|
|
|
|
{ reason : err.message, mci : Object.keys(options.mciMap), formId : formIdKey },
|
|
|
|
'Unable to find matching form configuration');
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function createViews(callback) {
|
|
|
|
self.createViewsFromMCI(options.mciMap, function viewsCreated(err) {
|
|
|
|
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) {
|
|
|
|
if(_.isObject(formConfig)) {
|
|
|
|
self.applyViewConfig(formConfig, function configApplied(err, info) {
|
|
|
|
self.formInitialFocusId = info.initialFocusId;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function prepareFormSubmission(callback) {
|
|
|
|
if(!_.isObject(formConfig) || !_.isObject(formConfig.submit)) {
|
|
|
|
callback(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.on('submit', function formSubmit(formData) {
|
|
|
|
|
|
|
|
self.client.log.trace( { formData : self.getLogFriendlyFormData(formData) }, 'Form submit');
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Locate configuration for this form ID
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
var confForFormId;
|
|
|
|
if(_.isObject(formConfig.submit[formData.submitId])) {
|
|
|
|
confForFormId = formConfig.submit[formData.submitId];
|
|
|
|
} else if(_.isObject(formConfig.submit['*'])) {
|
|
|
|
confForFormId = formConfig.submit['*'];
|
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// no configuration for this submitId
|
2018-06-22 05:15:04 +00:00
|
|
|
self.client.log.debug( { formId : formData.submitId }, 'No configuration for form ID');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Locate a matching action block based on the submitted data
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
for(var c = 0; c < confForFormId.length; ++c) {
|
|
|
|
var actionBlock = confForFormId[c];
|
|
|
|
|
|
|
|
if(_.isEqualWith(formData.value, actionBlock.value, self.actionBlockValueComparator)) {
|
|
|
|
self.handleActionWrapper(formData, actionBlock);
|
2018-06-23 03:26:46 +00:00
|
|
|
break; // there an only be one...
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function loadActionKeys(callback) {
|
|
|
|
if(!_.isObject(formConfig) || !_.isArray(formConfig.actionKeys)) {
|
|
|
|
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
|
|
|
//
|
|
|
|
if(!_.isArray(ak.keys)) {
|
|
|
|
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) {
|
|
|
|
if(self.formInitialFocusId) {
|
|
|
|
self.switchFocus(self.formInitialFocusId);
|
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
if(_.isFunction(cb)) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-04-19 08:13:13 +00:00
|
|
|
};
|
|
|
|
|
2015-04-04 20:41:04 +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
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!view) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-08-13 22:05:17 +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 = {
|
2018-06-23 03:26:46 +00:00
|
|
|
id : this.formId,
|
|
|
|
submitId : this.focusedView.id,
|
|
|
|
value : {},
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(key) {
|
|
|
|
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
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!view.acceptsInput) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
viewData = view.getData();
|
|
|
|
if(_.isUndefined(viewData)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
formData.value[ view.submitArgName ? view.submitArgName : view.id ] = viewData;
|
|
|
|
} catch(e) {
|
|
|
|
this.client.log.error( { error : e.message }, 'Exception caught gathering form data' );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return formData;
|
2016-10-25 03:49:45 +00:00
|
|
|
};
|