2015-05-08 02:07:38 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2017-01-06 17:38:04 +01:00
|
|
|
* Bootstrap (v4.0.0-alpha.6): button.js
|
2015-05-08 02:07:38 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
const Button = (($) => {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
const NAME = 'button'
|
2017-01-06 17:38:04 +01:00
|
|
|
const VERSION = '4.0.0-alpha.6'
|
2015-05-08 02:07:38 +02:00
|
|
|
const DATA_KEY = 'bs.button'
|
2015-05-13 21:48:34 +02:00
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
2015-05-08 02:07:38 +02:00
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
|
|
|
|
const ClassName = {
|
|
|
|
ACTIVE : 'active',
|
|
|
|
BUTTON : 'btn',
|
|
|
|
FOCUS : 'focus'
|
|
|
|
}
|
|
|
|
|
|
|
|
const Selector = {
|
|
|
|
DATA_TOGGLE_CARROT : '[data-toggle^="button"]',
|
|
|
|
DATA_TOGGLE : '[data-toggle="buttons"]',
|
|
|
|
INPUT : 'input',
|
|
|
|
ACTIVE : '.active',
|
|
|
|
BUTTON : '.btn'
|
|
|
|
}
|
|
|
|
|
|
|
|
const Event = {
|
2015-05-13 21:48:34 +02:00
|
|
|
CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`,
|
|
|
|
FOCUS_BLUR_DATA_API : `focus${EVENT_KEY}${DATA_API_KEY} `
|
|
|
|
+ `blur${EVENT_KEY}${DATA_API_KEY}`
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Button {
|
|
|
|
|
|
|
|
constructor(element) {
|
2015-05-08 07:26:40 +02:00
|
|
|
this._element = element
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 04:45:38 +02:00
|
|
|
|
|
|
|
// getters
|
|
|
|
|
|
|
|
static get VERSION() {
|
|
|
|
return VERSION
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-08 02:07:38 +02:00
|
|
|
// public
|
|
|
|
|
|
|
|
toggle() {
|
|
|
|
let triggerChangeEvent = true
|
2016-11-21 15:36:00 +01:00
|
|
|
const rootElement = $(this._element).closest(
|
2015-05-08 02:07:38 +02:00
|
|
|
Selector.DATA_TOGGLE
|
|
|
|
)[0]
|
|
|
|
|
|
|
|
if (rootElement) {
|
2016-11-21 15:36:00 +01:00
|
|
|
const input = $(this._element).find(Selector.INPUT)[0]
|
2015-05-08 02:07:38 +02:00
|
|
|
|
|
|
|
if (input) {
|
|
|
|
if (input.type === 'radio') {
|
|
|
|
if (input.checked &&
|
2015-05-08 07:26:40 +02:00
|
|
|
$(this._element).hasClass(ClassName.ACTIVE)) {
|
2015-05-08 02:07:38 +02:00
|
|
|
triggerChangeEvent = false
|
|
|
|
|
|
|
|
} else {
|
2016-11-21 15:36:00 +01:00
|
|
|
const activeElement = $(rootElement).find(Selector.ACTIVE)[0]
|
2015-05-08 02:07:38 +02:00
|
|
|
|
|
|
|
if (activeElement) {
|
|
|
|
$(activeElement).removeClass(ClassName.ACTIVE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (triggerChangeEvent) {
|
2015-05-08 07:26:40 +02:00
|
|
|
input.checked = !$(this._element).hasClass(ClassName.ACTIVE)
|
2016-01-14 17:41:36 +01:00
|
|
|
$(input).trigger('change')
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
2016-02-16 15:16:27 +01:00
|
|
|
|
2016-02-17 16:21:02 +01:00
|
|
|
input.focus()
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
2016-02-16 15:16:27 +01:00
|
|
|
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
|
|
|
|
2016-11-06 14:48:55 +01:00
|
|
|
this._element.setAttribute('aria-pressed',
|
|
|
|
!$(this._element).hasClass(ClassName.ACTIVE))
|
|
|
|
|
2015-05-08 02:07:38 +02:00
|
|
|
if (triggerChangeEvent) {
|
2015-05-08 07:26:40 +02:00
|
|
|
$(this._element).toggleClass(ClassName.ACTIVE)
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 21:48:34 +02:00
|
|
|
dispose() {
|
|
|
|
$.removeData(this._element, DATA_KEY)
|
|
|
|
this._element = null
|
|
|
|
}
|
|
|
|
|
2015-05-08 02:07:38 +02:00
|
|
|
|
|
|
|
// static
|
|
|
|
|
|
|
|
static _jQueryInterface(config) {
|
|
|
|
return this.each(function () {
|
|
|
|
let data = $(this).data(DATA_KEY)
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = new Button(this)
|
|
|
|
$(this).data(DATA_KEY, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config === 'toggle') {
|
|
|
|
data[config]()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Data Api implementation
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(document)
|
2015-08-19 04:22:46 +02:00
|
|
|
.on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
|
2015-05-08 02:07:38 +02:00
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
let button = event.target
|
|
|
|
|
|
|
|
if (!$(button).hasClass(ClassName.BUTTON)) {
|
|
|
|
button = $(button).closest(Selector.BUTTON)
|
|
|
|
}
|
|
|
|
|
|
|
|
Button._jQueryInterface.call($(button), 'toggle')
|
|
|
|
})
|
2015-08-19 04:22:46 +02:00
|
|
|
.on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
|
2016-11-21 15:36:00 +01:00
|
|
|
const button = $(event.target).closest(Selector.BUTTON)[0]
|
2015-05-08 02:07:38 +02:00
|
|
|
$(button).toggleClass(ClassName.FOCUS, /^focus(in)?$/.test(event.type))
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
$.fn[NAME] = Button._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = Button
|
|
|
|
$.fn[NAME].noConflict = function () {
|
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return Button._jQueryInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
return Button
|
|
|
|
|
|
|
|
})(jQuery)
|
|
|
|
|
|
|
|
export default Button
|