From 3fd6b4163669225a2c5127eb7093078036a74681 Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Fri, 13 Sep 2013 16:57:37 -0500 Subject: [PATCH] Tab bar selection works --- hacking/TabBar.js | 57 ++++++++++++++++++++++--------- hacking/TabBar.unit.js | 67 ++++++++++++++++++++++++++++++------- hacking/TabBarController.js | 4 +++ hacking/tabs.html | 52 ++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 27 deletions(-) create mode 100644 hacking/tabs.html diff --git a/hacking/TabBar.js b/hacking/TabBar.js index f045506ab9..b63cb60606 100644 --- a/hacking/TabBar.js +++ b/hacking/TabBar.js @@ -14,8 +14,8 @@ TabBarItem.prototype = { // Test if this is a "i" tag with icon in the class name // TODO: This heuristic might not be sufficient - if(child.tagName == 'i' && /icon/.test(child.className)) { - this.icon = child; + if(child.tagName.toLowerCase() == 'i' && /icon/.test(child.className)) { + this.icon = child.className; break; } @@ -25,19 +25,17 @@ TabBarItem.prototype = { this.title = this.el.innerText.trim(); this._tapHandler = function(e) { - console.log('TAP', e); - _this.onTap(); + _this.onTap && _this.onTap(e); }; - ionic.on('click', this._tapHandler, this.el); ionic.on('tap', this._tapHandler, this.el); }, - onTap: function() {}, + onTap: function() { + }, // Remove the event listeners from this object destroy: function() { - ionic.off('click', this._tapHandler, this.el); ionic.off('tap', this._tapHandler, this.el); }, @@ -68,32 +66,45 @@ TabBar = function(opts) { }; TabBar.prototype = { + // get all the items for the TabBar + getItems: function() { + return this.items; + }, + + // Add an item to the tab bar addItem: function(item) { this.items.push(item); this._bindEventsOnItem(item); }, + // Remove an item from the tab bar removeItem: function(index) { var item = this.items[index]; if(!item) { return; } - item.onSelect = undefined; + item.onTap = undefined; item.destroy(); }, - _itemActivateHandler: function(e) { - }, - _bindEventsOnItem: function(item) { - item.onSelect = function() { - console.log(item.title + ' selected!'); + var _this = this; + + if(!this._itemTapHandler) { + this._itemTapHandler = function(e) { + console.log('TAB HANDERL', e, this); + _this.selectItem(this); + }; } + item.onTap = this._itemTapHandler; }, + // Get the currently selected item getSelectedItem: function() { return this.selectedItem; }, + + // Set the currently selected item by index setSelectedItem: function(index) { this.selectedItem = this.items[index]; @@ -106,10 +117,18 @@ TabBar.prototype = { this.selectedItem && this.selectedItem.setSelected(true); }, - getItems: function() { - return this.items; + // Select the given item assuming we can find it in our + // item list. + selectItem: function(item) { + for(var i = 0, j = this.items.length; i < j; i += 1) { + if(this.items[i] == item) { + this.setSelectedItem(i); + return; + } + } }, + // Build the initial items list from the given DOM node. _buildItems: function() { var item, items = Array.prototype.slice.call(this.el.children); @@ -124,6 +143,14 @@ TabBar.prototype = { this.selectedItem = this.items[0]; } + }, + + // Destroy this tab bar + destroy: function() { + for(var i = 0, j = this.items.length; i < j; i += 1) { + this.items[i].destroy(); + } + this.items.length = 0; } }; diff --git a/hacking/TabBar.unit.js b/hacking/TabBar.unit.js index 814c3d7b23..71a8bfea7f 100644 --- a/hacking/TabBar.unit.js +++ b/hacking/TabBar.unit.js @@ -11,11 +11,9 @@ describe('TabBar view', function() { el: element.get(0) }); + items = tabBar.getItems(); }); it('Should read tabs', function() { - - items = tabBar.getItems(); - expect(items.length).toEqual(3); expect(items[0].getTitle()).toEqual('Tab 1'); expect(items[1].getTitle()).toEqual('Tab 2'); @@ -29,8 +27,6 @@ describe('TabBar view', function() { }); it('Should select', function() { - items = tabBar.getItems(); - // Track selection object tabBar.setSelectedItem(1); expect(tabBar.getSelectedItem().getTitle()).toEqual('Tab 2'); @@ -53,14 +49,24 @@ describe('TabBar view', function() { expect(items[2].el.classList.contains('active')).toEqual(false); }); - it('Should set item icon', function() { - items = tabBar.getItems(); - - var item = items[0]; - expect(item.getIcon()).toBe(undefined); - }); it('Should handle item click event', function() { + debugger; + var item = items[0]; + spyOn(item, 'onTap'); + spyOn(tabBar, '_itemTapHandler'); + + var event = new CustomEvent('tap', { + target: item.el + }); + item.el.dispatchEvent(event); + + expect(item.onTap).toHaveBeenCalled(); + expect(tabBar._itemTapHandler).toHaveBeenCalled(); + }); + + + it('Should unbind item on destroy', function() { var item = items[0]; spyOn(item, 'onTap'); @@ -70,8 +76,45 @@ describe('TabBar view', function() { item.el.dispatchEvent(event); expect(item.onTap).toHaveBeenCalled(); - }); + item.onTap.isSpy = false; + spyOn(item, 'onTap'); + + tabBar.destroy(); + + var event = new CustomEvent('tap', { + target: item.el + }); + item.el.dispatchEvent(event); + + expect(item.onTap).not.toHaveBeenCalled(); + }); +}); + +describe('TabBarItem view', function() { + var element, tabBar, items; + + beforeEach(function() { + element = $('
' + + ' Tab 1' + + 'Tab 2' + + 'Tab 3'); + + tabBar = new TabBar({ + el: element.get(0) + }); + + items = tabBar.getItems(); + }); + it('Should set item title', function() { + var item = items[0]; + expect(item.getTitle()).toBe('Tab 1'); + }); + it('Should set item icon', function() { + + var item = items[0]; + expect(item.getIcon()).toBe('icon-home'); + }); it('Should unbind item on destroy', function() { var item = items[0]; spyOn(item, 'onTap'); diff --git a/hacking/TabBarController.js b/hacking/TabBarController.js index c8de85716f..bdbe00fb7d 100644 --- a/hacking/TabBarController.js +++ b/hacking/TabBarController.js @@ -1,3 +1,5 @@ +(function(window, document, ionic) { + TabBarController = function(options) { this.tabBar = options.tabBar; @@ -77,3 +79,5 @@ TabBarController.prototype = { this.selectController(0); } } + +})(this, document, ion = this.ionic || {}); diff --git a/hacking/tabs.html b/hacking/tabs.html new file mode 100644 index 0000000000..9393e5276a --- /dev/null +++ b/hacking/tabs.html @@ -0,0 +1,52 @@ + + + + Tab Bars + + + + + + + + +
+ +
+

Tab Bars

+
+ +
+ +
+ +
+ + + + + + + +