2015-11-03 22:17:14 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const loadMenu = require('./menu_util.js').loadMenu;
|
|
|
|
const { Errors, ErrorReasons } = require('./enig_error.js');
|
|
|
|
const { getResolvedSpec } = require('./menu_util.js');
|
2023-08-24 00:06:48 +00:00
|
|
|
const { MenuFlags } = require('./menu_module.js');
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
2023-08-24 00:06:48 +00:00
|
|
|
const bunyan = require('bunyan');
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-25 06:57:12 +00:00
|
|
|
module.exports = class MenuStack {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(client) {
|
2018-06-23 03:26:46 +00:00
|
|
|
this.client = client;
|
2022-06-05 20:04:25 +00:00
|
|
|
this.stack = [];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
push(moduleInfo) {
|
|
|
|
return this.stack.push(moduleInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
pop() {
|
|
|
|
return this.stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
peekPrev() {
|
2023-08-24 00:06:48 +00:00
|
|
|
return this.stack[this.stack.length - 2];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
top() {
|
2023-08-24 00:06:48 +00:00
|
|
|
return this.stack[this.stack.length - 1];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get currentModule() {
|
|
|
|
const top = this.top();
|
2018-12-09 09:32:41 +00:00
|
|
|
assert(top, 'Empty menu stack!');
|
|
|
|
return top.instance;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
next(cb) {
|
|
|
|
const currentModuleInfo = this.top();
|
2022-06-05 20:04:25 +00:00
|
|
|
const menuConfig = currentModuleInfo.instance.menuConfig;
|
|
|
|
const nextMenu = getResolvedSpec(this.client, menuConfig.next, 'next');
|
|
|
|
if (!nextMenu) {
|
|
|
|
return cb(
|
|
|
|
Array.isArray(menuConfig.next)
|
|
|
|
? Errors.MenuStack(
|
|
|
|
'No matching condition for "next"',
|
|
|
|
ErrorReasons.NoConditionMatch
|
|
|
|
)
|
|
|
|
: Errors.MenuStack(
|
|
|
|
'Invalid or missing "next" member in menu config',
|
|
|
|
ErrorReasons.InvalidNextMenu
|
|
|
|
)
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (nextMenu === currentModuleInfo.name) {
|
|
|
|
return cb(
|
|
|
|
Errors.MenuStack(
|
|
|
|
'Menu config "next" specifies current menu',
|
|
|
|
ErrorReasons.AlreadyThere
|
|
|
|
)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.goto(nextMenu, {}, cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prev(cb) {
|
|
|
|
const menuResult = this.top().instance.getMenuResult();
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.pop().instance.leave(); // leave & remove current
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2023-08-24 03:04:22 +00:00
|
|
|
const previousModuleInfo = this.pop(); // get previous; we'll re-create a instance
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (previousModuleInfo) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const opts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
extraArgs: previousModuleInfo.extraArgs,
|
|
|
|
savedState: previousModuleInfo.savedState,
|
|
|
|
lastMenuResult: menuResult,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return this.goto(previousModuleInfo.name, opts, cb);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(
|
|
|
|
Errors.MenuStack('No previous menu available', ErrorReasons.NoPreviousMenu)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
goto(name, options, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!cb && _.isFunction(options)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
options = options || {};
|
2023-02-13 00:48:34 +00:00
|
|
|
|
2023-08-24 03:04:22 +00:00
|
|
|
const currentModuleInfo = this.top();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (currentModuleInfo && name === currentModuleInfo.name) {
|
|
|
|
if (cb) {
|
|
|
|
cb(
|
|
|
|
Errors.MenuStack(
|
|
|
|
'Already at supplied menu',
|
|
|
|
ErrorReasons.AlreadyThere
|
|
|
|
)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: name,
|
2023-08-24 03:04:22 +00:00
|
|
|
client: this.client,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2023-08-24 03:04:22 +00:00
|
|
|
if (
|
|
|
|
currentModuleInfo &&
|
|
|
|
currentModuleInfo.menuFlags.includes(MenuFlags.ForwardArgs)
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
loadOpts.extraArgs = currentModuleInfo.extraArgs;
|
|
|
|
} else {
|
|
|
|
loadOpts.extraArgs = options.extraArgs || _.get(options, 'formData.value');
|
|
|
|
}
|
2018-06-23 03:26:46 +00:00
|
|
|
loadOpts.lastMenuResult = options.lastMenuResult;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
loadMenu(loadOpts, (err, modInst) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2023-08-24 03:04:22 +00:00
|
|
|
const errCb = cb || this.client.defaultHandlerMissingMod();
|
2018-06-22 05:15:04 +00:00
|
|
|
errCb(err);
|
|
|
|
} else {
|
2023-08-24 03:04:22 +00:00
|
|
|
this.client.log.debug({ menuName: name }, 'Goto menu module');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.client.acs.hasMenuModuleAccess(modInst)) {
|
|
|
|
if (cb) {
|
2018-08-04 17:49:44 +00:00
|
|
|
return cb(Errors.AccessDenied('No access to this menu'));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// If menuFlags were supplied in menu.hjson, they should win over
|
|
|
|
// anything supplied in code.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
let menuFlags;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === modInst.menuConfig.config.menuFlags.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
menuFlags = Array.isArray(options.menuFlags) ? options.menuFlags : [];
|
|
|
|
} else {
|
2018-08-05 20:06:30 +00:00
|
|
|
menuFlags = modInst.menuConfig.config.menuFlags;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// in code we can ask to merge in
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
Array.isArray(options.menuFlags) &&
|
2023-08-24 00:06:48 +00:00
|
|
|
options.menuFlags.includes(MenuFlags.MergeFlags)
|
2022-06-05 20:04:25 +00:00
|
|
|
) {
|
2023-08-24 00:06:48 +00:00
|
|
|
menuFlags = [...new Set(options.menuFlags)]; // make unique
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (currentModuleInfo) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// save stack state
|
2022-06-05 20:04:25 +00:00
|
|
|
currentModuleInfo.savedState =
|
|
|
|
currentModuleInfo.instance.getSaveState();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
currentModuleInfo.instance.leave();
|
|
|
|
|
2023-08-24 00:06:48 +00:00
|
|
|
if (currentModuleInfo.menuFlags.includes(MenuFlags.NoHistory)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 03:04:22 +00:00
|
|
|
this.push({
|
2022-06-05 20:04:25 +00:00
|
|
|
name: name,
|
|
|
|
instance: modInst,
|
|
|
|
extraArgs: loadOpts.extraArgs,
|
|
|
|
menuFlags: menuFlags,
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// restore previous state if requested
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.savedState) {
|
2018-06-22 05:15:04 +00:00
|
|
|
modInst.restoreSavedState(options.savedState);
|
|
|
|
}
|
|
|
|
|
2023-08-24 03:04:22 +00:00
|
|
|
if (this.client.log.level() <= bunyan.TRACE) {
|
|
|
|
const stackEntries = this.stack.map(stackEntry => {
|
2023-08-24 00:06:48 +00:00
|
|
|
let name = stackEntry.name;
|
|
|
|
if (stackEntry.instance.menuConfig.config.menuFlags.length > 0) {
|
|
|
|
name += ` (${stackEntry.instance.menuConfig.config.menuFlags.join(
|
|
|
|
', '
|
|
|
|
)})`;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
});
|
|
|
|
|
2023-08-24 03:04:22 +00:00
|
|
|
this.client.log.trace({ stack: stackEntries }, 'Updated menu stack');
|
2023-08-24 00:06:48 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
modInst.enter();
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
};
|