From 5e009172978df1391aa07bdfbcf59039b3befc1d Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Fri, 13 Sep 2013 17:55:44 -0500 Subject: [PATCH] TabBarController works! --- hacking/TabBar.js | 22 ++++++++++++++++++-- hacking/TabBarController.js | 35 ++++++++++++++++++++++++++++++-- hacking/TabBarController.unit.js | 10 ++++++++- hacking/tabs.html | 35 +++++++++++++++++++++++++++++--- 4 files changed, 94 insertions(+), 8 deletions(-) diff --git a/hacking/TabBar.js b/hacking/TabBar.js index 705ec6dde8..a043224279 100644 --- a/hacking/TabBar.js +++ b/hacking/TabBar.js @@ -6,6 +6,22 @@ TabBarItem = function(el) { this._buildItem(); }; TabBarItem.prototype = { + // Factory for creating an item from a given javascript object + create: function(itemData) { + var item = document.createElement('a'); + item.className = 'tab-item'; + + if(itemData.icon) { + var icon = document.createElement('i'); + icon.className = itemData.icon; + item.appendChild(icon); + } + item.appendChild(document.createTextNode(itemData.title)); + + return new TabBarItem(item); + }, + + _buildItem: function() { var _this = this, child, children = Array.prototype.slice.call(this.el.children); @@ -30,7 +46,6 @@ TabBarItem.prototype = { ionic.on('tap', this._tapHandler, this.el); }, - onTap: function(e) { console.log('On tap'); }, @@ -114,7 +129,10 @@ TabBar.prototype = { } // Select the new item - this.selectedItem && this.selectedItem.setSelected(true); + if(this.selectedItem) { + this.selectedItem.setSelected(true); + this.onTabSelected && this.onTabSelected(this.selectedItem, index); + } }, // Select the given item assuming we can find it in our diff --git a/hacking/TabBarController.js b/hacking/TabBarController.js index bdbe00fb7d..fc99fb6297 100644 --- a/hacking/TabBarController.js +++ b/hacking/TabBarController.js @@ -5,20 +5,26 @@ TabBarController = function(options) { this._bindEvents(); - this.controllers = []; + this.controllers = options.controllers || []; // Bind or set our tabWillChange callback this.controllerWillChange = options.controllerWillChange || function(controller) {}; this.controllerChanged = options.controllerChanged || function(controller) {}; + + this.setSelectedController(0); }; TabBarController.prototype = { // Start listening for events on our tab bar _bindEvents: function() { - this.tabBar.onTabSelected = function(e) { + var _this = this; + + this.tabBar.onTabSelected = function(item, index) { + _this.setSelectedController(index); }; }, + selectController: function(index) { var shouldChange = true; @@ -38,8 +44,26 @@ TabBarController.prototype = { // Force the selection of a controller at the given index setSelectedController: function(index) { + if(index >= this.controllers.length) { + return; + } this.selectedController = this.controllers[index]; this.selectedIndex = index; + + this._showController(index); + this.tabBar.setSelectedItem(index); + }, + + _showController: function(index) { + var c; + + for(var i = 0, j = this.controllers.length; i < j; i ++) { + c = this.controllers[i]; + c.el.style.display = 'none'; + } + + c = this.controllers[index]; + c.el.style.display = 'block'; }, _clearSelected: function() { @@ -66,6 +90,13 @@ TabBarController.prototype = { addController: function(controller) { this.controllers.push(controller); + var item = TabBarItem.prototype.create({ + title: controller.title, + icon: controller.icon + }); + + this.tabBar.addItem(item); + // If we don't have a selected controller yet, select the first one. if(!this.selectedController) { this.setSelectedController(0); diff --git a/hacking/TabBarController.unit.js b/hacking/TabBarController.unit.js index 8f6b240390..6767744653 100644 --- a/hacking/TabBarController.unit.js +++ b/hacking/TabBarController.unit.js @@ -10,10 +10,18 @@ describe('TabBarController', function() { it('Should add Controllers', function() { ctrl.addController({ - title: 'Item 1' + title: 'Item 1', + icon: 'icon-home' }); expect(ctrl.getController(0).title).toEqual('Item 1'); + + var items = ctrl.tabBar.getItems(); + expect(items.length).toEqual(1); + + expect(items[0].getTitle()).toEqual('Item 1'); + expect(items[0].getIcon()).toEqual('icon-home'); + }); it('Should set Controllers', function() { diff --git a/hacking/tabs.html b/hacking/tabs.html index 9393e5276a..171e8e525f 100644 --- a/hacking/tabs.html +++ b/hacking/tabs.html @@ -16,8 +16,31 @@

Tab Bars

-
- +
+
+

Tab 1

+

+ Friends +

+
+
+

Tab 2

+

+ Friends +

+
+
+

Tab 3

+

+ Friends +

+
+
+

Tab 4

+

+ Friends +

+