From 19d2aa54326b46cf167ad3f137abcc04c20e8323 Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Mon, 16 Sep 2013 19:11:58 -0500 Subject: [PATCH] Basic NavController with Tests --- hacking/NavBar.js | 9 +++++++ hacking/NavController.js | 49 +++++++++++++++++++++++++++++++++++ hacking/NavController.unit.js | 48 ++++++++++++++++++++++++++++++++++ hacking/TabBar.js | 2 +- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 hacking/NavBar.js create mode 100644 hacking/NavController.js create mode 100644 hacking/NavController.unit.js diff --git a/hacking/NavBar.js b/hacking/NavBar.js new file mode 100644 index 0000000000..f82ff447f2 --- /dev/null +++ b/hacking/NavBar.js @@ -0,0 +1,9 @@ +(function(window, document, ionic) { + + NavBar = function() { + }; + + NavBar.prototype = { + }; +})(this, document, ion = this.ionic || {}); + diff --git a/hacking/NavController.js b/hacking/NavController.js new file mode 100644 index 0000000000..a0d005177c --- /dev/null +++ b/hacking/NavController.js @@ -0,0 +1,49 @@ +(function(window, document, ionic) { + NavController = function(opts) { + this.navBar = opts.navBar; + this.controllers = opts.controllers || []; + }; + + NavController.prototype = { + getControllers: function() { + return this.controllers; + }, + getTopController: function() { + return this.topController; + }, + push: function(controller) { + this.controllers.push(controller); + + // Indicate we are switching controllers + var shouldSwitch = this.switchingController && this.switchingController(controller) || true; + + // Return if navigation cancelled + if(shouldSwitch === false) + return; + + // Actually switch the active controllers + + // Grab the top controller on the stack + var next = this.controllers[this.controllers.length - 1]; + // Switch to it (TODO: Animate or such things here) + this.topController = next; + + return controller; + }, + + + pop: function() { + var next, last; + + // Grab the controller behind the top one on the stack + last = this.controllers.pop(); + + next = this.controllers[this.controllers.length - 1]; + + // Switch to it (TODO: Animate or such things here) + this.topController = next; + + return last; + } + }; +})(this, document, this.ionic || {}); diff --git a/hacking/NavController.unit.js b/hacking/NavController.unit.js new file mode 100644 index 0000000000..6373c96fb0 --- /dev/null +++ b/hacking/NavController.unit.js @@ -0,0 +1,48 @@ +describe('NavController', function() { + var ctrl; + + beforeEach(function() { + ctrl = new NavController({ + navBar: new NavBar() + }); + }); + + it('Should load controllers', function() { + ctrl = new NavController({ + controllers: [{}] + }); + expect(ctrl.getControllers().length).toEqual(1); + }); + + it('Should push controller', function() { + ctrl.push({ + title: 'Page 1' + }); + expect(ctrl.getControllers().length).toEqual(1); + ctrl.push({ + title: 'Page 2' + }); + expect(ctrl.getControllers().length).toEqual(2); + var last = ctrl.pop(); + expect(ctrl.getControllers().length).toEqual(1); + expect(last.title).toEqual('Page 2'); + }); + + it('Should change top view controller', function() { + expect(ctrl.getTopController()).toBe(undefined); + var c1 = { + title: 'Page 1' + }; + var c2 = { + title: 'Page 2' + }; + ctrl.push(c1); + expect(ctrl.getTopController()).toEqual(c1); + ctrl.push(c2); + expect(ctrl.getTopController()).toEqual(c2); + ctrl.pop(); + expect(ctrl.getTopController()).toEqual(c1); + ctrl.pop(); + expect(ctrl.getTopController()).toEqual(undefined); + }); +}); diff --git a/hacking/TabBar.js b/hacking/TabBar.js index 9df4a0d913..c37d2433f3 100644 --- a/hacking/TabBar.js +++ b/hacking/TabBar.js @@ -186,4 +186,4 @@ TabBar.prototype = { } }; -})(this, document, ion = this.ionic || {}); +})(this, document, this.ionic || {});