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

177 lines
4.3 KiB
JavaScript
Raw Normal View History

2015-05-08 02:07:38 +02:00
/**
* --------------------------------------------------------------------------
2016-09-05 21:41:44 +02:00
* Bootstrap (v4.0.0-alpha.4): button.js
2015-05-08 02:07:38 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
const Button = (($) => {
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const NAME = 'button'
2016-09-05 21:41:44 +02:00
const VERSION = '4.0.0-alpha.4'
2015-05-08 02:07:38 +02:00
const DATA_KEY = 'bs.button'
2015-05-13 21:48:34 +02:00
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
2015-05-08 02:07:38 +02:00
const JQUERY_NO_CONFLICT = $.fn[NAME]
const ClassName = {
ACTIVE : 'active',
BUTTON : 'btn',
FOCUS : 'focus'
}
const Selector = {
DATA_TOGGLE_CARROT : '[data-toggle^="button"]',
DATA_TOGGLE : '[data-toggle="buttons"]',
INPUT : 'input',
ACTIVE : '.active',
BUTTON : '.btn'
}
const Event = {
2015-05-13 21:48:34 +02:00
CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`,
FOCUS_BLUR_DATA_API : `focus${EVENT_KEY}${DATA_API_KEY} `
+ `blur${EVENT_KEY}${DATA_API_KEY}`
2015-05-08 02:07:38 +02:00
}
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Button {
constructor(element) {
2015-05-08 07:26:40 +02:00
this._element = element
2015-05-08 02:07:38 +02:00
}
// getters
static get VERSION() {
return VERSION
}
2015-05-08 02:07:38 +02:00
// public
toggle() {
let triggerChangeEvent = true
2015-08-19 04:22:46 +02:00
let rootElement = $(this._element).closest(
2015-05-08 02:07:38 +02:00
Selector.DATA_TOGGLE
)[0]
if (rootElement) {
2015-05-08 07:26:40 +02:00
let input = $(this._element).find(Selector.INPUT)[0]
2015-05-08 02:07:38 +02:00
if (input) {
if (input.type === 'radio') {
if (input.checked &&
2015-05-08 07:26:40 +02:00
$(this._element).hasClass(ClassName.ACTIVE)) {
2015-05-08 02:07:38 +02:00
triggerChangeEvent = false
} else {
let activeElement = $(rootElement).find(Selector.ACTIVE)[0]
if (activeElement) {
$(activeElement).removeClass(ClassName.ACTIVE)
}
}
}
if (triggerChangeEvent) {
2015-05-08 07:26:40 +02:00
input.checked = !$(this._element).hasClass(ClassName.ACTIVE)
$(this._element).trigger('change')
2015-05-08 02:07:38 +02:00
}
2016-02-17 16:21:02 +01:00
input.focus()
2015-05-08 02:07:38 +02:00
}
2015-05-08 02:07:38 +02:00
} else {
2015-05-08 07:26:40 +02:00
this._element.setAttribute('aria-pressed',
!$(this._element).hasClass(ClassName.ACTIVE))
2015-05-08 02:07:38 +02:00
}
if (triggerChangeEvent) {
2015-05-08 07:26:40 +02:00
$(this._element).toggleClass(ClassName.ACTIVE)
2015-05-08 02:07:38 +02:00
}
}
2015-05-13 21:48:34 +02:00
dispose() {
$.removeData(this._element, DATA_KEY)
this._element = null
}
2015-05-08 02:07:38 +02:00
// static
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
if (!data) {
data = new Button(this)
$(this).data(DATA_KEY, data)
}
if (config === 'toggle') {
data[config]()
}
})
}
}
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
$(document)
2015-08-19 04:22:46 +02:00
.on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
2015-05-08 02:07:38 +02:00
event.preventDefault()
let button = event.target
if (!$(button).hasClass(ClassName.BUTTON)) {
button = $(button).closest(Selector.BUTTON)
}
Button._jQueryInterface.call($(button), 'toggle')
})
2015-08-19 04:22:46 +02:00
.on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
let button = $(event.target).closest(Selector.BUTTON)[0]
2015-05-08 02:07:38 +02:00
$(button).toggleClass(ClassName.FOCUS, /^focus(in)?$/.test(event.type))
})
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[NAME] = Button._jQueryInterface
$.fn[NAME].Constructor = Button
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Button._jQueryInterface
}
return Button
})(jQuery)
export default Button