2015-05-08 02:07:38 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2019-02-13 17:01:40 +01:00
|
|
|
* Bootstrap (v4.3.1): button.js
|
2015-05-08 02:07:38 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-22 23:37:55 +01:00
|
|
|
import {
|
|
|
|
jQuery as $
|
|
|
|
} from './util/index'
|
2017-08-21 13:49:41 +02:00
|
|
|
import Data from './dom/data'
|
|
|
|
import EventHandler from './dom/eventHandler'
|
|
|
|
import SelectorEngine from './dom/selectorEngine'
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
const NAME = 'button'
|
2019-02-13 17:01:40 +01:00
|
|
|
const VERSION = '4.3.1'
|
2018-09-26 10:39:01 +02:00
|
|
|
const DATA_KEY = 'bs.button'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
|
|
|
|
|
|
|
const ClassName = {
|
|
|
|
ACTIVE : 'active',
|
|
|
|
BUTTON : 'btn',
|
|
|
|
FOCUS : 'focus'
|
|
|
|
}
|
|
|
|
|
|
|
|
const Selector = {
|
|
|
|
DATA_TOGGLE_CARROT : '[data-toggle^="button"]',
|
|
|
|
DATA_TOGGLE : '[data-toggle="buttons"]',
|
2018-12-10 09:40:08 +01:00
|
|
|
INPUT : 'input:not([type="hidden"])',
|
2018-09-26 10:39:01 +02:00
|
|
|
ACTIVE : '.active',
|
|
|
|
BUTTON : '.btn'
|
|
|
|
}
|
|
|
|
|
|
|
|
const Event = {
|
|
|
|
CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`,
|
2017-08-21 13:49:41 +02:00
|
|
|
FOCUS_DATA_API : `focus${EVENT_KEY}${DATA_API_KEY}`,
|
|
|
|
BLUR_DATA_API : `blur${EVENT_KEY}${DATA_API_KEY}`
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Button {
|
|
|
|
constructor(element) {
|
|
|
|
this._element = element
|
2018-06-24 22:02:03 +02:00
|
|
|
Data.setData(element, DATA_KEY, this)
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Getters
|
|
|
|
|
|
|
|
static get VERSION() {
|
|
|
|
return VERSION
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Public
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
toggle() {
|
|
|
|
let triggerChangeEvent = true
|
|
|
|
let addAriaPressed = true
|
2017-08-21 13:49:41 +02:00
|
|
|
|
|
|
|
const rootElement = SelectorEngine.closest(
|
|
|
|
this._element,
|
2018-09-26 10:39:01 +02:00
|
|
|
Selector.DATA_TOGGLE
|
2017-08-21 13:49:41 +02:00
|
|
|
)
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (rootElement) {
|
2017-08-21 13:49:41 +02:00
|
|
|
const input = SelectorEngine.findOne(Selector.INPUT, this._element)
|
2015-05-11 04:45:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (input) {
|
|
|
|
if (input.type === 'radio') {
|
|
|
|
if (input.checked &&
|
|
|
|
this._element.classList.contains(ClassName.ACTIVE)) {
|
|
|
|
triggerChangeEvent = false
|
|
|
|
} else {
|
2017-08-21 13:49:41 +02:00
|
|
|
const activeElement = SelectorEngine.findOne(Selector.ACTIVE, rootElement)
|
2015-05-11 04:45:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (activeElement) {
|
2017-08-21 13:49:41 +02:00
|
|
|
activeElement.classList.remove(ClassName.ACTIVE)
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (triggerChangeEvent) {
|
|
|
|
if (input.hasAttribute('disabled') ||
|
|
|
|
rootElement.hasAttribute('disabled') ||
|
|
|
|
input.classList.contains('disabled') ||
|
|
|
|
rootElement.classList.contains('disabled')) {
|
|
|
|
return
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
2018-09-25 20:06:09 +02:00
|
|
|
|
|
|
|
input.checked = !this._element.classList.contains(ClassName.ACTIVE)
|
2017-08-21 13:49:41 +02:00
|
|
|
EventHandler.trigger(input, 'change')
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
input.focus()
|
|
|
|
addAriaPressed = false
|
2017-04-10 15:43:54 +02:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2016-11-06 14:48:55 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (addAriaPressed) {
|
|
|
|
this._element.setAttribute('aria-pressed',
|
|
|
|
!this._element.classList.contains(ClassName.ACTIVE))
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (triggerChangeEvent) {
|
2017-08-21 13:49:41 +02:00
|
|
|
this._element.classList.toggle(ClassName.ACTIVE)
|
2015-05-13 21:48:34 +02:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-13 21:48:34 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
dispose() {
|
2017-08-21 13:49:41 +02:00
|
|
|
Data.removeData(this._element, DATA_KEY)
|
2018-09-26 10:39:01 +02:00
|
|
|
this._element = null
|
|
|
|
}
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Static
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
static _jQueryInterface(config) {
|
|
|
|
return this.each(function () {
|
2017-08-21 13:49:41 +02:00
|
|
|
let data = Data.getData(this, DATA_KEY)
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (!data) {
|
|
|
|
data = new Button(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config === 'toggle') {
|
|
|
|
data[config]()
|
|
|
|
}
|
|
|
|
})
|
2015-05-08 02:07:38 +02:00
|
|
|
}
|
2018-06-24 22:02:03 +02:00
|
|
|
|
|
|
|
static _getInstance(element) {
|
|
|
|
return Data.getData(element, DATA_KEY)
|
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Data Api implementation
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2017-08-21 13:49:41 +02:00
|
|
|
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
|
|
|
|
event.preventDefault()
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2017-08-21 13:49:41 +02:00
|
|
|
let button = event.target
|
|
|
|
if (!button.classList.contains(ClassName.BUTTON)) {
|
|
|
|
button = SelectorEngine.closest(button, Selector.BUTTON)
|
|
|
|
}
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2017-08-21 13:49:41 +02:00
|
|
|
let data = Data.getData(button, DATA_KEY)
|
|
|
|
if (!data) {
|
|
|
|
data = new Button(button)
|
|
|
|
Data.setData(button, DATA_KEY, data)
|
|
|
|
}
|
|
|
|
data.toggle()
|
|
|
|
})
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2017-08-21 13:49:41 +02:00
|
|
|
EventHandler.on(document, Event.FOCUS_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
|
|
|
|
const button = SelectorEngine.closest(event.target, Selector.BUTTON)
|
|
|
|
button.classList.add(ClassName.FOCUS)
|
|
|
|
})
|
|
|
|
|
|
|
|
EventHandler.on(document, Event.BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
|
|
|
|
const button = SelectorEngine.closest(event.target, Selector.BUTTON)
|
|
|
|
button.classList.remove(ClassName.FOCUS)
|
|
|
|
})
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
2017-08-21 13:49:41 +02:00
|
|
|
* add .button to jQuery only if jQuery is present
|
2018-09-26 10:39:01 +02:00
|
|
|
*/
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-03-11 16:18:56 +01:00
|
|
|
if (typeof $ !== 'undefined') {
|
2017-08-21 13:49:41 +02:00
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
$.fn[NAME] = Button._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = Button
|
|
|
|
|
|
|
|
$.fn[NAME].noConflict = () => {
|
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return Button._jQueryInterface
|
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-08 02:07:38 +02:00
|
|
|
|
|
|
|
export default Button
|