76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassYUI.add('tabview-base', function(Y) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glassvar getClassName = Y.ClassNameManager.getClassName,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass TABVIEW = 'tabview',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass TAB = 'tab',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass CONTENT = 'content',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass PANEL = 'panel',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass SELECTED = 'selected',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass EMPTY_OBJ = {},
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass DOT = '.',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass _classNames = {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabview: getClassName(TABVIEW),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabviewPanel: getClassName(TABVIEW, PANEL),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabviewList: getClassName(TABVIEW, 'list'),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tab: getClassName(TAB),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabLabel: getClassName(TAB, 'label'),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabPanel: getClassName(TAB, PANEL),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass selectedTab: getClassName(TAB, SELECTED),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass selectedPanel: getClassName(TAB, PANEL, SELECTED)
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass _queries = {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabview: DOT + _classNames.tabview,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabviewList: '> ul',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tab: '> ul > li',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabLabel: '> ul > li > a ',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabviewPanel: '> div',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabPanel: '> div > div',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass selectedTab: '> ul > ' + DOT + _classNames.selectedTab,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass selectedPanel: '> div ' + DOT + _classNames.selectedPanel
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass TabviewBase = function(config) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.init.apply(this, arguments);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass };
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassTabviewBase.NAME = 'tabviewBase';
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassTabviewBase._queries = _queries;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassTabviewBase._classNames = _classNames;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassY.mix(TabviewBase.prototype, {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass init: function(config) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass config = config || EMPTY_OBJ;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this._node = config.host || Y.one(config.node);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.refresh();
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass initClassNames: function(index) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass Y.Object.each(_queries, function(query, name) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // this === tabview._node
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (_classNames[name]) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass var result = this.all(query);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (index !== undefined) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass result = result.item(index);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (result) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass result.addClass(_classNames[name]);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }, this._node);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this._node.addClass(_classNames.tabview);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass _select: function(index) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass var node = this._node,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass oldItem = node.one(_queries.selectedTab),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass oldContent = node.one(_queries.selectedPanel),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass newItem = node.all(_queries.tab).item(index),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass newContent = node.all(_queries.tabPanel).item(index);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (oldItem) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass oldItem.removeClass(_classNames.selectedTab);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (oldContent) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass oldContent.removeClass(_classNames.selectedPanel);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (newItem) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass newItem.addClass(_classNames.selectedTab);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (newContent) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass newContent.addClass(_classNames.selectedPanel);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass initState: function() {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass var node = this._node,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass activeNode = node.one(_queries.selectedTab),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass activeIndex = activeNode ?
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass node.all(_queries.tab).indexOf(activeNode) : 0;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this._select(activeIndex);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // collapse extra space between list-items
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass _scrubTextNodes: function() {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this._node.one(_queries.tabviewList).get('childNodes').each(function(node) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (node.get('nodeType') === 3) { // text node
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass node.remove();
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass });
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // base renderer only enlivens existing markup
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass refresh: function() {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this._scrubTextNodes();
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.initClassNames();
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.initState();
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.initEvents();
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tabEventName: 'click',
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass initEvents: function() {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // TODO: detach prefix for delegate?
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // this._node.delegate('tabview|' + this.tabEventName),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this._node.delegate(this.tabEventName,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.onTabEvent,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass _queries.tab,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass );
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass onTabEvent: function(e) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass e.preventDefault();
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this._select(this._node.all(_queries.tab).indexOf(e.currentTarget));
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass destroy: function() {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this._node.detach(this.tabEventName);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass});
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassY.TabviewBase = TabviewBase;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass}, '@VERSION@' ,{requires:['node-event-delegate', 'classnamemanager', 'skin-sam-tabview']});