2015-05-07 17:07:38 -07:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2023-03-22 09:12:33 +02:00
|
|
|
* Bootstrap button.js
|
2020-06-16 21:41:47 +03:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2015-05-07 17:07:38 -07:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2022-10-26 08:26:51 +03:00
|
|
|
import { defineJQueryPlugin } from './util/index.js'
|
|
|
|
import EventHandler from './dom/event-handler.js'
|
|
|
|
import BaseComponent from './base-component.js'
|
2018-11-14 10:16:56 +01:00
|
|
|
|
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 DATA_KEY = 'bs.button'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
const CLASS_NAME_ACTIVE = 'active'
|
2020-07-22 22:33:11 +03:00
|
|
|
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="button"]'
|
2020-03-07 11:31:42 +02:00
|
|
|
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-13 15:19:28 +03:00
|
|
|
* Class definition
|
2018-09-26 10:39:01 +02:00
|
|
|
*/
|
|
|
|
|
2019-09-04 17:58:29 +03:00
|
|
|
class Button extends BaseComponent {
|
2018-09-26 10:39:01 +02:00
|
|
|
// Getters
|
2021-05-11 10:49:30 +03:00
|
|
|
static get NAME() {
|
|
|
|
return NAME
|
2019-09-04 17:58:29 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Public
|
|
|
|
toggle() {
|
2020-05-02 11:11:24 +02:00
|
|
|
// 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 12:48:34 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Static
|
2019-07-28 15:24:46 +02:00
|
|
|
static jQueryInterface(config) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return this.each(function () {
|
2021-06-03 18:53:27 +03:00
|
|
|
const data = Button.getOrCreateInstance(this)
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
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
|
|
|
/**
|
2021-10-13 15:19:28 +03:00
|
|
|
* Data API implementation
|
2018-09-26 10:39:01 +02:00
|
|
|
*/
|
2015-05-07 17:07:38 -07:00
|
|
|
|
2020-05-02 11:11:24 +02:00
|
|
|
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, event => {
|
2017-08-21 14:49:41 +03:00
|
|
|
event.preventDefault()
|
2015-05-07 17:07:38 -07:00
|
|
|
|
2020-05-02 11:11:24 +02:00
|
|
|
const button = event.target.closest(SELECTOR_DATA_TOGGLE)
|
2021-06-03 18:53:27 +03:00
|
|
|
const data = Button.getOrCreateInstance(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
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* jQuery
|
|
|
|
*/
|
2020-11-01 14:32:36 +01:00
|
|
|
|
2021-05-11 10:49:30 +03:00
|
|
|
defineJQueryPlugin(Button)
|
2015-05-07 17:07:38 -07:00
|
|
|
|
|
|
|
export default Button
|