enigma-bbs/core/menu_stack.js

211 lines
6.4 KiB
JavaScript
Raw Normal View History

2015-11-03 22:17:14 +00:00
/* jslint node: true */
'use strict';
// ENiGMA½
const loadMenu = require('./menu_util.js').loadMenu;
const { Errors, ErrorReasons } = require('./enig_error.js');
const { getResolvedSpec } = require('./menu_util.js');
const { MenuFlags } = require('./menu_module.js');
2015-11-03 22:17:14 +00:00
// deps
const _ = require('lodash');
const assert = require('assert');
const bunyan = require('bunyan');
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 = [];
}
push(moduleInfo) {
return this.stack.push(moduleInfo);
}
pop() {
return this.stack.pop();
}
peekPrev() {
return this.stack[this.stack.length - 2];
}
top() {
return this.stack[this.stack.length - 1];
}
get currentModule() {
const top = this.top();
2018-12-09 09:32:41 +00:00
assert(top, 'Empty menu stack!');
return top.instance;
}
next(cb) {
const currentModuleInfo = this.top();
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
)
);
}
if (nextMenu === currentModuleInfo.name) {
return cb(
Errors.MenuStack(
'Menu config "next" specifies current menu',
ErrorReasons.AlreadyThere
)
);
}
this.goto(nextMenu, {}, cb);
}
prev(cb) {
const menuResult = this.top().instance.getMenuResult();
this.pop().instance.leave(); // leave & remove current
2023-08-24 03:04:22 +00:00
const previousModuleInfo = this.pop(); // get previous; we'll re-create a instance
if (previousModuleInfo) {
const opts = {
extraArgs: previousModuleInfo.extraArgs,
savedState: previousModuleInfo.savedState,
lastMenuResult: menuResult,
};
return this.goto(previousModuleInfo.name, opts, cb);
}
return cb(
Errors.MenuStack('No previous menu available', ErrorReasons.NoPreviousMenu)
);
}
goto(name, options, cb) {
if (!cb && _.isFunction(options)) {
cb = options;
options = {};
}
options = options || {};
2023-08-24 03:04:22 +00:00
const currentModuleInfo = this.top();
if (currentModuleInfo && name === currentModuleInfo.name) {
if (cb) {
cb(
Errors.MenuStack(
'Already at supplied menu',
ErrorReasons.AlreadyThere
)
);
}
return;
}
const loadOpts = {
name: name,
2023-08-24 03:04:22 +00:00
client: this.client,
};
2023-08-24 03:04:22 +00:00
if (
currentModuleInfo &&
currentModuleInfo.menuFlags.includes(MenuFlags.ForwardArgs)
) {
loadOpts.extraArgs = currentModuleInfo.extraArgs;
} else {
loadOpts.extraArgs = options.extraArgs || _.get(options, 'formData.value');
}
loadOpts.lastMenuResult = options.lastMenuResult;
loadMenu(loadOpts, (err, modInst) => {
if (err) {
2023-08-24 03:04:22 +00:00
const errCb = cb || this.client.defaultHandlerMissingMod();
errCb(err);
} else {
2023-08-24 03:04:22 +00:00
this.client.log.debug({ menuName: name }, 'Goto menu module');
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;
}
//
// If menuFlags were supplied in menu.hjson, they should win over
// anything supplied in code.
//
let menuFlags;
if (0 === modInst.menuConfig.config.menuFlags.length) {
menuFlags = Array.isArray(options.menuFlags) ? options.menuFlags : [];
} else {
menuFlags = modInst.menuConfig.config.menuFlags;
// in code we can ask to merge in
if (
Array.isArray(options.menuFlags) &&
options.menuFlags.includes(MenuFlags.MergeFlags)
) {
menuFlags = [...new Set(options.menuFlags)]; // make unique
}
}
if (currentModuleInfo) {
// save stack state
currentModuleInfo.savedState =
currentModuleInfo.instance.getSaveState();
currentModuleInfo.instance.leave();
if (currentModuleInfo.menuFlags.includes(MenuFlags.NoHistory)) {
this.pop();
}
}
2023-08-24 03:04:22 +00:00
this.push({
name: name,
instance: modInst,
extraArgs: loadOpts.extraArgs,
menuFlags: menuFlags,
});
// restore previous state if requested
if (options.savedState) {
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 => {
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');
}
modInst.enter();
if (cb) {
cb(null);
}
}
});
}
2015-11-03 22:17:14 +00:00
};