ES6 menu stack

This commit is contained in:
Bryan Ashby 2016-07-25 00:57:12 -06:00
parent 823dbc9466
commit ea0ec9ae4e
1 changed files with 127 additions and 130 deletions

View File

@ -2,93 +2,96 @@
'use strict'; 'use strict';
// ENiGMA½ // ENiGMA½
var loadMenu = require('./menu_util.js').loadMenu; const loadMenu = require('./menu_util.js').loadMenu;
var _ = require('lodash'); // deps
var assert = require('assert'); const _ = require('lodash');
const assert = require('assert');
module.exports = MenuStack; // :TODO: Stack is backwards.... top should be most recent! :)
function MenuStack(client) { module.exports = class MenuStack {
constructor(client) {
this.client = client; this.client = client;
this.stack = []; this.stack = [];
var self = this;
this.push = function(moduleInfo) {
return self.stack.push(moduleInfo);
};
this.pop = function() {
return self.stack.pop();
};
this.peekPrev = function() {
if(this.stackSize() > 1) {
return self.stack[self.stack.length - 2];
} }
};
this.top = function() { push(moduleInfo) {
if(self.stackSize() > 0) { return this.stack.push(moduleInfo);
return self.stack[self.stack.length - 1];
} }
};
this.stackSize = function() { pop() {
return self.stack.length; return this.stack.pop();
}; }
}
MenuStack.prototype.next = function(cb) { peekPrev() {
var currentModuleInfo = this.top(); if(this.stackSize > 1) {
return this.stack[this.stack.length - 2];
}
}
top() {
if(this.stackSize > 0) {
return this.stack[this.stack.length - 1];
}
}
get stackSize() {
return this.stack.length;
}
get currentModule() {
const top = this.top();
if(top) {
return top.instance;
}
}
next(cb) {
const currentModuleInfo = this.top();
assert(currentModuleInfo, 'Empty menu stack!'); assert(currentModuleInfo, 'Empty menu stack!');
var menuConfig = currentModuleInfo.instance.menuConfig; const menuConfig = currentModuleInfo.instance.menuConfig;
var next; let nextMenu;
if(_.isArray(menuConfig.next)) { if(_.isArray(menuConfig.next)) {
next = this.client.acs.getConditionalValue(menuConfig.next, 'next'); nextMenu = this.client.acs.getConditionalValue(menuConfig.next, 'next');
if(!next) { if(!nextMenu) {
cb(new Error('No matching condition for \'next\'!')); return cb(new Error('No matching condition for \'next\'!'));
return;
} }
} else if(_.isString(menuConfig.next)) { } else if(_.isString(menuConfig.next)) {
next = menuConfig.next; nextMenu = menuConfig.next;
} else { } else {
cb(new Error('Invalid or missing \'next\' member in menu config!')); return cb(new Error('Invalid or missing \'next\' member in menu config!'));
return;
} }
if(next === currentModuleInfo.name) { if(nextMenu === currentModuleInfo.name) {
cb(new Error('Menu config \'next\' specifies current menu!')); return cb(new Error('Menu config \'next\' specifies current menu!'));
return;
} }
this.goto(next, { }, cb); this.goto(nextMenu, { }, cb);
}; }
MenuStack.prototype.prev = function(cb) { prev(cb) {
// :TODO: leave() should really take a cb...
this.pop().instance.leave(); // leave & remove current this.pop().instance.leave(); // leave & remove current
const previousModuleInfo = this.pop(); // get previous const previousModuleInfo = this.pop(); // get previous
if(previousModuleInfo) { if(previousModuleInfo) {
this.goto( const opts = {
previousModuleInfo.name,
{
extraArgs : previousModuleInfo.extraArgs, extraArgs : previousModuleInfo.extraArgs,
savedState : previousModuleInfo.savedState savedState : previousModuleInfo.savedState
}, };
cb
);
} else {
cb(new Error('No previous menu available!'));
}
};
MenuStack.prototype.goto = function(name, options, cb) { return this.goto(previousModuleInfo.name, opts, cb);
var currentModuleInfo = this.top(); }
return cb(new Error('No previous menu available!'));
}
goto(name, options, cb) {
const currentModuleInfo = this.top();
if(!cb && _.isFunction(options)) { if(!cb && _.isFunction(options)) {
cb = options; cb = options;
@ -112,12 +115,12 @@ MenuStack.prototype.goto = function(name, options, cb) {
loadOpts.extraArgs = options.extraArgs; loadOpts.extraArgs = options.extraArgs;
} }
loadMenu(loadOpts, function menuLoaded(err, modInst) { loadMenu(loadOpts, (err, modInst) => {
if(err) { if(err) {
// :TODO: probably should just require a cb...
const errCb = cb || self.client.defaultHandlerMissingMod(); const errCb = cb || self.client.defaultHandlerMissingMod();
errCb(err); errCb(err);
} else { } else {
// :TODO: Move this log to caller
self.client.log.debug( { menuName : name }, 'Goto menu module'); self.client.log.debug( { menuName : name }, 'Goto menu module');
if(currentModuleInfo) { if(currentModuleInfo) {
@ -141,7 +144,7 @@ MenuStack.prototype.goto = function(name, options, cb) {
modInst.enter(); modInst.enter();
self.client.log.trace( self.client.log.trace(
{ stack : _.map(self.stack, function(si) { return si.name; } ) }, { stack : _.map(self.stack, stackEntry => stackEntry.name) },
'Updated menu stack'); 'Updated menu stack');
if(cb) { if(cb) {
@ -149,11 +152,5 @@ MenuStack.prototype.goto = function(name, options, cb) {
} }
} }
}); });
};
MenuStack.prototype.getCurrentModule = function() {
var top = this.top();
if(top) {
return top.instance;
} }
}; };