You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.5 KiB

  1. // Native Javascript for Bootstrap 3 | Tab
  2. // by dnp_theme
  3. (function(factory){
  4. // CommonJS/RequireJS and "native" compatibility
  5. if(typeof module !== "undefined" && typeof exports == "object") {
  6. // A commonJS/RequireJS environment
  7. if(typeof window != "undefined") {
  8. // Window and document exist, so return the factory's return value.
  9. module.exports = factory();
  10. } else {
  11. // Let the user give the factory a Window and Document.
  12. module.exports = factory;
  13. }
  14. } else {
  15. // Assume a traditional browser.
  16. window.Tab = factory();
  17. }
  18. })(function(){
  19. // TAB DEFINITION
  20. // ===================
  21. var Tab = function( element,options ) {
  22. options = options || {};
  23. this.tab = typeof element === 'object' ? element : document.querySelector(element);
  24. this.tabs = this.tab.parentNode.parentNode;
  25. this.dropdown = this.tabs.querySelector('.dropdown');
  26. if ( this.tabs.classList.contains('dropdown-menu') ) {
  27. this.dropdown = this.tabs.parentNode;
  28. this.tabs = this.tabs.parentNode.parentNode;
  29. }
  30. this.options = {};
  31. // default tab transition duration
  32. this.duration = 150;
  33. this.options.duration = document.documentElement.classList.contains('ie') ? 0 : (options.duration || this.duration);
  34. this.init();
  35. }
  36. // TAB METHODS
  37. // ================
  38. Tab.prototype = {
  39. init : function() {
  40. var self = this;
  41. self.actions();
  42. self.tab.addEventListener('click', self.action, false);
  43. },
  44. actions : function() {
  45. var self = this;
  46. this.action = function(e) {
  47. e = e || window.e;
  48. var next = e.target; //the tab we clicked is now the next tab
  49. var nextContent = document.getElementById(next.getAttribute('href').replace('#','')); //this is the actual object, the next tab content to activate
  50. var activeTab = self.getActiveTab();
  51. var activeContent = self.getActiveContent();
  52. //toggle "active" class name
  53. activeTab.classList.remove('active');
  54. next.parentNode.classList.add('active');
  55. //handle dropdown menu "active" class name
  56. if ( !self.tab.parentNode.parentNode.classList.contains('dropdown-menu')){
  57. self.dropdown && self.dropdown.classList.remove('active');
  58. } else {
  59. self.dropdown && self.dropdown.classList.add('active');
  60. }
  61. //1. hide current active content first
  62. activeContent.classList.remove('in');
  63. setTimeout(function() {
  64. //2. toggle current active content from view
  65. activeContent.classList.remove('active');
  66. nextContent.classList.add('active');
  67. }, self.options.duration);
  68. setTimeout(function() {
  69. //3. show next active content
  70. nextContent.classList.add('in');
  71. }, self.options.duration*2);
  72. e.preventDefault();
  73. },
  74. this.getActiveTab = function() {
  75. var activeTabs = self.tabs.querySelectorAll('.active');
  76. if ( activeTabs.length === 1 && !activeTabs[0].classList.contains('dropdown') ) {
  77. return activeTabs[0]
  78. } else if ( activeTabs.length > 1 ) {
  79. return activeTabs[activeTabs.length-1]
  80. }
  81. console.log(activeTabs.length)
  82. },
  83. this.getActiveContent = function() {
  84. var a = self.getActiveTab().getElementsByTagName('A')[0].getAttribute('href').replace('#','');
  85. return a && document.getElementById(a)
  86. }
  87. }
  88. }
  89. // TAB DATA API
  90. // =================
  91. var Tabs = document.querySelectorAll("[data-toggle='tab'], [data-toggle='pill']"), tbl = Tabs.length, i=0;
  92. for ( i;i<tbl;i++ ) {
  93. var tab = Tabs[i], options = {};
  94. options.duration = tab.getAttribute('data-duration') && tab.getAttribute('data-duration') || false;
  95. new Tab(tab,options);
  96. }
  97. return Tab;
  98. });