tab.js 6.82 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 Data from './dom/data'
import EventHandler from './dom/eventHandler'
import SelectorEngine from './dom/selectorEngine'
11
12
import Util from './util'

Johann-S's avatar
Johann-S committed
13
14
15
16
17
18
19
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

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

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',
45
  ACTIVE_UL             : ':scope > li > .active',
Johann-S's avatar
Johann-S committed
46
47
  DATA_TOGGLE           : '[data-toggle="tab"], [data-toggle="pill"], [data-toggle="list"]',
  DROPDOWN_TOGGLE       : '.dropdown-toggle',
48
  DROPDOWN_ACTIVE_CHILD : ':scope > .dropdown-menu .active'
Johann-S's avatar
Johann-S committed
49
}
fat's avatar
tab es6  
fat committed
50

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

class Tab {
  constructor(element) {
    this._element = element
60
61

    Data.setData(this._element, DATA_KEY, this)
fat's avatar
tab es6  
fat committed
62
63
  }

Johann-S's avatar
Johann-S committed
64
65
66
67
  // Getters

  static get VERSION() {
    return VERSION
fat's avatar
tab es6  
fat committed
68
69
  }

Johann-S's avatar
Johann-S committed
70
  // Public
fat's avatar
tab es6  
fat committed
71

Johann-S's avatar
Johann-S committed
72
73
  show() {
    if (this._element.parentNode &&
74
75
76
      this._element.parentNode.nodeType === Node.ELEMENT_NODE &&
      this._element.classList.contains(ClassName.ACTIVE) ||
      this._element.classList.contains(ClassName.DISABLED)) {
Johann-S's avatar
Johann-S committed
77
      return
fat's avatar
tab es6  
fat committed
78
79
    }

Johann-S's avatar
Johann-S committed
80
81
    let target
    let previous
82
    const listElement = SelectorEngine.closest(this._element, Selector.NAV_LIST_GROUP)
Johann-S's avatar
Johann-S committed
83
    const selector = Util.getSelectorFromElement(this._element)
fat's avatar
tab es6  
fat committed
84

Johann-S's avatar
Johann-S committed
85
    if (listElement) {
86
      const itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? Selector.ACTIVE_UL : Selector.ACTIVE
87
      previous = Util.makeArray(SelectorEngine.find(itemSelector, listElement))
Johann-S's avatar
Johann-S committed
88
      previous = previous[previous.length - 1]
fat's avatar
tab es6  
fat committed
89
90
    }

91
    let hideEvent = null
fat's avatar
tab es6  
fat committed
92

Johann-S's avatar
Johann-S committed
93
    if (previous) {
94
95
96
      hideEvent = EventHandler.trigger(previous, Event.HIDE, {
        relatedTarget: this._element
      })
Johann-S's avatar
Johann-S committed
97
    }
fat's avatar
tab es6  
fat committed
98

99
100
101
    const showEvent = EventHandler.trigger(this._element, Event.SHOW, {
      relatedTarget: previous
    })
Johann-S's avatar
Johann-S committed
102

103
104
    if (showEvent.defaultPrevented ||
      hideEvent !== null && hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
105
106
      return
    }
fat's avatar
tab es6  
fat committed
107

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

    this._activate(
      this._element,
      listElement
    )

    const complete = () => {
118
      EventHandler.trigger(previous, Event.HIDDEN, {
fat's avatar
tab es6  
fat committed
119
120
        relatedTarget: this._element
      })
121
      EventHandler.trigger(this._element, Event.SHOWN, {
fat's avatar
tab es6  
fat committed
122
123
        relatedTarget: previous
      })
Johann-S's avatar
Johann-S committed
124
    }
fat's avatar
tab es6  
fat committed
125

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

Johann-S's avatar
Johann-S committed
133
  dispose() {
134
    Data.removeData(this._element, DATA_KEY)
Johann-S's avatar
Johann-S committed
135
136
    this._element = null
  }
fat's avatar
tab es6  
fat committed
137

Johann-S's avatar
Johann-S committed
138
  // Private
fat's avatar
tab es6  
fat committed
139

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

    const active          = activeElements[0]
    const isTransitioning = callback &&
      (active && active.classList.contains(ClassName.FADE))
fat's avatar
tab es6  
fat committed
148

Johann-S's avatar
Johann-S committed
149
150
151
152
153
    const complete = () => this._transitionComplete(
      element,
      active,
      callback
    )
fat's avatar
tab es6  
fat committed
154

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

159
      EventHandler.one(active, Util.TRANSITION_END, complete)
Johann-S's avatar
Johann-S committed
160
      Util.emulateTransitionEnd(active, transitionDuration)
Johann-S's avatar
Johann-S committed
161
162
    } else {
      complete()
fat's avatar
tab es6  
fat committed
163
    }
Johann-S's avatar
Johann-S committed
164
  }
fat's avatar
tab es6  
fat committed
165

Johann-S's avatar
Johann-S committed
166
167
  _transitionComplete(element, active, callback) {
    if (active) {
168
      active.classList.remove(ClassName.ACTIVE)
fat's avatar
fat committed
169

170
      const dropdownChild = SelectorEngine.findOne(Selector.DROPDOWN_ACTIVE_CHILD, active.parentNode)
fat's avatar
tab es6  
fat committed
171

Johann-S's avatar
Johann-S committed
172
      if (dropdownChild) {
173
        dropdownChild.classList.remove(ClassName.ACTIVE)
174
175
      }

Johann-S's avatar
Johann-S committed
176
177
      if (active.getAttribute('role') === 'tab') {
        active.setAttribute('aria-selected', false)
fat's avatar
tab es6  
fat committed
178
179
180
      }
    }

181
    element.classList.add(ClassName.ACTIVE)
Johann-S's avatar
Johann-S committed
182
183
184
    if (element.getAttribute('role') === 'tab') {
      element.setAttribute('aria-selected', true)
    }
185

Johann-S's avatar
Johann-S committed
186
    Util.reflow(element)
187
188
189
190

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

192
193
    if (element.parentNode && element.parentNode.classList.contains(ClassName.DROPDOWN_MENU)) {
      const dropdownElement = SelectorEngine.closest(element, Selector.DROPDOWN)
194

Johann-S's avatar
Johann-S committed
195
      if (dropdownElement) {
196
        Util.makeArray(SelectorEngine.find(Selector.DROPDOWN_TOGGLE))
197
          .forEach((dropdown) => dropdown.classList.add(ClassName.ACTIVE))
fat's avatar
tab es6  
fat committed
198
199
      }

Johann-S's avatar
Johann-S committed
200
201
      element.setAttribute('aria-expanded', true)
    }
fat's avatar
tab es6  
fat committed
202

Johann-S's avatar
Johann-S committed
203
204
    if (callback) {
      callback()
fat's avatar
tab es6  
fat committed
205
    }
Johann-S's avatar
Johann-S committed
206
  }
fat's avatar
tab es6  
fat committed
207

Johann-S's avatar
Johann-S committed
208
  // Static
fat's avatar
tab es6  
fat committed
209

Johann-S's avatar
Johann-S committed
210
211
  static _jQueryInterface(config) {
    return this.each(function () {
212
      const data = Data.getData(this, DATA_KEY) || new Tab(this)
fat's avatar
tab es6  
fat committed
213

Johann-S's avatar
Johann-S committed
214
215
216
      if (typeof config === 'string') {
        if (typeof data[config] === 'undefined') {
          throw new TypeError(`No method named "${config}"`)
fat's avatar
tab es6  
fat committed
217
        }
Johann-S's avatar
Johann-S committed
218
219
220
        data[config]()
      }
    })
fat's avatar
tab es6  
fat committed
221
  }
222
223
224
225

  static _getInstance(element) {
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
226
}
fat's avatar
tab es6  
fat committed
227

Johann-S's avatar
Johann-S committed
228
229
230
231
232
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
fat's avatar
tab es6  
fat committed
233

234
235
236
237
238
239
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
  event.preventDefault()

  const data = Data.getData(this, DATA_KEY) || new Tab(this)
  data.show()
})
fat's avatar
tab es6  
fat committed
240

Johann-S's avatar
Johann-S committed
241
242
243
244
245
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
 */
fat's avatar
tab es6  
fat committed
246

247
248
249
250
251
252
253
254
255
const $ = Util.jQuery
if (typeof $ !== 'undefined') {
  const JQUERY_NO_CONFLICT = $.fn[NAME]
  $.fn[NAME]               = Tab._jQueryInterface
  $.fn[NAME].Constructor   = Tab
  $.fn[NAME].noConflict    = () => {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Tab._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
256
}
fat's avatar
tab es6  
fat committed
257
258

export default Tab