2015-05-07 20:07:38 -04:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2015-12-08 12:36:08 -05:00
|
|
|
* Bootstrap (v4.0.0-alpha.2): button.js
|
2015-05-07 20:07:38 -04:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
const Button = (($) => {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
const NAME = 'button'
|
2015-12-05 05:19:21 -05:00
|
|
|
const VERSION = '4.0.0-alpha'
|
2015-05-07 20:07:38 -04:00
|
|
|
const DATA_KEY = 'bs.button'
|
2015-05-13 15:48:34 -04:00
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
2015-05-07 20:07:38 -04: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 15:48:34 -04: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-07 20:07:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Button {
|
|
|
|
|
|
|
|
constructor(element) {
|
2015-05-08 01:26:40 -04:00
|
|
|
this._element = element
|
2015-05-07 20:07:38 -04:00
|
|
|
}
|
|
|
|
|
2015-05-10 22:45:38 -04:00
|
|
|
|
|
|
|
// getters
|
|
|
|
|
|
|
|
static get VERSION() {
|
|
|
|
return VERSION
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-07 20:07:38 -04:00
|
|
|
// public
|
|
|
|
|
|
|
|
toggle() {
|
|
|
|
let triggerChangeEvent = true
|
2015-08-18 22:22:46 -04:00
|
|
|
let rootElement = $(this._element).closest(
|
2015-05-07 20:07:38 -04:00
|
|
|
Selector.DATA_TOGGLE
|
|
|
|
)[0]
|
|
|
|
|
|
|
|
if (rootElement) {
|
2015-05-08 01:26:40 -04:00
|
|
|
let input = $(this._element).find(Selector.INPUT)[0]
|
2015-05-07 20:07:38 -04:00
|
|
|
|
|
|
|
if (input) {
|
|
|
|
if (input.type === 'radio') {
|
|
|
|
if (input.checked &&
|
2015-05-08 01:26:40 -04:00
|
|
|
$(this._element).hasClass(ClassName.ACTIVE)) {
|
2015-05-07 20:07:38 -04:00
|
|
|
triggerChangeEvent = false
|
|
|
|
|
|
|
|
} else {
|
|
|
|
let activeElement = $(rootElement).find(Selector.ACTIVE)[0]
|
|
|
|
|
|
|
|
if (activeElement) {
|
|
|
|
$(activeElement).removeClass(ClassName.ACTIVE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (triggerChangeEvent) {
|
2015-05-08 01:26:40 -04:00
|
|
|
input.checked = !$(this._element).hasClass(ClassName.ACTIVE)
|
|
|
|
$(this._element).trigger('change')
|
2015-05-07 20:07:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-05-08 01:26:40 -04:00
|
|
|
this._element.setAttribute('aria-pressed',
|
|
|
|
!$(this._element).hasClass(ClassName.ACTIVE))
|
2015-05-07 20:07:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (triggerChangeEvent) {
|
2015-05-08 01:26:40 -04:00
|
|
|
$(this._element).toggleClass(ClassName.ACTIVE)
|
2015-05-07 20:07:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 15:48:34 -04:00
|
|
|
dispose() {
|
|
|
|
$.removeData(this._element, DATA_KEY)
|
|
|
|
this._element = null
|
|
|
|
}
|
|
|
|
|
2015-05-07 20:07:38 -04: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-18 22:22:46 -04:00
|
|
|
.on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
|
2015-05-07 20:07:38 -04:00
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
let button = event.target
|
|
|
|
|
|
|
|
if (!$(button).hasClass(ClassName.BUTTON)) {
|
|
|
|
button = $(button).closest(Selector.BUTTON)
|
|
|
|
}
|
|
|
|
|
|
|
|
Button._jQueryInterface.call($(button), 'toggle')
|
|
|
|
})
|
2015-08-18 22:22:46 -04:00
|
|
|
.on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
|
|
|
|
let button = $(event.target).closest(Selector.BUTTON)[0]
|
2015-05-07 20:07:38 -04: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
|