| Total Complexity | 58 |
| Complexity/F | 3.41 |
| Lines of Code | 263 |
| Function Count | 17 |
| Duplicated Lines | 5 |
| Ratio | 1.9 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like resources/bootstrap/js/tab.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /*! |
||
| 6 | View Code Duplication | (function (global, factory) { |
|
|
|
|||
| 7 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) : |
||
| 8 | typeof define === 'function' && define.amd ? define(['jquery', './util.js'], factory) : |
||
| 9 | (global = global || self, global.Tab = factory(global.jQuery, global.Util)); |
||
| 10 | }(this, function ($, Util) { 'use strict'; |
||
| 11 | |||
| 12 | $ = $ && $.hasOwnProperty('default') ? $['default'] : $; |
||
| 13 | Util = Util && Util.hasOwnProperty('default') ? Util['default'] : Util; |
||
| 14 | |||
| 15 | function _defineProperties(target, props) { |
||
| 16 | for (var i = 0; i < props.length; i++) { |
||
| 17 | var descriptor = props[i]; |
||
| 18 | descriptor.enumerable = descriptor.enumerable || false; |
||
| 19 | descriptor.configurable = true; |
||
| 20 | if ("value" in descriptor) descriptor.writable = true; |
||
| 21 | Object.defineProperty(target, descriptor.key, descriptor); |
||
| 22 | } |
||
| 23 | } |
||
| 24 | |||
| 25 | function _createClass(Constructor, protoProps, staticProps) { |
||
| 26 | if (protoProps) _defineProperties(Constructor.prototype, protoProps); |
||
| 27 | if (staticProps) _defineProperties(Constructor, staticProps); |
||
| 28 | return Constructor; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * ------------------------------------------------------------------------ |
||
| 33 | * Constants |
||
| 34 | * ------------------------------------------------------------------------ |
||
| 35 | */ |
||
| 36 | |||
| 37 | var NAME = 'tab'; |
||
| 38 | var VERSION = '4.3.1'; |
||
| 39 | var DATA_KEY = 'bs.tab'; |
||
| 40 | var EVENT_KEY = "." + DATA_KEY; |
||
| 41 | var DATA_API_KEY = '.data-api'; |
||
| 42 | var JQUERY_NO_CONFLICT = $.fn[NAME]; |
||
| 43 | var Event = { |
||
| 44 | HIDE: "hide" + EVENT_KEY, |
||
| 45 | HIDDEN: "hidden" + EVENT_KEY, |
||
| 46 | SHOW: "show" + EVENT_KEY, |
||
| 47 | SHOWN: "shown" + EVENT_KEY, |
||
| 48 | CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY |
||
| 49 | }; |
||
| 50 | var ClassName = { |
||
| 51 | DROPDOWN_MENU: 'dropdown-menu', |
||
| 52 | ACTIVE: 'active', |
||
| 53 | DISABLED: 'disabled', |
||
| 54 | FADE: 'fade', |
||
| 55 | SHOW: 'show' |
||
| 56 | }; |
||
| 57 | var Selector = { |
||
| 58 | DROPDOWN: '.dropdown', |
||
| 59 | NAV_LIST_GROUP: '.nav, .list-group', |
||
| 60 | ACTIVE: '.active', |
||
| 61 | ACTIVE_UL: '> li > .active', |
||
| 62 | DATA_TOGGLE: '[data-toggle="tab"], [data-toggle="pill"], [data-toggle="list"]', |
||
| 63 | DROPDOWN_TOGGLE: '.dropdown-toggle', |
||
| 64 | DROPDOWN_ACTIVE_CHILD: '> .dropdown-menu .active' |
||
| 65 | /** |
||
| 66 | * ------------------------------------------------------------------------ |
||
| 67 | * Class Definition |
||
| 68 | * ------------------------------------------------------------------------ |
||
| 69 | */ |
||
| 70 | |||
| 71 | }; |
||
| 72 | |||
| 73 | var Tab = |
||
| 74 | /*#__PURE__*/ |
||
| 75 | function () { |
||
| 76 | function Tab(element) { |
||
| 77 | this._element = element; |
||
| 78 | } // Getters |
||
| 79 | |||
| 80 | |||
| 81 | var _proto = Tab.prototype; |
||
| 82 | |||
| 83 | // Public |
||
| 84 | _proto.show = function show() { |
||
| 85 | var _this = this; |
||
| 86 | |||
| 87 | if (this._element.parentNode && this._element.parentNode.nodeType === Node.ELEMENT_NODE && $(this._element).hasClass(ClassName.ACTIVE) || $(this._element).hasClass(ClassName.DISABLED)) { |
||
| 88 | return; |
||
| 89 | } |
||
| 90 | |||
| 91 | var target; |
||
| 92 | var previous; |
||
| 93 | var listElement = $(this._element).closest(Selector.NAV_LIST_GROUP)[0]; |
||
| 94 | var selector = Util.getSelectorFromElement(this._element); |
||
| 95 | |||
| 96 | if (listElement) { |
||
| 97 | var itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? Selector.ACTIVE_UL : Selector.ACTIVE; |
||
| 98 | previous = $.makeArray($(listElement).find(itemSelector)); |
||
| 99 | previous = previous[previous.length - 1]; |
||
| 100 | } |
||
| 101 | |||
| 102 | var hideEvent = $.Event(Event.HIDE, { |
||
| 103 | relatedTarget: this._element |
||
| 104 | }); |
||
| 105 | var showEvent = $.Event(Event.SHOW, { |
||
| 106 | relatedTarget: previous |
||
| 107 | }); |
||
| 108 | |||
| 109 | if (previous) { |
||
| 110 | $(previous).trigger(hideEvent); |
||
| 111 | } |
||
| 112 | |||
| 113 | $(this._element).trigger(showEvent); |
||
| 114 | |||
| 115 | if (showEvent.isDefaultPrevented() || hideEvent.isDefaultPrevented()) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (selector) { |
||
| 120 | target = document.querySelector(selector); |
||
| 121 | } |
||
| 122 | |||
| 123 | this._activate(this._element, listElement); |
||
| 124 | |||
| 125 | var complete = function complete() { |
||
| 126 | var hiddenEvent = $.Event(Event.HIDDEN, { |
||
| 127 | relatedTarget: _this._element |
||
| 128 | }); |
||
| 129 | var shownEvent = $.Event(Event.SHOWN, { |
||
| 130 | relatedTarget: previous |
||
| 131 | }); |
||
| 132 | $(previous).trigger(hiddenEvent); |
||
| 133 | $(_this._element).trigger(shownEvent); |
||
| 134 | }; |
||
| 135 | |||
| 136 | if (target) { |
||
| 137 | this._activate(target, target.parentNode, complete); |
||
| 138 | } else { |
||
| 139 | complete(); |
||
| 140 | } |
||
| 141 | }; |
||
| 142 | |||
| 143 | _proto.dispose = function dispose() { |
||
| 144 | $.removeData(this._element, DATA_KEY); |
||
| 145 | this._element = null; |
||
| 146 | } // Private |
||
| 147 | ; |
||
| 148 | |||
| 149 | _proto._activate = function _activate(element, container, callback) { |
||
| 150 | var _this2 = this; |
||
| 151 | |||
| 152 | var activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL') ? $(container).find(Selector.ACTIVE_UL) : $(container).children(Selector.ACTIVE); |
||
| 153 | var active = activeElements[0]; |
||
| 154 | var isTransitioning = callback && active && $(active).hasClass(ClassName.FADE); |
||
| 155 | |||
| 156 | var complete = function complete() { |
||
| 157 | return _this2._transitionComplete(element, active, callback); |
||
| 158 | }; |
||
| 159 | |||
| 160 | if (active && isTransitioning) { |
||
| 161 | var transitionDuration = Util.getTransitionDurationFromElement(active); |
||
| 162 | $(active).removeClass(ClassName.SHOW).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration); |
||
| 163 | } else { |
||
| 164 | complete(); |
||
| 165 | } |
||
| 166 | }; |
||
| 167 | |||
| 168 | _proto._transitionComplete = function _transitionComplete(element, active, callback) { |
||
| 169 | if (active) { |
||
| 170 | $(active).removeClass(ClassName.ACTIVE); |
||
| 171 | var dropdownChild = $(active.parentNode).find(Selector.DROPDOWN_ACTIVE_CHILD)[0]; |
||
| 172 | |||
| 173 | if (dropdownChild) { |
||
| 174 | $(dropdownChild).removeClass(ClassName.ACTIVE); |
||
| 175 | } |
||
| 176 | |||
| 177 | if (active.getAttribute('role') === 'tab') { |
||
| 178 | active.setAttribute('aria-selected', false); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | $(element).addClass(ClassName.ACTIVE); |
||
| 183 | |||
| 184 | if (element.getAttribute('role') === 'tab') { |
||
| 185 | element.setAttribute('aria-selected', true); |
||
| 186 | } |
||
| 187 | |||
| 188 | Util.reflow(element); |
||
| 189 | |||
| 190 | if (element.classList.contains(ClassName.FADE)) { |
||
| 191 | element.classList.add(ClassName.SHOW); |
||
| 192 | } |
||
| 193 | |||
| 194 | if (element.parentNode && $(element.parentNode).hasClass(ClassName.DROPDOWN_MENU)) { |
||
| 195 | var dropdownElement = $(element).closest(Selector.DROPDOWN)[0]; |
||
| 196 | |||
| 197 | if (dropdownElement) { |
||
| 198 | var dropdownToggleList = [].slice.call(dropdownElement.querySelectorAll(Selector.DROPDOWN_TOGGLE)); |
||
| 199 | $(dropdownToggleList).addClass(ClassName.ACTIVE); |
||
| 200 | } |
||
| 201 | |||
| 202 | element.setAttribute('aria-expanded', true); |
||
| 203 | } |
||
| 204 | |||
| 205 | if (callback) { |
||
| 206 | callback(); |
||
| 207 | } |
||
| 208 | } // Static |
||
| 209 | ; |
||
| 210 | |||
| 211 | Tab._jQueryInterface = function _jQueryInterface(config) { |
||
| 212 | return this.each(function () { |
||
| 213 | var $this = $(this); |
||
| 214 | var data = $this.data(DATA_KEY); |
||
| 215 | |||
| 216 | if (!data) { |
||
| 217 | data = new Tab(this); |
||
| 218 | $this.data(DATA_KEY, data); |
||
| 219 | } |
||
| 220 | |||
| 221 | if (typeof config === 'string') { |
||
| 222 | if (typeof data[config] === 'undefined') { |
||
| 223 | throw new TypeError("No method named \"" + config + "\""); |
||
| 224 | } |
||
| 225 | |||
| 226 | data[config](); |
||
| 227 | } |
||
| 228 | }); |
||
| 229 | }; |
||
| 230 | |||
| 231 | _createClass(Tab, null, [{ |
||
| 232 | key: "VERSION", |
||
| 233 | get: function get() { |
||
| 234 | return VERSION; |
||
| 235 | } |
||
| 236 | }]); |
||
| 237 | |||
| 238 | return Tab; |
||
| 239 | }(); |
||
| 240 | /** |
||
| 241 | * ------------------------------------------------------------------------ |
||
| 242 | * Data Api implementation |
||
| 243 | * ------------------------------------------------------------------------ |
||
| 244 | */ |
||
| 245 | |||
| 246 | |||
| 247 | $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) { |
||
| 248 | event.preventDefault(); |
||
| 249 | |||
| 250 | Tab._jQueryInterface.call($(this), 'show'); |
||
| 251 | }); |
||
| 252 | /** |
||
| 253 | * ------------------------------------------------------------------------ |
||
| 254 | * jQuery |
||
| 255 | * ------------------------------------------------------------------------ |
||
| 256 | */ |
||
| 257 | |||
| 258 | $.fn[NAME] = Tab._jQueryInterface; |
||
| 259 | $.fn[NAME].Constructor = Tab; |
||
| 260 | |||
| 261 | $.fn[NAME].noConflict = function () { |
||
| 262 | $.fn[NAME] = JQUERY_NO_CONFLICT; |
||
| 263 | return Tab._jQueryInterface; |
||
| 264 | }; |
||
| 265 | |||
| 266 | return Tab; |
||
| 267 | |||
| 268 | })); |
||
| 269 | //# sourceMappingURL=tab.js.map |
||
| 270 |