tab.js 6.95 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
11
12
13
14
15
16
import {
  jQuery as $,
  TRANSITION_END,
  emulateTransitionEnd,
  getSelectorFromElement,
  getTransitionDurationFromElement,
  makeArray,
  reflow
} from './util/index'
17
18
19
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
import SelectorEngine from './dom/selectorEngine'
20

Johann-S's avatar
Johann-S committed
21
22
23
24
25
26
27
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

const NAME               = 'tab'
XhmikosR's avatar
XhmikosR committed
28
const VERSION            = '4.3.1'
Johann-S's avatar
Johann-S committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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',
53
  ACTIVE_UL             : ':scope > li > .active',
Johann-S's avatar
Johann-S committed
54
55
  DATA_TOGGLE           : '[data-toggle="tab"], [data-toggle="pill"], [data-toggle="list"]',
  DROPDOWN_TOGGLE       : '.dropdown-toggle',
56
  DROPDOWN_ACTIVE_CHILD : ':scope > .dropdown-menu .active'
Johann-S's avatar
Johann-S committed
57
}
fat's avatar
tab es6  
fat committed
58

Johann-S's avatar
Johann-S committed
59
60
61
62
63
64
65
66
67
/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */

class Tab {
  constructor(element) {
    this._element = element
68
69

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

Johann-S's avatar
Johann-S committed
72
73
74
75
  // Getters

  static get VERSION() {
    return VERSION
fat's avatar
tab es6  
fat committed
76
77
  }

Johann-S's avatar
Johann-S committed
78
  // Public
fat's avatar
tab es6  
fat committed
79

Johann-S's avatar
Johann-S committed
80
81
  show() {
    if (this._element.parentNode &&
82
83
84
      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
85
      return
fat's avatar
tab es6  
fat committed
86
87
    }

Johann-S's avatar
Johann-S committed
88
89
    let target
    let previous
90
    const listElement = SelectorEngine.closest(this._element, Selector.NAV_LIST_GROUP)
91
    const selector = getSelectorFromElement(this._element)
fat's avatar
tab es6  
fat committed
92

Johann-S's avatar
Johann-S committed
93
    if (listElement) {
94
      const itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? Selector.ACTIVE_UL : Selector.ACTIVE
95
      previous = makeArray(SelectorEngine.find(itemSelector, listElement))
Johann-S's avatar
Johann-S committed
96
      previous = previous[previous.length - 1]
fat's avatar
tab es6  
fat committed
97
98
    }

99
    let hideEvent = null
fat's avatar
tab es6  
fat committed
100

Johann-S's avatar
Johann-S committed
101
    if (previous) {
102
103
104
      hideEvent = EventHandler.trigger(previous, Event.HIDE, {
        relatedTarget: this._element
      })
Johann-S's avatar
Johann-S committed
105
    }
fat's avatar
tab es6  
fat committed
106

107
108
109
    const showEvent = EventHandler.trigger(this._element, Event.SHOW, {
      relatedTarget: previous
    })
Johann-S's avatar
Johann-S committed
110

111
112
    if (showEvent.defaultPrevented ||
      hideEvent !== null && hideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
113
114
      return
    }
fat's avatar
tab es6  
fat committed
115

Johann-S's avatar
Johann-S committed
116
    if (selector) {
117
      target = SelectorEngine.findOne(selector)
Johann-S's avatar
Johann-S committed
118
119
120
121
122
123
124
125
    }

    this._activate(
      this._element,
      listElement
    )

    const complete = () => {
126
      EventHandler.trigger(previous, Event.HIDDEN, {
fat's avatar
tab es6  
fat committed
127
128
        relatedTarget: this._element
      })
129
      EventHandler.trigger(this._element, Event.SHOWN, {
fat's avatar
tab es6  
fat committed
130
131
        relatedTarget: previous
      })
Johann-S's avatar
Johann-S committed
132
    }
fat's avatar
tab es6  
fat committed
133

Johann-S's avatar
Johann-S committed
134
135
136
137
138
139
    if (target) {
      this._activate(target, target.parentNode, complete)
    } else {
      complete()
    }
  }
fat's avatar
tab es6  
fat committed
140

Johann-S's avatar
Johann-S committed
141
  dispose() {
142
    Data.removeData(this._element, DATA_KEY)
Johann-S's avatar
Johann-S committed
143
144
    this._element = null
  }
fat's avatar
tab es6  
fat committed
145

Johann-S's avatar
Johann-S committed
146
  // Private
fat's avatar
tab es6  
fat committed
147

Johann-S's avatar
Johann-S committed
148
  _activate(element, container, callback) {
149
    const activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL')
150
151
152
153
154
155
      ? 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
156

Johann-S's avatar
Johann-S committed
157
158
159
160
161
    const complete = () => this._transitionComplete(
      element,
      active,
      callback
    )
fat's avatar
tab es6  
fat committed
162

Johann-S's avatar
Johann-S committed
163
    if (active && isTransitioning) {
164
      const transitionDuration = getTransitionDurationFromElement(active)
165
      active.classList.remove(ClassName.SHOW)
fat's avatar
tab es6  
fat committed
166

167
168
      EventHandler.one(active, TRANSITION_END, complete)
      emulateTransitionEnd(active, transitionDuration)
Johann-S's avatar
Johann-S committed
169
170
    } else {
      complete()
fat's avatar
tab es6  
fat committed
171
    }
Johann-S's avatar
Johann-S committed
172
  }
fat's avatar
tab es6  
fat committed
173

Johann-S's avatar
Johann-S committed
174
175
  _transitionComplete(element, active, callback) {
    if (active) {
176
      active.classList.remove(ClassName.ACTIVE)
fat's avatar
fat committed
177

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

Johann-S's avatar
Johann-S committed
180
      if (dropdownChild) {
181
        dropdownChild.classList.remove(ClassName.ACTIVE)
182
183
      }

Johann-S's avatar
Johann-S committed
184
185
      if (active.getAttribute('role') === 'tab') {
        active.setAttribute('aria-selected', false)
fat's avatar
tab es6  
fat committed
186
187
188
      }
    }

189
    element.classList.add(ClassName.ACTIVE)
Johann-S's avatar
Johann-S committed
190
191
192
    if (element.getAttribute('role') === 'tab') {
      element.setAttribute('aria-selected', true)
    }
193

194
    reflow(element)
195
196
197
198

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

200
201
    if (element.parentNode && element.parentNode.classList.contains(ClassName.DROPDOWN_MENU)) {
      const dropdownElement = SelectorEngine.closest(element, Selector.DROPDOWN)
202

Johann-S's avatar
Johann-S committed
203
      if (dropdownElement) {
204
        makeArray(SelectorEngine.find(Selector.DROPDOWN_TOGGLE))
205
          .forEach((dropdown) => dropdown.classList.add(ClassName.ACTIVE))
fat's avatar
tab es6  
fat committed
206
207
      }

Johann-S's avatar
Johann-S committed
208
209
      element.setAttribute('aria-expanded', true)
    }
fat's avatar
tab es6  
fat committed
210

Johann-S's avatar
Johann-S committed
211
212
    if (callback) {
      callback()
fat's avatar
tab es6  
fat committed
213
    }
Johann-S's avatar
Johann-S committed
214
  }
fat's avatar
tab es6  
fat committed
215

Johann-S's avatar
Johann-S committed
216
  // Static
fat's avatar
tab es6  
fat committed
217

Johann-S's avatar
Johann-S committed
218
219
  static _jQueryInterface(config) {
    return this.each(function () {
220
      const data = Data.getData(this, DATA_KEY) || new Tab(this)
fat's avatar
tab es6  
fat committed
221

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

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

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

242
243
244
245
246
247
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
248

Johann-S's avatar
Johann-S committed
249
250
251
252
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
253
 * add .tab to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
254
 */
fat's avatar
tab es6  
fat committed
255

256
257
258
259
260
261
262
263
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
264
}
fat's avatar
tab es6  
fat committed
265
266

export default Tab