diff --git a/core/menu_module.js b/core/menu_module.js index a563e48a..3b2c0fbd 100644 --- a/core/menu_module.js +++ b/core/menu_module.js @@ -216,6 +216,18 @@ MenuModule.prototype.restoreSavedState = function(savedState) { // nothing in base }; +MenuModule.prototype.nextMenu = function(cb) { + self.client.menuStack.next(cb); +}; + +MenuModule.prototype.prevMenu = function(cb) { + self.client.menuStack.prev(cb); +}; + +MenuModule.prototype.gotoMenu = function(name, options, cb) { + self.client.menuStack.goto(name, options, cb); +}; + MenuModule.prototype.leave = function() { this.detachViewControllers(); }; diff --git a/core/menu_stack.js b/core/menu_stack.js index 8ea815f6..eca1e8ad 100644 --- a/core/menu_stack.js +++ b/core/menu_stack.js @@ -2,7 +2,7 @@ 'use strict'; // ENiGMA½ -var menuUtil = require('./menu_util.js'); +var loadMenu = require('./menu_util.js').loadMenu; var _ = require('lodash'); @@ -20,6 +20,9 @@ MenuModule prevMenu() */ +// :TODO: Clean up client attach/detach/etc. +// :TODO: Cleanup up client currentMenuModule related stuff (all over!). Make this a property that returns .menuStack.getCurrentModule() + module.exports = MenuStack; function MenuStack(client) { @@ -37,22 +40,18 @@ function MenuStack(client) { }; this.top = function() { - return self.stack[self.stack.length - 1]; + if(self.stackSize() > 0) { + return self.stack[self.stack.length - 1]; + } }; + + this.stackSize = function() { + return self.stack.length; + } } MenuStack.prototype.next = function(cb) { - var currentModuleInfo = this.menuStack.top(); - - /* - { - instance : modInst, - menuConfig : {}, - extraArgs : {} - name : 'menuName', - savedState : {} - } - */ + var currentModuleInfo = this.top(); if(!_.isString(currentModuleInfo.menuConfig.next)) { this.log.error('No \'next\' member in menu config!'); @@ -67,8 +66,14 @@ MenuStack.prototype.next = function(cb) { this.goto(current.menuConfig.next, { }, cb); }; -MenuStack.prototype.prev = function() { - +MenuStack.prototype.prev = function(cb) { + var previousModuleInfo = this.pop(); + + if(previousModuleInfo) { + this.goto(previousModuleInfo.name, { extraArgs : previousModuleInfo.extraArgs, savedState : previousModuleInfo.savedState }, cb); + } else { + cb(new Error('No previous menu available!')); + } }; MenuStack.prototype.goto = function(name, options, cb) { @@ -76,7 +81,7 @@ MenuStack.prototype.goto = function(name, options, cb) { var self = this; - if(name === currentModuleInfo.name) { + if(currentModuleInfo && name === currentModuleInfo.name) { var err = new Error('Already at supplied menu!'); self.client.log.warn( { menuName : name, error : err.toString() }, 'Cannot go to menu'); @@ -93,22 +98,30 @@ MenuStack.prototype.goto = function(name, options, cb) { extraArgs : options.extraArgs, }; - menuUtil.loadMenu(loadOpts, function menuLoaded(err, modInst) { + loadMenu(loadOpts, function menuLoaded(err, modInst) { if(err) { var errCb = cb || self.defaultHandlerMissingMod(); errCb(err); } else { - self.client.detachCurrentMenuModule(); - self.client.log.debug( { menuName : name }, 'Goto menu module'); - var modInfo = { + if(currentModuleInfo) { + // save stack state + currentModuleInfo.savedState = currentModuleInfo.instance.getSaveState(); + + currentModuleInfo.instance.leave(); + } + + self.push( { name : name, instance : modInst, extraArgs : options.extraArgs, - }; + }); - self.push(modInfo); + // restore previous state if requested + if(options.savedState) { + modInst.restoreSavedState(options.savedState); + } modInst.enter(self.client);