Skip to content

Instantly share code, notes, and snippets.

@meeech
Created August 13, 2012 18:02
Show Gist options
  • Save meeech/3342781 to your computer and use it in GitHub Desktop.
Save meeech/3342781 to your computer and use it in GitHub Desktop.
nav stack
//navGroup Wrapper to be able to implement home.
//Track stack of open windows
var _navGroup, //Original
currentStack = [],
uuid = require('lib/util/uuid');
var create = function(args) {
if (Ti.Platform.osname != 'android') {
_navGroup = Ti.UI.iPhone.createNavigationGroup(args);
} else {
_navGroup = require('lib/navGroupStub');
_navGroup.firstWindow = args.window;
}
};
exports.create = create;
var flush = function() {
currentStack = [];
};
//Return the iOS NavGroup Instance
var get = function() {
return _navGroup;
};
exports.get = get;
//Return to top level quick quick.
var home = function() {
currentStack.forEach(function(item) {
_navGroup.close(item, {animated: false});
});
flush();
};
exports.home = home;
//Used to close all windows except stackId.
//You would want to use this AFTER calling navGroup open, so you have the stackId
//note: When Closing windows this way, close handler doesnt fire EXCEPT on the
//visible window.
var closeExcept = function(win) {
currentStack.forEach(function(item) {
if(item._stackId != win._stackId) {
_navGroup.close(item, {animated: false});
} else {
win.backButtonTitle = 'Home';
}
});
currentStack = [win];
};
exports.closeExcept = closeExcept;
//Remove self from stack, and remove closeHandler
var closeHandler = function(e) {
Ti.API.debug('NavGroup Window Close Handler');
e.source.removeEventListener('close', closeHandler);
currentStack.some(function(item, index) {
if(item._stackId == e.source._stackId) {
currentStack.splice(index, 1);
return true;
}
});
};
//Open a window
//@return string _stackId: UUID of window on the stack.
var open = function(win, args) {
//patch for droid to keep same pattern of navgroup.open()
win = win || _navGroup.firstWindow;
Ti.API.debug('navGroupOpen');
args = args || {};
//Fix backButtonText
//We only set it to Home when we call closeExcept()
win.backButtonTitle = 'Back';
win._stackId = win._stackId || uuid();
_navGroup.open(win,args);
win.addEventListener('close', closeHandler);
currentStack.push(win);
return win._stackId;
};
exports.open = open;
var close = function(args) {
Ti.API.debug('navGroup Close');
_navGroup.close(args);
};
exports.close = close;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment