Skip to content

Instantly share code, notes, and snippets.

@pegli
Created October 31, 2011 16:08
Show Gist options
  • Save pegli/1327845 to your computer and use it in GitHub Desktop.
Save pegli/1327845 to your computer and use it in GitHub Desktop.
NavigationController with added pop() and close_all() methods
/**
* https://github.com/appcelerator-developer-relations/Forging-Titanium/tree/master/ep-002
* written by Kevin Whinnery, Appcelerator
*/
function dump(windowStack) {
var titles = [];
for (var i=0; i < windowStack.length; i++) {
var win = windowStack[i]
titles.push(win.title);
}
return JSON.stringify(titles);
}
exports.NavigationController = function() {
this.windowStack = [];
var that = this;
Ti.App.addEventListener('app:pop_window', function(e) {
that.pop();
});
};
exports.NavigationController.prototype.pop = function() {
Ti.API.info("before close: "+dump(this.windowStack));
var winToClose = this.windowStack && this.windowStack.length > 1 ? this.windowStack[this.windowStack.length - 1] : undefined;
if (winToClose) {
(this.navGroup) ? this.navGroup.close(winToClose) : winToClose.close();
}
};
exports.NavigationController.prototype.open = function(/*Ti.UI.Window*/windowToOpen) {
//add the window to the stack of windows managed by the controller
this.windowStack.push(windowToOpen);
//grab a copy of the current nav controller for use in the callback
var that = this;
windowToOpen.addEventListener('close', function(e) {
var popped = that.windowStack.pop();
popped = null;
Ti.API.info("after event: "+dump(that.windowStack));
});
//hack - setting this property ensures the window is "heavyweight" (associated with an Android activity)
windowToOpen.navBarHidden = windowToOpen.navBarHidden || false;
//This is the first window
if(this.windowStack.length === 1) {
if(Ti.Platform.osname === 'android') {
windowToOpen.exitOnClose = true;
windowToOpen.open();
} else {
this.navGroup = Ti.UI.iPhone.createNavigationGroup({
window : windowToOpen
});
var containerWindow = Ti.UI.createWindow();
containerWindow.add(this.navGroup);
containerWindow.open();
}
}
//All subsequent windows
else {
if(Ti.Platform.osname === 'android') {
windowToOpen.open();
} else {
this.navGroup.open(windowToOpen);
}
}
};
//go back to the initial window of the NavigationController
exports.NavigationController.prototype.home = function() {
while(this.windowStack.length > 1) {
var winToClose = this.windowStack.pop();
winToClose.popped = true; // prevents close() event listener from removing from the stack
(this.navGroup) ? this.navGroup.close(winToClose, { animated: false }) : winToClose.close({ animated: false });
}
};
// remove all windows from this controller
exports.NavigationController.prototype.close_all = function() {
while(this.windowStack.length > 0) {
var winToClose = this.windowStack.pop();
winToClose.popped = true; // prevents close() event listener from removing from the stack
(this.navGroup) ? this.navGroup.close(winToClose, { animated: false }) : winToClose.close({ animated: false });
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment