0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-08 05:54:23 +01:00
Bootstrap/js/src/button.js

196 lines
5.0 KiB
JavaScript
Raw Normal View History

2015-05-07 17:07:38 -07:00
/**
* --------------------------------------------------------------------------
2019-02-13 18:01:40 +02:00
* Bootstrap (v4.3.1): button.js
2015-05-07 17:07:38 -07:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
import { getjQuery } from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import SelectorEngine from './dom/selector-engine'
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2015-05-07 17:07:38 -07:00
2019-02-26 13:20:34 +02:00
const NAME = 'button'
const VERSION = '4.3.1'
const DATA_KEY = 'bs.button'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
2018-09-26 10:39:01 +02:00
const CLASS_NAME_ACTIVE = 'active'
const CLASS_NAME_BUTTON = 'btn'
const CLASS_NAME_DISABLED = 'disabled'
const CLASS_NAME_FOCUS = 'focus'
2018-09-26 10:39:01 +02:00
const SELECTOR_DATA_TOGGLE_CARROT = '[data-toggle^="button"]'
const SELECTOR_DATA_TOGGLE = '[data-toggle="buttons"]'
const SELECTOR_INPUT = 'input:not([type="hidden"])'
const SELECTOR_ACTIVE = '.active'
const SELECTOR_BUTTON = '.btn'
2018-09-26 10:39:01 +02:00
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
const EVENT_FOCUS_DATA_API = `focus${EVENT_KEY}${DATA_API_KEY}`
const EVENT_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
Data.setData(element, DATA_KEY, this)
2015-05-07 17:07:38 -07:00
}
2018-09-26 10:39:01 +02:00
// Getters
static get VERSION() {
return VERSION
2015-05-07 17:07:38 -07:00
}
2018-09-26 10:39:01 +02:00
// Public
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
toggle() {
let triggerChangeEvent = true
let addAriaPressed = true
2017-08-21 14:49:41 +03:00
const rootElement = SelectorEngine.closest(
this._element,
SELECTOR_DATA_TOGGLE
2017-08-21 14:49:41 +03:00
)
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
if (rootElement) {
const input = SelectorEngine.findOne(SELECTOR_INPUT, this._element)
if (input && input.type === 'radio') {
if (input.checked &&
this._element.classList.contains(CLASS_NAME_ACTIVE)) {
triggerChangeEvent = false
} else {
const activeElement = SelectorEngine.findOne(SELECTOR_ACTIVE, rootElement)
if (activeElement) {
activeElement.classList.remove(CLASS_NAME_ACTIVE)
2015-05-07 17:07:38 -07:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
if (triggerChangeEvent) {
if (input.hasAttribute('disabled') ||
rootElement.hasAttribute('disabled') ||
input.classList.contains(CLASS_NAME_DISABLED) ||
rootElement.classList.contains(CLASS_NAME_DISABLED)) {
2018-09-26 10:39:01 +02:00
return
2015-05-07 17:07:38 -07:00
}
input.checked = !this._element.classList.contains(CLASS_NAME_ACTIVE)
2017-08-21 14:49:41 +03:00
EventHandler.trigger(input, 'change')
2015-05-07 17:07:38 -07:00
}
2018-09-26 10:39:01 +02:00
input.focus()
addAriaPressed = false
}
2018-09-26 10:39:01 +02:00
}
2018-09-26 10:39:01 +02:00
if (addAriaPressed) {
this._element.setAttribute('aria-pressed',
!this._element.classList.contains(CLASS_NAME_ACTIVE))
2015-05-07 17:07:38 -07:00
}
2018-09-26 10:39:01 +02:00
if (triggerChangeEvent) {
this._element.classList.toggle(CLASS_NAME_ACTIVE)
2015-05-13 12:48:34 -07:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-13 12:48:34 -07:00
2018-09-26 10:39:01 +02:00
dispose() {
2017-08-21 14:49:41 +03:00
Data.removeData(this._element, DATA_KEY)
2018-09-26 10:39:01 +02:00
this._element = null
}
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
// Static
2015-05-07 17:07:38 -07:00
2019-07-28 15:24:46 +02:00
static jQueryInterface(config) {
2018-09-26 10:39:01 +02:00
return this.each(function () {
2017-08-21 14:49:41 +03:00
let data = Data.getData(this, DATA_KEY)
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
if (!data) {
data = new Button(this)
}
if (config === 'toggle') {
data[config]()
}
})
2015-05-07 17:07:38 -07:00
}
2019-07-28 15:24:46 +02:00
static getInstance(element) {
return Data.getData(element, DATA_KEY)
}
2018-09-26 10:39:01 +02:00
}
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-07 17:07:38 -07:00
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, event => {
2017-08-21 14:49:41 +03:00
event.preventDefault()
2015-05-07 17:07:38 -07:00
2017-08-21 14:49:41 +03:00
let button = event.target
if (!button.classList.contains(CLASS_NAME_BUTTON)) {
button = SelectorEngine.closest(button, SELECTOR_BUTTON)
2017-08-21 14:49:41 +03:00
}
2015-05-07 17:07:38 -07:00
2017-08-21 14:49:41 +03:00
let data = Data.getData(button, DATA_KEY)
if (!data) {
data = new Button(button)
}
2019-02-26 13:20:34 +02:00
2017-08-21 14:49:41 +03:00
data.toggle()
})
2015-05-07 17:07:38 -07:00
EventHandler.on(document, EVENT_FOCUS_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, event => {
const button = SelectorEngine.closest(event.target, SELECTOR_BUTTON)
2019-03-16 16:10:23 +02:00
if (button) {
button.classList.add(CLASS_NAME_FOCUS)
2019-03-16 16:10:23 +02:00
}
2017-08-21 14:49:41 +03:00
})
EventHandler.on(document, EVENT_BLUR_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, event => {
const button = SelectorEngine.closest(event.target, SELECTOR_BUTTON)
2019-03-16 16:10:23 +02:00
if (button) {
button.classList.remove(CLASS_NAME_FOCUS)
2019-03-16 16:10:23 +02:00
}
2017-08-21 14:49:41 +03:00
})
2015-05-07 17:07:38 -07:00
2019-08-02 15:51:05 +02:00
const $ = getjQuery()
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2017-08-21 14:49:41 +03:00
* add .button to jQuery only if jQuery is present
2018-09-26 10:39:01 +02:00
*/
2019-03-25 11:32:02 +01:00
/* istanbul ignore if */
2019-08-02 15:51:05 +02:00
if ($) {
2019-02-26 13:20:34 +02:00
const JQUERY_NO_CONFLICT = $.fn[NAME]
2019-07-28 15:24:46 +02:00
$.fn[NAME] = Button.jQueryInterface
2019-02-26 13:20:34 +02:00
$.fn[NAME].Constructor = Button
2017-08-21 14:49:41 +03:00
2019-02-26 13:20:34 +02:00
$.fn[NAME].noConflict = () => {
2017-08-21 14:49:41 +03:00
$.fn[NAME] = JQUERY_NO_CONFLICT
2019-07-28 15:24:46 +02:00
return Button.jQueryInterface
2017-08-21 14:49:41 +03:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-07 17:07:38 -07:00
export default Button