2015-11-03 22:17:14 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2016-07-25 06:57:12 +00:00
|
|
|
const loadMenu = require('./menu_util.js').loadMenu;
|
2016-09-20 03:30:26 +00:00
|
|
|
const Errors = require('./enig_error.js').Errors;
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
// :TODO: Stack is backwards.... top should be most recent! :)
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
module.exports = class MenuStack {
|
|
|
|
constructor(client) {
|
|
|
|
this.client = client;
|
|
|
|
this.stack = [];
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
push(moduleInfo) {
|
|
|
|
return this.stack.push(moduleInfo);
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
pop() {
|
|
|
|
return this.stack.pop();
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
peekPrev() {
|
|
|
|
if(this.stackSize > 1) {
|
|
|
|
return this.stack[this.stack.length - 2];
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
top() {
|
|
|
|
if(this.stackSize > 0) {
|
|
|
|
return this.stack[this.stack.length - 1];
|
2015-11-04 06:15:49 +00:00
|
|
|
}
|
2016-07-25 06:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get stackSize() {
|
|
|
|
return this.stack.length;
|
|
|
|
}
|
2015-11-04 06:15:49 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
get currentModule() {
|
|
|
|
const top = this.top();
|
|
|
|
if(top) {
|
|
|
|
return top.instance;
|
2015-11-03 23:42:11 +00:00
|
|
|
}
|
2016-07-25 06:57:12 +00:00
|
|
|
}
|
2015-11-03 23:42:11 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
next(cb) {
|
|
|
|
const currentModuleInfo = this.top();
|
|
|
|
assert(currentModuleInfo, 'Empty menu stack!');
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
const menuConfig = currentModuleInfo.instance.menuConfig;
|
|
|
|
let nextMenu;
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
if(_.isArray(menuConfig.next)) {
|
|
|
|
nextMenu = this.client.acs.getConditionalValue(menuConfig.next, 'next');
|
|
|
|
if(!nextMenu) {
|
2016-09-20 03:30:26 +00:00
|
|
|
return cb(Errors.MenuStack('No matching condition for "next"', 'NOCONDMATCH'));
|
2016-07-25 06:57:12 +00:00
|
|
|
}
|
|
|
|
} else if(_.isString(menuConfig.next)) {
|
|
|
|
nextMenu = menuConfig.next;
|
|
|
|
} else {
|
2016-09-20 03:30:26 +00:00
|
|
|
return cb(Errors.MenuStack('Invalid or missing "next" member in menu config', 'BADNEXT'));
|
2016-07-25 06:57:12 +00:00
|
|
|
}
|
2015-11-12 07:31:25 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
if(nextMenu === currentModuleInfo.name) {
|
2016-09-20 03:30:26 +00:00
|
|
|
return cb(Errors.MenuStack('Menu config "next" specifies current menu', 'ALREADYTHERE'));
|
2015-11-12 07:31:25 +00:00
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
this.goto(nextMenu, { }, cb);
|
2015-11-03 22:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
prev(cb) {
|
2016-08-31 03:31:24 +00:00
|
|
|
const menuResult = this.top().instance.getMenuResult();
|
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
// :TODO: leave() should really take a cb...
|
|
|
|
this.pop().instance.leave(); // leave & remove current
|
2016-07-20 03:01:49 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
const previousModuleInfo = this.pop(); // get previous
|
2015-11-04 06:15:49 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
if(previousModuleInfo) {
|
|
|
|
const opts = {
|
2016-08-31 03:31:24 +00:00
|
|
|
extraArgs : previousModuleInfo.extraArgs,
|
|
|
|
savedState : previousModuleInfo.savedState,
|
|
|
|
lastMenuResult : menuResult,
|
2016-07-25 06:57:12 +00:00
|
|
|
};
|
2015-11-04 06:15:49 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
return this.goto(previousModuleInfo.name, opts, cb);
|
2015-11-03 22:17:14 +00:00
|
|
|
}
|
2016-07-25 06:57:12 +00:00
|
|
|
|
2016-09-20 03:30:26 +00:00
|
|
|
return cb(Errors.MenuStack('No previous menu available', 'NOPREV'));
|
2015-11-03 22:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
goto(name, options, cb) {
|
|
|
|
const currentModuleInfo = this.top();
|
2015-11-04 06:15:49 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
if(!cb && _.isFunction(options)) {
|
|
|
|
cb = options;
|
|
|
|
}
|
2015-11-03 23:42:11 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
const self = this;
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
if(currentModuleInfo && name === currentModuleInfo.name) {
|
|
|
|
if(cb) {
|
2016-09-20 03:30:26 +00:00
|
|
|
cb(Errors.MenuStack('Already at supplied menu', 'ALREADYTHERE'));
|
2015-11-03 23:42:11 +00:00
|
|
|
}
|
2016-07-25 06:57:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
const loadOpts = {
|
|
|
|
name : name,
|
|
|
|
client : self.client,
|
|
|
|
};
|
2015-11-04 06:15:49 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
if(_.isObject(options)) {
|
2016-08-31 03:31:24 +00:00
|
|
|
loadOpts.extraArgs = options.extraArgs;
|
|
|
|
loadOpts.lastMenuResult = options.lastMenuResult;
|
2015-11-03 22:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
loadMenu(loadOpts, (err, modInst) => {
|
|
|
|
if(err) {
|
|
|
|
// :TODO: probably should just require a cb...
|
|
|
|
const errCb = cb || self.client.defaultHandlerMissingMod();
|
|
|
|
errCb(err);
|
|
|
|
} else {
|
|
|
|
self.client.log.debug( { menuName : name }, 'Goto menu module');
|
|
|
|
|
|
|
|
if(currentModuleInfo) {
|
|
|
|
// save stack state
|
|
|
|
currentModuleInfo.savedState = currentModuleInfo.instance.getSaveState();
|
|
|
|
|
|
|
|
currentModuleInfo.instance.leave();
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2017-01-28 19:33:06 +00:00
|
|
|
if(modInst.menuConfig.options.menuFlags.includes('noHistory')) {
|
|
|
|
this.pop().instance.leave(); // leave & remove current
|
|
|
|
}
|
2016-12-31 21:50:29 +00:00
|
|
|
}
|
2016-07-25 06:57:12 +00:00
|
|
|
|
2017-01-28 19:33:06 +00:00
|
|
|
self.push({
|
|
|
|
name : name,
|
|
|
|
instance : modInst,
|
|
|
|
extraArgs : loadOpts.extraArgs,
|
|
|
|
});
|
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
// restore previous state if requested
|
|
|
|
if(options && options.savedState) {
|
|
|
|
modInst.restoreSavedState(options.savedState);
|
|
|
|
}
|
|
|
|
|
2017-01-28 19:33:06 +00:00
|
|
|
const stackEntries = self.stack.map(stackEntry => {
|
|
|
|
let name = stackEntry.name;
|
|
|
|
if(stackEntry.instance.menuConfig.options.menuFlags.length > 0) {
|
|
|
|
name += ` (${stackEntry.instance.menuConfig.options.menuFlags.join(', ')})`;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
});
|
2016-07-25 06:57:12 +00:00
|
|
|
|
2017-01-28 19:33:06 +00:00
|
|
|
self.client.log.trace( { stack : stackEntries }, 'Updated menu stack' );
|
|
|
|
|
|
|
|
modInst.enter();
|
2016-07-25 06:57:12 +00:00
|
|
|
|
|
|
|
if(cb) {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-12-08 02:15:15 +00:00
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
};
|