cbpFWTabs.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ;( function( window ) {
  2. 'use strict';
  3. function extend( a, b ) {
  4. for( var key in b ) {
  5. if( b.hasOwnProperty( key ) ) {
  6. a[key] = b[key];
  7. }
  8. }
  9. return a;
  10. }
  11. function CBPFWTabs( el, options ) {
  12. this.el = el;
  13. this.options = extend( {}, this.options );
  14. extend( this.options, options );
  15. this._init();
  16. }
  17. CBPFWTabs.prototype.options = {
  18. start : 0
  19. };
  20. CBPFWTabs.prototype._init = function() {
  21. // tabs elems
  22. this.tabs = [].slice.call( this.el.querySelectorAll( 'nav > ul > li' ) );
  23. // content items
  24. this.items = [].slice.call( this.el.querySelectorAll( '.content-wrap > section' ) );
  25. // current index
  26. this.current = -1;
  27. // show current content item
  28. this._show();
  29. // init events
  30. this._initEvents();
  31. };
  32. CBPFWTabs.prototype._initEvents = function() {
  33. var self = this;
  34. this.tabs.forEach( function( tab, idx ) {
  35. tab.addEventListener( 'click', function( ev ) {
  36. ev.preventDefault();
  37. self._show( idx );
  38. } );
  39. } );
  40. };
  41. CBPFWTabs.prototype._show = function( idx ) {
  42. if( this.current >= 0 ) {
  43. this.tabs[ this.current ].className = this.items[ this.current ].className = '';
  44. }
  45. // change current
  46. this.current = idx != undefined ? idx : this.options.start >= 0 && this.options.start < this.items.length ? this.options.start : 0;
  47. $('video').trigger('pause');
  48. this.tabs[ this.current ].className = 'tab-current';
  49. this.items[ this.current ].className = 'content-current';
  50. };
  51. // add to global namespace
  52. window.CBPFWTabs = CBPFWTabs;
  53. })( window );
  54. (function() {
  55. [].slice.call( document.querySelectorAll( '.tabs' ) ).forEach( function( el ) {
  56. new CBPFWTabs( el );
  57. });
  58. })();