2014-10-23 05:41:00 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-03-30 03:47:48 +00:00
|
|
|
// ENiGMA½
|
2014-10-23 05:41:00 +00:00
|
|
|
var MCIViewFactory = require('./mci_view_factory.js').MCIViewFactory;
|
2015-03-28 00:02:00 +00:00
|
|
|
var menuUtil = require('./menu_util.js');
|
2015-07-04 22:03:44 +00:00
|
|
|
//var Log = require('./logger.js').log;
|
2015-04-19 08:13:13 +00:00
|
|
|
var Config = require('./config.js').config;
|
2015-04-05 07:15:04 +00:00
|
|
|
var asset = require('./asset.js');
|
2015-04-28 04:40:05 +00:00
|
|
|
var ansi = require('./ansi_term.js');
|
2015-03-28 00:02:00 +00:00
|
|
|
|
2015-03-30 03:47:48 +00:00
|
|
|
var events = require('events');
|
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
2015-03-28 00:02:00 +00:00
|
|
|
var async = require('async');
|
2015-04-02 04:13:29 +00:00
|
|
|
var _ = require('lodash');
|
2015-04-19 08:13:13 +00:00
|
|
|
var paths = require('path');
|
2014-10-23 05:41:00 +00:00
|
|
|
|
|
|
|
exports.ViewController = ViewController;
|
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
var MCI_REGEXP = /([A-Z]{2})([0-9]{1,2})/;
|
|
|
|
|
2015-04-14 06:19:14 +00:00
|
|
|
function ViewController(options) {
|
|
|
|
assert(_.isObject(options));
|
|
|
|
assert(_.isObject(options.client));
|
2015-04-21 04:50:58 +00:00
|
|
|
|
2015-04-14 06:19:14 +00:00
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
var self = this;
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
this.client = options.client;
|
|
|
|
this.views = {}; // map of ID -> view
|
|
|
|
this.formId = options.formId || 0;
|
2015-07-11 22:39:42 +00:00
|
|
|
this.mciViewFactory = new MCIViewFactory(this.client); // :TODO: can this not be a singleton?
|
2015-07-25 22:10:12 +00:00
|
|
|
this.noInput = _.isBoolean(options.noInput) ? options.noInput : false;
|
2015-07-10 05:23:37 +00:00
|
|
|
|
|
|
|
this.actionKeyMap = {};
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
this.clientKeyPressHandler = function(ch, key) {
|
|
|
|
//
|
2015-07-09 04:34:40 +00:00
|
|
|
// Process key presses treating form submit mapped keys special.
|
|
|
|
// Everything else is forwarded on to the focused View, if any.
|
2015-06-26 04:34:33 +00:00
|
|
|
//
|
2015-06-05 22:20:26 +00:00
|
|
|
if(key) {
|
2015-07-10 05:23:37 +00:00
|
|
|
var actionForKey = self.actionKeyMap[key.name];
|
|
|
|
if(actionForKey) {
|
|
|
|
if(_.isNumber(actionForKey)) {
|
|
|
|
//
|
|
|
|
// Key works on behalf of a view -- switch focus & submit
|
|
|
|
//
|
|
|
|
self.switchFocus(actionForKey);
|
|
|
|
self.submitForm();
|
|
|
|
} else if(_.isString(actionForKey)) {
|
|
|
|
// :TODO: Populate formData here?
|
|
|
|
// :TODO: Populate actionBlock here -- that is, the actionKey entry... or
|
|
|
|
// really we just need |extraArgs| to be present, if any
|
|
|
|
menuUtil.handleAction(self.client, { }, { action : actionForKey } );
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2015-06-05 04:29:14 +00:00
|
|
|
}
|
2015-06-05 22:20:26 +00:00
|
|
|
|
|
|
|
if(self.focusedView && self.focusedView.acceptsInput) {
|
|
|
|
self.focusedView.onKeyPress(ch, key);
|
|
|
|
}
|
2015-06-05 04:29:14 +00:00
|
|
|
};
|
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
this.viewActionListener = function(action) {
|
2014-10-23 05:41:00 +00:00
|
|
|
switch(action) {
|
|
|
|
case 'next' :
|
|
|
|
self.emit('action', { view : this, action : action });
|
|
|
|
self.nextFocus();
|
|
|
|
break;
|
|
|
|
|
2015-07-11 22:39:42 +00:00
|
|
|
case 'accept' :
|
2015-03-31 03:29:06 +00:00
|
|
|
if(self.focusedView && self.focusedView.submit) {
|
2014-11-02 19:07:17 +00:00
|
|
|
self.submitForm();
|
|
|
|
} else {
|
|
|
|
self.nextFocus();
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-02 19:07:17 +00:00
|
|
|
this.submitForm = function() {
|
2015-07-04 18:02:37 +00:00
|
|
|
self.emit('submit', this.getFormData());
|
2014-11-02 19:07:17 +00:00
|
|
|
};
|
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
this.getLogFriendlyFormData = function(formData) {
|
2015-04-27 02:46:16 +00:00
|
|
|
// :TODO: these fields should be part of menu.json sensitiveMembers[]
|
2015-04-20 04:58:18 +00:00
|
|
|
var safeFormData = _.cloneDeep(formData);
|
|
|
|
if(safeFormData.value.password) {
|
|
|
|
safeFormData.value.password = '*****';
|
|
|
|
}
|
2015-04-24 05:00:48 +00:00
|
|
|
if(safeFormData.value.passwordConfirm) {
|
|
|
|
safeFormData.value.passwordConfirm = '*****';
|
|
|
|
}
|
2015-04-20 04:58:18 +00:00
|
|
|
return safeFormData;
|
|
|
|
};
|
|
|
|
|
2015-04-14 06:19:14 +00:00
|
|
|
this.switchFocusEvent = function(event, view) {
|
|
|
|
if(self.emitSwitchFocus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitSwitchFocus = true;
|
|
|
|
self.emit(event, view);
|
|
|
|
self.emitSwitchFocus = false;
|
|
|
|
};
|
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
this.createViewsFromMCI = function(mciMap, cb) {
|
|
|
|
async.each(Object.keys(mciMap), function entry(name, nextItem) {
|
|
|
|
var mci = mciMap[name];
|
|
|
|
var view = self.mciViewFactory.createFromMCI(mci);
|
|
|
|
|
2015-07-25 22:10:12 +00:00
|
|
|
if(view && false === self.noInput) {
|
2015-04-20 04:58:18 +00:00
|
|
|
view.on('action', self.viewActionListener);
|
|
|
|
|
|
|
|
self.addView(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
nextItem(null);
|
|
|
|
},
|
|
|
|
function complete(err) {
|
|
|
|
self.setViewOrder();
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-04-28 04:40:05 +00:00
|
|
|
// :TODO: move this elsewhere
|
2015-04-20 04:58:18 +00:00
|
|
|
this.setViewPropertiesFromMCIConf = function(view, conf) {
|
2015-07-27 04:51:06 +00:00
|
|
|
|
|
|
|
var propAsset;
|
|
|
|
var propValue;
|
|
|
|
|
|
|
|
function callModuleMethod(path) {
|
|
|
|
if('' === paths.extname(path)) {
|
|
|
|
path += '.js';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var methodMod = require(path);
|
|
|
|
// :TODO: fix formData & extraArgs
|
|
|
|
return methodMod[propAsset.asset](self.client.currentMenuModule, {}, {} );
|
|
|
|
} catch(e) {
|
|
|
|
self.client.log.error( { error : e.toString(), methodName : propAsset.asset }, 'Failed to execute asset method');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 05:14:17 +00:00
|
|
|
for(var propName in conf) {
|
2015-07-27 04:51:06 +00:00
|
|
|
propAsset = asset.getViewPropertyAsset(conf[propName]);
|
2015-06-30 05:14:17 +00:00
|
|
|
if(propAsset) {
|
|
|
|
switch(propAsset.type) {
|
|
|
|
case 'config' :
|
2015-07-11 22:39:42 +00:00
|
|
|
propValue = asset.resolveConfigAsset(conf[propName]);
|
2015-06-30 05:14:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// :TODO: handle @art (e.g. text : @art ...)
|
|
|
|
|
2015-07-27 04:51:06 +00:00
|
|
|
case 'method' :
|
|
|
|
case 'systemMethod' :
|
|
|
|
if(_.isString(propAsset.location)) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if('systemMethod' === propAsset.type) {
|
|
|
|
// :TODO:
|
|
|
|
} else {
|
|
|
|
// local to current module
|
|
|
|
var currentModule = self.client.currentMenuModule;
|
|
|
|
if(_.isFunction(currentModule.menuMethods[propAsset.asset])) {
|
|
|
|
// :TODO: Fix formData & extraArgs... this all needs general processing
|
|
|
|
propValue = currentModule.menuMethods[propAsset.asset]({}, {});//formData, conf.extraArgs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/*case 'method' :
|
|
|
|
case 'systemMethod' :
|
|
|
|
if(_.isString(actionAsset.location)) {
|
|
|
|
callModuleMenuMethod(paths.join(Config.paths.mods, actionAsset.location));
|
|
|
|
} else {
|
|
|
|
if('systemMethod' === actionAsset.type) {
|
|
|
|
// :TODO: Need to pass optional args here -- conf.extraArgs and args between e.g. ()
|
|
|
|
// :TODO: Probably better as system_method.js
|
|
|
|
callModuleMenuMethod(paths.join(__dirname, 'system_menu_method.js'));
|
|
|
|
} else {
|
|
|
|
// local to current module
|
|
|
|
var currentModule = client.currentMenuModule;
|
|
|
|
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) {
|
|
|
|
currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
break;
|
|
|
|
|
2015-06-30 05:14:17 +00:00
|
|
|
default :
|
|
|
|
propValue = propValue = conf[propName];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
propValue = conf[propName];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.isUndefined(propValue)) {
|
|
|
|
view.setPropertyValue(propName, propValue);
|
|
|
|
}
|
2015-07-04 18:02:37 +00:00
|
|
|
}
|
2015-04-20 04:58:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.applyViewConfig = function(config, cb) {
|
|
|
|
var highestId = 1;
|
|
|
|
var submitId;
|
|
|
|
var initialFocusId = 1;
|
|
|
|
|
|
|
|
async.each(Object.keys(config.mci), function entry(mci, nextItem) {
|
|
|
|
var mciMatch = mci.match(MCI_REGEXP); // :TODO: How to handle auto-generated IDs????
|
|
|
|
|
|
|
|
var viewId = parseInt(mciMatch[2]);
|
|
|
|
assert(!isNaN(viewId));
|
|
|
|
|
2015-07-10 17:11:08 +00:00
|
|
|
if(viewId > highestId) {
|
|
|
|
highestId = viewId;
|
|
|
|
}
|
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
var view = self.getView(viewId);
|
2015-04-27 02:46:16 +00:00
|
|
|
|
|
|
|
if(!view) {
|
2015-07-04 22:03:44 +00:00
|
|
|
self.client.log.warn( { viewId : viewId }, 'Cannot find view');
|
2015-04-27 02:46:16 +00:00
|
|
|
nextItem(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
var mciConf = config.mci[mci];
|
|
|
|
|
|
|
|
self.setViewPropertiesFromMCIConf(view, mciConf);
|
|
|
|
|
|
|
|
if(mciConf.focus) {
|
|
|
|
initialFocusId = viewId;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextItem(null);
|
|
|
|
},
|
2015-07-10 17:11:08 +00:00
|
|
|
function complete(err) {
|
2015-04-20 04:58:18 +00:00
|
|
|
// default to highest ID if no 'submit' entry present
|
|
|
|
if(!submitId) {
|
|
|
|
self.getView(highestId).submit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(err, { initialFocusId : initialFocusId } );
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-07 05:08:10 +00:00
|
|
|
// method for comparing submitted form data to configuration entries
|
|
|
|
this.actionBlockValueComparator = function(formValue, actionValue) {
|
|
|
|
//
|
|
|
|
// For a match to occur, one of the following must be true:
|
|
|
|
//
|
|
|
|
// * 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.
|
|
|
|
//
|
|
|
|
if(_.isNumber(actionValue) || _.isString(actionValue)) {
|
|
|
|
if(_.isUndefined(formValue[actionValue])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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( { formValue : formValue, actionValue : actionValue }, 'Action match');
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2015-07-04 18:02:37 +00:00
|
|
|
if(!options.detached) {
|
|
|
|
this.attachClientEvents();
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(ViewController, events.EventEmitter);
|
|
|
|
|
|
|
|
ViewController.prototype.attachClientEvents = function() {
|
|
|
|
if(this.attached) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-12 02:12:07 +00:00
|
|
|
var self = this;
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
this.client.on('key press', this.clientKeyPressHandler);
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2015-07-12 02:12:07 +00:00
|
|
|
Object.keys(this.views).forEach(function vid(i) {
|
|
|
|
// remove, then add to ensure we only have one listener
|
|
|
|
self.views[i].removeListener('action', self.viewActionListener);
|
|
|
|
self.views[i].on('action', self.viewActionListener);
|
|
|
|
});
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
this.attached = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewController.prototype.detachClientEvents = function() {
|
|
|
|
if(!this.attached) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
this.client.removeListener('key press', this.clientKeyPressHandler);
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2014-11-04 07:34:54 +00:00
|
|
|
for(var id in this.views) {
|
|
|
|
this.views[id].removeAllListeners();
|
|
|
|
}
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
this.attached = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewController.prototype.viewExists = function(id) {
|
|
|
|
return id in this.views;
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewController.prototype.addView = function(view) {
|
|
|
|
assert(!this.viewExists(view.id), 'View with ID ' + view.id + ' already exists');
|
|
|
|
|
|
|
|
this.views[view.id] = view;
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewController.prototype.getView = function(id) {
|
|
|
|
return this.views[id];
|
|
|
|
};
|
|
|
|
|
2015-04-12 05:48:41 +00:00
|
|
|
ViewController.prototype.getFocusedView = function() {
|
|
|
|
return this.focusedView;
|
|
|
|
};
|
|
|
|
|
2015-07-09 04:34:40 +00:00
|
|
|
ViewController.prototype.setFocus = function(focused) {
|
|
|
|
if(focused) {
|
|
|
|
this.attachClientEvents();
|
|
|
|
} else {
|
|
|
|
this.detachClientEvents();
|
2015-07-04 18:02:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
ViewController.prototype.switchFocus = function(id) {
|
2015-07-09 04:34:40 +00:00
|
|
|
this.setFocus(true); // ensure events are attached
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
if(this.focusedView && this.focusedView.acceptsFocus) {
|
2015-04-14 06:19:14 +00:00
|
|
|
this.switchFocusEvent('leave', this.focusedView);
|
2014-10-23 05:41:00 +00:00
|
|
|
this.focusedView.setFocus(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
var view = this.getView(id);
|
|
|
|
if(view && view.acceptsFocus) {
|
2015-06-05 04:29:14 +00:00
|
|
|
this.switchFocusEvent('return', view);
|
2015-04-14 06:19:14 +00:00
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
this.focusedView = view;
|
|
|
|
this.focusedView.setFocus(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-07 05:08:10 +00:00
|
|
|
ViewController.prototype.nextFocus = function() {
|
2014-10-23 05:41:00 +00:00
|
|
|
if(!this.focusedView) {
|
|
|
|
this.switchFocus(this.views[this.firstId].id);
|
|
|
|
} else {
|
|
|
|
var nextId = this.views[this.focusedView.id].nextId;
|
|
|
|
this.switchFocus(nextId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewController.prototype.setViewOrder = function(order) {
|
|
|
|
var viewIdOrder = order || [];
|
|
|
|
|
|
|
|
if(0 === viewIdOrder.length) {
|
|
|
|
for(var id in this.views) {
|
2014-10-28 03:58:34 +00:00
|
|
|
if(this.views[id].acceptsFocus) {
|
|
|
|
viewIdOrder.push(id);
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 03:58:34 +00:00
|
|
|
viewIdOrder.sort(function intSort(a, b) {
|
|
|
|
return a - b;
|
|
|
|
});
|
2014-10-23 05:41:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:25:11 +00:00
|
|
|
if(viewIdOrder.length > 0) {
|
|
|
|
var view;
|
|
|
|
var count = viewIdOrder.length - 1;
|
|
|
|
for(var i = 0; i < count; ++i) {
|
|
|
|
this.views[viewIdOrder[i]].nextId = viewIdOrder[i + 1];
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2014-10-31 22:25:11 +00:00
|
|
|
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) {
|
2015-07-06 01:05:55 +00:00
|
|
|
this.client.term.rawWrite(ansi.hideCursor());
|
2015-06-26 04:34:33 +00:00
|
|
|
|
|
|
|
for(var id in this.views) {
|
|
|
|
if(initialFocusId === id) {
|
|
|
|
continue; // will draw @ focus
|
|
|
|
}
|
|
|
|
this.views[id].redraw();
|
|
|
|
}
|
|
|
|
|
2015-07-06 01:05:55 +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) {
|
2015-04-19 08:13:13 +00:00
|
|
|
assert(_.isObject(options));
|
|
|
|
assert(_.isObject(options.mciMap));
|
2015-04-21 04:50:58 +00:00
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
var self = this;
|
2015-07-25 22:10:12 +00:00
|
|
|
var promptName = _.isString(options.promptName) ? options.promptName : self.client.currentMenuModule.menuConfig.prompt;
|
|
|
|
var promptConfig = _.isObject(options.config) ? options.config : self.client.currentMenuModule.menuConfig.promptConfig;
|
2015-04-19 08:13:13 +00:00
|
|
|
var initialFocusId = 1; // default to first
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function createViewsFromMCI(callback) {
|
2015-04-20 04:58:18 +00:00
|
|
|
self.createViewsFromMCI(options.mciMap, function viewsCreated(err) {
|
2015-04-19 08:13:13 +00:00
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
2015-05-14 20:56:02 +00:00
|
|
|
function applyThemeCustomization(callback) {
|
|
|
|
if(_.isObject(promptConfig)) {
|
|
|
|
menuUtil.applyThemeCustomization({
|
2015-07-25 22:10:12 +00:00
|
|
|
name : promptName,//self.client.currentMenuModule.menuConfig.prompt,
|
2015-05-14 20:56:02 +00:00
|
|
|
type : "prompts",
|
|
|
|
client : self.client,
|
|
|
|
configMci : promptConfig.mci,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
},
|
2015-04-20 04:58:18 +00:00
|
|
|
function applyViewConfiguration(callback) {
|
2015-05-01 04:29:24 +00:00
|
|
|
if(_.isObject(promptConfig.mci)) {
|
|
|
|
self.applyViewConfig(promptConfig, function configApplied(err, info) {
|
|
|
|
initialFocusId = info.initialFocusId;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
2015-04-19 08:13:13 +00:00
|
|
|
},
|
2015-08-07 05:08:10 +00:00
|
|
|
function prepareFormSubmission(callback) {
|
2015-07-25 22:10:12 +00:00
|
|
|
if(false === self.noInput) {
|
|
|
|
self.on('submit', function promptSubmit(formData) {
|
|
|
|
self.client.log.trace( { formData : self.getLogFriendlyFormData(formData) }, 'Prompt submit');
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2015-08-07 05:08:10 +00:00
|
|
|
if(_.isString(self.client.currentMenuModule.menuConfig.action)) {
|
|
|
|
menuUtil.handleAction(self.client, formData, self.client.currentMenuModule.menuConfig);
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Menus that reference prompts can have a sepcial "submit" block without the
|
|
|
|
// hassle of by-form-id configurations, etc.
|
|
|
|
//
|
|
|
|
// "submit" : [
|
|
|
|
// { ... }
|
|
|
|
// ]
|
|
|
|
//
|
|
|
|
var menuSubmit = self.client.currentMenuModule.menuConfig.submit;
|
|
|
|
if(!_.isArray(menuSubmit)) {
|
|
|
|
self.client.log.debug('No configuration to handle submit');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Locate matching action block
|
|
|
|
//
|
|
|
|
// :TODO: this is bacially the same as for menus -- DRY it up!
|
|
|
|
for(var c = 0; c < menuSubmit.length; ++c) {
|
|
|
|
var actionBlock = menuSubmit[c];
|
|
|
|
|
|
|
|
if(_.isEqual(formData.value, actionBlock.value, self.actionBlockValueComparator)) {
|
|
|
|
menuUtil.handleAction(self.client, formData, actionBlock);
|
|
|
|
break; // there an only be one...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-25 22:10:12 +00:00
|
|
|
});
|
|
|
|
}
|
2015-04-19 08:13:13 +00:00
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
2015-04-27 02:46:16 +00:00
|
|
|
function drawAllViews(callback) {
|
2015-06-26 04:34:33 +00:00
|
|
|
self.redrawAll(initialFocusId);
|
2015-04-27 02:46:16 +00:00
|
|
|
callback(null);
|
|
|
|
},
|
2015-04-20 04:58:18 +00:00
|
|
|
function setInitialViewFocus(callback) {
|
2015-04-19 08:13:13 +00:00
|
|
|
if(initialFocusId) {
|
|
|
|
self.switchFocus(initialFocusId);
|
|
|
|
}
|
2015-04-20 04:58:18 +00:00
|
|
|
callback(null);
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
ViewController.prototype.loadFromMenuConfig = function(options, cb) {
|
2015-04-19 08:13:13 +00:00
|
|
|
assert(_.isObject(options));
|
2015-04-20 04:58:18 +00:00
|
|
|
assert(_.isObject(options.mciMap));
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var formIdKey = options.formId ? options.formId.toString() : '0';
|
|
|
|
var initialFocusId = 1; // default to first
|
|
|
|
var formConfig;
|
|
|
|
|
|
|
|
// :TODO: honor options.withoutForm
|
|
|
|
|
|
|
|
// method for comparing submitted form data to configuration entries
|
2015-08-07 05:08:10 +00:00
|
|
|
/*
|
2015-04-20 04:58:18 +00:00
|
|
|
var actionBlockValueComparator = function(formValue, actionValue) {
|
|
|
|
//
|
2015-04-21 04:50:58 +00:00
|
|
|
// For a match to occur, one of the following must be true:
|
2015-04-20 04:58:18 +00:00
|
|
|
//
|
2015-04-21 04:50:58 +00:00
|
|
|
// * actionValue is a Object:
|
|
|
|
// a) All key/values must exactly match
|
2015-07-13 19:41:21 +00:00
|
|
|
// b) value is null; The key (view ID or "argName") must be present
|
2015-04-21 04:50:58 +00:00
|
|
|
// in formValue. This is a wildcard/any match.
|
|
|
|
// * actionValue is a Number: This represents a view ID that
|
|
|
|
// must be present in formValue.
|
2015-07-13 19:41:21 +00:00
|
|
|
// * actionValue is a string: This represents a view with
|
|
|
|
// "argName" set that must be present in formValue.
|
2015-04-21 04:50:58 +00:00
|
|
|
//
|
2015-07-13 19:41:21 +00:00
|
|
|
if(_.isNumber(actionValue) || _.isString(actionValue)) {
|
2015-04-21 04:50:58 +00:00
|
|
|
if(_.isUndefined(formValue[actionValue])) {
|
|
|
|
return false;
|
2015-04-20 04:58:18 +00:00
|
|
|
}
|
2015-04-21 04:50:58 +00:00
|
|
|
} else {
|
|
|
|
var actionValueKeys = Object.keys(actionValue);
|
|
|
|
for(var i = 0; i < actionValueKeys.length; ++i) {
|
|
|
|
var viewId = actionValueKeys[i];
|
|
|
|
if(!_.has(formValue, viewId)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-20 04:58:18 +00:00
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
if(null !== actionValue[viewId] && actionValue[viewId] !== formValue[viewId]) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-20 04:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-21 04:50:58 +00:00
|
|
|
|
2015-07-04 22:03:44 +00:00
|
|
|
self.client.log.trace( { formValue : formValue, actionValue : actionValue }, 'Action match');
|
2015-04-20 04:58:18 +00:00
|
|
|
return true;
|
|
|
|
};
|
2015-08-07 05:08:10 +00:00
|
|
|
*/
|
2015-04-19 08:13:13 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
2015-04-20 04:58:18 +00:00
|
|
|
function findMatchingFormConfig(callback) {
|
2015-04-21 04:50:58 +00:00
|
|
|
menuUtil.getFormConfigByIDAndMap(self.client.currentMenuModule.menuConfig, formIdKey, options.mciMap, function matchingConfig(err, fc) {
|
2015-04-20 04:58:18 +00:00
|
|
|
formConfig = fc;
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
if(err) {
|
|
|
|
// non-fatal
|
2015-07-04 22:03:44 +00:00
|
|
|
self.client.log.trace(
|
2015-04-20 04:58:18 +00:00
|
|
|
{ error : err, mci : Object.keys(options.mciMap), formId : formIdKey },
|
|
|
|
'Unable to find matching form configuration');
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 04:58:18 +00:00
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function createViews(callback) {
|
|
|
|
self.createViewsFromMCI(options.mciMap, function viewsCreated(err) {
|
2015-04-19 08:13:13 +00:00
|
|
|
callback(err);
|
|
|
|
});
|
2015-04-20 04:58:18 +00:00
|
|
|
},
|
2015-05-14 04:21:55 +00:00
|
|
|
function applyThemeCustomization(callback) {
|
|
|
|
if(_.isObject(formConfig)) {
|
|
|
|
menuUtil.applyThemeCustomization({
|
|
|
|
name : self.client.currentMenuModule.menuName,
|
2015-07-10 05:23:37 +00:00
|
|
|
type : 'menus',
|
2015-05-14 04:21:55 +00:00
|
|
|
client : self.client,
|
|
|
|
configMci : formConfig.mci,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
2015-04-20 04:58:18 +00:00
|
|
|
function applyViewConfiguration(callback) {
|
|
|
|
if(_.isObject(formConfig)) {
|
|
|
|
self.applyViewConfig(formConfig, function configApplied(err, info) {
|
|
|
|
initialFocusId = 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) {
|
2015-06-27 21:32:29 +00:00
|
|
|
|
2015-07-04 22:03:44 +00:00
|
|
|
self.client.log.trace( { formData : self.getLogFriendlyFormData(formData) }, 'Form submit');
|
2015-04-20 04:58:18 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Locate configuration for this form ID
|
|
|
|
//
|
|
|
|
var confForFormId;
|
|
|
|
if(_.isObject(formConfig.submit[formData.submitId])) {
|
|
|
|
confForFormId = formConfig.submit[formData.submitId];
|
|
|
|
} else if(_.isObject(formConfig.submit['*'])) {
|
|
|
|
confForFormId = formConfig.submit['*'];
|
|
|
|
} else {
|
|
|
|
// no configuration for this submitId
|
2015-07-09 04:34:40 +00:00
|
|
|
self.client.log.debug( { formId : formData.submitId }, 'No configuration for form ID');
|
2015-04-20 04:58:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Locate a matching action block based on the submitted data
|
|
|
|
//
|
|
|
|
for(var c = 0; c < confForFormId.length; ++c) {
|
|
|
|
var actionBlock = confForFormId[c];
|
|
|
|
|
2015-08-07 05:08:10 +00:00
|
|
|
if(_.isEqual(formData.value, actionBlock.value, self.actionBlockValueComparator)) {
|
2015-04-21 04:50:58 +00:00
|
|
|
menuUtil.handleAction(self.client, formData, actionBlock);
|
2015-04-20 04:58:18 +00:00
|
|
|
break; // there an only be one...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
2015-07-10 05:23:37 +00:00
|
|
|
function loadActionKeys(callback) {
|
|
|
|
if(!_.isObject(formConfig) || !_.isArray(formConfig.actionKeys)) {
|
|
|
|
callback(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var actionKeyEntry;
|
|
|
|
formConfig.actionKeys.forEach(function akEntry(ak) {
|
|
|
|
//
|
|
|
|
// * '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)
|
|
|
|
//
|
|
|
|
// Ultimately, we'll create a map of key -> { viewId | action }
|
|
|
|
//
|
|
|
|
// :TODO: allow for args to be passed along with action as well...
|
|
|
|
if(!_.isArray(ak.keys)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.isNumber(ak.viewId)) {
|
|
|
|
actionKeyEntry = ak.viewId;
|
|
|
|
} else if(_.isString(ak.action)) {
|
|
|
|
actionKeyEntry = ak.action;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ak.keys.forEach(function actionKeyName(kn) {
|
|
|
|
self.actionKeyMap[kn] = actionKeyEntry;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
2015-04-27 02:46:16 +00:00
|
|
|
function drawAllViews(callback) {
|
2015-06-26 04:34:33 +00:00
|
|
|
self.redrawAll(initialFocusId);
|
2015-04-27 02:46:16 +00:00
|
|
|
callback(null);
|
|
|
|
},
|
2015-04-20 04:58:18 +00:00
|
|
|
function setInitialViewFocus(callback) {
|
|
|
|
if(initialFocusId) {
|
|
|
|
self.switchFocus(initialFocusId);
|
|
|
|
}
|
|
|
|
callback(null);
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
|
|
|
],
|
2015-04-20 04:58:18 +00:00
|
|
|
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) {
|
|
|
|
var self = this;
|
|
|
|
var view;
|
|
|
|
|
|
|
|
return format.replace(/{(\d+)}/g, function replacer(match, number) {
|
|
|
|
view = self.getView(number);
|
|
|
|
|
|
|
|
if(!view) {
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2015-04-17 04:29:53 +00:00
|
|
|
return view.getData();
|
2015-04-04 20:41:04 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-07-04 18:02:37 +00:00
|
|
|
ViewController.prototype.getFormData = function() {
|
|
|
|
/*
|
|
|
|
Example form data:
|
|
|
|
{
|
|
|
|
id : 0,
|
|
|
|
submitId : 1,
|
|
|
|
value : {
|
|
|
|
"1" : "hurp",
|
|
|
|
"2" : [ 'a', 'b', ... ],
|
|
|
|
"3" 2,
|
|
|
|
"pants" : "no way"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
var formData = {
|
|
|
|
id : this.formId,
|
|
|
|
submitId : this.focusedView.id,
|
|
|
|
value : {},
|
|
|
|
};
|
|
|
|
|
|
|
|
var viewData;
|
|
|
|
var view;
|
|
|
|
for(var id in this.views) {
|
|
|
|
try {
|
|
|
|
view = this.views[id];
|
|
|
|
viewData = view.getData();
|
|
|
|
if(!_.isUndefined(viewData)) {
|
|
|
|
if(_.isString(view.submitArgName)) {
|
|
|
|
formData.value[view.submitArgName] = viewData;
|
|
|
|
} else {
|
|
|
|
formData.value[id] = viewData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(e) {
|
2015-07-22 05:52:20 +00:00
|
|
|
this.client.log.error(e); // :TODO: Log better ;)
|
2015-07-04 18:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return formData;
|
|
|
|
}
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
/*
|
2015-04-04 20:41:04 +00:00
|
|
|
ViewController.prototype.formatMenuArgs = function(args) {
|
|
|
|
var self = this;
|
|
|
|
|
2015-04-05 07:15:04 +00:00
|
|
|
return _.mapValues(args, function val(value) {
|
2015-04-04 20:41:04 +00:00
|
|
|
if('string' === typeof value) {
|
2015-04-05 07:15:04 +00:00
|
|
|
return self.formatMCIString(value);
|
2015-04-04 20:41:04 +00:00
|
|
|
}
|
2015-04-05 07:15:04 +00:00
|
|
|
return value;
|
2015-04-04 20:41:04 +00:00
|
|
|
});
|
2015-04-21 04:50:58 +00:00
|
|
|
};
|
|
|
|
*/
|