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-03-30 03:47:48 +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;
|
|
|
|
this.mciViewFactory = new MCIViewFactory(this.client);
|
2015-04-24 05:00:48 +00:00
|
|
|
this.submitKeyMap = {};
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
this.clientKeyPressHandler = function(key, isSpecial) {
|
2014-10-23 05:41:00 +00:00
|
|
|
if(isSpecial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(self.focusedView && self.focusedView.acceptsInput) {
|
|
|
|
key = 'string' === typeof key ? key : key.toString();
|
|
|
|
self.focusedView.onKeyPress(key, isSpecial);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
this.clientSpecialKeyHandler = function(keyName) {
|
|
|
|
|
2015-04-24 05:00:48 +00:00
|
|
|
var submitViewId = self.submitKeyMap[keyName];
|
|
|
|
if(submitViewId) {
|
|
|
|
self.switchFocus(submitViewId);
|
|
|
|
self.submitForm();
|
|
|
|
} else {
|
|
|
|
if(self.focusedView && self.focusedView.acceptsInput) {
|
|
|
|
self.focusedView.onSpecialKeyPress(keyName);
|
|
|
|
}
|
2014-10-23 05:41:00 +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;
|
|
|
|
|
2014-11-02 19:07:17 +00:00
|
|
|
case 'accept' : // :TODO: consider naming this 'done'
|
2014-10-23 05:41:00 +00:00
|
|
|
// :TODO: check if id is submit, etc.
|
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-03-26 05:23:14 +00:00
|
|
|
/*
|
|
|
|
Generate a form resonse. Example:
|
|
|
|
|
|
|
|
{
|
|
|
|
id : 0,
|
|
|
|
submitId : 1,
|
2015-03-30 03:47:48 +00:00
|
|
|
value : {
|
2015-03-26 05:23:14 +00:00
|
|
|
"1" : "hurp",
|
|
|
|
"2" : [ 'a', 'b', ... ],
|
2015-04-24 05:00:48 +00:00
|
|
|
"3" 2,
|
|
|
|
"pants" : "no way"
|
2015-03-26 05:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
2014-11-02 19:07:17 +00:00
|
|
|
var formData = {
|
2015-03-26 05:23:14 +00:00
|
|
|
id : self.formId,
|
|
|
|
submitId : self.focusedView.id,
|
2015-03-30 03:47:48 +00:00
|
|
|
value : {},
|
2014-11-02 19:07:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var viewData;
|
2015-04-19 08:13:13 +00:00
|
|
|
var view;
|
2014-11-02 19:07:17 +00:00
|
|
|
for(var id in self.views) {
|
|
|
|
try {
|
2015-04-19 08:13:13 +00:00
|
|
|
view = self.views[id];
|
|
|
|
viewData = view.getData();
|
|
|
|
if(!_.isUndefined(viewData)) {
|
|
|
|
if(_.isString(view.submitArgName)) {
|
|
|
|
formData.value[view.submitArgName] = viewData;
|
|
|
|
} else {
|
|
|
|
formData.value[id] = viewData;
|
|
|
|
}
|
2014-11-02 19:07:17 +00:00
|
|
|
}
|
|
|
|
} catch(e) {
|
2015-03-30 03:47:48 +00:00
|
|
|
Log.error(e); // :TODO: Log better ;)
|
2014-11-02 19:07:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('submit', formData);
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if(view) {
|
|
|
|
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-04-27 02:46:16 +00:00
|
|
|
|
2015-04-28 04:40:05 +00:00
|
|
|
function setViewProp(propName, setter) {
|
2015-04-27 02:46:16 +00:00
|
|
|
if(conf[propName]) {
|
2015-04-28 04:40:05 +00:00
|
|
|
var propValue = asset.resolveConfigAsset(conf[propName]);
|
|
|
|
if(propValue) {
|
|
|
|
if(setter) {
|
|
|
|
setter(propValue);
|
|
|
|
} else {
|
|
|
|
view[propName] = propValue;
|
|
|
|
}
|
|
|
|
}
|
2015-04-27 02:46:16 +00:00
|
|
|
}
|
2015-04-24 05:00:48 +00:00
|
|
|
}
|
2015-04-20 04:58:18 +00:00
|
|
|
|
2015-04-28 04:40:05 +00:00
|
|
|
setViewProp('items', function(v) { view.setItems(v); });
|
|
|
|
setViewProp('text', function(v) { view.setText(v); });
|
|
|
|
setViewProp('textStyle');
|
|
|
|
setViewProp('focusTextStyle');
|
|
|
|
setViewProp('maxLength');
|
|
|
|
setViewProp('width', function(v) { view.dimens.width = parseInt(v, 10); });
|
|
|
|
|
|
|
|
setViewProp('fillChar', function(v) {
|
|
|
|
if(_.isNumber(v)) {
|
|
|
|
view.fillChar = String.fromCharCode(v);
|
|
|
|
} else if(_.isString(v)) {
|
|
|
|
view.fillChar = v.substr(0, 1);
|
2015-04-27 02:46:16 +00:00
|
|
|
}
|
2015-04-28 04:40:05 +00:00
|
|
|
});
|
2015-04-27 02:46:16 +00:00
|
|
|
|
2015-04-28 04:40:05 +00:00
|
|
|
setViewProp('password', function(v) {
|
|
|
|
if(true === v) {
|
|
|
|
view.textMaskChar = self.client.currentThemeInfo.getPasswordChar();
|
|
|
|
}
|
|
|
|
});
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2015-04-28 04:40:05 +00:00
|
|
|
setViewProp('textMaskChar', function(v) { view.textMaskChar = v.substr(0, 1); });
|
|
|
|
setViewProp('hotKeys', function(v) { view.setHotKeys(v); });
|
2015-04-27 02:46:16 +00:00
|
|
|
|
2015-04-28 04:40:05 +00:00
|
|
|
setViewProp('submit', function(v) {
|
|
|
|
if(_.isBoolean(v)) {
|
|
|
|
view.submit = v;
|
|
|
|
} else {
|
|
|
|
view.submit = _.isArray(v) && v.length > 0;
|
|
|
|
}
|
|
|
|
});
|
2015-04-20 04:58:18 +00:00
|
|
|
|
2015-04-28 04:40:05 +00:00
|
|
|
setViewProp('argName', function(v) { view.submitArgName = v; });
|
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));
|
|
|
|
|
|
|
|
var view = self.getView(viewId);
|
2015-04-27 02:46:16 +00:00
|
|
|
|
|
|
|
if(!view) {
|
|
|
|
Log.warn( { viewId : viewId }, 'Cannot find view');
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(view.submit) {
|
|
|
|
submitId = viewId;
|
2015-04-24 05:00:48 +00:00
|
|
|
|
|
|
|
if(_.isArray(mciConf.submit)) {
|
|
|
|
for(var i = 0; i < mciConf.submit.length; i++) {
|
|
|
|
self.submitKeyMap[mciConf.submit[i]] = viewId;
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 04:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nextItem(null);
|
|
|
|
},
|
|
|
|
function complete(err) {
|
|
|
|
|
|
|
|
// default to highest ID if no 'submit' entry present
|
|
|
|
if(!submitId) {
|
|
|
|
self.getView(highestId).submit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(err, { initialFocusId : initialFocusId } );
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
this.attachClientEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(ViewController, events.EventEmitter);
|
|
|
|
|
|
|
|
ViewController.prototype.attachClientEvents = function() {
|
|
|
|
if(this.attached) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
this.client.on('key press', this.clientKeyPressHandler);
|
|
|
|
this.client.on('special key', this.clientSpecialKeyHandler);
|
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);
|
|
|
|
this.client.removeListener('special key', this.clientSpecialKeyHandler);
|
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;
|
|
|
|
};
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
ViewController.prototype.switchFocus = function(id) {
|
|
|
|
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-04-14 06:19:14 +00:00
|
|
|
this.switchFocusEvent('enter', view);
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
this.focusedView = view;
|
|
|
|
this.focusedView.setFocus(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewController.prototype.nextFocus = function() {
|
|
|
|
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-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-04-21 04:50:58 +00:00
|
|
|
var promptConfig = 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-04-20 04:58:18 +00:00
|
|
|
function applyViewConfiguration(callback) {
|
|
|
|
self.applyViewConfig(promptConfig, function configApplied(err, info) {
|
|
|
|
initialFocusId = info.initialFocusId;
|
2015-04-19 08:13:13 +00:00
|
|
|
callback(err);
|
2015-04-20 04:58:18 +00:00
|
|
|
});
|
2015-04-19 08:13:13 +00:00
|
|
|
},
|
2015-04-20 04:58:18 +00:00
|
|
|
function prepareFormSubmission(callback) {
|
2015-04-19 08:13:13 +00:00
|
|
|
|
|
|
|
self.on('submit', function promptSubmit(formData) {
|
2015-04-20 04:58:18 +00:00
|
|
|
Log.trace( { formData : self.getLogFriendlyFormData(formData) }, 'Prompt submit');
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
menuUtil.handleAction(self.client, formData, self.client.currentMenuModule.menuConfig);
|
2015-04-19 08:13:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
2015-04-27 02:46:16 +00:00
|
|
|
function drawAllViews(callback) {
|
2015-04-28 04:40:05 +00:00
|
|
|
self.client.term.write(ansi.hideCursor());
|
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
for(var id in self.views) {
|
|
|
|
if(initialFocusId === id) {
|
|
|
|
continue; // will draw @ focus
|
|
|
|
}
|
|
|
|
self.views[id].redraw();
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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
|
|
|
|
// b) value is null; The key (view ID) 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.
|
|
|
|
//
|
|
|
|
if(_.isNumber(actionValue)) {
|
|
|
|
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-04-20 04:58:18 +00:00
|
|
|
return true;
|
|
|
|
};
|
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
|
|
|
|
Log.trace(
|
|
|
|
{ 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
|
|
|
},
|
|
|
|
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) {
|
|
|
|
Log.trace( { formData : self.getLogFriendlyFormData(formData) }, 'Form submit');
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
Log.debug( { formId : formData.submitId }, 'No configuration for form ID');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Locate a matching action block based on the submitted data
|
|
|
|
//
|
|
|
|
for(var c = 0; c < confForFormId.length; ++c) {
|
|
|
|
var actionBlock = confForFormId[c];
|
|
|
|
|
|
|
|
if(_.isEqual(formData.value, actionBlock.value, 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-04-27 02:46:16 +00:00
|
|
|
function drawAllViews(callback) {
|
2015-04-28 04:40:05 +00:00
|
|
|
self.client.term.write(ansi.hideCursor());
|
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
for(var id in self.views) {
|
|
|
|
if(initialFocusId === id) {
|
|
|
|
continue; // will draw @ focus
|
|
|
|
}
|
|
|
|
self.views[id].redraw();
|
|
|
|
}
|
|
|
|
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-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
|
|
|
};
|
|
|
|
*/
|