responsive-tabs.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define("/Plugin/responsive-tabs", ["exports", "jquery"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(exports, require("jquery"));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod.exports, global.jQuery);
  11. global.PluginResponsiveTabs = mod.exports;
  12. }
  13. })(this, function (_exports, _jquery) {
  14. "use strict";
  15. Object.defineProperty(_exports, "__esModule", {
  16. value: true
  17. });
  18. _exports.default = void 0;
  19. _jquery = babelHelpers.interopRequireDefault(_jquery);
  20. var pluginName = 'responsiveHorizontalTabs';
  21. var defaults = {
  22. navSelector: '.nav-tabs',
  23. itemSelector: '.nav-item',
  24. dropdownSelector: '>.dropdown',
  25. dropdownItemSelector: '.dropdown-item',
  26. tabSelector: '.tab-pane',
  27. activeClassName: 'active'
  28. };
  29. var responsiveHorizontalTabs =
  30. /*#__PURE__*/
  31. function () {
  32. function responsiveHorizontalTabs(el, options) {
  33. babelHelpers.classCallCheck(this, responsiveHorizontalTabs);
  34. var $tabs = this.$tabs = (0, _jquery.default)(el);
  35. this.options = options = _jquery.default.extend(true, {}, defaults, options);
  36. var $nav = this.$nav = $tabs.find(this.options.navSelector);
  37. var $dropdown = this.$dropdown = $nav.find(this.options.dropdownSelector);
  38. var $items = this.$items = $nav.find(this.options.itemSelector).filter(function () {
  39. return !(0, _jquery.default)(this).is($dropdown);
  40. });
  41. this.$dropdownItems = $dropdown.find(this.options.dropdownItemSelector);
  42. this.$tabPanel = this.$tabs.find(this.options.tabSelector);
  43. this.breakpoints = [];
  44. $items.each(function () {
  45. (0, _jquery.default)(this).data('width', (0, _jquery.default)(this).width());
  46. });
  47. this.init();
  48. this.bind();
  49. }
  50. babelHelpers.createClass(responsiveHorizontalTabs, [{
  51. key: "init",
  52. value: function init() {
  53. if (this.$dropdown.length === 0) {
  54. return;
  55. }
  56. this.$dropdown.show();
  57. this.breakpoints = [];
  58. var length = this.length = this.$items.length;
  59. var dropWidth = this.dropWidth = this.$dropdown.width();
  60. var total = 0;
  61. this.flag = length;
  62. if (length <= 1) {
  63. this.$dropdown.hide();
  64. return;
  65. }
  66. for (var i = 0; i < length - 2; i++) {
  67. if (i === 0) {
  68. this.breakpoints.push(this.$items.eq(i).outerWidth() + dropWidth);
  69. } else {
  70. this.breakpoints.push(this.breakpoints[i - 1] + this.$items.eq(i).width());
  71. }
  72. }
  73. for (i = 0; i < length; i++) {
  74. total += this.$items.eq(i).outerWidth();
  75. }
  76. this.breakpoints.push(total);
  77. this.layout();
  78. }
  79. }, {
  80. key: "layout",
  81. value: function layout() {
  82. if (this.breakpoints.length <= 0) {
  83. return;
  84. }
  85. var width = this.$nav.width();
  86. var i = 0;
  87. var activeClassName = this.options.activeClassName;
  88. var active = this.$tabPanel.filter(".".concat(activeClassName)).index();
  89. for (; i < this.breakpoints.length; i++) {
  90. if (this.breakpoints[i] > width) {
  91. break;
  92. }
  93. }
  94. if (i === this.flag) {
  95. return;
  96. }
  97. this.$items.children().removeClass(activeClassName);
  98. this.$dropdownItems.removeClass(activeClassName);
  99. this.$dropdown.children().removeClass(activeClassName);
  100. if (i === this.breakpoints.length) {
  101. this.$dropdown.hide();
  102. this.$items.show();
  103. this.$items.eq(active).children().addClass(activeClassName);
  104. } else {
  105. this.$dropdown.show();
  106. for (var j = 0; j < this.length; j++) {
  107. if (j < i) {
  108. this.$items.eq(j).show();
  109. this.$dropdownItems.eq(j).hide();
  110. } else {
  111. this.$items.eq(j).hide();
  112. this.$dropdownItems.eq(j).show();
  113. }
  114. }
  115. if (active < i) {
  116. this.$items.eq(active).children().addClass(activeClassName);
  117. } else {
  118. this.$dropdown.children().addClass(activeClassName);
  119. this.$dropdownItems.eq(active).addClass(activeClassName);
  120. }
  121. }
  122. this.flag = i;
  123. }
  124. }, {
  125. key: "bind",
  126. value: function bind() {
  127. var self = this;
  128. (0, _jquery.default)(window).resize(function () {
  129. self.layout();
  130. });
  131. }
  132. }], [{
  133. key: "_jQueryInterface",
  134. value: function _jQueryInterface(options) {
  135. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  136. args[_key - 1] = arguments[_key];
  137. }
  138. if (typeof options === 'string') {
  139. var method = options;
  140. if (/^\_/.test(method)) {
  141. return false;
  142. }
  143. return this.each(function () {
  144. var api = _jquery.default.data(this, pluginName);
  145. if (api && typeof api[method] === 'function') {
  146. api[method].apply(api, args);
  147. }
  148. });
  149. }
  150. return this.each(function () {
  151. if (!_jquery.default.data(this, pluginName)) {
  152. _jquery.default.data(this, pluginName, new responsiveHorizontalTabs(this, options));
  153. } else {
  154. _jquery.default.data(this, pluginName).init();
  155. }
  156. });
  157. }
  158. }]);
  159. return responsiveHorizontalTabs;
  160. }();
  161. _jquery.default.fn[pluginName] = responsiveHorizontalTabs._jQueryInterface;
  162. _jquery.default.fn[pluginName].constructor = responsiveHorizontalTabs;
  163. _jquery.default.fn[pluginName].noConflict = function () {
  164. _jquery.default.fn[pluginName] = window.JQUERY_NO_CONFLICT;
  165. return responsiveHorizontalTabs._jQueryInterface;
  166. };
  167. var _default = responsiveHorizontalTabs;
  168. _exports.default = _default;
  169. });