2015-11-03 22:17:14 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2015-11-03 23:42:11 +00:00
|
|
|
var loadMenu = require('./menu_util.js').loadMenu;
|
2015-11-03 22:17:14 +00:00
|
|
|
|
|
|
|
var _ = require('lodash');
|
2015-11-04 06:15:49 +00:00
|
|
|
var assert = require('assert');
|
2015-11-03 22:17:14 +00:00
|
|
|
|
|
|
|
module.exports = MenuStack;
|
|
|
|
|
|
|
|
function MenuStack(client) {
|
|
|
|
this.client = client;
|
|
|
|
this.stack = [];
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.push = function(moduleInfo) {
|
|
|
|
return self.stack.push(moduleInfo);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.pop = function() {
|
|
|
|
return self.stack.pop();
|
|
|
|
};
|
|
|
|
|
2015-11-04 06:15:49 +00:00
|
|
|
this.peekPrev = function() {
|
|
|
|
if(this.stackSize() > 1) {
|
|
|
|
return self.stack[self.stack.length - 2];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-03 22:17:14 +00:00
|
|
|
this.top = function() {
|
2015-11-03 23:42:11 +00:00
|
|
|
if(self.stackSize() > 0) {
|
|
|
|
return self.stack[self.stack.length - 1];
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
};
|
2015-11-03 23:42:11 +00:00
|
|
|
|
|
|
|
this.stackSize = function() {
|
|
|
|
return self.stack.length;
|
2015-11-12 07:31:25 +00:00
|
|
|
};
|
2015-11-03 22:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MenuStack.prototype.next = function(cb) {
|
2015-11-03 23:42:11 +00:00
|
|
|
var currentModuleInfo = this.top();
|
2015-11-04 06:15:49 +00:00
|
|
|
assert(currentModuleInfo, 'Empty menu stack!');
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2015-11-04 06:15:49 +00:00
|
|
|
var menuConfig = currentModuleInfo.instance.menuConfig;
|
2015-11-12 07:31:25 +00:00
|
|
|
var next;
|
|
|
|
|
|
|
|
if(_.isArray(menuConfig.next)) {
|
2016-07-24 17:47:34 +00:00
|
|
|
next = this.client.acs.getConditionalValue(menuConfig.next, 'next');
|
2015-11-12 07:31:25 +00:00
|
|
|
if(!next) {
|
|
|
|
cb(new Error('No matching condition for \'next\'!'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if(_.isString(menuConfig.next)) {
|
|
|
|
next = menuConfig.next;
|
|
|
|
} else {
|
|
|
|
cb(new Error('Invalid or missing \'next\' member in menu config!'));
|
2015-11-03 22:17:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-14 19:22:21 +00:00
|
|
|
if(next === currentModuleInfo.name) {
|
2015-11-04 06:15:49 +00:00
|
|
|
cb(new Error('Menu config \'next\' specifies current menu!'));
|
2015-11-03 22:17:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-14 19:22:21 +00:00
|
|
|
this.goto(next, { }, cb);
|
2015-11-03 22:17:14 +00:00
|
|
|
};
|
|
|
|
|
2015-11-03 23:42:11 +00:00
|
|
|
MenuStack.prototype.prev = function(cb) {
|
2015-11-04 06:15:49 +00:00
|
|
|
this.pop().instance.leave(); // leave & remove current
|
2016-07-20 03:01:49 +00:00
|
|
|
|
|
|
|
const previousModuleInfo = this.pop(); // get previous
|
2015-11-04 06:15:49 +00:00
|
|
|
|
2015-11-03 23:42:11 +00:00
|
|
|
if(previousModuleInfo) {
|
2016-07-20 03:01:49 +00:00
|
|
|
this.goto(
|
|
|
|
previousModuleInfo.name,
|
|
|
|
{
|
|
|
|
extraArgs : previousModuleInfo.extraArgs,
|
|
|
|
savedState : previousModuleInfo.savedState
|
|
|
|
},
|
|
|
|
cb
|
|
|
|
);
|
2015-11-03 23:42:11 +00:00
|
|
|
} else {
|
|
|
|
cb(new Error('No previous menu available!'));
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MenuStack.prototype.goto = function(name, options, cb) {
|
2015-11-04 06:15:49 +00:00
|
|
|
var currentModuleInfo = this.top();
|
|
|
|
|
|
|
|
if(!cb && _.isFunction(options)) {
|
|
|
|
cb = options;
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-07-20 03:01:49 +00:00
|
|
|
const self = this;
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2015-11-03 23:42:11 +00:00
|
|
|
if(currentModuleInfo && name === currentModuleInfo.name) {
|
2015-11-03 22:17:14 +00:00
|
|
|
if(cb) {
|
2015-11-04 06:15:49 +00:00
|
|
|
cb(new Error('Already at supplied menu!'));
|
2015-11-03 22:17:14 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-20 03:01:49 +00:00
|
|
|
const loadOpts = {
|
2015-11-03 22:17:14 +00:00
|
|
|
name : name,
|
|
|
|
client : self.client,
|
|
|
|
};
|
|
|
|
|
2016-01-15 05:44:33 +00:00
|
|
|
if(_.isObject(options)) {
|
2015-11-04 06:15:49 +00:00
|
|
|
loadOpts.extraArgs = options.extraArgs;
|
|
|
|
}
|
|
|
|
|
2015-11-03 23:42:11 +00:00
|
|
|
loadMenu(loadOpts, function menuLoaded(err, modInst) {
|
2015-11-03 22:17:14 +00:00
|
|
|
if(err) {
|
2016-07-20 03:01:49 +00:00
|
|
|
const errCb = cb || self.client.defaultHandlerMissingMod();
|
2015-11-03 22:17:14 +00:00
|
|
|
errCb(err);
|
|
|
|
} else {
|
2015-11-04 06:15:49 +00:00
|
|
|
// :TODO: Move this log to caller
|
2015-11-03 22:17:14 +00:00
|
|
|
self.client.log.debug( { menuName : name }, 'Goto menu module');
|
|
|
|
|
2015-11-03 23:42:11 +00:00
|
|
|
if(currentModuleInfo) {
|
|
|
|
// save stack state
|
|
|
|
currentModuleInfo.savedState = currentModuleInfo.instance.getSaveState();
|
|
|
|
|
|
|
|
currentModuleInfo.instance.leave();
|
|
|
|
}
|
|
|
|
|
2015-11-04 06:15:49 +00:00
|
|
|
self.push({
|
2015-11-03 22:17:14 +00:00
|
|
|
name : name,
|
|
|
|
instance : modInst,
|
2015-11-04 06:15:49 +00:00
|
|
|
extraArgs : loadOpts.extraArgs,
|
2015-11-03 23:42:11 +00:00
|
|
|
});
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2015-11-03 23:42:11 +00:00
|
|
|
// restore previous state if requested
|
2015-11-04 06:15:49 +00:00
|
|
|
if(options && options.savedState) {
|
2015-11-03 23:42:11 +00:00
|
|
|
modInst.restoreSavedState(options.savedState);
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
modInst.enter();
|
2015-11-03 22:17:14 +00:00
|
|
|
|
2015-11-04 06:15:49 +00:00
|
|
|
self.client.log.trace(
|
|
|
|
{ stack : _.map(self.stack, function(si) { return si.name; } ) },
|
|
|
|
'Updated menu stack');
|
|
|
|
|
2015-11-03 22:17:14 +00:00
|
|
|
if(cb) {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
MenuStack.prototype.getCurrentModule = function() {
|
2015-12-08 02:15:15 +00:00
|
|
|
var top = this.top();
|
|
|
|
if(top) {
|
|
|
|
return top.instance;
|
|
|
|
}
|
2015-11-03 22:17:14 +00:00
|
|
|
};
|