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