0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00
Bootstrap/js/src/button.js

123 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-05-08 02:07:38 +02:00
/**
* --------------------------------------------------------------------------
2020-11-11 18:07:37 +01:00
* Bootstrap (v5.0.0-alpha3): button.js
2020-06-16 20:41:47 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2015-05-08 02:07:38 +02:00
* --------------------------------------------------------------------------
*/
import { getjQuery, onDOMContentLoaded } from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2015-05-08 02:07:38 +02:00
2019-02-26 12:20:34 +01:00
const NAME = 'button'
2020-11-11 18:07:37 +01:00
const VERSION = '5.0.0-alpha3'
2019-02-26 12:20:34 +01:00
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'
2018-09-26 10:39:01 +02:00
const SELECTOR_DATA_TOGGLE = '[data-toggle="button"]'
2018-09-26 10:39:01 +02:00
const EVENT_CLICK_DATA_API = `click${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-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() {
// Toggle class and sync the `aria-pressed` attribute with the return value of the `.toggle()` method
this._element.setAttribute('aria-pressed', this._element.classList.toggle(CLASS_NAME_ACTIVE))
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
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 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
}
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-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
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, event => {
2017-08-21 13:49:41 +02:00
event.preventDefault()
2015-05-08 02:07:38 +02:00
const button = event.target.closest(SELECTOR_DATA_TOGGLE)
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)
}
2019-02-26 12:20:34 +01:00
2017-08-21 13:49:41 +02:00
data.toggle()
})
2015-05-08 02:07:38 +02:00
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-01 14:49:51 +01:00
* add .Button to jQuery only if jQuery is present
2018-09-26 10:39:01 +02:00
*/
onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
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
}
2017-08-21 13:49:41 +02:00
}
})
2015-05-08 02:07:38 +02:00
export default Button