jquery-smarttab
Version:
The flexible tab control plugin for jQuery
12 lines • 11.8 kB
JavaScript
"use strict";function _classCallCheck(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,n){for(var i=0;i<n.length;i++){var e=n[i];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}function _createClass(t,n,i){return n&&_defineProperties(t.prototype,n),i&&_defineProperties(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}
/*!
* jQuery SmartTab v4.0.2
* The flexible tab control plugin for jQuery
* http://www.techlaboratory.net/jquery-smarttab
*
* Created by Dipu Raj (http://dipu.me)
*
* Licensed under the terms of the MIT License
* https://github.com/techlab/jquery-smarttab/blob/master/LICENSE
*/
!function(i){"function"==typeof define&&define.amd?define(["jquery"],i):"object"===("undefined"==typeof module?"undefined":_typeof(module))&&module.exports?module.exports=function(t,n){return void 0===n&&(n="undefined"!=typeof window?require("jquery"):require("jquery")(t)),i(n),n}:i(jQuery)}(function(u){var s={selected:0,theme:"basic",justified:!0,autoAdjustHeight:!0,backButtonSupport:!0,enableUrlHash:!0,transition:{animation:"none",speed:"400",easing:"",prefixCss:"",fwdShowCss:"",fwdHideCss:"",bckShowCss:"",bckHideCss:""},autoProgress:{enabled:!1,interval:3500,stopOnFocus:!0},keyboard:{keyNavigation:!0,keyLeft:[37,38],keyRight:[39,40],keyHome:[36],keyEnd:[35]},style:{mainCss:"st",navCss:"nav",navLinkCss:"nav-link",contentCss:"tab-content",contentPanelCss:"tab-pane",themePrefixCss:"st-theme-",justifiedCss:"st-justified",anchorDefaultCss:"default",anchorActiveCss:"active",loaderCss:"st-loading"},getContent:null},i=function(){function e(t,n){var i=this;_classCallCheck(this,e),this.options=u.extend(!0,{},s,n),this.main=u(t),this.nav=this._getFirstDescendant("."+this.options.style.navCss),this.container=this._getFirstDescendant("."+this.options.style.contentCss),this.tabs=this.nav.find("."+this.options.style.navLinkCss),this.pages=this.container.children("."+this.options.style.contentPanelCss),this.current_index=-1,this.autoProgressId=null,this.is_init=!1,this._init(),setTimeout(function(){i._load()},0)}return _createClass(e,[{key:"_init",value:function(){if(this._setElements(),!0===this.is_init)return!0;this._setEvents(),this.is_init=!0,this._triggerEvent("initialized")}},{key:"_load",value:function(){this.pages.hide(),this.tabs.removeClass(this.options.style.anchorActiveCss),this.current_index=-1;var t=(t=this._getURLHashIndex())||this.options.selected;this._showTab(t),this._triggerEvent("loaded")}},{key:"_getFirstDescendant",value:function(i){var e=this.main.children(i);return 0<e.length?e:(this.main.children().each(function(t,n){n=u(n).children(i);if(0<n.length)return e=n,!1}),0<e.length?e:(this._showError("Element not found "+i),!1))}},{key:"_setElements",value:function(){var i=this;this.main.removeClass(function(t,n){return(n.match(new RegExp("(^|\\s)"+i.options.style.themePrefixCss+"\\S+","g"))||[]).join(" ")}).addClass(this.options.style.mainCss+" "+this.options.style.themePrefixCss+this.options.theme),this.main.toggleClass(this.options.style.justifiedCss,this.options.justified)}},{key:"_setEvents",value:function(){var i=this;this.tabs.on("click",function(t){t.preventDefault();t=u(t.currentTarget);i._isShowable(t)&&i._showTab(i.tabs.index(t))}),this.options.autoProgress.enabled&&this.options.autoProgress.stopOnFocus&&(u(this.main).on("mouseover",function(t){t.preventDefault(),i._stopAutoProgress()}),u(this.main).on("mouseleave",function(t){t.preventDefault(),i._startAutoProgress()})),u(document).keyup(function(t){i._keyNav(t)}),u(window).on("hashchange",function(t){var n;!0===i.options.backButtonSupport&&(n=i._getURLHashIndex())&&i._isShowable(i.tabs.eq(n))&&(t.preventDefault(),i._showTab(n))}),u(window).on("resize",function(t){i._fixHeight(i.current_index)})}},{key:"_navigate",value:function(t){"first"==t?this._showTab(this._getShowable(-1,"next")):"last"==t?this._showTab(this._getShowable(this.tabs.length,"prev")):this._showTab(this._getShowable(this.current_index,t))}},{key:"_showTab",value:function(e){var s,o=this;return-1!==e&&null!==e&&(e!=this.current_index&&(!!this.tabs.eq(e)&&(!!this._isEnabled(this.tabs.eq(e))&&(s=this._getStepDirection(e),(-1===this.current_index||!1!==this._triggerEvent("leaveTab",[this._getAnchor(this.current_index),this.current_index,e,s]))&&void this._loadContent(e,function(){var t=o._getAnchor(e),n=(o._setURLHash(t.attr("href")),o._setAnchor(e),o._getPage(o.current_index)),i=o._getPage(e);o._transit(i,n,s,function(){o._fixHeight(e),o._triggerEvent("showTab",[t,e,o._getStepPosition(e)])}),o.current_index=e})))))}},{key:"_getShowable",value:function(i,e){var s=this,o=null;return("prev"==e?u(this.tabs.slice(0,i).get().reverse()):this.tabs.slice(i+1)).each(function(t,n){if(s._isEnabled(u(n)))return o="prev"==e?i-(t+1):t+i+1,!1}),o}},{key:"_isShowable",value:function(t){return!!this._isEnabled(t)}},{key:"_isEnabled",value:function(t){return!t.hasClass(this.options.style.anchorDisabledCss)&&!t.hasClass(this.options.style.anchorHiddenCss)}},{key:"_getStepDirection",value:function(t){return this.current_index<t?"forward":"backward"}},{key:"_getStepPosition",value:function(t){return 0===t?"first":t===this.tabs.length-1?"last":"middle"}},{key:"_getAnchor",value:function(t){return null==t||-1==t?null:this.tabs.eq(t)}},{key:"_getPage",value:function(t){return null==t||-1==t?null:this.pages.eq(t)}},{key:"_loadContent",value:function(t,n){var i,e,s,o;u.isFunction(this.options.getContent)&&(i=this._getPage(t))?(e=this._getStepDirection(t),s=this._getStepPosition(t),o=this._getAnchor(t),this.options.getContent(t,e,s,o,function(t){t&&i.html(t),n()})):n()}},{key:"_transit",value:function(n,i,t,e){var s=u.fn.smartTab.transitions[this.options.transition.animation];this._stopAnimations(),u.isFunction(s)?s(n,i,t,this,function(t){!1===t&&(null!==i&&i.hide(),n.show()),e()}):(null!==i&&i.hide(),n.show(),e())}},{key:"_stopAnimations",value:function(){u.isFunction(this.container.finish)&&(this.pages.finish(),this.container.finish())}},{key:"_fixHeight",value:function(t){!1===this.options.autoAdjustHeight?this.container.css("height","auto"):(t=this._getPage(t).outerHeight(),u.isFunction(this.container.finish)&&u.isFunction(this.container.animate)&&0<t?this.container.finish().animate({height:t},this.options.transition.speed):this.container.css({height:0<t?t:"auto"}))}},{key:"_setAnchor",value:function(t){null!==this.current_index&&0<=this.current_index&&this.tabs.eq(this.current_index).removeClass(this.options.style.anchorActiveCss),this.tabs.eq(t).addClass(this.options.style.anchorActiveCss)}},{key:"_keyNav",value:function(t){this.options.keyboard.keyNavigation&&(-1<u.inArray(t.which,this.options.keyboard.keyLeft)?(this._navigate("prev"),t.preventDefault()):-1<u.inArray(t.which,this.options.keyboard.keyRight)?(this._navigate("next"),t.preventDefault()):-1<u.inArray(t.which,this.options.keyboard.keyHome)?(this._navigate("first"),t.preventDefault()):-1<u.inArray(t.which,this.options.keyboard.keyEnd)&&(this._navigate("last"),t.preventDefault()))}},{key:"_triggerEvent",value:function(t,n){t=u.Event(t);return this.main.trigger(t,n),!t.isDefaultPrevented()&&t.result}},{key:"_setURLHash",value:function(t){this.options.enableUrlHash&&window.location.hash!==t&&history.pushState(null,null,t)}},{key:"_getURLHashIndex",value:function(){if(this.options.enableUrlHash){var t=window.location.hash;if(0<t.length){t=this.nav.find("a[href*='"+t+"']");if(0<t.length)return this.tabs.index(t)}}return!1}},{key:"_showError",value:function(t){console.error(t)}},{key:"_startAutoProgress",value:function(){var t=this;this.options.autoProgress.enabled&&!this.autoProgressId&&(this.autoProgressId=setInterval(function(){return t._navigate("next")},this.options.autoProgress.interval))}},{key:"_stopAutoProgress",value:function(){this.autoProgressId&&(clearInterval(this.autoProgressId),this.autoProgressId=null)}},{key:"_restartAutoProgress",value:function(){this._stopAutoProgress(),this._startAutoProgress()}},{key:"goToTab",value:function(t){this._isShowable(this.tabs.eq(t))&&this._showTab(t)}},{key:"next",value:function(){this._navigate("next")}},{key:"prev",value:function(){this._navigate("prev")}},{key:"first",value:function(){this._navigate("first")}},{key:"last",value:function(){this._navigate("last")}},{key:"reset",value:function(){this.tabs.removeClass(this.options.style.anchorActiveCss),this._setURLHash("#"),this._init(),this._load()}},{key:"setOptions",value:function(t){this.options=u.extend(!0,{},this.options,t),this._init()}},{key:"getOptions",value:function(){return this.options}},{key:"getInfo",value:function(){return{currentPage:this.current_index||0,totalPages:this.tabs?this.tabs.length:0}}},{key:"loader",value:function(t){this.main.toggleClass(this.options.style.loaderCss,"show"===t)}},{key:"fixHeight",value:function(){this._fixHeight(this.current_index)}}]),e}();u.fn.smartTab=function(t){return void 0===t||"object"===_typeof(t)?this.each(function(){u.data(this,"smartTab")||u.data(this,"smartTab",new i(this,t))}):"string"==typeof t&&"_"!==t[0]&&"init"!==t?(n=u.data(this[0],"smartTab"),"destroy"===t&&u.data(this,"smartTab",null),n instanceof i&&"function"==typeof n[t]?n[t].apply(n,Array.prototype.slice.call(arguments,1)):this):void 0;var n},u.fn.smartTab.transitions={fade:function(t,n,i,e,s){u.isFunction(t.fadeOut)?n?n.fadeOut(e.options.transition.speed,e.options.transition.easing,function(){t.fadeIn(e.options.transition.speed,e.options.transition.easing,function(){s()})}):t.fadeIn(e.options.transition.speed,e.options.transition.easing,function(){s()}):s(!1)},slideSwing:function(t,n,i,e,s){u.isFunction(t.slideDown)?n?n.slideUp(e.options.transition.speed,e.options.transition.easing,function(){t.slideDown(e.options.transition.speed,e.options.transition.easing,function(){s()})}):t.slideDown(e.options.transition.speed,e.options.transition.easing,function(){s()}):s(!1)},slideHorizontal:function(t,n,i,s,e){var o,a,r,h;u.isFunction(t.animate)?(o=function(t,n,i,e){t.css({position:"absolute",left:n}).show().animate({left:i},s.options.transition.speed,s.options.transition.easing,e)},-1==s.current_index&&s.container.height(t.outerHeight()),a=s.container.width(),n&&(r=n.css(["position","left"]),o(n,0,a*("backward"==i?1:-1),function(){n.hide().css(r)})),h=t.css(["position"]),o(t,a*("backward"==i?-2:1),0,function(){t.css(h),e()})):e(!1)},slideVertical:function(t,n,i,s,e){var o,a,r,h;u.isFunction(t.animate)?(o=function(t,n,i,e){t.css({position:"absolute",top:n}).show().animate({top:i},s.options.transition.speed,s.options.transition.easing,e)},-1==s.current_index&&s.container.height(t.outerHeight()),a=s.container.height(),n&&(r=n.css(["position","top"]),o(n,0,a*("backward"==i?-1:1),function(){n.hide().css(r)})),h=t.css(["position"]),o(t,a*("backward"==i?1:-2),0,function(){t.css(h),e()})):e(!1)},css:function(t,n,i,e,s){var o,a;0==e.options.transition.fwdHideCss.length||0==e.options.transition.bckHideCss.length?s(!1):(o=function(t,n,i){n&&0!=n.length||i(),t.addClass(n).one("animationend",function(t){u(t.currentTarget).removeClass(n),i()}),t.addClass(n).one("animationcancel",function(t){u(t.currentTarget).removeClass(n),i("cancel")})},a=e.options.transition.prefixCss+" "+("backward"==i?e.options.transition.bckShowCss:e.options.transition.fwdShowCss),n?(i=e.options.transition.prefixCss+" "+("backward"==i?e.options.transition.bckHideCss:e.options.transition.fwdHideCss),o(n,i,function(){n.hide(),o(t,a,function(){s()}),t.show()})):(o(t,a,function(){s()}),t.show()))}}});