forked from elboletaire/tabbedcontent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.tabbedcontent.min.js
3 lines (3 loc) · 2.06 KB
/
jquery.tabbedcontent.min.js
1
2
3
/* Tabs plugin for jQuery created by Òscar Casajuana < elboletaire at underave dot net >
* Copyright 2013 Òscar Casajuana (Apache License v.2.0) */
(function($,g,h,i){"use strict";var j=function(c,d){var f={links:c.prev().find('a').length?c.prev().find('a'):'.tabs a',errorSelector:'.error-message',speed:false,onSwitch:false,onInit:false,currentClass:'current'},firstTime=true,children=c.children(),history=h.history,loc=g.location;d=$.extend(f,d);if(!d.links.version){d.links=$(d.links)}function tabExists(a){return children.filter(a).length?true:false}function getTabId(a){if(a.toString().match(/^[0-9]$/)){return'#'+children.eq(a).attr('id')}return'#'+a}function onSwitch(a){if(firstTime&&history!==i&&('pushState'in history)){firstTime=false;h.setTimeout(function(){history.replaceState(null,'',a)},100)}if(d.onSwitch&&typeof d.onSwitch==='function'){d.onSwitch(a)}}function switchTab(a,b){if(!a.toString().match(/^#/)){a=getTabId(a)}if(!tabExists(a)){return false}d.links.removeClass(d.currentClass);d.links.filter('a[href='+a+']').addClass(d.currentClass);children.hide();if(b){if(history!==i&&('pushState'in history)){history.pushState(null,'',a)}else{h.location.hash=a}}children.filter(a).show(d.speed,function(){if(d.speed){onSwitch(a)}});if(!d.speed){onSwitch(a)}return true}function apiSwitch(a){return switchTab(a,true)}function hashSwitch(e){if(tabExists(loc.hash)){switchTab(loc.hash)}}function init(){if(tabExists(loc.hash)){switchTab(loc.hash)}else if(d.errorSelector&&children.find(d.errorSelector).length){children.each(function(){if($(this).find(d.errorSelector).length){switchTab("#"+$(this).attr("id"));return false}})}else{switchTab("#"+c.children(":first-child").attr("id"))}if('onhashchange'in h){$(h).bind('hashchange',hashSwitch)}else{var a=loc.href;h.setInterval(function(){if(a!==loc.href){hashSwitch.call(h.event);a=loc.href}},100)}if(d.onInit&&typeof d.onInit==='function'){d.onInit()}}init();return{'switch':apiSwitch,'switchTab':apiSwitch}};$.fn.tabbedContent=function(b){return this.each(function(){var a=new j($(this),b);$(this).data('api',a)})}})(jQuery,document,window);