tab.js 6.51 KB
Newer Older
fat's avatar
tab es6  
fat committed
1
2
/**
 * --------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
3
 * Bootstrap (v4.3.1): tab.js
fat's avatar
tab es6  
fat committed
4
5
6
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

8
9
10
import $ from 'jquery'
import Util from './util'

Johann-S's avatar
Johann-S committed
11
12
13
14
15
16
17
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

const NAME               = 'tab'
XhmikosR's avatar
XhmikosR committed
18
const VERSION            = '4.3.1'
Johann-S's avatar
Johann-S committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const DATA_KEY           = 'bs.tab'
const EVENT_KEY          = `.${DATA_KEY}`
const DATA_API_KEY       = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME]

const Event = {
  HIDE           : `hide${EVENT_KEY}`,
  HIDDEN         : `hidden${EVENT_KEY}`,
  SHOW           : `show${EVENT_KEY}`,
  SHOWN          : `shown${EVENT_KEY}`,
  CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`
}

const ClassName = {
  DROPDOWN_MENU : 'dropdown-menu',
  ACTIVE        : 'active',
  DISABLED      : 'disabled',
  FADE          : 'fade',
  SHOW          : 'show'
}

const Selector = {
  DROPDOWN              : '.dropdown',
  NAV_LIST_GROUP        : '.nav, .list-group',
  ACTIVE                : '.active',
  ACTIVE_UL             : '> li > .active',
  DATA_TOGGLE           : '[data-toggle="tab"], [data-toggle="pill"], [data-toggle="list"]',
  DROPDOWN_TOGGLE       : '.dropdown-toggle',
  DROPDOWN_ACTIVE_CHILD : '> .dropdown-menu .active'
}
fat's avatar
tab es6  
fat committed
49

Johann-S's avatar
Johann-S committed
50
51
52
53
54
55
56
57
58
/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */

class Tab {
  constructor(element) {
    this._element = element
fat's avatar
tab es6  
fat committed
59
60
  }

Johann-S's avatar
Johann-S committed
61
62
63
64
  // Getters

  static get VERSION() {
    return VERSION
fat's avatar
tab es6  
fat committed
65
66
  }

Johann-S's avatar
Johann-S committed
67
  // Public
fat's avatar
tab es6  
fat committed
68

Johann-S's avatar
Johann-S committed
69
70
71
72
73
74
  show() {
    if (this._element.parentNode &&
        this._element.parentNode.nodeType === Node.ELEMENT_NODE &&
        $(this._element).hasClass(ClassName.ACTIVE) ||
        $(this._element).hasClass(ClassName.DISABLED)) {
      return
fat's avatar
tab es6  
fat committed
75
76
    }

Johann-S's avatar
Johann-S committed
77
78
79
80
    let target
    let previous
    const listElement = $(this._element).closest(Selector.NAV_LIST_GROUP)[0]
    const selector = Util.getSelectorFromElement(this._element)
fat's avatar
tab es6  
fat committed
81

Johann-S's avatar
Johann-S committed
82
    if (listElement) {
83
      const itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? Selector.ACTIVE_UL : Selector.ACTIVE
Johann-S's avatar
Johann-S committed
84
85
      previous = $.makeArray($(listElement).find(itemSelector))
      previous = previous[previous.length - 1]
fat's avatar
tab es6  
fat committed
86
87
    }

Johann-S's avatar
Johann-S committed
88
89
90
    const hideEvent = $.Event(Event.HIDE, {
      relatedTarget: this._element
    })
fat's avatar
tab es6  
fat committed
91

Johann-S's avatar
Johann-S committed
92
93
94
    const showEvent = $.Event(Event.SHOW, {
      relatedTarget: previous
    })
fat's avatar
tab es6  
fat committed
95

Johann-S's avatar
Johann-S committed
96
97
98
    if (previous) {
      $(previous).trigger(hideEvent)
    }
fat's avatar
tab es6  
fat committed
99

Johann-S's avatar
Johann-S committed
100
101
102
103
104
105
    $(this._element).trigger(showEvent)

    if (showEvent.isDefaultPrevented() ||
        hideEvent.isDefaultPrevented()) {
      return
    }
fat's avatar
tab es6  
fat committed
106

Johann-S's avatar
Johann-S committed
107
108
109
110
111
112
113
114
115
116
117
    if (selector) {
      target = document.querySelector(selector)
    }

    this._activate(
      this._element,
      listElement
    )

    const complete = () => {
      const hiddenEvent = $.Event(Event.HIDDEN, {
fat's avatar
tab es6  
fat committed
118
119
120
        relatedTarget: this._element
      })

Johann-S's avatar
Johann-S committed
121
      const shownEvent = $.Event(Event.SHOWN, {
fat's avatar
tab es6  
fat committed
122
123
124
        relatedTarget: previous
      })

Johann-S's avatar
Johann-S committed
125
126
127
      $(previous).trigger(hiddenEvent)
      $(this._element).trigger(shownEvent)
    }
fat's avatar
tab es6  
fat committed
128

Johann-S's avatar
Johann-S committed
129
130
131
132
133
134
    if (target) {
      this._activate(target, target.parentNode, complete)
    } else {
      complete()
    }
  }
fat's avatar
tab es6  
fat committed
135

Johann-S's avatar
Johann-S committed
136
137
138
139
  dispose() {
    $.removeData(this._element, DATA_KEY)
    this._element = null
  }
fat's avatar
tab es6  
fat committed
140

Johann-S's avatar
Johann-S committed
141
  // Private
fat's avatar
tab es6  
fat committed
142

Johann-S's avatar
Johann-S committed
143
  _activate(element, container, callback) {
144
    const activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL')
145
146
      ? $(container).find(Selector.ACTIVE_UL)
      : $(container).children(Selector.ACTIVE)
fat's avatar
tab es6  
fat committed
147

Johann-S's avatar
Johann-S committed
148
    const active = activeElements[0]
149
    const isTransitioning = callback && (active && $(active).hasClass(ClassName.FADE))
Johann-S's avatar
Johann-S committed
150
151
152
153
154
    const complete = () => this._transitionComplete(
      element,
      active,
      callback
    )
fat's avatar
tab es6  
fat committed
155

Johann-S's avatar
Johann-S committed
156
157
    if (active && isTransitioning) {
      const transitionDuration = Util.getTransitionDurationFromElement(active)
fat's avatar
tab es6  
fat committed
158

Johann-S's avatar
Johann-S committed
159
      $(active)
Martijn Cuppens's avatar
Martijn Cuppens committed
160
        .removeClass(ClassName.SHOW)
Johann-S's avatar
Johann-S committed
161
162
163
164
        .one(Util.TRANSITION_END, complete)
        .emulateTransitionEnd(transitionDuration)
    } else {
      complete()
fat's avatar
tab es6  
fat committed
165
    }
Johann-S's avatar
Johann-S committed
166
  }
fat's avatar
tab es6  
fat committed
167

Johann-S's avatar
Johann-S committed
168
169
  _transitionComplete(element, active, callback) {
    if (active) {
Martijn Cuppens's avatar
Martijn Cuppens committed
170
      $(active).removeClass(ClassName.ACTIVE)
fat's avatar
fat committed
171

Johann-S's avatar
Johann-S committed
172
173
174
      const dropdownChild = $(active.parentNode).find(
        Selector.DROPDOWN_ACTIVE_CHILD
      )[0]
fat's avatar
tab es6  
fat committed
175

Johann-S's avatar
Johann-S committed
176
177
      if (dropdownChild) {
        $(dropdownChild).removeClass(ClassName.ACTIVE)
178
179
      }

Johann-S's avatar
Johann-S committed
180
181
      if (active.getAttribute('role') === 'tab') {
        active.setAttribute('aria-selected', false)
fat's avatar
tab es6  
fat committed
182
183
184
      }
    }

Johann-S's avatar
Johann-S committed
185
186
187
188
    $(element).addClass(ClassName.ACTIVE)
    if (element.getAttribute('role') === 'tab') {
      element.setAttribute('aria-selected', true)
    }
189

Johann-S's avatar
Johann-S committed
190
    Util.reflow(element)
191
192
193
194

    if (element.classList.contains(ClassName.FADE)) {
      element.classList.add(ClassName.SHOW)
    }
fat's avatar
tab es6  
fat committed
195

196
    if (element.parentNode && $(element.parentNode).hasClass(ClassName.DROPDOWN_MENU)) {
Johann-S's avatar
Johann-S committed
197
      const dropdownElement = $(element).closest(Selector.DROPDOWN)[0]
198

Johann-S's avatar
Johann-S committed
199
200
      if (dropdownElement) {
        const dropdownToggleList = [].slice.call(dropdownElement.querySelectorAll(Selector.DROPDOWN_TOGGLE))
201

Johann-S's avatar
Johann-S committed
202
        $(dropdownToggleList).addClass(ClassName.ACTIVE)
fat's avatar
tab es6  
fat committed
203
204
      }

Johann-S's avatar
Johann-S committed
205
206
      element.setAttribute('aria-expanded', true)
    }
fat's avatar
tab es6  
fat committed
207

Johann-S's avatar
Johann-S committed
208
209
    if (callback) {
      callback()
fat's avatar
tab es6  
fat committed
210
    }
Johann-S's avatar
Johann-S committed
211
  }
fat's avatar
tab es6  
fat committed
212

Johann-S's avatar
Johann-S committed
213
  // Static
fat's avatar
tab es6  
fat committed
214

Johann-S's avatar
Johann-S committed
215
216
217
218
  static _jQueryInterface(config) {
    return this.each(function () {
      const $this = $(this)
      let data = $this.data(DATA_KEY)
fat's avatar
tab es6  
fat committed
219

Johann-S's avatar
Johann-S committed
220
221
222
223
      if (!data) {
        data = new Tab(this)
        $this.data(DATA_KEY, data)
      }
fat's avatar
tab es6  
fat committed
224

Johann-S's avatar
Johann-S committed
225
226
227
      if (typeof config === 'string') {
        if (typeof data[config] === 'undefined') {
          throw new TypeError(`No method named "${config}"`)
fat's avatar
tab es6  
fat committed
228
        }
Johann-S's avatar
Johann-S committed
229
230
231
        data[config]()
      }
    })
fat's avatar
tab es6  
fat committed
232
  }
Johann-S's avatar
Johann-S committed
233
}
fat's avatar
tab es6  
fat committed
234

Johann-S's avatar
Johann-S committed
235
236
237
238
239
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
fat's avatar
tab es6  
fat committed
240

Johann-S's avatar
Johann-S committed
241
242
243
244
245
$(document)
  .on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
    event.preventDefault()
    Tab._jQueryInterface.call($(this), 'show')
  })
fat's avatar
tab es6  
fat committed
246

Johann-S's avatar
Johann-S committed
247
248
249
250
251
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
 */
fat's avatar
tab es6  
fat committed
252

Johann-S's avatar
Johann-S committed
253
254
255
256
257
258
$.fn[NAME] = Tab._jQueryInterface
$.fn[NAME].Constructor = Tab
$.fn[NAME].noConflict = () => {
  $.fn[NAME] = JQUERY_NO_CONFLICT
  return Tab._jQueryInterface
}
fat's avatar
tab es6  
fat committed
259
260

export default Tab