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