0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-26 18:52:18 +01:00
Bootstrap/js/src/button.js

207 lines
6.1 KiB
JavaScript
Raw Normal View History

2015-05-07 17:07:38 -07:00
/**
* --------------------------------------------------------------------------
2020-05-12 19:04:19 +03:00
* Bootstrap (v4.5.0): button.js
2015-05-07 17:07:38 -07:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
import $ from 'jquery'
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
const NAME = 'button'
2020-05-12 19:04:19 +03:00
const VERSION = '4.5.0'
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 JQUERY_NO_CONFLICT = $.fn[NAME]
const CLASS_NAME_ACTIVE = 'active'
const CLASS_NAME_BUTTON = 'btn'
const CLASS_NAME_FOCUS = 'focus'
const SELECTOR_DATA_TOGGLE_CARROT = '[data-toggle^="button"]'
const SELECTOR_DATA_TOGGLES = '[data-toggle="buttons"]'
const SELECTOR_DATA_TOGGLE = '[data-toggle="button"]'
const SELECTOR_DATA_TOGGLES_BUTTONS = '[data-toggle="buttons"] .btn'
const SELECTOR_INPUT = 'input:not([type="hidden"])'
const SELECTOR_ACTIVE = '.active'
const SELECTOR_BUTTON = '.btn'
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
const EVENT_FOCUS_BLUR_DATA_API = `focus${EVENT_KEY}${DATA_API_KEY} ` +
`blur${EVENT_KEY}${DATA_API_KEY}`
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Button {
constructor(element) {
this._element = element
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
const rootElement = $(this._element).closest(
SELECTOR_DATA_TOGGLES
2018-09-26 10:39:01 +02:00
)[0]
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
if (rootElement) {
const input = this._element.querySelector(SELECTOR_INPUT)
2018-09-26 10:39:01 +02:00
if (input) {
if (input.type === 'radio') {
if (input.checked &&
this._element.classList.contains(CLASS_NAME_ACTIVE)) {
2018-09-26 10:39:01 +02:00
triggerChangeEvent = false
} else {
const activeElement = rootElement.querySelector(SELECTOR_ACTIVE)
2018-09-26 10:39:01 +02:00
if (activeElement) {
$(activeElement).removeClass(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 it's not a radio button or checkbox don't add a pointless/invalid checked property to the input
if (input.type === 'checkbox' || input.type === 'radio') {
input.checked = !this._element.classList.contains(CLASS_NAME_ACTIVE)
}
2018-09-26 10:39:01 +02:00
$(input).trigger('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
}
Fix keyboard handling of button-style checkbox/radio button groups (#28834) - adds more defensive checks to make sure no unnecessary toggling happens on disabled buttons; this also fixes an up-to-now undiscovered bug where a toggle button with `.disabled` class would still have its `aria-pressed` toggled - adds a set of explicit tests for the above case of disabled buttons and `aria-pressed` - remove a now irrelevant (or at least very nonsensical) test for `<label>` containing both an actionable and a `hidden` `<input>` - expand the test for disabled checkbox to also explicitly test starting conditions (used mainly in my debugging) - ensure that `$btn[0].click()` is used to click checkboxes in tests, rather than the `click()` on the jquery object which is simply a shorthand for `trigger('click')` and does not actually trigger the browser default behavior - remove the `preventDefault()` from the button handling, which was preventing correct keyboard functionality for checkboxes/radio buttons - add extra logic to the button.js code to handle checkboxes correctly and avoid double-triggering as a result of mouse interactions (which saw the checkboxes being toggled twice, thus returning them to their original state) - add logic that prevents the `checked` property from being added incorrectly for any inputs other than radio buttons and checkboxes - added more tests (including the most basic test for a properly triggered fake checkbox button) - work around Firefox bug #1540995 (which this code was hitting after removing the `preventDefault()`, due to Firefox's incorrect toggling of disabled checkboxes when programmatically (but not manually) activated with a `click()` event
2019-06-05 14:23:25 +01:00
if (!(this._element.hasAttribute('disabled') || this._element.classList.contains('disabled'))) {
if (addAriaPressed) {
this._element.setAttribute('aria-pressed',
!this._element.classList.contains(CLASS_NAME_ACTIVE))
Fix keyboard handling of button-style checkbox/radio button groups (#28834) - adds more defensive checks to make sure no unnecessary toggling happens on disabled buttons; this also fixes an up-to-now undiscovered bug where a toggle button with `.disabled` class would still have its `aria-pressed` toggled - adds a set of explicit tests for the above case of disabled buttons and `aria-pressed` - remove a now irrelevant (or at least very nonsensical) test for `<label>` containing both an actionable and a `hidden` `<input>` - expand the test for disabled checkbox to also explicitly test starting conditions (used mainly in my debugging) - ensure that `$btn[0].click()` is used to click checkboxes in tests, rather than the `click()` on the jquery object which is simply a shorthand for `trigger('click')` and does not actually trigger the browser default behavior - remove the `preventDefault()` from the button handling, which was preventing correct keyboard functionality for checkboxes/radio buttons - add extra logic to the button.js code to handle checkboxes correctly and avoid double-triggering as a result of mouse interactions (which saw the checkboxes being toggled twice, thus returning them to their original state) - add logic that prevents the `checked` property from being added incorrectly for any inputs other than radio buttons and checkboxes - added more tests (including the most basic test for a properly triggered fake checkbox button) - work around Firefox bug #1540995 (which this code was hitting after removing the `preventDefault()`, due to Firefox's incorrect toggling of disabled checkboxes when programmatically (but not manually) activated with a `click()` event
2019-06-05 14:23:25 +01:00
}
2015-05-07 17:07:38 -07:00
Fix keyboard handling of button-style checkbox/radio button groups (#28834) - adds more defensive checks to make sure no unnecessary toggling happens on disabled buttons; this also fixes an up-to-now undiscovered bug where a toggle button with `.disabled` class would still have its `aria-pressed` toggled - adds a set of explicit tests for the above case of disabled buttons and `aria-pressed` - remove a now irrelevant (or at least very nonsensical) test for `<label>` containing both an actionable and a `hidden` `<input>` - expand the test for disabled checkbox to also explicitly test starting conditions (used mainly in my debugging) - ensure that `$btn[0].click()` is used to click checkboxes in tests, rather than the `click()` on the jquery object which is simply a shorthand for `trigger('click')` and does not actually trigger the browser default behavior - remove the `preventDefault()` from the button handling, which was preventing correct keyboard functionality for checkboxes/radio buttons - add extra logic to the button.js code to handle checkboxes correctly and avoid double-triggering as a result of mouse interactions (which saw the checkboxes being toggled twice, thus returning them to their original state) - add logic that prevents the `checked` property from being added incorrectly for any inputs other than radio buttons and checkboxes - added more tests (including the most basic test for a properly triggered fake checkbox button) - work around Firefox bug #1540995 (which this code was hitting after removing the `preventDefault()`, due to Firefox's incorrect toggling of disabled checkboxes when programmatically (but not manually) activated with a `click()` event
2019-06-05 14:23:25 +01:00
if (triggerChangeEvent) {
$(this._element).toggleClass(CLASS_NAME_ACTIVE)
Fix keyboard handling of button-style checkbox/radio button groups (#28834) - adds more defensive checks to make sure no unnecessary toggling happens on disabled buttons; this also fixes an up-to-now undiscovered bug where a toggle button with `.disabled` class would still have its `aria-pressed` toggled - adds a set of explicit tests for the above case of disabled buttons and `aria-pressed` - remove a now irrelevant (or at least very nonsensical) test for `<label>` containing both an actionable and a `hidden` `<input>` - expand the test for disabled checkbox to also explicitly test starting conditions (used mainly in my debugging) - ensure that `$btn[0].click()` is used to click checkboxes in tests, rather than the `click()` on the jquery object which is simply a shorthand for `trigger('click')` and does not actually trigger the browser default behavior - remove the `preventDefault()` from the button handling, which was preventing correct keyboard functionality for checkboxes/radio buttons - add extra logic to the button.js code to handle checkboxes correctly and avoid double-triggering as a result of mouse interactions (which saw the checkboxes being toggled twice, thus returning them to their original state) - add logic that prevents the `checked` property from being added incorrectly for any inputs other than radio buttons and checkboxes - added more tests (including the most basic test for a properly triggered fake checkbox button) - work around Firefox bug #1540995 (which this code was hitting after removing the `preventDefault()`, due to Firefox's incorrect toggling of disabled checkboxes when programmatically (but not manually) activated with a `click()` event
2019-06-05 14:23:25 +01:00
}
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() {
$.removeData(this._element, DATA_KEY)
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
2018-09-26 10:39:01 +02:00
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
if (!data) {
data = new Button(this)
$(this).data(DATA_KEY, data)
}
if (config === 'toggle') {
data[config]()
}
})
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
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
$(document)
.on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, (event) => {
2018-09-26 10:39:01 +02:00
let button = event.target
const initialButton = button
2015-05-07 17:07:38 -07:00
if (!$(button).hasClass(CLASS_NAME_BUTTON)) {
button = $(button).closest(SELECTOR_BUTTON)[0]
2018-09-26 10:39:01 +02:00
}
2015-05-07 17:07:38 -07:00
Fix keyboard handling of button-style checkbox/radio button groups (#28834) - adds more defensive checks to make sure no unnecessary toggling happens on disabled buttons; this also fixes an up-to-now undiscovered bug where a toggle button with `.disabled` class would still have its `aria-pressed` toggled - adds a set of explicit tests for the above case of disabled buttons and `aria-pressed` - remove a now irrelevant (or at least very nonsensical) test for `<label>` containing both an actionable and a `hidden` `<input>` - expand the test for disabled checkbox to also explicitly test starting conditions (used mainly in my debugging) - ensure that `$btn[0].click()` is used to click checkboxes in tests, rather than the `click()` on the jquery object which is simply a shorthand for `trigger('click')` and does not actually trigger the browser default behavior - remove the `preventDefault()` from the button handling, which was preventing correct keyboard functionality for checkboxes/radio buttons - add extra logic to the button.js code to handle checkboxes correctly and avoid double-triggering as a result of mouse interactions (which saw the checkboxes being toggled twice, thus returning them to their original state) - add logic that prevents the `checked` property from being added incorrectly for any inputs other than radio buttons and checkboxes - added more tests (including the most basic test for a properly triggered fake checkbox button) - work around Firefox bug #1540995 (which this code was hitting after removing the `preventDefault()`, due to Firefox's incorrect toggling of disabled checkboxes when programmatically (but not manually) activated with a `click()` event
2019-06-05 14:23:25 +01:00
if (!button || button.hasAttribute('disabled') || button.classList.contains('disabled')) {
event.preventDefault() // work around Firefox bug #1540995
} else {
const inputBtn = button.querySelector(SELECTOR_INPUT)
Fix keyboard handling of button-style checkbox/radio button groups (#28834) - adds more defensive checks to make sure no unnecessary toggling happens on disabled buttons; this also fixes an up-to-now undiscovered bug where a toggle button with `.disabled` class would still have its `aria-pressed` toggled - adds a set of explicit tests for the above case of disabled buttons and `aria-pressed` - remove a now irrelevant (or at least very nonsensical) test for `<label>` containing both an actionable and a `hidden` `<input>` - expand the test for disabled checkbox to also explicitly test starting conditions (used mainly in my debugging) - ensure that `$btn[0].click()` is used to click checkboxes in tests, rather than the `click()` on the jquery object which is simply a shorthand for `trigger('click')` and does not actually trigger the browser default behavior - remove the `preventDefault()` from the button handling, which was preventing correct keyboard functionality for checkboxes/radio buttons - add extra logic to the button.js code to handle checkboxes correctly and avoid double-triggering as a result of mouse interactions (which saw the checkboxes being toggled twice, thus returning them to their original state) - add logic that prevents the `checked` property from being added incorrectly for any inputs other than radio buttons and checkboxes - added more tests (including the most basic test for a properly triggered fake checkbox button) - work around Firefox bug #1540995 (which this code was hitting after removing the `preventDefault()`, due to Firefox's incorrect toggling of disabled checkboxes when programmatically (but not manually) activated with a `click()` event
2019-06-05 14:23:25 +01:00
if (inputBtn && (inputBtn.hasAttribute('disabled') || inputBtn.classList.contains('disabled'))) {
event.preventDefault() // work around Firefox bug #1540995
return
}
if (initialButton.tagName !== 'LABEL' || inputBtn && inputBtn.type !== 'checkbox') {
Button._jQueryInterface.call($(button), 'toggle')
}
Fix keyboard handling of button-style checkbox/radio button groups (#28834) - adds more defensive checks to make sure no unnecessary toggling happens on disabled buttons; this also fixes an up-to-now undiscovered bug where a toggle button with `.disabled` class would still have its `aria-pressed` toggled - adds a set of explicit tests for the above case of disabled buttons and `aria-pressed` - remove a now irrelevant (or at least very nonsensical) test for `<label>` containing both an actionable and a `hidden` `<input>` - expand the test for disabled checkbox to also explicitly test starting conditions (used mainly in my debugging) - ensure that `$btn[0].click()` is used to click checkboxes in tests, rather than the `click()` on the jquery object which is simply a shorthand for `trigger('click')` and does not actually trigger the browser default behavior - remove the `preventDefault()` from the button handling, which was preventing correct keyboard functionality for checkboxes/radio buttons - add extra logic to the button.js code to handle checkboxes correctly and avoid double-triggering as a result of mouse interactions (which saw the checkboxes being toggled twice, thus returning them to their original state) - add logic that prevents the `checked` property from being added incorrectly for any inputs other than radio buttons and checkboxes - added more tests (including the most basic test for a properly triggered fake checkbox button) - work around Firefox bug #1540995 (which this code was hitting after removing the `preventDefault()`, due to Firefox's incorrect toggling of disabled checkboxes when programmatically (but not manually) activated with a `click()` event
2019-06-05 14:23:25 +01:00
}
2018-09-26 10:39:01 +02:00
})
.on(EVENT_FOCUS_BLUR_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, (event) => {
const button = $(event.target).closest(SELECTOR_BUTTON)[0]
$(button).toggleClass(CLASS_NAME_FOCUS, /^focus(in)?$/.test(event.type))
2018-09-26 10:39:01 +02:00
})
2015-05-07 17:07:38 -07:00
$(window).on(EVENT_LOAD_DATA_API, () => {
// ensure correct active class is set to match the controls' actual values/states
// find all checkboxes/readio buttons inside data-toggle groups
let buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLES_BUTTONS))
for (let i = 0, len = buttons.length; i < len; i++) {
const button = buttons[i]
const input = button.querySelector(SELECTOR_INPUT)
if (input.checked || input.hasAttribute('checked')) {
button.classList.add(CLASS_NAME_ACTIVE)
} else {
button.classList.remove(CLASS_NAME_ACTIVE)
}
}
// find all button toggles
buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE))
for (let i = 0, len = buttons.length; i < len; i++) {
const button = buttons[i]
if (button.getAttribute('aria-pressed') === 'true') {
button.classList.add(CLASS_NAME_ACTIVE)
} else {
button.classList.remove(CLASS_NAME_ACTIVE)
}
}
})
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2015-05-07 17:07:38 -07:00
2018-09-26 10:39:01 +02:00
$.fn[NAME] = Button._jQueryInterface
$.fn[NAME].Constructor = Button
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Button._jQueryInterface
}
2015-05-07 17:07:38 -07:00
export default Button