tab.js 6.45 KB
Newer Older
1
import $ from 'jquery'
fat's avatar
tab es6  
fat committed
2
3
4
5
import Util from './util'

/**
 * --------------------------------------------------------------------------
Mark Otto's avatar
Mark Otto committed
6
 * Bootstrap (v4.1.3): tab.js
fat's avatar
tab es6  
fat committed
7
8
9
10
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

Johann-S's avatar
Johann-S committed
11
12
13
14
15
16
17
18
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
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

const NAME               = 'tab'
const VERSION            = '4.1.3'
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
83
84
85
    if (listElement) {
      const itemSelector = listElement.nodeName === 'UL' ? Selector.ACTIVE_UL : Selector.ACTIVE
      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
144
  _activate(element, container, callback) {
    let activeElements
dercodercom's avatar
dercodercom committed
145
    if (container && container.nodeName === 'UL') {
Johann-S's avatar
Johann-S committed
146
147
148
149
      activeElements = $(container).find(Selector.ACTIVE_UL)
    } else {
      activeElements = $(container).children(Selector.ACTIVE)
    }
fat's avatar
tab es6  
fat committed
150

Johann-S's avatar
Johann-S committed
151
152
153
    const active = activeElements[0]
    const isTransitioning = callback &&
      (active && $(active).hasClass(ClassName.FADE))
fat's avatar
tab es6  
fat committed
154

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

Johann-S's avatar
Johann-S committed
161
162
    if (active && isTransitioning) {
      const transitionDuration = Util.getTransitionDurationFromElement(active)
fat's avatar
tab es6  
fat committed
163

Johann-S's avatar
Johann-S committed
164
      $(active)
Martijn Cuppens's avatar
Martijn Cuppens committed
165
        .removeClass(ClassName.SHOW)
Johann-S's avatar
Johann-S committed
166
167
168
169
        .one(Util.TRANSITION_END, complete)
        .emulateTransitionEnd(transitionDuration)
    } else {
      complete()
fat's avatar
tab es6  
fat committed
170
    }
Johann-S's avatar
Johann-S committed
171
  }
fat's avatar
tab es6  
fat committed
172

Johann-S's avatar
Johann-S committed
173
174
  _transitionComplete(element, active, callback) {
    if (active) {
Martijn Cuppens's avatar
Martijn Cuppens committed
175
      $(active).removeClass(ClassName.ACTIVE)
fat's avatar
fat committed
176

Johann-S's avatar
Johann-S committed
177
178
179
      const dropdownChild = $(active.parentNode).find(
        Selector.DROPDOWN_ACTIVE_CHILD
      )[0]
fat's avatar
tab es6  
fat committed
180

Johann-S's avatar
Johann-S committed
181
182
      if (dropdownChild) {
        $(dropdownChild).removeClass(ClassName.ACTIVE)
183
184
      }

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

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

Johann-S's avatar
Johann-S committed
195
196
    Util.reflow(element)
    $(element).addClass(ClassName.SHOW)
fat's avatar
tab es6  
fat committed
197

Johann-S's avatar
Johann-S committed
198
199
200
201
202
203
    if (element.parentNode &&
        $(element.parentNode).hasClass(ClassName.DROPDOWN_MENU)) {
      const dropdownElement = $(element).closest(Selector.DROPDOWN)[0]
      if (dropdownElement) {
        const dropdownToggleList = [].slice.call(dropdownElement.querySelectorAll(Selector.DROPDOWN_TOGGLE))
        $(dropdownToggleList).addClass(ClassName.ACTIVE)
fat's avatar
tab es6  
fat committed
204
205
      }

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

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

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

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

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

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

Johann-S's avatar
Johann-S committed
242
243
244
245
246
$(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
247

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

Johann-S's avatar
Johann-S committed
254
255
256
257
258
259
$.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
260
261

export default Tab