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');
|
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 02:11:26 +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 {
|
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 02:11:26 +00:00
|
|
|
if (this.stackSize > 1) {
|
|
|
|
return this.stack[this.stack.length - 2];
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
top() {
|
2023-08-24 02:11:26 +00:00
|
|
|
if (this.stackSize > 0) {
|
|
|
|
return this.stack[this.stack.length - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get stackSize() {
|
|
|
|
return this.stack.length;
|
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(
|
2023-08-24 02:11:26 +00:00
|
|
|
'No matching condition for "next"',
|
|
|
|
ErrorReasons.NoConditionMatch
|
|
|
|
)
|
2022-06-05 20:04:25 +00:00
|
|
|
: Errors.MenuStack(
|
2023-08-24 02:11:26 +00:00
|
|
|
'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();
|
|
|
|
|
2023-08-24 02:11:26 +00:00
|
|
|
// :TODO: leave() should really take a cb...
|
2022-06-05 20:04:25 +00:00
|
|
|
this.pop().instance.leave(); // leave & remove current
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const previousModuleInfo = this.pop(); // get previous
|
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) {
|
|
|
|
const currentModuleInfo = this.top();
|
|
|
|
|
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 || {};
|
|
|
|
const self = this;
|
|
|
|
|
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,
|
|
|
|
client: self.client,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2023-08-24 02:11:26 +00:00
|
|
|
if (currentModuleInfo && currentModuleInfo.menuFlags.includes('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 02:11:26 +00:00
|
|
|
// :TODO: probably should just require a cb...
|
2018-06-22 05:15:04 +00:00
|
|
|
const errCb = cb || self.client.defaultHandlerMissingMod();
|
|
|
|
errCb(err);
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.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;
|
|
|
|
}
|
|
|
|
|
2023-08-24 02:11:26 +00:00
|
|
|
//
|
|
|
|
// Handle deprecated 'options' block by merging to config and warning user.
|
|
|
|
// :TODO: Remove in 0.0.10+
|
|
|
|
//
|
|
|
|
if (modInst.menuConfig.options) {
|
|
|
|
self.client.log.warn(
|
|
|
|
{ options: modInst.menuConfig.options },
|
|
|
|
'Use of "options" is deprecated. Move relevant members to "config" block! Support will be fully removed in future versions'
|
|
|
|
);
|
|
|
|
Object.assign(
|
|
|
|
modInst.menuConfig.config || {},
|
|
|
|
modInst.menuConfig.options
|
|
|
|
);
|
|
|
|
delete modInst.menuConfig.options;
|
|
|
|
}
|
|
|
|
|
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 02:11:26 +00:00
|
|
|
options.menuFlags.includes('mergeFlags')
|
2022-06-05 20:04:25 +00:00
|
|
|
) {
|
2023-08-24 02:11:26 +00:00
|
|
|
menuFlags = _.uniq(menuFlags.concat(options.menuFlags));
|
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 02:11:26 +00:00
|
|
|
if (currentModuleInfo.menuFlags.includes('noHistory')) {
|
|
|
|
this.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (menuFlags.includes('popParent')) {
|
|
|
|
this.pop().instance.leave(); // leave & remove current
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.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 02:11:26 +00:00
|
|
|
const stackEntries = self.stack.map(stackEntry => {
|
|
|
|
let name = stackEntry.name;
|
|
|
|
if (stackEntry.instance.menuConfig.config.menuFlags.length > 0) {
|
|
|
|
name += ` (${stackEntry.instance.menuConfig.config.menuFlags.join(
|
|
|
|
', '
|
|
|
|
)})`;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
});
|
|
|
|
|
|
|
|
self.client.log.trace({ stack: stackEntries }, 'Updated menu stack');
|
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
|
|
|
};
|