mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-06 22:29:44 +08:00
158 lines
3.4 KiB
JavaScript
158 lines
3.4 KiB
JavaScript
(function(window, document, ionic) {
|
|
|
|
TabBarItem = function(el) {
|
|
this.el = el;
|
|
|
|
this._buildItem();
|
|
};
|
|
TabBarItem.prototype = {
|
|
_buildItem: function() {
|
|
var _this = this, child, children = Array.prototype.slice.call(this.el.children);
|
|
|
|
for(var i = 0, j = children.length; i < j; i++) {
|
|
child = children[i];
|
|
|
|
// Test if this is a "i" tag with icon in the class name
|
|
// TODO: This heuristic might not be sufficient
|
|
if(child.tagName.toLowerCase() == 'i' && /icon/.test(child.className)) {
|
|
this.icon = child.className;
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
// Set the title to the text content of the tab.
|
|
this.title = this.el.innerText.trim();
|
|
|
|
this._tapHandler = function(e) {
|
|
_this.onTap && _this.onTap(e);
|
|
};
|
|
|
|
ionic.on('tap', this._tapHandler, this.el);
|
|
},
|
|
|
|
onTap: function() {
|
|
},
|
|
|
|
// Remove the event listeners from this object
|
|
destroy: function() {
|
|
ionic.off('tap', this._tapHandler, this.el);
|
|
},
|
|
|
|
getIcon: function() {
|
|
return this.icon;
|
|
},
|
|
|
|
getTitle: function() {
|
|
return this.title;
|
|
},
|
|
|
|
setSelected: function(isSelected) {
|
|
this.isSelected = isSelected;
|
|
if(isSelected) {
|
|
this.el.classList.add('active');
|
|
} else {
|
|
this.el.classList.remove('active');
|
|
}
|
|
}
|
|
};
|
|
|
|
TabBar = function(opts) {
|
|
this.el = opts.el;
|
|
|
|
this.items = [];
|
|
|
|
this._buildItems();
|
|
};
|
|
|
|
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.onTap = undefined;
|
|
item.destroy();
|
|
},
|
|
|
|
_bindEventsOnItem: function(item) {
|
|
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];
|
|
|
|
// Deselect all
|
|
for(var i = 0, j = this.items.length; i < j; i += 1) {
|
|
this.items[i].setSelected(false);
|
|
}
|
|
|
|
// Select the new item
|
|
this.selectedItem && this.selectedItem.setSelected(true);
|
|
},
|
|
|
|
// 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);
|
|
|
|
for(var i = 0, j = items.length; i < j; i += 1) {
|
|
item = new TabBarItem(items[i]);
|
|
this.items[i] = item;
|
|
this._bindEventsOnItem(item);
|
|
}
|
|
|
|
if(this.items.length > 0) {
|
|
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;
|
|
}
|
|
};
|
|
|
|
})(this, document, ion = this.ionic || {});
|